﻿// JScript File


// var _callQueue = new Array();
//        var _executingElement = null;

//       Sys.Application.add_load(ApplicationLoadHandler)
//            function ApplicationLoadHandler(sender, args)
//            {
//                if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
//                {
//                  Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequest);
//                }
//            }
//            
//          var prm = Sys.WebForms.PageRequestManager.getInstance();
//            var lastPostBackElement;
//                function InitializeRequest(sender, args)
//                { 
//                    var postBackElement = args.get_postBackElement();
//                    
////                    if (!prm.get_isInAsyncPostBack()) 
////                    {
////                       if (_executingElement !== args.get_postBackElement().id)
////                                {
////                                  //alert("abort");
////                                  prm.abortPostBack();
////                                }
////                    }
//                     if (prm.get_isInAsyncPostBack())
//                    {
//                        if (_executingElement !== args.get_postBackElement().id)
//                                {
//                                // Grab the event argument value
//                                var evArg = $get("__EVENTARGUMENT").value;

//                                //Does not match which means it is another control
//                                //which request the update, so cancel it temporary and 
//                                //add it in the call queue
//                                args.set_cancel(true);
//                                Array.enqueue(_callQueue, new Array(postBackElement, evArg));

//                                  //alert("abort");
//                                  //prm.abortPostBack();
//                                }    
//                    }
//                    //lastPostBackElement = args.get_postBackElement().id; 
//                  
//                }

//                
//        
//        
//        
//        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, args) 
//        { 
//               
//               //Check if we have a pending call
//                if (_callQueue.length > 0)
//                {
//                //Get the first item from the call queue and setting it
//                //as current executing item
//                _executingElement = Array.dequeue(_callQueue);

//                var _element = _executingElement[0];
//                var _eventArg = _executingElement[1];

//                //Now Post the from which will also fire the initializeRequest
//                prm._doPostBack(_element.id, _eventArg);

//               }
//                
//            if (args.get_error() && args.get_response().get_timedOut()) 
//            { 
//                   
//                    // remember to set errorHandled = true to keep from getting a popup from the AJAX library itself 
//                args.set_errorHandled(true); 
//            } 
//            // parser error handler
//            if (args.get_error() && args.get_error().name === 'Sys.WebForms.PageRequestManagerParserErrorException') 
//            {
//                args.set_errorHandled(true); 
//            } 

//            }
//        )



