
var eh = new EventHandler();

function init() {
	synchronizeSubscriptionState();
	alertError();
	showLayer("Footer");
}

eh.addEventListener(window, "load", init, false);

function alertError() {
	/*v.o.:commented out upon request.*/
	//if(strMessage!="") {
	//	alert(strMessage);
	//}
}

function synchronizeSubscriptionState(objSelf) {
	var objForm = new FormObject(document.forms["FrmRegister"]);
	/*
	if(objSelf) {
		if(objSelf == objForm.getField("trialCheck1") ) {
			objForm.getField("trialCheck1").checked = true;
			objForm.getField("trialCheck2").checked = false;
			objForm.setFieldValue("intRequestSubscription", 0);			
		}
		else {
			objForm.getField("trialCheck1").checked = false;
			objForm.getField("trialCheck2").checked = true;
			objForm.setFieldValue("intRequestSubscription", 1);			
		}
	}
	else {
		if( objForm.getField("trialCheck1").checked ) {
			objForm.getField("trialCheck1").checked = true;
			objForm.getField("trialCheck2").checked = false;
			objForm.setFieldValue("intRequestSubscription", 0);
		}
		else {
			objForm.getField("trialCheck1").checked = false;
			objForm.getField("trialCheck2").checked = true;
			objForm.setFieldValue("intRequestSubscription", 1);
		}	
	}
	*/
}

