﻿var oUploadFileXML = "";
var oUploadCommonFileXML = "";
var oUploadENTITYFileXML = "";
var oUploadAllFileXML = "";
var oViewOrderXml = "";
var oViewENTfileXml = "";
var langKey = "";
var varOpenFlag = 0;
var oListgridForDetail;
var oDatasourceForDetail;
var RowIDForDetail;
var gUploadFile_ReqPage = '';
var CC_Status;
var common_SelectedIndexes = [];// used for collecting selected indexes for any grid(checkbox selection)
var common_LocalRefreshEnable = false; // used for refreshed the grid from local datasource.
//var gIsFromUploadPanel = false;   /// used if UploadFile page is open from UploadPanel

function ppInfoBox(InfoboxId) {

    var mi = document.getElementById(InfoboxId);
    if (mi != null) {
        mi.className = "ppinfo";
        mi.style.display = "none";
    }
}
function NoOfRecords(Listgrid, Spanid) {
    
    var oListgrid = bb.document.getElementById(Listgrid);
    var dataset = oListgrid.getDataSource();
    var Records = bb.getProperty(dataset, 'totalRecords')
   
    if (Records == '0') {
        document.getElementById(Spanid).innerHTML = 'No record found !';
        bb.document.getElementById('lmPleaseWait').hide();
    }
    else {
        document.getElementById(Spanid).innerHTML = '';
    }
    common_LocalRefreshEnable = false;
};
function hideppInfoBox(InfoboxId) {

    var mi = document.getElementById(InfoboxId);
    mi.style.display = "block";
}
function showTopMsg(messageText, hideAfter, isError, isClearRequired) {
    var tm = document.getElementById('topmsg');
    if (tm) {
        if (isError == '1')
            tm.style.color = "red";
        else
            tm.style.color = "green";
        tm.style.display = "block";
        tm.innerHTML = messageText;
        if (isClearRequired == true) {
            var btnDiv = document.getElementById('topMsgButton');
            if (btnDiv) {
                btnDiv.style.display = 'block';
            }
        }
        else
            setTimeout("hidediv('topmsg')", hideAfter);
    }
}
function hidediv(arg) {
    document.getElementById(arg).style.display = 'none';
}


function showTopplaceorderMsg(messageText, hideAfter, isError, isClearRequired) {
    var tm = document.getElementById('topmsgplaceorder');
    if (tm) {
        if (isError == '1')
            tm.style.color = "red";
        else
            tm.style.color = "green";
        tm.style.display = "block";
        tm.innerHTML = messageText;
        if (isClearRequired == true) {
            var btnDiv = document.getElementById('topMsgplaceorderButton');
            if (btnDiv) {
                btnDiv.style.display = 'block';
            }
        }
        else
            setTimeout("hidediv('topmsgplaceorder')", hideAfter);
    }
}
function EcStencileuncheck() {
    if (document.getElementById("ChkStencilcompatible") != null) {
        document.getElementById("ChkStencilcompatible").checked = false;
    }

}

function getSelectedText(comboBoxId) {
    var comboBox = bb.evaluateSmart("id('" + comboBoxId + "')");
    if (comboBox == null) {
        //throw "comboBoxId is invalid";
        return '';
    }
    else {
        var selectedIndex = comboBox.getProperty('selectedIndex');
        if (selectedIndex == -1) {
            return null;
        }
        return selecteText = comboBox.getProperty('selectedOption').getProperty('text');
        //        if (bb.browser.ie)
        //            return comboBox.modelNode.childNodes[selectedIndex].text;
        //        else
        //            return comboBox.modelNode.childNodes[selectedIndex].textContent;
    }
}

function addOption(selectbox, text, value, isSelected) {
    if (text != null) {
        var btl = "http://www.backbase.com/2006/btl";
        var option = bb.document.createElementNS(btl, 'b:comboBoxOption');
        bb.command.setText(option, text);
        option.setAttribute("value", value);
        if (isSelected)
            option.setAttribute("selected", "true");
        selectbox.appendChild(option);
        return option;
    }
};
function addListBoxOption(listbox, text, value) {
    var btl = "http://www.backbase.com/2006/btl";
    option = bb.document.createElementNS(btl, 'b:listBoxOption');
    bb.command.setText(option, text);
    option.setAttribute("value", value);
    listbox.appendChild(option);
};
function addCheckBoxGroupOption(checkboxgroup, text, value, val) {
    var btl = "http://www.backbase.com/2006/btl";
    option = bb.document.createElementNS(btl, 'b:checkBoxGroupOption');
    bb.command.setText(option, text);
    option.setAttribute("value", value);
    option.viewNode.style.width = "50%";
    if (val)
        option.setAttribute("selected", "true");
    checkboxgroup.appendChild(option);
};

// Added By Manish Maintaining Order Status
function Enum() { };
Enum.OrderStatus = { Exception: 181, Production: 184, Cancel: 185, Preparation: 186, New: 255 };
Enum.ExceptionStatus = { New: 402, PutInProcess: 403, Continue: 404, PutToCustomer: 405, SendToCustomer: 406 };
// Ended

function OnFail(arg) {
    alert('Error:' + arg._message);
};
function getGuid() {
    var guid = "{";
    for (var i = 0; i < 32; i++)
        guid += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "")
    return guid + "}";
};

