﻿/***
Description: New File added for Sub Form Submission handling, as a part of "One-To-Many SubForm" project.

Created By: NAGESH CHOPRA
Created Date: February 22, 2007
*****/

var SubForm_xmlHTTP;
var counter = 0;

function SubForm_AJAX_Execute(handler, url) {
    var objXmlHttp = null;

    if (navigator.userAgent.indexOf('Opera') >= 0) {
        alert('This example does not work in Opera');
        return;
    }
    else if (navigator.userAgent.indexOf('MSIE') >= 0) {
        var strName = 'Msxml2.XMLHTTP';
        if (navigator.appVersion.indexOf('MSIE 5.5') >= 0) {
            strName = 'Microsoft.XMLHTTP';
        }
        try {
            objXmlHttp = new ActiveXObject(strName);
            objXmlHttp.onreadystatechange = handler;

        } catch (e) {
            alert('Error. Scripting for ActiveX might be disabled');
            return;
        }
    }
    else if (navigator.userAgent.indexOf('Mozilla') >= 0) {
        objXmlHttp = new XMLHttpRequest();
        objXmlHttp.onload = handler;
        objXmlHttp.onerror = handler;
    }
    //alert(objXmlHttp.responseText);
    SubForm_xmlHTTP = objXmlHttp;
    //   alert(SubForm_xmlHTTP.responseText);
    objXmlHttp.open('GET', url, false);
    objXmlHttp.send(null);

}


var goSrcSubForm = null;

/*Global object reference for the Source Sub Form being Submitted. This global variable
is required for the AJAX handler function "SubFormServiceHandler" to be able to save
the RecordID received from SubForm Submission Service, into the hidden the field of the
SubForm being submitted.
*/


var gaSubFormSubmitQueue = new Array();
/*Global Array representing a Queue for the actions put on hold (enqueued) 
while a sub form AJAX submission is under process. This queue maintained for the scenario
when the user submits parent form without saving his changes to the sub forms. 
In this case the unsaved sub forms are submitted through a loop before submission 
of parent form. Since sub form submission loop works faster than the actual AJAX 
submission of the form, so it creates incosistent results, like sub form submission 
going missing randomly or ajax handler being executed just for the last sub form in the 
loop. This queue hold the subsequent form submissions while the current sub forms Ajax 
call is in process. 
It also enqueues (holds) the submission of basicned (parent form). Parent form is put 
on hold because there is a PostNewAction on it that misses some subform records if 
parent form is submitted before Ajax process for all sub forms has ended.
*/

var gbSubFormHasErrors = new Boolean();
gbSubFormHasErrors = false;

var SubFormTempRecordIDExists = new Boolean(false);

function GetParentByTagName(element, tag) {
    while (element != null && element.tagName != tag) {
        element = element.parentNode;
    }
    return element;
}

function GetParentById(element, lookupID) {

    while (element != null && element.id != lookupID) {
        element = element.parentNode;

    }

    return element;
}

function fnCloneSubFormByID(uniqueSubFormID) {
    var oCloneSrc = document.getElementByID(uniqueSubFormID);
    if (typeof (oCloneSrc) == 'object')
        fnCloneSubForm(oCloneSrc);
}

function fnCloneSubForm(oCloneSrc, oInsertBefore) {

    /* the 'true' possible value specifies to clone
    the childNodes as well.
    */
    var oCloneNode = oCloneSrc.cloneNode(true);
    /* When the cloned node is added,
    oCloneSrc becomes a collection.
    */

    oCloneNode.id = oCloneNode.id + "_Clone";
    oCloneNode.name = oCloneNode.name + "_Clone";

    oCloneNode.style.display = 'block';

    if (typeof (oInsertBefore) != 'object') {
        oInsertBefore = null;
    }
    oCloneSrc.parentNode.insertBefore(oCloneNode, oInsertBefore);

    var SubFormMaxSequenceInt = 0;

    SubFormMaxSequenceInt = fnGetMaxSequenceIntBySubFormSeedId(oCloneSrc.id);


    var SubFormSequenceFieldId;
    SubFormSequenceFieldId = oCloneSrc.all("SubFormSequenceInputName").value;

    oCloneNode.all(SubFormSequenceFieldId).value = SubFormMaxSequenceInt + 1;
}

