function redirect(page)
{
    window.location.href = page
}

function open_window(page,params)
{
    window.open(page,"popup",params)
}

function reset_iframe(id,src) {
    var _frame = document.getElementById(id);
    _frame.src = src;
}  
   
 function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function stopBubbling(e) {

  if(!e)
    e = window.event;
  
  if(e.stopPropagation)
    e.stopPropagation();
  
  if(e.preventDefault)
    e.preventDefault();
    
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
     
}

function default_button(btnID, e)
{ 
    var btn = document.getElementById(btnID); 
    
     if (document.all) { 
       if (e.keyCode == 13) { 
            stopBubbling(e) 
            btn.click();
        } 
     } else if (document.getElementById || document.layers) { 
       if (e.which == 13) {         
      
            if( btn && btn.click ) {                                
                stopBubbling(e) 
                btn.click();     
            } else if ( btn &&  btn.onclick ) {
               var temp = eval(btn.onclick);               
                if( temp ) { 
                    stopBubbling(e)  
                    temp(e);                   
                } 
            }   
        }
     }      
}

function ShowInternationalDealer(ddl) {
    var value = ddl[ddl.selectedIndex].value;
    if( value != '0' ) {
        open_window(value, 'redirect', 'width=500px, height=500px, resizable=1, scrollbars=1');
    }
} 

function FindLocalRetailer(tbx_zip, ddl_states, mBehaviorID, baseURL) {
    
      var zip = tbx_zip.value;
      var state = ddl_states[ddl_states.selectedIndex].value;
    
      if( (zip == '' || zip == 'zip' ) && state == '' ) {
        alert("Invalid Zip Code And State"); 
        return false;
      }      
      
      var m = $find(mBehaviorID);
      if( m ) {
        var url = baseURL + '?zip=' + zip + '&state=' + state

        m.set_dynamicContextKey('{src: ' + url + ', width: 800px, height: 600px;}');   
        m.show()  
       }
       
      return true;
   } 
   
    function EmailSignup(tbxID, modalID, baseURL, e) {
        var tbx = $get(tbxID);

        if( tbx ) {
            if( tbx.value == '' || tbx.value == 'Sign up for KEEN emails' ) {
                stopBubbling(e)                      
                setTimeout('alert(\'Invalid Email Address\');', 100);
                return false;
            } else {
                OpenModal(modalID, baseURL + 'I_thanks.aspx?e=' + tbx.value, '610px', '300px', 'no')
                tbx.value =  'Sign up for KEEN emails';
               return true; 
            }
         }               
    } 
   
   function EmailSignup2(tbxID, modalID, baseURL, initialText, e) {
        var tbx = $get(tbxID);

        if( tbx ) {
            if( tbx.value == '' || tbx.value == initialText ) {
                stopBubbling(e)                      
                setTimeout('alert(\'Invalid Email Address\');', 100);
                return false;
            } else {
                OpenModal(modalID, baseURL + 'I_thanks.aspx?e=' + tbx.value, '610px', '300px', 'no')
                tbx.value =  initialText
               return true; 
            }
         }
   }

   function EmailSignupEU(tbxID, modalID, baseURL, initialText, e) {
       var tbx = $get(tbxID);

       if (tbx) {
           if (tbx.value == '' || tbx.value == initialText) {
               stopBubbling(e)
               setTimeout('alert(\'Invalid Email Address\');', 100);
               return false;
           } else {
               OpenModal(modalID, baseURL + 'I_EU_Optin.aspx?e=' + tbx.value, '610px', '330px', 'no')
               tbx.value = initialText
               return true;
           }
       }
   }  
   
   function SearchProducts(url, tbxSearchID) {
        var Query = $get(tbxSearchID).value;
        if( Query == '' || Query == 'find products') {
          setTimeout('alert(\'Invalid Search Criteria\');',100)
           return false
        }                
        redirect(url + '?q=' + Query);
        return false;
    }  
   
      function SearchProducts2(url, tbxSearchID, initialText) {
        var Query = $get(tbxSearchID).value;
        if( Query == '' || Query == initialText) {
          setTimeout('alert(\'Invalid Search Criteria\');',100)
           return false
        }                
        redirect(url + '?q=' + Query);
        return false;
    }   


