
function addLoadEvent(whichfunction) {
	var onload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = whichfunction;
	}
	else {
		window.onload = function() {
			onload();
			whichfunction();
		}
	}
}

function resetFields(whichform) {
	for (var i = 0; i < whichform.elements.length; i ++) {
		var element = whichform.elements[i];
		if (element.type == "submit") continue;
		if (element.type == "radio") 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;
				
			}
		}
	}
}

function prepareForms() {
	for (var i = 0; i < document.forms.length; i ++) {
		var thisform = document.forms[i];
		resetFields(thisform);
		if (thisform.className.indexOf("validate") != -1) {
			thisform.onsubmit = function() {
				return validateForm(this);
			}
		}
	}
}

function validateForm(whichform) {
	validform = true;
	firsterror = null;
	for (var i = 0; i < whichform.elements.length; i ++) {
		var element = whichform.elements[i];
		if (element.className.indexOf("required") != -1) {
			if (!isFilled(element)) {
				writeError(element, "Information required");				
			}
		}
		if (element.className.indexOf("email") != -1) {
			if (!isEmail(element)) {
				writeError(element, "Not a valid email address");
			}
		}
		if (element.className.indexOf("password") != -1) {
			if (!isPassword(element)) {
				writeError(element, "Passwords must contain at least six characters");
			}
		}
	}
	if (firsterror) {
		firsterror.focus();
	}
	if (validform) {
		// alert('All data is valid!');
		return true;
	}else{
	return false;}
}

function writeError(whichelement, message) {
	validform = false;
	if (whichelement.haserror) {
		return;
	}
	whichelement.className += ' error';
	whichelement.onchange = removeError;
	var sp = document.createElement('span');
	sp.className = 'error';
	sp.appendChild(document.createTextNode(message));
	insertAfter(sp, whichelement);
	whichelement.haserror = sp;	
	if (!firsterror) {
		firsterror = whichelement;
	}
}

function removeError() {
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.haserror);
	this.haserror = null;
	this.onchange = null;
}


function isFilled(whichfield) {
	if (whichfield.value.length < 1 || whichfield.value == whichfield.defaultValue) {
		return false;
	}
	else {
		return true;
	}
}

function isEmail(whichfield) {
	var emailRexp = /(\w+[\w|\.|-]*\w+)(@\w+[\w|\.|-]*\w+\.\w{2,4})/;
	if ( emailRexp.test(whichfield.value) == false) {
		return false;
	}
	else {
		return true;
	}
}	

function isPassword(whichfield) {
	if (whichfield.value.length < 6 || whichfield.value == whichfield.defaultValue) {
		return false;
	}
	else {
		return true;
	}
}

function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	}
	else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

function getIndex(value, array) {
    for (var i = 0; i < array.length; i ++) {
		if (array[i] == value) {
			return i;
		}
	}
}

function popUp(url, width, height, popupFeatures)
	{
	var popup = window.open( url, "_blank", "width=" + width + ",height=" + height + "," + popupFeatures );
	return popup.focus();
	}

function redirectAgentSearch ()
    {
    if (document.agentsearch.AgentName.value != "Enter location here" )
    {
        location.href="http://" + location.hostname + "/LatestHomes/agentsearch.aspx?detail=" + escape (document.agentsearch.AgentName.value);
    }
}


addLoadEvent(prepareForms);



