// Event Type Constants
var APPROVE_CLICKED = "ApproveClicked";
var APPROVE_SUBMITTED = "ApproveSubmitted";
var DOCUMENT_BUTTONS_LOAD = "DocumentButtonsLoad";
var DOWNLOAD_CLICKED = "Download_Clicked";
var ADD_TO_CART = "AddToCart";
var DOWNLOAD_SUBMITTED = "DownloadSubmitted";
var REGION_BUTTON_CLICKED = "RegionButtonClicked";
var INPUT_SUBMITTED = "InputSubmitted";
var JOB_TICKET_OPENED = "JobTicketOpened";
var JOB_TICKET_SUBMITTED = "JobTicketSubmitted";
var NAVIGATE_PAGES = "NavigatePages";
var PREFERENCES_LOAD = "PreferencesLoad";
var PREFERENCES_SUBMITTED = "PreferencesSubmitted";
var PREVIEW_LOAD = "PreviewLoad";
var QUIT = "Quit";
var REGION_BUTTONS_LOAD = "RegionButtonsLoad";
var REJECT_CLICKED = "RejectClicked";
var REJECT_SUBMITTED = "RejectSubmitted";
var INIT = "Init";
var SUBMIT_FOR_APPROVAL = "SubmitForApproval";
var SUBMIT_FOR_APPROVAL_SUBMITTED = "SubmitForApprovalSubmitted";
var QUIT_SUBMITTED = "QuitSubmitted";

// Window Sizes
var DOWNLOAD_WINDOW_HEIGHT = 285;
var DOWNLOAD_WINDOW_WIDTH = 260;

var DELIVER_DOWNLOAD_WINDOW_HEIGHT = 285;
var DELIVER_DOWNLOAD_WINDOW_WIDTH = 260;

//////////////////////////////////////////////////////////////
// WF - Document Approval Reason
// Added 2 new window dimensions for document approval reason
var APPROVE_WINDOW_WITH_REASON_HEIGHT=375;
var APPROVE_WINDOW_WITH_REASON_WIDTH=550;
var APPROVE_WINDOW_HEIGHT = 250;
var APPROVE_WINDOW_WIDTH = 250;
//////////////////////////////////////////////////////////////


var REJECT_WINDOW_HEIGHT = 375;
var REJECT_WINDOW_WIDTH = 550;

var SUBMIT_WINDOW_HEIGHT = 250;
var SUBMIT_WINDOW_WIDTH = 250;

var INPUT_WINDOW_HEIGHT = 450;
var INPUT_WINDOW_WIDTH = 300;

var PREPARING_DOWNLOAD_WINDOW_HEIGHT = 175;
var PREPARING_DOWNLOAD_WINDOW_WIDTH = 275;

var JOB_TICKET_WINDOW_HEIGHT = 400;
var JOB_TICKET_WINDOW_WIDTH = 600;

var MAILING_LIST_WINDOW_HEIGHT = 550;
var MAILING_LIST_WINDOW_WIDTH = 772;

// Status Graphic States
var NORMAL = 1;
var EDITING  = 2;
var COMPLETED = 3;

var sessionID = "";
var keyEvent = "";
var keySessionID = "";

var REPORT_SCREEN_URL = "index.htm";

function SetEventKey(key)
{
    keyEvent = key;
}

function SetSessionIDKey(key)
{
    keySessionID = key;
}

function AppendEventType(URL, eventType)
{
    return (URL + "?" + keyEvent + "=" + eventType + "&rand=" + Math.floor(Math.random() * 1000000000));
}

function AppendSessionID(URL)
{
    return (URL + "?" + keySessionID + "=" + sessionID + "&rand=" + Math.floor(Math.random() * 1000000000));
}

function AppendEventTypeAndSessionID(URL, eventType)
{
    return (URL + "?" + keyEvent + "=" + eventType + "&" + keySessionID + "=" + sessionID + "&rand=" + Math.floor(Math.random() * 1000000000));
}

function SetSessionID(ID)
{
    sessionID = ID;
}

function Redirect(URL)
{
    window.location.href = URL;
}

