String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

//Global Variables 
var wait_msg = 'Loading, please wait...';
var PgmLists,LocListXml,gsponsor; //to hold the xml in local memory 
var restore_level=0;
var strMajor='Please select a subject area';
var strSubMajor='Please select a program';
var strDegree='Please select a degree';
var pid=1; // promotion id, increment this every time you promote this file to handle caching problem in IE 

// ****** UI event handlers start 
function EvtSubject(sponsor){ //When Subject area List clicked 
	var majors = GetSelectedOptionsList('major'); //selected major list
	palert('');	
	ClearList('sub_major',strMajor);
	ClearList('degree',strSubMajor);
	ClearList('location',strDegree);
	
	if (majors.length>1 && majors.substring(0,1)==',') {
		palert('You can not have "All Subjects" and also select a subject',1);
		document.getElementById('major')[0].selected=false;
		majors = majors.substring(1);
	}
 
	if (majors.trim()!=''){   
		ClearList('sub_major',wait_msg); setSrchBreadCrumbColor(1);	
		// Create Ajax object 
		var obj_Ajax=new PcomAjax();  
		obj_Ajax.load('submajor.asp?sponsor='+sponsor+'&reqtype=getprograms&subjarea=' + majors.substring(0,(majors.length-1))+'&pid='+pid, LoadPrograms);
	}else{setSrchBreadCrumbColor(0);document.getElementById('countbox').style.display='none';}
}

function EvtSubMajor(sponsor){ //When School Programs List clicked 
 	var cnt = 0; //Count validation 
 	var sub_major=document.getElementById('sub_major');
	ClearList('degree',strSubMajor);
	ClearList('location',strDegree);
	
	if (sub_major.length==1 && document.getElementById('sub_major')[0].text==strMajor){
		ClearList('sub_major',strMajor); return null;
	}
	
    for (var i = 0; i < sub_major.length; i++) { if (sub_major[i].selected) cnt++; }

	if ( cnt > 30)	{	
 		palert('Please limit your selection to 30 programs or less.  You currently have chosen ' + cnt + ' programs.',2);
 		document.progsearch.sub_major.focus();
 		return null;
 	} //count validation ends

	var submajors = GetSelectedOptionsList('sub_major'); //selected Major List
	palert('');
		
	if (submajors.length>1 && submajors.substring(0,1)==',') {
		palert('You can not have "No Preference" and also select a program',2);
		document.getElementById('sub_major')[0].selected=false;
		submajors = submajors.substring(1);
	}
	
	if (submajors.trim()!=''){ 
		ShowCount(GetTotalCount(PgmLists,submajors));
		ClearList('degree',wait_msg);	setSrchBreadCrumbColor(2);	
		var majors = GetSelectedOptionsList('major'); //selected major list
		// Create Ajax object 
		var obj_Ajax=new PcomAjax();
		obj_Ajax.load('submajor.asp?sponsor='+sponsor+'&reqtype=getawards&subjarea=' + majors.substring(0,(majors.length-1)) + '&submajor=' + submajors.substring(0,(submajors.length-1))+'&pid='+pid, LoadAwdLevels);
	}else{setSrchBreadCrumbColor(1);ShowCount(GetTotalCount(PgmLists,''));}
	
}

function EvtDegree(sponsor){ //When degree/award List clicked 
	var awardlist = GetSelectedOptionsList('degree'); //selected Degree List
	ClearList('location',strDegree);
	if (awardlist.length==1){ClearList('degree',strSubMajor); return null;} 
	
	palert('');
	if (awardlist.indexOf('undecided')>0) {
		palert('You can not have "Undecided" and also an award level selected',3);
		var d=document.getElementById('degree');
		d[d.length-1].selected=false;
		awardlist = GetSelectedOptionsList('degree');
	}
	
	var submajors = GetSelectedOptionsList('sub_major'); //selected sub Major List
	if (awardlist.trim()!=''){
		ClearList('location',wait_msg);setSrchBreadCrumbColor(3);		
		var majors = GetSelectedOptionsList('major'); //selected major list
		
		var obj_Ajax=new PcomAjax(); // Create Ajax object 
		obj_Ajax.load('submajor.asp?sponsor='+sponsor+'&reqtype=getlocations&subjarea=' + majors.substring(0,(majors.length-1)) + '&submajor=' + submajors.substring(0,(submajors.length-1)) + '&degree=' + awardlist.substring(0,(awardlist.length-1))+'&pid='+pid, LoadLocations) ;
	}else{setSrchBreadCrumbColor(2);ShowCount(GetTotalCount(PgmLists,submajors));}
}

