
var chosenOffers = new Array();
var chosenPrograms = new Array();

var tester = new Array();



function addimp(clickid, offerid)
{
    var url = "http://calls.getyourdegree.net/imp.php?c="+clickid+"&o="+offerid;
    new Ajax(url).request();
}

function strictInteger(myfield, e, dec) {

	if (window.event) {
		var key = window.event.keyCode;
	} else if (e) {
		var key = e.which;
	} else {
		return true;
	}

	if (key == null || key in {0:'',8:'',9:'',13:'',27:''}) {
		return true;
	}

	var keychar = String.fromCharCode(key);
	if (keychar == parseInt(keychar, 10)) {
		return true;
	}

	return false;
}



oldclass = new Array();

function checkUnits() {

	var education = document.getElementById('education');
	var unitsArea = document.getElementById('unitsArea');
	var lowedulevel = new Array(3,4);
	
	if(in_array(education.value,lowedulevel))
	{
		alert("Please note that not all schools appearing in your search results may accept leads having this level of education.");
	}
	
	if (education.value&& education.value > 6 && education.value < 12) {
		unitsArea.style.display = '';
		document.getElementById('units').focus();
	} else {
		unitsArea.style.display = 'none';
	}

}

function swapProgram(thisid,programid) {

	var checkbox = document.getElementById('program' + programid);
	var form = document.getElementById('schools');
	var bar = document.getElementById(thisid);
	var offer = programid.match(/[0-9]+/).toString();
	var deactivate = false;
	var others;
	
	if((offer == 105) && document.getElementById('others2956') && (document.getElementById('others2956').className == "othersDisplay")){
			alert("You're allowed to fill only ONE lead per School\nSchool Name: Westwood College (105)\nIf the lead is interested in this school, please uncheck the previously selected school (Offerid 2956)");
			checkbox.checked=false;
			return false;
	}else if((offer == 2956) && document.getElementById('others105') && (document.getElementById('others105').className == "othersDisplay")){
			alert("You're allowed to fill only ONE lead per School\nSchool Name: Westwood College (2956)\nIf the lead is interested in this school, please uncheck the previously selected school (Offerid 105)");
			checkbox.checked=false;
			return false;
	}


	if (others = document.getElementById('others' + offer)) {
		others.className = 'othersDisplay';


	}

		var currentOfferGroup = document.getElementById('group' + offer).value;
		//alert(currentOfferGroup);


		for(var i = 0; i < this.chosenOffers.length; i++){

			if(document.getElementById('group' + this.chosenOffers[i]))
				chosenOfferGroup = document.getElementById('group' + this.chosenOffers[i]).value;
			else
				chosenOfferGroup = null;

			//alert("chosenOfferGroup is "+chosenOfferGroup);

			//if offer from the same group is chosen
			if( currentOfferGroup == chosenOfferGroup ){
				
				//alert("already chosen from group");
				coffer = this.chosenOffers[i]+"";

				//if offer is different than other offer chosen from the group
				if(offer != this.chosenOffers[i]){

					//hide last offer form
					othersOld = document.getElementById('others' + this.chosenOffers[i]);
					if(othersOld)
						othersOld.className = 'others';

					//uncheck last program box
					var checkboxOld = document.getElementById('program' + this.chosenPrograms[coffer]);
					checkboxOld.checked = false;

					//unhighlight last program name
					var highlighted = document.getElementById('programa' + this.chosenPrograms[coffer]);
					highlighted.className = 'inner';


				}

				//delete from chosenOffers
				//alert("deleting from chosen"+coffer);
				this.chosenOffers.splice(i,1);
				//delete from chosenPrograms
				delete this.chosenPrograms[coffer];

			}

		}


	//}


	this.chosenOffers.push(offer);
	offerstring = offer+"";
	programstring = programid+"";
	this.chosenPrograms[offerstring] = programid;

	//mark logo box
	chosenOfferGroup = document.getElementById('group' + offer).value;
	
	document.getElementById('logo'+chosenOfferGroup).className="TabbedPanelsTab TabbedPanelsProgramSelected";
	
	var offersInCurrentGroup = new Array();

	//*-+-:> loop through all offerids
 	for(var i = 0, l; (l = form[i]); i++) {

		//if id empty, then move on.
		if (l.id == '') {
 			continue;
 		}

		if (i%2) var swap = 'inner'; else var swap = 'alter';
		//*-+-:> match the offer id's 
		if (l.type == 'checkbox') {

			if(l.id.match(/[0-9]+/)){
				var match = l.id.match(/[0-9]+/).toString();
				var tbar = l.id.replace(/program/,'programa');
			
				//*-+-:> if current (active) offer
				if (offer == match) {
						
					//*-+-:> diff program selected, uncheck previously checked program
					if ('program' + programid != l.id && l.checked == true) {
						document.getElementById(l.id).checked = false;
						document.getElementById(tbar).className = swap;
					}
	
					//*-+-:> same program selected, deactivate checked program
					if ('program' + programid == l.id && l.checked == true) {
						document.getElementById(l.id).checked = false;
						document.getElementById(tbar).className = swap;
						deactivate = true;
					}

					//*-+-:> show/hide programs all additional programs in active offer
					if ('program' + programid != l.id && l.checked == false && document.getElementById(tbar).style.display !="none") {
						document.getElementById(tbar).style.display = "none";
					}else if ('program' + programid != l.id && l.checked == false && document.getElementById(tbar).style.display =="none") {
						document.getElementById(tbar).style.display = "";							
					}

				} 
				//*-+-:> record offers in active group
	 			if( chosenOfferGroup == document.getElementById('group' + match).value){
					if(!in_array(match,offersInCurrentGroup)){
						offersInCurrentGroup.push(match);
					}
				}
			}
		}
	}
	
	//*-+-:> for all offers in currently active group
	for(var i=0;i<offersInCurrentGroup.length;i++){

		var match = offersInCurrentGroup[i];

		//*-+-:> if all programs deactivated, show all offers and programs
		//*-+-:> otherwise hide all non active programs/offers in group
		if(deactivate){
			if(document.getElementById(chosenOfferGroup+'-'+match))
				document.getElementById(chosenOfferGroup+'-'+match).style.display = "";
		} else if( offer != match){
			if(document.getElementById(chosenOfferGroup+'-'+match))
				document.getElementById(chosenOfferGroup+'-'+match).style.display = "none";
		}
	}

	if (deactivate == false) {
		checkbox.checked = true;
		bar.className = 'sublistActive';

	}
	
	if (deactivate == true) {

		if (others = document.getElementById('others' + offer)) {
			others.className = 'others';
		}

		var offerstring = offer+"";
		//delete from chosenPrograms
		delete this.chosenPrograms[offerstring];
		for(var i = 0; i < this.chosenOffers.length; i++){
			if(chosenOffers[i] == offer ){
				//delete from chosenOffers
				//alert('deleteing'+offer);
				this.chosenOffers.splice(i,1);
			}
		}

		//unmark logobox of currently open school
		chosenOfferGroup = document.getElementById('group' + offer).value;
		document.getElementById('logo'+chosenOfferGroup).className = "TabbedPanelsTab TabbedPanelsTabSelected";

	}

	
	refreshSelected();

	return true;
	
}





