/************************************************************
File:		enquiry.js
Author:		Michelle Schiemann
Date:		10/09/2003
Purpose:	javascript required only for the enquiry pages
*************************************************************/

function submit_form(eventTarget, eventArgument) {
	//this is the postback method that SHOULD be generated by .Net, but doesnt seem to be??
	//renamed from __doPostBack, just in case it starts to generate it again
	var theform;
	if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
//		theform = document.forms["frmEnquiry"];
		theform = document.forms[0];
	}
	else {
//		theform = document.frmEnquiry;
		theform = document.forms[0];
	}
	theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
	theform.__EVENTARGUMENT.value = eventArgument;
	theform.submit();
}

function onLoadFormEnquiry(){setfocus();resize_comments();display_msg();}

function wait_cursor() {
	//sets the mouse cursor to hourglass
	document.body.style.cursor = "wait"; 
	if (document.all){
		for (var i=0; i < document.all.length; i++) {//disable each control
			document.all(i).style.cursor = "wait";
			//document.all(i).enabled = false;
		}
	}
//	if (document.getElementById&&!document.all) {
//		for(i=0; i<document.forms[0].elements.length; i++){
//			document.forms[0].elements[i].style.cursor = "wait";
//		}
//	}
}

function setfocus() {
	//set the focus to the first text box on the form
	//document.all.txtName.focus();
//	document.getElementById('txtName').focus();
	if (document.all){
		for (var i=0; i < document.all.length; i++) {
			sName = new String(document.all(i).name);
			if (sName.search("txtName")!= -1) document.all(i).focus();
		}
	}
}	

function resetfields() {
	//This clears the values of the controls on the form - called from reset button
	if (document.all){
		for (var i=0; i < document.all.length; i++) {
			sName = new String(document.all(i).name);
			if (sName.search("txt")!= -1) { //its a text box
				document.all(i).value='';
			}else if (sName.search("chk")!= -1) { //its a check box
					document.all(i).checked=false;		
			}	
		}
	}
	setfocus();
}

function submit_enq() {
	var message='';
	//variables for each field
	var chkPostId;
	var chkFaxId;
	var chkEmailId;
	var chkPhoneId;
	var txtAddId;
	var txtCityId;
	var txtPostcodeId;
	var txtStateId;
	var txtFaxId;
	var txtEmailId;
	var txtPhoneId;
	var txtNameId;
	//get ids
		for (var i=0; i < document.all.length; i++) {
			sName = new String(document.all(i).name);
			if (sName.search("chkPost")!= -1) chkPostId=sName;
			else if (sName.search("chkFax")!= -1) chkFaxId=sName;
			else if (sName.search("chkEmail")!= -1) chkEmailId=sName;
			else if (sName.search("chkPhone")!= -1) chkPhoneId=sName;
			else if (sName.search("txtAddress")!= -1) txtAddId=sName;
			else if (sName.search("txtCity")!= -1) txtCityId=sName;
			else if (sName.search("txtPostcode")!= -1) txtPostcodeId=sName;
			else if (sName.search("txtState")!= -1) txtStateId=sName;
			else if (sName.search("txtFax")!= -1) txtFaxId=sName;
			else if (sName.search("txtEmail")!= -1) txtEmailId=sName;
			else if (sName.search("txtPhone")!= -1) txtPhoneId=sName;
			else if (sName.search("txtName")!= -1) txtNameId=sName;
		}
	//check data
	//if user wishes to be contacted by post, ensure address is filled in 
	var ele = document.forms[0].elements;
	
//	if (ele['chkPost'].checked==true) {
	if (ele[chkPostId].checked==true) {
//		if ((ele['txtAddress'].value=='') || (ele['txtCity'].value=='')||(ele['txtPostcode'].value=='')||(ele['txtState'].value=='')){
		if ((ele[txtAddId].value=='') || (ele[txtCityId].value=='')||(ele[txtPostcodeId].value=='')||(ele[txtStateId].value=='')){
			message += 'Please enter your full address if you wish us to communicate via post\n';
		}
	}

	//contact via fax
//	if ((ele['chkFax'].checked==true) && (ele['txtFax'].value=='')){
	if ((ele[chkFaxId].checked==true) && (ele[txtFaxId].value=='')){
		message += 'Please enter your fax number if you wish us to communicate via fax\n';
	}
	//contact via email
	if ((ele[chkEmailId].checked==true) && ((ele[txtEmailId].value=='')||(validEmail(ele[txtEmailId].value)==false))){
		message += 'Please enter a valid email address if you wish us to communicate via email\n';
	}
	//contact via phone
	if ((ele[chkPhoneId].checked==true) && (ele[txtPhoneId].value=='')){
		message += 'Please enter your phone number if you wish us to communicate via phone\n';
	}
	if (ele[txtNameId].value=='') {
		message += 'Please enter your Name\n';
	}
	//make sure there is at least one method of communication selected
	if ((ele[chkFaxId].checked==false)&&(ele[chkPostId].checked==false)
		&& (ele[chkPhoneId].checked==false)&& (ele[chkEmailId].checked==false)) {
			message += 'Please let us know your preferred method of communication\n';
	}
	//make sure there is at least one product selected
//	if ((ele['chkCredFinPro'].checked==false)&&(ele['chkPFR'].checked==false)
//		&& (ele['chkCR'].checked==false)&& (ele['chkTI'].checked==false)) {
//			message += 'Please let us know which product you are interested in, so we can best direct your enquiry\n';
//	}
	if (message!='') {
		alert(message);
		setfocus();
	}else{
		wait_cursor();
		submit_form('SubmitForm','');
	}
}