function fnCloneSubFormDisplayNone(oCloneSrc, oInsertBefore) {

    /* the 'true' possible value specifies to clone
    the childNodes as well.
    */
    var oCloneNode = oCloneSrc.cloneNode(true);
    /* When the cloned node is added,
    oCloneSrc becomes a collection.
    */

    oCloneNode.id = oCloneNode.id + "_Clone";
    oCloneNode.name = oCloneNode.name + "_Clone";


    oCloneNode.style.display = 'none';

    if (typeof (oInsertBefore) != 'object') {
        oInsertBefore = null;
    }
    oCloneSrc.parentNode.insertBefore(oCloneNode, oInsertBefore);

    var SubFormMaxSequenceInt = 0;

    SubFormMaxSequenceInt = fnGetMaxSequenceIntBySubFormSeedId(oCloneSrc.id);


    var SubFormSequenceFieldId;
    SubFormSequenceFieldId = oCloneSrc.all("SubFormSequenceInputName").value;

    oCloneNode.all(SubFormSequenceFieldId).value = SubFormMaxSequenceInt + 1;
}

function fnLocateSubFormSequenceField(oSubFormSeed) {
    var SubFormSequenceFieldId;
    if (oSubFormSeed.all("_InputNames") != null) {
        var arrSubFormInputNames = oSubFormSeed.all("_InputNames").value.toString().split("~|");
    }

    if (typeof (arrSubFormInputNames) != 'undefined') {
        var InputItem;
        for (InputItem in arrSubFormInputNames) {
            if (oSubFormSeed.all(arrSubFormInputNames[InputItem]) != null && oSubFormSeed.all(arrSubFormInputNames[InputItem]).value.toUpperCase() == 'SUBFORMSEQUENCE') {
                SubFormSequenceFieldId = oSubFormSeed.all(arrSubFormInputNames[InputItem]).name;
                break;
            }
        }
    }

    oSubFormSeed.all("SubFormSequenceInputName").value = SubFormSequenceFieldId;
}


function fnGetMaxSequenceIntBySubFormSeedId(pSubFormSeedId) {

    var SubFormSequenceFieldId;
    SubFormSequenceFieldId = document.getElementById(pSubFormSeedId).all("SubFormSequenceInputName").value;

    var SubFormMaxSequenceInt = 0;
    var arrSubForms, cntSubForm;

    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Edit");
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        if (isNaN(parseInt(arrSubForms[cntSubForm].all(SubFormSequenceFieldId).value))) {
            arrSubForms[cntSubForm].all(SubFormSequenceFieldId).value = "1";
        }
        if (arrSubForms[cntSubForm].all(SubFormSequenceFieldId).value > SubFormMaxSequenceInt) {
            SubFormMaxSequenceInt = eval(arrSubForms[cntSubForm].all(SubFormSequenceFieldId).value);
        }
    }

    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Copy");
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        if (arrSubForms[cntSubForm].all(SubFormSequenceFieldId).value > SubFormMaxSequenceInt) {
            SubFormMaxSequenceInt = eval(arrSubForms[cntSubForm].all(SubFormSequenceFieldId).value);
        }
    }
    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Clone");
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        if (arrSubForms[cntSubForm].all(SubFormSequenceFieldId).value > SubFormMaxSequenceInt) {
            SubFormMaxSequenceInt = eval(arrSubForms[cntSubForm].all(SubFormSequenceFieldId).value);
        }
    }

    return SubFormMaxSequenceInt;
}

function fnAddSubForm(oSrcSubForm, oSubFormValidator) {

    if (typeof (oSubFormValidator) != "object" || typeof (oSubFormValidator.validateSubForm) != "function") {
        try {
            oSubFormValidator = eval(String().concat(oSrcSubForm.name, "_Validator"));
            if (typeof (oSubFormValidator) != "object" || typeof (oSubFormValidator.validateSubForm) != "function") {
                throw "";
            }
        }
        catch (e) {
            oSubFormValidator = new Validator(oSrcSubForm.name);
        }
    }

    oSrcSubForm.all("PageUse").value = "NewSubmit";

    gaSubFormSubmitQueue.push(function() {
        if (oSubFormValidator.validateSubForm(oSrcSubForm)) {
            fnSubmitSubForm(oSrcSubForm, fnAddSubFormHandler);
            return true;
        }
        else {
            gbSubFormHasErrors = true;
            gaSubFormSubmitQueue = new Array();
            return false;
        }
    });
    if (gaSubFormSubmitQueue.length == 1) {
        gaSubFormSubmitQueue.slice(0, 1)[0]();
    }

}

function fnAddSubFormEdit(oSrcSubForm, oSubFormValidator) {

    if (typeof (oSubFormValidator) != "object" || typeof (oSubFormValidator.validateSubForm) != "function") {
        try {
            oSubFormValidator = eval(String().concat(oSrcSubForm.name, "_Validator"));
            if (typeof (oSubFormValidator) != "object" || typeof (oSubFormValidator.validateSubForm) != "function") {
                throw "";
            }
        }
        catch (e) {
            oSubFormValidator = new Validator(oSrcSubForm.name);
        }
    }

    oSrcSubForm.all("PageUse").value = "NewSubmit";

    gaSubFormSubmitQueue.push(function() {
        if (oSubFormValidator.validateSubForm(oSrcSubForm)) {
            fnSubmitSubFormEdit(oSrcSubForm, fnAddSubFormHandlerEdit);
            return true;
        }
        else {
            gbSubFormHasErrors = true;
            gaSubFormSubmitQueue = new Array();
            return false;
        }
    });
    if (gaSubFormSubmitQueue.length == 1) {
        gaSubFormSubmitQueue.slice(0, 1)[0]();
    }

}

