var popUnderWindowReference = null;
/* Returns information about browser window as 2 element array:
    [0] = width in pixels
    [1] = height in pixels */
function getWindowDimensions() {
    var windowDimensions=new Array(630,460);
    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") {
            windowDimensions[0] = window.innerWidth;
            windowDimensions[1] = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) {
            windowDimensions[0] = document.body.offsetWidth;
            windowDimensions[1] = document.body.offsetHeight;
        }
    }
    return windowDimensions;
}

/* Returns information about browser window as 2 element array:
    [0] = number of pixels from left of screen
    [1] = number of pixels from top of screen */
function getWindowLocation() {
    var windowOffsets=new Array(0,0);
    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") {
            windowOffsets[0] = window.screenX;
            windowOffsets[1] = window.screenY;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) {
            windowOffsets[0] = window.screenLeft;
            windowOffsets[1] = window.screenTop;
        }
    }
    return windowOffsets;
}

function freeShippingPopups() {
    revealElement("FreeShippingDescription", 300, 440);
    popUnder();
    window.focus();
}

function freeShippingPopups1() {
    revealElement("FreeShippingDescription", 300, 440);
    popUnder1();
    window.focus();
}

function freeShippingPopupsGeneric( popunderURL ) {
    revealElement("FreeShippingDescription", 300, 440);
    popUnderGeneric(popunderURL);
    window.focus();
}

/* Looks for a hidden field by the name of FSAddPid2QS. If it finds the field then it will
add content which will result in a query string of the form FshDisplayPid=## to be appended to
the query string after the form is submitted. */
function SetFshHiddenFieldToPidFromUrl( url )
{
    /* If there is a free shipping hidden field for adding query string params update it with the pid */
    try
    {
        var hiddenAddToQsField = PRVD.CT.UT.GetElementsByPrvdName(document,"FSAddPid2QS");
        /* Make sure the hidden field exists */
        if ( hiddenAddToQsField != null && hiddenAddToQsField.length > 0 )
        {
            /* And we only want to add the field if we haven't already added it once.. */
            if ( hiddenAddToQsField[0].value == null || -1 == hiddenAddToQsField[0].value.indexOf("FshDisplayPid") )
            {
                matches = /\?pid(\d+)=/.exec(url);
            
                if ( matches.length == 2 )
                {
                    PRVD.CT.UT.AddQueryStringParamToHiddenField( hiddenAddToQsField[0], "FshDisplayPid", matches[1]);
                }
            }
        }
    }
    catch ( ex ) { }
}

/* PopUnder a window with the name 'FreeShipping', if there is a hidden field
which can be used to add the url's freeshipping pid to the query string then 
this function will also update that hidden field*/
function PopUnderFshWindow( url )
{
    PopUnderFshWindowAndRevealElement( url, true);
}

function PopUnderFshWindowAndRevealElement( url, doRevealElement )
{
    if ( doRevealElement )
    {
        revealElement("FreeShippingDescription", 300, 440);
    }

    SetFshHiddenFieldToPidFromUrl( url );
    var dimensions = getWindowDimensions();
    var windowLocation = getWindowLocation();
    if (popUnderWindowReference == null || popUnderWindowReference.closed) 
    {
        popUnderWindowReference = window.open(url,'FreeShipping','status=1,toolbar=1,location=1,menubar=1,resizable=1,scrollbars=1,width='+dimensions[0]+',height='+dimensions[1]+',left='+windowLocation[0]+',top='+windowLocation[1]);
    }
    window.focus();
}


/* PopUp a window with the name 'FreeShipping' */
function PopUpFshWindow( url )
{
    var dimensions = getWindowDimensions();
    var windowLocation = getWindowLocation();
    win = window.open(url,'FreeShipping','status=1,toolbar=1,location=1,menubar=1,resizable=1,scrollbars=1,width='+dimensions[0]+',height='+dimensions[1]+',left='+windowLocation[0]+',top='+windowLocation[1]);
    win.focus();
}

/* Spawns new up window and sets it's Z order equal to the existing browser window - 1. */
function popUnder() {
    var dimensions = getWindowDimensions();
    var windowLocation = getWindowLocation();
    /* This window is intended to open underneath the current browser window, so
       prevent the user from accidentally spawning several new browser windows */
    if (popUnderWindowReference == null || popUnderWindowReference.closed) {
        popUnderWindowReference = window.open('https://www.freeshipping.com/join.asp?pid4=342','FreeShipping','status=1,toolbar=1,location=1,menubar=1,resizable=1,scrollbars=1,width='+dimensions[0]+',height='+dimensions[1]+',left='+windowLocation[0]+',top='+windowLocation[1]);
    }
}

