/*************************************************************
     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
/*************************************************************/ 

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

//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("first_para")) {
		var date_element = document.createElement("p");
		var date_text = document.createTextNode(get_date());
		date_element.appendChild(date_text);
		var date_area = document.getElementById("first_para");
		date_area.parentNode.insertBefore(date_element,date_area);
	} else {
		return false;
	}
}
addLoadEvent(currentDate);*/


//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 MKMS Property Services Limited";
	
		if (theHour < 7)
		{greeting_text = "An EARLY Good Morning and Welcome to MKMS Property Services Limited";
		return greeting_text;
		}
		if (theHour > 6 && theHour <12)
		{greeting_text = "Good Morning and Welcome to MKMS Property Services Limited";
		return greeting_text;
		}
		if (theHour > 11 && theHour <18)
		{greeting_text = "good afternoon and welcome to MKMS Property Services Limited";
		return greeting_text;
		}
		if (theHour >17)
		{greeting_text = "Good Evening and Welcome to MKMS Property Services Limited";
		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());
		greeting.appendChild(greeting_text);
		var greeting_area = document.getElementById("first_para");
		greeting_area.parentNode.insertBefore(greeting,greeting_area);
	} else {
		return false;
	}
}
addLoadEvent(homepageGreeting);

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