Sys.Browser.WebKit = {}; //Safari 3 is considered WebKit
if( navigator.userAgent.indexOf( 'WebKit/' ) > -1 )
{
  Sys.Browser.agent = Sys.Browser.WebKit;
  Sys.Browser.version = parseFloat( navigator.userAgent.match(/WebKit\/(\d+(\.\d+)?)/)[1]);
  Sys.Browser.name = 'WebKit';
}
//Fix Safari Delete bug
function fixWebKitForMaskedEdits()
{
  if ((AjaxControlToolkit) && (AjaxControlToolkit.MaskedEditBehavior))
  {
    AjaxControlToolkit.MaskedEditBehavior.prototype._ExecuteNav = function(evt,scanCode)
    {
        if (evt.type == "keydown")
        {
            if (Sys.Browser.agent == Sys.Browser.InternetExplorer) 
            {
                // ctrl v 
                if ( (scanCode == 86 || scanCode == 118) && !evt.shiftKey && evt.ctrlKey && !evt.altKey) {
                    this._SetCancelEvent(evt);
                    this._PasteFromClipBoard();
                    return;
                }
                //Shift Ins 
                if (evt.shiftKey && !evt.ctrlKey && !evt.altKey && evt.keyCode == 45) {
                    this._SetCancelEvent(evt);
                    this._PasteFromClipBoard();
                    return;
                }
            }
        }
        if (Sys.Browser.agent != Sys.Browser.InternetExplorer || evt.type == "keypress") 
        {
            //Shift Ins 
            if (evt.rawEvent.shiftKey && !evt.rawEvent.ctrlKey && !evt.rawEvent.altKey && evt.rawEvent.keyCode == 45) {
                //at opera assume Ins = "-" not execute Shift-Ins
                this._SetCancelEvent(evt);
                this._PasteFromClipBoard();
                return;
            }
            // ctrl v 
            if (evt.type == "keypress" && (scanCode == 86 || scanCode == 118) && !evt.shiftKey && evt.ctrlKey && !evt.altKey) {
                this._SetCancelEvent(evt);
                this._PasteFromClipBoard();
                return;
            }
        }
        if (Sys.Browser.agent == Sys.Browser.InternetExplorer || Sys.Browser.agent == Sys.Browser.WebKit || evt.type == "keypress")
        {
            if (scanCode == 8) // BackSpace
            {
                this._SetCancelEvent(evt);
                curpos = this._deleteTextSelection();
                if (curpos != -1)
                {
                    this.setSelectionRange(curpos,curpos);
                }
                else
                {
                    curpos = this._getCurrentPosition();
                    this._backspace(curpos);
                    
                    curpos = this._getPreviousPosition(curpos-1);
                    this.setSelectionRange(curpos,curpos);
                }
                var wrapper = AjaxControlToolkit.TextBoxWrapper.get_Wrapper(this.get_element());
                if (this._MessageValidatorTip && wrapper.get_Value() == this._EmptyMask)
                {
                    this.ShowTooltipMessage(true);
                }
                // Opera not perform cancel event. Re-perform at Timer
                if (Sys.Browser.agent == Sys.Browser.Opera)
                {
                    this._SaveText = wrapper.get_Value(); 
                    this._SavePosi = curpos;
                    this._timer.set_enabled(false);
                    this._timer.set_interval(1);
                    this._timer.set_enabled(true);
                }
            }
            else if (scanCode == 46 || scanCode == 127) // delete
            {
                this._SetCancelEvent(evt);
                var wrapper = AjaxControlToolkit.TextBoxWrapper.get_Wrapper(this.get_element());
                curpos = this._deleteTextSelection();
                if (curpos == -1)
                {
                    curpos = this._getCurrentPosition();
                    if (!this._isValidMaskedEditPosition(curpos))
                    {
                        if (curpos != this._LogicLastInt && this._InputDirection != AjaxControlToolkit.MaskedEditInputDirections.RightToLeft)
                        {
                            curpos = this._getNextPosition(curpos);
                        }
                    }
                    this._deleteAtPosition(curpos,false);
                }
                else
                {
                    if (this._InputDirection == AjaxControlToolkit.MaskedEditInputDirections.RightToLeft)
                    {
                        ClearText = this._getClearMask(wrapper.get_Value());
                        if (ClearText != "")
                        {
                            ClearText = ClearText.replace(new RegExp("(\\" + this.get_CultureThousandsPlaceholder() + ")", "g"), "") + '';
                            if (ClearText.substring(ClearText.length-1,ClearText.length) == this.get_CultureDecimalPlaceholder())
                            {
                                ClearText = ClearText.substring(0,ClearText.length-1);
                                this.loadValue(ClearText,this._LogicLastInt);
                            }
                            else
                            {
                                this.loadValue(ClearText,this._LogicLastPos);
                            }
                        }
                    }
                }
                this.setSelectionRange(curpos,curpos);
                if (this._MessageValidatorTip && wrapper.get_Value() == this._EmptyMask)
                {
                    this.ShowTooltipMessage(true);
                }
                if (Sys.Browser.agent == Sys.Browser.Opera)
                {
                    this._SaveText = wrapper.get_Value(); 
                    this._SavePosi = curpos;
                    this._timer.set_enabled(false);
                    this._timer.set_interval(1);
                    this._timer.set_enabled(true);
                }
            }
            else if(evt.ctrlKey)
            {
                if (scanCode == 39 || scanCode == 35 || scanCode == 34) //Right or END or pgdown
                {
                    this._DirectSelText = "R";
                    if (Sys.Browser.agent == Sys.Browser.Opera)
                    {
                        return;
                    }
                    this._SetCancelEvent(evt);
                    curpos = this._getCurrentPosition();
                    this.setSelectionRange(curpos,this._LogicLastPos+1);
                }
                else if (scanCode == 37  || scanCode == 36 || scanCode == 33) //Left or Home or pgup
                {
                    this._DirectSelText = "L";
                    if (Sys.Browser.agent == Sys.Browser.Opera)
                    {
                        return;
                    }
                    this._SetCancelEvent(evt);
                    curpos = this._getCurrentPosition();
                    this.setSelectionRange(this._LogicFirstPos,curpos);
                }
            }
            else if (scanCode == 35 || scanCode == 34) //END or pgdown
            {
                this._DirectSelText = "R";
                if (Sys.Browser.agent == Sys.Browser.Opera)
                {
                    return;
                }
                this._SetCancelEvent(evt);
                if (evt.shiftKey)
                {
                    curpos = this._getCurrentPosition();
                    this.setSelectionRange(curpos,this._LogicLastPos+1);
                }
                else
                {
                    this.setSelectionRange(this._LogicLastPos+1,this._LogicLastPos+1);
                }
            }
            else if (scanCode == 36 || scanCode == 33) //Home or pgup
            {
                this._DirectSelText = "L";
                if (Sys.Browser.agent == Sys.Browser.Opera)
                {
                    return;
                }
                this._SetCancelEvent(evt);
                if (evt.shiftKey)
                {
                    curpos = this._getCurrentPosition();
                    this.setSelectionRange(this._LogicFirstPos,curpos);
                }
                else
                {
                    this.setSelectionRange(this._LogicFirstPos,this._LogicFirstPos);
                }
            }
            else if (scanCode == 37) //left
            {
                this._DirectSelText = "L";
                if (Sys.Browser.agent == Sys.Browser.Opera)
                {
                    return;
                }
                this._SetCancelEvent(evt);
                if (evt.shiftKey)
                {
                    var BoundSel = this._GetBoundSelection();
                    if (BoundSel)
                    {
                        if (BoundSel.left > this._LogicFirstPos)
                        {
                            BoundSel.left --;     
                        }
                        this.setSelectionRange(BoundSel.left,BoundSel.right);
                    }
                    else
                    {
                        var pos = this._getCurrentPosition();
                        if (pos  > this._LogicFirstPos)
                        {
                            this.setSelectionRange(pos -1,pos);
                        }
                    }
                }
                else
                {
                    curpos = this._getCurrentPosition()-1;
                    if (curpos < this._LogicFirstPos)
                    {
                        curpos = this._LogicFirstPos;
                    }
                    this.setSelectionRange(curpos,curpos);
                }
                if (Sys.Browser.agent == Sys.Browser.Opera)
                {
                    var wrapper = AjaxControlToolkit.TextBoxWrapper.get_Wrapper(this.get_element());
                    this._SaveText = wrapper.get_Value(); 
                    this._SavePosi = curpos;
                    this._timer.set_enabled(false);
                    this._timer.set_interval(1);
                    this._timer.set_enabled(true);
                }
            }
            else if (scanCode == 39) // right
            {
                this._DirectSelText = "R";
                if (Sys.Browser.agent == Sys.Browser.Opera)
                {
                    return;
                }
                this._SetCancelEvent(evt);
                if (evt.shiftKey)
                {
                    var BoundSel = this._GetBoundSelection();
                    if (BoundSel)
                    {
                        if (BoundSel.right < this._LogicLastPos+1)
                        {
                            BoundSel.right ++;     
                        }
                        this.setSelectionRange(BoundSel.left,BoundSel.right);
                    }
                    else
                    {
                        pos = this._getCurrentPosition();
                        if (pos  < this._LogicLastPos+1)
                        {
                            this.setSelectionRange(pos,pos+1);
                        }
                    }
                }
                else
                {
                    curpos = this._getCurrentPosition()+1;
                    if (curpos > this._LogicLastPos+1)
                    {
                        curpos = this._LogicLastPos+1;
                    }
                    this.setSelectionRange(curpos,curpos);
                }
                if (Sys.Browser.agent == Sys.Browser.Opera)
                {
                    var wrapper = AjaxControlToolkit.TextBoxWrapper.get_Wrapper(this.get_element());
                    this._SaveText = wrapper.get_Value(); 
                    this._SavePosi = curpos;
                    this._timer.set_enabled(false);
                    this._timer.set_interval(1);
                    this._timer.set_enabled(true);
                }
            }
            else if (scanCode == 27) // esc
            {
                this._SetCancelEvent(evt);
                var wrapper = AjaxControlToolkit.TextBoxWrapper.get_Wrapper(this.get_element());
                if (this._EmptyMask == this._initialvalue)
                {
                    wrapper.set_Value("");
                }
                else
                {
                    wrapper.set_Value(this._initialvalue);
                }
                this._onFocus();
            }
            //else if (scanCode == 38 || scanCode == 40)  //up - down 
        }
        // any other nav key
        this._SetCancelEvent(evt);
    }
  }
}