// JavaScript Document

// Show Hide Store Locator
/*sHover = function() {
	if (document.all&&document.getElementById) {
		navRt = document.getElementById("storeLocator");
		for (i=0; i<navRt.childNodes.length; i++) {
			node = navRt.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}*/

sfClick = function() {	
	var sfEl = document.getElementById("storeLocator").getElementsByTagName("li");
	for (var i=0; i<sfEl.length; i++) {
			sfEl[i].getElementsByTagName("A")[0].stateCaller = sfEl[i];
	
			// get the element inner html
			var tmpStr = new String(sfEl[i].innerHTML);
				
			tmpStr = tmpStr.toLowerCase();
			// check that a list exists
	
			if (tmpStr.indexOf("<ul") != -1){
				// there is a ul so change the tag to expand the menu
				sfEl[i].getElementsByTagName("A")[0].href = "javascript:;";
			}
	
			sfEl[i].getElementsByTagName("A")[0].onmousedown = function(){
				// get the element
				states = this.stateCaller;
				
				if(states.className != "over"){
					states.className="over";
				} else {
					states.className="under";
					var killsOpen = states.getElementsByTagName("li");
					for(var z = 0; z < killsOpen.length; z++){
						killsOpen[z].className = "under";
					}
				}
			}
		}
}

/*closeBtn = function() {
	document.getElementById("close").onmousedown = function(){
		document.getElementById("innerUl").style.display = "none";
	}
	
}*/

window.onload=function(){
	sfClick();
}
function storelocatorLoad()
{
	sfClick();
}

function submitStoreLocator()
{
	var frm = document.getElementById("hidden_storelocator");
	document.getElementById("hiddenzip").value = document.getElementById("visiblezip").value;
	document.getElementById("hiddendistance").value = document.getElementById("visibledistance").value;
	frm.submit();
}
function submitcontactus()
{
	if(validateContactUs())
	{
		var frm = document.getElementById("hidden_contactus");
		transfervalue("vName","hName");
		transfervalue("vEmail","hEmail");
		transfervalue("vAddress","hAddress");
		transfervalue("vCity","hCity");
		transfervalue("vState","hState");
		transfervalue("vZip","hZip");
		transfervalue("vPhone","hPhone");
		transfervalue("vComments","hComments");
	//	frm.action = "/pages/contactus2.aspx";
	//	frm.method = "post";
		frm.submit();
	}
}
function transfervalue(from, to)
{
	document.getElementById(to).value = document.getElementById(from).value;
}

function validateContactUs()
{
	var str = " is a required field.\n"
	var msg = ""
	if(!validate_required("vName")){msg = msg + "Name" + str;}
	if(!validate_required("vEmail")){msg = msg + "Email" + str;}
	if(!validateEmail("vEmail")){msg = msg + "Please enter a valid email address.\n";}
	if(!validate_required("vPhone")){msg = msg + "Phone Number" + str;}	
	if(!validate_phone("vPhone")){msg = msg + "Please enter phone number in the following format: (555) 555-1234.\n";}
	var zipStr = validateZIP("vZip");
	if(zipStr.length > 0){msg = msg + zipStr;}
	if(!validate_required("vComments")){msg = msg + "Please enter your inquiry.";}
	
	if(msg.length > 0)
	{
		alert(msg);
		return false;
	}
	return true;
}

function validate_required(obj)
{
	var field = document.getElementById(obj);
	with (field)
	{
		if (value==null||value=="")
		  {return false}
		else {return true}
	}
}
function validate_phone(obj) 
{
 	var s = document.getElementById(obj).value;
 	if(s.length > 0)
 	{
	     // Check for correct phone number
	     rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
	 
	     if (!rePhoneNumber.test(s)) {
	          //alert("Phone Number Must Be Entered As: (555) 555-1234");
	          return false;
	     }
	} 
	return true;
} 
function validateZIP(obj) {

	var valid = "0123456789";
	var field = document.getElementById(obj).value;
	if (field.length!=5) {
		return "Zip code must be 5 digits.\n";
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			return "Invalid characters in your zip code.\n";
		}
	}
	return "";
}
function validateEmail(obj) {	
	var field = document.getElementById(obj).value;
	if(field.length > 0)
	{
		return (field.indexOf(".") > 2) && (field.indexOf("@") > 0); 
	}
	return true;
}

