﻿
// Global Variable Declarations
var isLoaded = false;

// Global Functions

// Adds appropriate targets to anchor tags.
function processAnchorTargets()
{
    if (!document.getElementsByTagName) return;

    var anchors = document.getElementsByTagName("a");
    
    for (var i=0; i < anchors.length; i++)
    {
        var anchor = anchors[i];
        
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "popup")
        {
            anchor.target = "_blank";
        }
    }
}

// Submits form[0]
function submitForm()
{
    document.forms[0].submit();
    return true;
}

// Submit Target Form
function submitTargetForm(in_formIndex)
{
    document.forms[in_formIndex].submit();
    return true;
}


// Image Swaps
function imageSwap(in_Name, in_State)
{
    if (!isLoaded)
    {
        return true;
    }
    document.images[in_Name].src = eval(in_Name + "_" + in_State + ".src");
}

// Opens a popup with a description of the Card Code.
function openCardCodeDescription(in_appRoot)
{
	popWindow(in_appRoot + "Web/NetCommerce/CardCode.htm", "scrollbars=no,width=575,height=350");
	return false;
}

// Window Openers
function popWindow(in_targetUrl, in_windowParameters)
{
    //windowParameters are very browser dependent.  Be sure to test in all browswers when
    //changing window parameters.

    //For instance for Both IE and Netscape browsers to absolutely position a window properly 2 sets
    // of dimensions should be included example: screenX=0,screenY=0,left=0,top=0
    //To set the height and width of the windows example: width=625,height=400
    //A Good PopupWindow in all browsers with no toolbar, no scrollbar, and not resizable.
    //windowParameters = "scrollbars=no,width=625,height=400";
    
    if ( in_windowParameters.length == 0 )
    {
        in_windowParameters = "resizable,dependent,toolbar,scrollbars,location,status,menubar";
    }
    
    popupWin = window.open(in_targetUrl,'PopupWindow',in_windowParameters);
    
    return false;
}

function popWindowWithTarget(in_targetUrl, in_targetWindowName, in_windowParameters)
{
    if ( in_windowParameters == null )
    {
        in_windowParameters = "";
    }
    
    popupWin = window.open(in_targetUrl,in_targetWindowName,in_windowParameters);
    
    return false;
}

function setBinaryDataUploaderKeyValue(binaryDataKeyControlClientId,binaryDataKeyControlValue,binaryDataOnClickActionControlClientId)
{
	document.getElementById(binaryDataKeyControlClientId).value = binaryDataKeyControlValue;
	document.getElementById(binaryDataOnClickActionControlClientId).click();
	return false;
}


function updateParentBinaryDataUploaderAndClose(binaryDataKeyControlClientId,binaryDataKeyControlValue,binaryDataOnClickActionControlClientId)
{
	// note forms[0].elements does not work for input type="image" for some scary reason.
	window.opener.document.getElementById(binaryDataKeyControlClientId).value = binaryDataKeyControlValue;
	window.opener.document.getElementById(binaryDataOnClickActionControlClientId).click();
	closeSelfWindow();
	return false;
}

//function openChildWindow(targetUrl,windowName,windowParameters)
//{

//    childWindow = window.open(targetUrl,windowName,windowParameters);

//    if (childWindow.opener == null)
//    {
//    	childWindow.opener = self;
//    }
//    
//    return false;
//}

function closeSelfWindow()
{
	window.self.close();
    return false;
}

function refreshParentPageAndClose()
{
	window.opener.location.reload();
	closeSelfWindow();
	return false;
}

function refreshParentPageAndClose(refreshUrl)
{
	if (refreshUrl != null) {
		window.opener.location.href = refreshUrl;
	}
	else {
		window.opener.location.reload();
	}
	closeSelfWindow();
	return false;
}

// Edit Mode helper functions

/*
var editMenuHideTO;

function editMenuShow(menuId) {
	$get(menuId).style.display = "block";
}

function editMenuHide(menuId) {
	$get(menuId).style.display = "none";
}

function editMenuHideWithDelay(menuId) {
	editMenuHideTO = setTimeout("$get(\"" + menuId + "\").style.display = \"none\";", 50);
}

function editMenuHideCancel() {
	if (editMenuHideTO) {
		clearTimeout(editMenuHideTO);
	}
}
*/

// Global Initialization

function blurInputs()
{
    if (!document.getElementsByTagName) return;

    var inputs = document.getElementsByTagName("input");
    
    for (var i=0; i < inputs.length; i++)
    {
        var inp = inputs[i];
        inp.blur();
    }
    
}

function globalInit()
{
    processAnchorTargets();
    
    isLoaded = true;
} 
