﻿
//Used for sending a zipcode to the search paeg
function submitToSearchWithZipCode(appPath,queryString)
{
	openPopup(appPath+'popups/enterZipCode.aspx?'+queryString, 215, 100);
}
function submitFormpopupZipCode()
{
if (Validate(document.getElementById('popupZipCode'))) { document.getElementById('popupZipCode').submit(); }
}

//calls the validate form function to validate the popup form.
function validatePopup()
{
	var ok = Validate(document.getElementById("popupForm"));
	
	return ok;
}

//open the popup
//give it the url to get through ajax and the size
function openPopup(url,width,height)
{
	
	
	closePopup();
	sizePopup(width,height);
	
	makeRequest(url,"popup");
	
	showPopup();
}
//"close" the popup (hide and hardcode the contents to a blank popup so they don't show up the next time it's shown)
function closePopup()
{
	hidePopup();
	hardcodeContents(document.getElementById('popupTemplate').innerHTML,"popup");
}
//show the popup
function showPopup()
{   
	WindowBounds=GetWindowBounds() 
	document.getElementById('gabrielsPopupBg').style.height = ""+  WindowBounds.PageHeight + "px";
	
	document.getElementById('popup').style.display='block';
	//ie 6 (and below?) doesn't render select boxes correctly so we have to hide them if they are under popups,
	//but let's not bother browsers that work correctly:
	if (isIeLessThan7())
		HideSelects();
	
}
//hide the popup
function hidePopup()
{
	//ie 6 (and below?) doesn't render select boxes correctly so we had to hide them if they are under popups,
	//but let's not bother browsers that work correctly:
	if (isIeLessThan7())
		ShowSelects();
		
	document.getElementById('gabrielsPopupBg').style.height = "0px";
	
	document.getElementById('popup').style.display='none';
		
}
//size the popup
function sizePopup(width,height)
{
	document.getElementById("popup").style.width=width+"px";
	document.getElementById("popup").style.height="auto";//height+"px";
	
	if (window.innerWidth==undefined)
	{
		if (document.documentElement && document.documentElement.clientWidth)
			ww= document.documentElement.clientWidth;
		else if (document.body)
			ww= document.body.clientWidth;
			
		if (document.documentElement && document.documentElement.clientHeight)
			wh= document.documentElement.clientHeight;
		else if (document.body)
			wh= document.body.clientHeight;
			
	}
	else
	{
		ww=window.innerWidth;
		wh=window.innerHeight;
	}
	
	x = Math.round((ww / 2) - (width / 2));
	y = Math.round((wh / 2) - (height / 2));
	
	y+=document.documentElement.scrollTop;
	
	//y+=document.body.scrollTop;
	
	document.getElementById("popup").style.left=x+"px";
	document.getElementById("popup").style.top=y+"px";
	
}
//submit a form on a popup to another popup
//give it the target url of the popup to submit to
function submitPopup(target)
{
	submitToDivPopup(target, "popup");
}

//submit a form on a popup to another popup of a different size
//give it the target url of the popup to submit to and size
function sizeAndSubmitPopup(target, width, height)
{
	hidePopup();
	document.getElementById('popupContents').style.display='none';
	submitPopup(target);
	sizePopup(width,height);
	showPopup();
}

//submit a form on a popup to a div to be populated through AJAX, then close the popup
//give it the target url of the div to get from AJAX and the id of that div
function submitToDivAndClosePopup(target, div_id)
{
	submitToDivPopup(target, div_id);
	closePopup();
}

//submit a form on a popup to a div to be populated through AJAX
//give it the target url of the div to get from AJAX and the id of that div
function submitToDivPopup(target, div_id)
{
	submitFormToDiv("popupForm", target, div_id)
}

//submit a form on a page to a div to be populated through AJAX, then close the popup
//give it the form id, the target url of the div to get from AJAX and the id of that div
function submitFormToDivAndClosePopup(form_id, target, div_id)
{
	submitFormToDiv(form_id, target, div_id)
	closePopup();
}

//submit a form on a page to a div to be populated through AJAX
//give it the form id, the target url of the div to get from AJAX and the id of that div
function submitFormToDiv(form_id, target, div_id)
{
	var form = document.getElementById(form_id);
	var query = buildQueryString(form);
	makePOSTRequest(target, div_id, query);
}

//submit a form on a page to a popup
//give it the form id, the url of the popup and size
function submitFormOpenPopup(form_id,url,width,height)
{
	closePopup();
	
	sizePopup(width,height);
	
	submitFormToDiv(form_id, url, "popup")
	
	showPopup();
}
//submit a form on a popup to a page
//give it the url of the page to submit to
function submitPopupToPage(url)
{
	document.getElementById("popupForm").action=url;
	document.getElementById("popupForm").method="post";
	document.getElementById("popupForm").submit();
}

//builds query string from form items
//give it the form id, returns a query string
//used for form submission
function buildQueryString(form) {
    var str = "";
    var element, i = 0;
    while ((element = form.elements[i++]) != null) {
        var qc = toQueryComponent(element);
        if (qc != "") str += "&" + qc;
    }
    return str.substring(1);
}
//builds query component from a form input
//give it the form element, returns a query string component
//used for form submission
function toQueryComponent(input) {
    if (!input.name || input.disabled)
        return "";

    var n = urlencode(input.name);

    switch (input.type) {
    case "text":
    case "password":
    case "submit":
    case "hidden":
        return n + "=" + urlencode(input.value);
    case "textarea":
        // normalize line breaks as CR LF pairs as per RFC 1866
        var v = input.value.split(/\r\n|\r|\n/).join("\r\n");
        return n + "=" + urlencode(v);
    case "checkbox":
    case "radio":
        if (!input.checked)
            return "";
        var v = getRealValue(input);
        if (v === null) v = "on";
        return n + "=" + urlencode(v);
    case "select-one":
    case "select-multiple":
        var nvp = [];
        var opt, i = 0;
        while ((opt = input.options[i++]) != null) {
            if (opt.selected) {
                var v = getRealValue(opt);
                if (v === null) v = opt.text;
                // older versions of IE do not support Array.push
                nvp[nvp.length] = n + "=" + urlencode(v);
            }
        }
        return nvp.join("&");
    default:
        // input types reset, button, image, and file not implemented
        return "";
    }
}

//javascript url encode
function urlencode(str) {
    var v;
    try { v = encodeURIComponent(str); } catch (e) { v = escape(str); }
    return v.replace(/%20/g,"+");
}

//for the toQueryComponent function
function getRealValue(input) {
    var attr = input.getAttributeNode("value");
    return (attr && attr.specified) ? input.getAttribute("value") : null;
}

//========================================================
//   HideSelects()
//   Hides all selects
//========================================================
function HideSelects()
{
    
    var SelectCollection = document.getElementsByTagName("select");

	if (SelectCollection.length == 0) { return; }
	
    for (var s=0;s<SelectCollection.length;s++)
    {
        var SelectObject = SelectCollection[s];
		SelectObject.style.visibility = "hidden";    
    }
}
//========================================================
//   ShowSelects()
//   restores visibility to all select objects in the DOM
//========================================================
function ShowSelects()
{
	var SelectCollection = document.getElementsByTagName("select");

	if (SelectCollection.length == 0) { return; }

	for (var s=0;s<SelectCollection.length;s++)
	{   
	    SelectCollection[s].style.visibility="visible";
	}

}