function EvtLocation(){ //When Location List clicked 
	var locations = GetSelectedOptionsList('location'); //selected locations list 
	//alert(locations);
	if (locations.length==1 && document.getElementById('location')[0].text==strDegree){
		ClearList('location',strDegree);  return null;
	}
	
	palert('');	
	if (locations.length>1 && locations.substring(0,1)==',') {
		palert('You can not have "All Locations" with other selected locations',4);
		document.getElementById('location')[0].selected=false;
		locations = locations.substring(1);
	}
	
	if (locations.length>1)
	{    
	     var countrylist= "";
	     var statelist= "";
	     var locationlist =locations.split(','); 
	     for (var i=0;i<locationlist.length-1; i++)
	        {
	           if (locationlist[i].length>2)
	           { 
	                countrylist+= locationlist[i] + ",";
	            }
	            else
	            {
	                statelist += locationlist[i] + ",";
	            }
	        }//for
	        
	            if (countrylist.length > 0)
	            {
	              locations = countrylist;
	            }
	            else 
	            {
	             locations = statelist;
	            }
	}
	
		      
	if (locations.trim()!=''){
		document.getElementById('locationsel').value = locations; 
		ShowCount(GetTotalCount(LocListXml,locations)); setSrchBreadCrumbColor(4);
	}
	else{setSrchBreadCrumbColor(3);ShowCount(LocListXml.getElementsByTagName('Data')[0].attributes[1].value);}
}

function evtkwcheck(t){// when keyword search check box clicked 
	if (t.checked){
		palert('');	
		document.getElementById('namecheck').checked=false;
		document.getElementById('pgmcheck').checked=false;
		document.getElementById('anyQuery').type='text';
		hide(document.getElementById('instName'));
		hide(document.getElementById('progName'));
		show(document.getElementById('anyQuery'));
	}
}
function evtcheck(){// when program/ name search check box clicked 
	var ckw =document.getElementById('kwcheck');
	var cnm=document.getElementById('namecheck');
	var cpg=document.getElementById('pgmcheck');
	var txtpgm=document.getElementById('progName');
	var txtinst=document.getElementById('instName');
	palert('');	
	
	//hide keyword search 
	document.getElementById('kwcheck').checked=false;
	hide(document.getElementById('anyQuery'));
	txtpgm.style.hide;
	if (cpg.checked && cnm.checked){
		txtpgm.size=25;txtinst.size=25; 
		show(txtpgm);
		show(txtinst);
		
	}else if(cpg.checked){
		hide(txtinst);
		show(txtpgm);
		txtpgm.size=txtinst.size=55; 
	}else { 
	
		show(txtinst);
		hide(txtpgm);
		txtpgm.size=txtinst.size=55; 
	}
}
// ****** UI event handlers end 


// ****** functions to populate list objects dynamically from xml start ******* 
 
function LoadPrograms(xmldoc){ //Loads Programs list with given xml doc 
	ClearList("sub_major");
	var pgmList = document.getElementById('sub_major');
	var dNodes = xmldoc.getElementsByTagName('Data');
	FillList(pgmList,dNodes);
	//if(dNodes.length>0) ShowCount(dNodes[0].attributes[1].value);
	if(dNodes.length>0) ShowCount(GetTotalCount(xmldoc,''));
	
	PgmLists = xmldoc;
	
	if (restore_level>0){
		SelectList('sub_major',document.getElementById('sub_majorsel').value);
		restore_level--;
		if (restore_level>0) EvtSubMajor(gsponsor); //do awards level loading 
	}
}

function LoadAwdLevels(xmldoc) {//Loads awards list box with given xml doc

	var degreeList = document.getElementById('degree');
	var deg_chg_evt = degreeList.getAttribute('onchange');

	ClearList('location',strDegree);
	ClearList('degree');

	var dNodes = xmldoc.getElementsByTagName('Data');
	FillList(degreeList,dNodes); 
	
	if (restore_level>0){
		SelectList('degree',document.getElementById('degreesel').value);
		restore_level--;
		if (restore_level>0) EvtDegree(gsponsor); //do locations level loading 
	}
		
}