function ValidatePhoneParts(part1, part2, part3) {
  
    //Validate Part 1   
    if( part1.length != 3 || !IsNumeric(part1) ) {
        return false
    } 
   
   //Validate Part 2   
    if( part2.length != 3 || !IsNumeric(part2) ) {
        return false
    } 
   
   //Validate Part 3    
       if( part3.length != 4 || !IsNumeric(part3) ) {
        return false
    } 
   return true; 
}

function PhoneNumberAutoTab(tbx, nextTbxID, e) {
    var tbx_next = $get(nextTbxID); 
    var sourceKeyCode = (e.keyCode) ? e.keyCode : e.charCode;
        
    if( sourceKeyCode != 9 && sourceKeyCode != 16) {     
        if( tbx.value.length == tbx.maxLength ) {
           tbx_next.focus();       
        }          
    } else {
         stopBubbling(e)
    } 
}

/***IMAGE CACHING FUNCTIONS***/
function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/***/


/***/
/**MODAL MANAGEMENT**/

var CurrentModal;
var CurrentModalOnCloseDoPostBack = false;
var CurrentModalOnCloseTargetID = '';
var CurrentModalOnCloseArgument = '';
var CurrentModalOpeningParent = null;
var CurrentModalOnCloseCallbackFunction = '';

function OpenModal(behaviorID, url, width, height, OpenModalParams) {
    var m = $find(behaviorID);

    if (m) {

        CurrentModal = m

        var scrolling = 'NO';

        if (OpenModalParams) {
            CurrentModalOnCloseDoPostBack = OpenModalParams.OnClosePostBack
            CurrentModalOnCloseTargetID = OpenModalParams.OnCloseTargetID
            CurrentModalOnCloseArgument = OpenModalParams.OnCloseArgument
            CurrentModalOpeningParent = OpenModalParams.Parent;
            CurrentModalOnCloseCallbackFunction = OpenModalParams.OnCloseCallbackFunction;
            if (OpenModalParams.ScrollFrame && OpenModalParams.ScrollFrame == true) {
                scrolling = 'auto';
            } else {
                $('.topMessage').hide();
            }
        }

        if (url != '') {
            var mContent = $get(behaviorID + '_content')
            if (mContent) {
                mContent.innerHTML = '<iframe src=\'' + url + '\' width=\'' + width + '\' height=\'' + height + '\' scrolling=\'' + scrolling + '\' frameborder=\'0\'/>';
            }
        }

        m.show()

        return true;
    } else {
        return false;
    }

}

function OpenModalParams() {
    var OnClosePostBack = false;
    var OnCloseTargetID = '';
    var OnCloseArgument = '';
    var OnCloseCallbackFunction = '';
    var Parent = null;
    var ScrollFrame = false;
}

function HideModal(HideModalParams) {

    var m = CurrentModal;

    if (m) {
 
        var mContent = $get(m._id + '_content')
        if (mContent) {
            mContent.innerHTML = '';
        }

        m.hide();

        var _EventArgs = (CurrentModalOnCloseArgument == '') ? '' : CurrentModalOnCloseArgument;
        if (HideModalParams) {
            _EventArgs = (HideModalParams.EventArgs == 'undefined') ? '' : HideModalParams.EventArgs;
     
            if (HideModalParams.CancelOnCloseDoPostBack) {
                CurrentModalOnCloseDoPostBack = false;
            }
        }

        if (CurrentModalOnCloseDoPostBack) {
            if (CurrentModalOpeningParent) {
                CurrentModalOpeningParent.UpdatePage(CurrentModalOnCloseTargetID, _EventArgs);
            } else {
                UpdatePage(CurrentModalOnCloseTargetID, _EventArgs);
            }
        } else if (CurrentModalOnCloseCallbackFunction && CurrentModalOnCloseCallbackFunction.indexOf('(') == -1) {

            var _tmp = CurrentModalOnCloseCallbackFunction;
            _tmp = _tmp + '({ \'EventTargetID\':\'' + CurrentModalOnCloseTargetID + '\', \'EventArgs\':\'' + _EventArgs + '\' })'

            if (CurrentModalOpeningParent) {
                CurrentModalOpeningParent.eval(_tmp);
            } else {
                eval(_tmp);
            }
        } else if (CurrentModalOnCloseCallbackFunction && CurrentModalOnCloseCallbackFunction.indexOf('(') >= 0) {

            eval(CurrentModalOnCloseCallbackFunction)
        }

        CurrentModal = null;
        CurrentModalOnCloseDoPostBack = false;
        CurrentModalOnCloseTargetID = '';
        CurrentModalOnCloseArgument = '';
        CurrentModalOpeningParent = null;

    }
}

