
var goback = false;		//if this is true, it stops any form validation from running
						//used for when the Back or Back To Start buttons are clicked

function isEmailValid(e) {
	//checks if the specified email address appears to be in a valid format (doesn't actually check it exists)
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)|( )");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(e) && r2.test(e));
}

function showOptional(div, hide, instant) {
	//shows or hides "div", set "instant" when page is loading to skip animation
	div = $("#" + div.replace(/:/g, "\\:"));	//jQuery doesn't like IDs containing ":", escape them
	if (instant) {
		hide? div.hide() : div.show();
	} else {
		hide? div.slideUp('normal') : div.slideDown('normal');
	}	
	//disable required fields in this div, so that they aren't validated
	div.find(":input").attr("disabled", hide? "disabled" : "");
}

function isFormOK(f) {
	//attached to the onSubmit event of the main form
	if (goback) return true;	//skip validation

	//scan all required inputs of this form (those with a class of "required" that aren't disabled)
	var fields = $(":input.required:enabled").get();
	for (var n = 0; n < fields.length; n++) {
		if (isBlank(fields[n].id, fields[n].title)) return false;
	}

	//each page can optionally define a "validateForm()" function which will be called here
	if (typeof(validateForm) == "function" && !validateForm()) return false;

	return true;
}

function showPDF(button, save) {
	//called when the "Save PDF" or "Display PDF" buttons are clicked
	var f = button.form;
	//change the target of the submitted form (which contains TFNs etc)
	if (save) {
		f.action = "pdf/createpdf.php?save=1";
	} else {
		f.action = "pdf/createpdf.php";
		f.target = "_blank";	//display PDF in a new window
	}
	f.submit();
	f.target = "";		//after submitting, reset the form properties
	f.action = ".";		//so that the Back button works correctly
}

function setFocus() {
	//attempts to find the first element of the main form and set the keyboard focus onto it
	$(":input").each(function() {
		if (this.type == "text" || this.type == "radio" || this.type == "select-one") {
			this.focus();
			return false;
		}
	});
	//can't just use $("input[type=text],input[type=radio],select").eq(0).focus() - doesn't return them in document order
}

//John Choi disabled this setFocus function because it caused javascript error on trying to focus on hidden fiedls such as LE existing account name field
//$(setFocus);		//call once the page has fully loaded with jQuery onload method


function getVal(id) {
	//get the current value of a form element, including special case for select lists
	var e = getItem(id);
	if (!e) return;
	return (e.type == "select-one")? e.options[e.selectedIndex].value : e.value;
}

function getItem(id) {
	return document.getElementById(id);
}

function isBlank(id, desc) {
	//if the specified form field is blank, focus on it, show an alert and don't allow form submit to proceed
	if (!getVal(id)) {
		getItem(id).focus();
		if (desc == "") desc = "a required value";
		alert("You must enter " + desc + ".");
		return true;
	}
	return false;
}

function areContactsBlank(prefix, desc) {
	//isBlank for a group of contact numbers (work, home, mobile) - ignore fax
	//"prefix" is the start of the field keys, eg "Company:Director1Contacts"
	if (getItem(prefix + ":WorkPhone").disabled) return false;		//don't need to enter contacts if they are hidden/disabled
	if (!getVal(prefix + ":WorkPhone") && !getVal(prefix + ":HomePhone") && !getVal(prefix + ":MobilePhone")) {
		getItem(prefix + ":WorkPhone").focus();
		alert("You must enter at least one contact number" + desc + ".");
		return true;
	}
	return false;
}

function checkABNorACN(s) {
	if (s == "") return true;				//allow a blank string

	s = new String(s).replace(/\s/g, "");	//strip spaces
	var digits = s.split("");				//split into single chars
	var sum = 0;
	var ok = false;

	if (digits.length == 11) {				//test for ABN
		var weights = new Array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
		digits[0] = new String(parseInt(digits[0]) - 1);	//subtract 1 from first digit
		for (var n = 0; n < 11; n++) {
			sum += parseInt(digits[n]) * weights[n];
		}
		ok = (sum % 89 == 0);				//total mod 89 should equal zero for a valid ABN
	} else if (digits.length == 9) {		//test for ACN/ARBN
		for (var n = 0; n < 8; n++) {
			sum += parseInt(digits[n]) * (8 - n);
		}
		ok = (parseInt(digits[8]) == (10 - (sum % 10)));
	}
	if (ok) return true;
	return confirm("The ABN/ACN you entered does not appear to be valid, do you want to continue anyway?");
}
