﻿// JScript File
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

//Global Variables
//var check_flag = true; 
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 strLocation='Please Select a Location';
var strStudy='Please Select a Study Option';
//var strHomeLocation='Select Your Home Location';
//var strTuition='Select a Tuition Level';
var pid=1; // promotion id, increment this every time you promote this file to handle caching problem in IE 

// ****** UI event handlers start 

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',1);
     document.getElementById('lstlocation')[0].selected=false;
     document.getElementById('lstlocation')[0].value="";
     ClearList('lststudy_options',strLocation);
     //document.progsearch.lstlocation.focus();
     //return null;
    }
     else if ( count > 20 )	{
 		palert('Please select only 20 locations. You currently have chosen ' + count + ' locations.',1);
 		document.getElementById('lstlocation')[0].selected=false;
 		document.progsearch.lstlocation.focus();
        return null;
 	 } //count validation ends
   
    var locations = GetSelectedOptionsList('lstlocation'); //selected location list
     
	if (locations.trim()!=''){ 
		ClearList('lststudy_options',wait_msg); 
		setSrchBreadCrumbColor(1);	
			// Create Ajax object 	
		var obj_Ajax=new PcomAjax();  
		obj_Ajax.load('ajaxxmldata.asp?sponsor='+sponsor+'&reqtype=getstudyopt&location='+ locations.substring(0, (locations.length-1)) + '&pid=' + pid, LoadStudy);

        }
    else{
        setSrchBreadCrumbColor(0);
        document.getElementById('countbox').style.display='none';
        }

}

function EvtStudy(sponsor){ //When Study options  List clicked 
//alert('EvtStudy');
var locations = GetSelectedOptionsList('lstlocation'); //selected location list
var studyoptions = GetSelectedOptionsList('lststudy_options'); //selected study options list
//alert("studyoptions="+studyoptions);
palert('');	
//	ClearList('lsthome_location',strStudy);
//	ClearList('lsttuition',strHomeLocation);
	

	if (studyoptions.trim()!='' && studyoptions.trim()!=','){   
	    ShowCount(GetTotalCount(StudyListXml,studyoptions))
		//ClearList('lsthome_location',wait_msg); 
		setSrchBreadCrumbColor(2);	
		// 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)) + '&pid=' + pid, LoadHomeLocations);
        }
        else{
        setSrchBreadCrumbColor(1);
        document.getElementById('countbox').style.display='none';
        }
}
/*
function EvtHomeLocation(sponsor){ //When Home Location area List clicked 
//alert('EvtHomeLocation');
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
//alert(studyoptions);
	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(3);	
		// 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)) + '&pid=' + pid, LoadTuition);
        }
        else
        {
         setSrchBreadCrumbColor(2);
         document.getElementById('countbox').style.display='none';
        }

}

function EvtTuition(sponsor){ //When Tuition area List clicked 
//alert('EvtTuition');
    palert('');	
    var tuitions = GetSelectedOptionsList('lsttuition'); //selected tuition list
     if (tuitions.trim()!=''){
     ShowCount(GetTotalCount(TuitionListXml,tuitions));
       setSrchBreadCrumbColor(4)}   

}

*/

// ****** UI event handlers end 


// ****** functions to populate list objects dynamically from xml start ******* 
 


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(dNodes[0].attributes[1].value);
	//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 (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('locationsel').value != '') restore_level=1;  
	if (document.getElementById('studyoptionsel').value != '') restore_level=2;
	//if (document.getElementById('homelocationsel').value != '') restore_level=3;
	//if (document.getElementById('Tuitionsel').value != '') restore_level=4;
	if  (restore_level > 0)   EvtLocation(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 checkform1(sponsor)
{	
	var check_flag = true;
	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 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 ( location.selectedIndex < 0 && check_flag) {
		palert(c,1);
		document.progsearch.lstlocation.focus();
		check_flag = false;
	}
 
	if (studyoptions.selectedIndex < 0 && check_flag) { 
	  //alert(studyoptions.options[0].value);
		palert(e,2);
		document.progsearch.lststudy_options.focus();
		check_flag = false;
	}
/*	
	if (homelocation.selectedIndex < 1 && check_flag) { 
		palert(f,3);
		document.progsearch.lsthome_location.focus();
		check_flag = false;
	}
		
	if (tuition.selectedIndex < 0 && check_flag) { 
		palert(g,4);
		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.',4);
		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.",4);
		
	}
	if (check_flag && cnt>0){
		document.progsearch.action = "ug_searchresults.asp?sponsor=" + sponsor;
		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<3; 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)
}