function HideModalParams() {
    var EventArgs;
    var CancelOnCloseDoPostBack = false;
}

function ModalOnCloseCallbackParams() {
    var EventTargetID = '';
    var EventArgs = '';
}

function UpdatePage(target, args) {
    setTimeout('__doPostBack(\'' + target + '\', \'' + args + '\')', 100)
}





function UpdatePanel(target, args) {
    setTimeout('__doPostBack(\'' + target + '\', \'' + args + '\')',100)
}

function ChangePaginationPageSize(ddlID, baseURL) {
    var ddl = $get(ddlID);

    if ( baseURL.indexOf('?') == -1 ) {
        baseURL += "?"      
    } else {
        baseURL += "&"      
    }
   
    redirect(baseURL + 'cp=1&ps=' + ddl[ddl.selectedIndex].value);    
}

function GetFlashMovie(movieName) {     
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    } else {
        return document[movieName]
    } 
} 

function ClearDefaultText(tbxID, defaultText) {
    var tbx = $get(tbxID);
    if( tbx ) {
        if( tbx.value == defaultText ) {
            tbx.value = '';
        }
    }  
}

function SetNavSelectedClass(navID, sClass) {
    var n = $get(navID);
    if( n ) {
        if( n.className == '' ) {
            n.className = sClass;
        } else {
            n.className += ' ' + sClass;
        }
    }  
}

/***/

// Tab Functions
function tab(id, pnl, cont, onMouseover) {

    this.id = id;
    this.pnl = pnl;   
    this.container = cont;  
    this.onMouseover = onMouseover; 
    
   this._onChangeHandler = null;  
}

tab.prototype = {

    init:  function() {     
        this._onChangeHandler = Function.createDelegate(this, this._onChange);        
        var e = $get(this.get_id());
        
        if( !this.onMouseover ) {
            $addHandler(e, "click", this._onChangeHandler);        
        } else {
            $addHandler(e, "mouseover", this._onChangeHandler);      
        }
    },
   
    _onChange: function() {
        this.container._onChange(this.get_id());
    },  
   
    get_tabOnChangeHandler: function() {
        return this._onChangeHandler;
    },   
    
    get_id: function() {
        return this.id;
    },
    set_id: function(id) {
        this.id = id;
    },
   
   get_pnl: function() {
        return this.pnl;
    },
    set_pnl: function(pnl) {
        this.pnl = pnl;
    }    
    
}


// Tabs collection functions
function tabs() {
     this.page_tabs = new Array();
     this._tabOnChangeHandler = null;  
}

