﻿/// <reference path="jquery.intellisense.js"/>

// ===========================================================================
// Handle swapping of Flash marketing messages with static graphic for pop-ups
// ===========================================================================
function SwapMarketingFlash(action) {
    if (action=='flash'){
    // swap the dynamic flash content if the marketing content is currently visible
        if((!$("#flashContainer").attr("class"))||($("#flashContainer").attr("class").indexOf("hideMe") < 0)){
           $("#flashContainer").show();
           $("#flashContentPlaceholder").hide();
           var showCloseFlash = setTimeout(function(){$(".closeFlash").css("visibility","visible");},2500);
        }
    }
    else if (action=='static'){
    // swap to the static marketing content if the marketing content is currently visible
        if((!$("#flashContainer").attr("class"))||($("#flashContainer").attr("class").indexOf("hideMe") < 0)){
              $("#flashContainer").hide();
              $("#flashContentPlaceholder").show();
              //var showCloseFlash = setTimeout(function(){$(".closeFlash").css("visibility","visible");},2500);
        }
    }
    if ($("#flashContainer").length>0){
      if ($("#flashContainer").attr("class").indexOf("hideMe") == 0) {
          $("#flashContainer").hide();
          $("#flashContentPlaceholder").hide();
          $(".flashOpen").css("display","block");
      }
    }
}
// =====================
// Show Preview Panel    
// =====================
function ShowFeedbackPanel() {
    ClearAlertMsg();
    // show the panel
    yuiFeedbackPanel.show();
    // swap the static marketing content
    if (document.location.pathname.toLowerCase()=='/default.aspx'){
        SwapMarketingFlash('static');    
    }
}
// =====================
// Hide Preview Panel    
// =====================
function CloseFeedbackPanel(action) {
    if (action=='send'){
        //call the web service to send the feedback
        var uidctrl, pagectrl
        uidctrl=document.getElementById('hidUniversalLoggedInUserCtrl').value;
        pagectrl=document.getElementById('hidUniversalPageIdCtrl').value;
        BNAlerts.Services.SubmitFeedback(document.getElementById(uidctrl).value, document.getElementById('txtFeedback').value, document.getElementById(pagectrl).value);
        document.getElementById('txtFeedback').value = "";
        SetAlertMsg('Thank you for your Feedback');
    }
    // hide the panel
    yuiFeedbackPanel.hide();
    //swap the static marketing content
    if (document.location.pathname.toLowerCase()=='/default.aspx'){
        SwapMarketingFlash('flash');    
    }
}

// =====================
// Show DemoVideo Panel    
// =====================
function ShowDemoVideoPanel() {
    // set the src
    document.getElementById('framDemoVideo').src = '/utils/demovideo.aspx';
    // show the panel
    yuiDemoVideoPanel.show();
    // swap the static marketing content
    if (document.location.pathname.toLowerCase() == '/default.aspx') {
        SwapMarketingFlash('static');
    }
}
// =====================
// Hide Preview Panel    
// =====================
function CloseDemoVideoPanel(action) {
    // reset the src
    document.getElementById('framDemoVideo').src = '/misc/loading.htm';
    // hide the panel
    yuiDemoVideoPanel.hide();
    //swap the static marketing content
    if (document.location.pathname.toLowerCase() == '/default.aspx') {
        SwapMarketingFlash('flash');
    }
}

