﻿// JScript File

var friends = new Array(5);
   friends[0] = 'uxFriend1';
   friends[1] = 'uxFriend2';
   friends[2] = 'uxFriend3';
   friends[3] = 'uxFriend4';
   friends[4] = 'uxFriend5';
   
   var emails = new Array(5);
   emails[0] = 'uxFriendEmail1';
   emails[1] = 'uxFriendEmail2';
   emails[2] = 'uxFriendEmail3';
   emails[3] = 'uxFriendEmail4';
   emails[4] = 'uxFriendEmail5';

  var snagtypes = new Array(17);  
  snagtypes[0] = 'cheesemustard';
  snagtypes[1] = 'onions';
  snagtypes[2] = 'cheeseonions';
  snagtypes[3] = 'mustard';
  snagtypes[4] = 'sauce';
  snagtypes[5] = 'plain';
  snagtypes[6] = 'cheesesaucemustard';
  snagtypes[7] = 'cheese';
  snagtypes[8] = 'cheesemustardsauceonions';
  snagtypes[9] = 'cheesesauce';
  snagtypes[10] = 'onionssauce';
  snagtypes[11] = 'mustardsauce';
  snagtypes[12] = 'onionsmustard';
  snagtypes[13] = 'mustardsauceonions';
  snagtypes[14] = 'cheeseonionssauce';
  snagtypes[15] = 'cheesemustardonions';
  snagtypes[16] = 'thelot';

var snagimagepath = "images/snag/preview/snag_"
var snagimageextension = ".png";

/*siwtches the snag image on the first step of the form*/
function SwitchImage()
{
  var regular = $("input[name$='hotdogtypes']:checked").val() == '2' ? false : true;
  var onions = $("#step1 input[type='checkbox'][id$='uxExtraOnions']").attr('checked');
  var cheese = $("#step1 input[type='checkbox'][id$='uxExtraCheese']").attr('checked');
  var mustard = $("#step1 input[type='checkbox'][id$='uxExtraMustard']").attr('checked');
  var sauce = $("#step1 input[type='checkbox'][id$='uxExtraSauce']").attr('checked');
  
  var imagenumber = GetSnagTypeImage(regular,onions,cheese,mustard,sauce);
  
  $("#snagimage").attr("src", snagimagepath + imagenumber + snagimageextension);
  
}