function fnSaveSubFormEdit(oSrcSubForm, oSubFormValidator) {



    if (typeof (oSubFormValidator) != "object" || typeof (oSubFormValidator.validateSubForm) != "function") {
        try {
            oSubFormValidator = eval(String().concat(oSrcSubForm.name, "_Validator"));
            if (typeof (oSubFormValidator) != "object" || typeof (oSubFormValidator.validateSubForm) != "function") {
                throw "";
            }
        }
        catch (e) {
            oSubFormValidator = new Validator(oSrcSubForm.name);
        }
    }


    oSrcSubForm.all("PageUse").value = "EditSubmit";
    gaSubFormSubmitQueue.push(function() {
        if (oSubFormValidator.validateSubForm(oSrcSubForm)) {
            fnSubmitSubFormEdit(oSrcSubForm, fnSaveSubFormHandlerEdit);
            return true;
        }
        else {
            gbSubFormHasErrors = true;
            gaSubFormSubmitQueue = new Array();
            return false;
        }
    });
    if (gaSubFormSubmitQueue.length == 1) {
        gaSubFormSubmitQueue.slice(0, 1)[0]();
    }

}

function fnRemoveSubForm(oSrcSubForm, oCloneSrc) {
    //alert(oSrcSubForm);
    //alert(oSrcSubForm.all("PageUse").value);
    oSrcSubForm.all("PageUse").value = "DeleteSubmit";
    fnSubmitSubForm(oSrcSubForm, fnRemoveSubFormHandler);
    if (document.getElementsByName(oSrcSubForm.id).length == 0 && typeof (oCloneSrc) == 'object') {
        fnCloneSubForm(oCloneSrc);
    }
}

function fnRemoveSubFormEdit(oSrcSubForm, oCloneSrc) {

    oSrcSubForm.all("PageUse").value = "DeleteSubmit";
    fnSubmitSubForm(oSrcSubForm, fnRemoveSubFormHandler);

}

function fnRemoveCopySubForm(oSrcSubForm, oCloneSrc) {
    oSrcSubForm.removeNode(true);
    if (document.getElementsByName(oSrcSubForm.id).length == 0 && typeof (oCloneSrc) == 'object') {
        fnCloneSubForm(oCloneSrc);
    }
}



function fnSubmitSubForm(oSrcSubForm, fnSubmitSubFormHandler) {
    if (oSrcSubForm.all("_InputNames") != null) {
        var arrSubFormInputNames = oSrcSubForm.all("_InputNames").value.toString().split("~|");

    }

    var FieldsQueryString, SubFormSubmitUrl;
    FieldsQueryString = "";
    SubFormSubmitUrl = "";

    if (typeof (arrSubFormInputNames) != 'undefined') {
        var InputItem;
        for (InputItem in arrSubFormInputNames) {


            if (oSrcSubForm.all(arrSubFormInputNames[InputItem]) != null) {
                var inputText = String(oSrcSubForm.all(arrSubFormInputNames[InputItem]).value);
                inputText = inputText.replace(/%/g, "%25");
                inputText = inputText.ReplaceAll(" ", "%20");
                inputText = inputText.ReplaceAll(",", "%2C");
                inputText = inputText.ReplaceAll("&", "%26");
                inputText = inputText.ReplaceAll("+", "%2B");
                inputText = inputText.replace(/~/g, "%7E");
                FieldsQueryString = FieldsQueryString + "&";
                FieldsQueryString = FieldsQueryString + arrSubFormInputNames[InputItem];
                FieldsQueryString = FieldsQueryString + "=";
                FieldsQueryString = FieldsQueryString + inputText;
            }
        }
    }
    if (FieldsQueryString.indexOf("&") == 0) {
        FieldsQueryString = FieldsQueryString.slice(1);
    }

    SubFormSubmitUrl = "NEDService.asp?" + FieldsQueryString;
    //alert(SubFormSubmitUrl);

    goSrcSubForm = oSrcSubForm;

    document.body.style.cursor = "wait";
    SubForm_AJAX_Execute(fnSubmitSubFormHandler, SubFormSubmitUrl);


}

