﻿// JScript File


function doPost (url, data, callback)
{ 
    var httpRequest; 
    if (typeof ActiveXObject != 'undefined')
    { 
        httpRequest = new ActiveXObject('Msxml2.XMLHTTP'); 
    } 
    else if (typeof XMLHttpRequest != 'undefined')
    { 
        httpRequest = new XMLHttpRequest(); 
    } 
    if (httpRequest)
    { 
        httpRequest.open('POST', url, true); 
        httpRequest.onreadystatechange = function ()
        { 
            if (httpRequest.readyState == 4)
            { 
                callback(httpRequest); 
            } 
        } 
        httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        var requestBody = '';
        for (var argName in data)
        {
            var encodedArgName = formEncode(argName); 
            var value = data[argName]; 
            if (typeof value == 'object')
            { 
                for (var i = 0; i < value.length; i++)
                { 
                    requestBody += encodedArgName + '=' + formEncode(value[i]) + '&'; 
                } 
            } 
            else
            { 
                requestBody += encodedArgName + '=' + formEncode(value) + '&'; 
            }
        }
        requestBody = requestBody.substring(0, requestBody.length - 1); 
        httpRequest.send(requestBody); 
    }
} 

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();
}
