﻿



//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(document.getElementById('popup'));
	
}
//hide the popup
function hidePopup()
{
	document.getElementById('gabrielsPopupBg').style.height = "0px";
	
	document.getElementById('popup').style.display='none';
	//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();
		
}
//size the popup
function sizePopup(width,height)
{
	document.getElementById("popup").style.width=width+"px";
	document.getElementById("popup").style.height=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));
	
	
	//Double-You Aitch A Tee
	if (document.documentElement.scrollTop>0)
		y+=document.documentElement.scrollTop;
	else
		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(obj)
//   Hides selects that are overlap 
//   within the boundaries of obj
//========================================================
function HideSelects(obj)
{
    var RO = new RecurseOffset(obj);
    var ObjectLeft = RO.offsetLeft;
    var ObjectTop = RO.offsetTop;
    var ObjectRight = ObjectLeft + RO.offsetWidth;
    var ObjectBottom = ObjectTop + RO.offsetHeight;
    
    var SelectCollection = document.getElementsByTagName("select");

	if (SelectCollection.length == 0) { return; }
	
    for (var s=0;s<SelectCollection.length;s++)
    {
        var SelectObject = SelectCollection[s];
        var SO = new RecurseOffset(SelectObject);
   	    var SelectLeft = SO.offsetLeft;
        var SelectTop = SO.offsetTop;
        var SelectRight = SelectLeft + SO.offsetWidth;
        var SelectBottom = SelectTop + SO.offsetHeight;

        //====================== detect collision -- with buffer... always seems to be off by some pixels
       
        if (
            (SelectRight > ObjectLeft-30) &&
            (SelectBottom > ObjectTop-5) &&
            (SelectLeft < ObjectRight+30) &&
            (SelectTop < ObjectBottom+45)
            )
            {
                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";
	}

}

//========================================================
//   RecurseOffset(obj)
//   returns true offsetLeft and offsetTop
//	 cross borwser, returns the true top and left positon
//   of an object including parent objects it may be nested in
//   also returns values for offsetWidth and offsetHeight
//   for ease of use
//========================================================
function RecurseOffset(obj)
{
	this.ParentObj = null;
	this.CurrentObj = obj;
    this.offsetLeft = obj.offsetLeft;
    this.offsetTop = obj.offsetTop;
	this.offsetWidth = obj.offsetWidth;
    this.offsetHeight = obj.offsetHeight;
	RecurseOffset.prototype.Init = function()
	{
	    if (this.CurrentObj.offsetParent != null)
	    {
	        do
		    {
				
                this.ParentObj = this.CurrentObj.offsetParent;
                this.offsetLeft += this.ParentObj.offsetLeft;
                this.offsetTop += this.ParentObj.offsetTop;
                this.CurrentObj = this.ParentObj;
				
		    }
		    while (this.CurrentObj.offsetParent != null);
	    }
	}
	this.Init();
}