//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 ClearText(objradio)
{
	objradio.value="";
}

function otherBlur(textboxObj, radioGroup, indexNo)
{
	textboxObj.value = trim(textboxObj.value);
	
	if (textboxObj.value == "") 
	{
		radioGroup[indexNo].checked = false;
		lastFocused = textboxObj;
	}

	if (radioGroup[indexNo].type == "radio" && textboxObj.name.indexOf(radioGroup[indexNo].name)!=-1 )
	{
		//textboxObj.value="";
	}

}

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.value) {
			//Vic
			case "VIC":
				ChangeCountry(f, 1);
				SetAreaCodes(f, "03");
				break;
			//Nsw
			case "NSW":
				ChangeCountry(f, 1);
				SetAreaCodes(f, "02");
				break;
			//Qld
			case "QLD":
				ChangeCountry(f, 1);
				SetAreaCodes(f, "07");
				break;
			//SA
			case "SA":
				ChangeCountry(f, 1);
				SetAreaCodes(f, "08");
				break;
			//NT
			case "NT":
				ChangeCountry(f, 1);
				SetAreaCodes(f, "08");
				break;
			//WA
			case "WA":
				ChangeCountry(f, 1);
				SetAreaCodes(f, "08");
				break;
			//Tas
			case "TAS":
				ChangeCountry(f, 1);
				SetAreaCodes(f, "03");
				break;
			//Act
			case "ACT":
				ChangeCountry(f, 1);
				SetAreaCodes(f, "02");
				break;				
			case "Other":
				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.value="";
	f.State_Other.focus();
	
}


function CheckBlank(f,tb) {
	tb.value = trim(tb.value)
	if (tb.value == "") {
		//hide this & reset title select
		f.State.selectedIndex = 0;
		ShowHide(true,"divt1");
		ChangeCountry(f, 1);
		SetAreaCodes(f, "03");
	}
}

function CheckState(sel) {
	if (sel.selectedIndex != 1) {
		//non australia, so swap state to other
		sel.form.State.selectedIndex = 8;
		//ShowOther(sel.form, "divt1");
		
		//also blank out area codes
		SetAreaCodes(sel.form, "");
	}
}


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;
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function checkEmail(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Please enter a valid email address")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Please enter a valid email address")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Please enter a valid email address")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Please enter a valid email address")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Please enter a valid email address")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Please enter a valid email address")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    alert("Please enter a valid email address")
	    return false
	 }

	 return true					
}
