function mttTrim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function mttGetCookie() {
	var cookieValue = null;
	if (parent.document.cookie && parent.document.cookie != '') {
		var cookies = parent.document.cookie.split(';');
		for (var i = 0; i < cookies.length; i++) {
			var cookie = mttTrim(cookies[i]);
			// Does this cookie string begin with the name we want?
			if ( cookie.substring(0, 12) == 'formReferrer') {
				cookieValue = decodeURIComponent(cookie.substring(13));
				break;
			}
		}
	}
	return cookieValue;
}
function init() {
	D = document;
	var mttFormCookie = mttGetCookie();
	if(mttFormCookie != null || mttFormCookie != "") {
		D.request.referrer.value = mttFormCookie;

	}
/*
	if (D.referrer&&D.referrer!="") {
		D.request.referrer.value = D.referrer;
		if(Cookie.get('web_ref')==null||Cookie.get('web_ref')=="")
			Cookie.set('web_ref',parent.document.referrer);
	}
*/
		$('name').focus();
		
		$('specialisation').options[0] = new Option('Select ESE Centre First','');
		$('startingDate').options[0] = new Option('Select ESE Centre First','');

		// populate birth date starts
		new Ajax.Updater(
					'divBirthDate', 
					'/_getFormData.php?ACT=DO&LANG='+LANG+'&WHICH=1', {
						method: 'get'
					});
		// populate birth date ends

		// populate nationality starts
		new Ajax.Updater(
					'divNationality', 
					'/_getFormData.php?ACT=DO&LANG='+LANG+'&SELECTED='+SELECTED_NATION+'&WHICH=2', {
						method: 'get'
					});
		// populate nationality ends


		// populate country phone code starts
		new Ajax.Updater(
					'divCountryCode', 
					'/_getFormData.php?ACT=DO&LANG='+LANG+'&SELECTED='+SELECTED_COUNTRY_CODE+'&WHICH=3', {
						method: 'get'
					});
		// populate country phone code ends



		// populate campus starts
		new Ajax.Updater(
					'divCampus', 
					'/_getFormData.php?ACT=DO&LANG='+LANG+'&WHICH=4', {
						method: 'get'
					});
		// populate campus ends


		// populate footer starts
		new Ajax.Updater(
					'footer', 
					'/_getFormData.php?ACT=DO&LANG='+LANG+'&WHICH=7', {
						method: 'get'
					});
		// populate footer ends
}
function validate(){
	var D = document;
	var f = D.request;
	var missing=false;
	if(f.name.value == "") {
		missing=true;
		$('name').addClassName('redBorder');
	}
	if(f.surname.value == "") {
		missing=true;
		$('surname').addClassName('redBorder');
	}
	if(f.dob_day.value == "") {
		missing=true;
		$('dob_day').addClassName('redBorder');
	}
	if(f.dob_month.value == "") {
		missing=true;
		$('dob_month').addClassName('redBorder');
	}
	if(f.dob_year.value == "") {
		missing=true;
		$('dob_year').addClassName('redBorder');
	}
	if(validate_email(f.email) == false) {
		missing=true;
		$('email').addClassName('redBorder');
	}
	if(f.country_code.value == "") {
		missing=true;
		$('country_code').addClassName('redBorder');
	}
	if(f.phone.value == "") {
		missing=true;
		$('phone').addClassName('redBorder');
	}
	if(f.nationality.value == "") {
		missing=true;
		$('nationality').addClassName('redBorder');
	}
	if(f.campus.value == "") {
		missing=true;
		$('campus').addClassName('redBorder');
	}
	if(f.specialisation.value == "") {
		missing=true;
		$('specialisation').addClassName('redBorder');
	}
	if(f.startingDate.value == "" ) {
		missing=true;
		$('startingDate').addClassName('redBorder');
	}
	if (missing){
		$('errorDiv').show();
		return false;
	}else{
		f.invia.disabled=true;
		f.submit();
	}
}