// old call function OpenNewWindow(URL, windowName)
function OpenNewWindow()
{
    var argv = OpenNewWindow.arguments;

    var myURL = argv[0]+"&rand=" + Math.floor(Math.random() * 1000000000);

    var width = eval(argv[1] + "_WIDTH");
    var height = eval(argv[1] + "_HEIGHT");
    if (argv.length != 3)
    {
        return window.open(myURL, argv[1], "WIDTH=" + width + ",HEIGHT=" + height + ",scrollbars");
    }
    else
    {
        return window.open(myURL, argv[1], "WIDTH=" + width + ",HEIGHT=" + height + ",scrollbars," + argv[2]);
    }
}

function GetNewWindow(URL, windowName)
{
    var width = eval(windowName + "_WIDTH");
    var height = eval(windowName + "_HEIGHT");

    return window.open(URL, windowName, "WIDTH=" + width + ",HEIGHT=" + height + ",scrollbars");
}

function GetTopWindow()
{
    if (window.opener != null)
    {
        return (opener.GetTopWindow());
    }

    if (window.parent != null)
    {
        if (window.parent.agilisTopFrame == 1)
        {
           return (window.parent);
        }
        else
        {
            return (window.parent.GetTopWindow());
        }
    }
}

function RedrawPreviewImage()
{
    // just reload the whole frame
    var topWindow = GetTopWindow();
    if (!topWindow.redrawInProgress)
    {
        topWindow.redrawInProgress = true;
        var previewFrame = topWindow.frames.preview;

        previewFrame.location.reload();
    }
}

function RedrawDocumentButtons()
{
    // just reload the whole frame
    var topWindow = GetTopWindow();
    var documentButtonsFrame = topWindow.frames.documentbuttons;

    documentButtonsFrame.location.reload();
}

function drawServerBusy()
{
    var topWindow = GetTopWindow();

    var documentButtonsFrame = topWindow.frames.documentbuttons;
    var statusGraphic = eval("documentButtonsFrame.document.images['serverStatus']");
    if (statusGraphic != null)
    {
        statusGraphic.src = "images/anim.gif";
    }
}

function drawServerIdle()
{
    var topWindow = GetTopWindow();
    var documentButtonsFrame = topWindow.frames.documentbuttons;
    var statusGraphic = eval("documentButtonsFrame.document.images['serverStatus']");
    if (statusGraphic != null)
    {
        statusGraphic.src = "images/blank.gif";
    }
}

function RedrawRegionButtons()
{
    // just reload the whole frame
    var topWindow = GetTopWindow();
    var regionButtonsFrame = topWindow.frames.regionbuttons;

    regionButtonsFrame.location.reload();
}

function GetStatusGraphic(status)
{
    if (status == NORMAL)
        return "box.gif";

    if (status == EDITING)
        return "glow.gif";

    if (status == COMPLETED)
        return "check.gif";
}

function GetStatus(statusGraphic)
{
    // if statusGraphic contains the string "box.gif"...
    if (statusGraphic.indexOf("box.gif") != -1)
        return NORMAL;

    // if statusGraphic contains the string "glow.gif"...
    if (statusGraphic.indexOf("glow.gif") != -1)
        return EDITING;

    // if statusGraphic contains the string "check.gif"...
    if (statusGraphic.indexOf("check.gif") != -1)
        return COMPLETED;
}

function GetRegionButtonStatus(regionID)
{
    var topWindow = GetTopWindow();
    var regionButtonsFrame = topWindow.frames.regionbuttons;
    var statusGraphic = eval("regionButtonsFrame.document.images['status" + regionID + "']");

    return GetStatus(statusGraphic.src);
}

function SetRegionButtonStatus(regionID, status)
{
    var topWindow = GetTopWindow();

    var regionButtonsFrame = topWindow.frames.regionbuttons;
    var statusGraphic = eval("regionButtonsFrame.document.images['status" + regionID + "']");

    if (statusGraphic != null)
    {
        statusGraphic.src = "images/" + GetStatusGraphic(status);
    }
}

function SetStatusMessage(msg)
{
    var topWindow = GetTopWindow();
    topWindow.status = msg;
}

function AppendStatusMessage(msg)
{
    var topWindow = GetTopWindow();
    topWindow.status = topWindow.status + msg;
}

function ClearStatusMessage()
{
    SetStatusMessage("");
}