function fnSubmitSubFormEdit(oSrcSubForm, fnSubmitSubFormHandler) {
    if (oSrcSubForm.all("_InputNames") != null) {
        var arrSubFormInputNames = oSrcSubForm.all("_InputNames").value.toString().split("~|");

    }

    var FieldsQueryString, SubFormSubmitUrl;
    FieldsQueryString = "";
    SubFormSubmitUrl = "";

    if (typeof (arrSubFormInputNames) != 'undefined') {
        var InputItem;
        for (InputItem in arrSubFormInputNames) {

            if (oSrcSubForm.all(arrSubFormInputNames[InputItem]) != null) {
                var inputText = String(oSrcSubForm.all(arrSubFormInputNames[InputItem]).value);
                inputText = inputText.replace(/%/g, "%25");
                inputText = inputText.ReplaceAll(" ", "%20");
                inputText = inputText.ReplaceAll(",", "%2C");
                inputText = inputText.replace(/&/g, "%26");
                inputText = inputText.ReplaceAll("+", "%2B");
                inputText = inputText.replace(/~/g, "%7E");
                FieldsQueryString = FieldsQueryString + "&";
                FieldsQueryString = FieldsQueryString + arrSubFormInputNames[InputItem];
                FieldsQueryString = FieldsQueryString + "=";
                FieldsQueryString = FieldsQueryString + inputText;
                oSrcSubForm.all(arrSubFormInputNames[InputItem]).style.color = "Black";
            }
        }
    }
    if (FieldsQueryString.indexOf("&") == 0) {
        FieldsQueryString = FieldsQueryString.slice(1);
    }

    SubFormSubmitUrl = "NEDService.asp?" + FieldsQueryString;
    // alert(SubFormSubmitUrl);

    goSrcSubForm = oSrcSubForm;

    document.body.style.cursor = "wait";
    SubForm_AJAX_Execute(fnSubmitSubFormHandler, SubFormSubmitUrl);


}

function fnSaveEnableSubFormAll(oSrcSubForm) {
    if (oSrcSubForm.all("_InputNames") != null) {
        var arrSubFormInputNames = oSrcSubForm.all("_InputNames").value.toString().split("~|");
    }
    if (typeof (arrSubFormInputNames) != 'undefined') {
        var InputItem;
        for (InputItem = 0; InputItem < arrSubFormInputNames.length; InputItem++) {
            oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onchange', function() { oSrcSubForm.all("SubFormModified").value = 1; });
            oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onkeydown', function() { oSrcSubForm.all("SubFormModified").value = 1; });
        }
    }
}

function fnSaveEnableSubForm(oSrcSubForm) {


    if (oSrcSubForm.all("_InputNames") != null) {
        var arrSubFormInputNames = oSrcSubForm.all("_InputNames").value.toString().split("~|");

    }

    if (typeof (arrSubFormInputNames) != 'undefined') {
        var InputItem;
        for (InputItem = 0; InputItem < arrSubFormInputNames.length; InputItem++) {

            if (oSrcSubForm.all(arrSubFormInputNames[InputItem]) != null && oSrcSubForm.all(arrSubFormInputNames[InputItem]).type != 'hidden') {
                // oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onchange', function () {if (oSrcSubForm.all("_SaveBtn").style.display == "none") { oSrcSubForm.all("_SaveBtn").style.display = "inline"; } });
                //  oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onkeydown', function () {if (oSrcSubForm.all("_SaveBtn").style.display == "none") { oSrcSubForm.all("_SaveBtn").style.display = "inline"; } });
                oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onchange', function() { counter = 0; oSrcSubForm.all("SubFormModified").value = 1; });
                oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onkeydown', function() { counter = 0; oSrcSubForm.all("SubFormModified").value = 1; });
            }
        }
    }

}



function fnSaveEnableSubFormEdit(oSrcSubForm) {


    if (oSrcSubForm.all("_InputNames") != null) {
        var arrSubFormInputNames = oSrcSubForm.all("_InputNames").value.toString().split("~|");

    }

    if (typeof (arrSubFormInputNames) != 'undefined') {
        var InputItem;
        for (InputItem = 0; InputItem < arrSubFormInputNames.length; InputItem++) {

            if (oSrcSubForm.all(arrSubFormInputNames[InputItem]) != null && oSrcSubForm.all(arrSubFormInputNames[InputItem]).type != 'hidden') {
                oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onchange', function() { if (oSrcSubForm.all("_SaveBtn").style.display == "none") { oSrcSubForm.all("_SaveBtn").style.display = "inline"; } });
                oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onkeydown', function() { if (oSrcSubForm.all("_SaveBtn").style.display == "none") { oSrcSubForm.all("_SaveBtn").style.display = "inline"; } });
                oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onchange', function() { oSrcSubForm.all("SubFormModified").value = 1; });
                oSrcSubForm.all(arrSubFormInputNames[InputItem]).attachEvent('onkeydown', function() { oSrcSubForm.all("SubFormModified").value = 1; });
            }
        }
    }

}


