//ORIGINAL TCS JAVASCRIPT CODES

vFormName = 'form1';
vState = 'state';
var uop_previousid = '';

//FIELD NAME,DISPLAY NAME,TYPE
//TYPE (name,phone,zip,email,text,select)
vRequiredFields = '';
vRequiredFields += 'firstname,First Name,text,';
vRequiredFields += 'lastname,Last Name,text,';
vRequiredFields += 'phone,Phone Number,phone,';
vRequiredFields += 'phone2,Work Phone,phone,';
vRequiredFields += 'address1,Address,text,';
vRequiredFields += 'city,City,text,';
vRequiredFields += 'state,State,select,';
vRequiredFields += 'zip,Zip,zip,';
vRequiredFields += 'country,Country,select,';
vRequiredFields += 'email,Email,email,';
vRequiredFields += 'school_type,Method of Study,select,';
vRequiredFields += 'gradmonth,High School Gradmonth,select,';
vRequiredFields += 'gradyear,High School Gradyear,select,';
vRequiredFields += 'dobmonth,Month of Birth,select,';
vRequiredFields += 'dobday,Day of Birth,select,';
vRequiredFields += 'dobyear,Year of Birth,select';


//INDEX VALIDATION FUNCTION
function validate()
{
	var d=document.form1;

	msg = "";
	if(d.zip.value.length < 5)
	{
		msg = "-Please enter a valid Zip Code.\n";
	}
	if(d.degree.value == '')
	{
		msg += "-Please select a field of study.\n";
	}
	if(d.firstname.value == '')
	{
		msg += "-Please enter your firstname.\n";
	}
	if(d.lastname.value == '')
	{
		msg += "-Please enter your lastname.\n";
	}
	if(d.phone.value == '')
	{
		msg += "-Please enter your phone number.\n";
	}
	if(d.email.value == '')
	{
		msg += "-Please enter your email.\n";
	}

	if(msg)
	{
		alert(msg);
		return false;
	}
	else
	{
		return true;
	}
}

function set_client_id(val)
{
	var d = document.form1;
	var checked = false;
	if(document.getElementById('radiobuttona')){
		doc1 = document.getElementById('radiobuttona');
		checked = checked || doc1.checked;
	}
	if(document.getElementById('radiobuttonb')){
		doc2 = document.getElementById('radiobuttonb');
		checked = checked || doc2.checked;
	}
	if(document.getElementById('radiobuttonc')){
		doc3 = document.getElementById('radiobuttonc');
		checked = checked || doc3.checked;
	}
	if(document.getElementById('radiobuttond')){
		doc4 = document.getElementById('radiobuttond');
		checked = checked || doc4.checked;
	}

	if (!checked){
		doc1.checked = true;
		checked = true;
	}
	else if(val == 'A')
		doc1.checked = true;
	else if(val == 'B')
		doc2.checked = true;
	else if(val == 'C')
		doc3.checked = true;
	else if(val == 'D')
		doc4.checked = true;

	if(checked)
	{
		if (doc1.checked)
		{
			strlist = doc1.value.split("^");
			d.client.value = strlist[0];
			d.form_id.value = strlist[1];
			d.campus_key.value = strlist[2];
			d.program_key.value = strlist[3];
		}

		else if (doc2.checked )
		{
			strlist = doc2.value.split("^");
			d.client.value = strlist[0];
			d.form_id.value = strlist[1];
			d.campus_key.value = strlist[2];
			d.program_key.value = strlist[3];
		}
	
		else if(doc3.checked)
		{
			strlist = doc3.value.split("^");
			d.client.value = strlist[0];
			d.form_id.value = strlist[1];
			d.campus_key.value = strlist[2];
			d.program_key.value = strlist[3];
		}
		else if(doc4.checked )
		{
			strlist = doc4.value.split("^");
			d.client.value = strlist[0];
			d.form_id.value = strlist[1];
			d.campus_key.value = strlist[2];
			d.program_key.value = strlist[3];
		}
	}
	else
	{
		alert('Please select a valid school.');
	}
}
//END OLD TCS FORM CODES (can remove old code except for function validate() after new form-vertical goes live)

/************************************************************************************/
/*** 		BELOW THIS IS JAVASCRIPT CODE FOR TCS FORM-VERTICAL FORM FEATURE	  ***/
/************************************************************************************/

//SPECAIL FUNCTION TO DEAL WITH UOP
function setprevious_id(invar) {
	if(invar != '' && invar != -1) {
		uop_previousid = invar;
	} else {
		uop_previousid = '';
	}
}

//function to hide submitted client radio option
function hideClientOption(pass_check, form_id)
{
	if( pass_check && form_id != 1 )
	{
		//hide option
		document.getElementById("client_" + form_id).style.display = "none";
		document.getElementById("step1picktag").style.display = "none";
		document.getElementById("beforegopickone").style.display = "block";
		document.getElementById("step1tag").className = "leftThankyou";
		document.getElementById("step2tag").className = "rightThankyou";
	
		return true;
	}
	else if( pass_check && form_id == 1 )
	{
		return true;
	}
	else
	{
		return false;
	}
}