function SendFile(URL)
{
    var topWindow = GetTopWindow();
    var windowName = "DELIVER_DOWNLOAD_WINDOW";
    var width = eval(windowName + "_WIDTH");
    var height = eval(windowName + "_HEIGHT");

    if (isSP2())
    {
        var dlWindow = window.open(URL, windowName, "WIDTH=" + width + ",HEIGHT=" + height + ",scrollbars=no");
        dlWindow.focus();
    }
    else
    {
        topWindow.frames.actions.location.href = URL;
    }
}

function isSP2 ()
{
   var g_fIsSP2 = (window.navigator.userAgent.indexOf("SV1") != -1);

   return (g_fIsSP2);
}

function IsMacintosh()
{
    return navigator.userAgent.toLowerCase().indexOf("mac") != -1;
}

function IsIE()
{
    return navigator.userAgent.toLowerCase().indexOf("msie") != -1;
}

function AHREFTag(URL)
{
    return "<A href=\"#\" onClick=\"" + URL + "\">";
}

function ClickAHREFTag(URL)
{
    return "<A href=\"" + URL + "\">";
}

 function tcnc(inURL)
 {
       if (confirm("The template has not been completely filled in.  Continue anyway?"))
       {
               var topWindow = GetTopWindow();
               topWindow.location.href=inURL;
       }
}

var compilerWindow;

function openCompilerWindow(url)
{
    compilerWindow = window.open(url,'CompilerWindow','menubar=no,toolbar=no,resizable=yes,width=750,height=550,scrollbars=yes');
    return compilerWindow;
}


var compilerLayoutScaleFactor;
var compilercompilerLayoutHeight;
var compilercompilerLayoutWidth;
var compilerLayoutUrl;
var compilerUpdateUrl;

var resizeMinimumWidth = "";
var resizeMaximumWidth = "";
var resizeMinimumHeight = "";
var resizeMaximumHeight = "";
var originalWidth = "";
var originalHeight = "";

function setSize()
{
   compilerLayoutWidth = document.getElementById("compilerLayoutWidthText").value;
    compilerLayoutHeight = document.getElementById("compilerLayoutHeightText").value;
}

function setScaleFactor()
{
    requestedWidth = document.getElementById("compilerLayoutWidthText").value;
    requestedHeight = document.getElementById("compilerLayoutHeightText").value;

    if ((requestedHeight/originalHeight)*originalWidth >= requestedWidth)
    {
        compilerLayoutScaleFactor = requestedWidth/originalWidth;
    }
    else if ((requestedWidth/originalWidth)*originalHeight > requestedHeight)
    {
        compilerLayoutScaleFactor = requestedHeight/originalHeight;
    }
    else
    {
        compilerLayoutScaleFactor = 1;
    }
}

function setHeightFromWidth()
{
   if (document.getElementById("compilerLayoutWidthText").value != "" && !isNaN(document.getElementById("compilerLayoutWidthText").value))
    {
        requestedWidth = document.getElementById("compilerLayoutWidthText").value;
        var tempScale = requestedWidth/originalWidth;
        var tempHeight = originalHeight * tempScale;
        tempHeight = tempHeight.toFixed(2);
        document.getElementById("compilerLayoutHeightText").value = tempHeight;
        compilerLayoutHeight = document.getElementById("compilerLayoutHeightText").value;
    }
    else
    {
        alert("Please enter a valid decimal");
    }

}

function setWidthFromHeight()
{
    if (document.getElementById("compilerLayoutHeightText").value != "" && !isNaN(document.getElementById("compilerLayoutHeightText").value))
    {
        requestedHeight = document.getElementById("compilerLayoutHeightText").value;
        var tempScale = requestedHeight/originalHeight;
        var tempWidth = originalWidth * tempScale;
        tempWidth = tempWidth.toFixed(2);
        document.getElementById("compilerLayoutWidthText").value =  tempWidth;
        compilerLayoutWidth = document.getElementById("compilerLayoutWidthText").value;
    }
    else
    {
        alert("Please enter a valid decimal");
    }
}

function validateLayoutDimensions()
{
    var isValid = true;


    v1 = validateWidth(compilerLayoutWidth);
    v2 = validateMinimumWidth(compilerLayoutWidth);
    v3 = validateMaximumWidth(compilerLayoutWidth);

    v4 = validateHeight(compilerLayoutHeight);
    v5 = validateMinimumHeight(compilerLayoutHeight);
    v6 = validateMaximumHeight(compilerLayoutHeight);

    isValid = v1 && v2 && v3 && v4 && v5 && v6;

    return isValid;
}