function setColumnResizing(oListgrid) {
    var aColumns = oListgrid.selectNodes('.//b:listGridCol[@resizable = "true"]');
    var bFoundFirst = false;
    if (!aColumns || aColumns.length == 0) return;

    for (var i = 0, iLimit = aColumns.length; i < iLimit; i++) {
        if (!bFoundFirst && !aColumns[i].getProperty('hidden')) {
            bFoundFirst = true;
            aColumns[i].setAttribute('resizeEdges', 'right');
        }
        else {
            aColumns[i].setAttribute('resizeEdges', 'right left');
            aColumns[i].setAttribute('resizeType', 'abstract');
            aColumns[i].setAttribute('resizeGripSize', '8px');
            aColumns[i].setProperty('resizable', true);
        }
    }
};
function saveListgridPrefrences(oListgridId) {
    var oListgrid = bb.document.getElementById(oListgridId);
    if (oListgrid == null || oListgrid == undefined) throw Error("Invalid Listgrid!");
    var aColumns = oListgrid.selectNodes('.//b:listGridCol[@resizable = "true"]', NSResolver);
    if (!aColumns || aColumns.length == 0) return;
    var oList = [];
    for (var i = 0, iLimit = aColumns.length; i < iLimit; i++) {
        var listGridCol = aColumns[i];
        var oListColumn = {};
        oListColumn.id = listGridCol.getAttribute("id");
        oListColumn.width = listGridCol.getAttribute("width");
        oList.push(oListColumn);
    }
    var oListProperties = {};
    oListProperties.columns = oList;
    oListProperties.sortField = oListgrid.getAttribute("sortField");
    oListProperties.sortDirection = oListgrid.getAttribute("sortDirection");
    var vData = JSONRequest.serialize(oListProperties);
    EC09WebApp.EC09WebService.EC09WebService.saveGridSettings(oListgridId, vData, function(oResponse) {
        alert(MsgSaveGridSettings);
    }, OnFail);
};
function setListgridPrefrences(oListgrid) {
    // get grid settings data from the server using broker page        
    var GridId = oListgrid.getAttribute("id");
    var sUrl = "API/JSONRequest.aspx?Gridid=" + GridId
    var iRequest = JSONRequest.get(sUrl,
                 function(iRequestNumber, Data, Err) {
                     if (Err) {
                         alert(Err.message);
                     }
                     else {
                         if (Data) {
                             var oListProperties = Data;
                             var sortField = oListProperties.sortField;
                             if (sortField)
                                 oListgrid.setProperty("sortField", sortField);
                             var sortDirection = oListProperties.sortDirection;
                             if (sortDirection)
                                 oListgrid.setProperty("sortDirection", sortDirection);
                             var aColumns = oListProperties.columns;
                             if (aColumns && aColumns.length > 0) {
                                 for (var i = 0, iLimit = aColumns.length; i < iLimit; i++) {
                                     var ListColumn = oListgrid.selectSingleNode('.//b:listGridCol[@resizable = "true" and @id="' + aColumns[i].id + '"]', NSResolver);
                                     ListColumn.setAttribute("width", aColumns[i].width);
                                 }
                             }
                         }
                     }
                 });
};
NSResolver = function(sPrefix) {
    switch (sPrefix) {
        case "b":
            return "http://www.backbase.com/2006/btl";
            break;
        case "c":
            return "http://www.backbase.com/2006/command";
            break;
        default:
            return null;
    }
};
function parseBTL(oXml, sTarget, sMode) {
    var myXML = bb.xml.parse(oXml).documentElement;
    var oDestElm = bb.document.getElementById(sTarget);
    bb.command.create(myXML, oDestElm, sMode);
};
function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji)
            return ft[1];
    }
};
String.prototype.trim = function() {
    var varToTrim = this.replace(/^\s+/, '');
    return varToTrim.replace(/\s+$/, '');
};

//Common function to clear combo values
function ClearCombo(comboId) {
    var i, recordlength = bb.document.getElementById(comboId).getProperty("childNodes").length;
    if (recordlength != undefined) {
        for (i = 0; i < recordlength; i++)
            bb.command.destroy(bb.document.getElementById(comboId).getProperty("childNodes")[0]);
    }
};
function TrimSpacesFromInputTextBox(tblTableName) {
    var tblOfInputTextBoxes = document.getElementById(tblTableName);
    var txtInputBoxes = tblOfInputTextBoxes.getElementsByTagName("input");
    for (var intCountTextBox = 0; intCountTextBox < txtInputBoxes.length; intCountTextBox++) {
        if (txtInputBoxes[intCountTextBox] != null)
            txtInputBoxes[intCountTextBox].value = txtInputBoxes[intCountTextBox].value.trim();
    }
};
function clearHTMLCombo(comboId) {
    var cbo = document.getElementById(comboId);
    cbo.options.length = 0;
};
//Added by Maitreyee on 12th May 2009 to return formatted date
//function GetFormattedDate(value)
//{
//   if (value!='')
//   { var strDate=value;
//    var strSplitted = strDate.split("-");
//    return strSplitted[2].substr(0,2) + '/' + strSplitted[1] + '/' + strSplitted[0]; 
//   }
//   else
//    return '';
//};

function GetFormattedDate(value) {
    try
    {
    if (value != '') {
        var strDate = value;
        var strSplitted = strDate.split("-");
        var datepassed = strSplitted[1] + '/' + strSplitted[2].substr(0, 2) + '/' + strSplitted[0] + ' ' + strSplitted[2].substr(3, 8);


        var now = new Date();
        var offset = now.getTimezoneOffset() / 60;
        var strOffset = String(offset);
        if (strOffset != "0") {
            if (strOffset.charAt(0) == "-") {
                strOffset = strOffset.substr(1, strOffset.length); // If - sign then Remove
            }
            else {
                strOffset = "-" + strOffset; //Else Add - Sign
            }
        }

        var retdate = getCalculatedOffsetDate(parseFloat(strOffset), datepassed); //Pass here Foramat MM/DD/YYYY
        //return GetDateFormat(retdate, 'MM/dd/yyyy hh:nn:ss'); //Pass here Users Format
        var strrootfomat = document.getElementById("hdnrootcompanydateformat").value;
        if (strrootfomat != "") {
            return GetDateFormat(retdate, strrootfomat);
        } else {
            return GetDateFormat(retdate, 'dd/MM/yyyy'); // DEFAULT
        }
    }
    else
        return '';

    }
    catch(err) {
        
        return 'Hold'
    }
};

