Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
// USAGE:
// arrayName.inArray('searchValue')
{
    var i;
    for (i=0; i < this.length; i++) {
        // Matches identical (===), not just similar (==).
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};


function validate(){
	var valid = "Y";
	var f = document.getElementById("printform")
	var ff = document.getElementById("printform").elements
	var req = "";

	for(i=0;i<ff.length;i++){
  		if(ff[i].name == "required"){
			req = ff[i].value;
			var reqArray = req.split(",");
		}
		
		if (ff[i].name == "ZIP" || ff[i].name == "phone1" || ff[i].name == "Phone Number" || ff[i].name == "Mobile Phone" ){
			if (isNaN(ff[i].value.replace(/[\s.()-]/g, ""))){
			 	alert('please enter a \"number\" in the '+ff[i].name+' field. \nPlease click \"OK\" to correct this')
				ff[i].style.border = '1px solid red';
				ff[i].focus();
				ff[i].select();
				return false;
				break;				
			}
		}
		
 		if (ff[i].value == "" && reqArray.inArray(ff[i].name)){ 
		var thisAlt = "";
		if(ff[i].alt){thisAlt = "\n"+ff[i].alt+" ";}	
		alert('The '+ff[i].name+' field is empty. ' + thisAlt+ '\nPlease click \"OK\" to correct this') 
			//ff[i].style.borderColor = 'red';
			ff[i].style.border = '1px solid red';
			ff[i].focus();
			valid = 'N';
			return false;
			break;	
		} 	
	}
	if (valid == "Y"){
		f.action = "includes/mailer/mailer.php";
		return true;	
	}	
}

document.onclick = function(e){
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	switch(targ.id){
		case "submit":		
		if(document.getElementById("printform")){	
			return validate();
			break;
		}else{
			alert('no form')
			return true;				
		}
	}
}