var whitespace = " \t\n\r";
function isBlank(val)
{
    if(val == null || val.length == 0) {
        return true;
    } else {
        for(i=0; i<val.length; i++) {
            c = val.charAt(i);
            if(whitespace.indexOf(c) == -1) {
                return false;
            }
        }
    }

    return true;
}

function validateWidth(obj)
{
    var result = true;

    if (isNaN(obj) || isBlank(obj))
    {
        result = false;
        alert("Layout Width is not a number");
    }

    return result;
}
function validateHeight(obj)
{
    var result = true;

    if (isNaN(obj) || isBlank(obj))
    {
        result = false;
        alert("Layout Height is not a number");
    }
    return result;
}

function validateMinimumWidth(obj)
{
    var result = true;
    if(resizeMinimumWidth != null && resizeMinimumWidth != "" && parseFloat(obj) < resizeMinimumWidth)
    {
        result = false;
        alert("Width must be greater than or equal to " + resizeMinimumWidth);
    }

    return result;
}
function validateMaximumWidth(obj)
{
    var result = true;

    if(resizeMaximumWidth != null && resizeMaximumWidth != "" && parseFloat(obj) > resizeMaximumWidth)
    {
        result = false;
        alert("Width must be less than or equal to " + resizeMaximumWidth);
    }

    return result;
}
function validateMinimumHeight(obj)
{
    var result = true;

    if(resizeMinimumHeight != null && resizeMinimumHeight != "" && parseFloat(obj) < resizeMinimumHeight)
    {
        result = false;
        alert("Height must be greater than or equal to " + resizeMinimumHeight);
    }

    return result;
}
function validateMaximumHeight(obj)
{
    var result = true;

    if(resizeMaximumHeight != null && resizeMaximumHeight != "" && parseFloat(obj) > resizeMaximumHeight)
    {
        result = false;
        alert("Height must be less than or equal to " + resizeMaximumHeight);
    }

    return result;
}

function customizeLayout(url, scaleFactor, compilerLayoutHeight, compilerLayoutWidth, scaleable, resizable)
{

    if (scaleable == 'true' || resizable == 'true')
    {
      if(!validateLayoutDimensions()) { return; }
    }

    var resize = "&& false";
    var size = "&& false";

    if (resizable == 'true')
    {
        resize  = "&& true";
    }

    if (scaleable == 'true')
    {
        size = "&& true";
    }

    if (scaleFactor != null && scaleFactor != ""  && scaleable == 'true')
    {
        url += '&scaleFactor='+scaleFactor;
    }

    else if(compilerLayoutHeight != null && compilerLayoutHeight != "" && compilerLayoutWidth != null && compilerLayoutWidth != ""  && resizable == 'true')
    {
        url += '&ResizeHeight='+compilerLayoutHeight+'&ResizeWidth='+compilerLayoutWidth;
    }

    var w = openCompilerWindow(url);
    w.focus();
}

function updateLayout()
{
    var w = openCompilerWindow(compilerUpdateUrl);
    w.focus();
}

var compilerLayoutReadyToClose = false;

function compilerWindowReadyToClose() {
    return compilerLayoutReadyToClose;
}

function setCompilerWindowReadyToClose() {
    compilerLayoutReadyToClose = true;
}

function setCompilerWindowNotReadyToClose() {
    compilerLayoutReadyToClose = false;
}

function setPv(pvID)
{
    document.productPVSelectionForm.pvID.value = pvID;
}

function refreshPage()
{
    document.contactForm.submit();
    return false;
}

var needsProof = false;

function setNeedsProof()
{
     needsProof = true;
}

function proofed()
{
     needsProof = false;
}

function validateProof()
{
    if(needsProof)
    {
        return false;
    }
    return true;
}


function setPvUv(elem)
{
    if (document.productPVSelectionForm.pvUvID)
    {
        document.productPVSelectionForm.pvUvID.value = elem.value;
    }
}

function makeResizableDimensionsReadonly() {
    var el1 = document.getElementById("compilerLayoutWidthText");
    if(el1) { el1.readOnly = true; }
    var el2 = document.getElementById("compilerLayoutHeightText");
    if(el2) { el2.readOnly = true; }
}


