//Populate default value for AreaOfInterest & ProgramsOfInterestType - used in search & CC version
function populateDefaultValue(){
	var requestAOI='AreaOfInterest';
	var requestPOIType='ProgramsOfInterestType';
	var valAOI='AreaOfInterest';
	var valPOIType='ProgramsOfInterestType';
	var selectElements=document.getElementsByTagName('select');
	// BUG127228 Hitting back on CampusCorner defaults you to AOI1 of Art and Design
	// If the form submit is not valid, either restore the values to default or retain the values provided by the user.
	// Currently we are setting the values back to default(Select Any)
	var aoiSelectElement = null;
	for (var i=0; i<selectElements.length; i++) {
			if(valAOI == selectElements[i].name ){
				aoiSelectElement = selectElements[i];
			}
	}
	var checkAOIHeader = aoiSelectElement.options[0].value;
	if( (checkAOIHeader != '---Select a Program---') && (checkAOIHeader != '---Any Program---')) {
		var oOption = document.createElement('OPTION');
 	  	oOption.value = '---Select a Program---';
 	  	//oOption.disabled = true;
 	    oOption.innerHTML = '---Select a Program---'; 
		aoiSelectElement.insertBefore(oOption,aoiSelectElement.options[0]); 
	}
	// End of BUG127228
	
	//ENH116795 by bgharate
	requestAOI = requestAOI.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
	var regexS = "[\\?&]"+requestAOI+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var resultsAOI = regex.exec( window.location.href ); 
	if( resultsAOI == null ) {
			for (var i=0; i<selectElements.length; i++) {
			if(valAOI==selectElements[i].name || valPOIType==selectElements[i].name){
				selectElements[i].selectedIndex=0;
			}
		}
	}else{ 
		//For ProgramsOfInterestType
		requestPOIType=requestPOIType.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
		regexS = "[\\?&]"+requestPOIType+"=([^&#]*)";  
		regex = new RegExp( regexS );  
		var resultsPOIType = regex.exec( window.location.href ); 
		setValidAOI(aoiSelectElement,resultsAOI[1]);
		if(null != resultsPOIType)
			getSubSubjects(resultsAOI[1],resultsPOIType[1]);
		else 
			getSubSubjects(resultsAOI[1],null);
	}
}

/*
This Function returns the new AOI/AOI2 skins for old AOI/AOI2 skins 
*/
function setValidAOI(aoiSelectElement,areaOfInterest)
{
	$.ajax({
				type: "GET",
				url: "getNewAoiMapping.jsp",
				data: { aoi: areaOfInterest},
				success: function(data){
					aoiSelectElement.value = data;
				}
		});			 
}

function doProcessing() {
  document.getElementById('submitBtn').className = 'thide'; 	
  document.getElementById('processing').className = 'tshow';
}

function openWin(page,win_width,win_height) {

    try {
    	popup_window.close();
    } catch(e) {
					
    }

    if ( page.indexOf("mailto") == -1 ) {
    	var options = "scrollbars=yes, width=" + win_width + ", height=" + win_height;
    	var win = window.open(page, 'MyWin', options);
    	if (window.focus) { win.focus() }
    }
}

// On_change method for State field
function onChangeSP() {
	try{
		var cn = document.form.SP.value.substring(0,2);
		if(cn == "US" || cn == "CA" || cn == "UM"){
			document.form.CN.value = cn;
			//document.form.PC.disabled = '';
		}else{
			document.form.CN.value = '---Select One---';
			//document.form.PC.disabled = 'disabled';
		}
	}catch(e){}
}

// On_change method for Country field
function onChangeCN() {
	try{
		if ( document.form.CN.value == "US" 
				|| document.form.CN.value == "UM"
				|| document.form.CN.value == "CA" ) {
			//document.form.PC.disabled = '';
			//document.form.SP.disabled = '';
		}else{
			//document.form.PC.value = '';
			document.form.SP.value = '(Unspecified)';
			//document.form.PC.disabled = 'disabled';
	//		document.form.SP.disabled = 'disabled';
		}
	}catch(e){}
}

