/*************************************************************
     TEMPLATE BASED WEBSITE DESIGNED AND DEVELOPED BY
           VEBRA GRAPHICS (VEBRA SOLUTIONS LTD.)
                      COPYRIGHT 2006

      AUTHOR: DAVID SWALLOW (david.swallow@vebra.com)
                        27/01/2006 

                      www.vebra.info
/*************************************************************/ 

//*************************
//     RE-USABLE FUNCTIONS
//*************************

//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();
		}
	}
}

//A means of inserting an element AFTER an existing element
function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

//*************************
//     SITE-SPECIFIC FUNCTIONS
//*************************

//Creates a string containing a time-sensitive welcome greeting, which can be added to the onload event handler
function get_greeting()
{
	var theDate = new Date();
	var theHour = theDate.getHours();
	var greeting_text = "Welcome to";
	
		if (theHour < 7)
		{greeting_text = "An EARLY Good Morning and Welcome to ";
		return greeting_text;
		}
		if (theHour > 6 && theHour <12)
		{greeting_text = "Good Morning and Welcome to ";
		return greeting_text;
		}
		if (theHour > 11 && theHour <18)
		{greeting_text = "good afternoon and Welcome to ";
		return greeting_text;
		}
		if (theHour >17)
		{greeting_text = "Good Evening and Welcome to ";
		return greeting_text;
		}
}

//Adds the welcome greeting to the homepage
function homepageGreeting() {
	if (document.getElementById("first_para")) {
		var greeting = document.createElement("h2");
		var greeting_text = document.createTextNode(get_greeting());
		var greeting_name = document.createTextNode("Matheson & Co.");
		
		greeting.appendChild(greeting_text);
		//greeting.appendChild(document.createElement("br"));
		greeting.appendChild(greeting_name);
		
		var greeting_area = document.getElementById("first_para");
		greeting_area.parentNode.insertBefore(greeting,greeting_area);
	} else {
		return false;
	}
}
addLoadEvent(homepageGreeting);

//Creates the Select All / Deselect All function on the Property Search Area List
function selectAll(){
	for (i = 0; i <= 100; i++) {
		if (document.getElementById("box"+i)){
			document.getElementById("box"+i).checked = true;
		}
	}
}
function deselectAll(){
	for (i = 0; i <= 100; i++) {
		if (document.getElementById("box"+i)){
			document.getElementById("box"+i).checked = false;
		}
	}
}

//Creates a couple of Select All / Deselect All links utilising the functions above, and inserts them into the document.
function searchForm(){
	if (document.getElementById("areas")){
		var selector = document.createElement("p");
		selector.setAttribute("class","small_para");
		selector.setAttribute("className", "small_para")
		
		var selectLink = document.createElement("a");
		var select_text = document.createTextNode("Select All");
		selectLink.appendChild(select_text);
		selectLink.onmouseover = selectLink.style.cursor='pointer';
		selectLink.onclick = selectAll;
		
		var deselectLink = document.createElement("a");
		var deselect_text = document.createTextNode("Deselect All");
		deselectLink.appendChild(deselect_text);
		deselectLink.onmouseover = deselectLink.style.cursor='pointer';
		deselectLink.onclick = deselectAll;
		
		selector.appendChild(selectLink);
		selector.appendChild(document.createTextNode(" / "));
		selector.appendChild(deselectLink);
		
		var selectBox = document.getElementById("areas");
		insertAfter(selector,selectBox);
	}
}
addLoadEvent(searchForm);

//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 == "radio"||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;
	}
}

//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;
			}
		}
	}
	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];
		if (thisform.name == "searchform"){continue;}
		resetFields(thisform);
		thisform.onsubmit = function() {
			return validateForm(this);
		}
	}
}
addLoadEvent(prepareForms);

//Adjusts navigation highlight and page title on Lettings Search due to limitations of Vebra software
function nav_highlight(){
	var prop_link = document.getElementById("property_search");
	var special_link = document.getElementById("lettings_search");
	prop_link.setAttribute("class","");
	prop_link.setAttribute("className", "");
	
	special_link.setAttribute("class","current");
	special_link.setAttribute("className","current");
}

if(location.href == "http://www.vebra.com/home/solex/search.asp?firmid=17695&branchid=0&dbtype=2&norefine=1"){
	addLoadEvent(nav_highlight);
}

function NewHomes(){
	var nh_form = document.getElementById("nh_form");
	var nh_link = document.getElementById("nh_link");
	nh_link.onclick = function() {
		nh_form.submit();
		return false;
	}
}
addLoadEvent(NewHomes);