/*************************************************************
     TEMPLATE BASED WEBSITE DESIGNED AND DEVELOPED BY
           VEBRA GRAPHICS (VEBRA SOLUTIONS LTD.)
                      COPYRIGHT 2005

      AUTHOR: DAVID SWALLOW (david.swallow@vebra.com)
                        22/11/2005

                      www.vebra.info
/*************************************************************/ 

//*************************
//     RE-USABLE FUNCTIONS
//*************************

// Checkbox checked
function codename() {
if(document.contactus.checkboxname.checked)
{
document.contactus.Submit.disabled=false;
}
else
{
document.contactus.Submit.disabled=true;
}
}

//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 the current date, which can be added to the onload event handler
function get_date()
{
	var months=new Array(13);
		months[1]="January";
		months[2]="February";
		months[3]="March";
		months[4]="April";
		months[5]="May";
		months[6]="June";
		months[7]="July";
		months[8]="August";
		months[9]="September";
		months[10]="October";
		months[11]="November";
		months[12]="December";
				
		var time=new Date();
		var lmonth=months[time.getMonth() + 1];
		var date=time.getDate();
		var year=time.getFullYear();

		var current_date = lmonth + " " + date + ", " + year;
		return current_date;
}

//Adds the current date to each page
function currentDate() {
	if (document.getElementById("date")) {
		dateElement = document.getElementById("date").firstChild;
		dateElement.data = get_date();
	} else {
		return false;
	}
}
addLoadEvent(currentDate);

//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);



//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];
		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(){
	if (document.getElementById){
		var prop_link = document.getElementById("property_search");
		var lets_link = document.getElementById("lettings_search");
		var page_title = document.getElementById("page_title").firstChild;
		prop_link.setAttribute("class","");
		prop_link.setAttribute("className", "")
		lets_link.setAttribute("class","current");
		lets_link.setAttribute("className","current");
		page_title.data = "Lettings Search";
	}
}
if(location.href == "http://www.vebra.com/home/solex/search.asp?firmid=11637&branchid=0&dbtype=1&norefine=1"){
	addLoadEvent(nav_highlight);
}