function GetFormattedDateWithTime(value) {
    if (value != '') {
        var strDate = value;
        var strSplitted = strDate.split("-");
        var datepassed = strSplitted[1] + '/' + strSplitted[2].substr(0, 2) + '/' + strSplitted[0] + ' ' + strSplitted[2].substr(3, 8);


        var now = new Date();
        var offset = now.getTimezoneOffset() / 60;
        var strOffset = String(offset);
        if (strOffset != "0") {
            if (strOffset.charAt(0) == "-") {
                strOffset = strOffset.substr(1, strOffset.length); // If - sign then Remove
            }
            else {
                strOffset = "-" + strOffset; //Else Add - Sign
            }
        }

        var retdate = getCalculatedOffsetDate(parseFloat(strOffset), datepassed); //Pass here Foramat MM/DD/YYYY
        //return GetDateFormat(retdate, 'MM/dd/yyyy hh:nn:ss'); //Pass here Users Format
        var strrootfomat = document.getElementById("hdnrootcompanydateformat").value;
        if (strrootfomat != "") {
            return GetDateFormat(retdate, strrootfomat + '  ' + 'HH:NN:SS');
        } else {
            return GetDateFormat(retdate, 'dd/MM/yyyy HH:NN:SS'); // DEFAULT
        }
    }
    else
        return '';

};


function getCalculatedOffsetDate(offset, dat) {
    var now = new Date(dat);
    var hour = 60 * 60 * 1000;
    var min = 60 * 1000;
    return new Date(now.getTime() + (offset * min) + (offset * hour));
}


function GetDateFormat(formatDate, formatString) {
    if (formatDate instanceof Date) {
        var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
        var yyyy = formatDate.getFullYear();
        var yy = yyyy.toString().substring(2);
        var m = parseInt(formatDate.getMonth()) + 1;
        var mm = m < 10 ? "0" + m : m;
        var mmm = months[m];
        var d = formatDate.getDate();
        var dd = d < 10 ? "0" + d : d;

        var h = formatDate.getHours();
        var hh = h < 10 ? "0" + h : h;
        var n = formatDate.getMinutes();
        var nn = n < 10 ? "0" + n : n;
        var s = formatDate.getSeconds();
        var ss = s < 10 ? "0" + s : s;

        formatString = formatString.replace(/yyyy/i, yyyy);
        formatString = formatString.replace(/yy/i, yy);
        formatString = formatString.replace(/mmm/i, mmm);
        formatString = formatString.replace(/mm/i, mm);
        formatString = formatString.replace(/m/i, m);
        formatString = formatString.replace(/dd/i, dd);
        formatString = formatString.replace(/d/i, d);
        formatString = formatString.replace(/hh/i, hh);
        formatString = formatString.replace(/h/i, h);
        formatString = formatString.replace(/nn/i, nn);
        formatString = formatString.replace(/n/i, n);
        formatString = formatString.replace(/ss/i, ss);
        formatString = formatString.replace(/s/i, s);

        return formatString;
    } else {
        return "";
    }
};

















function LoadUploadFile(success, fail, sTarget, sMode) {
    bb.document.getElementById('lmPleaseWait').show();
    if (oUploadFileXML == "") {

        var lmPleaseWait = bb.document.getElementById('lmPleaseWait');
        lmPleaseWait.show();
        bb.command.load('FileManagement/UploadOrderFiles.aspx', 'GET', null, null, null, null,
            function(oResponse) {
                if (oResponse) {
                    oUploadFileXML = oResponse.responseText;
                    parseBTL(oUploadFileXML, sTarget, sMode);
                    bb.document.getElementById('lmPleaseWait').hide();
                    eval(success + "()");
                }
            }, null);
    }
    else {
        parseBTL(oUploadFileXML, sTarget, sMode);
        bb.document.getElementById('lmPleaseWait').hide();
        eval(success + "()");
    }

};

function LoadUploadCommonFile(success, fail, sTarget, sMode) {
    bb.document.getElementById('lmPleaseWait').show();
    if (oUploadCommonFileXML == "") {

        var lmPleaseWait = bb.document.getElementById('lmPleaseWait');
        lmPleaseWait.show();
        bb.command.load('FileManagement/UploadCommonFile.aspx', 'POST', null, null, null, null,
            function(oResponse) {
                if (oResponse) {
                    oUploadCommonFileXML = oResponse.responseText;
                    parseBTL(oUploadCommonFileXML, sTarget, sMode);
                    bb.document.getElementById('lmPleaseWait').hide();
                    eval(success + "()");
                }
            }, null);
    }
    else {
        parseBTL(oUploadCommonFileXML, sTarget, sMode);
        bb.document.getElementById('lmPleaseWait').hide();
        eval(success + "()");
    }

};

function LoadUploadFileForPanel(success, fail, sTarget, sMode) {
    bb.document.getElementById('lmPleaseWait').show();
    if (oUploadFileXML == "") {

        var lmPleaseWait = bb.document.getElementById('lmPleaseWait');
        lmPleaseWait.show();
        bb.command.load('FileManagement/UploadFileForPanel.aspx', 'GET', null, null, null, null,
            function(oResponse) {
                if (oResponse) {
                    oUploadFileXML = oResponse.responseText;
                    parseBTL(oUploadFileXML, sTarget, sMode);
                    bb.document.getElementById('lmPleaseWait').hide();
                    eval(success + "()");
                }
            }, null);
    }
    else {
        parseBTL(oUploadFileXML, sTarget, sMode);
        bb.document.getElementById('lmPleaseWait').hide();
        eval(success + "()");
    }

};

function LoadEntityUploadFile(successENT, fail, sTargetENT, sModeENT) {
    if (oUploadENTITYFileXML == "") {
        bb.document.getElementById('lmPleaseWait').show();
        bb.command.load('FileManagement/UploadOrderFiles.aspx', 'GET', null, null, null, null,
            function(oResponse) {
                if (oResponse) {
                    oUploadENTITYFileXML = oResponse.responseText;
                    parseBTL(oUploadENTITYFileXML, sTargetENT, sModeENT);
                    eval(successENT + "()");
                }
            }, null);
    }
    else {
        parseBTL(oUploadENTITYFileXML, sTargetENT, sModeENT);
        eval(successENT + "()");
    }

};