//The transition effects for the babysteps plug in
 function step_transition(currstep, nextstep)
 {
    currStep.slideUp();  
    nextStep.slideDown();
 }
 
 //Returns a value that is formatted as currency.
 function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
}
 
 //Calculates the subtotal of the product.
 function CalcSubTotal(quantity,validate)
 {
 SwitchImage();
 if(validate)
 {
  CheckTextBoxValidRules();
 }
 //$ - end of string ^ - start of string * - wildcard [@name=xxxx - perfect match
   var base = $("input[name$='hotdogtypes']:checked").val() == '2' ? 10.00 : 3.00;
   var extra = 0;
   
   var extradon = $("input[id$='uxExtraDonation']").val() != '' ? $("input[id$='uxExtraDonation']").val()*1 :       formatCurrency('0');
   
   //check to see if var is a number
   if(isNaN(extradon)) extradon = 0;
   
   var price;   
    
    $("#step1 input[type='checkbox'][id*='Extra']").each(function(){
      if($(this).attr('checked'))
      {
         var value = $(this).attr('value');
         extra += (value*1);
      }
      
    });
    
    var extradonformat=formatCurrency(extradon);
    var subtotal=formatCurrency((extra+base));
    var total = formatCurrency((extra+base)*quantity+extradon);
    
    
    $("#step2 [id$='uxPrice']").html(subtotal);
    $("#step1 input[id$='uxSubTotal']").val(subtotal);
    $("#step2 [id$='uxTotal']").html(total);
    $("#gfcdonamount").html(extradonformat);
    
    $("#uxQuantity").html(quantity == 0 ? '0':quantity);
 }
 
 //shows/hides the condiments on the front page.
 function ShowCondiments(show)
 {
    switch(show)
    {
      case 1:
        $("#condiments").slideDown();
        $("input[id$='uxSubTotal']").val(formatCurrency(3.00));
        break;
      case 2:
        $("#condiments").slideUp();
        $("input[id$='uxSubTotal']").val(formatCurrency(10.00));
        $("#step1 input[type='checkbox'][id*='Extra']").each(function(){
          $(this).attr('checked','');
        });
        break;
      default:
        $("#condiments").hide();
        $("input[id$='uxSubTotal']").val(formatCurrency(10.00));
        break;
    }
 }
 
 //re calculates the totals. Also disables/enables extradonation field based on whether one of the uxFor* checkboxes is selected.
 function ReCalc()
 {
  QuantityCheck();
  
  CheckCheckBoxes();
  
  var checked = $("input[id$='uxForYou']").attr('checked') || $("input[id$='uxForFriends']").attr('checked');
  
  var clearextradon = !$("input[id$='uxForYou']").attr('checked') && !$("input[id$='uxForFriends']").attr('checked');
  if(clearextradon)
  {
    $("input[id$='uxExtraDonation']").val(formatCurrency(0));
    QuantityCheck();
  }
  $("input[id$='uxExtraDonation']").attr('disabled', !checked);
  
 } 
 
 //shows/hides friend names and email section also sets the validation techinique
 function ShowFriends(friendelement)
 {
 var ele = $('#'+friendelement.get(0).id+":checked").val();
  if(ele != null)
  {
    $("#friendsEmails").slideDown();
    //adds class to ensure element is included in validation
    $("input[id$='uxFriend1']").rules("add", { required: true, isRestricted:true });
    $("input[id$='uxFriendEmail1']").rules("add", { required: true, email: true });
    $("#aspnetForm").valid();
  }
  else
  {
    $("#friendsEmails").slideUp();
    //adds class to ensure element is NOT included in validation
    $("input[id$='uxFriendEmail1']").rules("remove");
    $("input[id$='uxFriend1']").rules("remove");
    
    //resets all values to null
    $("input[id*='uxFriend']").each(function(){
      $(this).val('');
    });
  }
 }
 
 //Calculates the quantity.
 function QuantityCheck()
 {
  var yourself = $("input[id$='uxForYou']:checked").val();
  var friend = $("input[id$='uxForFriends']:checked").val();
  
  ShowFriends($("input[id$='uxForFriends']"));
 i=0;
 
 //inludes friend text boxes in quantity if the checkbox is checked.
 if(friend != null)
 {
   $("input[id*='uxFriendEmail']").each(function(){
    if($(this).valid()) 
    {
      i++;
    }
  });
 }
  
  //add one to the quantity if the checkbox is checked.
  if(yourself != null) i++;
  
  CalcSubTotal(i,true);
 }

//instantiates the babysteps plug in.
 function stepify(){
 var step1 = $('#step1');
 var step2 = $('#step2');
 var step3 = $('#step3');
 step1.show();
 
 $.fn.bindStep.defaults.prevBtn = "images/common/previous.png";
 $.fn.bindStep.defaults.nextBtn = "images/common/next.png";
 $.fn.bindStep.defaults.stepIndicator = "test";
 $.fn.bindStep.defaults.generateMarkup = function(id1,id2,img){
 return([
 '<img width="120" height="25" style="float:right; margin-top:5px; cursor:pointer; cursor:hand;" ',
 'src="', img, '" ',
 'id="', id1 , '" ',
 '/>',
 ].join(''));
 } 
 /*$.fn.bindStep.defaults.layoutButton =  function(step, imgBtn, imgType) {
            if (imgType == 'next' || imgType == 'prev') {
                step.after(imgBtn);
            }
        }*/
/* //Dont like the default hide/show?  
 step1.bindStep(step2,{  
   transition: function(currStep,nextStep){  
     currStep.slideUp();  
     nextStep.slideDown();  
  }  
 });transition: step_transition(currStep, nextStep);   */
 step1.bindStep(step2,{  
   transition: function(currStep,nextStep){  
     currStep.fadeOut().hide();  
     nextStep.fadeIn();
     QuantityCheck();
     $("#activityContainer").addClass("TransactionDisclaimer");
      
  },
  nextValidator: function(){
    var isvalid = true;
    $("#step1 input[@type='text']").each(function(){
            isvalid = $(this).valid();
            return isvalid;
    });
    
    return isvalid;
  }  
 });
 step2.bindStep(step3,{  
   transition: function(currStep,nextStep){  
     currStep.fadeOut().hide();  
     nextStep.fadeIn();   
  },
  nextValidator: function(){
    var isvalid = true;
    $("#step1 input[@type='text']").each(function(){
            isvalid = $(this).valid();
            return isvalid;
    });
    
    return isvalid;
  }  
 });
   }

