﻿
function formEncode(arg)
{
   // Arguments should be x-www-form-urlencoded which means replace spaces with '+' signs.
   // We have to do it after the other encoding because the other encoding encodes the + signs also.
   arg = safeEncode(arg);
   
   return arg.replace(/%20/g, '+');
}
   
function safeEncode(value)
{ 
    if (typeof encodeURIComponent != 'undefined')
    { 
        return encodeURIComponent(value); 
    } 
    else
    { 
        return escape(value); 
    } 
} 

/* EXAMPLE
window.onload = function (evt) { 
   doPost('test20040207.aspx', 
     { 'GOD': 'Kibo', 
       'devil': 'Xibo' 
     }, 
     showResponseText 
   ); 
   doPost('test20040207.aspx', 
     { 'riders': ['Lance', 'Jan', 'Erik'], 
       'year': 2004 
     }, 
     showResponseText 
   ); 

} 
*/


//
// PostNavigate
//
// This function is a form of navigate like window.navigate or window.open.  But it 
// does something more in that it allows you to navigate with a POST verb.  It does this
// by dynamically building a FORM element with your values and it does a submit on that
// form element.
function PostNavigate(url, data)
{
    // Create the form element with the url as the action.
    var elForm = document.createElement("FORM");
    elForm.setAttribute("action", url);
    elForm.setAttribute("method", "POST");
    document.body.appendChild(elForm);
   
    // Add each argument as a hidden INPUT element.
    for (var argName in data)
    {
        var elInput = document.createElement("INPUT");
        elInput.setAttribute("type", "hidden");
        elInput.setAttribute("name", argName);
        elInput.setAttribute("value", data[argName]);
        
        elForm.appendChild(elInput);
    }
    
    // And submit the form.
    elForm.submit();
}

function GetNextSourceElement(element)
{
    if(element.nodeType != 3)
    {
        GetNextSourceElement.distance--;
        if(GetNextSourceElement.distance == 0)
        {
            return element;
        }
    }
    
    var elNext = element.firstChild;
    
    if(elNext != null)
    {
        elNext = GetNextSourceElement(elNext);
        if(GetNextSourceElement.distance == 0)
        {
            return elNext;
        } 
    }
    
    var elNext = element.nextSibling;
    if(elNext != null)
    {
        elNext = GetNextSourceElement(elNext);
    }
    
    if(GetNextSourceElement.distance == 0)
    {
        return elNext;
    }
    else
    {
        return null;
    }
}

function GetASPServerElement(markerId)
{
    var marker = document.getElementById(markerId);
    
    if(marker == null)
    {
        return null;
    }
    
    GetNextSourceElement.distance = parseInt(marker.innerHTML) + 1;
    return GetNextSourceElement(marker);
}

function GetParentDir(doc)
{
    if(!doc) doc = document;
    
    var strFull = doc.location.href;
    var nLastSlash = strFull.lastIndexOf("/");
    return strFull.slice(0,nLastSlash);
}

function GetTotalOffset(el)
{
    var obj = new Object;
    obj.offsetLeft = 0;
    obj.offsetTop = 0;
    
    while(el)
    {
        obj.offsetLeft += el.offsetLeft;
        obj.offsetTop += el.offsetTop;
        el = el.offsetParent;
    }
    return obj;
}

function OnEnterDoButton(buttonId, event)
{
    if(event.which || event.keyCode || event.charCode)
    {
        if ((event.which == 13) || (event.keyCode == 13) || (event.charCode == 13))
        {
            event.returnValue = false;
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();
            // document.getElementById(buttonId).click();
            return false;
        }
    }
    else
    {
        return true;
    };
}

function DisableEnter(event)
{
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if(keyCode == 13)
    {
        return false;
    } 
    else
    {
        return true;
    }
}      

// KeyPress is for FF
function OnFormCheckEnterKeyPress( e )
{
    var numCharCode;
    var elTarget;
    var strType;

    // get event if not passed
    if (!e) var e = window.event;

    // get character code of key pressed
    if (e.keyCode) numCharCode = e.keyCode;
    else if (e.which) numCharCode = e.which;

    // get target
    if (e.target) elTarget = e.target;
    else if (e.srcElement) elTarget = e.srcElement;

    // if form input field
    if ( elTarget.tagName.toLowerCase() == 'input' )
    {
        // get type
        strType = elTarget.getAttribute('type').toLowerCase();

        // based on type
        switch ( strType )
        {
            case 'checkbox' :
            case 'radio' :
            case 'text' :
            case 'password' :
                // if this is a return
                if ( numCharCode == 13 )
                {
                    var enterAction = elTarget.getAttribute('onenterkey');
                    if(enterAction)
                    {
                        eval(enterAction);
                    }
                    // cancel event to prevent form submission
                    return false;
                }
                break;
        };
    };
    // process default action
    return true;
}

function ReMapEnterKeyToOnEnterKey(el)
{
    el.onkeypress = OnFormCheckEnterKeyPress;
}
