//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 Country_onChange(f){

	var countryCode;
	if(f.Country.value.split("|").length == 2){
		countryCode = f.Country.value.split("|")[0];
		SetCountryCodes(f,countryCode);
	}else{
		SetCountryCodes(f,"");
	}
}
function SetCountryCodes(f, newVal) {
	f.PhoneCountry.value = newVal;
	f.PhoneArea.value = "";
	f.FaxCountry.value = newVal;
	f.FaxArea.value = "";
	f.MobileCountry.value = newVal;
}
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) {
		//United Arab Emirates	
		switch(State.selectedIndex) {
			// Abu Dhabi 
			case 0:
				
				ChangeCountry(f, 1);
				SetCountryAreaCodes(f, "971");
				SetAreaCodes(f, "02");
				ShowHide(true,'divt1');
				break;
			//Ajman
			case 1:
				ChangeCountry(f, 1);
				SetCountryAreaCodes(f, "971");
				SetAreaCodes(f, "06");
				ShowHide(true,'divt1');
				break;
			// Dubai
			case 2:
				ChangeCountry(f, 1);
				SetCountryAreaCodes(f, "971");
				SetAreaCodes(f, "04");
				ShowHide(true,'divt1');
				break;
			//Fujairah
			case 3:
				ChangeCountry(f, 1);
				SetCountryAreaCodes(f, "971");
				SetAreaCodes(f, "09");
				ShowHide(true,'divt1');
				break;
			//Ras al-Khaimah
			case 4:
				ChangeCountry(f, 1);
				SetCountryAreaCodes(f, "971");
				SetAreaCodes(f, "07");
				ShowHide(true,'divt1');
				break;
			//Sharjah
			case 5:
				ChangeCountry(f, 1);
				SetCountryAreaCodes(f, "971");
				SetAreaCodes(f, "08");
				ShowHide(true,'divt1');
				break;
			//Umm al-Quwain
			case 6:
				ChangeCountry(f, 1);
				SetCountryAreaCodes(f, "971");
				SetAreaCodes(f, "06");
				ShowHide(true,'divt1');
				break;
			//Act
			//case 7:
			//	ChangeCountry(f, 1);
			//	SetAreaCodes(f, "02");
			//	break;				
			default:
				ChangeCountry(f, 0);
				SetCountryAreaCodes(f, "");
				SetAreaCodes(f, "");
				ShowOther(f, 'divt1');
				break;
		}	//switch
	}//with
}

function Title_onChange(f) {
	with (f) {
		//United Arab Emirates	
		switch(Title.selectedIndex) {	
			case 7:
				ShowTitleOther(f, 'divt_title');
				break;
			default:
				ShowHide(true,'divt_title');
				break;
			
		}	//switch
	}//with
}

function ChangeCountry(form, newIndex) {
	form.Country.selectedIndex = newIndex;
}

function SetAreaCodes(f, newVal) {
	f.PhoneArea.value = newVal;
	f.FaxArea.value = newVal;
}

function SetCountryAreaCodes(f, newVal) {
	f.PhoneCountry.value = newVal;
	f.FaxCountry.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 ShowTitleOther(f,_w) {
	ShowHide(false,_w);
	f.Title_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");
	}
}

function CheckState(sel) {
	if (sel.selectedIndex != 1) {
		//non australia, so swap state to other
		//sel.form.State.selectedIndex = 7;
		//ShowOther(sel.form, "divt1");
		
		//also blank out area codes
		//SetAreaCodes(sel.form, "");
		sel.form.PhoneCountry.value = "";
		sel.form.FaxCountry.value = "";
		sel.form.MobileCountry.value = "";
		sel.form.PhoneArea.value = "";
		sel.form.FaxArea.value = "";
	}
	else {
		//sel.form.State.selectedIndex = 2;
		//ShowHide(true, "divt1");
		sel.form.PhoneCountry.value = "971";
		sel.form.FaxCountry.value = "971";
		sel.form.MobileCountry.value = "971";
		sel.form.PhoneArea.value = "04";
		sel.form.FaxArea.value = "04";
		}
		
		
}


function Title_onchange_1(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;
}