/*iterates through array and calls a function with variables based on array position*/
function CheckTextBoxValidRules()
{
 
  var arLen=friends.length;
  for ( var i=1, len=arLen; i<len; ++i )
  {
    var friend = friends[i];
    var email = emails[i];
    ValidateForm(friend,email);
  }
}

/*Dynamically adds and removes validation rules based on field values*/
function ValidateForm(friend,email) {
    var name = $("input[id$='"+friend+"']");
    var address = $("input[id$='"+email+"']");

    if (name.val() != '' || address.val() != '') {
        name.rules("add", { required: true, isRestricted:true });
        address.rules("add", { required: true, email: true });
    }
    if (name.val() == ''&& address.val() == '') {
        name.rules("remove");
        address.rules("remove");
        
    }
}

/*Validates the central checkboxes. */
function CheckCheckBoxes()
{
  var you = $("input[id$='uxForYou']").is(":checked");
  var friend = $("input[id$='uxForFriends']").is(":checked");
  
  if(!you && !friend)
  {
    $("label[for$='uxForFriends']").show();
    $(".forwhom").addClass('error');
    return false;
  }
  else
  {
    $("label[for$='uxForFriends']").hide();
    $(".forwhom").removeClass('error');
    return true;
  }
  
}

/*Selects all text in a text area.*/
function SelectAllTextArea(id)
{
    id.focus();
    id.select();
}

/*Get the correct snag image based upon options*/
function GetSnagTypeImage(regular,onions,cheese,mustard,sauce)
{
  if(regular && !onions && !cheese && !mustard && !sauce)
    return 5;
    
  if(regular && onions && !cheese && !mustard && !sauce)
    return 1;
   
  if(regular && onions && cheese && !mustard && !sauce)
    return 2;
    
    if(regular && onions && cheese && mustard && !sauce)
    return 15;
    
    if(regular && onions && cheese && mustard && sauce)
    return 8;
    
    if(regular && !onions && cheese && !mustard && !sauce)
    return 7;
    
    if(regular && !onions && cheese && mustard && !sauce)
    return 0;
    
    if(regular && !onions && cheese && mustard && sauce)
    return 6;
    
    if(regular && !onions && !cheese && mustard && !sauce)
    return 3;
    
    if(regular && !onions && !cheese && mustard && sauce)
    return 11;
    
    if(regular && onions && !cheese && mustard && sauce)
    return 13;
    
    if(regular && !onions && !cheese && !mustard && sauce)
    return 4;
    
    if(regular && onions && !cheese && !mustard && sauce)
    return 10;
    
    if(regular && !onions && cheese && !mustard && sauce)
    return 9;
    
    if(regular && !onions && !cheese && mustard && !sauce)
    return 3;
    
    if(regular && onions && !cheese && mustard && !sauce)
    return 12;
    
    if(regular && onions && cheese && !mustard && sauce)
    return 14;
    
    if(!regular)
    return 16;
}

/*adds rules for the donate.aspx js validation*/
    function ValidateDonate()
    {
      var name = $("input[id$='uxName']");
      var email = $("input[id$='uxEmailAddress']");
      var extradon = $("input[id$='uxExtraDonation']");
 
      name.rules("add", { required: true });
      email.rules("add", { required: true, email: true });
      extradon.rules("add", {required:true, isMoney:true, isGreaterThanZero:true});
    }

/*Used for the jquery tab funtionality*/
    $(document).ready(function(){
    $("#infoContainer > ul").tabs();
    });
