
schedule("window", initForm);


String.prototype.validEmail = function() {
	if (this.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/))
	{
		return true;
	}
	
	return false;
}
function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}
function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isBoolean(a) {
    return typeof a == 'boolean';
}
function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}
function isFunction(a) {
    return typeof a == 'function';
}
function isNull(a) {
    return typeof a == 'object' && !a;
}
function isNumber(a) {
	return typeof a == 'number' && isFinite(a);
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isString(a) {
    return typeof a == 'string';
}
function isUndefined(a) {
    return typeof a == 'undefined';
} 
function valueNaN(i) {
	return !(parseInt(i) > -1);
}
function validEmailFormat(txtValue) {
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	if (! txtValue.match(re)) {
		return (false);
	}
	return (true);
}
function validpostcodeFormat(txtValue) {
	var re = /^([0-9]{1,4})$/
	if (!txtValue.match(re)) {
		return (false);
	}
	return (true);
}

function trim(value){
	if(value.length < 1){
		return"";
	}
	value = rTrim(value);
	value = lTrim(value);
	if(value==""){
		return "";
	} else {
		return value;
	}
} //End Function

function rTrim(value){
	var w_space = String.fromCharCode(32);
	var v_length = value.length;
	var strTemp = "";
	if(v_length < 0){
		return "";
	}
	var iTemp = v_length -1;
	
	while(iTemp > -1){
		if(value.charAt(iTemp) == w_space){
		} else {
			strTemp = value.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;	
	} //End While
	return strTemp;

} //End Function

function lTrim(value){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = value.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length){
		if(value.charAt(iTemp) == w_space){
		} else {
			strTemp = value.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function


function initForm()
{
	if (document.getElementById("contactform"))
	{
		var downloadForm = document.getElementById("contactform");	
		downloadForm.onsubmit = checkForm;

	}
	else if (document.getElementById("kidscorner"))
	{
		var downloadForm = document.getElementById("kidscorner");	
		downloadForm.onsubmit = checkkidsform;

	}
	else if (document.getElementById("stationary"))
	{
		var downloadForm = document.getElementById("stationary");	
		downloadForm.onsubmit = checkstationaryform;

	}
	else if (document.getElementById("careerform"))
	{
		var downloadForm = document.getElementById("careerform");	
		downloadForm.onsubmit = checkcareerform;

	}

};

function checkForm()
{
	if (fieldNotEmpty(document.getElementById("firstname")) == false) return false;
	if (fieldNotEmpty(document.getElementById("lastname")) == false) return false;
	if (fieldNotEmpty(document.getElementById("phone")) == false) return false;
	if (fieldNotEmpty(document.getElementById("message")) == false) return false;
	if (fieldValidEmail(document.getElementById("email")) == false) return false;
	if (fieldValidPostcode(document.getElementById("postcode")) == false) return false;
	
	return true;
};

function checkkidsform()
{
	if (fieldNotEmpty(document.getElementById("firstname")) == false) return false;
	if (fieldValidEmail(document.getElementById("email")) == false) return false;
	
	return true;
};
function checkstationaryform()
{	
	if (fieldNotEmpty(document.getElementById("title")) == false) return false;
	if (fieldNotEmpty(document.getElementById("firstname")) == false) return false;
	if (fieldNotEmpty(document.getElementById("lastname")) == false) return false;
	if (fieldNotEmpty(document.getElementById("practicename")) == false) return false;
	if (fieldNotEmpty(document.getElementById("address")) == false) return false;	
	if (fieldNotEmpty(document.getElementById("suburb")) == false) return false;	
	if (fieldNotEmpty(document.getElementById("postcode")) == false) return false;	
	if (fieldValidPostcode(document.getElementById("postcode")) == false) return false;
	if (quantityAdded(document.getElementById("quantity")) == false) return false;
	//if (fieldNotEmpty(document.getElementById("genReferralPad")) == false || fieldNotEmpty(document.getElementById("mriReferralPad")) == false) return false;	
	
	return true;
};
function checkcareerform()
{	
	if (fieldNotEmpty(document.getElementById("title")) == false) return false;
	if (fieldNotEmpty(document.getElementById("firstname")) == false) return false;
	if (fieldNotEmpty(document.getElementById("lastname")) == false) return false;	
	if (fieldNotEmpty(document.getElementById("postcode")) == false) return false;	
	if (validatepostandmeail(document.getElementById("email"), document.getElementById("phone")) == false) return false;
	if (somethingNotSelected(document.getElementById("jobs"), ("checkbox")) == false) return false;
	
	return true;
};

function checkapplicationform()
{
	if (document.getElementById("title").value == "Unspecified") 
		alert("Please select your title.");
	else if (document.getElementById("firstname").value.length == 0)
		alert("Please enter your first name.");
	else if (document.getElementById("lastname").value.length == 0)
		alert("Please enter your last name.");
	else if (document.getElementById("address").value.length == 0)
		alert("Please enter your street address.");
	else if (document.getElementById("suburb").value.length == 0)
		alert("Please enter your suburb.");
	else if (document.getElementById("postcode").value.length != 4)
		alert("Please enter a 4-digit postcode.");
	else if (document.getElementById("email").value.length == 0)
		alert("Please enter your e-mail address.");
	else if (document.getElementById("confirmemail").value != document.getElementById("email").value)
		alert("Please ensure your e-mail address and confirmation address are identical and correct.");
	else if (document.getElementById("gender").value == "Unspecified")
		alert("Please select your gender.");
	else if (document.getElementById("ausresident").value == "Unspecified")
		alert("Please select your residency status.");
	else if (document.getElementById("ausresident").value == "No" && document.getElementById("visastatus").value == "Unspecified")
		alert("Please select your Visa status.");
	else if ((document.getElementById("visastatus").value == "Sponsorship" || document.getElementById("visastatus").value == "Other") && document.getElementById("visadetails").value.length == 0)
		alert("Please enter details for your Visa status.");
	else if (document.getElementById("position").value == "Unspecified")
		alert("Please select the position you are interested in applying for.");
	else if (document.getElementById("region").value == "Unspecified")
		alert("Please select the region you are interested in applying for.");
	else if (document.getElementById("commute").value == "Unspecified")
		alert("Please indicate if you are able to commute or not.");
	else if (document.getElementById("payrate").value.length == 0)
		alert("Please enter your expected pay rate.");
	else if (document.getElementById("workstatus").value == "Unspecified")
		alert("Please select your preferred work status.");
	else if (!document.getElementById("confirm").checked)
		alert("Please check the box at the end of the form to confirm that you understand the conditions of application.");
	else
		return true;
		
	return false;
}

function checkprivacy()
{
	if (!document.getElementById("confirm").checked)
		alert("Please check the box at the end of the form to confirm that you understand the conditions of application.");
		
	return document.getElementById("confirm").checked;
}

function fieldNotEmpty(fieldElement)
{
	if (trim(fieldElement.value).length == 0)
	{
		//var title = fieldElement.parentNode.getElementsByClassName("label-text")[0].firstChild.nodeValue;
		//var title = fieldElement.parentNode.getElementsByClassName("label-text")[0].firstChild.nodeValue;
		var title = getElementsByClassName(fieldElement.parentNode, 'span', 'label-text')[0].firstChild.nodeValue;
		if (title == "") alert(fieldElement.parentNode.getElementsByTagName("label")[0].childNode[1].value);
		
		showError = getElementsByClassName(fieldElement.parentNode, 'span', 'attention hide')[0].className = "attention";
		
		alert("Please Enter your " + title);
		fieldElement.focus();
		return false;
	}
	
	return true;
};
function somethingNotSelected(wrapperElement, inputType)
{
	var lastElement;
	var somethingChecked = false;
	var inputs = wrapperElement.getElementsByTagName("input");
	for(i=0; i<inputs.length; i++)
	{ 
		lastElement = inputs[i];
		if(inputs[i].checked)
		{
			somethingChecked = true;
		}
	}
	if (!somethingChecked)
	{
		if(wrapperElement.getElementsByTagName("input")[0].type == inputType) alert("Please select at least one option.");
		else alert("Please choose a Position Type.");
		
		showError = getElementsByClassName(wrapperElement.parentNode, 'span', 'attention hide')[0].className = "attention";
		lastElement.focus();
		return false;
	}
	
	return true;
};
function fieldValidPostcode(fieldElement)
{
	if (trim(fieldElement.value).length == 4){
		if (!validpostcodeFormat(fieldElement.value))
		{
			alert("Please Enter a 4 digit Postcode");
			showError = getElementsByClassName(fieldElement.parentNode, 'span', 'attention hide')[0].className = "attention";
			fieldElement.focus();
			return false;
		}
	}else if (trim(fieldElement.value).length > 0){
		alert("Please Enter a 4 digit Postcode");
		showError = getElementsByClassName(fieldElement.parentNode, 'span', 'attention hide')[0].className = "attention";
		fieldElement.focus();
		return false;
	}
	
	return true;
};
function fieldValidEmail(fieldElement)
{
		if (!validEmailFormat(fieldElement.value))
		{
			alert("Please Enter a Valid Email Address");
			showError = getElementsByClassName(fieldElement.parentNode, 'span', 'attention hide')[0].className = "attention";
			fieldElement.focus();
			return false;
		}
		
	return true;
};

function quantityAdded(fieldElement)
{
	var textFields = fieldElement.getElementsByTagName('input');
	
	for (var i=0; i<textFields.length; i++)
	{
		if (trim(textFields[i].value) == 0)
		{
			checker = false;
		}
		else
		{
			if (!validpostcodeFormat(textFields[i].value))
			{
				alert("Please Enter a Valid Quantity.");
				showError = getElementsByClassName(textFields[i].parentNode, 'span', 'attention hide')[0].className = "attention";
				fieldElement.focus();
				return false;
			}
			else
			{
				checker = true;
				break;
			}
		}
	}
	
	if (checker == true){
		return true;
	}else{
		alert("Please Enter a Quantity Amount.");
		showError = document.getElementById("quantity-attention").className = "attention";
		return false;
	}
	
	return true;
};



function validatepostandmeail(fieldElement1, fieldElement2)
{
	if (trim(fieldElement1.value).length == 0  && trim(fieldElement2.value).length == 0)
		{
			alert("Please Enter either a Phone Number or and Email Address");
			showError = getElementsByClassName(fieldElement1.parentNode, 'span', 'attention hide')[0].className = "attention";			
			showError = getElementsByClassName(fieldElement2.parentNode, 'span', 'attention hide')[0].className = "attention";
			fieldElement1.focus();
			return false;
		}
	else if (trim(fieldElement1.value).length > 0)
		{
			if (fieldValidEmail(fieldElement1) == false) return false;
		}
		
	return true;
};
