// JavaScript Document
/*Revamp with ajax*/
var xmlDoc;
var ntsxmlHttp;
var introDivId = 'introPara';
var testDivId = 'resultsPara';
var selectByThemeDivId = 'selectByThemeDiv';
var selectByTypeDivId = 'selectByTypeDiv';
var clearResultsDivId = 'clearResultsDiv';
var themeSelected = 'All Themes';
var typeSelected = 'All Types';
var ntsxmllink = '../nts_asia_resources.xml';
var isSafari = /Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent);
var isOpera = /Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent);
var isMSIE = /MSIE (\d+\.\d+);/.test(navigator.userAgent);
  
if (window.ActiveXObject)
  {// code for IE
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
else if (document.implementation.createDocument)
  {// code for Firefox, Mozilla, Opera, etc.
  xmlDoc=document.implementation.createDocument("","",null);
  //alert(navigator.userAgent);
  
  	if(isSafari||isOpera){
	  ntsxmlHttp=new XMLHttpRequest();
	  ntsxmlHttp.open("GET", ntsxmllink, true);
	  ntsxmlHttp.send(null);
	  ntsxmlHttp.onreadystatechange= function() {
	  	if (ntsxmlhttp.readyState==4)
  			{// 4 = "loaded"
  			if (ntsxmlhttp.status==200)
    			{// 200 = OK
    			 // ...our code here...
				 xmlDoc=ntsxmlHttp.responseXML.documentElement;
    			}
  			else
    			{
    			alert("Problem retrieving XML data");
    			}
  			}
	  } //end ntsxmlHttp.onreadystatechange function()
  	}
  }
else
  {
  alert('Your browser cannot handle this script');
  }

if(!isSafari&&!isOpera){ 
 xmlDoc.async=false;
 xmlDoc.load(ntsxmllink);
}
var xroot=xmlDoc.getElementsByTagName("resource");
var nts_asia_resource = new Array();
var nts_asia_resources_count = xroot.length;
var res_types = new Array();
var res_themes = new Array();
var res_texts = new Array();
for(dr_i=0;dr_i<nts_asia_resources_count;dr_i++){
	nts_asia_resource[dr_i] = new Array();
	nts_asia_resource[dr_i]['name']=xmlDoc.getElementsByTagName("name")[dr_i].childNodes[0].nodeValue;
	nts_asia_resource[dr_i]['theme']=xmlDoc.getElementsByTagName("theme")[dr_i].childNodes[0].nodeValue;
	nts_asia_resource[dr_i]['type']=xmlDoc.getElementsByTagName("type")[dr_i].childNodes[0].nodeValue;
	nts_asia_resource[dr_i]['sub_type']=xmlDoc.getElementsByTagName("sub_type")[dr_i].childNodes[0].nodeValue;
	nts_asia_resource[dr_i]['description']=xmlDoc.getElementsByTagName("description")[dr_i].childNodes[0].nodeValue;
	nts_asia_resource[dr_i]['source']=xmlDoc.getElementsByTagName("source")[dr_i].childNodes[0].nodeValue;
	nts_asia_resource[dr_i]['html_link']=xmlDoc.getElementsByTagName("html_link")[dr_i].childNodes[0].nodeValue;
	nts_asia_resource[dr_i]['image']=xmlDoc.getElementsByTagName("image")[dr_i].childNodes[0].nodeValue;
	nts_asia_resource[dr_i]['date']=xmlDoc.getElementsByTagName("date")[dr_i].childNodes[0].nodeValue;
}
function selectType(){
	var resTypesDiv = document.getElementById('resTypes');
	var resTypeSelected = resTypesDiv.options[resTypesDiv.selectedIndex].value;
	//alert(resTypeSelected);
	typeSelected = resTypeSelected;
	getContentResults();
}
function getTypes(){
	for(dr_i=0;dr_i<nts_asia_resources_count;dr_i++){
		var i_type = nts_asia_resource[dr_i]['type'];
		var i_sub_type = nts_asia_resource[dr_i]['sub_type'];
		if(i_type!=''){
			var i_types = new Array();
			/*loop through and update types array*/
			i_types[0]='';
			if(i_sub_type!=''){i_types[0]=i_type+" / "+i_sub_type;}
			else{i_types[0]=i_type;}
			var cat_match = false;
			for(x=0;x<i_types.length;x++){
				for(y=0;y<res_types.length;y++){
					if (res_types[y]==i_types[x]){
						cat_match = true;
						break;
					}
				}
				if(cat_match!=true){
					res_types.push(i_types[x]);
				}
			}
			/*end - loop through and update types array*/
		}//end - if
	}//end for
	res_types.sort();
	var optionValues ='';
	var checkBoxOrValues = '';
	var checkBoxAndValues = '';
	optionValues ="<option value=\"All Types\">- All Types -</option>";
	for(j=0;j<res_types.length;j++){
		var typeValue = res_types[j];
		optionValues +="<option value=\""+typeValue+"\">"+typeValue+"</option>";
		//checkBoxOrValues +=catValue+" :<input type=\"checkbox\" name=\"checkOr\" value=\""+catValue+"\"><br/>";
		//checkBoxAndValues +=catValue+" :<input type=\"checkbox\" name=\"checkAnd\" value=\""+catValue+"\"><br/>";
	}
	optionValues = 	"<select id=\"resTypes\" name=\"resTypes\" onchange=\"selectType()\">"+optionValues+"</select>";
	document.getElementById(selectByTypeDivId).innerHTML = optionValues;
	
}
function selectTheme(){
	var resThemesDiv = document.getElementById('resThemes');
	var resThemeSelected = resThemesDiv.options[resThemesDiv.selectedIndex].value;
	//alert(resThemeSelected);
	themeSelected = resThemeSelected;
	getContentResults();
}
function getThemes(){
	for(dr_i=0;dr_i<nts_asia_resources_count;dr_i++){
		var i_theme = nts_asia_resource[dr_i]['theme'];
		//testing only
		//var i_text = nts_asia_resource[dr_i]['description'];
		//	res_texts.push(i_text);
		//end testing only	
		if(i_theme!=''){
			var i_themes = new Array();
			var patt_slash = new RegExp("\u002F");
			var slash_present = false;
			slash_present = patt_slash.test(i_theme);
			/*capture (all) the theme(s)*/
			if(slash_present){
				i_themes = i_theme.split(patt_slash);
			}
			else{
				i_themes[0]=i_theme;
			}
			/*end - capture (all) the theme(s)*/
			/*loop through and update themes array*/
			for(x=0;x<i_themes.length;x++){
				var cat_match = false;
				for(y=0;y<res_themes.length;y++){
					if (res_themes[y]==i_themes[x]){
						cat_match = true;
						break;
					}
				}
				if(cat_match!=true){
					res_themes.push(i_themes[x]);
				}
			}
			/*end - loop through and update themes array*/
		}//end - if
	}//end - for
	/*sort themes*/
	res_themes.sort();
	/*end - sort themes*/
	var optionValues ='';
	var checkBoxOrValues = '';
	var checkBoxAndValues = '';
	optionValues ="<option value=\"All Themes\">- All Themes -</option>";
	for(j=0;j<res_themes.length;j++){
		var themeValue = res_themes[j];
		optionValues +="<option value=\""+themeValue+"\">"+themeValue+"</option>";
		//checkBoxOrValues +=catValue+" :<input type=\"checkbox\" name=\"checkOr\" value=\""+catValue+"\"><br/>";
		//checkBoxAndValues +=catValue+" :<input type=\"checkbox\" name=\"checkAnd\" value=\""+catValue+"\"><br/>";
	}
	optionValues = 	"<select id=\"resThemes\" name=\"resThemes\" onchange=\"selectTheme()\">"+optionValues+"</select>";
	//testing only
	//var textValues='';
	//for(j=0;j<res_texts.length;j++){
	//	var textValue = res_texts[j];
	//	textValues +=textValue;
	//}
	//document.getElementById(testDivId).innerHTML = textValues;
	//end testing only	
	
	//alert(optionValues);
	document.getElementById(selectByThemeDivId).innerHTML = optionValues;
	document.getElementById(clearResultsDivId).innerHTML = "<a href='#' onclick='clearContentResults()'>Clear Results</a>";
	getTypes();
	//getContentResults();
	//document.getElementById("res_Or_div").innerHTML = '<form id=\"res_Or_form\">'+checkBoxOrValues+'</form>';
	//document.getElementById("res_And_div").innerHTML = '<form id=\"res_And_form\">'+checkBoxAndValues+'</form>';
	// for initialization
	//type_selected[0]='Our Resources';
	//catSelect('All',0,0); 
}//end - getThemes();

function getContentResults(){
	var div_display_results = testDivId;
	var display_results = "";
	var dr_name;
	var dr_theme;
	var dr_type;
	var dr_sub_type;
	var dr_description;
	var dr_source;
	var dr_html_link;
	var dr_image;
	var dr_date;
	
	/*type
	*/
	for(dr_i=0;dr_i<nts_asia_resources_count;dr_i++){
		dr_name = nts_asia_resource[dr_i]['name'];
		dr_theme = nts_asia_resource[dr_i]['theme'];
		dr_type = nts_asia_resource[dr_i]['type'];
		dr_sub_type = nts_asia_resource[dr_i]['sub_type'];
		dr_description = nts_asia_resource[dr_i]['description'];
		dr_source = nts_asia_resource[dr_i]['source']
		dr_html_link = nts_asia_resource[dr_i]['html_link'];
		dr_image = nts_asia_resource[dr_i]['image'];
		dr_date = nts_asia_resource[dr_i]['date'];
		
		/*Conditional Checkings!!*/
		if(themeSelected!='All Themes'){
			var str = dr_theme;
			var patt1 = new RegExp(themeSelected);
			var result = patt1.test(str);
			if(!result){
				continue;
			}
		}
		if(typeSelected!='All Types'){
			var dr_test_type = '';
			if(dr_sub_type!=''){dr_test_type = dr_type+" / "+dr_sub_type;}
			else{dr_test_type = dr_type;}
			if(dr_test_type!=typeSelected){
				continue;
			}
		}
		
		if(dr_image!=''){
			dr_image = "<img class=\"\" style=\"border:1px solid #ccc;\" src=\""+dr_image+"\" alt=\"\" />";
		}
		
		if(dr_html_link!=''){
			dr_name = "<a href='"+dr_html_link+"' target='_blank'>"+dr_name+"</a>";
		}
		display_results += "<table style=''><tr><td><p class='namep' style='text-align:left;'><b>"+dr_name+"</b></p> "+dr_description+"<span style='margin-left:10px;'>"+dr_source+"</span> <span style='color: #999966;'>"+dr_date+"</span>"+
						   "<br/><span style='color: red;margin-left:10px;'>"+dr_theme+"</span><br/><span style='color: #009900;margin-left:10px;'> "+dr_type+"</span><span style='color: #006600;margin-left:10px;'> "+dr_sub_type+"</span></p></td><td style='vertical-align:top'>"+dr_image+"</td></tr></table>";
	}// end for loop
	if(display_results==''){display_results="<table style=''><tr><td><p class='jus'>No Results Available</p></td></tr></table>";}
	document.getElementById(div_display_results).innerHTML = display_results;
}
function clearContentResults(){
	getThemes();
	var div_display_results = testDivId;
	document.getElementById(div_display_results).innerHTML = '';
}
function clearContentResultsType(){
	getTypes();
	var div_display_results = testDivId;
	document.getElementById(div_display_results).innerHTML = '';
}
