function validate(f) {
  var message;
  message = '';
  var flag;

  flag = true;
   if (f.x_first_name.value == '') {
      message +=  '* First Name empty'+ "\n";
      $('.required.firstname').fadeIn();
      flag = false;
    }
 
    if(f.x_last_name.value == '') {
      message +=  '* Last Name empty'+ "\n";
      $('.required.lastname').fadeIn();
      flag = false;
    }
    if (f.x_email.value == '') {
      message +=  '* Email empty'+ "\n";
      $('.required.email').fadeIn();
      flag = false;
    }
 
    if (f.x_ship_to_address.value == '') {
      message +=  '* Shipping Address empty'+ "\n";
      $('.required.address').fadeIn();
      flag = false;
   }

   if (f.x_ship_to_city.value == '') {
     message +=  '* Shipping City empty'+ "\n";
      $('.required.city').fadeIn();
     flag = false;
   }

   if (f.x_ship_to_zip.value == '') {
     message +=  '* Shipping Zip Code empty'+ "\n";
      $('.required.zip').fadeIn();
     flag = false;
   }

   if (f.x_ship_to_state.value == "0") {
     message +=  '* Shipping State empty'+ "\n";
      $('.required.state').fadeIn();
     flag = false;
   }

   if (f.x_phone.value == "") {
     message +=  '* Shipping Phone empty'+ "\n";
      $('.required.phone').fadeIn();
     flag = false;
   }

  if(flag) {
      document.shopcart.submit();
  } else {
      alert(message);
  }

}

function pageselectCallback(page_index, jq) {
  var new_content = $('#hiddenresult div.result:eq('+page_index+')').clone();
  $('#Searchresult').empty().append(new_content);
  return false;
}

function initPagination() {
  var num_entries = $('#hiddenresult div.result').length;
  $("#Pagination").pagination(num_entries, {
    num_edge_entries: 2,
    num_display_entries: 2,
    callback: pageselectCallback,
    items_per_page:1
  });
}

