﻿// JScript File

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

//Global Variables 
var wait_msg = 'Loading, please wait...';
var PgmLists,LocListXml,DegreeListXml,StudyListXml,TuitionListXml,gsponsor,count; //to hold the xml in local memory 
var restore_level=0;
var strProgram='Please Select a Program';
var strDegree='Please Select a Degree';
var strLocation='Please Select a Location';
var strStudy='Select a Study Option';
var strHomeLocation='Select Your Home Location';
var strTuition='Please Select a Tuition Level';
var pid=1; // promotion id, increment this every time you promote this file to handle caching problem in IE 
var locationcnt = new Array();

// ****** UI event handlers start 
function EvtProgram(sponsor){ //When Program area List clicked 
	var programs = GetSelectedOptionsList('lstprogram'); //selected Program list
	//alert(programs);
	palert('');	
	ClearList('lstdegree',strProgram);
	ClearList('lstlocation',strDegree);
	ClearList('lststudy_options',strLocation);
	//ClearList('lsthome_location',strStudy);
	//ClearList('lsttuition',strHomeLocation);

	if (programs.length>1 && programs.substring(0,1)==',') {
		palert('You can not have "No Preference" and also select a program',1);
		document.getElementById('lstprogram')[0].selected=false;
		programs = programs.substring(1);
		ClearList('lstdegree');
	}

     count = 0;
     var program_selected = document.getElementById('lstprogram')
       
     for (var i = 0; i < program_selected.length; i++) { if (program_selected[i].selected) count++; }
	 if ( count > 10)	{
 		palert('Please select no more than 10 programs.  You currently have chosen ' + count + ' programs.',1);
 		document.progsearch.lstprogram.focus();
 		return null;
 	 } //count validation ends
     ClearList('lstdegree');

 
	if (programs.trim()!=''){ 
		ClearList('lstdegree',wait_msg); 
		setSrchBreadCrumbColor(1);	
		
		// Create Ajax object 
		var obj_Ajax=new PcomAjax();  
		obj_Ajax.load('ajaxxmldata.asp?sponsor='+sponsor+'&reqtype=getdegrees&pgm='+  programs.substring(0,(programs.length-1)) + '&pid='+pid, LoadDegrees);
	}
	else{
	       setSrchBreadCrumbColor(0);
	       document.getElementById('countbox').style.display='none';
	      }
}
function EvtDegree(sponsor){ //When Degree area List clicked 
	//alert('EvtDegree');	
	var programs = GetSelectedOptionsList('lstprogram'); //selected Program list
	//if (progr
	var degrees = GetSelectedOptionsList('lstdegree'); //selected Degree list
	//alert(degrees);
	palert('');
	ClearList('lstlocation',strDegree);
	ClearList('lststudy_options',strLocation);
	//ClearList('lsthome_location',strStudy);
	//ClearList('lsttuition',strHomeLocation);

 
	if (degrees.trim()!='' && programs.trim() != ''){  
	  ShowCount(GetTotalCount(DegreeListXml,degrees)); 
		ClearList('lstlocation',wait_msg);
		 setSrchBreadCrumbColor(2);		
			// Create Ajax object
		var obj_Ajax=new PcomAjax();  
		obj_Ajax.load('ajaxxmldata.asp?sponsor='+sponsor+'&reqtype=getlocation&degree='+ degrees.substring(0,(degrees.length-1)) + '&pgm=' + programs.substring(0,(programs.length-1)) + '&pid='+pid, LoadLocations);
         }
         else{
               setSrchBreadCrumbColor(1);
               document.getElementById('countbox').style.display='none';
             }
	
	
	

}
function EvtLocation(sponsor){ //When Location area List clicked 
//	alert('EvtLocation');
	//alert(locations);
	palert('');	
	ClearList('lststudy_options',strLocation);
	//ClearList('lsthome_location',strStudy);
	//ClearList('lsttuition',strHomeLocation);
     count = 0;
     var location_selected = document.getElementById('lstlocation');
     for (var i = 0; i < location_selected.length; i++) { if (location_selected[i].selected) count++;}
     if (count > 1 && location_selected.selectedIndex == 0){		
     palert('You can not have "All Locations" with other selected locations',3);
     document.getElementById('lstlocation')[0].selected=false;
     ClearList('lststudy_options',strLocation);
     //return null;
     }
     if ( count > 20)	{
    
 		palert('Please select only 20 locations.  You currently have chosen ' + count + ' locations.',3);
 		document.progsearch.lstlocation.focus();
 		return null;
 	 } //count validation ends
     ClearList('lststudy_options',strLocation);
 	var programs = GetSelectedOptionsList('lstprogram'); //selected Program list
	var degrees = GetSelectedOptionsList('lstdegree'); //selected Degree list
	var locations = GetSelectedOptionsList('lstlocation'); //selected location list

	if (locations.trim()!=''&& degrees.trim()!='' && programs.trim() != ''){   
	     ShowCount(GetTotalCount(LocListXml,locations));
		ClearList('lststudy_options',wait_msg); 
		setSrchBreadCrumbColor(3);	
			// Create Ajax object 
		var obj_Ajax=new PcomAjax();  
		obj_Ajax.load('ajaxxmldata.asp?sponsor='+sponsor+'&reqtype=getstudyopt&location='+ locations.substring(0, (locations.length-1)) + '&degree=' + degrees.substring(0,(degrees.length-1)) + '&pgm=' + programs.substring(0,(programs.length-1)) + '&pid='+pid, LoadStudy);
        }
    else{
        setSrchBreadCrumbColor(2);
        document.getElementById('countbox').style.display='none';
        }

}
function EvtStudy(sponsor){ //When Study options  List clicked 
//alert('EvtStudy');
//	var programs = GetSelectedOptionsList('lstprogram'); //selected Program list
//	var degrees = GetSelectedOptionsList('lstdegree'); //selected Degree list
//	var locations = GetSelectedOptionsList('lstlocation'); //selected locations list
	var studyoptions = GetSelectedOptionsList('lststudy_options'); //selected study options list
	//alert(studyoptions);
	palert('');	
//	//ClearList('lsthome_location',strStudy);
//	ClearList('lsttuition',strHomeLocation);
	

	if (studyoptions.trim()!='' && studyoptions.trim()!=','){   
	    ShowCount(GetTotalCount(StudyListXml,studyoptions))
//		ClearList('lsthome_location',wait_msg); 
		setSrchBreadCrumbColor(4);	
		// Create Ajax object 
//		var obj_Ajax=new PcomAjax();  
//		obj_Ajax.load('ajaxxmldata.asp?sponsor='+sponsor+'&reqtype=gethomelocations&studyoption='+ studyoptions.substring(0,(studyoptions.length-1)) + '&location='+ locations.substring(0, (locations.length-1)) + '&degree=' + degrees.substring(0,(degrees.length-1)) + '&pgm=' + programs.substring(0,(programs.length-1)) + ' &pid='+pid, LoadHomeLocations);
        }
        else{
        setSrchBreadCrumbColor(3);
        document.getElementById('countbox').style.display='none';
        }

}
/*

function EvtHomeLocation(sponsor){ //When Home Location area List clicked 
//alert('EvtHomeLocation');
    var programs = GetSelectedOptionsList('lstprogram'); //selected Program list
	var degrees = GetSelectedOptionsList('lstdegree'); //selected Degree list
	var locations = GetSelectedOptionsList('lstlocation'); //selected locatons list
	var studyoptions = GetSelectedOptionsList('lststudy_options'); //selected study options list
    var homeLocations = GetSelectedOptionsList('lsthome_location'); //selected home location list
	palert('');	
	ClearList('lsttuition',strHomeLocation);
	 count = 0;
     var homelocation_selected = document.getElementById('lsthome_location')
	if (homeLocations.trim()!=''){   
		 //ShowCount(GetTotalCount(PgmLists,homeLocations));
		ClearList('lsttuition',wait_msg); 
		setSrchBreadCrumbColor(5);	
		// Create Ajax object 
		var obj_Ajax=new PcomAjax();  
		obj_Ajax.load('ajaxxmldata.asp?sponsor='+sponsor+'&reqtype=gettuition&homeLocations=' + homeLocations.substring(0,(homeLocations.length-1)) + '&studyoption='+ studyoptions.substring(0,(studyoptions.length-1)) + '&location='+ locations.substring(0, (locations.length-1)) + '&degree=' + degrees.substring(0,(degrees.length-1)) + '&pgm=' + programs.substring(0,(programs.length-1)) + ' &pid='+pid, LoadTuition);
        }
        else
        {
         setSrchBreadCrumbColor(4);
         document.getElementById('countbox').style.display='none';
        }

}
function EvtTuition(sponsor){ //When Tuition area List clicked 
//alert('EvtTuition');
   
    var tuitions = GetSelectedOptionsList('lsttuition'); //selected tuition list
    palert('');	
     if (tuitions.trim()!=''){
     ShowCount(GetTotalCount(TuitionListXml,tuitions));
       setSrchBreadCrumbColor(6)}   
	// end of ajax calls


}

*/