function LoadViewFileForPage(CallFromPage,OrderNumber,OrderType, success, fail)
{
    var winViewFilesID = bb.document.getElementById("ViewOrderFiles");
    if (winViewFilesID) {
        if (winViewFilesID.getAttribute("open") == "false") {
            winViewFilesID.setAttribute("open", "true");
        }
        else {
            if (winViewFilesID.getAttribute("minimized") == 'true') {
                winViewFilesID.restore();
            }
            bb.document.getElementById('cphContentArea').activate(winViewFilesID);
        }

        debugger;
        eval(success + "()");
    }
    else {
            oViewOrderXml = "";
        if (oViewOrderXml == "") {

            bb.document.getElementById('lmPleaseWait').show();
            bb.command.load('FileManagement/ViewOrderFiles.aspx?From=' + CallFromPage + '&OrderNumber=' + OrderNumber + '&OrderType=' + OrderType  , 'GET', null, null, null, null,
         function(oResponse) {
             if (oResponse) {
                 oViewOrderXml = oResponse.responseText;
                 parseBTL(oViewOrderXml, 'cphContentArea', 'firstChild');
                 bb.document.getElementById('lmPleaseWait').hide();
                 eval(success + "()");
             }
         }

         );
        }
        else {
            parseBTL(oViewOrderXml, 'cphContentArea', 'firstChild');
            bb.document.getElementById('lmPleaseWait').hide();
            eval(success + "()");
        }

    }
};
function LoadViewFile(success, fail) {

    var winViewFilesID = bb.document.getElementById("ViewOrderFiles");
    if (winViewFilesID) {
        if (winViewFilesID.getAttribute("open") == "false") {
            winViewFilesID.setAttribute("open", "true");
        }
        else {
            if (winViewFilesID.getAttribute("minimized") == 'true') {
                winViewFilesID.restore();
            }
            bb.document.getElementById('cphContentArea').activate(winViewFilesID);
        }

        debugger;
        eval(success + "()");
    }
    else {
        if (oViewOrderXml == "") {

            bb.document.getElementById('lmPleaseWait').show();
            bb.command.load('FileManagement/ViewOrderFiles.aspx', 'GET', null, null, null, null,
         function(oResponse) {
             if (oResponse) {
                 oViewOrderXml = oResponse.responseText;
                 parseBTL(oViewOrderXml, 'cphContentArea', 'firstChild');
                 bb.document.getElementById('lmPleaseWait').hide();
                 eval(success + "()");
             }
         }

         );
        }
        else {
            parseBTL(oViewOrderXml, 'cphContentArea', 'firstChild');
            bb.document.getElementById('lmPleaseWait').hide();
            eval(success + "()");
        }

    }

};
function LoadViewCompanyFile(successENT, fail) {


    var LoadViewCompanyFile = bb.document.getElementById("ViewFiles");
    if (LoadViewCompanyFile) {
        if (LoadViewCompanyFile.getAttribute("open") == "false") {
            LoadViewCompanyFile.setAttribute("open", "true");
        }
        else {
            if (LoadViewCompanyFile.getAttribute("minimized") == 'true') {
                LoadViewCompanyFile.restore();
            }
            bb.document.getElementById('cphContentArea').activate(LoadViewCompanyFile);
        }
        eval(successENT + "()");
    }
    else {
        if (oViewENTfileXml == "") {
            bb.command.load('FileManagement/ViewFile.aspx', 'GET', null, null, null, null,
         function(oResponse) {
             if (oResponse) {
                 oViewENTfileXml = oResponse.responseText;
                 parseBTL(oViewENTfileXml, 'cphContentArea', 'firstChild');
                 bb.document.getElementById('lmPleaseWait').hide();
                 eval(successENT + "()");
             }
         });
        }
        else {
            parseBTL(oViewENTfileXml, 'cphContentArea', 'firstChild');
            bb.document.getElementById('lmPleaseWait').hide();
            eval(success + "()");
        }

    }

};
//Added by Maitreyee on 15th July 09, to get the key id of selected row in a grid
function GetSelectedIdOfGrid(lstGrdId, keyId) {
    var lstGrd = bb.document.getElementById(lstGrdId);
    var ds = lstGrd.getDataSource();
    var RowID = 0;
    var RowID = bb.getProperty(lstGrd, 'selectedIndex');
    var RecordID = 0;

    if (RowID != undefined)
        RecordID = btl.dataSource.getValue(ds, RowID, keyId);

    return RecordID;
};
function LoadViewContactFile(successENT, fail) {
    var LoadViewContactFile = bb.document.getElementById("ViewFiles");
    if (LoadViewContactFile) {
        if (LoadViewContactFile.getAttribute("open") == "false") {
            LoadViewContactFile.setAttribute("open", "true");
        }
        else {
            if (LoadViewContactFile.getAttribute("minimized") == 'true') {
                LoadViewContactFile.restore();
            }
            bb.document.getElementById('cphContentArea').activate(LoadViewContactFile);
        }
        eval(successENT + "()");
    }
    else {
        if (oViewENTfileXml == "") {
            bb.command.load('FileManagement/ViewFile.aspx', 'GET', null, null, null, null,
         function(oResponse) {
             if (oResponse) {
                 oViewENTfileXml = oResponse.responseText;
                 parseBTL(oViewENTfileXml, 'cphContentArea', 'firstChild');
                 eval(successENT + "()");
             }
         });
        }
        else {
            parseBTL(oViewENTfileXml, 'cphContentArea', 'firstChild');
            eval(success + "()");
        }

    }

};

function callWebService(webMethod, parameters, onSucceeded, onFailed) {
    Sys.Net.WebServiceProxy.invoke("EC09WebService/EC09WebService.asmx",
        webMethod, false, parameters, onSucceeded,
        onFailed);
};


//////////////Added By bhavik/////////////////////////

function SwitchToAdmin() {
    if (gAdminName != '') {
        EC09WebApp.EC09WebService.EC09WebService.SwicthToAdmin(gAdminName, OnSuccesAdminLogin, OnFailAdminLogin);
    }
}

function OnSuccesAdminLogin() {
    location.href = 'Default.aspx';
}

function OnFailAdminLogin() {

};

function isEmpty(val) {
    if (val) {
        return ((val === null) || val.length == 0 || /^\s+$/.test(val));
    } else {
        return true;
    }
};