/* Spawns new up window and sets it's Z order equal to the existing browser window - 1. */
function popUnder1() {
    var dimensions = getWindowDimensions();
    var windowLocation = getWindowLocation();
    /* This window is intended to open underneath the current browser window, so
       prevent the user from accidentally spawning several new browser windows */
    if (popUnderWindowReference == null || popUnderWindowReference.closed) {
        popUnderWindowReference = window.open('https://www.freeshipping.com/join_pf006.asp?pid6=342','FreeShipping','status=1,toolbar=1,location=1,menubar=1,resizable=1,scrollbars=1,width='+dimensions[0]+',height='+dimensions[1]+',left='+windowLocation[0]+',top='+windowLocation[1]);
    }
}
/* Generic popunder method. */
function popUnderGeneric( url ) {
    var dimensions = getWindowDimensions();
    var windowLocation = getWindowLocation();
    /* This window is intended to open underneath the current browser window, so
       prevent the user from accidentally spawning several new browser windows */
    if (popUnderWindowReference == null || popUnderWindowReference.closed) {
        popUnderWindowReference = window.open(url,'FreeShipping','status=1,toolbar=1,location=1,menubar=1,resizable=1,scrollbars=1,width='+dimensions[0]+',height='+dimensions[1]+',left='+windowLocation[0]+',top='+windowLocation[1]);
    }
}

/* Spawns new up window and sets it's Z order equal to the existing browser window - 1. */
function popUnderSB() {
    var dimensions = getWindowDimensions();
    var windowLocation = getWindowLocation();
    /* This window is intended to open underneath the current browser window, so
       prevent the user from accidentally spawning several new browser windows */
    if (popUnderWindowReference == null || popUnderWindowReference.closed) {
        popUnderWindowReference = window.open('http://www.smilebox.com/partner/proflowers/index.html','FreeShipping','status=1,toolbar=1,location=1,menubar=1,resizable=1,scrollbars=1,width='+dimensions[0]+',height='+dimensions[1]+',left='+windowLocation[0]+',top='+windowLocation[1]);
    }
}

/* Generic method to reveal an element that had previously
   been hidden by the CSS style "display:hidden".
   elementID == string of an id in the DOM
   popWidth sets the width of the revealed element
   popHeight sets the height of the revealed element
   Assert: elementID exists in the DOM */
function revealElement(elementID, popWidth, popHeight) {
    var windowDimensions = getWindowDimensions();    
    try {
        var element = document.getElementById(elementID);
        element.style.display = "block";
        element.style.left = windowDimensions[0]/2 - popWidth/2;
        element.style.top = windowDimensions[1]/2 - popHeight/2;
        element.style.width = popWidth;
        element.style.height = popHeight;
    } catch (ex) {
        //elementID was not found in the DOM;
    }
}

/* Hides a named element in the DOM
    Assert: element has id attribute and value. */
function hideElement(element) {
    try {
        document.getElementById(element.id).style.display="none";
    } catch (ex) {    
        //element does not have an id.
    }
}

/* The next four functions are to work around a known
    bug in IE 6 where select elements always appear at
    the highest z-order, regardless of CSS styling */

/* hides the select elements on a page */
function hideSelects(){

    if ( (navigator.appName.indexOf("Microsoft")!=-1) && (parseInt(navigator.appVersion)>3) && (parseInt(navigator.appVersion)<7) )
    {    
	    var oSelects=document.getElementsByTagName("select");
	    for(var i=0;i<oSelects.length;i++)
	    {
		    oSelects[i].style.display="none";
	    }
	}
}

/* restores the select elements on a page */
function showSelects(){
	if ( (navigator.appName.indexOf("Microsoft")!=-1) && (parseInt(navigator.appVersion)>3) && (parseInt(navigator.appVersion)<7) )
    {    
	    var oSelects=document.getElementsByTagName("select");
	    for(var i=0;i<oSelects.length;i++)
	    {
		    oSelects[i].style.display ="inline";
    	}
    }
}

/* Same reveal method from above, but with a
    parameter to support hiding any select
    elements on a page */
function revealElement(elementID, popWidth, popHeight, hideSelectElements) {
    var windowDimensions = getWindowDimensions();    
    try {
        var element = document.getElementById(elementID);
        element.style.display = "block";
        element.style.left = windowDimensions[0]/2 - popWidth/2;
        element.style.top = windowDimensions[1]/2 - popHeight/2;
        element.style.width = popWidth;
        element.style.height = popHeight;
        
        if( hideSelectElements )
        {
            hideSelects();
        }
    } catch (ex) {
        //elementID was not found in the DOM;
    }
}

/* Same hide method from above, but with a
    parameter to support restoring previously
    hidden select elements */
function hideElement(element, restoreSelects) {
    try {
        document.getElementById(element.id).style.display="none";
        if( restoreSelects )
        {
            showSelects();
        }
    } catch (ex) {    
        //element does not have an id.
    }
}
/*  Searches within an element for a direct child who has an id which matches or contains
    the id provided to this method */
function RetHdnFld( div, id ) 
{ 
    for ( var i = 0; i < div.childNodes.length; i++ ) 
    { 
        if ( div.childNodes[i].id != null && div.childNodes[i].id.indexOf( id ) != -1 ) 
        {
            return div.childNodes[i]; 
        } 
    }
}

/*  Refreshes/Reopens the FSH popunder with the value of the link with id HiddenDynamicFshLink
    which is contained within the div HiddenDynamicFshLink */
function RefreshFshPopunder( id )
{
    try 
    {
        var hfDiv = PRVD.CT.UT.GetElementsByPrvdName(document, id)[0];
        var link = RetHdnFld( hfDiv, id );
        var url = link.getAttribute("href");
        PopUnderFshWindowAndRevealElement(url,false);        
    }
    catch ( ex ) { }
}
