function DisableButton(obj) {
  obj.blur();
  obj.src          = '/images/btn_processing.gif';
  obj.disabled     = true;
  obj.style.border = 'none';
}

var ie4     = false;
var ie5     = false;
var ns4     = false;
var ns6     = false;
var mac     = false;
var testing = false;
function SetBrowser() {
  agent       = navigator.userAgent.toLowerCase();
  mac         = (agent.indexOf("mac")!=-1);
  ie4         = (document.all && !document.getElementById) ? true : false;
  ie5         = (document.all && document.getElementById)  ? true : false;
  ns4         = (document.layers)                          ? true : false;
  ns6         = (document.getElementById && !document.all) ? true : false;
  if (testing){ alert("agent: "+agent+"\nmac: "+mac+"\nie4: "+ie4+"\nie5: "+ie5+"\nns4: "+ns4+"\nns6: "+ns6+"\n"); }
}

Event.onReady(function() {
   InitMenus("nav");
   //InitMenus("shoppingTasks");
   //InitStoreTasks("shoppingTasks");
   SetBrowser();
    if (!(mac && ie5)) {
      ScrollLinks.start();
    }
});


function safemail(name, domain, display, css, subject) {
  css       = (css)     ? ' class="' + css + '"' : '';
  subject   = (subject) ? '?subject=' + subject : '';
  displayed = (display) ? display : name + '@' + domain;
  mailto    = name + '@' + domain + subject;
  document.write('<a href="mailto:' + mailto + '"' + css + '>' + displayed + '</a>');
}

function SetFocus(fieldname,formname){if (formname == ''){ formname = 'form'; }if (eval("document."+formname+".elements[fieldname].value == ''")){ eval("document."+formname+".elements[fieldname].focus();"); }}

function HideOutput() {
  // check for previous reponses/errors
  if ($('flash')) { Element.hide('flash'); }
}

function PopWindow(title, url, width, height) {
  BuildWindow({id:'popwindow', title:title, url:url, width:width, height:height});
}
function SubmitForm(obj, form_action) {
  // disable button
  DisableButton(obj);
  
  // get elements
  var frm = document.forms[obj.form.name];
  var act = frm.elements['form_action'];
  
  // set form action
  act.value = form_action;
  
  // submit form
  frm.submit();
}

function Trim(s){
 // Remove leading spaces and carriage returns
 while (s.substring(0,1) == ' ')
 { s = s.substring(1,s.length); }
     
 // Remove trailing spaces and carriage returns
 while (s.substring(s.length-1,s.length) == ' ')
 { s = s.substring(0,s.length-1); }
     
 return s;
}

function AllowOnlyEmail(){
 // Get the ASCII value of the key that the user entered
 var key = window.event.keyCode;

 // Verify if the key entered was a numeric character (0-9) or a decimal (.)
 if ( (key > 47 && key < 58) || // Numeric values (0 - 9)
  key == 46 || // . character
  key==64 || // @ character
  (key >= 65 && key <= 90) || // A-Z (upper case)
  (key >= 97 && key <= 122) ) // a-z (lower case)
    // If it was, then allow the entry to continue
    return;
   else
    // If it was not, then dispose the key and continue with entry
    window.event.returnValue = null;
}

function NumericOnly(){
    // Get the ASCII value of the key that the user entered
    var key = window.event.keyCode;
    
    if ((key > 47 && key < 58) || // Numeric (0-9)
          key == 46 || // delete key
          key==8 || // backspace key
          (key >= 37 && key <= 40)) // Arrow Keys
        // If it was, then allow the entry to continue
        return;
    else
        // If it was not, then dispose the key and continue with entry
        window.event.returnValue = null;
}

function doCustomPostBack(actionUrl, submit){
    if ((typeof(actionUrl) == "undefined") || (actionUrl === null) || (actionUrl.length === 0)) {
      return;
    }
  
    // theForm is defined by ASP.NET
    theForm.action = actionUrl;
  
    if(submit){
        __doPostBack("","");
    }
}

var Banner = {};

(function(){

    function toggle(id) {
        var e = document.getElementById(id);
        if(e.style.display == 'inline'){
            e.style.display = 'none';
        }else{
            e.style.display = 'inline';
        }
    }
    
    function toggleHandler(id){
        return function(e){
            toggle(id);
            e.preventDefault();
        };
    }

    function keyHandler(keyCode, action){
        return function(e){
            if(keyCode == e.which){
                action();
                e.preventDefault();
            }
        };
    }

    function listenForEnter(id, postAction){
        var el = $jq(id);
        if(el){
            el.keypress(keyHandler(13, postAction));
        }
    }
	
    function listenForClick(id, action){
        var el = $jq(id);
        if(el){
            el.click(action);
        }
    }
	
    function hookEvents(loginUrl){	
        var postThunk = function(){ doCustomPostBack(loginUrl, true); };
        listenForEnter('#fmEmailAddress', postThunk);
        listenForEnter('#fmPassword', postThunk);
	
        listenForClick('#createAcctNoBtn', toggleHandler('benefitsOverlayContainer'));
        listenForClick('#createAcctWhyLnk', toggleHandler('benefitsOverlayContainer'));
	
        listenForClick('#showSignInBtn', toggleHandler('signInOverlayContainer'));
        listenForClick('#btnClose', toggleHandler('signInOverlayContainer'));
	
        listenForClick('#btnSignIn', function(e){
            doCustomPostBack(loginUrl, true);
            e.preventDefault();
        });
    }
	
    Banner = {
        Login: function(loginUrl){
            this.LoginUrl = loginUrl;
        }
    };
	
    Banner.Login.prototype = {
        register: function(){
            var loginUrl = this.LoginUrl;
            $jq(document).ready(function(){
                hookEvents(loginUrl);
            });
        }
    };
}());