﻿//
//
// ----- Utility Functions -----
//
//
//

function GetEventTarget(evt)
{
    var target;
    if (evt.target)
    {
        target = evt.target;
    }
    else if (evt.srcElement)
    {
        target = evt.srcElement;
    }
    if (target.nodeType == 3) // defeat Safari bug
    {
        target = targ.parentNode;
    }
    return target;
}

function GetEventKeyCode(evt)
{
    var keycode;
    if (window.event)
    {
        keycode = evt.keyCode;
    }
    else if (evt)
    {
        keycode = evt.which;
    }
    return keycode;
}

//function ShowDDExtOnDownArrow(evt)
//{
//    var evt = evt || window.event;
//    var keycode = GetEventKeyCode(evt);
//    var target = GetEventTarget(evt);
//    var dde = $("#" + target.id).attr("ddeBehaviorId");
//    var menu = $("#" + target.id).attr("ddeMenuId");
//    if (keycode == 40)
//    {
//        $find(dde).show();
//        $("#" + menu + " a")[0].focus();
//    }
//    return true;
//}

//function MoveToNextDropDownEntryOnDownArrow(evt)
//{
//    var evt = evt || window.event;
//    var keycode = GetEventKeyCode(evt);
//    if (keycode == 40)
//    {
//        var target = GetEventTarget(evt);
//        var next = $("#" + target.id).next();
//        if (!next)
//        {
//            next = $("#" + target.id).first();
//        }
//        if (!next)
//        {
//            next.focus();
//        }
//    }
//    return true;
//}

//
// IsDefined
//
// Returns true if the type of the object is not 'undefined'
//
function IsDefined(obj)
{
    return (typeof (obj) != "undefined");
}

//
// GetObject
//
// Returns the object with the given name
//
function GetObject(sObjName)
{
    var dom = document.getElementById;
    var obj = (dom) ? document.getElementById(sObjName) : document.all[sObjName];
    return obj;
}

//
// SetInnerHTML
//
// Returns nothing.
//
// Sets the innerHTML attribute of the object with the given name to the HTML string passed in
//
function SetInnerHTML(sObjName, sHTML)
{
    GetObject(sObjName).innerHTML = sHTML;
}

//
// SetAndFocusTextbox
//
// Returns nothing.
//
// Sets the value attribute of the textbox with the given name to the string passed in
//
function SetTextbox(sTextboxName, sValue)
{
    var oTextBox = GetObject(sTextboxName);
    oTextBox.value = sValue;
    oTextBox.onchange();
}

//
// SaveObjectName
//
// Saves the name of the object in the hidden field
//
function SaveObjectName(oWithFocus, sHiddenFieldName)
{
    var object = GetObject(sHiddenFieldName);
    //alert("oldvalue='" + object.value + "', newvalue='" +oWithFocus.id + "'" )
    object.value = oWithFocus.id;
    object.click();
}

//
// DisableObject
//
// Returns nothing.
//
// Sets the disabled attribute of the object with the given name to the value passed in
//
function DisableObject(sName, bDisabled)
{
    var obj = GetObject(sName);
    obj.disabled = bDisabled;
}

//
// CheckBoxChecked
//
// Returns nothing.
//
// Client side validation script for verifying that checkbox has been checked.
//
//function CheckBoxChecked(val, args)
//{
//    args.IsValid = GetObject(args.Value).checked;
//}


//
// SetClass
//
// Returns nothing.
//
// Sets the CSS class of an object.
//
function SetClass(sName, sClassName)
{
    var obj = GetObject(sName);
    obj.className = sClassName;
}


//
// ToggleClass
//
// Returns nothing.
//
// Changes the CSS class of an object from the original class to the alternate class or back.
//
function ToggleClass(sName, sOriginalClassName, sAlternateClassName)
{
    var obj = GetObject(sName);

    if ((!IsDefined(obj.className)) || obj.className != sOriginalClassName)
    {
        obj.className = sOriginalClassName;
    }
    else
    {
        obj.className = sAlternateClassName;
    }
}


//
// SetVisibile
//
// Returns nothing.
//
// Changes the visibility of the given object.
//
function SetVisible(sName, bVisible)
{
    var obj = GetObject(sName);
    obj.style.visibility = bVisible ? 'visible' : 'hidden';
    obj.style.display = bVisible ? 'block' : 'none';
}


//
// SetImage
//
// Returns nothing.
//
// Changes the image URL of the given object.
//
function SetImage(sName, sImageUrl)
{
    var obj = GetObject(sName);
    obj.src = sImageUrl;
}


//
// OpenInNewWindow
//
// Opens the given link in a new browser window.
//
function OpenInNewWindow(sURL, iHeight, iWidth)
{
    window.open(sURL, "_blank", "status=0,toolbar=0,menubar=1,directories=0,resizable=1,scrollbars=1,height=" + iHeight + ",width=" + iWidth);
    //return(true);
}


//
// AbcDoUpload
//
// Used by the WebSupergoo ABC Upload control.
//
function AbcDoUpload(inForm, sApplicationVirtualPath, sHdnUploadButtonClicked)
{
    if ((typeof (Page_BlockSubmit) == 'boolean') && (Page_BlockSubmit == true)) return;

    var oHdnUploadButtonClicked = GetObject(sHdnUploadButtonClicked);
    if (oHdnUploadButtonClicked.value != "true") return;

    theFeats = 'height=160,width=600,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no';
    theUniqueID = Math.floor(Math.random() * 1000000) * ((new Date()).getTime() % 1000);

    var sPath = location.href.substring(0, location.href.indexOf('\/', location.href.indexOf('\/') + 2)) + sApplicationVirtualPath + "/Orders/ProgressBar.aspx?ProgressID=" + theUniqueID;

    var newWindow = window.open(sPath, theUniqueID, theFeats);
    newWindow.opener = self;

    var theForm = GetObject(inForm);
    thePos = theForm.action.indexOf('?');

    if (thePos >= 0) theForm.action = theForm.action.substring(0, thePos);
    theForm.action += '?UploadID=' + theUniqueID;

    return true;
}
