//PRELOADER FOR CSS HOVER ITEMS
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 loadMenuImages() {
	mm_preloadimages('images/menu-home-hover.png','images/menu-about-us-hover.png','images/menu-services-hover.png','images/menu-residential-hover.png','images/menu-commercial-hover.png','images/menu-clients-hover.png','images/menu-contact-us-hover.png');
}

//CONTACT FORM FIELD CHECKER
function checkForm(theform) {
	var returnval=true //by default, allow form submission
	var checkit = document.getElementById('required_fields').value;
	var requireds=theform.required_fields.value.split(";") //split using blank space as delimiter
	for (i=0; i<requireds.length; i++){
		var fieldnamez = requireds[i];
		theform[fieldnamez].style.border = ''
		if(fieldnamez.length > 1){
			if (theform[fieldnamez].type=="text" || theform[fieldnamez].type=="textarea" || theform[fieldnamez].type=="file"){
				if (theform[fieldnamez].value==""){ //if empty field
					alert("Please make sure all required fields are filled out.") //alert error message
					theform[fieldnamez].style.border = '1px solid #7A030C'
					theform[fieldnamez].focus();
					returnval=false //disallow form submission
					break //end loop. No need to continue.
				}
			}
			if (theform[fieldnamez].type=="select-one"){
				if (theform[fieldnamez].value==""){
					alert("Please make sure all required fields are filled out.") //alert error message
					theform[fieldnamez].style.border = '1px solid #7A030C'
					theform[fieldnamez].focus();
					returnval=false //disallow form submission
					break //end loop. No need to continue.
				}
			}
			if(theform[fieldnamez].name=="email"){
				var str = theform.email.value
				var at="@"
				var dot="."
				var comma=","
				var lat=str.indexOf(at)
				var lstr=str.length
				var ldot=str.indexOf(dot)
				if (str.indexOf(at)==-1){
					alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #7A030C'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
				if (str.indexOf(comma)>0){
					alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #7A030C'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
				if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #7A030C'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #7A030C'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(at,(lat+1))!=-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #7A030C'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #7A030C'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(dot,(lat+2))==-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #7A030C'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(" ")!=-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #7A030C'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
			}
		}
		theform[fieldnamez].style.border = ''
	}
	return returnval; 
}

//SLIDESHOW FUNCTIONS
var time;
function changeSelectClass(index) {
	clearTimeout(time);
	var clear=document.getElementById('toggler').getElementsByTagName('a');
	for(i=0;i<clear.length;i++){
		clear[i].setAttribute("className","select"); //FOR IE
		clear[i].setAttribute("class","select");
	}
	clear[index].setAttribute("className","selected"); //FOR IE
	clear[index].setAttribute("class","selected");
}
function switchImg(name) {
	var image=name;
	document.getElementById('home-slideshow-inner').getElementsByTagName('img')[0].setAttribute("src",image);
}
function slideSelect(index,name) {
	changeSelectClass(index);
	switchImg(name);
}
function autoSlide(index,last,name) {
	changeSelectClass(index);
	switchImg(name);
	if(index==last){
		index=0;
	}
	else index++;
	var att=document.getElementById('toggler').getElementsByTagName('a')[index].getAttribute("onclick");
	var next=att.split("'");
	var callFunction="autoSlide("+index+","+last+",'"+next[1]+"')";
	time=setTimeout(callFunction, 5000);
}
function init() {
	loadMenuImages();
}
window.onload=init;