var GlobalCallQueue = {
        _callQueue : [],    // Maintains the list of webmethods to call
        _callInProgress : 0,    // Number of calls currently in progress by browser
        _maxConcurrentCall : 2, // Max number of calls to execute at a time
        _delayBetweenCalls : 50, // Delay between execution of calls 
        call : function(servicePath, methodName, useGet, 
            params, onSuccess, onFailure, userContext, timeout)
        {
            var queuedCall = new QueuedCall(servicePath, methodName, useGet, 
                params, onSuccess, onFailure, userContext, timeout);
                
            Array.add(GlobalCallQueue._callQueue,queuedCall);
            GlobalCallQueue.run();
        },
        run : function()
        {
            /// Execute a call from the call queue
            
            if( 0 == GlobalCallQueue._callQueue.length ) return;
            if( GlobalCallQueue._callInProgress < GlobalCallQueue._maxConcurrentCall )
            {
                GlobalCallQueue._callInProgress ++;
                // Get the first call queued
                var queuedCall = GlobalCallQueue._callQueue[0];
                Array.removeAt( GlobalCallQueue._callQueue, 0 );
                
                // Call the web method
                queuedCall.execute();
            }    
            else
            {
                // cannot run another call. Maximum concurrent 
                // webservice method call in progress
            }            
        },
        callComplete : function()
        {
            GlobalCallQueue._callInProgress --;
            GlobalCallQueue.run();
        }
    };
    
    QueuedCall = function( servicePath, methodName, useGet, params, 
        onSuccess, onFailure, userContext, timeout )
    {
        this._servicePath = servicePath;
        this._methodName = methodName;
        this._useGet = useGet;
        this._params = params;
        
        this._onSuccess = onSuccess;
        this._onFailure = onFailure;
        this._userContext = userContext;
        this._timeout = timeout;
    }
    
    QueuedCall.prototype = 
    {
        execute : function()
        {
            Sys.Net.WebServiceProxy.original_invoke( 
                this._servicePath, this._methodName, this._useGet, this._params,  
                Function.createDelegate(this, this.onSuccess), // Handle call complete
                Function.createDelegate(this, this.onFailure), // Handle call complete
                this._userContext, this._timeout );
        },
        onSuccess : function(result, userContext, methodName)
        {
            this._onSuccess(result, userContext, methodName);
            GlobalCallQueue.callComplete();            
        },        
        onFailure : function(result, userContext, methodName)
        {
            this._onFailure(result, userContext, methodName);
            GlobalCallQueue.callComplete();            
        }        
    };
    
    function enableQueuedCall()
    {
        
        
        Sys.Net.WebServiceProxy.original_invoke = Sys.Net.WebServiceProxy.invoke;
        Sys.Net.WebServiceProxy.invoke = 
            function Sys$Net$WebServiceProxy$invoke(servicePath, methodName, 
                useGet, params, onSuccess, onFailure, userContext, timeout)
        {   
            GlobalCallQueue.call(servicePath, methodName, useGet, params, 
                onSuccess, onFailure, userContext, timeout);
        }
    }    
    
    if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
    
    
    function CheckCallbackOnFocus(id, type)
    {
        if(type == 'name')
        {
            if(id.value == callbackName)
                id.value = '';
        }
        else if(type == 'phone')
        {
            if(id.value == callbackPhone)
                id.value = '';
        }
    }
    
    function CheckCallbackOnBlur(id, type)
    {
        if(type == 'name')
        {
            if(id.value == '')
                id.value = callbackName;
        }
        else if(type == 'phone')
        {
            if(id.value == '')
                id.value = callbackPhone;
        }
    }
    
    
    
    
     function isCountryCode(e) {

        var charCode;
        if (!e) var e = window.event;
        if (e.keyCode) charCode = e.keyCode;
        else if (e.which) charCode = e.which;


        //var charCode = (evt.which) ? evt.which : event.keyCode;
        if (charCode == 45)
            return true;
            
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }


    //This function allows only number.[decimal not allowed]
    function isNumberKey(e) {

        var charCode;
        if (!e) var e = window.event;
        if (e.keyCode) charCode = e.keyCode;
        else if (e.which) charCode = e.which;


        //var charCode = (evt.which) ? evt.which : event.keyCode;

        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }
    
    
    function ShowHideTelephoneDiv(varType) {
        

        var objcountrycode = document.getElementById(cmbCountry_Step1);

        //objcountrycode.selectedIndex = 0;
//        if (varType == 'Telephone') {
//            if (document.getElementById(hdnDefaultCountry1).value.length > 0) {
//                objcountrycode.selectedIndex = document.getElementById(hdnDefaultCountry1).value;
//            }
//        }
//        else {
//            objcountrycode.selectedIndex = 0;
//        }
        var objTelephoneDiv = document.getElementById('divTelephone');

        var countrycode;
        var area;
        var phnumber;

        var objarea = document.getElementById(txtAreaCode_Step1);
        var objphnumber = document.getElementById(txtNumber_Step1);
        var countrycode = objcountrycode.options[objcountrycode.selectedIndex].value
        var countryText = objcountrycode.options[objcountrycode.selectedIndex].text;


        if (varType == 'Telephone') {
//            var objTcountry = document.getElementById(hdnTelephoneCountry_Step1);
//            var objTArea = document.getElementById(hdnTelephoneAreaCode_Step1);
//            var objTnumber = document.getElementById(hdnTelephoneNumber_Step1);
            var objTcountry = document.getElementById(txtCountryCode);
            var objTArea = document.getElementById(txtAreaCode);
            var objTnumber = document.getElementById(txtPhNumber);
            if (objTcountry.value.length > 0) {
                //objcountrycode.selectedIndex = objTcountry.value;
                objcountrycode.value = objTcountry.value;
            }
            if (objTArea.value.length > 0) {
                objarea.value = objTArea.value;
            }
            else {
                objarea.value = '';
            }
            if (objTnumber.value.length > 0) {
                objphnumber.value = objTnumber.value;
                //Page_ClientValidate(document.getElementById('<%= cstvTelephone.ClientID %>'));
                //ValidatorEnable(document.getElementById(cstvTelephone_Step1), false);                
            }
            else {
                objphnumber.value = '';
            }
        }        

        if (objTelephoneDiv != null) {
            if (objTelephoneDiv.style.display == 'block') {
                objTelephoneDiv.style.display = 'none';
                ShowHideMasterDiv();

            }
            else {
                objTelephoneDiv.style.position = "absolute";
                objTelephoneDiv.style.display = 'block';
                objTelephoneDiv.style.top = '460px'; //'360px';
                //objTelephoneDiv.style.left = '292px';
                objTelephoneDiv.style.left = '10px';
                objTelephoneDiv.style.width = '550px';
                objTelephoneDiv.style.height = '140px';
            }
        }
    }
    
    
     function ValidatePhoneDiv_Step1() 
     {
            var varType;
            varType = 'Telephone';
            var isPhoneValid = false;
            var SetPhone = false;
            var objcountrycode = document.getElementById(cmbCountry_Step1);
            var objarea = document.getElementById(txtAreaCode_Step1);
            var objphnumber = document.getElementById(txtNumber_Step1);

            if (objcountrycode.selectedIndex > 0 && objarea.value.length > 0 && objphnumber.value.length > 0) {
                isPhoneValid = true;
                SetPhone = true;
            }
            else 
            {
                document.getElementById(lblInvalidNumber_Step1).style.display = 'block';
            }

            if (SetPhone == true) 
            {
                document.getElementById(lblInvalidNumber_Step1).style.display = 'none';
                SetCompanyTelephone(varType);
                ShowHideTelephoneDiv(varType);
            }
     }
    
    
    function SetCompanyTelephone(varType1)
    {
    
        var countrycode;
        var area;
        var phnumber;

        area = document.getElementById(txtAreaCode_Step1).value;
        phnumber = document.getElementById(txtNumber_Step1).value;
        var objcountrycode = document.getElementById(cmbCountry_Step1);

        var countrycode = objcountrycode.options[objcountrycode.selectedIndex].value
        var countryIndex = objcountrycode.selectedIndex;
        var countryText = objcountrycode.options[objcountrycode.selectedIndex].text;
       
        document.getElementById(txtAreaCode).value = area;
        document.getElementById(txtPhNumber).value = phnumber;
        document.getElementById(txtCountryCode).value = countrycode;

        return false;
     }
    
    
   function CloseDiv() 
   {
     document.getElementById('divTelephone').style.display = 'none';
   }
    
    
    
    
    
