function init() {
document.onkeydown = doOnKeyDown;
$("#loading").ajaxStart(function(){
   $(this).show();
 });
$("#loading").ajaxStop(function(){
   $(this).hide();
 });
}

function doOnKeyDown (e) {
  e = e || window.event;
  if (e.keyCode == 27){
    hideRe();
  }
}

function hideRe() {
    $('#sa').animate({ opacity: "0" }, 500);
    $('#sa').hide();
    $('#advance').hide();
}

function showPopUp(mode, idComm, type, a, b, c, d, e) {
  $('#sa').show();
  $('#sa').animate({ opacity: "0.5" }, 100);
  $('#advance').hide();
    $.ajax({
    url: sHomePath + '/_content/popup_'+type+'.php',
    type: 'POST',
    data: {mode: mode, idComm: idComm, type: type, a: a, b: b, c: c,d: d,e: e},
    success: function(request) {
        $('#advance').html(request);
        var wrapper=document.documentElement.clientHeight-134;
        document.getElementById('popupwrapper').style.maxHeight=wrapper+'px';
        $('#advance').show();
  }
}); 
}

function showCart(mode, idItem, type) {
  if (mode == "ordercart") {
    if ($("#fname").val()=="") {
        alert( "Please enter Your Name" );
        $("#fname").focus();
        return false ;
    }
    if ($("#lname").val()=="") {
        alert( "Please enter Your Last Name" );
        $("#lname").focus();
        return false ;
    }
    if ($("#address").val()=="") {
        alert( "Please enter Your Address" );
        $("#address").focus();
        return false ;
    }
    if ($("#city").val()=="") {
        alert( "Please enter Your City" );
        $("#city").focus();
        return false ;
    }
    if ($("#state").val()=="") {
        alert( "Please enter Your State" );
        $("#state").focus();
        return false ;
    }
    if ($("#zip").val()=="") {
        alert( "Please enter Your Zip" );
        $("#zip").focus();
        return false ;
    }
    if ($("#email").val()=="") {
        alert( "Please enter Your Email" );
        $("#email").focus();
        return false ;
    } else {
        if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test($("#email").val()))) {
            alert('Email address you entered appears to be in the wrong format.\nPlease use xxxx@xxx.xxx');
            $("#email").select();
        return false;
        } 
    }
    if ($("#card_name").val()=="") {
        alert( "Please enter Your Name as it apears on credit card" );
        $("#card_name").focus();
        return false ;
    }
    if ($("#card_type").val()=="") {
        alert( "Please select Card Type" );
        $("#card_type").focus();
        return false ;
    }
    if ($("#card_num").val()=="") {
        alert( "Please enter Card number" );
        $("#card_num").focus();
        return false ;
    }
    if ($("#exp_date").val()=="") {
        alert( "Please enter Expiration Date" );
        $("#exp_date").focus();
        return false ;
    }
    if ($("#card_csv").val()=="") {
        alert( "Please enter CSV code" );
        $("#card_csv").focus();
        return false ;
    }
  }

  $('#sa').show();
  $('#sa').animate({ opacity: "0.5" }, 100);
  $('#advance').hide();
    var options = {
    url:       '/cart/view_'+type+'.php',
    type:      'POST',
    data:      {mode: mode, idItem: idItem, type: type},
    success:   function(response) { 
          $('#advance').html(response);
          var wrapper=document.documentElement.clientHeight-134;
          document.getElementById('popupwrapper').style.maxHeight=wrapper+'px'; 
          $('#advance').show();
        } 
    }
    if(type=='cart'){
      $("#frmitem").ajaxSubmit(options);
    } else {
      $.ajax(options);
    }
}
function getDiscount() {
  $("#frmitem").ajaxSubmit({
    url: '/cart/discount.php',
    type: 'POST',
    success: function(response) {
      var data = eval("(" + response + ")");
        $('#dtotal').val(data.t2);
        $('#nowtotal').val(data.t1);
        t = number_format(data.t1, 2, '.', ',');
        $('#adtotal').html('$' + t);
    }
  }); 
}
function chMainImg(img) {
  $.post('/cart/mainimg.php',{img: img},function(response){
    $('#main-img').html(response);
  });
}

function showStep2(){  $('#item-left').hide();  $('#item-right').hide();  $('#steps').html('Step 2.');  $('#item-step2').animate({ opacity: 'toggle'});}
function showStep1(){  $('#item-step2').hide();  $('#steps').html('Step 1.');  $('#item-left').animate({ opacity: 'toggle' });  $('#item-right').animate({ opacity: 'toggle' });}

function CheckCalc(id)
{
  if ($("#ad"+id).attr("checked") === false) {
    $("#ad"+id).attr("checked", "checked");
  } else {
    $("#ad"+id).removeAttr("checked");
  }
  calcTotal(id);
}
function calcTotal(id)
{
  if ($("#ad"+id).attr("checked") === false) {
    var t = parseInt($('#nowtotal').val()) - parseInt($("#ad"+id).val());
  } else {
    var t = parseInt($('#nowtotal').val()) + parseInt($("#ad"+id).val());
  }
  $('#nowtotal').val(t);
  t = number_format(t, 2, '.', ',');
  $('#adtotal').html('$' + t);
}
function number_format( number, decimals, dec_point, thousands_sep )
{
  var i, j, kw, kd, km; if( isNaN(decimals = Math.abs(decimals)) ){     decimals = 2;   }   if( dec_point == undefined ){       dec_point = ",";    }   if( thousands_sep == undefined ){       thousands_sep = ".";    }   i = parseInt(number = (+number || 0).toFixed(decimals)) + "";   if( (j = i.length) > 3 ){       j = j % 3;  } else{     j = 0;  }   km = (j ? i.substr(0, j) + thousands_sep : ""); kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);  kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : ""); return km + kw + kd;
}
