// JavaScript Document

function refreshMainOptionSelection( productId ){
	
	
}

function trimTrailingComma(str){

	var len = str.length;
    if (str.substr(len-1,1) == ",") {
        str = str.substring(0,len-1);
    }
	
	return str ;
}

function calculateDelivery(deliveryId){
	
	var url = fullURL + "/inc/ajax_product.php" ;
	var postData = "action=calculatedelivery&deliveryid=" + deliveryId ;
		
		$.ajax({
			  url: url,
			  type: 'POST',
			  data: postData,
			  success: function(responseText){	  
				  $('#checkout_total').html(responseText) ;
				  doCufon();
			  }
			});
	
}


function getSelectedPriceItem(){	
	return $('input.main_option_item:checked').val() ;
}

function refreshExtraSelection(productId){
	
	var url = fullURL + "/inc/ajax_product.php" ;
	var postData = "action=refreshextras&productid=" + productId + "&listitemid=" + getSelectedPriceItem() ;
		
		$.ajax({
			  url: url,
			  type: 'POST',
			  data: postData,
			  success: function(responseText){	  
				  $('#product_extra_selection').html(responseText) ;
				  doCufon();
			  }
			});
		
		refreshOptionSelection(productId) ;
}


function refreshOptionSelection(productId){
	
	
	
	var url = fullURL + "/inc/ajax_product.php" ;
	var postData = "action=refreshoptions&productid=" + productId + "&listitemid=" + getSelectedPriceItem() ;
		
		$.ajax({
			  url: url,
			  type: 'POST',
			  data: postData,
			  success: function(responseText){	  
				  $('#product_option2_selection').html(responseText) ;
				  calculateTotalPrice(productId) ;
				  doCufon();
			  }
			});
}

function highLight(){
	
		$('input.main_option_item').each(function(){	
			
			var id = $(this).attr('id').replace("list_item_","") ;
			
			if($(this).attr('checked') === true){
				$('#product_option_price_' + id).addClass("selectedRow");
			}else{
				$('#product_option_price_' + id).removeClass("selectedRow");
			}
		}) ;
		
		$('#product_extra_selection input').each(function() {
																  
			var id = $(this).attr('id').replace("product_extra_","") ;
			
			if($(this).attr('checked') === true){
				$('#product_extra_row_' + id).addClass("selectedRow");
			}else{
				$('#product_extra_row_' + id).removeClass("selectedRow");
			}														  
		
		});
		
	
}

function calculateTotalPrice( productId ){
	
		highLight() ;
	
		var extras = [] ;
		var option2 = [] ;
	
		$('#product_extra_selection input:checked').each(function() {
			extras.push($(this).val());
		});
		
		$('#product_option2_selection input:radio:checked').each(function() { 																  
			option2.push($(this).val());
		});
		
		option2 = option2.join(',') ;
		extras = extras.join(',') ;
		
		var url = fullURL + "/inc/ajax_product.php" ;
		var postData = "action=calculateprice&productid=" + productId + "&listitemid=" + getSelectedPriceItem() + "&extras=" + extras + "&option2=" + option2 ;
		
		$.ajax({
			  url: url,
			  type: 'POST',
			  data: postData,
			  success: function(responseText){	  
				  $('#product_totalprice').html(responseText) ;
			  }
			});
	
}