function fnSaveEnableSubFormById(sSubFormId) {
    var arrSubForms = document.getElementsByName(sSubFormId);

    for (var cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        fnSaveEnableSubForm(arrSubForms[cntSubForm]);
    }
}

function fnSaveEnableSubFormAllById(sSubFormId) {
    var arrSubForms = document.getElementsByName(sSubFormId);

    for (var cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        fnSaveEnableSubFormAll(arrSubForms[cntSubForm]);
    }
}

function fnSaveEnableSubFormEditById(sSubFormId) {
    var arrSubForms = document.getElementsByName(sSubFormId);

    for (var cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        fnSaveEnableSubFormEdit(arrSubForms[cntSubForm]);
    }
}

function fnAddTempParentRecordIdToSubForm(sSubFormId, sFieldName, sTempParentRecordId) {
    //    alert(sSubFormId);
    //    alert(sFieldName);
    //    alert(sTempParentRecordId);


    var arrSubForms = document.getElementsByName(sSubFormId);

    for (var cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        if (arrSubForms[cntSubForm].all(sFieldName) != null) {
            // alert("fnAddTempParentRecordIdToSubForm: "+arrSubForms[cntSubForm].all(sFieldName).value);
            arrSubForms[cntSubForm].all(sFieldName).value = sTempParentRecordId;
        }
    }

    // if (!SubFormTempRecordIDExists) {
    try {
        basicned.action += "&TempRecordID=" + sTempParentRecordId;
    }
    catch (err)
        { }
    // SubFormTempRecordIDExists = true;
    // }
    // alert(basicned.action);
}


function fnAddSubFormHandler() {
    if (SubForm_xmlHTTP.readyState == 4 || SubForm_xmlHTTP.readyState == 'complete') {

        var responseText = SubForm_xmlHTTP.responseText;

        var sRecordID = "";

        sRecordID = fnLookUpAJAXResponse(responseText);

        if (sRecordID != null) {

            sRecordID = sRecordID[0].split("___")[1];
            goSrcSubForm.all("RecordId").value = sRecordID;

            oAddBtn = goSrcSubForm.all("_AddBtn");
            oRemoveBtn = goSrcSubForm.all("_RemoveBtn");
            // oSaveBtn = goSrcSubForm.all("_SaveBtn");
            oAddBtn.style.display = "none";
            oRemoveBtn.style.display = "inline";
            //  oSaveBtn.style.display = "inline";

            goSrcSubForm.id = goSrcSubForm.id.replace("_Clone", "_Edit");
            goSrcSubForm.name = goSrcSubForm.name.replace("_Clone", "_Edit");

            fnSaveEnableSubForm(goSrcSubForm);

            if (document.getElementsByName(goSrcSubForm.id.replace("_Edit", "_Clone")).length == 0) {
                fnCloneSubForm(document.getElementById(goSrcSubForm.id.replace("_Edit", "")));
            }
        }
        else {
            gaSubFormSubmitQueue = new Array();
        }

        document.body.style.cursor = "auto";

        gaSubFormSubmitQueue.shift();
        if (gaSubFormSubmitQueue.length > 0) {
            gaSubFormSubmitQueue.slice(0, 1)[0]()
        }
    }
}

function fnAddSubFormHandlerEdit() {
    if (SubForm_xmlHTTP.readyState == 4 || SubForm_xmlHTTP.readyState == 'complete') {

        var responseText = SubForm_xmlHTTP.responseText;

        var sRecordID = "";

        sRecordID = fnLookUpAJAXResponse(responseText);

        if (sRecordID != null) {

            sRecordID = sRecordID[0].split("___")[1];
            goSrcSubForm.all("RecordId").value = sRecordID;

            oAddBtn = goSrcSubForm.all("_AddBtn");
            oRemoveBtn = goSrcSubForm.all("_RemoveBtn");

            oAddBtn.style.display = "none";
            oRemoveBtn.style.display = "inline";

            goSrcSubForm.all("_SaveBtn").style.display = "none";

            goSrcSubForm.id = goSrcSubForm.id.replace("_Clone", "_Edit");
            goSrcSubForm.name = goSrcSubForm.name.replace("_Clone", "_Edit");

            fnSaveEnableSubFormEdit(goSrcSubForm);

            if (document.getElementsByName(goSrcSubForm.id.replace("_Edit", "_Clone")).length == 0) {
                fnCloneSubForm(document.getElementById(goSrcSubForm.id.replace("_Edit", "")));
            }
        }
        else {
            gaSubFormSubmitQueue = new Array();
        }

        document.body.style.cursor = "auto";

        gaSubFormSubmitQueue.shift();
        if (gaSubFormSubmitQueue.length > 0) {
            gaSubFormSubmitQueue.slice(0, 1)[0]()
        }
    }
}


