// give our cart some fancy effects...highlight row on hover and show delete button
function add_cart_functions(){
  $(document).ready(function() {
    
     /* add highlight to our cart rows on hover */
     $(".cart_row").hover(function() {
       $(this).addClass("cart_row_highlight");
       $(".cart_row_highlight .cart_row_delete").show();
     },function(){
       $(this).removeClass("cart_row_highlight");
       $(".cart_row_delete").hide();
     });
    
  });
}

//used to delete a cart row item
function modify_cart(url,id){
  cart_ajax_call(url, {id: id});
}

//generic ajax call used for the cart
function cart_ajax_call(url, data){

  $.ajax({ 
    type: "post",url: url,
    data: data,
    beforeSend: function(){$("#cart_loader_img").show();}, 
    complete: function(){ $("#cart_loader_img").hide();}, 
    success: function(html){$("#cart_partial").replaceWith(html)} 
  });

}

var tried_update_cart=false;
function is_submit_valid(){
	//assume we dont have any errrors
	no_errors = true;
	
	//lets set up
	checked = $('#i_understand').attr('checked')
	shipping = $('#shipping_true_value')[0].value
	
	//if they have not checked the box warn them
	if (!checked){
		$('#i_understand_error').show();
	  	no_errors = false
	}
	else
		$('#i_understand_error').hide();
		
	if (shipping == false || shipping == 0 || shipping == "" || shipping == null){//if they have not calculated shipping warn them	  
	  if(tried_update_cart) {
  		$('#no_shipping').show();
  		no_errors = false;
  	} else {
  	  
  	  tried_update_cart = true;
  	  var inputs = [];
      $(':input', $('#cart_form')).each(function() {
        inputs.push(this.name + '=' + escape(this.value));
      })
  	  $.ajax({ 
        type: "post",url: '/cart/update',
        data: inputs.join('&'),
        beforeSend: function(){$("#cart_loader_img").show();}, 
        complete: function(){ $("#cart_loader_img").hide();}, 
        success: function(html){$("#cart_partial").replaceWith(html); if(is_submit_valid())$('#cart_form').submit()} 
      }); 
      return false;
  	  
  	}
	} 
	else
		$('#no_shipping').hide();
		
	return no_errors
	
}

function is_valid_zip_state(zip, state){
	if (!(zip == "" || zip == null || zip == " ")){
		$.get('/checkout/verify_zip_state', {zip:zip, state:state},
		  function(data){
		    if (data == 'false'){
				$('#zip_state_warning').show();
			}
			else
				$('#zip_state_warning').hide();
		  });
	}
}

function agree_to_shipping_terms(){
  $.get('/cart/agree_to_shipping_terms')
}

//generic ajax call used for the checkout page
function checkout_ajax_call(url, data){

  $.ajax({ 
    type: "post",url: url,
    data: data,
    beforeSend: function(){$("#checkout_loader_img").show();}, 
    complete: function(){ $("#checkout_loader_img").hide();}, 
    success: function(html){
    	$("#checkout_partial").replaceWith(html);
	} 
  });

}

// give our cart some fancy effects...highlight row on hover and show delete button
function add_checkout_functions(){
  $(document).ready(function() {

    $('form:not(#update_cart, #cc)').submit(function() {
	    
		var inputs = [];
		$(this)
			.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
			.filter(":enabled")
			.each(function() {
			inputs.push((this.name || this.id || this.parentNode.name || this.parentNode.id)  + '=' + escape(this.value));
		});
	
	    checkout_ajax_call(this.action, inputs.join('&'))
	    
	    // by default - we'll always return false so it doesn't redirect the user.
	    return false;
	  });
    
  });
}

function edit_checkout(edit_url){
  checkout_ajax_call(edit_url,Array())
}

function show_gift_message(){
  
  //dont use gift cause omg youll kill ie
  gm = $('#gift_message');
  
  if (gm.is(":hidden")){
    gm.slideDown("slow");
    $.get('/cart/order_is_gift')
  }
  else{
	$('#message')[0].value = ''
    gm.hide();
    $.get('/cart/order_not_gift')
  }
  
}

function update_cart(){

    var inputs = [];
    $(':input', $('#cart_form')).each(function() {
      inputs.push(this.name + '=' + escape(this.value));
    })
 
    cart_ajax_call('/cart/update', inputs.join('&'))
    
}

function continue_shopping(last_location){
	
	gm = $('#gift_message');

  	if (!gm.is(":hidden")){
		//check to see if they entered a zip a gift message
		m = $('#message')[0].value
	
		//if we have something lets update our cart object
		if (m != undefined && m != null && m != '')
			$.get('/cart/update_gift_message', {message:m},function(){window.location = last_location});
		else
			window.location = last_location
	}
	else
		window.location = last_location
}