// ****** UI event handlers end 


// ****** functions to populate list objects dynamically from xml start ******* 
 
function LoadDegrees(xmldoc){ //Loads Degrees list with given xml doc 
    ClearList("lstdegree");
	var degreeList = document.getElementById('lstdegree');
	var dNodes = xmldoc.getElementsByTagName('Data');
	FillList(degreeList,dNodes);
	
	if(dNodes.length>0) ShowCount(GetTotalCount(xmldoc,''));
	
	DegreeListXml = xmldoc;
	
	if (restore_level>0){
		SelectList('lstdegree',document.getElementById('degreesel').value);
		restore_level--;
		if (restore_level>0) EvtDegree(gsponsor); //do location level loading 
	}
}

function LoadLocations(xmldoc) {//Loads Locations list box with given xml doc
// alert('LoadLocations');
    ClearList("lstlocation");
	var locationList = document.getElementById('lstlocation');
	var dNodes = xmldoc.getElementsByTagName('Data');
	FillList(locationList,dNodes); 
	
	if(dNodes.length>0){ ShowCount(dNodes[0].attributes[1].value);}
	
	
	LocListXml = xmldoc;
	
	if (restore_level>0){
		SelectList('lstlocation',document.getElementById('locationsel').value);
		restore_level--;
		if (restore_level>0) EvtLocation(gsponsor); //do study option level loading 
	}	
}

