function ShowObj(objMID) {
	document.getElementById(objMID).style.display='';
}
function HideObj(objMID) {
	document.getElementById(objMID).style.display='none';
}

function checkNumberOfChildren(theForm) {
	if ((theForm.numChildren.value == "1") || (theForm.numChildren.value == "")) {
		ShowObj('5a');
		HideObj('5b');
	} else {
		ShowObj('5b');
		HideObj('5a');
	}
}

function checkOver18(theForm){

	if(theForm.childWillingToSponsor[0].checked){
		ShowObj('15a');
		HideObj('17a');
	}
	else{
		HideObj('15a');
		ShowObj('17a');
		document.getElementById('textChange').innerHTML='I';
	}
}//funcation

function checkGuardian18(theForm){

	if(theForm.childWillingToSponsorOver18[0].checked){
		HideObj('17a');
	}
	else{
		ShowObj('17a');
		document.getElementById('textChange').innerHTML='As your child is under the required sponsorship age of 18, i';
	}

}//function
function checkSponsoringChildOver18(theForm) {
	if (theForm.childWillingToSponsorOver18[0].checked) {
	//	HideObj('7b');
		ShowObj('8a');
		HideObj('8b');
		ShowObj('8a2');
		HideObj('8b2');
		ShowObj('9a');
		HideObj('9b');
	} else {
//		ShowObj('7b');
		ShowObj('8b');
		HideObj('8a');
		ShowObj('8b2');
		HideObj('8a2');
		ShowObj('9b');
		HideObj('9a');
	}
}
function selectSponsorType(theForm) {
	if ((theForm.someone_else_sponsor.value == "childguardian") || (theForm.someone_else_sponsor.value == "childspouse") || (theForm.someone_else_sponsor.value == "spouseguardian")) {
		ShowObj('18a');
	} 
	if ((theForm.someone_else_sponsor.value == "communityorganisation") || (theForm.someone_else_sponsor.value == "")){
		HideObj('18a');		
	}
}

function writeChildOrChildrenDescriptor(theForm) {
	if (theForm.numChildren.value == "1") {
		document.getElementById('childOrChildrenDescriptor').innerHTML='your child';
	} else {
		if (theForm.numChildrenInOz.value == "1") {
			document.getElementById('childOrChildrenDescriptor').innerHTML='your child in Australia';
		} else {
			document.getElementById('childOrChildrenDescriptor').innerHTML='one of your children in Australia';
		}
	}
}


function isValidEmailAddr(inputStr){
		// check for the @ char
			var atPos=inputStr.indexOf("@",0);
			var spacePos=inputStr.indexOf(" ",0);
		
			if (spacePos>0)
			{ // no spaces allow
				return false;
			}	
			else if (atPos<0)
			{ // invalid or no @
				return false;
			}
			else
			{
				var pointPos=inputStr.indexOf(".",atPos);
			 	if (pointPos==-1){// not found
				return false;
			}
			else if (pointPos==inputStr.length-1)
			{ // last position
				return false;
			}
		}
		return true;
}

// functions used for intentionality, key drivers and service selection guide question at the end of assessment page
function enableKeyDrivers(theForm) {
	var str = new String() ;

	if(document.getElementById){
		//thisbrowser="NN6";
		bebacklayer = document.getElementById("key_drivers");
	}
	if (document.all) {
		//thisbrowser="ie"
		bebacklayer = document.all["key_drivers"];
	}

	if (bebacklayer) {
		if (theForm.intentionality.value != "0") {	
			bebacklayer.style.display = "block";
		} else {	
			bebacklayer.style.display = "none";
		}
	}
}


function checkIntentionalityReason(theForm)
{
	if(theForm.intentionality.value == "10")
	{
		ShowObj("intentionality_other");
		theForm.intentionality_other.focus();
	}
	else
	{
		HideObj("intentionality_other");
	}
}

function checkKeyDriversReason(theForm)
{
	if(theForm.txt_key_drivers.value == "12")
	{
		ShowObj("key_drivers_other");
		theForm.key_drivers_other.focus();
	}
	else
	{
		HideObj("key_drivers_other");
	}
}

function checkServiceSelectionReason(theForm)
{
	if(theForm.service_guide_other.checked)
	{
		ShowObj("service_guide_other_text");
		theForm.service_guide_other_text.focus();
	}
	else
	{		
		HideObj("service_guide_other_text");
	}
}

function showQuestion(str)
{ 
	checkIntentionalityReason(document.pt);	
	enableKeyDrivers(document.pt);	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="/onlineassessments/include/getKeyDrivers.asp"
	url=url+"?q="+str
	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}	
	
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	document.getElementById("txt_key_drivers").innerHTML=xmlHttp.responseText 
	} 
} 
	
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}			
	return objXMLHttp
}