function LoadLocations(xmldoc){ //Loads location list box with given xml doc

	ClearList('location');
	var locList = document.getElementById('location');	
	var dNodes = xmldoc.getElementsByTagName('Data');
	FillList(locList,dNodes);
	if(dNodes.length>0) ShowCount(dNodes[0].attributes[1].value);
	
	LocListXml=xmldoc;
	if (restore_level>0){
		SelectList('location',document.getElementById('locationsel').value);
		restore_level--;
	}
}

// ****** functions to populate list objects dynamically from xml end *******

function restoreState(sponsor){//To restore state of ajax dyanamic data for IE 
	gsponsor=sponsor;
	setSrchBreadCrumbColor(0);
	ClearList('sub_major',strMajor);
	ClearList('degree',strSubMajor);
	ClearList('location',strDegree);

	if (document.getElementById('majorsel').value != '') restore_level=1;
	if (document.getElementById('sub_majorsel').value != '') restore_level=2; 
	if (document.getElementById('degreesel').value != '') restore_level=3;  
	if (document.getElementById('locationsel').value != '') restore_level=4;
	if  (restore_level > 0)  EvtSubject(gsponsor);
	
	//name/kw/pgm search section state restore
	//if (document.getElementById('kwcheck').checked){
//		evtkwcheck(document.getElementById('kwcheck'));
//	}else{evtcheck();}

}	
function setSrchBreadCrumbColor(index){
	var obj=document.getElementById('ilist').childNodes;
	for (var i=0; i<obj.length;i++) 
	 if (index==i) obj[i].style.color='red';
	 else obj[i].style.color='gray';
}

function GetTotalCount(xmldoc,csvlist){ // return the count attribute total of a given xml 
	var xmlNodes = xmldoc.getElementsByTagName('Data');
	var cnt=0;
	var list=csvlist.split(',');
	
	if (list.length-1 > 0){
		for (var j=0; j<xmlNodes.length; j++) {
			for (var i=0;i<list.length-1; i++){
				if (xmlNodes[j].attributes[0].value==list[i]){
					cnt+=parseInt(xmlNodes[j].attributes[1].value);
				}
			}
		}
	}else{
		for (var j=0; j<xmlNodes.length; j++) {
			cnt+=parseInt(xmlNodes[j].attributes[1].value);
		}
	}
	return cnt;
}

function ShowCount(cnt){
	document.getElementById('countbox').style.display="block";
	document.getElementById('schoolcount').innerHTML=cnt;
}

function hide(t){//to hide an object 
	try{t.style.display='none';
	}catch(e){
		try{
			t.style.visibility="hidden";
		}catch(f){}
	}
}
function show(t){ // to show an object 
	try{t.style.display='inline';
	}catch(e){
		try{
			t.style.visibility="visible;";
		}catch(f){}
	}
}

