
/* CONSTANTS */

var categories_array=Array(); // placeholder for categories
var thexml='';  // placeholder for XML
var products_array=Array(); // placeholder for products

/* FUNCTIONS */

/* utility functions */
function clearSelect(element){
	try{
 $(element).options.length=0;
	}
	catch(e){
		alert('clearSelect: '+e);
	}
}
function hideElement(element){
 $(element).fade('out');
}
function showElement(element){
 $(element).fade('in');
}
function showLoading(){
 $('loading').fade('in');
}
function hideLoading(){
 $('loading').fade('out');
}
function displayElement(element,state){
	if(state==true){
  $(element).style.display="block";
	}
	else{
		$(element).style.display="none";
	}
}

function addOption(selectbox,text,value ){
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	$(selectbox).options.add(optn);
}
/* data gathering */


function initDrops(xmlpath){
	 getXML(xmlpath);
		$('div_secondary').fade('hide');
		$('div_tertiary').fade('hide');
			$('div_welcome').fade('show');;
}
function getXML(xmlpath){
	var req = new Request({  
		method: 'get',  
		url: xmlpath,  
		onRequest: function() { showLoading(); },  
		onComplete: function(response) { thexml=this.response.xml;getCategories();showPrimary(); }  
	}).send(); 
}

function getCategories(){
	try{
	var categories = thexml.getElementsByTagName("category");
	for(var counter=0;counter<categories.length;counter++){
	 categories_array.push(categories[counter].getAttribute("title"));
	}
	}
	catch(e){
		alert('getCategories: '+e);
	}
}

function getSubCategories(category){
 var subcategories_array=Array();
	var categories = thexml.getElementsByTagName("category");
	for(var counter=0;counter<categories.length;counter++){
	 if(categories[counter].getAttribute("title")==category){
		 var subcategories = categories[counter].getElementsByTagName("subcategory");
			for(var x=0;x<subcategories.length;x++){
		  subcategories_array.push(subcategories[x].getAttribute("title"));
			}
		}
	}
	return subcategories_array;
}

function getProducts(){
 try{
		var category=$('primary').value; // get main category
		var subcategory=$('secondary').value; // get subcategory
		var items_array=Array();
		var categories = thexml.getElementsByTagName("category");
		for(var counter=0;counter<categories.length;counter++){
			if(categories[counter].getAttribute("title")==category){ // this is the current category
				var subcategories = categories[counter].getElementsByTagName("subcategory"); // get all subcategories in this category
				for(var x=0;x<subcategories.length;x++){ // steop through subcategories
					if(subcategories[x].getAttribute("title")==subcategory){
						var items = subcategories[x].getElementsByTagName("item");
						for(var y=0;y<items.length;y++){
						 try{var title=items[y].getAttribute("title");}catch(e){var title="";}
							try{var image=items[y].getElementsByTagName("image")[0].firstChild.nodeValue;}catch(e){var image="";}
							try{var url=items[y].getElementsByTagName("link")[0].firstChild.nodeValue;}catch(e){var url="";}
							try{var sample=items[y].getElementsByTagName("sample")[0].firstChild.nodeValue;}catch(e){var sample="";}
							try{var description=items[y].getElementsByTagName("description")[0].firstChild.nodeValue;}catch(e){var description="";}
							items_array.push({title:title,url:url,sample:sample,description:description,image:image});
						}
					}
				}
			}
		}
		products_array=items_array;
		return items_array;
	}
	catch(e){
	 alert('getProducts: '+e);
	}
}

/* display handling */
function showPrimary(){
 showLoading();
 try{
	 // $('output').innerHTML='';
		clearSelect('primary');
		//$('primary').add(new Option(default_option_primary,''), null);
		addOption('primary',default_option_primary,'');
		for(var i=0;i<categories_array.length;i++){
			//$('primary').add(new Option(categories_array[i], categories_array[i]), null);
			addOption('primary',categories_array[i],categories_array[i]);
		}
		hideLoading();

	}
	catch(e){
	  alert('showPrimary: '+e);
	}
}

function showSecondary(){
	if($('primary').value==''){
		hideElement('div_secondary');
		hideElement('div_tertiary');
		hideElement('div_welcome');
		hideElement(outputfield);
		return false;
	}
 showLoading();
 hideElement('div_tertiary');
 hideElement('div_welcome');
 hideElement(outputfield);
 var subs=getSubCategories($('primary').value);
	showElement('div_secondary');
	try{
		clearSelect('secondary');
		//$('secondary').add(new Option(default_option_secondary,''), null);
		addOption('secondary',default_option_secondary,'');
		for(var i=0;i<subs.length;i++){
			// $('secondary').add(new Option(subs[i], subs[i]), null);
			addOption('secondary',subs[i],subs[i]);
		}
		hideLoading();
	}
	catch(e){
	 alert('showSecondary: '+e);
	}
}