// Convert HTML entities into numerical entities
function HTML2Numerical(s) {
    var arr1 = new Array('&nbsp;', '&iexcl;', '&cent;', '&pound;', '&curren;', '&yen;', '&brvbar;', '&sect;', '&uml;', '&copy;', '&ordf;', '&laquo;', '&not;', '&shy;', '&reg;', '&macr;', '&deg;', '&plusmn;', '&sup2;', '&sup3;', '&acute;', '&micro;', '&para;', '&middot;', '&cedil;', '&sup1;', '&ordm;', '&raquo;', '&frac14;', '&frac12;', '&frac34;', '&iquest;', '&agrave;', '&aacute;', '&acirc;', '&atilde;', '&Auml;', '&aring;', '&aelig;', '&ccedil;', '&egrave;', '&eacute;', '&ecirc;', '&euml;', '&igrave;', '&iacute;', '&icirc;', '&iuml;', '&eth;', '&ntilde;', '&ograve;', '&oacute;', '&ocirc;', '&otilde;', '&Ouml;', '&times;', '&oslash;', '&ugrave;', '&uacute;', '&ucirc;', '&Uuml;', '&yacute;', '&thorn;', '&szlig;', '&agrave;', '&aacute;', '&acirc;', '&atilde;', '&auml;', '&aring;', '&aelig;', '&ccedil;', '&egrave;', '&eacute;', '&ecirc;', '&euml;', '&igrave;', '&iacute;', '&icirc;', '&iuml;', '&eth;', '&ntilde;', '&ograve;', '&oacute;', '&ocirc;', '&otilde;', '&ouml;', '&divide;', '&oslash;', '&ugrave;', '&uacute;', '&ucirc;', '&uuml;', '&yacute;', '&thorn;', '&yuml;', '&quot;', '&amp;', '&lt;', '&gt;', '&oelig;', '&oelig;', '&scaron;', '&scaron;', '&yuml;', '&circ;', '&tilde;', '&ensp;', '&emsp;', '&thinsp;', '&zwnj;', '&zwj;', '&lrm;', '&rlm;', '&ndash;', '&mdash;', '&lsquo;', '&rsquo;', '&sbquo;', '&ldquo;', '&rdquo;', '&bdquo;', '&dagger;', '&dagger;', '&permil;', '&lsaquo;', '&rsaquo;', '&euro;', '&fnof;', '&alpha;', '&beta;', '&gamma;', '&delta;', '&epsilon;', '&zeta;', '&eta;', '&theta;', '&iota;', '&kappa;', '&lambda;', '&mu;', '&nu;', '&xi;', '&omicron;', '&pi;', '&rho;', '&sigma;', '&tau;', '&upsilon;', '&phi;', '&chi;', '&psi;', '&omega;', '&alpha;', '&beta;', '&gamma;', '&delta;', '&epsilon;', '&zeta;', '&eta;', '&theta;', '&iota;', '&kappa;', '&lambda;', '&mu;', '&nu;', '&xi;', '&omicron;', '&pi;', '&rho;', '&sigmaf;', '&sigma;', '&tau;', '&upsilon;', '&phi;', '&chi;', '&psi;', '&omega;', '&thetasym;', '&upsih;', '&piv;', '&bull;', '&hellip;', '&prime;', '&prime;', '&oline;', '&frasl;', '&weierp;', '&image;', '&real;', '&trade;', '&alefsym;', '&larr;', '&uarr;', '&rarr;', '&darr;', '&harr;', '&crarr;', '&larr;', '&uarr;', '&rarr;', '&darr;', '&harr;', '&forall;', '&part;', '&exist;', '&empty;', '&nabla;', '&isin;', '&notin;', '&ni;', '&prod;', '&sum;', '&minus;', '&lowast;', '&radic;', '&prop;', '&infin;', '&ang;', '&and;', '&or;', '&cap;', '&cup;', '&int;', '&there4;', '&sim;', '&cong;', '&asymp;', '&ne;', '&equiv;', '&le;', '&ge;', '&sub;', '&sup;', '&nsub;', '&sube;', '&supe;', '&oplus;', '&otimes;', '&perp;', '&sdot;', '&lceil;', '&rceil;', '&lfloor;', '&rfloor;', '&lang;', '&rang;', '&loz;', '&spades;', '&clubs;', '&hearts;', '&diams;');
    var arr2 = new Array('&#160;', '&#161;', '&#162;', '&#163;', '&#164;', '&#165;', '&#166;', '&#167;', '&#168;', '&#169;', '&#170;', '&#171;', '&#172;', '&#173;', '&#174;', '&#175;', '&#176;', '&#177;', '&#178;', '&#179;', '&#180;', '&#181;', '&#182;', '&#183;', '&#184;', '&#185;', '&#186;', '&#187;', '&#188;', '&#189;', '&#190;', '&#191;', '&#192;', '&#193;', '&#194;', '&#195;', '&#196;', '&#197;', '&#198;', '&#199;', '&#200;', '&#201;', '&#202;', '&#203;', '&#204;', '&#205;', '&#206;', '&#207;', '&#208;', '&#209;', '&#210;', '&#211;', '&#212;', '&#213;', '&#214;', '&#215;', '&#216;', '&#217;', '&#218;', '&#219;', '&#220;', '&#221;', '&#222;', '&#223;', '&#224;', '&#225;', '&#226;', '&#227;', '&#228;', '&#229;', '&#230;', '&#231;', '&#232;', '&#233;', '&#234;', '&#235;', '&#236;', '&#237;', '&#238;', '&#239;', '&#240;', '&#241;', '&#242;', '&#243;', '&#244;', '&#245;', '&#246;', '&#247;', '&#248;', '&#249;', '&#250;', '&#251;', '&#252;', '&#253;', '&#254;', '&#255;', '&#34;', '&#38;', '&#60;', '&#62;', '&#338;', '&#339;', '&#352;', '&#353;', '&#376;', '&#710;', '&#732;', '&#8194;', '&#8195;', '&#8201;', '&#8204;', '&#8205;', '&#8206;', '&#8207;', '&#8211;', '&#8212;', '&#8216;', '&#8217;', '&#8218;', '&#8220;', '&#8221;', '&#8222;', '&#8224;', '&#8225;', '&#8240;', '&#8249;', '&#8250;', '&#8364;', '&#402;', '&#913;', '&#914;', '&#915;', '&#916;', '&#917;', '&#918;', '&#919;', '&#920;', '&#921;', '&#922;', '&#923;', '&#924;', '&#925;', '&#926;', '&#927;', '&#928;', '&#929;', '&#931;', '&#932;', '&#933;', '&#934;', '&#935;', '&#936;', '&#937;', '&#945;', '&#946;', '&#947;', '&#948;', '&#949;', '&#950;', '&#951;', '&#952;', '&#953;', '&#954;', '&#955;', '&#956;', '&#957;', '&#958;', '&#959;', '&#960;', '&#961;', '&#962;', '&#963;', '&#964;', '&#965;', '&#966;', '&#967;', '&#968;', '&#969;', '&#977;', '&#978;', '&#982;', '&#8226;', '&#8230;', '&#8242;', '&#8243;', '&#8254;', '&#8260;', '&#8472;', '&#8465;', '&#8476;', '&#8482;', '&#8501;', '&#8592;', '&#8593;', '&#8594;', '&#8595;', '&#8596;', '&#8629;', '&#8656;', '&#8657;', '&#8658;', '&#8659;', '&#8660;', '&#8704;', '&#8706;', '&#8707;', '&#8709;', '&#8711;', '&#8712;', '&#8713;', '&#8715;', '&#8719;', '&#8721;', '&#8722;', '&#8727;', '&#8730;', '&#8733;', '&#8734;', '&#8736;', '&#8743;', '&#8744;', '&#8745;', '&#8746;', '&#8747;', '&#8756;', '&#8764;', '&#8773;', '&#8776;', '&#8800;', '&#8801;', '&#8804;', '&#8805;', '&#8834;', '&#8835;', '&#8836;', '&#8838;', '&#8839;', '&#8853;', '&#8855;', '&#8869;', '&#8901;', '&#8968;', '&#8969;', '&#8970;', '&#8971;', '&#9001;', '&#9002;', '&#9674;', '&#9824;', '&#9827;', '&#9829;', '&#9830;');
    return swapArrayVals(s, arr1, arr2);
}