function doValidate() {
	var objForm = new FormObject(document.forms[1]);
	
	var strFirstName = objForm.trimFieldValue("strFirstName");
	var strLastName = objForm.trimFieldValue("strLastName");
	var strOrganisation = objForm.trimFieldValue("strOrganisation");
	var strTelephone = objForm.trimFieldValue("strTelephone","[^\\d]");
	var strMobile = objForm.trimFieldValue("strMobile","[^\\d]");
	var strFax = objForm.trimFieldValue("strFax","[^\\d]");
	var strZipCode = objForm.trimFieldValue("strZipCode","[^\\d]");
	var strEmail = objForm.trimFieldValue("strEmail","email");
	var strStreetName = objForm.trimFieldValue("strStreetName");
	var strCity = objForm.trimFieldValue("strCity");
	
	var lngCountry = objForm.getFieldValue("lngCountryID");
	var strCountryName = document.getElementById("lngCountryID").options[document.getElementById("lngCountryID").options.selectedIndex].text;
	
	var lngCounty = objForm.getFieldValue("lngCountyID");
	var strCountyName = document.getElementById("lngCountyID").options[document.getElementById("lngCountyID").options.selectedIndex].text;
	
	
	var strTitle = objForm.trimFieldValue("strTitle");
	var strUsername = objForm.trimFieldValue("strUsername");
	var strPassword = objForm.getFieldValue("strPassword");
	var strPasswordConfirm = objForm.getFieldValue("strPasswordConfirm");
	var strJobTitle = objForm.getFieldValue("strJobTitle");
	var txtComment = objForm.getFieldValue("txtComment");
	var strStationName = objForm.getFieldValue("strTitle");
	
	var education="";
	if (objForm.getField("intSchoolPrimary").checked)
	{
	education += "Primary ";
	}
	if (objForm.getField("intSchoolSecondary").checked)
	{
	education += "Secondary ";
	}
	if (objForm.getField("intSchoolFE").checked)
	{
	education += "FE ";
	}
	if (objForm.getFieldValue("strSchoolOther")!="")
	{
	education += "Other : "+objForm.getFieldValue("strSchoolOther")+" ";
	}
	if (education != "")
	{
	var intAlertEducation = 0;
	education +=""
	}
	else
	{
	var intAlertEducation = 1;
	education ="None"
	}
	

	/*salesforce form*/
	var first_name = strFirstName;
	var last_name = strLastName;
	var company = strOrganisation;
	var title = strJobTitle;
	var email = strEmail;
	var street = strStreetName;
	var description = txtComment;
	var phone = strTelephone;
	var fax = strFax;
	var mobile = strMobile;
	
	var country =strCountryName;
	var county = strCountyName;
	var city = strCity;
	var postcodes = strZipCode;
	var education_category=education;
	var station_name = strStationName;
	var contact_me;
	contact_me = "I would like to start a TRIAL on NUMU";
	/*
	if( objForm.getField("intRequestSubscription").checked) {
		document.forms[1]["00N20000000hHqM"].value = "Yes";
			contact_me = "I would like to start a STATION on DBass";
	}
	else {
		document.forms[1]["00N20000000hHqM"].value = "No";
		contact_me = "I would like to start a TRIAL on DBass";
	}
	*/
	

	objForm.setFieldValue("first_name",first_name);
	objForm.setFieldValue("last_name",last_name);	
	objForm.setFieldValue("company",company);	
	objForm.setFieldValue("title",title);	
	objForm.setFieldValue("email",email);	
	objForm.setFieldValue("street",street);	
	objForm.setFieldValue("description",description);	
	objForm.setFieldValue("phone",phone);
	objForm.setFieldValue("fax",fax);
	objForm.setFieldValue("mobile",mobile);
	objForm.setFieldValue("country",country);
	objForm.setFieldValue("region",county);
	objForm.setFieldValue("city",city);
	objForm.setFieldValue("zip",postcodes);
	objForm.setFieldValue("education_category",education_category);
	objForm.setFieldValue("contact_me",contact_me);
	objForm.setFieldValue("station_name",station_name);
	
	
	var strMessage = "";
	var strErrorAction = "";
	
	if(strFirstName == "") {
		strMessage += "- First name is required.\n";
		strErrAction = 'objForm.setFocus("strFirstName")';
	}
	
	if(strLastName == "") {
		strMessage += "- Second name is required.\n";
		strErrAction = 'objForm.setFocus("strLastName")';
	}
	
	if(strOrganisation == "") {
		strMessage += "- School / Organisation name is required.\n";
		strErrAction = 'objForm.setFocus("strOrganisation")';
	}
	if(strTelephone == "") {
		strMessage += "- Telephone number is required.\n";
		strErrAction = 'objForm.setFocus("strTelephone")';
	}
	
	if(!strTelephone.test("^44\\d{10,10}$")) {
		//strMessage += "- Your phone number should be a 12 digit numeric value, starting with '44'.\n";
		//objForm.setFocus("strTelephone");
	}
	
	if(!strMobile.test("^44\\d{10,10}$")) {
		//strMessage += "- Your mobile number should be a 12 digit numeric value, starting with '44'.\n";
		//objForm.setFocus("strMobile");
	}
	
	if(strFax!="" && !strFax.test("^44\\d{10,10}$")) {
		//strMessage += "- Your fax number should be a 12 digit numeric value, starting with '44'.\n";
		//objForm.setFocus("strFax");
	}

	var v = new Validator();
	if(!v.isEmail(strEmail)) {
		strMessage += "- You should provide a valid e-mail address.\n";
		strErrAction = 'objForm.setFocus("strEmail")';
	}
	
	if(strStreetName == "") {
		strMessage += "- Street name is required.\n";
		strErrAction = 'objForm.setFocus("strStreetName")';
	}
	
	strMessage+=doValidateCounty(lngCountry);
	
	if(strCity == "") {
		strMessage += "- City is required.\n";
		strErrAction = 'objForm.setFocus("strCity")';
	}
	
	if(strZipCode.length<2) {
	strMessage += "- You should provide a valid zip-code.\n";
	objForm.setFocus("strZipCode");
	}	
	
	
	if(strTitle == "") {
		strMessage += "- You should provide a site name.\n";
		strErrAction = 'objForm.setFocus("strTitle")';
	}
	
	if(strUsername == "") {
		strMessage += "- User alias is required.\n";
		strErrAction = 'objForm.setFocus("strUsername")';
	}
	
	if(strPassword == "") {
		strMessage += "- Password is required.\n";
		strErrAction = 'objForm.setFocus("strPassword")';
	}
	else if(strPassword.length < 5) {
		strMessage += "- Passwords should be at least 5 characters.\n";
		strErrAction = 'objForm.setFocus("strPassword")';
	}
	else if(strPassword != strPasswordConfirm) {
		strMessage += "- Password and password confirmation do not match.\n";
		objForm.setFieldValue("strPassword", "");
		objForm.setFieldValue("strPasswordConfirm", "");
		strErrAction = 'objForm.setFocus("strPassword")';
	}
	
	if (intAlertEducation == 1){
		strMessage += "- School type is required.\n";
	}
	
	
	if(strMessage!="") {
		alert(strMessage);
		eval(strErrAction);
		strMessage = "";
		return;
	} 
	else {
		objForm.post();
	}
}
