﻿
function clearInnerHTML(obj) {
    nObj = obj.cloneNode(false);
    obj.parentNode.insertBefore(nObj, obj);
    obj.parentNode.removeChild(obj);
}

function replaceHtml(el, html) {
    var oldEl = typeof el === "string" ? document.getElementById(el) : el;
    /*@cc_on // Pure innerHTML is slightly faster in IE
    oldEl.innerHTML = html;
    return oldEl;
    @*/
    var newEl = oldEl.cloneNode(false);
    newEl.innerHTML = html;
    oldEl.parentNode.replaceChild(newEl, oldEl);
    return newEl;
};

function createForm(formName, actionURL, target, method) {

    var oForm = document.createElement("FORM");
    oForm.name = formName; //Form name
    oForm.id = formName; // Form ID
    oForm.action = actionURL; // URL to post back
    oForm.target = target; // This is important. Use “_blank” if you wish to post back to new window
    oForm.method = method; // Method GET or POST
    return oForm;

}


function SendForm(sFormName) {
    try {
        var sTarget = '_self';
        var oSpan = window.document.getElementById(sFormName);
        if (oSpan == null) { oSpan = window.document.getElementsByName(sFormName).item(0); };
        if (oSpan == null) { alert('Unable to find data to submit: ' + sFormName); return false; };
        //need certain values from oSpan object (action="" target="_blank")
        if (oSpan.action == null) { alert('Unable to determine target form action'); return false; };
        if (oSpan.target != null) { sTarget = oSpan.target; };

        var parentElement = window.document.getElementById("FormArea");
        parentElement.innerHTML = ""
        
        //create form tag
        var oForm = createForm(sFormName, oSpan.action, sTarget, "POST");
        if (oForm == null) { alert('Unable to find target form to submit'); return false; };

        //var newFormContent = oSpan.cloneNode(true);
        var newFormContent = oSpan.innerHTML;
        // clear the contents of your destination element.
        //clearInnerHTML(parentElement);
        oForm = replaceHtml(oForm, newFormContent)
        // Place the form into the page
        parentElement.appendChild(oForm);
        oForm.submit();
        return false;
    } catch (Err) { alert(Err); return false; };
}