// Convert Numerical entities into HTML entities
function NumericalToHTML(s) {
    var arr1 = new Array('&#160;', '&#161;', '&#162;', '&#163;', '&#164;', '&#165;', '&#166;', '&#167;', '&#168;', '&#169;', '&#170;', '&#171;', '&#172;', '&#173;', '&#174;', '&#175;', '&#176;', '&#177;', '&#178;', '&#179;', '&#180;', '&#181;', '&#182;', '&#183;', '&#184;', '&#185;', '&#186;', '&#187;', '&#188;', '&#189;', '&#190;', '&#191;', '&#192;', '&#193;', '&#194;', '&#195;', '&#196;', '&#197;', '&#198;', '&#199;', '&#200;', '&#201;', '&#202;', '&#203;', '&#204;', '&#205;', '&#206;', '&#207;', '&#208;', '&#209;', '&#210;', '&#211;', '&#212;', '&#213;', '&#214;', '&#215;', '&#216;', '&#217;', '&#218;', '&#219;', '&#220;', '&#221;', '&#222;', '&#223;', '&#224;', '&#225;', '&#226;', '&#227;', '&#228;', '&#229;', '&#230;', '&#231;', '&#232;', '&#233;', '&#234;', '&#235;', '&#236;', '&#237;', '&#238;', '&#239;', '&#240;', '&#241;', '&#242;', '&#243;', '&#244;', '&#245;', '&#246;', '&#247;', '&#248;', '&#249;', '&#250;', '&#251;', '&#252;', '&#253;', '&#254;', '&#255;', '&#34;', '&#38;', '&#60;', '&#62;', '&#338;', '&#339;', '&#352;', '&#353;', '&#376;', '&#710;', '&#732;', '&#8194;', '&#8195;', '&#8201;', '&#8204;', '&#8205;', '&#8206;', '&#8207;', '&#8211;', '&#8212;', '&#8216;', '&#8217;', '&#8218;', '&#8220;', '&#8221;', '&#8222;', '&#8224;', '&#8225;', '&#8240;', '&#8249;', '&#8250;', '&#8364;', '&#402;', '&#913;', '&#914;', '&#915;', '&#916;', '&#917;', '&#918;', '&#919;', '&#920;', '&#921;', '&#922;', '&#923;', '&#924;', '&#925;', '&#926;', '&#927;', '&#928;', '&#929;', '&#931;', '&#932;', '&#933;', '&#934;', '&#935;', '&#936;', '&#937;', '&#945;', '&#946;', '&#947;', '&#948;', '&#949;', '&#950;', '&#951;', '&#952;', '&#953;', '&#954;', '&#955;', '&#956;', '&#957;', '&#958;', '&#959;', '&#960;', '&#961;', '&#962;', '&#963;', '&#964;', '&#965;', '&#966;', '&#967;', '&#968;', '&#969;', '&#977;', '&#978;', '&#982;', '&#8226;', '&#8230;', '&#8242;', '&#8243;', '&#8254;', '&#8260;', '&#8472;', '&#8465;', '&#8476;', '&#8482;', '&#8501;', '&#8592;', '&#8593;', '&#8594;', '&#8595;', '&#8596;', '&#8629;', '&#8656;', '&#8657;', '&#8658;', '&#8659;', '&#8660;', '&#8704;', '&#8706;', '&#8707;', '&#8709;', '&#8711;', '&#8712;', '&#8713;', '&#8715;', '&#8719;', '&#8721;', '&#8722;', '&#8727;', '&#8730;', '&#8733;', '&#8734;', '&#8736;', '&#8743;', '&#8744;', '&#8745;', '&#8746;', '&#8747;', '&#8756;', '&#8764;', '&#8773;', '&#8776;', '&#8800;', '&#8801;', '&#8804;', '&#8805;', '&#8834;', '&#8835;', '&#8836;', '&#8838;', '&#8839;', '&#8853;', '&#8855;', '&#8869;', '&#8901;', '&#8968;', '&#8969;', '&#8970;', '&#8971;', '&#9001;', '&#9002;', '&#9674;', '&#9824;', '&#9827;', '&#9829;', '&#9830;');
    var arr2 = new Array('&nbsp;', '&iexcl;', '&cent;', '&pound;', '&curren;', '&yen;', '&brvbar;', '&sect;', '&uml;', '&copy;', '&ordf;', '&laquo;', '&not;', '&shy;', '&reg;', '&macr;', '&deg;', '&plusmn;', '&sup2;', '&sup3;', '&acute;', '&micro;', '&para;', '&middot;', '&cedil;', '&sup1;', '&ordm;', '&raquo;', '&frac14;', '&frac12;', '&frac34;', '&iquest;', '&agrave;', '&aacute;', '&acirc;', '&atilde;', '&Auml;', '&aring;', '&aelig;', '&ccedil;', '&egrave;', '&eacute;', '&ecirc;', '&euml;', '&igrave;', '&iacute;', '&icirc;', '&iuml;', '&eth;', '&ntilde;', '&ograve;', '&oacute;', '&ocirc;', '&otilde;', '&Ouml;', '&times;', '&oslash;', '&ugrave;', '&uacute;', '&ucirc;', '&Uuml;', '&yacute;', '&thorn;', '&szlig;', '&agrave;', '&aacute;', '&acirc;', '&atilde;', '&auml;', '&aring;', '&aelig;', '&ccedil;', '&egrave;', '&eacute;', '&ecirc;', '&euml;', '&igrave;', '&iacute;', '&icirc;', '&iuml;', '&eth;', '&ntilde;', '&ograve;', '&oacute;', '&ocirc;', '&otilde;', '&ouml;', '&divide;', '&oslash;', '&ugrave;', '&uacute;', '&ucirc;', '&uuml;', '&yacute;', '&thorn;', '&yuml;', '&quot;', '&amp;', '&lt;', '&gt;', '&oelig;', '&oelig;', '&scaron;', '&scaron;', '&yuml;', '&circ;', '&tilde;', '&ensp;', '&emsp;', '&thinsp;', '&zwnj;', '&zwj;', '&lrm;', '&rlm;', '&ndash;', '&mdash;', '&lsquo;', '&rsquo;', '&sbquo;', '&ldquo;', '&rdquo;', '&bdquo;', '&dagger;', '&dagger;', '&permil;', '&lsaquo;', '&rsaquo;', '&euro;', '&fnof;', '&alpha;', '&beta;', '&gamma;', '&delta;', '&epsilon;', '&zeta;', '&eta;', '&theta;', '&iota;', '&kappa;', '&lambda;', '&mu;', '&nu;', '&xi;', '&omicron;', '&pi;', '&rho;', '&sigma;', '&tau;', '&upsilon;', '&phi;', '&chi;', '&psi;', '&omega;', '&alpha;', '&beta;', '&gamma;', '&delta;', '&epsilon;', '&zeta;', '&eta;', '&theta;', '&iota;', '&kappa;', '&lambda;', '&mu;', '&nu;', '&xi;', '&omicron;', '&pi;', '&rho;', '&sigmaf;', '&sigma;', '&tau;', '&upsilon;', '&phi;', '&chi;', '&psi;', '&omega;', '&thetasym;', '&upsih;', '&piv;', '&bull;', '&hellip;', '&prime;', '&prime;', '&oline;', '&frasl;', '&weierp;', '&image;', '&real;', '&trade;', '&alefsym;', '&larr;', '&uarr;', '&rarr;', '&darr;', '&harr;', '&crarr;', '&larr;', '&uarr;', '&rarr;', '&darr;', '&harr;', '&forall;', '&part;', '&exist;', '&empty;', '&nabla;', '&isin;', '&notin;', '&ni;', '&prod;', '&sum;', '&minus;', '&lowast;', '&radic;', '&prop;', '&infin;', '&ang;', '&and;', '&or;', '&cap;', '&cup;', '&int;', '&there4;', '&sim;', '&cong;', '&asymp;', '&ne;', '&equiv;', '&le;', '&ge;', '&sub;', '&sup;', '&nsub;', '&sube;', '&supe;', '&oplus;', '&otimes;', '&perp;', '&sdot;', '&lceil;', '&rceil;', '&lfloor;', '&rfloor;', '&lang;', '&rang;', '&loz;', '&spades;', '&clubs;', '&hearts;', '&diams;');
    return swapArrayVals(s, arr1, arr2);
}