// end function for intentionality and key drivers question at the end of assessment page


//When the user presses BACK, the same elements should be hidden / shown
function pageReloadCheckAll() {
	checkNumberOfChildren(document.pt);
	checkSponsoringChildOver18(document.pt);
	
	enableKeyDrivers(document.pt);
	showQuestion(document.pt.intentionality.value);
	checkServiceSelectionReason(document.pt);
}

function checkPointSubmit(theForm, showLogin) {
	if (showLogin) {//Q1
		if (theForm.contact_firstname.value == ""){
				alert("Question 1: Please specify your first name.");
				theForm.contact_firstname.focus();
				return false;
		}//if 

//Q2

		if (theForm.contact_surname.value == ""){
				alert("Question 2: Please specify your last name.");
				theForm.contact_surname.focus();
				return false;
		}//if 

//Q3

		if (theForm.email.value == ""){
				alert("Question 3: Please specify your email address.");
				theForm.email.focus();
				return false;
		}//if 
			else if (!isValidEmailAddr(theForm.email.value)){
				alert("Your first email address does not seem to be correct.");
				theForm.email.focus();
				theForm.email.select();
				return false;
			}
			
		email = theForm.email.value;
		email2 = theForm.emailConfirmation.value;
		
		if (email2=="") {
			alert("Please verify your e-mail address.");
			theForm.emailConfirmation.focus();
			return false;
		}
		else {
			if (email != email2) {
				alert("Your e-mail address do not match.");
				theForm.emailConfirmation.focus();
				return false;
			}
		}	
//Q4
		
		login = theForm.customerLogin.value;
		if (login.length < 6 || login.length > 20) {
			alert("Username has to be 6 - 20 characters");
			theForm.customerLogin.focus();
			return false;
			} else {
				for (i=0;i<login.length;i++) {
					c = login.substring(i,i+1);
					if (c == " ") {
						alert("Login can't have any spaces");
						theForm.customerLogin.focus();
						return false;
					}
					if (c == "'") {
						alert("Login can't have '");
						theForm.customerLogin.focus();
						return false;
					}
				}			
			}
		
//Q5
			
			password = theForm.customerPassword.value;
			password2 = theForm.customerPasswordConfirmation.value;
			if (password.length < 6 || password.length > 12) {
				alert("Password has to be 6 - 12 characters");
				theForm.customerPassword.focus();
				return false;
			} else {
				for (i=0;i<password.length;i++) {
					c = password.substring(i,i+1);
					if (c == " ") {
						alert("Password can't have any spaces");
						theForm.customerPassword.focus();
						return false;
					}
					if (c == "'") {
						alert("Password can't have '");
						theForm.customerPassword.focus();
						return false;
					}
				}
			}

//Q6

		if (password != password2) {
				alert("Password and retype have to be the same");
				theForm.customerPasswordConfirmation.focus();
				return false;
			}
	}

//Q7
/*
	if (theForm.age.value == ""){
			alert("Question 6: Please specify your age.");
			theForm.age.focus();
			return false;
	}//if 
*/
	if (theForm.dobDay.value==0) {
		alert("Please select your date of birth.");
		theForm.dobDay.focus();
		return false;
	}
		
	if (theForm.dobMonth.value==0) {
		alert("Please select the month of your birthday.");
		theForm.dobMonth.focus();
		return false;
	}
		
	if (theForm.dobYear.value==0) {
		alert("Please select the year of your birthday.");
		theForm.dobYear.focus();
		return false;
	}

	switch(parseInt(theForm.dobMonth.value,10))
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if(parseInt(theForm.dobDay.value,10) < 1 || parseInt(theForm.dobDay.value,10) > 31)
			{
				alert("Please enter a valid date of birth.");
				theForm.dobDay.focus();
				return false;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if(parseInt(theForm.dobDay.value,10) < 1 || parseInt(theForm.dobDay.value,10) > 30)
			{
				alert("Please enter a valid date of birth.");
				theForm.dobDay.focus();
				return false;
			}
			break;
		case 2:
			maxDobDay = 28;
			if(parseInt(theForm.dobYear.value,10) % 4 == 0)
			{
				maxDobDay = 29;
			}
			if(parseInt(theForm.dobDay.value,10) < 1 || parseInt(theForm.dobDay.value,10) > maxDobDay)
			{
				alert("Please enter a valid date of birth.");
				theForm.dobDay.focus();
				return false;
			}
			break;
	}
	
//Q8

	if (theForm.gender.value == "") {
		alert("Question 7: Please specify your gender.");
		theForm.gender.focus();
		return false;
	}
	
//Q9

	if (theForm.marital_status.value == "") {
		alert("Question 8: Please specify your marital status.");
		theForm.marital_status.focus();
		return false;
	}
	
//Q10

	if (theForm.numChildren.value == "") {
		alert("Question 9: Please specify how many children you have.");
		theForm.numChildren.focus();
		return false;
	}
	//Q5
	if (theForm.numChildren.value == "1") {
		if ((!theForm.childInOz[0].checked) && (!theForm.childInOz[1].checked)) {
			alert("Question 10: Please specify whether your child lives in Australia.");
			theForm.childInOz[0].focus();
			return false;
		}
	} else {
		if (theForm.numChildrenInOz.value == "-1") {
			alert("Question 10: Please specify how many of your children live in Australia.");
			theForm.numChildrenInOz.focus();
			return false;
		}
		if (theForm.numChildrenInOz.value > theForm.numChildren.value) {
			alert("Question 10: You can't have more children in Australia than your total number of children (see Question 9).");
			theForm.numChildrenInOz.focus();
			return false;
		}
	}
	//Q6
	if ((!theForm.childWillingToSponsor[0].checked) && (!theForm.childWillingToSponsor[1].checked)) {
		alert("Question 11: Please specify if you have a child in Australia who is willing to sponsor you.");
		theForm.childWillingToSponsor[0].focus();
		return false;
	}
	//Q7
/*	if ((!theForm.childWillingToSponsorOver18[0].checked) && (!theForm.childWillingToSponsorOver18[1].checked)) {
		alert("Question 12: Please specify whether your child is above 18.");
		theForm.childWillingToSponsorOver18[0].focus();
		return false;
	}*/
	//check rest of form based on selection of Q7
	if (theForm.childWillingToSponsorOver18[0].checked) { //child is over 18
		//Q8
		if (theForm.childWillingToSponsorDescription.value == "-1") {
			alert("Question 13: Please specify the status of your sponsoring child.");
			theForm.childWillingToSponsorDescription.focus();
			return false;
		}
		//Q9
		if (theForm.childWillingToSponsorYearsInOz.value == "-1") {
			alert("Question 14: Please specify how long your sponsoring child has been living in Australia.");
			theForm.childWillingToSponsorYearsInOz.focus();
			return false;
		}
	} else { //child is NOT willing / able to sponsor
		//Q7b
/*		if ((!theForm.someoneElseSponsorOver18[0].checked) && (!theForm.someoneElseSponsorOver18[1].checked)) {
			alert("Addition to Question 12: Please specify if there is anyone else who is over 18 and can sponsor you.");
			theForm.someoneElseWillingToSponsorOver18.focus();
			return false;
		}*/
		//Q8
		if (theForm.someoneElseWillingToSponsorDescription.value == "-1") {
			alert("Question 13: Please specify the status of your sponsor.");
			theForm.someoneElseWillingToSponsorDescription.focus();
			return false;
		}
		//Q9
		if (theForm.someoneElseWillingToSponsorYearsInOz.value == "-1") {
			alert("Question 14: Please specify how long your sponsor has been living in Australia.");
			theForm.someoneElseWillingToSponsorYearsInOz.focus();
			return false;
		}
	}
	
	// Validate Q16  ----------
		if (theForm.intentionality.value == "0") {
		alert("Please tell us how far into the immigration process you are currently.");
		theForm.intentionality.focus();
		return false;
		}
		
		/*
		if (theForm.intentionality.value="10" && theForm.intentionality_other.value=="") {
			alert("Please tell us how far into the immigration process you are currently.");
			theForm.intentionality_other.focus();
			return false;
		}
		*/	
	
		if (theForm.txt_key_drivers.value == "0") {
			alert("Please specify what key factor would allow you to proceed further.");
			theForm.txt_key_drivers.focus();
			return false;
		}
	
		/*
		if (theForm.txt_key_drivers.value="12" && theForm.key_drivers_other.value=="") {
			alert("Please specify what key factor would allow you to proceed further.");
			theForm.key_drivers_other.focus();
			return false;
		}
		*/
	
		if (!(theForm.help_prepare_visa.checked || theForm.prepare_on_behalf.checked || theForm.specific_aspects.checked || theForm.help_moving.checked || theForm.help_job.checked || theForm.info_life.checked || theForm.service_guide_other.checked)) {
			alert("Please specify in what key way(s) would you like us to assist you.");
			return false;
		}
	
		if (theForm.service_guide_other.checked && theForm.service_guide_other_text.value=="") {
			alert("Please specify in what key way(s) would you like us to assist you.");
			theForm.service_guide_other_text.focus();
			return false;
		}
		// End Q16 Validation ---------------------
	
	return true;
}