var strBrowserType;
var blnZero = false;
blnIsBrowserCompatible();
var strGroup;

function subTest()
{
   var strURL = "www.yahoo.com";
   window.open(strURL, "SeatLayout", "toolbar=no,width=840,height=558,scrollbars=yes,status=yes,menubar=no,location=no");   
}

function subGetDataAndExecute(strScriptId, strURLData, strCallBackCode, blnStamp)
{
    try
    {
        var objHead = document.getElementsByTagName("head").item(0);
        var objScript = document.getElementById(strScriptId);
        if(objScript) {
            objHead.removeChild(objScript);
        }
        if(blnStamp == true) {
            strURLData += strTimeStamp(); 
        }
        objScript = document.createElement("script");
        objScript.setAttribute("src", strURLData);
        objScript.setAttribute("id", strScriptId);
        if(strCallBackCode.length > 0)
        {
            switch(strBrowserType) {
                case "ie":
                    objScript.onreadystatechange = function() {
                        if(this.readyState == "complete" || this.readyState == "loaded")
                        {
                            subExecuteCommand(strCallBackCode, 0);
                        }
                    }
                    break;
                case "ff":
                    objScript.onload = function() {
                        subExecuteCommand(strCallBackCode, 0);
                    }
                    break;
                case "cr":
                    objScript.onload = function() {
                        subExecuteCommand(strCallBackCode, 0);
                    }
                    break;
                default:
                    alert('ERROR:: Unknown browser detected !!!');
                    break;
            }
        }
        objHead.appendChild(objScript);
        
    } catch(e) {
        subDisplayError("MiscCode.js", "subGetDataAndExecute('" + strScriptId + "', '" + strURLData + "', '" + strCallBackCode + "')", e);
    }
}

function subCheckEmployeeDiscount()
{
    var strEmpCode = document.getElementById("txtEmpCode").value;
    var transId = strGetCookie("lngTransId", "0");
    
    if(strEmpCode.trim() == "")
    {
        alert("Please enter a valid Employee code to check.");
        return;
    }
    callAjaxEx("jxDataSecure.aspx?dt=CEMD&cemd=" + strEmpCode + "&tid=" + transId, "subEvaluateCheck");
}

function subEvaluateCheck(strReturn)
{
    if(!strReturn.trim() == "")
    {
        alert(strReturn);
    }
    else
    {
        alert("There is a technical error at the server.\nThe server is unable to process your request.\nWe regret for Inconvenience occurred to you.");
    }
    if(!strReturn.startsWith("Congratulations!"))
        document.getElementById("txtEmpCode").value = "";
}

String.prototype.trim = function () {return this.replace(/^\s*/, "").replace(/\s*$/, "");}

String.prototype.startsWith = function (strStart) { return (this.match("^"+strStart)==strStart);}

String.prototype.endsWith = function(strEnd) {return (this.match(strEnd+"$")==strEnd)}


function subGetDataAndExecuteEx(strScriptId, strURLData, strCallBackCode, blnStamp) {
    try {
        var objHead = document.getElementsByTagName("head").item(0);
        var objScript = document.getElementById(strScriptId);
        if (objScript) {
            objHead.removeChild(objScript);
        }
        if (blnStamp == true) {
            strURLData += strTimeStamp();
        }
        objScript = document.createElement("script");
        objScript.setAttribute("src", strURLData);
        objScript.setAttribute("id", strScriptId);
        if (strCallBackCode.length > 0) {
            switch (strBrowserType) {
                case "ie":
                    objScript.onreadystatechange = function() {
                        if (this.readyState == "complete" || this.readyState == "loaded") {
                            subExecuteCommand(strCallBackCode, 0);
                        }
                    }
                    break;
                case "ff":
                    objScript.onload = function() {
                        subExecuteCommand(strCallBackCode, 0);
                    }
                    break;
                case "cr":
                    objScript.onload = function() {
                        subExecuteCommand(strCallBackCode, 0);
                    }
                    break;
                default:
                    alert('ERROR:: Unknown browser detected !!!');
                    break;
            }
        }
        objHead.appendChild(objScript);

    } catch (e) {
        subDisplayError("MiscCode.js", "subGetDataAndExecute('" + strScriptId + "', '" + strURLData + "', '" + strCallBackCode + "')", e);
    }
}

