var schemeBox = "";
var objValue ="";

// browser sniff and return value of the object...

function getObject(objName) {
	if(document.all) {
		return document.all[objName]; // ie
	}
	if(document.getElementById) {
		return document.getElementById(objName); // W3C DOM Standard
	}
	else if(document.layers) {
		return eval('document.' + objName); // netscape4
	}
	return null; // unknown!
}

//if the field is empty
function IsEmptyValue(objName) {
	var objValue = getObject(objName).value;
	if(objValue != null){
		return objValue.length == 0;
	}
	else {
		return false;
	}
}

//validation script
function CheckForm() {
	var Mphone1 = document.frmOrder.txtMPhoneA.value;
	var Mphone2 = document.frmOrder.txtMPhoneB.value;
	var phone1 = document.frmOrder.txtPhoneA.value;
	var phone2 = document.frmOrder.txtPhoneB.value;
	var phoneprovided = false;
	var LMphone1 = document.frmOrder.txtMPhoneA.value;
	var LMphone2 = document.frmOrder.txtMPhoneB.value;
	var Lphone1 = document.frmOrder.txtPhoneA.value;
	var Lphone2 = document.frmOrder.txtPhoneB.value;
	var addNewClient = document.frmOrder.addToMail.Value;

	//takes the field to the isEmptyValue funtion to validate the value
	if(IsEmptyValue('txtName')) {
		alert("Please enter your name");
		document.frmOrder.txtName.focus();
		return false;
	}
	var schemeBox = getObject('lstRelates');
	if(schemeBox != null && schemeBox.value == "none") {
		alert("Please tell us what your frmOrder is about");
		return false;				
	}
	if(IsEmptyValue('txtEmail')) {
		alert("Please enter in your email address");
		document.frmOrder.txtEmail.focus();
		return false;
	}
	else {
		if(!IsValidEmail(getObject('txtEmail').value)) {
			alert("Please enter a valid email address");
			document.frmOrder.txtEmail.focus();
			document.frmOrder.txtEmail.value = "";
			return false;
		}
	}
	if ((phone1 != "") || (phone2 != "")) {
		if ((phone1 != "") && (phone2 != "")){
			if (isNaN(phone1) || isNaN(phone2)){
				alert ("please ensure you only put numbers into the telephone field");
				document.frmOrder.txtPhoneA.value = "";
				document.frmOrder.txtPhoneB.value = "";
				document.frmOrder.txtPhoneA.focus();
				return false;
			}
			else {phoneprovided = true;}
			}
		else{ 
			alert("Please enter your area code and number");
				document.frmOrder.txtPhoneA.focus();
				return false;
		}
		if (Lphone1.length !=2){
			alert ("Please enter your area code and number");
			document.frmOrder.txtPhoneA.focus();
			return false;			
		}
		if (Lphone2.length !=8){
			alert ("Please enter your area code and number");
			document.frmOrder.txtPhoneB.focus();
			return false;			
		}
	}
	if (phoneprovided == false){
		if ((Mphone1 == "") || (Mphone2 == "")) {
			alert("Please enter your area code and number");
			return false;
		}
		else{
			if (isNaN(Mphone1) || isNaN(Mphone2)){
				alert ("Please ensure you only put numbers into either of the telephone fields");
				document.frmOrder.txtMPhoneA.value = "";
				document.frmOrder.txtMPhoneB.value = "";
				document.frmOrder.txtMPhoneA.focus();
				return false;
			}
			if (LMphone1.length !=2){
				alert ("Please enter a valid mobile number, using both boxes");
				document.frmOrder.txtMPhoneA.focus();
				return false;			
			}
			if (LMphone2.length !=8){
				alert ("Please enter a valid mobile number, using both boxes");
				document.frmOrder.txtPhoneB.focus();
				return false;			
			}
		}
	}
	if((phone1 != "") && (Mphone1 != "")){
		if (isNaN(phone1) || isNaN(Mphone1)){
			alert ("Please ensure you only put numbers into either of the telephone fields");
			document.frmOrder.txtMPhoneA.value = "";
			document.frmOrder.txtPhoneA.focus();
			return false;
		}
		if (Lphone1.length !=2){
			alert ("Please enter your area code and number");
			return false;			
		}
		if (LMphone1.length !=2){
			alert ("Please enter your area code and number");
			return false;			
		}
	}
	if((Mphone1 != "") || (Mphone2 != "")){
		if (isNaN(Mphone1) || isNaN(Mphone2)){
			alert ("Please ensure you only put numbers into either of the telephone fields");
			return false;
		}
		if (LMphone1.length !=2){
			alert ("Please enter your area code and number");
			document.frmOrder.txtMPhoneA.focus();
			return false;			
		}
		if (LMphone2.length !=8){
			alert ("Please enter your area code and number");
			document.frmOrder.txtMPhoneB.focus();
			return false;			
		}
	}
}

//checks that the email string given has a valid structure for an email address
function IsValidEmail(txtEmail) {
	if(txtEmail == null) return true;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(txtEmail);
}

function startCalcSub(fldRef){
	var i = fldRef;
	strParam = "calc("+i+")";
	interval = window.setInterval(strParam,1000);
}
function calc(fldRef){
	var runningTotal=0.0;
	var gst=0.0;
	var totalx=0.0;
	var j = fldRef;
	var q = 0;
	var quantity = parseInt(document.frmOrder["txtQuantity-"+j].value);
	var pricePer = parseFloat(document.frmOrder["txtEach-"+j].value);
	if(quantity > 0 && pricePer > 0){
  		document.frmOrder["txtSubTotal-"+j].value = ((quantity* 1) * (pricePer*1));
		for(q=1;q<=10;q++){
			if(parseFloat(document.frmOrder["txtSubTotal-"+q].value) > 0.0){
			runningTotal = runningTotal + parseFloat(document.frmOrder["txtSubTotal-"+q].value);
			}
		}
		gst = runningTotal/10;
		totalx = runningTotal + gst;
		document.frmOrder["txtTotalSub1"].value = runningTotal.toFixed(2);
		document.frmOrder["txtGst"].value = gst.toFixed(2);
		document.frmOrder["txtTotal"].value = totalx.toFixed(2);
	}
}
function stopCalcSub(){
	clearInterval(interval);
}