tabs.prototype = {

    init: function () {

        var _items = this.page_tabs;
        for (x = 0; x < _items.length; ++x) {
            var pnl = $get(_items[x].pnl);
            pnl.style.display = "none"
        }
    },

    add: function (tab) {

        tab.init();
        this.page_tabs[this.get_length()] = tab;

    },


    _onChange: function (id) {

        this.tab_activate(id);

    },

    tab_activate: function (e) {
        var _items = this.page_tabs;

        for (x = 0; x < _items.length; ++x) {
            var pnl = $get(_items[x].pnl);
            var tb = $get(_items[x].id);
            var _children = tb.childNodes;

            if (_items[x].id == e) {

                pnl.style.display = "block";

                for (y = 0; y < _children.length; y++) {


                    
                    if (_children[y].tagName == 'IMG') {
                        
                        if (_children[y].className == 'onstate') {
                            _children[y].className = 'active';
                        } else if (_children[y].className == 'offstate') {
                            _children[y].className = 'inactive';
                        }
                    } else if (_children[y].tagName == 'A') {

                        _children[y].className = 'tab_on'
                    }
                }


            }
            else {

                pnl.style.display = "none";

                for (y = 0; y < _children.length; ++y) {

                    if (_children[y].tagName == 'IMG') {
                        if (_children[y].className == 'active') {
                            _children[y].className = 'onstate';
                        } else if (_children[y].className == 'inactive') {
                            _children[y].className = 'offstate';
                        }
                    } else if (_children[y].tagName == 'A') {
                        _children[y].className = 'tab'


                    }

                }


            }
        }
    },

    items: function () {
        return this.page_tabs;
    },

    get_length: function () {
        return this.page_tabs.length;
    }

}


 /***********************************************
 * Found at http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/msg/f2234ba565b8effa?dmode=source&output=gplain
 ***********************************************/

 function SetUniqueRadioButton(nameregex, current) {
     re = new RegExp(nameregex);
     for (i = 0; i < document.forms[0].elements.length; i++) {
         elm = document.forms[0].elements[i]
         if (elm.type == 'radio') {
             if (re.test(elm.name)) {
                 elm.checked = false;
             }
         }
     }
     current.checked = true;
 }


 /**
 *
 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */
 (function ($) {
     /**
     * attaches a character counter to each textarea element in the jQuery object
     * usage: $("#myTextArea").charCounter(max, settings);
     */

     $.fn.charCounter = function (max, settings) {
         max = max || 100;
         settings = $.extend({
             container: "<span></span>",
             classname: "charcounter",
             format: "(%1 characters remaining)",
             pulse: true,
             delay: 0
         }, settings);
         var p, timeout;

         function count(el, container) {
             el = $(el);
             if (el.val().length > max) {
                 el.val(el.val().substring(0, max));
                 if (settings.pulse && !p) {
                     pulse(container, true);
                 };
             };
             if (settings.delay > 0) {
                 if (timeout) {
                     window.clearTimeout(timeout);
                 }
                 timeout = window.setTimeout(function () {
                     container.html(settings.format.replace(/%1/, (max - el.val().length)));
                 }, settings.delay);
             } else {
                 container.html(settings.format.replace(/%1/, (max - el.val().length)));
             }
         };

         function pulse(el, again) {
             if (p) {
                 window.clearTimeout(p);
                 p = null;
             };
             el.animate({ opacity: 0.1 }, 100, function () {
                 $(this).animate({ opacity: 1.0 }, 100);
             });
             if (again) {
                 p = window.setTimeout(function () { pulse(el) }, 200);
             };
         };

         return this.each(function () {
             var container = (!settings.container.match(/^<.+>$/))
				? $(settings.container)
				: $(settings.container)
					.insertAfter(this)
					.addClass(settings.classname);
             $(this)
				.bind("keydown", function () { count(this, container); })
				.bind("keypress", function () { count(this, container); })
				.bind("keyup", function () { count(this, container); })
				.bind("focus", function () { count(this, container); })
				.bind("mouseover", function () { count(this, container); })
				.bind("mouseout", function () { count(this, container); })
				.bind("paste", function () {
				    var me = this;
				    setTimeout(function () { count(me, container); }, 10);
				});
             if (this.addEventListener) {
                 this.addEventListener('input', function () { count(this, container); }, false);
             };
             count(this, container);
         });
     };

 })(jQuery);

  /* Google Analytics function for Flash movies (or other non-html pages)
*/

 function Trackpage(inLink){
        _gaq.push(['_trackPageview', inLink])


 } 

  function Trackevent(inLink){
        _gaq.push(['_trackEvent', 'FlashTrack', inLink])
 } 