/*************************************************************
** JD Custom's fields for subfields within others fieldset 	**
** othersDisplay == that's the class name when they check   **
** on checkboxes and activates the fieldset					**
*************************************************************/
function hide_subfields(ids_arr){ 
	for(i=0; i< ids_arr.length; i++){
		document.getElementById(ids_arr[i]).style.display = 'none';
	}
	return true;
}

function changeFieldState(id,f_state){
	sf_element = document.getElementById(id);
	sf_element.style.display = f_state;
	return true;
}

function in_array(needle,haystack){
	for(i=0; i<haystack.length; i++)
	{
		if (needle == haystack[i]){return true};	
	}
	return false;
}

/*************************************************************
**	This function will deal with the Ajax Part.				**
**	If the <fieldset> is not showing then we clean up the 	**
**	the dropdown that was previously created. If it is 		**
**	then we populate the dynamically the dropdown according	**
** 	to whatever the server response is						**
** 	Response is handled by the last element that is passed	**
** 	in the php function call (this case populateCampusDD	**
*************************************************************/
function getCampusDropdown(offerid,programid,zip){

	x_gydajax.getCampusId(offerid,programid,zip,populateCampusDropdown);
	return;
/*
	fieldsetId='others' + offerid;
	class_name = document.getElementById(fieldsetId).className;
	if (class_name == 'othersDisplay'){
		return;
	}else{
		school_obj = document.getElementById('school[' + offerid + '][others][school]');
		removeOptions(school_obj);
		addOptions(school_obj,'....loading data.....','');
		return;
	}
*/

}