function showTertiary(){
	if($('secondary').value==''){
		hideElement('div_tertiary');
		hideElement(outputfield);
		return false;
	}
	if($('primary').value==''){
		hideElement('div_tertiary');
		return false;
	}
 showLoading();
 hideElement(outputfield);
 var products=getProducts();
	showElement('div_tertiary');
	try{
		clearSelect('tertiary');
		//$('tertiary').add(new Option(default_option_tertiary,''), null);
		addOption('tertiary',default_option_tertiary,'');
		for(var i=0;i<products.length;i++){
			addOption('tertiary',products[i]['title'],products[i]['title']);
			//$('tertiary').add(new Option(products[i]['title'], products[i]['title']), null);
		}
		hideLoading();
	}
	catch(e){
	  alert('showTertiary: '+e);
	}
}

function showHTML(){  // configure output of HTML here
 var html='';
	var show=false;
 var productselected=$('tertiary').value;
	for(var i=0;i<products_array.length;i++){
	 if(products_array[i]['title']==productselected){
		 var title=products_array[i]['title'];
			var url=products_array[i]['url'];
			// if you want to display HTML inline add it to the XML file and uncomment this...
			/*var image=products_array[i]['image'];
			var sample=products_array[i]['sample'];
			var description=products_array[i]['description'];
		 html+='<br><br><strong>Product Recommendation:</strong>';
			html+=' '+title;
			if(image!=""){
			 html+='&nbsp; <img src="'+image+'" width="122" height="30" align="middle">';
			}
			if(url!=""){
			 html+='<br><br><a href="javascript:void(0);" onclick="opener.location=\''+url+'\';self.close()">';
    html+='<img src="images/view.gif" width="164" height="18" border="0"></a>';
				html+='<br><br>';
			}
   if(sample!=""){
				html+='<a href="'+sample+'" target="_blank">';
				html+='<img src="images/pdf.gif" width="34" height="20" border="0" align="center">'; 
				html+=' View Sample Report</a><br>';
			}
			if(description!=""){
			 html+='<blockquote>'+description+'<blockquote>';
			}
			var show=true;*/
			/*$(outputfield).innerHTML=html;
   showElement(outputfield);*/
			document.location.href=url;
		}
	}
	if(show==true){
	 showElement(outputfield);
	}
	else{
	 hideElement(outputfield);
	}
}

function goToProduct(){
 var productselected=$('tertiary').value;
	for(var i=0;i<products_array.length;i++){
	 if(products_array[i]['title']==productselected){
			var url=products_array[i]['url'];
			document.location.href=url;
		}
	}
}


function showTabHTML(){
		hideLoading();
		displayElement('div_primary',false);
		displayElement('div_secondary',false);
		displayElement('div_tertiary',false);
		displayElement('div_welcome',false);
		
	 displayElement(outputfield,true);
	}
	function showDrops(){
	 displayElement(outputfield,false);
		displayElement('div_primary',true);
		displayElement('div_secondary',true);
		displayElement('div_tertiary',true);
		displayElement('div_welcome',true);
		
}

function setTabDisplay(tabid){
	categories_array=Array();
	thexml='';
	products_array=Array();
	for(var i=0;i<htd_tabs.length;i++){
		if(htd_tabs[i]['tabid']==htd_tab_default_selected&&htd_tabsloaded==false){ // first run
			displayElement(htd_tabs[i]['divcontent'],true);
			$(htd_tab_default_selected).className='selected';
			if(tabid!='tab3'){
			showDrops();initDrops(htd_tabs[i]['xmlpath']);
			}
			else{
				hideElement('div_welcome');
				displayElement(outputfield,true);
				showTabHTML();
			}
		}
		else{
			if(htd_tabs[i]['tabid']!=tabid){
				displayElement(htd_tabs[i]['divcontent'],false);
				$(htd_tabs[i]['tabid']).className='';
			}
			else{
				displayElement(htd_tabs[i]['divcontent'],true);
				$(htd_tabs[i]['tabid']).className='selected';
						if(tabid!='tab3'){
			showDrops();initDrops(htd_tabs[i]['xmlpath']);
			}
			else{
				hideElement('div_welcome');
				displayElement(outputfield,true);
				showTabHTML();
					
			}
			}
		}
	}
	
	htd_tabsloaded=true;
}