function blnIsBrowserCompatible()
{
    try
    {
        var arrBrowsers = new Array( new Array("MSIE", "ie"), new Array("FireFox", "ff"), new Array("Opera", "ff"), new Array("Chrome", "ff"));
        var strAgent = navigator.userAgent.toLowerCase();
        for(var intCount = 0; intCount < arrBrowsers.length; intCount++)
        {
            if(strAgent.indexOf(arrBrowsers[intCount][0].toLowerCase()) > -1)
            {
                strBrowserType = arrBrowsers[intCount][1].toLowerCase();
                return true;
            }
        }
    } catch(e) {
        subDisplayError("MiscCode.js", "blnIsBrowserCompatible()", e);
    }
    return false;
}

function subDisplayError(strFileName, strFunctionName, objError)
{
    alert("File: " + strFileName + "\nFunction: " + strFunctionName + "\nError: " + objError.message);
    //alert(strFunctionName);
}

function subAddToCombo(objCombo, strText, strValue)
{
    try {
        var strComboId = objCombo.id;
        var strOptionId = strComboId + "_opt_" + strValue;
        var objOption = document.getElementById(strOptionId);
        if(!objOption)
        {
            objOption = document.createElement("option");
            objOption.setAttribute("id", (strComboId + "_opt_" + strValue));
            objOption.setAttribute("value", strValue);
			objOption.setAttribute("title", strText);
            objOption.innerHTML = strText;
            objCombo.appendChild(objOption);
            
        }
    } catch(e) {
        subDisplayError("MiscCode.js", "subAddToCombo('" + objCombo.id + "', '" + strText + "', '" + strValue + "', '" + strGroup + "')", e);
    }
}

function subAddToComboForCinema(objCombo, strText, strValue)
{
    try {
    
        var strComboId = objCombo.id;
        var strOptionId = strComboId + "_opt_" + strValue;
        var objOption = document.getElementById(strOptionId);
        if(!objOption)
        {
            objOption = document.createElement("option");
            objOption.setAttribute("id", (strComboId + "_opt_" + strValue));
            objOption.setAttribute("value", strValue);
            objOption.setAttribute("title", strText);
            objOption.innerHTML = strText;
            objCombo.appendChild(objOption);
            if((strValue.length)< 2)
            {
                objOption.style="color:DeepPink; font-weight:bold;"
            }
            
        }
    } 
    catch(e) {
        if(!objOption)
            subDisplayError("MiscCode.js", "subAddToComboForCinema('" + objCombo.id + "', '" + strText + "', '" + strValue + "')", e);
    }
}
function subExecuteCommand(strCommand, intTimeOut)
{
    try
    {
        if(strBrowserType == "ie") {
            if(document.readyState == "complete") {
                if(intTimeOut == 0) {
                    intTimeOut = 500;
                }
                strCommand = "try{" + strCommand + ";}catch(e){alert('Error: '+e.message);};";
                window.setTimeout(strCommand, intTimeOut);
            } else {
                var strCode = "subExecuteCommand(\"" + strCommand + "\", 0);";
                window.setTimeout(strCode, 1000);
            }
        } else {
            strCommand = "try{" + strCommand + ";}catch(e){alert('Error: '+e.message);};";
            window.setTimeout(strCommand, intTimeOut);
        }
    } catch(e) {
        subDisplayError("MiscCode.js", "subExecuteCommand('" + strCommand + "', " + intTimeOut + ")", e);
    }
}

function strTimeStamp()
{
    try {
        var dtmDate = new Date();
        return "&stamp=" + dtmDate.toString();
    } catch(e) {
        subDisplayError("MiscCode.js", "strTimeStamp()", e);
    }
}

function subCheckMode()
{
        try
        {
//            if (strGetCookie("strMode", "") == "DEBUG")
//            {
//                document.getElementById("divError").innerHTML += "DEBUG MODE<BR>";
//                document.getElementById("divError").style.display = "block";
//            }
        }
        catch (e)
        {
            subDisplayError("MiscCode.js", "subCheckMode()", e);
        }
 }
   
