(function($) {
  var cache2 = [];
  // Arguments are image paths relative to the current page.
  $.preloadImages2 = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) 
    {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache2.push(cacheImage);
    }
  }
})(jQuery)

$(document).ready(function() {
 
    // Preload images
    jQuery.preloadImages2('skin1/images/fashion_mosaic/login/checkbox_not_checked.jpg', 'skin1/images/fashion_mosaic/login/checkbox_checked.jpg'); 
    // Shipping div
    if($('#new_customer #ship_box').length > 0 && $('#new_customer #shipping_eq_billing').length > 0)
    {
     $('#new_customer #shipping_eq_billing').click(function () 
     {
      if('Y'==$('#new_customer #ship2diff').val())
      {
       $('#new_customer #shipping_eq_billing').attr("src",'skin1/images/fashion_mosaic/login/checkbox_checked.jpg');
       $('#new_customer #ship2diff').val('');
       hideShipping(1);
      } else {
       $('#new_customer #shipping_eq_billing').attr("src",'skin1/images/fashion_mosaic/login/checkbox_not_checked.jpg');
       $('#new_customer #ship2diff').val('Y');
       hideShipping(0);
      }
     });  
    }
    
    hideShipping = function(hide)
    {
     if(hide)
     {
      $('#new_customer #ship_box').css('background', '#ffffff none');
      //$('#new_customer #ship_box').css('border', '1px solid red');
      $('#new_customer #ship_box input')
       .filter(function (index) {
                  return $(this).attr("type") == "text";
               }).hide();
      $('#new_customer #ship_box select').hide();
      $('#new_customer #ship_box img').hide();
      $('#new_customer #ship_box span.SmallText').hide();
     } else {
      //$('#new_customer #ship_box').show();
      $('#new_customer #ship_box').css('background', 'transparent url(images/fashion_mosaic/login/bg_input_text2.jpg) no-repeat left top');
      //$('#new_customer #ship_box').css('border', '1px solid red');
      $('#new_customer #ship_box input')
       .filter(function (index) {
                  return $(this).attr("type") == "text";
               }).show();
      $('#new_customer #ship_box select').show();
      $('#new_customer #ship_box img').show();
      $('#new_customer #ship_box span.SmallText').show();
      //Autofill the fields here
      shippingAutoFill();
     }
    }
    
    shippingAutoFill = function()
    {
     var autofill = [
      ['b_firstname','s_firstname'],
      ['b_lastname','s_lastname'],
      ['b_address','s_address'],
      ['b_address_2','s_address_2'],
      ['b_city','s_city'],
      ['b_zipcode','s_zipcode'],
      ['b_state','s_state'],
      ['b_country','s_country']      
     ];

     var billing = '';
     var shipping = '';
     for(x in autofill)
     {
      billing = '#new_customer #' + autofill[x][0]
      shipping = '#new_customer #ship_box #' + autofill[x][1];
      if($(billing).length>0)
      {
       $(shipping).val($(billing).val());
      }
     }
     
     /*
     if($('#new_customer #b_state').length>0)
     {
      //alert($('#new_customer #b_state').val());
      $('#new_customer #ship_box #s_state').val($('#new_customer #b_state').val());
      //alert($('#new_customer #ship_box #s_state').val());
     }
     */
    }
   
    if(hide_shipping)
    {
     hideShipping(1);
    }
    
});
