// validate the form
function checkRegistration(theForm){
	var why_reason = theForm.why_reason;
	var member_title = theForm.member_title;
	var firstname = theForm.firstname;
	var surname = theForm.surname;
	var email = theForm.email;
	var AccUsername = theForm.username;
	var AccPassword = theForm.password;
	var ConfirmPass = theForm.password_confirm;
	var agree_to_term = theForm.agree_to_term;
	var why_status = theForm.why_status

    if (!(why_reason==null || why_reason=='undefined'))
    {
	    if (why_reason.selectedIndex >= 0) {
    	
		    if (!checkDropDown(why_reason,"0")){
			    alert("Please select the reason why you want to migrate.");
			    return false;
		    }		    
	    }
	}

	var why_status = theForm.why_status
	if (!(why_status==null || why_status=='undefined'))
    {
	    if (why_status.options.length>0)
	    {
	   		    if (!checkDropDown(why_status,"0")){
			    alert("Please select the status.");
			    return false;
		    }	 
	    }
	}
	
	if (!checkDropDown(member_title,"0")){
		alert("Please select your Title.");
		return false;
	}		    

	if (isEmpty(firstname)){
		alert("Please provide your Given Name / First Name.");
		return false;
	}

	if (isEmpty(surname)){		
		alert("Please provide your Surname / Last Name.");
		return false;
	}
	
	if (isEmpty(email)){
		alert("Please provide your email address.");
		return false;
	}
	else if (!emailCheck(email.value)){
		email.focus();
		email.select();		
		return false;
	}
	
	var password=trimSpaces(AccPassword.value);
	if (password == ""){		
			alert("Please choose a password");
			AccPassword.focus();
			AccPassword.select();		
			return false;
	}
	else if (password.length<4){
			alert("The minimum length of a password is 4 chars");
			AccPassword.focus();
			AccPassword.select();		
			return false;
	}

	var confirmPass = trimSpaces(ConfirmPass.value);
	if ( confirmPass == ""){		
			alert("Please re-type your password");
			ConfirmPass.focus();
			ConfirmPass.select();		
			return false;
	}

	if (password != confirmPass ){		
			alert("Your retyped password does not match. Please try again.");
			AccPassword.focus();
			AccPassword.select();		
			return false;
	}

	if (!agree_to_term.checked){
		alert("You need to agree to the terms and conditions\nof migrationexpert.com to continue");
		return false;
	}
    
	return true;
}

// launch an window for validating username
function checkUsername(theForm){
	var username = trimSpaces(theForm.username.value);
	if (username != ""){
		var firstname = trimSpaces(theForm.firstname.value);
		var surname = trimSpaces(theForm.surname.value);
		if (firstname == "" && surname == ""){
			alert("Please enter your given name and family name");
			theForm.firstname.focus()
			return;
		}
		var checkPage = "/uk/register_validate_login.asp?u=" + username + "&f=" + firstname + "&s=" + surname;
		openHelp(checkPage, 400, 500, 'no');
	}
	else{
		theForm.username.focus()
		alert("Please enter a username");
	}
}

// show the username validation button
function showValidateButton(theControl){
	document.getElementById(theControl).style.display='';
}

// get the description of migration reason based on the ID - harded coded
function getStatus(why_migrate){
	var why_migrate_desc;
	var why_migrate_id = parseInt(why_migrate);
    var why_status = document.getElementById("why_status")
	var reasonString = "REASON FOR MIGRATING TO AUSTRALIA?";

	if (why_migrate_id == 3 || why_migrate_id == 10 || why_migrate_id == 12) {
		reasonString = "YOUR UK VISA PREFERENCE";
	} else {
		reasonString = "YOUR UK VISA PREFERENCE";
	}

    	why_status.options.length = 0 ;
        document.getElementById("status_div").style.display='none';	
        var i ;
        var j;
    	for(i=0;i<reasonRelation.length;i++)
		{
		    if (reasonRelation[i][0].split("/")[0]==why_migrate && reasonRelation[i].length>1)
		    {
		        document.getElementById("status_div").style.display='';	
        		why_status.options.length ++ ;
                why_status[why_status.length-1].value =  "0"
                why_status[why_status.length-1].text =  "Please Select Status"
		        for(j=1;j<reasonRelation[i].length;j++)
		        {
		             why_status.options.length ++ ;
		             why_status[why_status.length-1].value =  reasonRelation[i][j].split("/")[0]
                     why_status[why_status.length-1].text =  reasonRelation[i][j].split("/")[1]
                }

		    }
		}
		setStatus(why_migrate)

}

function setStatus(why_migrate)
{
    document.getElementById("why_migrate").value = why_migrate;
}


function setPreferenceANDStatus(why_migrate)
{

	NotSetStatus = true ;

    
    for(i=0;i<reasonRelation.length;i++)
    {
       
        for (j=0;j<reasonRelation[i].length;j++)
        {
             if (reasonRelation[i][j].split("/")[0]==why_migrate)
             {
                document.getElementById("why_reason").selectedIndex =i+1;
		
                if (NotSetStatus && reasonRelation[i].length>1)
                {
                    getStatus(reasonRelation[i][0].split("/")[0])
                    document.getElementById("status_div").style.display='';
		            NotSetStatus = false ;
                }
                if (j>0)
                {    
                    document.getElementById("why_status").selectedIndex =j;
                    	
                }
             }
        }
    }
}

function LoadWhyReason(why_reason)
{
    why_reason.options.length =1 ;	
    why_reason[0].value =  "0"
    why_reason[0].text =  "Please Select Your Preference"
    for(i=0;i<reasonRelation.length;i++)
    {
        
         why_reason.options.length ++ ;
         why_reason[why_reason.length-1].value =  reasonRelation[i][0].split("/")[0]
         why_reason[why_reason.length-1].text =  reasonRelation[i][0].split("/")[1]
    }

}