//reference to last-focused
var lastFocused = 'nothing';

//check all form elements
function checkEls(){
	var el;
	for (i = 0; i < document.forms[0].elements.length; i++){
		el = document.forms[0].elements[i];
		// if this doesn't already have an onClick handler
		// set it to have one that resets lastFocused
		if (!el.onclick)
			el.onclick = function () {lastFocused='nothing';};
	}
	document.body.onclick = function () {lastFocused='nothing';};
}

function goback() {
	history.back();
}

function printPage() {
  if (window.print)
    window.print();
  else
    alert("Sorry, your browser doesn't support this feature. Please print from the browser menu bar.");
} 


function otherFocus(textboxObj, radioGroup, indexNo)
{
	radioGroup[indexNo].checked = true;
	//radioGroup[radioGroup.length-1].checked = true; // last
}

function otherBlur(textboxObj, radioGroup, indexNo)
{
	textboxObj.value = trim(textboxObj.value);
	if (textboxObj.value == "") {
		radioGroup[indexNo].checked = false;
		lastFocused = textboxObj;
	}
}

function otherClick(textboxObj, objClicked)
{
	if (lastFocused == textboxObj) {
		objClicked.checked = false;
	}
	if (objClicked.checked) {
		textboxObj.focus();
	} else {
		textboxObj.value = "";
	}
	lastFocused = objClicked;
}

//checks value is correct email syntax using reg exp, and trims spaces
function Email_onblur(emailField) {

	var Email = trim(emailField.value);
	emailField.value = Email;

	if (Email != "") {
		var re = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if (!re.test(Email)) {
			alert("Please make sure the email address you have entered is correct.");
			emailField.focus();
			return false;
		}
	}
	return true;
}

//trim leading and trailing spaces from a text box
function trimFld(myfield) {	
	myfield.value = trim(myfield.value);
}

//remove leading & trailing spaces
function trim(strText) { 
	strText = strText.replace(/^\s+/, ""); //remove leading spaces
	strText = strText.replace(/\s+$/, ""); //remove trailing spaces
	return strText;
}

//allow only number in input box
// CL - now allows enter, tab, del, backspace to happen in non IE 
//      (IE let them through anyway because they didn't trigger an event)
function KeyCheck(e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (((keycode>47) && (keycode<58) )  || (keycode==0) || (keycode==127) || (keycode==9) || (keycode==8) || (keycode==13)) { return true; }
	else {
		alert("Please type in numbers 0-9 only. No spaces please");
		return false;
	}
}

//allow only number & space in input box
function KeyCheckSpace(e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (((keycode>47) && (keycode<58) )  || (keycode==0) || (keycode==127) || (keycode==9) || (keycode==8) || (keycode==13) || (keycode==32)) { return true; }
	else {
		alert("Please type in numbers or spaces only.");
		return false;
	}
}

function State_onChange(f) {
	with (f) {
		//Australia
		switch(State.selectedIndex) {
			//Vic
			case 0:
				ChangeCountry(f, 1);
				HideOther(f,"divt1");
				SetAreaCodes(f, "03");
				break;
			//Nsw
			case 1:
				ChangeCountry(f, 1);
				HideOther(f,"divt1");
				SetAreaCodes(f, "02");
				break;
			//Qld
			case 2:
				ChangeCountry(f, 1);
				HideOther(f,"divt1");
				SetAreaCodes(f, "07");
				break;
			//SA
			case 3:
				ChangeCountry(f, 1);
				HideOther(f,"divt1");
				SetAreaCodes(f, "08");
				break;
			//NT
			case 4:
				ChangeCountry(f, 1);
				HideOther(f,"divt1");
				SetAreaCodes(f, "08");
				break;
			//WA
			case 5:
				ChangeCountry(f, 1);
				HideOther(f,"divt1");
				SetAreaCodes(f, "08");
				break;
			//Tas
			case 6:
				ChangeCountry(f, 1);
				HideOther(f,"divt1");
				SetAreaCodes(f, "03");
				break;
			//Act
			case 7:
				ChangeCountry(f, 1);
				HideOther(f,"divt1");
				SetAreaCodes(f, "02");
				break;				
			default:
				ChangeCountry(f, 0);
				SetAreaCodes(f, "");
				ShowOther(f, 'divt1');
				break;
		}	//switch
	}//with
}

function ChangeCountry(form, newIndex) {
	form.Country.selectedIndex = newIndex;
}

function SetAreaCodes(f, newVal) {
	f.PhoneArea.value = newVal;
	f.FaxArea.value = newVal;
}

var isIE=document.all?true:false; 
var isDOM=document.getElementById?true:false; 
var isNS4=document.layers?true:false;
function ShowHide(hide,_w) {
  	if (isDOM) {
    	if (!hide) {
    		document.getElementById(_w).style.display='inline';
    		document.getElementById(_w).style.visibility='visible';
    	}
    	else {
    		document.getElementById(_w).style.display='none';
    		document.getElementById(_w).style.visibility='hidden';
    	}
  	}
  	else if (isIE) {
    	if (!hide) {
    		eval("document.all."+_w+".style.display='inline';");
    		eval("document.all."+_w+".style.visibility='visible';");
    	}
    	else {
    		eval("document.all."+_w+".style.display='none';");
    		eval("document.all."+_w+".style.visibility='hidden';");
    	}
  	}
  	else if(isNS4) {
    	if (!hide) {
    		eval("document.layers['"+_w+"'].display='inline';");
    		eval("document.layers['"+_w+"'].visibility='show';");
    	}
    	else {
    		eval("document.layers['"+_w+"'].display='none';");
    		eval("document.layers['"+_w+"'].visibility='hide';");
    	}
  	}
}

function ShowOther(f,_w) {
	ShowHide(false,_w);
	f.State_Other.focus();
}

function HideOther(f,_w) {
	ShowHide(true,_w);
	f.State_Other.value="";
}

function CheckBlank(f,tb) {
	tb.value = trim(tb.value)
	if (tb.value == "") {
		//hide this & reset title select
		f.State.selectedIndex = 0;
		ShowHide(true,"divt1");
	}
}

function CheckState(sel) {
//	if (sel.selectedIndex != 1) {
	if (sel[sel.selectedIndex].value.toLowerCase()!="australia") {
	//non australia, so swap state to other
		sel.form.State.selectedIndex = 8;
		ShowOther(sel.form, "divt1");
		
		//also blank out area codes
		SetAreaCodes(sel.form, "");
	}
	else {
		if (sel.form.State.selectedIndex == 8) sel.form.State.selectedIndex=0;
		HideOther(sel.form, "divt1");
	}	
}


function Title_onchange(f,_w) {
	var hide;
	if (f.Title.options[f.Title.selectedIndex].value == "Other")
		hide = false;	//visible
	else
		hide = true;	//hidden

	ShowHide(hide,_w);
	
  	//Other is selected, so move focus to Title_Other. Otherwise, move focus to FirstName
  	if (!hide) {
  		f.Title_Other.focus();
  	}
  	else {
  		//if it is hidden again, reset its value to blank
  		f.Title_Other.value = '';
  		f.FirstName.focus();
  	}
}

function Title_Other_onblur(f) {
	f.Title_Other.value = trim(f.Title_Other.value)
	if (f.Title_Other.value == '') {
		//hide this & reset title select
		f.Title.selectedIndex = 0;
		ShowHide(true,"divt1");
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function isNumeric(elem, warning){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(warning);
		elem.focus();
		return false;
	}
}