// =====================
// Set/Clear Alert Message
// =====================
function SetAlertMsg(msg) {
  //document.getElementById('spanAlertMsg').innerHTML=msg;
  //document.getElementById('divAlertMsg').style.visibility='visible';
  //for using Humanized Messages - http://code.google.com/p/humanmsg/wiki/UsingHumaneMessages
  humanMsg.displayMsg(msg); 
  // start the session timer
  StartSessionTimer();
}
function ClearAlertMsg() {
    //I believe this is obsolete if we are using humanMsg
    if (document.getElementById('spanAlertMsg') != null) {
        document.getElementById('spanAlertMsg').innerHTML = '';
        document.getElementById('divAlertMsg').style.visibility = 'hidden';
    }
} 
// =====================
// Shared login pop-up methods
// =====================
function ShowLoginFields(cmd){
  if (cmd==true) {
    // show the login fields and hide the registration fields
    document.getElementById("divLoginHeading").innerHTML='Login';
    document.getElementById("pLoginFirst").innerHTML='Please login to complete your action';
    document.getElementById('divShowLoginFields').style.display='block';
    document.getElementById('divShowRegFields').style.display='none';
  }
  else {
    // hide the login fields and show the registration fields
    document.getElementById("divLoginHeading").innerHTML='Register';
    document.getElementById("pLoginFirst").innerHTML='Please register to complete your action';
    document.getElementById('divShowLoginFields').style.display='none';
    document.getElementById('divShowRegFields').style.display = 'block';
    // set the soruce on the hidden frame to track the viewing of the reg screen
    document.getElementById('RegPopupTrackingFrame').setAttribute('src', '/utils/reg_popup.htm');
  }
}
function ShowLoginPanel() {
  // hide the progress indicator and error messages
  document.getElementById('spanLoginProgress').style.display='none';
  document.getElementById('spanLoginError').style.display='none';
  document.getElementById('spanRegProgress').style.display='none';
  document.getElementById('spanRegError').style.display='none';
  // show the panel
  yuiLoginPanel.show();
}
function CloseLoginPanel(action) {
  var rxUsername=/[a-zA-Z0-9]{4,20}/i;
  var rxPassword=/[a-zA-Z0-9!@#$%^&*]{6,12}/;
  var rxEmail = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
  var rxPostalCode = /[\w-\ ]{0,20}/;
  if (action=='login') {
    // process the login request
    // hide the error message
    document.getElementById('spanLoginError').style.display='none';  
    var username=document.getElementById('txtUsernamePU').value;
    var password=document.getElementById('txtPasswordPU').value;
    // verify that the username/password matches valid patterns
    if ((username.match(rxUsername)==username)&&(password.match(rxPassword)==password)) {
      // show the progress indicator
      var progress=document.getElementById('spanLoginProgress');
      progress.innerHTML='Processing login request';
      progress.style.display='block';
      // call the webservice to process the login/registration
      BNAlerts.Services.ProcessLogin(username,password,LoginReturn);
    }
    else {
      // show an error message
      document.getElementById('spanLoginError').style.display='block';
      document.getElementById('spanLoginError').innerHTML='Invalid username or password';
    }
  }
  else if (action=='register') {
    // process the registration request
    // hide the error message
    document.getElementById('spanRegError').style.display='none';  
    var username=document.getElementById('txtRegUsernamePU').value;
    var password=document.getElementById('txtRegPasswordPU').value;
    var password2=document.getElementById('txtRegPassword2PU').value;
    var email = document.getElementById('txtRegEmailPU').value;
    var postal = document.getElementById('txtRegPostalCodePU').value;
    //var code=$('#hidSessionInviteCode').attr('value');
    var code = '';
    // verify the username, password and email address
    if ((username.match(rxUsername) == username) && (password.match(rxPassword) == password) && (email.match(rxEmail))) {
      // verify that the passwords match
      
    if (password==password2) {
        // verify that the terms of service is selected
        if (document.getElementById('chkRegTermsPU').checked==true){
          // verify that a timezone is selected
          var index = document.forms[0].selRegTimeZonePU.selectedIndex;
          var val = document.getElementById('selRegTimeZonePU')[index].value;
          if (val!='') {          
            // verify that first and last name are filled in
            var first=document.getElementById('txtRegFirstNamePU').value;
            var last=document.getElementById('txtRegLastNamePU').value;
            if ((first!='')&&(last!='')) {
                // verify the postal code is valid
                if (postal.match(rxPostalCode) == postal) {
                    // get the country
                    var cindex = document.forms[0].selRegCountryPU.selectedIndex;
                    var country = document.getElementById('selRegCountryPU')[cindex].value;
                    // get the birth year
                    var bindex = document.forms[0].selRegBirthYearPU.selectedIndex;
                    var byear = document.getElementById('selRegBirthYearPU')[bindex].value;
                    // get the gender
                    var gindex = document.forms[0].selRegGenderPU.selectedIndex;
                    var gender = document.getElementById('selRegGenderPU')[gindex].value;
                    // submit the registration request
                    // NOTE: "LoginReturn" is a callback function unique to each page that uses this pop-up
                    BNAlerts.Services.ProcessRegistration(username, password, password2, email, first, last, val, code, country, postal, byear, gender, LoginReturn);
                }
                else {
                    // show an error message
                    document.getElementById('spanRegError').innerHTML = 'The postal code entered is not valid';
                    document.getElementById('spanRegError').style.display = 'block';
                }
            }
            else {
              // show an error message
              document.getElementById('spanRegError').innerHTML='First and last name are required';        
              document.getElementById('spanRegError').style.display='block';
            }
          }
          else {
            // show an error message
            document.getElementById('spanRegError').innerHTML='You must select a time zone';        
            document.getElementById('spanRegError').style.display='block';
          }
        }
        else {
          // show an error message
          document.getElementById('spanRegError').innerHTML='You must accept the terms of service';        
          document.getElementById('spanRegError').style.display='block';
        }
      }
      else {
        // show an error message
        document.getElementById('spanRegError').innerHTML='Passwords do not match';
        document.getElementById('spanRegError').style.display='block';
      }
    }
    else {
      // show an error message
      document.getElementById('spanRegError').innerHTML='Invalid username, password or email';
      document.getElementById('spanRegError').style.display='block';
    }
  }
}

//================================
//Session Time Out Handling
//==================================
var timeOutMinutes = 59; // set to 59 instead of 60 to avoid any strange timeout behavior betweent this and the session/webmux
var redirectUrl = "/a/sessionend.aspx";
var timeoutId=null;

//Starts the timer to show the warning message
function ClearTimer(){
  // clear the timeout if it exists
  if(timeoutId!==null){
    clearTimeout(timeoutId);
  }
}
function StartSessionTimer(){
  // clear the timer if it exists
  ClearTimer();
  // create new timer
  timeoutId=setTimeout(OnTimeOut, timeOutMinutes * 60000);
}
function OnTimeOut(){
    window.location.href = redirectUrl;
}