/*************************************************************
                 WEBSITE  DEVELOPED BY
           VEBRA GRAPHICS (VEBRA SOLUTIONS LTD.)
                      COPYRIGHT 2005

      AUTHOR: DAVID SWALLOW (david.swallow@vebra.com)
                        21/12/2005

                      www.vebra.info
/*************************************************************/ 

//A means of adding multiple events to the onload event handler
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  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=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function dreamweaver_nonsense() {
MM_preloadImages('images/link-main-property_search-2.gif','images/link-main-joinlist-2.gif','images/link-main-seller-2.gif','images/link-main-mortgage-2.gif','images/link-main-lettings-2.gif','images/link-main-about-2.gif','images/link-main-contact-2.gif','images/link-sub-sitemap-2.gif','images/link-sub-reginfo-2.gif','images/link-sub-usefulinfo-2.gif','images/link-main-home2.gif');
}

addLoadEvent(dreamweaver_nonsense);

//Brings the associated form field into focus when the text within a label is clicked
function focusLabels() {
	if (!document.getElementsByTagName) return false;
	var labels = document.getElementsByTagName("label");
	for (var i=0; i<labels.length; i++) {
		if (!labels[i].getAttribute("for")) continue;
		labels[i].onclick = function() {
			var id = this.getAttribute("for");
			if (!document.getElementById(id)) return false;
			var element = document.getElementById(id);
			element.focus();
		}
	}
}
addLoadEvent(focusLabels);

//Creates default placeholder text, which disappears and reappears automatically
function resetFields(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.type == "reset"||element.type == "submit"||element.type == "checkbox") continue;
		if (!element.defaultValue) continue;
		element.onfocus = function() {
			if (this.value == this.defaultValue) {
				this.value = "";
			}
		}
		element.onblur = function() {
			if (this.value == "") {
				this.value = this.defaultValue;
			}
		}
	}
}

//Determines whether the input field has been completed, based on whether the default still exists
function isFilled(field) {
	if (field.value.length < 1 || field.value == field.defaultValue) {
		return false;
	} else {
		return true;
	}
}

//Determines whether the email field has been completed correctly, based on the existence of certain characters
function isEmail(field) {
	if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1)
	{
		return false;
	} else {
		return true;
	}
}



//The function to loop through the checkboxes and check that at least one returns true
function validateCheckbox() {
	
}

//Draws upon the previous two functions to validate all form fields, based on whether the class "required" or "email" is present
function validateForm(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
		var element = whichform.elements[i];
		if (element.className.indexOf("required") != -1) {
			if (!isFilled(element)) {
				alert("Please fill in your "+element.name+".");
				return false;
			}
		}
		if (element.className.indexOf("email") != -1) {
			if (!isEmail(element)) {
				alert("Please enter a valid e-mail address.");
				return false;
			}
		}
	}
	
	if (document.getElementById("regForm")) {
		var types = document.getElementById("typeTable").getElementsByTagName("input"); //creates an array called 'locations'
		for (var i=0; i< types.length; i++) { //loops through each of the 'location' elements
			if (types.item(i).checked == true) {
				var typChecked = 1;
			}
		}
		if (!typChecked) {
			alert ("Please select a property type");
			return false;
		}
		
		var locations = document.getElementById("locTable").getElementsByTagName("input"); //creates an array called 'locations'
		for (var i=0; i< locations.length; i++) { //loops through each of the 'location' elements
			if (locations.item(i).checked == true) {
				var locChecked = 1;
			}
		}
		
		if (!locChecked) {
			alert ("Please select a location");
			return false;
		}
		
		var positions = document.getElementById("posTable").getElementsByTagName("input"); //creates an array called 'locations'
		for (var i=0; i< positions.length; i++) { //loops through each of the 'location' elements
			if (positions.item(i).checked == true) {
				var posChecked = 1;
			}
		}
		if (!posChecked) {
			alert ("Please select a buying position");
			return false;
		}
	}
	
	return true;
}

//The function to prepare the previous form enhancements for each form on a page
function prepareForms() {
	for (var i=0; i<document.forms.length; i++) {
		var thisform = document.forms[i];
			resetFields(thisform);
			thisform.onsubmit = function() {
			return validateForm(this);
		}
	}
}
addLoadEvent(prepareForms);
