function findObj(theObj, theDoc){
  	var p, i, foundObj;
  
  	if(!theDoc) theDoc = document;
  	if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  	{
    	theDoc = parent.frames[theObj.substring(p+1)].document;
    	theObj = theObj.substring(0,p);
  	}
  	if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  	for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    	foundObj = theDoc.forms[i][theObj];
  	for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    	foundObj = findObj(theObj,theDoc.layers[i].document);
  	if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  	return foundObj;
}

function getStores(oRegion){
	sVal = oRegion.options[oRegion.selectedIndex].value;
	hideAllStores(oRegion);
	if(sVal.indexOf('~')==-1){
		displayStore(sVal);
	}
}
function hideAllStores(oRegion){
	var storeField =document.getElementById('enquiryStore');
	if (storeField!=null){
		storeField.style.display ='none';
	}
	for(var i=0; i<oRegion.options.length; i++){
		var sVal = oRegion.options[i].value;
		if(sVal.indexOf('~')==-1){
			oSel = findObj(sVal);
			if(oSel){
				oSel.style.display = 'none';
				oSel.selectedIndex=0;
			}
		}
	}
}
function displayStore(sVal){
	oSel = findObj(sVal);
	var storeField =document.getElementById('enquiryStore');
	if(oSel){
		if (storeField!=null){
			storeField.style.display ='';
		}
		oSel.style.display = 'block'
	};
}

var errorMessage = "";
function validateQuickEnquiryForm_v2(ref) {
//givenNames
//region
	var validform = true;
	errorMessage = "";
	var v1 = validateEnquiryName();
	var v2 = validateEnquiryPhone();
	var v3 = validateEnquiryEmail();
	var v4 = validateEnquiryRegion();
	var v5 = validateEnquiryStore();
	validform = v1 && v2 && v3 && v4 && v5;
	if (!validform) {
		alert(errorMessage);
	}
	return validform;
}

function validateEnquiryName() {
	var validated = true;
	var givenName=$("#givenNames").val();
	if (givenName=="" || givenName.toLowerCase()=="name") {
		validated = false;
		errorMessage += "Name field is required\n";
	}
	return validated;
}

function checkValidEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	testresults=false
	if (filter.test(str)) testresults=true
	return (testresults)
}

function validateEnquiryEmail() {
	var validated = true;
	contactPref=$('input[name=contactPref]:checked').val() ;
	if (contactPref=="contactEmail") {
		email=$('#email').val();
		if (email=="" || email=="Email address") {
			validated = false;
			errorMessage += "Email address field is required\n";
		} else {
			if (!checkValidEmail(email)) {
				validated = false;
				errorMessage += "Email address field does not contain a valid email address\n";
			}
		}
	}
	return validated;
}

function validateEnquiryPhone() {
	var validated = true;
	contactPref=$('input[name=contactPref]:checked').val() ;
	if (contactPref=="contactPhone") {
		var phone=$('#phone').val();
		if (phone=="" || phone=="(__) ___ ____") {
			validated = false;
			errorMessage += "Phone field is required\n";
		}
	}
	return validated;
}

function validateEnquiryRegion(){
	var validated = true;
	region=$("#region").val();
	if (region=='') {
		validated = false;
		errorMessage += "Region is required\n";
	}
	return validated;
}

function validateEnquiryStore(){
	var validated = true;
	var oForm = document.onlineEnquiryForm;
	region=$("#region").val();
	if(region.indexOf('storediv-')>-1 && region!=''){
		    if (oForm.elements[region].value==''){
			    validated = false;
			    errorMessage += "Store is required\n";
			}
	}
	return validated;
}