function fnSaveSubFormHandler() {
    if (SubForm_xmlHTTP.readyState == 4 || SubForm_xmlHTTP.readyState == 'complete') {
        var responseText = SubForm_xmlHTTP.responseText;
        var sRecordID = "";

        sRecordID = fnLookUpAJAXResponse(responseText);

        if (sRecordID != null) {
            goSrcSubForm.all("_SaveBtn").style.display = "none";
            goSrcSubForm.all("SubFormModified").value = 0;
        }
        else {
            gaSubFormSubmitQueue = new Array();
        }

        document.body.style.cursor = "auto";

        gaSubFormSubmitQueue.shift();
        if (gaSubFormSubmitQueue.length > 0) {
            gaSubFormSubmitQueue.slice(0, 1)[0]();
        }
    }
}

function fnSaveSubFormHandlerEdit() {
    if (SubForm_xmlHTTP.readyState == 4 || SubForm_xmlHTTP.readyState == 'complete') {
        var responseText = SubForm_xmlHTTP.responseText;
        var sRecordID = "";

        sRecordID = fnLookUpAJAXResponse(responseText);

        if (sRecordID != null) {
            goSrcSubForm.all("_SaveBtn").style.display = "none";
            goSrcSubForm.all("SubFormModified").value = 0;
        }
        else {
            gaSubFormSubmitQueue = new Array();
        }

        document.body.style.cursor = "auto";

        gaSubFormSubmitQueue.shift();
        if (gaSubFormSubmitQueue.length > 0) {
            gaSubFormSubmitQueue.slice(0, 1)[0]();
        }
    }
}



function fnRemoveSubFormHandler() {
    if (SubForm_xmlHTTP.readyState == 4 || SubForm_xmlHTTP.readyState == 'complete') {
        var responseText = SubForm_xmlHTTP.responseText;
        var sRecordID = "";

        sRecordID = fnLookUpAJAXResponse(responseText);

        if (sRecordID != null) {
            goSrcSubForm.removeNode(true);
        }
        else {
            //gaSubFormSubmitQueue = new Array();
        }

        document.body.style.cursor = "auto";
    }
}


function fnSaveUnSavedSubFormsBySubFormSeedId(pSubFormSeedId, pFieldsExclusionList) {

    gaSubFormSubmitQueue = new Array;

    pFieldsExclusionList = String().concat("~|", pFieldsExclusionList, "~|").toUpperCase();

    var arrSubForms = new Array(), arrSubFormInputNames = new Array();
    var cntSubForm = new Number();
    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Edit");
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        //KK change to modified field so that Save buttons do not have to be displayed
        //if (arrSubForms[cntSubForm].all("_SaveBtn").style.display != "none") {

        //  alert("fnSaveUnSavedSubFormsBySubFormSeedId : "+arrSubForms[cntSubForm].all("SubFormModified").value);

        if (arrSubForms[cntSubForm].all("SubFormModified").value == 1) {
            arrSubForms[cntSubForm].all("_SaveBtn").fireEvent("onclick");
        }
    }




    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Clone");
    if (arrSubForms.length > 0) {

        arrSubFormInputNames = arrSubForms[0].all("_InputNames").value.toUpperCase().split("~|");
    }
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        // alert("Clone : " + arrSubForms[cntSubForm].id);
        //if (arrSubForms[cntSubForm].all("_AddBtn").style.display != "none") {
        if (arrSubForms[cntSubForm].all("SubFormModified").value != 1) {
            for (InputItem in arrSubFormInputNames) {
                if (arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]) != null
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).value.trim() != ""
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).type != 'hidden'
                    && pFieldsExclusionList.indexOf(arrSubFormInputNames[InputItem]) == -1) {

                    arrSubForms[cntSubForm].all("_AddBtn").fireEvent("onclick");
                    //    alert("Clone click");
                    arrSubForms[cntSubForm].all("SubFormModified").value = 1;
                    break;
                }
            }
        }
    }

    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Copy");
    if (arrSubForms.length > 0) {
        arrSubFormInputNames = arrSubForms[0].all("_InputNames").value.toUpperCase().split("~|");
    }
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {

        if (arrSubForms[cntSubForm].all("SubFormModified").value != 1) {
            for (InputItem in arrSubFormInputNames) {
                if (arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]) != null
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).value.trim() != ""
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).type != 'hidden'
                    && pFieldsExclusionList.indexOf(arrSubFormInputNames[InputItem]) == -1) {

                    arrSubForms[cntSubForm].all("_AddBtn").fireEvent("onclick");
                    //   alert("Copy click");
                    arrSubForms[cntSubForm].all("SubFormModified").value = 1;

                    break;
                }
            }
        }
    }


    if (gaSubFormSubmitQueue.length > 0) {

        gaSubFormSubmitQueue.push(function() {
            if (gbSubFormHasErrors) { return false; }
            else if (typeof (basicned.onsubmit) != "function") { basicned.submit(); return true; }
            else if (basicned.onsubmit()) { basicned.submit(); return true; }
            else { return false; }
        }
        )
        return false;
    }
    else {
        //return true;
        if (formValidator.hasErrors() || gbSubFormHasErrors) {
            gbSubFormHasErrors = false;
            return false;
        }
        else {
            return true;
        }
    }

}

