﻿// JScript File
// ***** AJAX UTILS ****
// must be reference after <AjaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />


// *********** Disable initiator control of postback *************

// Array of controls (client) IDs to disable on postback
// only the initiator will be disabled
controlsToDisableOnPostback = new Array();    

// Get a reference to the PageRequestManager.
if(Sys.WebForms != null)
{
    var prm = Sys.WebForms.PageRequestManager.getInstance();
     
    prm.add_initializeRequest(InitializeRequest);  
}

function AddControlToDisableOnPostback(controlID)
{
    controlsToDisableOnPostback.push(controlID);
}
 
// Executed anytime an async postback occurs.
function InitializeRequest(sender, args) 
{        
    if (controlsToDisableOnPostback.length > 0)
    {        
        // get element which caused the post back
        var elm = $get(args._postBackElement.id);
        
        // check if it is one of the registered controls
        var isFound = false;
        for (var i = 0; i < controlsToDisableOnPostback.length; i++)
        {            
            if (elm.id == controlsToDisableOnPostback[i]) 
            {
                isFound = true;
                break;
            }
        }
    
        if (isFound)
        {
            // found - disable all the registered controls
            for (var i = 0; i < controlsToDisableOnPostback.length; i++)
            {
                var elm = $get(controlsToDisableOnPostback[i]);
                if (elm != null) 
                {   
                    elm.disabled = true; 
                    if (elm.href != 'undefined')   
                    {
                        // for linkbutton
                        elm.href = 'javascript:nothing()';                    
                    }
                }
            }
        }
    }       
    
    // use this code in order to disable only the control which caused the post back
    //if (controlsToDisableOnPostback.length > 0)
    //{
    //    var elm = $get(args._postBackElement.id);
    //    for (var i = 0; i < controlsToDisableOnPostback.length; i++)
    //    {          
    //        if (elm.id == controlsToDisableOnPostback[i])
    //        {
    //            elm.disabled = true;
    //            break;
    //        }
    //    }
    //}
}   
function nothing() {}
// **************************************************************



        function AmountChange(min, max, amountCID, lastGoodAmountCID, isFloat, delegateJSFunc, fractionChar)
        {                    
            var amountElm = $get(amountCID);            
            var newVal = amountElm.value;
            var isChanged = false;
            newVal = newVal.replace(',','.');
            if(isNaN(newVal))
            {
                // return price to old good price
                amountElm.value = $get(lastGoodAmountCID).value;
            }            
            else if (newVal < min)
            {
                amountElm.value = ConvertFormattedNumberToString(min, isFloat);
                isChanged = true;
            }
            else if (newVal > max)
            {
                amountElm.value = ConvertFormattedNumberToString(max, isFloat);
                isChanged = true;
            }
            else
            {                        
                amountElm.value = ConvertFormattedNumberToString(ConvertStringToFormattedNumber(newVal, isFloat),isFloat);                                
                isChanged = true;                                
            }   
            if (isChanged)
            {
                var val = amountElm.value;
                amountElm.value = amountElm.value.replace('.', fractionChar);
                $get(lastGoodAmountCID).value = amountElm.value;                                
                ChangeAmountStatus(delegateJSFunc, val);                
            }
        }        
        // addAmount can be negative
        function ClickAdd(min, max, addAmount, amountCID, lastGoodAmountCID, isFloat, delegateJSFunc)
        {
           var oldVal = ConvertStringToFormattedNumber($get(amountCID).value);
                      
           if (!isNaN(oldVal))
           {
               var numOfDigits = Math.floor(Math.log(oldVal)/Math.log(10)+0.00001);
               var a = numOfDigits - 1; 
               if (a < 0) a = 0;
               var b = Math.pow(10, a);
               addAmount = addAmount * b;                              
           
               var newVal = oldVal + addAmount;               
               newVal = Math.round(newVal / b) * b; // round it
               if(newVal >= max)
               {
                    newVal = max;                
               }
               else if(newVal <= min)
               {
                    newVal = min;                
               }
                         
               var newValStr =  ConvertFormattedNumberToString(newVal,isFloat);
               $get(amountCID).value = newValStr;
               $get(lastGoodAmountCID).value = newValStr;
               ChangeAmountStatus(delegateJSFunc, newValStr);                     
           }
           else           
           {
               var lastGoodAmount = $get(lastGoodAmountCID).value;
               $get(amountCID).value = lastGoodAmount;
               ChangeAmountStatus(delegateJSFunc, lastGoodAmount); 
           }           
        }
        // Call delegate function
        function ChangeAmountStatus(delegateJSFunc, newAmount)
        {            
            if(delegateJSFunc != null && delegateJSFunc != '')
            {
                eval(delegateJSFunc + '(' + newAmount +')');
            }
        }
        
        function ConvertStringToFormattedNumber(val, isFloat)
        {
           if(isFloat)
           {
                return parseFloat(val);
           }
           else
           {
                return parseInt(val);
           }
        }
        
        function ConvertFormattedNumberToString(val, isFloat)
        {
           if(isFloat)
           {
                return val.toFixed(2);
           }
           else
           {
                return val.toFixed(0);
           }
        }
        
// ************************************************************** 