//function to display the no more option message
function displayNoOptionMessage()
{
	//if no more option, diplay no option message
	var d = document.form2;
	var displaycounter = 0;
	if( document.getElementById('client0') )
	{
		for( var i = 0; i < d.clientradio.length; i++ )
		{
			if( document.getElementById("client_" + d.clientradio[i].value).style.display != "none" )
			{
				displaycounter++;
			}
		}
	}
	if( displaycounter < 1 )
	{
		document.getElementById("middle").style.display = "none";
		document.getElementById("headerThankyou").style.display = "block";
		stopLoadingGif();
	}
}

//function to display no result message
function displayNoResultMessage()
{
	//if no result, diplay no result message
	document.getElementById("search_result").style.display = "none";
	document.getElementById("no_search_result").style.display = "block";
	document.getElementById("step2tag").style.background = "#FBCA97";
	stopLoadingGif();
}

//function to check radio button, show loading gif, and submit form id to form frame
function show_client_form(selected_ids,form_name)
{
	var d = document.form2;
	var checked = false;
	//split passed in variables
	var splitvariable = selected_ids.split("_");
	var selectedoption = splitvariable[0];
	var form_id = splitvariable[1];
	
	//display loading gif
	startLoadingGif();
	
	//if there was a selection, check that radio and set chosen td class of that option and 
	//set choice td class to other options
	if(selectedoption != '')
	{
		document.getElementById('client' + selectedoption).checked = true;
		document.getElementById("td_" + form_id).className = "chosen";
		
		for( var i = 0; i < d.clientradio.length; i++ )
		{
			if( !document.getElementById("client" + i).checked )
			{
				document.getElementById("td_" + d.clientradio[i].value).className = "choice";
			}
		}
		checked = true;
	}
	
	//default selection to first option
	if (!checked && document.getElementById('client0'))
	{
		doc1 = document.getElementById('client0');
		doc1.checked = true;
		checked = true;
		document.getElementById("td_" + doc1.value).className = "chosen";
	}

	//set form_id value and submit form_id to iframe
	document.getElementById("id").value = form_id;
	document.getElementById("form_name").value = form_name;
	changeIframe(form_id,form_name);
}

//onload function which will default select first option or show default form

function selectClientFormDefault()
{
	//display loading gif
	startLoadingGif();
	setTimeout(selectClientFormDefault_timeout,1000);
}

function selectClientFormDefault_timeout()
{
	var d = document.form2;
	var checked = false;
	var form_id = '';
	var form_name = '';
	//split passed in variables
	
	//display loading gif
	startLoadingGif();
	
	//auto select first client if there are results, else show default form
	if( document.getElementById('client0') )
	{
		//search and find a selected radio
		for( var i = 0; i < d.clientradio.length; i++ )
		{
			if( document.getElementById("client" + i).checked )
			{
				form_id = document.getElementById("client" + i).value;
				form_name = document.getElementById("form_name").value;
				document.getElementById("td_" + form_id).className = "chosen";
				checked = true;
			}
		}	
		
		//default selection to first option if none is found and there is a result
		if (!checked && document.getElementById('client0'))
		{
			radio0 = document.getElementById('client0');
			form_id = document.getElementById("client0").value;
			form_name = document.getElementById("form_name").value;
			radio0.checked = true;
			checked = true;
			document.getElementById("td_" + radio0.value).className = "chosen";
		}
	}
	else
	{
		//no result so show default form
		form_id = 1;
	}

	//set form_id value and submit form_id to iframe
	document.getElementById("id").value = form_id;
	document.getElementById("form_name").value = form_name;
	changeIframe(form_id,form_name);
}

//function to hide loading gif and show form of iframe
function stopLoadingGif()
{
	if( document.getElementById("div_loadingimg") )
	{
		document.getElementById("div_loadingimg").style.display = "none";
		document.getElementById("div_iframe").style.display = "block";
	}	
}

//function to show loading gif and hide form of iframe
function startLoadingGif()
{
	if( document.getElementById("div_loadingimg") )
	{
		document.getElementById("div_loadingimg").style.display = "block";
		document.getElementById("div_iframe").style.display = "none";
	}
}

//function to submit to form frame
function changeIframe(form_id,form_name)
{
	zip = document.getElementById("zip").value;
	programcategory_id = document.getElementById("programcategory_id").value;
	document.getElementById("formiframe").src = "formframe.php?id="+form_id+"&form_name="+form_name+"&zip="+zip+"&programcategory_id="+programcategory_id;
	setTimeout("stopLoadingGif();",2000);
	return false;
}

//This function adds another onload event
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else 
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
} 