function testEnter() {
	//if user presses enter submit the form  
	if (event.keyCode == 13) {        
		event.cancelBubble = true;
		event.returnValue = false;
		submit_enq();
	}else{
		if (event.keyCode == 27) {        //catch escape and cancel bubble
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
} 

function validEmail(email){
	var bSpecialFlag; var bBadFlag;
	var sepDomain;
	var arr;
	var atChr;
	var atLoop;
	
	var bValidFlag = false;
	if ((email != '') && (email.indexOf("@") != -1) && (email.indexOf(".") != -1)){
		var pos = 0;
		bSpecialFlag = false;
		for(atLoop = 0; atLoop < email.length; atLoop++){
			atChr = email.substr(atLoop, 1)
			if (atChr == '@'){
				pos++;
			}
			if ((atChr.charCodeAt(0) >= 32) && (atChr.charCodeAt(0) <= 44)){
				bSpecialFlag = true;
			}
			if ((atChr.charCodeAt(0) == 47) || (atChr.charCodeAt(0) == 96) || (atChr.charCodeAt(0) >= 123)){
				bSpecialFlag = true;
			}
			if ((atChr.charCodeAt(0) >= 58) && (atChr.charCodeAt(0) <= 63)){
				bSpecialFlag = true;
			}
			if ((atChr.charCodeAt(0) >= 91) && (atChr.charCodeAt(0) <= 94)){
				bSpecialFlag = true;
			}
		} 
		if ((pos == 1) && (bSpecialFlag == false)){
			bBadFlag = false;
			var arr = email.split('@');
			var sUserName = arr[0];
			var sDomainName = arr[1];
			//Domain Validation
			var arrDomain = sDomainName.split('.');
			
			if (sDomainName == 'hotmail.com.au'){
				bBadFlag = true;
			}
			if (sDomainName.indexOf(',') != -1){
				bBadFlag = true;
			}
			var val = arrDomain.length;
			if (val == 2){
				if (arrDomain[1] == 'co' || arrDomain[1] == 'con' || arrDomain[0].toLowerCase() == 'www') {
					bBadFlag = true;	
				} 
			}else if (val == 3){
				if (arrDomain[2].toLowerCase() == 'u' || arrDomain[1].toLowerCase() == 'co' || arrDomain[1].toLowerCase() == 'con'
					|| arrDomain[2].toLowerCase() == 'a' || arrDomain[0].toLowerCase() == 'www'){

					bBadFlag = true;
				}
			}
			if (sUserName == '' || sDomainName == '') {
				bBadFlag = true;
			}
			
			if ((sUserName.indexOf('.') == 0) || (sDomainName.indexOf('.') == 0) || (sDomainName.indexOf('.') == (sDomainName.length - 1))) {
				bBadFlag = true;
			}
			if (sDomainName.lastIndexOf('.') == (sDomainName.length - 1)){
				bBadFlag = true;
			}
			bValidFlag = true; 
		}
	}
	if (bBadFlag == true) {
		bValidFlag = false;
	}
	return bValidFlag;
}

function resize_comments(){
	if (document.all){
		for (var i=0; i < document.all.length; i++) {
			sName = new String(document.all(i).name);
			if (sName.search("txtComments")!= -1) {
				if (screen.width * screen.height <= 480000){
					//screen res is 800 x 600 or less
					document.all(i).style.height='15px';
				}else if (screen.width * screen.height >= 786432){
					//1024 x 768 +
					document.all(i).style.height='50px';
				}
			}
		}
	}
//	if (screen.width * screen.height <= 480000){
//		//screen res is 800 x 600 or less
//		document.getElementById?document.getElementById('txtComments').style.height='15px':document.all('txtComments').style.height='15px';
//	}else if (screen.width * screen.height >= 786432){
//	//1024 x 768 +
//		document.getElementById?document.getElementById('txtComments').style.height='50px':document.all('txtComments').style.height='50px';
//	}
}
