// JavaScript Document
String.prototype.trim = function ()
{
    return this.replace("/^\s+|\s+$/g", '');
}
function CheckForm(form)
{
	if (form.search_zip.value != "")
	{
		var validZipCode = (/\d{5}(-\d{4})?/);
		var gotIt = validZipCode.test(form.search_zip.value); 
		
		if(!gotIt)
		{
			alert('\"' + form.search_zip.value + "\" is not a valid zip code");                  
			form.search_zip.focus();
			return false;
		}
	}
	else
	{
		if (form.search_city.value == "")
		{
			alert("Please enter a city or zip code!");
			form.search_zip.focus();
			return false;
		}
		if(form.search_state.value== "")
		{
			alert("Please select a state!");
			form.search_state.focus();
			return false;
		}
	}
	return true;
}
function CheckQuoteForm(form)
{
	if(form.full_name.value.trim() == "")
	{
		alert("Full Name Required");
		form.full_name.value="";
		form.full_name.focus();
		return false;
	}
	if(form.contact_phone.value.trim() == "")
	{
		alert("Contact Phone Required");
		form.contact_phone.value="";
		form.contact_phone.focus();
		return false;
	}
	else
	{
		var validPhone = "(/(^\(?\d{3}\)?\s|-\d{3}-\d{4}$)|(^\(?\d{3}\d{3}\d{4}$)/)";
		var gotIt = validPhone.test(form.contact_phone.value); 
		
		if(!gotIt)
		{
			alert("Please enter contact phone as \"###-###-####\"");                  
			form.contact_phone.focus();
			return false;
		}
	}
	if(form.contact_email.value.trim() == "")
	{
		alert("Contact Email Required");
		form.contact_email.value="";
		form.contact_email.focus();
		return false;
	}
	else	
	{
		var apos = form.contact_email.value.indexOf("@"); 
		var dotpos = form.contact_email.value.lastIndexOf(".");
		var lastpos = form.contact_email.value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>4 || lastpos-dotpos<2)
		{
			alert('\"' + form.contact_email.value + "\" is not a valid email");
			form.contact_email.focus();
			return false;
		}
	}
	if(form.date_month.value.trim() == "")
	{
		alert("Move In Month Required");
		form.date_month.value="";
		form.date_month.focus();
		return false;
	}
	if(form.date_date.value.trim() == "")
	{
		alert("Move In Date Required");
		form.date_date.value="";
		form.date_date.focus();
		return false;
	}
	if(form.date_year.value.trim() == "")
	{
		alert("Move In Year Required");
		form.date_year.value="";
		form.date_year.focus();
		return false;
	}
	
	//parse out the data
	var moveinDate = new Date();
	var date = Date.parse(form.date_month.value + "/" + form.date_date.value + "/" +form. date_year.value);
	moveinDate.setTime(date);
	if(moveinDate == "NaN" || moveinDate == "Invalid Date")
	{
		alert("Invalid Move In Date. Please Use Calendar to Select Move In Date.");
		form.date_month.value="";
		form.date_date.value="";
		form.date_year.value="";
		form.date_month.focus();
		return false;
	}	
	var now = new Date();	
	now.setTime(Date.parse((now.getMonth() + 1) + "/" + now.getDate() + "/" + now.getFullYear()));
	if(moveinDate < now)
	{
		alert("Move In Date must be Today or Greater");
		form.date_month.value="";
		form.date_date.value="";
		form.date_year.value="";
		form.date_month.focus();
		return false;
	}
	form.submit_button.disabled=true;
	form.submit_button.style.backgroundColor="#999999";
	//alert(form.submit_button.disabled);
	return true;
}
function showhide(id)
{
	if (document.getElementById)
	{
		obj = document.getElementById(id);
		if (obj.style.display == "none")
		{
			obj.style.display = "";
		} 
		else
		{
			obj.style.display = "none";

		}
	}
}
function hideinfo()
{
	if (!document.getElementsByTagName && !document.createElement && !document.createTextNode) return;
	var nodes = document.getElementsByTagName("span");
	for(var i=nodes.length-1;i>=0;i--) 
	{
		if (nodes[i].className =="render_info") 
		{
			var at = / at /;
			var dot = / dot /g;
			var node = document.createElement("a");
			var address = nodes[i].firstChild.nodeValue;

			address = address.replace(at, "@");
			address = address.replace(dot, ".");

			address = address.replace(at, "@");
			address = address.replace(dot, ".");
			node.setAttribute("href", "mailto:"+address);
			node.appendChild(document.createTextNode(address));

			node.setAttribute("class","render_info");
			node.setAttribute("className","render_info");

			var prnt = nodes[i].parentNode;
			for(var j=0;j<prnt.childNodes.length;j++)
				if (prnt.childNodes[j] == nodes[i]) 
				{
					if (!prnt.replaceChild) return;
					prnt.replaceChild(node, prnt.childNodes[j]);
					break;
				}
		}
	}
}
function addDays(myDate, days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}
function OpenCouponWindow(coupon_link)
{
	leftVal = (500 - screen.width) / 2 * -1;
	topVal = (500 - screen.height) / 2 * -1;
	window.open(coupon_link, 'couponwindow','height=500, width=500,left='+leftVal+',top='+topVal + ',toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
}
function OpenQuoteWindow(quote_link)
{
	leftVal = (625 - screen.width) / 2 * -1;
	topVal = (625 - screen.height) / 2 * -1;
	window.open(quote_link, 'quotewindow','height=625, width=625,left='+leftVal+',top='+topVal + ',toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
}
function OpenMapWindow(map_link) {
    leftVal = (605 - screen.width) / 2 * -1;
    topVal = (535 - screen.height) / 2 * -1;
    window.open(map_link, 'mapwindow', 'height=535, width=605,left=' + leftVal + ',top=' + topVal + ',toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
}
function validateDate(fld) {

    var RegExPattern = "/^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/";
 //   var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 2-30-2000 would not be accepted.\nFormay mm/dd/yyyy.';
    if ((fld.value.match(RegExPattern)) && (fld.value!=''))
    {
        //alert('Date is OK'); 
        return true;
    }
    else
    {
        //alert(errorMessage);
        //fld.focus();
        return false;
    }
}
function bookmark_us(obj, url, title) {

    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) { // opera
        //var elem = document.createElement('a');
        obj.setAttribute('href', url);
        obj.setAttribute('title', title);
        obj.setAttribute('rel', 'sidebar');
        obj.click();
    }
    else if (document.all)// ie
        window.external.AddFavorite(url, title);
}

