function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function prepareForms(){
	if(document.getElementById("fm_AdType")){
		document.getElementById("fm_AdType").onblur = function(){return hideHospital();}
		if(document.getElementById("fm_Deanery").getAttribute("class")){
			document.getElementById("fm_Deanery").setAttribute("class","required maxwidth");
			document.getElementById("fm_Hospital1").setAttribute("class","required");
		}else{
			document.getElementById("fm_Deanery").className="required maxwidth";
			document.getElementById("fm_Hospital1").className="required";	
		}
	}
	//this is the property finder form select - it decides whether to show the other input box derectly below it
	if(document.getElementById("aid")){
		document.getElementById("aid").onchange = function(){return hideTownOther();}
		return hideTownOther();
	}
	if(document.getElementById("fm_UserType")){
		document.getElementById("fm_UserType").onblur = function(){return hideGMCGrade();}
		if(document.getElementById("fm_UserType").value=="PL") hideGMCGrade();
	}
	for (var i=0;i<document.forms.length;i++){
		var thisform=document.forms[i];
		thisform.onsubmit = function(){
			return validateForm(this);
		}
	}
}
function hideTownOther(){
	var element=document.getElementById("aid");
	var townOther=document.getElementById("TownOther");
	if(element.value=='0'){
		if(townOther.getAttribute("class")){
			townOther.setAttribute("class","normal");
		}else{
			townOther.className="normal";			
		}
	}else{
		if(townOther.getAttribute("class")){
			townOther.setAttribute("class","hidden");
		}else{
			townOther.className="hidden";			
		}	
	}
}
function hideGMCGrade(){//alert("here");
	var UserType=document.getElementById("fm_UserType");
	if(UserType.value=='PL'){
		if(document.getElementById("gmc")){
			var gmc=document.getElementById("gmc");
			if(gmc.getAttribute("class")){
				gmc.setAttribute("class","hidden");
			}else{
				gmc.className="hidden";			
			}
		}
		if(document.getElementById("grade")){
			var grade=document.getElementById("grade");
			if(grade.getAttribute("class")){
				grade.setAttribute("class","hidden");
			}else{
				grade.className="hidden";			
			}
		}
		if(document.getElementById("fm_GMC")){
			var fm_GMC=document.getElementById("fm_GMC");
			if(fm_GMC.getAttribute("class")){
				fm_GMC.setAttribute("class","");
			}else{
				fm_GMC.className="";			
			}
		}
		if(document.getElementById("fm_Grade")){
			var fm_Grade=document.getElementById("fm_Grade");
			if(fm_Grade.getAttribute("class")){
				fm_Grade.setAttribute("class","");
			}else{
				fm_Grade.className="";			
			}
		}
	}
	if(UserType.value!='PL'){//alert(document.getElementById("fm_Deanery").className);
		if(document.getElementById("gmc")){
			var gmc=document.getElementById("gmc");
			if(gmc.getAttribute("class")){
				gmc.setAttribute("class","normal required");
			}else{
				gmc.className="normal required";			
			}
		}
		if(document.getElementById("grade")){
			var grade=document.getElementById("grade");
			if(grade.getAttribute("class")){
				grade.setAttribute("class","normal required");
			}else{
				grade.className="normal required";			
			}
		}
		if(document.getElementById("fm_GMC")){
			var fm_GMC=document.getElementById("fm_GMC");
			if(fm_GMC.getAttribute("class")){
				fm_GMC.setAttribute("class","required numeric gmc");
			}else{
				fm_GMC.className="required numeric gmc";			
			}
		}
		if(document.getElementById("fm_Grade")){
			var fm_Grade=document.getElementById("fm_Grade");
			if(fm_Grade.getAttribute("class")){
				fm_Grade.setAttribute("class","required");
			}else{
				fm_Grade.className="required";			
			}
		}
	}
}
function hideHospital(){//alert("here");
	var adType=document.getElementById("fm_AdType");
	if(adType.value=='1'){
		if(document.getElementById("hospitalDetails")){
			var hospitalDetails=document.getElementById("hospitalDetails");
			if(hospitalDetails.getAttribute("class")){
				hospitalDetails.setAttribute("class","hidden");
			}else{
				hospitalDetails.className="hidden";			
			}
		}
		if(document.getElementById("deanery")){
			var deanery=document.getElementById("deanery");
			if(deanery.getAttribute("class")){
				deanery.setAttribute("class","hidden");
			}else{
				deanery.className="hidden";			
			}
		}
		if(document.getElementById("costLabel")){
			var costLabel=document.getElementById("costLabel");
			costLabel.childNodes[0].nodeValue="Weekly Rental (£)";
		}
	}
	if(adType.value!='1'){//alert(document.getElementById("fm_Deanery").className);
		if(document.getElementById("hospitalDetails")){
			var hospitalDetails=document.getElementById("hospitalDetails");
			if(hospitalDetails.getAttribute("class")){
				hospitalDetails.setAttribute("class","");
			}else{
				hospitalDetails.className="";			
			}
		}
		if(document.getElementById("deanery")){
			var deanery=document.getElementById("deanery");
			if(deanery.getAttribute("class")){
				deanery.setAttribute("class","");
			}else{
				deanery.className="";			
			}
		}
		if(document.getElementById("costLabel")){
			var costLabel=document.getElementById("costLabel");
			costLabel.childNodes[0].nodeValue="Value / Monthly Rental (£)";
		}
	}
}
function validateForm(myForm){
	if(document.getElementById("fm_AdType")){
		var adType=document.getElementById("fm_AdType");
		if(adType.value=='1'){//alert(document.getElementById("fm_Deanery").className);
			if(document.getElementById("fm_Deanery").getAttribute("class")){
				document.getElementById("fm_Deanery").setAttribute("class","");
				document.getElementById("fm_Hospital1").setAttribute("class","");
			}else{
				document.getElementById("fm_Deanery").className="";
				document.getElementById("fm_Hospital1").className="";		
			}
		}else{
			if(document.getElementById("fm_Deanery").getAttribute("class")){
				document.getElementById("fm_Deanery").setAttribute("class","required");
				document.getElementById("fm_Hospital1").setAttribute("class","required");
			}else{
				document.getElementById("fm_Deanery").className="required";
				document.getElementById("fm_Hospital1").className="required";		
			}
		}
	}
	for(var i=0;i<myForm.elements.length;i++){
		var element = myForm.elements[i];
		if(element.className.indexOf("required") != -1){
			if(!isFilled(element)){
				myElementName = element.name.replace("fm_","");
				myElementName = myElementName.replace("_"," ");
				if(element.className.indexOf("decision") != -1){
					myDecision=confirm("You have not provided an image. Click Cancel if this was a mistake.");
					return myDecision;
				}else{
					alert("Please fill in the  "+myElementName+" field");
					var myDiv = element.parentNode;
					var myLabels = myDiv.getElementsByTagName("LABEL");
					if(myLabels.length==1) myLabels[0].className+=" error";
					return false;
				}
			}
		}
		if(element.className.indexOf("numeric") != -1){
			if(!IsNumeric(element)){
				myElementName = element.name.replace("fm_","");
				myElementName = myElementName.replace("_"," ");
				alert("Please input only numbers in the  "+myElementName+" field");
				var myDiv = element.parentNode;
				var myLabels = myDiv.getElementsByTagName("LABEL");
				if(myLabels.length==1) myLabels[0].className+=" error";
				return false;
			}
		}
		if(element.className.indexOf("gmc") != -1){
			if(element.value.length>7){
				myElementName = element.name.replace("fm_","");
				myElementName = myElementName.replace("_"," ");
				alert("GMC number too long ");
				var myDiv = element.parentNode;
				var myLabels = myDiv.getElementsByTagName("LABEL");
				if(myLabels.length==1) myLabels[0].className+=" error";
				return false;
			}
			if(element.value==0000000){
				myElementName = element.name.replace("fm_","");
				myElementName = myElementName.replace("_"," ");
				alert("GMC number not valid ");
				var myDiv = element.parentNode;
				var myLabels = myDiv.getElementsByTagName("LABEL");
				if(myLabels.length==1) myLabels[0].className+=" error";
				return false;
			}
		}
		if(element.className.indexOf("email") != -1){
			if(!IsEmail(element)){
				myElementName = element.name.replace("fm_","");
				myElementName = myElementName.replace("_"," ");
				alert(myElementName+" appears to be in wrong format.");
				var myDiv = element.parentNode;
				var myLabels = myDiv.getElementsByTagName("LABEL");
				if(myLabels.length==1) myLabels[0].className+=" error";
				return false;
			}
		}
		if(element.id.indexOf("email2") != -1){
			$email1 = document.getElementById("email1").value;
			$email2 = element.value;
			if($email1 != $email2){
				myElementName = element.name.replace("fm_","");
				myElementName = myElementName.replace("_"," ");
				alert(myElementName+" doesn't match email above.");
				var myDiv = element.parentNode;
				var myLabels = myDiv.getElementsByTagName("LABEL");
				if(myLabels.length==1) myLabels[0].className+=" error";
				return false;
			}
		}
	}
}
function isFilled(field){
	if(field.type=="text"){
		if(field.value.length <1 ){
			return false;
		}
	}
	if(field.type=="checkbox"){
		if(field.checked== false){
			return false;
		}
	}
	if(field.type=="select-one"){
		if(field.selectedIndex== false){
			return false;
		}
	}
	if(field.type=="file"){
		if(field.value.length <1 ){
			return false;
		}
	}
	return true;
}
function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.value.length && IsNumber == true; i++) 
      { 
      Char = sText.value.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
function IsEmail(field){
	if(field.value.indexOf("@")==-1||field.value.indexOf(".")==-1){
		return false;
	}else{
		return true;
	}
}
addLoadEvent(prepareForms);