function populateCampusDropdown(response)
{
	if(response == "" || response == null)
		return;

	dropdown_array = response.split(":::");
	offerid = dropdown_array[0];
	school_obj = document.getElementById('school[' + offerid + '][others][school]');
	removeOptions(school_obj);
	for(i=1; i<dropdown_array.length; i++){
		temp_str = dropdown_array[i];
		temp_arr = temp_str.split(":");
		addOptions(school_obj,temp_arr[1],temp_arr[0]);
		temp_str = "";
		temp_arr = new Array();
	}
}

function addOptions(elmt,text,value)
{
	if(elmt == null)
		return;

	optn = document.createElement('option');
	optn.text = text;
	optn.value = value

	try{
		elmt.add(optn,null); //standard
	} catch(ex) {
		elmt.add(optn); //ie
	}
}

function removeOptions(elmt)
{
	if(elmt == null)
		return;

	for(i=0; i<=elmt.length; i++){
		elmt.remove(elmt[i]);
	}
}

function changeSubmitProperties(text,id)
{
	submitButton = document.getElementById(id);
	submitButton.value=text;
	submitButton.style.textTransform='lowercase'; 
	submitButton.style.fontWeight='bold'; 
	submitButton.style.fontFamily='Verdana'; 
	submitButton.style.fontSize='11px'; 
	submitButton.style.backgroundColor='#dedede'; 
	submitButton.style.color='#222222';
	submitButton.style.padding='0'; 
}

