function findObj(n, d) {
  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=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function getImage(imageName)
{
		findObj('img').src = imageName;
}

function submitDoc(formName) { 
  var obj;
 
	if (obj=findObj(formName)!=null) 
	{
		findObj(formName).submit(); 
	}
	else 
	{
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDoc function has the correct id and name.');
	}

}
function swapimage() { 
  var i,j=0,x,a=swapimage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }


function checkForm(formname) {

//loop through all elements in the form, check the type of each and validate accordingly.
//highlight each field that doesnt have a value.
//all values are required except textarea as they are optional comments

var showError = false;
var showEmailError = false;
var showEmail2Error = false;
var showTermsError = false;
var showSizeError = false;
var showPriceError = false;
var showPasswordError = false;
var hi_colour = "#FCC930";
var lo_colour = "#FFFFFF";
var form_colour = "#FFFFFF";
var form_colour2 = "#E6E6E6";
var checkboxMissing=false;
var DeliveryRequired=false;

var radio_btns = [];
for(var i=0; i< document[formname].elements.length; i++) {

//check textbox
if ((document[formname].elements[i].lang == "validate") || (document[formname].elements[i].lang == "validate-terms") || (document[formname].elements[i].lang == "validate-password") || (document[formname].elements[i].lang == "validate-email") || (document[formname].elements[i].lang == "validate-numeric") || (document[formname].elements[i].lang == "validate-height") || (document[formname].elements[i].lang == "validate-width") || (document[formname].elements[i].lang == "validate-depth") || ((document[formname].elements[i].lang == "validate-delivery") && DeliveryRequired)) {

if (document[formname].elements[i].type == "text" || document[formname].elements[i].type == "textarea" || document[formname].elements[i].type == "password") {
//if no value, highlight it

if (((document[formname].elements[i].lang == "validate") || (document[formname].elements[i].lang == "validate-password") || (document[formname].elements[i].lang == "validate-delivery") || (document[formname].elements[i].lang == "validate-width") || (document[formname].elements[i].lang == "validate-height") || (document[formname].elements[i].lang == "validate-depth")) && (document[formname].elements[i].value == "")) {
showError = true;
document[formname].elements[i].style.background=hi_colour;
} else if ((document[formname].elements[i].lang == "validate-email") && (!emailCheck(document[formname].elements[i].value))) {
showEmailError = true;
document[formname].elements[i].style.background=hi_colour;
} else if ((document[formname].elements[i].lang == "validate-password") && (document[formname].elements[i].value.length<6)) {
showPasswordError = true;
document[formname].elements[i].style.background=hi_colour;
} else if ((document[formname].elements[i].lang == "validate-width" && parseFloat(document[formname].elements[i].value) >183) || (document[formname].elements[i].lang == "validate-height" && parseFloat(document[formname].elements[i].value) >183) || (document[formname].elements[i].lang == "validate-depth" && parseFloat(document[formname].elements[i].value) >20) || ((document[formname].elements[i].lang == "validate-width" || document[formname].elements[i].lang == "validate-height" || document[formname].elements[i].lang == "validate-depth") && isNaN(document[formname].elements[i].value) )){
showSizeError = true;
document[formname].elements[i].style.background=hi_colour;
} else if ((document[formname].elements[i].lang == "validate-numeric") && (IsNumeric(document[formname].elements[i].value) ==false)){
showPriceError = true;
document[formname].elements[i].style.background=hi_colour;
} else {
//else dont highlight or un-highlight if its highlighted.
document[formname].elements[i].style.background=lo_colour;
}
}

if ((formname=='registration_details' || formname=='contact') && (document.getElementById('email').value != document.getElementById('email2').value)){
showEmail2Error = true;
document.getElementById('email').style.background=hi_colour;
document.getElementById('email2').style.background=hi_colour;
}

//check radio buttons - create entry in the array for this radio button group
if (document[formname].elements[i].type == "radio") {
btn = document[formname].elements[i];
if(!radio_btns[btn.name]) { radio_btns[btn.name] = 0; } //if button group not already in array, put in array and set to 0
if(btn.checked) { radio_btns[btn.name] = 1; } //if that button is checked, set to 1

}
if (document[formname].elements[i].type == "checkbox") {
btn = document[formname].elements[i];
if(!radio_btns[btn.name]) { radio_btns[btn.name] = 0; } //if button group not already in array, put in array and set to 0
if(btn.checked) { radio_btns[btn.name] = 1; } //if that button is checked, set to 1
}

}

for(var k in radio_btns)
{
if(!radio_btns[k]) {
checkboxMissing=true;
//set that button group to highlighted using the span tag around it.
document.getElementById(k).style.background=hi_colour; 
} else {
document.getElementById(k).style.background=form_colour; 
}


}
}
if (showPasswordError) {
	if (showError) {
		alert("Required information missing. \nPlease ensure you complete all of the higlighted fields.\nPassword must be at least 6 characters.");
	}else{
		alert("Password must be at least 6 characters.");
	}
	return false;
} 
if (showError) {
	alert("Required information missing. \nPlease ensure you complete all of the higlighted fields.");
	return false;
}
else if (showEmailError) {
	alert("Please enter a valid email address.");
	return false;
} 
else if (showEmail2Error) {
	alert("Please ensure you re-type your email address correctly.");
	return false;
} 
else if (showSizeError) {
	alert("Please note that your enqtry must not exceed 183cms x 183cms (including the frame) and must not project more than 15cms from the wall. \nPlease only enter numeric values for the size.");
	return false;
} 
else if (showPriceError) {
	alert("Please ensure the price entered is numeric.");
	return false;
} 
else if (checkboxMissing) {
	alert("Please ensure you agree to all terms and conditions by ticking the appropriate boxes.");
	return false;
} 
else{
	//submitDoc(formname);
	if (((formname=='registration_details' || formname=='ambassador_details' || formname=='friends_details' || formname=='map_registration') && document.getElementById('county').value=='')){
		alert("Please select your county.");
		return false;
	}else if (((formname=='map_registration') && document.getElementById('artmap_type').value=='')){
		alert("Please select your registration type.");
		return false;
	}else if (formname=='registration_details' && (document.getElementById('dob_d').value=='' || document.getElementById('dob_m').value=='' || document.getElementById('dob_y').value=='')){
		alert("Please enter your date of birth.");
		return false;
	}else{
		if (formname=='submission_details'){			
			PromptExit=false;
		}
		return true;
	}
	
}

}

function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	//alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null) {
    //alert("The username doesn't seem to be valid.")
    return false
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>4) {
   //alert("The address must end in a four or three-letter domain, or two letter country.")
   return false
}
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}
return true;
}