function white_space(field){
     return (field.value).replace(/ /g,'');
}

            $(document).ready(function(){      
                initPagination();
		pageselectCallback(0);


    $('.nextShipping').click(function(event){
	      $('.billing-form').hide();
	      $('.first-form').show();
	      $('.second-form').hide();

	      if(document.shopcart.infoShipping.value) {
		 document.shopcart.x_first_name.value = document.shopcart.first_name.value;
		 document.shopcart.x_last_name.value = document.shopcart.last_name.value;
		 document.shopcart.x_ship_to_address.value = document.shopcart.x_billing_to_address.value;
		 document.shopcart.x_ship_to_city.value = document.shopcart.x_billing_to_city.value;
		 document.shopcart.x_ship_to_zip.value = document.shopcart.billing_to_zip.value;
		 document.shopcart.shipping_country.value = document.shopcart.billing_country.value;
		 document.shopcart.x_ship_to_state.value = document.shopcart.billing_state.value;
	      }
    });

    $('.next').click(function(event){

	var i;
	for (i=0;i<document.shopcart.tmp_amount.length;i++){
	  if (document.shopcart.tmp_amount[i].checked) {
	    variable_index = i;
	  }
	}

	var items = new Array();
	var itemDetail = new Array();

	items[0] = 'item1<|>SKU:1 1 Burn & Cleanse + 1 Fast Cleanse + 1 extreme burn $99.99 = (SAVE $53.98 + free Shipping)<|>1 Burn & Cleanse + 1 Fast Cleanse + 1 extreme burn $99.99 = (SAVE $53.98 + free Shipping)<|>1<|>'+document.shopcart.tmp_amount[variable_index].value+'<|>NO';
	itemDetail[0] = 'SKU:1;1 Burn & Cleanse + 1 Fast Cleanse + 1 extreme burn $99.99 = (SAVE $53.98 + free Shipping);1 Burn & Cleanse + 1 Fast Cleanse + 1 extreme burn $99.99 = (SAVE $53.98 + free Shipping);1;'+document.shopcart.tmp_amount[variable_index].value;

	items[1] = 'item1<|>SKU:2 1 Burn & Cleanse + 1 Fast Cleanse $89.98 (SAVE $9.90 + free Shipping) <|>1 Burn & Cleanse + 1 Fast Cleanse $89.98 (SAVE $9.90 + free Shipping)<|>1<|>'+document.shopcart.tmp_amount[variable_index].value+'<|>NO';
	itemDetail[1] = 'SKU:2;1 Burn & Cleanse + 1 Fast Cleanse $89.98 (SAVE $9.90 + free Shipping);1 Burn & Cleanse + 1 Fast Cleanse $89.98 (SAVE $9.90 + free Shipping);1;'+document.shopcart.tmp_amount[variable_index].value;

	items[2] = 'item1<|>SKU:3 1 Extreme + 1 Fast Cleanse  $81.88 - (SAVE $ 9.09 + free Shipping)<|> 1 Extreme + 1 Fast Cleanse  $81.88 - (SAVE $ 9.09 + free Shipping)|>1<|>'+document.shopcart.tmp_amount[variable_index].value+'<|>NO';
	itemDetail[2] = 'SKU:3; 1 Extreme + 1 Fast Cleanse  $81.88 - (SAVE $ 9.09 + free Shipping);1 Extreme + 1 Fast Cleanse  $81.88 - (SAVE $ 9.09 + free Shipping);1;'+document.shopcart.tmp_amount[variable_index].value;

	items[3] = 'item1<|>SKU:7 2 Fast Cleanse + 1 (FREE) Extreme Burn $ 71.98<|> 2 Fast Cleanse + 1 (FREE) Extreme Burn $ 71.98 <|>1<|>'+document.shopcart.tmp_amount[variable_index].value+'<|>NO';
	itemDetail[3] = 'SKU:7; 2 Fast Cleanse + 1 (FREE) Extreme Burn $ 71.98; 2 Fast Cleanse + 1 (FREE) Extreme Burn $ 71.98;1;'+document.shopcart.tmp_amount[variable_index].value;

	items[4] = 'item1<|>SKU:4 1 Burn & Cleanse $62.99<|> 1 Burn & Cleanse $62.99<|>1<|>'+document.shopcart.tmp_amount[variable_index].value+'<|>NO';
	itemDetail[4] = 'SKU:4; 1 Burn & Cleanse $62.99;1 Burn &amp; Cleanse $62.99;1;'+document.shopcart.tmp_amount[variable_index].value;

	items[5] = 'item1<|>SKU:5 1 Extreme burn $54.99<|> 1 Extreme burn $54.99<|>1<|>'+document.shopcart.tmp_amount[variable_index].value+'<|>NO';
	itemDetail[5] = 'SKU:5; 1 Extreme burn $54.99; 1 Extreme burn $54.99;1;'+document.shopcart.tmp_amount[variable_index].value;

	items[6] = 'item1<|>SKU:6 1 Fast Cleanse $35.99<|> 1 Fast Cleanse $35.99<|>1<|>'+document.shopcart.tmp_amount[variable_index].value+'<|>NO';
	itemDetail[6] = 'SKU:6; 1 Fast Cleanse $35.99; 1 Fast Cleanse $35.99;1;'+document.shopcart.tmp_amount[variable_index].value;


// 	alert(items[variable_index]+' '+itemDetail[variable_index]);

	document.shopcart.x_line_item.value = items[variable_index];
	document.shopcart.item1.value = itemDetail[variable_index];

	var gst = 0;
	var pst = 0;
	var hst = 0;

	switch(document.shopcart.billing_state.value) {
	  case 0: alert('Error Carga State');
	  break;
	  case 'Alberta': gst = 0.05; pst = 0; hst = 0; // CA-Alberta Canada AB 5.0% 0.0% 5.0%
	  break;
	  case 'British Columbia': gst = 0.00; pst = 0; hst = 0.12; // CA-British Columbia Canada BC 5.0% 0.0% 12.0%
	  break;
	  case 'Manitoba': gst = 0.05; pst = 0; hst = 0; // CA-Manitoba Canada MB 5.0% 0.0% 12.0%
	  break;
	  case 'Newfoundland': gst = 0; pst = 0; hst = 0.13; // CA-Newfoudland Canada NF 0.0% 13.0% 13.0%
	  break;
	  case 'New Brunswick': gst = 0; pst = 0; hst = 0.13; // CA-New Brunswick Canada NB 0.0% 13.0% 13.0%
	  break;
	  case 'Nova Scotia':  gst = 0; pst = 0; hst = 0.15; // CA-Nova Scotia Canada NS 0.0% 13.0% 13.0%
	  break;
	  case 'Ontario': gst = 0.00; pst = 0; hst = 0.13; // Ca-Ontario Canada ON 5.0% 0.0% 13.0%
	  break;
	  case 'Prince Edward Island': gst = 0.05; pst = 0; hst = 0; // CA-Prince Edward island Canada PE 5.0% 0.0% 15.0%
	  break;
	  case 'Quebec': gst = 0.05; pst = 0; hst = 0; // CA-Quebec Canada QC 5.0% 0.0% 12.5%
	  break;
	  case 'Saskatchewan': gst = 0.05; pst = 0; hst = 0; // CA-Saskatchewan Canada SK 5.0% 0.0% 10.0%
	  break;
	  case 'Northwest Territories': gst = 0.05; pst = 0; hst = 0; // Northwest Territories Canada NT 5.0% 0.0% 5.0%
	  break;
	  case 'Nunavut': gst = 0.05; pst = 0; hst = 0; // Nunavut Canada NU 5.0% 0.0% 5.0%
	  break;
	  case 'Yukon Territory': gst = 0.05; pst = 0; hst = 0; // Yukon Canada YT 5.0% 0.0% 5.0%
	  break;
	}

	    if(!(gst === 0)) {
	      var resultgst=Math.round((document.shopcart.tmp_amount[variable_index].value*gst) * 100)/100  //returns 28.45
	      document.shopcart.gst.value = resultgst;
	      $('.gst').show();
	    } else {
	      document.shopcart.gst.value = 0;
	      $('.gst').hide();
	    }

	    if(!(pst === 0)) {
	      switch(document.shopcart.billing_state.value) {
		case 'Prince Edward Island': var resultpst=Math.round(((parseFloat(document.shopcart.tmp_amount[variable_index].value) + resultgst)*pst) * 100)/100  //returns 28.45
		break;
		case 'Quebec': var resultpst=Math.round(((parseFloat(document.shopcart.tmp_amount[variable_index].value) + resultgst)*pst) * 100)/100  //returns 28.45
		break;
		default: var resultpst=Math.round((document.shopcart.tmp_amount[variable_index].value*pst) * 100)/100  //returns 28.45
		break;
	      }

	      document.shopcart.pst.value = resultpst;
	      $('.pst').show();
	    } else {
	      document.shopcart.pst.value = 0;
	      $('.pst').hide();
	    }

	    if(!(hst === 0)) {
	      var resulthst=Math.round((document.shopcart.tmp_amount[variable_index].value*hst) * 100)/100  //returns 28.45
	      document.shopcart.hst.value = resulthst;
	      $('.hst').show();
	    } else {
	      document.shopcart.hst.value = 0;
	      $('.hst').hide();
	    }


	      $('.billing-form').hide();
	      $('.shipping').show();
	      $('.second-form').show();
	      $('.first-form').hide();

	      var amount = Math.round((parseFloat(document.shopcart.tmp_amount[variable_index].value)  + parseFloat(document.shopcart.gst.value) + parseFloat(document.shopcart.pst.value) + parseFloat(document.shopcart.hst.value)) * 100 ) / 100;

	      if(amount > 80) {
		  document.shopcart.shipping_cost.value = '0';
	      } else {
		    amount +=  parseFloat(document.shopcart.shipping_cost.value);
	      }

	      $.ajax({
		type: "POST",
		url: "hash-calculate.php",
		data: "x_login=" + document.shopcart.x_login.value + "&x_fp_sequence=" + document.shopcart.x_fp_sequence.value + "&x_fp_timestamp=" + document.shopcart.x_fp_timestamp.value + "&x_currency_code=" + document.shopcart.x_currency_code.value  + "&x_amount=" + amount,
		success: function(msg){
		  document.shopcart.x_fp_hash.value = msg;
		}
	      });

	    document.shopcart.x_amount.value = amount;

	    $('.total').show();

     });

    $('.back').click(function(event){
	      $('.billing-form').hide();
	      $('.first-form').show();
	      $('.second-form').hide();
     });


    $('.backBilling').click(function(event){
	      $('.billing-form').show();
	      $('.first-form').hide();
	      $('.second-form').hide();
     });
});


jQuery(document).ready(function($) {
    $('a[rel*=facebox]').facebox({
		width: 600,
		loading_image : 'loading.gif',
		close_image   : 'closelabel.gif'
    });
})