function LoadStudy(xmldoc){ //Loads study options list box with given xml doc
//alert('LoadStudy');

	ClearList('lststudy_options');
	var studyList = document.getElementById('lststudy_options');	
	var dNodes = xmldoc.getElementsByTagName('Data');
	FillList(studyList,dNodes);
	
	if(dNodes.length>0) //ShowCount(GetTotalCount(xmldoc,''));
	StudyListXml = xmldoc;
	
	if (restore_level>0){
		SelectList('lststudy_options',document.getElementById('studyoptionsel').value);
		restore_level--;
		if (restore_level>0) EvtStudy(gsponsor); //do home location level loading 
	}
	
}

/*
function LoadHomeLocations(xmldoc){ //Loads home Loacaion list box with given xml doc
//alert('LoadHomeLocations');
   ClearList("lsthome_location");
   var homeLocationList = document.getElementById('lsthome_location');	
	var dNodes = xmldoc.getElementsByTagName('Data');
	FillList(homeLocationList,dNodes);
	
	//if(dNodes.length>0) ShowCount(GetTotalCount(xmldoc,''));
	//PgmLists = xmldoc;
	
	if (restore_level>0){
		SelectList('lsthome_location',document.getElementById('homelocationsel').value);
		restore_level--;
		if (restore_level>0) EvtHomeLocation(gsponsor); //do tuition level loading 
	}
	
	
}
function LoadTuition(xmldoc){// Load Tuition list box with given xml doc
//alert('LoadTuition');
   ClearList("lsttuition");
   var tuitionList = document.getElementById('lsttuition');	
	var dNodes = xmldoc.getElementsByTagName('Data');
	FillList(tuitionList,dNodes);
    
    if(dNodes.length>0) ShowCount(GetTotalCount(xmldoc,''));
    	
	TuitionListXml=xmldoc;
	if (restore_level>0){
		SelectList('lsttuition',document.getElementById('Tuitionsel').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('lstdegree',strProgram);
	ClearList('lstlocation',strDegree);
	ClearList('lststudy_options',strLocation);
	//ClearList('lsthome_location',strStudy);
	//ClearList('lsttuition',strHomeLocation);

	if (document.getElementById('programsel').value != '') restore_level=1;
	if (document.getElementById('degreesel').value != '') restore_level=2; 
	if (document.getElementById('locationsel').value != '') restore_level=3;  
	if (document.getElementById('studyoptionsel').value != '') restore_level=4;
	//if (document.getElementById('homelocationsel').value != '') restore_level=5;
	//if (document.getElementById('Tuitionsel').value != '') restore_level=6;
	if  (restore_level > 0)   EvtProgram(gsponsor);
	

}	
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 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 (ckw.checked){
		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.action = "/fts/code/smplrslt.asp?sponsor="+sponsor;
		}
	}else {
	    var saction="InstSearchResults.asp?searchtype=namesearch&sponsor="+sponsor
		if (cnm.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="+sname;
			}
		}else{saction+="&instName="}
		if (cpg.checked){
			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+="&progName="+spgm; 
			}
		}else{saction+="&progName="}
		if ((cpg.checked)||(cnm.checked)){
			document.Search.action=saction;
		}else{palert('Please choose your search type',5);good_to_submit=false;}
	}// endof chk.checked 
	if (good_to_submit){
		document.Search.submit();
	}
}

function checkform1(sponsor)
{	
	var check_flag = true;
	var a='Please select a program';
	var b='Please select at least one degree';
	var d=strProgram; 
	var c='Please select a location';
	var e='Please select a study option'
	//var f='Please select a home location'
	//var g='Please select tuition range'    
    
    var program = document.getElementById('lstprogram');
    var degree=document.getElementById('lstdegree');
    var location = document.getElementById('lstlocation');
    var studyoptions = document.getElementById('lststudy_options');
  //  var homelocation = document.getElementById('lsthome_location');
   // var tuition = document.getElementById('lsttuition');
    var cnt=0;
	
    if (program.selectedIndex < 0 ){
		palert(a,1);
		document.progsearch.lstprogram.focus();
		check_flag=false;
    }

    
	
	if ( degree.selectedIndex < 0 && check_flag ){	
		palert(b,2);
		document.progsearch.lstdegree.focus();
		check_flag = false;
	}

	if ( location.selectedIndex < 0 && check_flag) {
		palert(c,3);
		document.progsearch.lstlocation.focus();
		check_flag = false;
	}
 
	
    if (studyoptions.selectedIndex < 1 && check_flag && studyoptions.options[0].value == "") { 

		palert(e,4);
		document.progsearch.lststudy_options.focus();
		check_flag = false;
	}
	
	/*
	
	if (homelocation.selectedIndex < 1 && check_flag) { 
		palert(f,5);
		document.progsearch.lsthome_location.focus();
		check_flag = false;
	}
		
	if (tuition.selectedIndex < 0 && check_flag) { 
		palert(g,6);
		document.progsearch.lsttuition.focus();
		check_flag = false;
	}
	if (tuition.selectedIndex == 0 && check_flag) { 
	  if (tuition.options[0].value == "NoMatch")
	   {
	    palert('There is not match to this criteria. Please choose again.',6);
		document.progsearch.lstprogram.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.",6);
		
	}
	if (check_flag){
		document.progsearch.action = "gradsearchresults.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)
}*/