function validate_and_submit(sponsor)
{
	var cnm=document.getElementById('namecheck');
	var cpg=document.getElementById('pgmcheck');
	var ckw =document.getElementById('kwcheck');
	var spgm=document.Search.progName.value.trim();
	var sname=document.Search.instName.value.trim();
	var sanyqry=document.Search.anyQuery.value.trim();
	var good_to_submit=true;
	
	 if ((cpg.checked)||(cnm.checked))//check if either pgm or name search checked
	{
	            var saction="InstSearchResults.asp?searchtype=namesearch&sponsor="+sponsor
	            
	            if ((cpg.checked)&&(cnm.checked))////both pgm and name search checked
	            {
			                if((sname=='Please type school name')||(sname==''))
			                {
				                palert('Please enter all or part of school name',5);
				                document.Search.instName.focus();
				                good_to_submit=false;
			                }
			               else if((spgm=='Please type program name')||(spgm==''))
			                {
				                palert('Please enter all or part of program name',5);
				                document.Search.progName.focus();
				                good_to_submit=false;
			                }
                            if(good_to_submit){
                            saction+="&instName="+escape(sname);
                            saction+="&progName="+escape(spgm); 
                            
                            }
	            
	            }//end if both pgm and name search checked
	            
	 else if(cpg.checked)//just pgm checked
	            {
	                   document.Search.pgmcheck.focus();
			                if((spgm=='Please type program name')||(spgm==''))
			                {
				                palert('Please enter all or part of program name',5);
				                document.Search.progName.focus();
				                good_to_submit=false;
			                }else
			                {
			                    saction+="&instName="
				                saction+="&progName="+escape(spgm); 
			                }
		        }//end else ifjust pgm checked

	            
	 else//name checked
	            {
	                    if((sname=='Please type school name')||(sname==''))
			                {
				                palert('Please enter all or part of school name',5);
				                document.Search.instName.focus();
				                good_to_submit=false;
			                }
			            else
			                {
				                
				                saction+="&instName="+escape(sname)+"&progName=";
			                }
	            }//end else just name chekced

		        document.Search.action=saction;
		        
    }// endof if part of either pgm or name searchchecked
    
    else if (ckw.checked)///else part 
    {
                 
		        if((sanyqry=='Please type keyword')||(sanyqry==''))
		        {
			        palert('Please enter a keyword or phrase',5);
			        document.Search.anyQuery.focus();
			        good_to_submit=false;
		        }else{document.Search.kwcheck.focus();
			        document.Search.action = "/fts/code/smplrslt.asp?sponsor="+sponsor;
		        }
		
	}
	
	else
	{
	    {palert('Please choose your search type',5);good_to_submit=false;}
	}
    
	if (good_to_submit){
	//alert('part1');
	document.Search.submit()};

}//end of function
	


function checkform1(sponsor)
{	
	var check_flag = true;
	var a='Please select a program';
	var b='Please select at least one degree';
	var d=strMajor; 
	var c='Please select a location';
	var sub_major=document.getElementById('sub_major');
    var Degree=document.getElementById('degree');
    var location = document.getElementById('location');
    var major = document.getElementById('major');
    var cnt=0;
	
    if (major.selectedIndex < 0 ){
		palert(d,1);
		document.progsearch.major.focus();
		check_flag=false;
    }

    for (var i = 0; i < sub_major.length; i++) {
        if (sub_major[i].selected) cnt++;
    }
	if ( cnt > 30)	{	
 		palert('Please limit your selection to 30 programs or less.  You currently have chosen ' + cnt + ' programs.',2);
 		document.progsearch.sub_major.focus();
 		check_flag=false;
 	} //count validation ends

 		
	if ( sub_major.selectedIndex < 0 && check_flag){	
		palert(a,2);
		document.progsearch.sub_major.focus();
		check_flag = false;
	}

	if ( Degree.selectedIndex < 0 && check_flag) {
		palert(b,3);
		document.progsearch.degree.focus();
		check_flag = false;
	}
 
	if (location.selectedIndex < 0 && check_flag) { 
		palert(c,4);
		document.progsearch.location.focus();
		check_flag = false;
	}
	cnt=0;
	try {
		 cnt = parseInt(document.getElementById('schoolcount').innerHTML);
	}catch (e){	cnt=0; }
	
	if (check_flag && (cnt>10000)) {
		check_flag=false;
		palert("Please refine your search to 10,000 results or less.",4);
		
	}
	if (check_flag){
	    
		Degreelist=GetSelectedOptionsList('degree').trim();  Degreelist.substring(1,(Degreelist.length-2));
		Locationlist=GetSelectedOptionsList('location').trim(); Locationlist.substring(1,(Locationlist.length-2))
		document.progsearch.action = "AcdSearchResults.asp";
		document.progsearch.submit();				
	}
	return check_flag;
}

function palert(a,id){
	var i='';
	if (typeof(id) != 'undefined') i=id;
	var err=document.getElementById('errmsg'+i);
	for (var j=1; j<5; j++) document.getElementById('errmsg'+j).style.display='none';
	if (a!='') {
		err.innerHTML=a;
		err.style.display='block';
	}
}

function keypressHandler1(DnEvents,sponsor)
{	K = navigator.appName=="Netscape" ? DnEvents.which : DnEvents.keyCode
	if (K==13) checkform1(sponsor)
}

function keypressHandler3(DnEvents,sponsor)
{	
	K = navigator.appName=="Netscape" ? DnEvents.which : DnEvents.keyCode
	if (K==13) validate_and_submit(sponsor)
}