function popMoreDetails(schoolid)
{
	url = "http://calls.getyourdegree.net/schoolinfo.php?schoolid=" + schoolid;
	
	width = "830";
	height = "500";
	x = (width)/2, y = (height)/2;
	
	if (screen) {
		y = (screen.availHeight - height)/2;
		x = (screen.availWidth - width)/2;
	}
	
	window.open(url,'_blank','width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x+',scrollbars=yes');
}


function toggle(id,inputfocus,toggle,oneway,newtext) {

	if (document.getElementById(id).style.display == '') {
		if (oneway == 0) {
			document.getElementById(id).style.display = 'none';

			if(id == "callcenterform")
				document.getElementById(toggle).innerHTML = 'SHOW SEARCH FORM';
		}
	}
	else {
		document.getElementById(id).style.display = '';

		if(id == "callcenterform")
				document.getElementById(toggle).innerHTML = 'HIDE SEARCH FORM';

		if (document.getElementById(inputfocus)) {
			document.getElementById(inputfocus).focus();
		}
	}
}

function refreshSelected(){

	var selectedPrograms="<table>";

	for(var i = 0; i < this.chosenOffers.length; i++){
				
		coffer = this.chosenOffers[i]+"";
		var image = "<img src='images/empty.png'>";

		//if selected has form errors, identify it
		if(document.getElementById("erroredofferlist"+coffer)){
		      selectedPrograms += "<tr><td valign='top'><a href='#statusbox' style='text-decoration:none;'><b><font color='red'>?</font></b></a>"
				+"</td><td valign='top'><b>"+"<a href='#statusbox' style='text-decoration:none;'>"
				+ document.getElementById('offerlist' + this.chosenOffers[i]).value + "</b><br>"
				+ document.getElementById('programlist' + this.chosenPrograms[coffer]).value + "</a><br></td></tr>";
		}else{
			if(this.chosenPrograms[coffer]){
				selectedPrograms += "<tr><td valign='top'><img src='images/empty.png'></td><td valign='top'><b>"
					+ document.getElementById('offerlist' + this.chosenOffers[i]).value + "</b><br>"
					+ document.getElementById('programlist' + this.chosenPrograms[coffer]).value + "<br></td></tr>";
			}
		}
		



		chosenOfferGroup = document.getElementById('group' + this.chosenOffers[i]).value;
		document.getElementById('logo'+chosenOfferGroup).className="TabbedPanelsTab TabbedPanelsProgramSelected";

	}
	selectedPrograms += "</table>";

	document.getElementById('selectedSchools').innerHTML =  selectedPrograms;

}


function simpletoggle(id,inputfocus) {

	if (document.getElementById(id).style.display == '') {
			document.getElementById(id).style.display = 'none';
			document.getElementById(id+"tab").className = 'gydtab';
	} else {
		document.getElementById(id).style.display = '';
		document.getElementById(id+"tab").className = 'gydtabActive';
	}

	/*
	//hide gyd on leadpoint or degreeinfo selection
	if(id == 'degreeinfo' || id == 'leadpoint'){
		document.getElementById('ccgyd').style.display = 'none';
		document.getElementById("ccgydtab").style.display = 'none';

		if(id == 'degreeinfo'){	
			if(di = document.getElementById('leadpoint') ){
				di.style.display = 'none';
				document.getElementById("leadpointtab").style.display = 'none';
			}
		}
	
	}*/
	

	if(di = document.getElementById('degreeinfo') ){
		if(di.id != id){
			di.style.display = 'none';
			document.getElementById("degreeinfotab").className = 'gydtab';
		}
	}

	if(cb = document.getElementById('collegebound') ){
		if(cb.id != id){
			cb.style.display = 'none';
			document.getElementById("collegeboundtab").className = 'gydtab';
		}
	}

	if(lp = document.getElementById('leadpoint') ){
		if(lp.id != id){
			lp.style.display = 'none';
			document.getElementById("leadpointtab").className = 'gydtab';
		}
	}

	if(gyd = document.getElementById('ccgyd')  ){
		if(gyd.id != id){
			gyd.style.display = 'none';
			document.getElementById("ccgydtab").className = 'gydtab';
		}
	}

	if(st = document.getElementById('simpletuition')  ){
		if(st.id != id){
			st.style.display = 'none';
			document.getElementById("simpletuitiontab").className = 'gydtab';
		}
	}
}





function markIncomplete(){


	inputqs = new Array();
	inputqs = document.getElementsByTagName('input');
	for(i = 0; i < inputqs.length; i++){


		if(!(inputqs[i].id in {"category":'',"degree":'',"address2":'',"location":'',"search":'',"program":'',"btn_requestInfo":''})) {


			if(inputqs[i].id != ""){
				inputqs[i].onfocus=function(){focusFormField(this.id);};
				inputqs[i].onblur=function(){blurFormField(this.id);};
			}
			else{
				inputqs[i].onfocus=function(){focusFormField(this.name);};
				inputqs[i].onblur=function(){blurFormField(this.name);};
			}

			if(trim(inputqs[i].value) =="")
				inputqs[i].className = "markerror";
			//addElement();

		}

	}

	inputqs = document.getElementsByTagName('select');
	for(i = 0; i < inputqs.length; i++){
	
		var match = inputqs[i].id.match(/program/);
		if(match != null){
			continue;
		}

		if(!(inputqs[i].id in {"category":'',"degree":'',"address2":'',"location":'',"search":'',"program":'',"btn_requestInfo":''})) {

			if(inputqs[i].id != ""){
				inputqs[i].onfocus=function(){focusFormField(this.id);};
				inputqs[i].onblur=function(){blurFormField(this.id);};
			}
			else{
				inputqs[i].onfocus=function(){focusFormField(this.name);};
				inputqs[i].onblur=function(){blurFormField(this.name);};
			}

			if(trim(inputqs[i].value) ==""){
				inputqs[i].className = "markerror";
			}
		}

	}
}

function focusFormField(id){
	if(id == "")
		return;

	if(document.getElementById(id)){
		document.getElementById(id).className="";
	}
	else if (document.getElementsByName(id)){
		document.getElementsByName(id)[0].className="";
	}
	else{}
	
}

function blurFormField(id){
	if(id == "")
		return;

	if(document.getElementById(id)){

		if(trim(document.getElementById(id).value) == "")
			document.getElementById(id).className = "markerror";
		else
			document.getElementById(id).className = "";
	}
	else{
		if(trim(document.getElementsByName(id)[0].value) == "")
			document.getElementsByName(id)[0].className = "markerror";
		else
			document.getElementsByName(id)[0].className = "";
	}
}

function trim(str) {
	if(!str)
		return str;
	var newstr;
	newstr = str.replace(/^\s*/, "").replace(/\s*$/, ""); 
	newstr = newstr.replace(/\s{2,}/, " "); 
	return newstr;
} 


function switchAllMatching(id,value){
	matching = document.getElementsByName(id);
	for(i = 0; i < matching.length; i++)
		matching[i].value = value;
}

function clearSelectedBox(id){
	var children = document.getElementById(id).getElementsByTagName('*');
	for(i = 0; i < children.length; i++){
		children[i].checked = false;

		if(children[i].className == 'sublistActive') 
			children[i].className = '{cycle name="programs" values=inner,alter}';
	}
}
function clearAltPhone(){
	document.getElementById("work_areacode").value = "";
	document.getElementById("work_prefix").value = "";
	document.getElementById("work_suffix").value = "";
}


function popAltPhone(){
	document.getElementById("work_areacode").value = document.getElementById("home_areacode").value;
	document.getElementById("work_prefix").value = document.getElementById("home_prefix").value;
	document.getElementById("work_suffix").value = document.getElementById("home_suffix").value;
}

function swapAltPhone(){

	if(document.getElementById("altphonecheck").checked == true){
		popAltPhone();	
		/*
		if(document.getElementById("work_areacode").value !="")
			focusFormField("work_areacode");
		if(document.getElementById("work_prefix").value !="")
			focusFormField("work_prefix");
		if(document.getElementById("work_suffix").value !="")
			focusFormField("work_suffix");
		*/
	}
	else{
		clearAltPhone();
		blurFormField("work_areacode");
		blurFormField("work_prefix");
		blurFormField("work_suffix");
	}
}



function reselectErrored(offerid,programid,value,numselected){

	//alert("# prior to select "+numselected);

	if(document.getElementById('school['+offerid+'][program]')){
		document.getElementById('school['+offerid+'][program]').value = programid;
	}

	if( document.getElementById('others'+offerid) )
		document.getElementById('others'+offerid).style.display ='block';


	if(value != ''){
		var funct = "offerid_"+offerid;
		if (eval("typeof " + funct + " == 'function'")){ 
			eval(funct)(value,offerid);
		}
	}

	countSelected(numselected);
}

function selectOfferProgram(obj,offerid,numselected){

	//alert("# prior to select "+numselected);
	if(obj.value != ''){

			jQuery('#'+'errors_'+offerid).slideDown('medium');
			jQuery('#'+'others'+offerid).slideDown('medium');
	} else{

			jQuery('#'+'errors_'+offerid).slideUp('medium');
			jQuery('#'+'others'+offerid).slideUp('medium');
	}

	countSelected(numselected);

}

function hideOfferProgram(offerid,numselected){

	//alert("# prior to select "+numselected);

	jQuery('#'+'errors_'+offerid).slideUp('medium');
	jQuery('#'+'others'+offerid).slideUp('medium');

	if(document.getElementById('school['+offerid+'][program]')){
		document.getElementById('school['+offerid+'][program]').value = "";
	}
	
	countSelected(numselected);
}


function countSelected(numselected){

	inputqs = document.getElementsByTagName('select');

	inactivelist = new Array();
	activelist = new Array();
	var count = numselected;

	for(i = 0; i < inputqs.length; i++){
		
		var match = inputqs[i].id.match(/program/);
		if(match != null){
			if( inputqs[i].value != ""){
				activelist.push(inputqs[i].id);
				count++;
			}
			else{
				inactivelist.push(inputqs[i].id);
			}
		}
	}

	if(count >= 3){
		for(i = 0; i < inactivelist.length; i++){
			var match = inactivelist[i].match(/[0-9]+/).toString();
			jQuery('#'+'li_'+match).slideUp('medium');
			//document.getElementById('li_'+match).style.display = 'none';
		}
	}else{
		for(i = 0; i < inactivelist.length; i++){
			var match = inactivelist[i].match(/[0-9]+/).toString();
			jQuery('#'+'li_'+match).slideDown('medium');
			//document.getElementById('li_'+match).style.display = 'block';
		}
	}

}