function displaySelectedSchool( vKeyjs ) {
	if(!vKeyjs || vKeyjs == -1){
		vKeyjs = schoolArray[0].vendorKey;
	}

	var index = 0;
	for( var i=0; i<schoolArray.length; i++ ) {
		document.getElementById( 'schoolBox' + (i+1) ).className = "schoolBox";
		if( schoolArray[i].vendorKey == vKeyjs ) {
			document.getElementById( 'schoolBox' + (i+1) ).className = "selectedBox";
			var posY = document.getElementById( 'schoolBox' + (i+1) ).offsetTop;
			var browserName=navigator.appName; 
			if (browserName=="Netscape"){ 
				posY = posY - 159;
			}else if (browserName=="Microsoft Internet Explorer"){
				//do nothing!?
				posY = posY + 10;
			}
			document.getElementById('selectedBoxRight').style.top = posY + 'px';
			document.getElementById('selectedBoxRight').style.height = 
				document.getElementById( 'schoolBox' + (i+1) ).offsetHeight + 'px';
			document.getElementById('rightImage').style.height = 
				document.getElementById( 'schoolBox' + (i+1) ).offsetHeight + 'px';
			document.getElementById('selectedBoxRight').style.width = '21px';
			index = i;
		}
	}

	document.getElementById( 'selectedLogo' ).innerHTML		= "<img src='" + schoolArray[index].logoURL
						+ "' alt='" + schoolArray[index].vendorName + "'>";
	document.getElementById( 'selectedName' ).innerHTML		= schoolArray[index].vendorName;
	document.getElementById( 'selectedDesc' ).innerHTML		= schoolArray[index].blurb;
	if(document.getElementById( 'programsLink' )){
		document.getElementById( 'programsLink' ).href	= "javascript:openCampusProgWin('show/programs.jsp?module=search&vendorKey="
			+ schoolArray[index].vendorKey + "', '520', '460')";
	}
	if(document.getElementById( 'locationsLink' )){
		document.getElementById( 'locationsLink' ).href	= "javascript:openCampusProgWin('show/campuslocations.jsp?vendorKey=" 
			+ schoolArray[index].vendorKey + "', '520', '460')";
	}

	document.form.selectedVendorKey.value = schoolArray[index].vendorKey;
}

function openCampusProgWin(page,win_width,win_height) {

    try {
    	popup_window.close();
    } catch(e) {
    }

    if ( page.indexOf("mailto") == -1 ) {
    	var options = "resizable=1,scrollbars=yes, width=" + win_width + ", height=" + win_height;
    	var win = window.open(page, 'MyWin', options);
    	if (window.focus) { win.focus();}
    }
}
 function getXMLHTTP()
	  { //fuction to return the xml http object
			var xmlhttp=false;	
			try{
				xmlhttp=new XMLHttpRequest();
			}
			catch(e)	{		
				try{			
					xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
				}
				
				catch(e){
					try{
					xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
					}
					catch(e1){
						xmlhttp=false;
					}
				}
			}
		 	
		return xmlhttp;
    }
    
function getSubSubjects(rootSubject,valuPOIType){
	if(rootSubject=='---Any Program---') {
		document.getElementById('ProgramsOfInterestType').style.display='none';
		return;
	}
	else if(rootSubject=='---Select a Program---') {
		document.getElementById('ProgramsOfInterestType').style.display='none';
		return;
	}
	else
	document.getElementById('ProgramsOfInterestType').style.display='block';
	$.ajax({
				type: "POST",
				url: "findSubSubject.jsp",
				data: { subject: rootSubject,POIType : valuPOIType},
				success: function(data){
				$("select[name=ProgramsOfInterestType]").html(data)
				}
		});
}

function cleansePOI(){
		//Enable POI to be self-sustainable if grouped
		$("select[name^='ProgramOfInterest']").each(function(){
			var newPOI = $(this).clone(true).empty(), optGroup = undefined;
			$(this).find("option").each(function(){
				if(this.value.indexOf('::LABEL::') > -1 ){
					if(optGroup != undefined)
						newPOI.append(optGroup);
					optGroup = $("<optgroup />").attr('label', this.text).addClass($(this).attr('class'));
				}else if(optGroup != undefined){
					optGroup.append(this);
				}else
					newPOI.append(this);
			});
			newPOI.append(optGroup);
			newPOI[0].selectedIndex = 0;
			$(this).replaceWith(newPOI);
		});
	}
function addPOIBestMatches(bestMatches)
{

	if(null != bestMatches ) 
	{
		var anyBestMatches = false;
		var bestMatchesOptions = "<option value='::LABEL::BESTMATCHES' class='poiDropDown'>---BEST MATCHES FOR YOU---</option>";
		for (var degree in bestMatches) 
		{
			degreeMatchList = bestMatches[degree];
			if(degreeMatchList.length > 0)
				bestMatchesOptions +="<option value='::LABEL::"+degree+"' class='poiDropDown'>----------"+degree+"----------</option>";
			for(var i in degreeMatchList)
			{
				anyBestMatches = true;
				for(var program in degreeMatchList[i])
				{
					bestMatchesOptions +="<option value='"+program+"' class='poiDropDown'>"+degreeMatchList[i][program]+"</option>"
				}
			}
		}
		if(anyBestMatches)
		{
			bestMatchesOptions+="<option value='::LABEL::EMPTYVALUE' disabled></option><option value='EMPTYVALUE' disabled></option>"
			$("select[name=ProgramOfInterest] option:eq(0)").after(bestMatchesOptions)
		}
	}
					
}