function fnSaveUnSavedSubFormsEditWithCheckBySubFormSeedId(pSubFormSeedId, pFieldsExclusionList, pValidationCheck) {

    if (counter == 1) {
        counter = 0;
        return false;
    }
    var validationCheck = pValidationCheck.split(',');
    var columnName = validationCheck[2];
    var total = 0;
    var validationColumnName = document.getElementsByName(validationCheck[0]);
    for (var i = 0; i <= validationColumnName.length - 1; i++) {
        if (!isNaN(parseFloat(validationColumnName[i].value))) {
            total = total + parseFloat(validationColumnName[i].value);
        }

    }
    document.getElementsByName(columnName)[0].value = total;
    var validationCheck = validationCheck[1];
    if (total != validationCheck) {
        alert("Total Deal Share % does not equal 100%");
        counter = 1;
        return false;
    }
    else {
        counter = 0;
    }


    gaSubFormSubmitQueue = new Array;

    pFieldsExclusionList = String().concat("~|", pFieldsExclusionList, "~|").toUpperCase();

    var arrSubForms = new Array(), arrSubFormInputNames = new Array();
    var cntSubForm = new Number();
    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Edit");
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {

        if (arrSubForms[cntSubForm].all("SubFormModified").value == 1) {
            arrSubForms[cntSubForm].all("_SaveBtn").fireEvent("onclick");
        }
    }




    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Clone");
    if (arrSubForms.length > 0) {

        arrSubFormInputNames = arrSubForms[0].all("_InputNames").value.toUpperCase().split("~|");
    }
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {

        if (arrSubForms[cntSubForm].all("SubFormModified").value != 1) {
            for (InputItem in arrSubFormInputNames) {
                if (arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]) != null
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).value.trim() != ""
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).type != 'hidden'
                    && pFieldsExclusionList.indexOf(arrSubFormInputNames[InputItem]) == -1) {

                    arrSubForms[cntSubForm].all("_AddBtn").fireEvent("onclick");

                    arrSubForms[cntSubForm].all("SubFormModified").value = 1;
                    break;
                }
            }
        }
    }

    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Copy");
    if (arrSubForms.length > 0) {
        arrSubFormInputNames = arrSubForms[0].all("_InputNames").value.toUpperCase().split("~|");
    }
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {

        if (arrSubForms[cntSubForm].all("SubFormModified").value != 1) {
            for (InputItem in arrSubFormInputNames) {
                if (arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]) != null
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).value.trim() != ""
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).type != 'hidden'
                    && pFieldsExclusionList.indexOf(arrSubFormInputNames[InputItem]) == -1) {

                    arrSubForms[cntSubForm].all("_AddBtn").fireEvent("onclick");

                    arrSubForms[cntSubForm].all("SubFormModified").value = 1;

                    break;
                }
            }
        }
    }


    if (gaSubFormSubmitQueue.length > 0) {
        counter = 0;
        gaSubFormSubmitQueue.push(function() {
            if (gbSubFormHasErrors) { return false; }
            else if (typeof (basicned.onsubmit) != "function") { basicned.submit(); return true; }
            else if (basicned.onsubmit()) { basicned.submit(); return true; }
            else { return false; }
        }
        )
        return false;
    }
    else {
        if (formValidator.hasErrors() || gbSubFormHasErrors) {
            gbSubFormHasErrors = false;
            counter = 1;
            return false;
        }
        else {
            counter = 0;
            //   location.reload(true);
            return true;

        }

    }

}