//-----------------------MasterDiv  Functions-----------------------------------------
function ShowHideMasterDiv()
{
  
    var objDiv= document.getElementById(MasterDivGray);
    if(objDiv != null)
    {
        if(objDiv.style.visibility=='visible')
        {
             objDiv.style.visibility = 'hidden';
        }
       else
       {
           objDiv.style.visibility ='visible';
       }
        SetMasterDivPosition(objDiv);
    }
    
}


function SetMasterDivPosition(objDiv)
{
    //Dhara:added for 
   if (window.innerHeight > 0 && window.scrollMaxY <= 0)
    { //firexox
       
         yWithScroll = window.innerHeight + "px"
         xWithScroll = window.innerWidth + "px";
        
    } 
    else if(window.scrollMaxY > 0)
    { //firexox
       
        yWithScroll = parseInt(window.innerHeight) + parseInt(window.scrollMaxY);
        yWithScroll = yWithScroll + "px";
        xWithScroll = window.innerWidth + "px";
    }
    else if (document.body.scrollHeight > document.documentElement.clientHeight)
    { // all but Explorer Mac  
       
        yWithScroll = document.body.scrollHeight;         
        xWithScroll = document.body.scrollWidth;   
        
    } 
    else 
    { // works in Explorer 6 Strict, Mozilla (not FF) and Safari     
        //alert(document.body.scrollHeight-document.documentElement.clientHeight);
          yWithScroll =  document.documentElement.clientHeight;
           xWithScroll = document.documentElement.clientWidth;     
         
    } 
    //alert(yWithScroll);   
   // alert(yWithScroll);
   // alert(xWithScroll);
    objDiv.style.position="absolute";
    objDiv.style.top="0px";
    objDiv.style.left="0px"; 
    objDiv.style.width=xWithScroll;
    objDiv.style.height=yWithScroll; 
    objDiv.style.overflow="hidden";
}

function HideClickedControl(ctrlID)
{
   var isPageValid = Page_ClientValidate();
    if(!isPageValid)
        return false;
        
  setTimeout("HideControl('" + ctrlID.id + "');", 0);  
}

function HideControl(ctrlID)
{
    ctrlID = document.getElementById(ctrlID);
    ctrlID.disabled = true;
}