// Numerically encodes all unicode characters
function numEncode(s) {

    if (isEmpty(s)) return "";
    var e = "";
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (c < " " || c > "~") {
            c = "&#" + c.charCodeAt() + ";";
        }
        e += c;
    }
    return e;
};

// HTML Decode numerical and HTML entities back to original values
function htmlDecode(s) {

    var c, m, d = s;

    if (isEmpty(d)) return "";

    // convert HTML entites back to numerical entites first
    d = HTML2Numerical(d);

    // look for numerical entities &#34;
    arr = d.match(/&#[0-9]{1,5};/g);

    // if no matches found in string then skip
    if (arr != null) {
        for (var x = 0; x < arr.length; x++) {
            m = arr[x];
            c = m.substring(2, m.length - 1); //get numeric part which is refernce to unicode character
            // if its a valid number we can decode
            if (c >= -32768 && c <= 65535) {
                // decode every single match within string
                d = d.replace(m, String.fromCharCode(c));
            } else {
                d = d.replace(m, ""); //invalid so replace with nada
            }
        }
    }

    return d;
};

// encode an input string into either numerical or HTML entities
function htmlEncode(s, dbl) {

    if (isEmpty(s)) return "";

    // do we allow double encoding? E.g will &amp; be turned into &amp;amp;
    dbl = dbl | false; //default to prevent double encoding

    // if allowing double encoding we do ampersands first
    if (dbl) {
        if (EncodeType == "numerical") {
            s = s.replace(/&/g, "&#38;");
        } else {
            s = s.replace(/&/g, "&amp;");
        }
    }

    // convert the xss chars to numerical entities ' " < >
    s = XSSEncode(s, false);

    if (EncodeType == "numerical" || !dbl) {
        // Now call function that will convert any HTML entities to numerical codes
        s = HTML2Numerical(s);
    }

    // Now encode all chars above 127 e.g unicode
    s = numEncode(s);

    // now we know anything that needs to be encoded has been converted to numerical entities we
    // can encode any ampersands & that are not part of encoded entities
    // to handle the fact that I need to do a negative check and handle multiple ampersands &&&
    // I am going to use a placeholder

    // if we don't want double encoded entities we ignore the & in existing entities
    if (!dbl) {
        s = s.replace(/&#/g, "##AMPHASH##");

        if (EncodeType == "numerical") {
            s = s.replace(/&/g, "&#38;");
        } else {
            s = s.replace(/&/g, "&amp;");
        }

        s = s.replace(/##AMPHASH##/g, "&#");
    }

    // replace any malformed entities
    s = s.replace(/&#\d*([^\d;]|$)/g, "$1");

    if (!dbl) {
        // safety check to correct any double encoded &amp;
        s = correctEncoding(s);
    }

    // now do we need to convert our numerical encoded string into entities
    if (EncodeType == "entity") {
        s = NumericalToHTML(s);
    }

    return s;
};

// Encodes the basic 4 characters used to malform HTML in XSS hacks
function XSSEncode(s, en) {
    if (!isEmpty(s)) {
        en = en || true;
        // do we convert to numerical or html entity?
        if (en) {
            s = s.replace(/\'/g, "&#39;"); //no HTML equivalent as &apos is not cross browser supported
            s = s.replace(/\"/g, "&quot;");
            s = s.replace(/</g, "&lt;");
            s = s.replace(/>/g, "&gt;");
        } else {
            s = s.replace(/\'/g, "&#39;"); //no HTML equivalent as &apos is not cross browser supported
            s = s.replace(/\"/g, "&#34;");
            s = s.replace(/</g, "&#60;");
            s = s.replace(/>/g, "&#62;");
        }
        return s;
    } else {
        return "";
    }
}

// returns true if a string contains html or numerical encoded entities
function hasEncoded(s) {
    if (/&#[0-9]{1,5};/g.test(s)) {
        return true;
    } else if (/&[A-Z]{2,6};/gi.test(s)) {
        return true;
    } else {
        return false;
    }
}

// will remove any unicode characters
function stripUnicode(s) {
    return s.replace(/[^\x20-\x7E]/g, "");

};

// corrects any double encoded &amp; entities e.g &amp;amp;
function correctEncoding(s) {
    return s.replace(/(&amp;)(amp;)+/, "$1");
};


// Function to loop through an array swaping each item with the value from another array e.g swap HTML entities with Numericals
function swapArrayVals(s, arr1, arr2) {
    if (isEmpty(s)) return "";
    var re;
    if (arr1 && arr2) {
        //ShowDebug("in swapArrayVals arr1.length = " + arr1.length + " arr2.length = " + arr2.length)
        // array lengths must match
        if (arr1.length == arr2.length) {
            for (var x = 0, i = arr1.length; x < i; x++) {
                re = new RegExp(arr1[x], 'g');
                s = s.replace(re, arr2[x]); //swap arr1 item with matching item from arr2	
            }
        }
    }
    return s;
};

function inArray(item, arr) {
    for (var i = 0, x = arr.length; i < x; i++) {
        if (arr[i] === item) {
            return i;
        }
    }
    return -1;
}

/// added by bhavik shah

function showPleaseWait(listGridId) {
    var oGrid = bb.document.getElementById(listGridId);
    if (oGrid != null) {
        var recordIndexes = bb.getProperty(oGrid, 'indexes');
        var indexCnt = 0;

        if (recordIndexes != null) {
            indexCnt = recordIndexes.length;
        }
        if (indexCnt <= 0) {
            bb.document.getElementById('lmPleaseWait').show();
        }
    }
}

function showPleaseWaitEmptyGrid(listGridId) {
    var oGrid = bb.document.getElementById(listGridId);
    if (oGrid != null) {
        var recordIndexes = bb.getProperty(oGrid, 'indexes');
        var indexCnt = 0;

        if (recordIndexes != null) {
            indexCnt = recordIndexes.length;
        }
        if (indexCnt < 0) {
            bb.document.getElementById('lmPleaseWait').show();
        }
    }
}

function Comm_UpdateGridFromDS(gridId,colName, colValue) {
    var grid = bb.document.getElementById(gridId);
    var arRecordIds = common_SelectedIndexes;
    if(grid){
        var dataSource = grid.getDataSource();
        for (var cnt=0;cnt<arRecordIds.length;cnt++) {
            btl.dataSource.setValue(dataSource, arRecordIds[cnt], colName, colValue);
        }
        btl.dataSource.actionApply(dataSource, 'update' / 'delete', arRecordIds, grid);
        dataSource.updateObservers("update", arRecordIds);
    }
}



function Comm_DeleteRowsFromGrid(gridId) {
    var grid = bb.document.getElementById(gridId);
    var arRecordIds = common_SelectedIndexes;
    if (grid) {
        var dataSource = grid.getDataSource();
        btl.dataSource.actionApply(dataSource, 'update' / 'delete', arRecordIds, grid);
        dataSource.updateObservers("delete", arRecordIds);
    }
}

function Comm_SelectedRecordIdsFromGrid(gridId) {
    var grid = bb.document.getElementById(gridId);
    if (grid) {
        var oDatasource = grid.getDataSource();
        var recordIndexes = grid.getProperty('indexes');
        var inputs = grid.selectNodes("view()/div[3]/table/tbody/tr/td[1]/div/input") //This will return all the checkbox in array
        common_SelectedIndexes = [];
        for (var iCount = 0; iCount < inputs.length; iCount++) {
            if (inputs[iCount].checked) {
                common_SelectedIndexes.push(btl.dataSource.getValue(oDatasource, recordIndexes[iCount], 'RowNumber'));
            }
        }
        return common_SelectedIndexes;
    }
    return null;
};

function EditCustomerProfile() {
    window.open('Basic/CRM/AddEditProfile.aspx?isStandard=1&CompanyId=' + gdCustCompanyId, '', 'resizable=yes,width=1000,height=600,toolbar=no,menubar=no,status=yes,scrollbars=yes')
}

function Comm_EditCustomerProfileFromAdmin(custCompId,custUserId,custContactId,isOnlyForUser,isForContact,companyType) {
    window.open('CRM/AddEditProfile.aspx?CompanyId=' + custCompId + '&CompType=' + companyType,'', 'resizable=yes,width=1000,height=600,toolbar=no,menubar=no,status=yes,scrollbars=yes')
}