function fnSaveUnSavedSubFormsEditBySubFormSeedId(pSubFormSeedId, pFieldsExclusionList) {

    if (counter == 1) {
        return false;
    }

    gaSubFormSubmitQueue = new Array;

    pFieldsExclusionList = String().concat("~|", pFieldsExclusionList, "~|").toUpperCase();

    var arrSubForms = new Array(), arrSubFormInputNames = new Array();
    var cntSubForm = new Number();
    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Edit");
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {
        //KK change to modified field so that Save buttons do not have to be displayed
        //if (arrSubForms[cntSubForm].all("_SaveBtn").style.display != "none") {

        //  alert("fnSaveUnSavedSubFormsBySubFormSeedId : "+arrSubForms[cntSubForm].all("SubFormModified").value);

        if (arrSubForms[cntSubForm].all("SubFormModified").value == 1) {
            arrSubForms[cntSubForm].all("_SaveBtn").fireEvent("onclick");
        }
    }




    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Clone");
    if (arrSubForms.length > 0) {

        arrSubFormInputNames = arrSubForms[0].all("_InputNames").value.toUpperCase().split("~|");
    }
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {

        if (arrSubForms[cntSubForm].all("SubFormModified").value != 1) {
            for (InputItem in arrSubFormInputNames) {
                if (arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]) != null
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).value.trim() != ""
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).type != 'hidden'
                    && pFieldsExclusionList.indexOf(arrSubFormInputNames[InputItem]) == -1) {

                    arrSubForms[cntSubForm].all("_AddBtn").fireEvent("onclick");

                    arrSubForms[cntSubForm].all("SubFormModified").value = 1;
                    break;
                }
            }
        }
    }

    arrSubForms = document.getElementsByName(pSubFormSeedId + "_Copy");
    if (arrSubForms.length > 0) {
        arrSubFormInputNames = arrSubForms[0].all("_InputNames").value.toUpperCase().split("~|");
    }
    for (cntSubForm = 0; cntSubForm < arrSubForms.length; cntSubForm++) {

        if (arrSubForms[cntSubForm].all("SubFormModified").value != 1) {
            for (InputItem in arrSubFormInputNames) {
                if (arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]) != null
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).value.trim() != ""
                    && arrSubForms[cntSubForm].all(arrSubFormInputNames[InputItem]).type != 'hidden'
                    && pFieldsExclusionList.indexOf(arrSubFormInputNames[InputItem]) == -1) {

                    arrSubForms[cntSubForm].all("_AddBtn").fireEvent("onclick");

                    arrSubForms[cntSubForm].all("SubFormModified").value = 1;

                    break;
                }
            }
        }
    }


    if (gaSubFormSubmitQueue.length > 0) {
        counter = 0;
        gaSubFormSubmitQueue.push(function() {
            if (gbSubFormHasErrors) { return false; }
            else if (typeof (basicned.onsubmit) != "function") { basicned.submit(); return true; }
            else if (basicned.onsubmit()) { basicned.submit(); return true; }
            else { return false; }
        }
        )
        return false;
    }
    else {
        if (formValidator.hasErrors() || gbSubFormHasErrors) {
            gbSubFormHasErrors = false;
            counter = 1;
            return false;
        }
        else {
            counter = 0;
            //     location.reload(true);
            return true;

        }

    }

}

function fnSetUpSubFormValidation(pSubFormName, oXMLSubFormValidationSpec) {
    //Validator.clearItems();
    // alert(pSubFormName);
    //alert(oXMLSubFormValidationSpec);
    var oSubFormValidator = new Validator(pSubFormName);
    var xmlSpecDoc = oXMLSubFormValidationSpec.XMLDocument;
    //alert(xmlSpecDoc.xml);
    oSubFormValidator.addItemsFromXML(xmlSpecDoc);

    return oSubFormValidator;

}


function fnLookUpAJAXResponse(responseText) {

    var oLoginFailedRegExp = new RegExp(/<Title>Login Failed<\/Title>/i);
    var sLoginFailed = responseText.match(oLoginFailedRegExp);
    var oRecordIDRegExp = new RegExp(/###RECORDGENERATED___\d+___###/i);
    var sRecordID = responseText.match(oRecordIDRegExp);

    var odt = window.top.document;

    if (sLoginFailed != null) {
        alert("Login Failed - Cannot save the changes - Please login again");
        sRecordID = null;

        try {
            //odt.all.bIsLoginOpen.innerText = false;
            odt.body.removeChild(odt.all.bIsLoginOpen);
        }
        catch (e) { }

        var wLoginPopUp = window.open("loginpopup.asp", 'credentials', 'width=500, height=455');
        //wLoginPopUp.document.write(responseText);
        //wLoginPopUp.document.location.reload();
    }

    else if (sRecordID == null) {
        alert("Unexpected failure - Cannot save the changes - Please login again and retry. If the error persists, contact the technical team.");

        try {
            //odt.all.bIsLoginOpen.innerText = false;
            odt.body.removeChild(odt.all.bIsLoginOpen);
        }
        catch (e) { }

        var wLoginPopUp = window.open("loginpopup.asp", 'credentials', 'width=500, height=455');
        //wLoginPopUp.document.write(responseText);
        //wLoginPopUp.document.location.reload();             
    }

    return sRecordID;

}





String.prototype.lTrim =
     function() {
         return this.replace(/^\s+/, '');
     }
String.prototype.lTrim =
     function() {
         return this.replace(/\s+$/, '');
     }
String.prototype.trim =
     function() {
         return this.replace(/^\s+|\s+$/g, '');
     }

String.prototype.ReplaceAll = function(stringToFind, stringToReplace) {
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}