jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function(){
	
	$.preloadImages(ROOT + "/library/pezz/images/loader.gif");
	$('#addToCart #basket_cover').css('opacity',0.5);
	//$('#buttons #basket_cover2').css('opacity',0.5);
	$('#view_cart_cover').css('opacity',1);

	$('.billing_checkbox').click(function(){
		var new_billing_id = $(this).closest('TR').attr('id');
		
		$('.billing_checkbox').removeClass('green-bg');
		
		$(this).addClass('green-bg');
		document.delivery_addresses.new_billing_id.value = new_billing_id;
	});
	
	$('.delivery_checkbox').click(function(){
		var new_delivery_id = $(this).closest('TR').attr('id');
		
		$('.delivery_checkbox').removeClass('green-bg');
		
		$(this).addClass('green-bg');
		document.delivery_addresses.new_delivery_id.value = new_delivery_id;
	});
   	
	$('SELECT#cart_postage_country').click(function(){
		var uk_postage = $('INPUT#hidden_uk_postage').val();
		var merch_total = $('INPUT#hidden_merch_total').val();
		var postage_multiplier = $(this).val();
		var cart_new_postage = uk_postage * postage_multiplier;

		var cart_new_total = parseFloat(cart_new_postage) + parseFloat(merch_total);
		
		if((cart_new_postage == '0' || cart_new_postage == '0.00') && merch_total >= '10')
			{
				cart_new_postage = "FREE";
				$('SPAN#cart_postage_cost').html(cart_new_postage);
				$('SPAN#cart_overall_total').html(cart_new_total.toFixed(2));
			}
		else
			{
				$('SPAN#cart_postage_cost').html(cart_new_postage.toFixed(2));
				$('SPAN#cart_overall_total').html(cart_new_total.toFixed(2));
			}
	});
	
	$('.your_choice select').blur(function(){		
				
		var qty = document.addtocartform.add_qty.value;
		var price = document.addtocartform.add_price.value;
		var spec_id = document.addtocartform.spec_id.value;
							
		if(spec_id && qty >= 1)
			{	
				var price1 = price * qty;
				var total_price = Math.round(price1*100)/100;
				$(".your_price #total_price").html("&pound; " + total_price);
			}
		else
			{
				if(!spec_id)
					{
						$.prompt(2,"Please select a size/colour");
					}
				else
					{
						$(".your_choice #text-box-div").addClass('error-border');
						$.prompt(2,"Please select a size/colour");
					}
			}
		
	});
	
	$('.your_choice input').click(function(){		
				
		$('.your_choice div').removeClass('error-border');
	});
	
	$('#addToCart #basket_cover').click(function(){
		$.prompt(2,"Please select a size/colour");
	});		
	$('#addToCart #basket_cover2').click(function(){
		$.prompt(2,"To increase your required quantity of this product please vivit your shopping basket.");
	});		
		
		
	
	$('.select_id div div div.ins').click(function(){
		
		var details = ($(this).attr('id'));
		
		var qty_av = ($(this).find('img').attr('id'));
if(qty_av > 10)
{
qty_av = 10;
}
		
		// div id is in thye form id_size_price
		
		var myArray = details.split('_'); 
		
		var the_id = myArray[0];
		var colour = myArray[1];
		var the_size = myArray[2];
		var the_price = myArray[3];
		

		// set all divs as normally unclicked
		$('.select_id div div div').removeClass('red-border');
		$('#choices #colour_size').removeClass('error');
		$('#addToCart #basket_cover').fadeOut(300);
		
		//$('#success #success_result').hide();
		
		// add value of clicked div to hidden form field
		document.addtocartform.spec_id.value = the_id;
		document.addtocartform.add_price.value = the_price;
		document.addtocartform.add_qty.value = 1;
		
		// clear options from dropdown select
		$('SELECT#add_qty').html('');
		
		// fill drop down qty box
		for(select_no=1; select_no<=qty_av;select_no=select_no+1)
			{
				// add an option to the drop down select
				$('#add_qty').append('<option value='+select_no+'>'+select_no+'</option>');
			}

		
		
		
		if(the_size && the_size != 'One Size Only')
			{
				var colour_size_details = the_size;
			}
		else
			{
				var colour_size_details = "";
			}
			
		if(colour && colour != 'None')
			{
				if(the_size && the_size != 'One Size Only')
					{
						colour_size_details += " | "+colour;
					}
				else
					{
						var colour_size_details = colour ;
					}
			}
				
		$("#colour_size").html("<span class='uppercase black-text'>" + colour_size_details + "</span>");
		
		$("#total_price").html("&pound; " + the_price);

		// give the clicked div a different background colour
		$(this).addClass('red-border');
		
	});
	
	
	
	$("#addToCart button").click(function(){		
		
		var hasError = false;
		
		// check to see which button
		var button_pressed = $(this).attr('id');
		
		var idVal = document.addtocartform.add_id.value;
		var specidVal = document.addtocartform.spec_id.value;
		
		if(idVal == '') 
			{
				$("#choices #colour_size").html('Select above...');
				$('#choices #colour_size').addClass('error');
				$('#choices #colour_size').addClass('error-border');
				hasError = true;
			}
		else
			{
				var qtyVal = document.addtocartform.add_qty.value;
				
				if(qtyVal == '') {
					
					qtyVal = 1;
				}
				
				var priceVal = document.addtocartform.add_price.value;
				
				//if(priceVal == '') {
				//	$(".choices #total_price").html("");
				//	$('.choices #total_price').addClass('error');
				//	hasError = true;
				//}	
			}
		
		if(hasError == false) {
			
			$('#success').html("");
	
			//$(this).hide();
			$("#success").html("<div id='loading_box' style='float: left; width: 303px' class='pink-border p10 m10b green-text uppercase bold f10 center'><img src='"+ROOT+"/images/loader.gif' alt='Loading' id='loading'></div>");
			
			
			$.post(ROOT + "/shop/add_to_cart.php",
   				{ add_id: idVal, spec_id: specidVal, add_qty: qtyVal, add_price: priceVal, button_name: button_pressed },
   					function(data){
	   					
	   					
	   					
	   					if(data)
							{
								var extra_message = "<div id='error_result' style='width: 283px; float: left' class='grey-box error normal uppercase'>"+data+"</div>";
							}
						else
							{
								var extra_message = "";
							}
							
						// do the rest
						
						if(button_pressed == 'cart')
							{
								var current_cart_total = $('#subheader_cart span#subheader_cart_total').html(); 
							
								var current_cart_qty = (+$("#subheader_cart #subheader_cart_qty").html()) || 0; 
		
		
								var total_qty_current = Number(current_cart_qty) + Number(qtyVal);
								
								// find the total price of qty/prive for this product add
								var total_price = Number(qtyVal) * Number(priceVal);
								//add the current cart total to this subtotal
								var total_price_current = Number(current_cart_total) + Number(total_price);
			
								$('span#subheader_cart_qty').html(total_qty_current);  
								
								$('span#subheader_cart_total').html(total_price_current);
								
								var colour_size_details2 = $("#colour_size").html();
								
								if(colour_size_details2)
									{
										var color_size_report = "Size/colour: "+colour_size_details2+", ";
									}
								else
									{
										var color_size_report = "";
									}
								
								$("#success").html(extra_message+"<div id='success_result' style='width: 283px; float: left' class='grey-box m10b uppercase bold'><div id='float-left' style='width: 36px'><img src='" + ROOT + "/shop/images/icon_tick.gif'></div><div id='float-left' class='normal f10' style='width: 247px'>Product successfully added to your cart!<br><span class='error f12'>"+color_size_report+"Qty: +"+qtyVal+"</span></div></div>");
								
								
								// reset form values so that it'll chuck errors if they add another straight away
								document.addtocartform.add_price.value = priceVal;
								document.addtocartform.add_qty.value = "1";
								$('#total_price').html("&pound;"+priceVal);
								
								// if its one size and one colour only then don't reset the ID field
								// otherwise wipe it so they have to select a size/colour
								// it checks the existance of a hidden div only coded in the OSO display
								var check_if_its_oso = $('#oso').html();
								
								if(!check_if_its_oso)
									{
										document.addtocartform.spec_id.value = "";
										$('.select_id div div div').removeClass('red-bg');
										$('#colour_size').html("&nbsp;");
									}
									
								$('#addToCart #basket_cover').fadeIn(300);
								$('#view_cart_cover').fadeOut(300);
							}
						else	
							{
								
								var colour_size_details2 = $("#colour_size").html();
								
								if(colour_size_details2)
									{
										var color_size_report = "Size/colour: "+colour_size_details2+", ";
									}
								else
									{
										var color_size_report = "";
									}
									
								$("#success").html(extra_message);
						
								$('#addToCart #basket_cover').fadeIn(300);
								
							}
							  
   					}
				 );
		}
		
		return false;
	});		
	
	
	$("FORM#checkout_add_address_form BUTTON").click(function(){		
		
		var idVal = document.checkout_add_address_form.address_id.value;
		
		var address1Val = document.checkout_add_address_form.address1_new.value;
		var address2Val = document.checkout_add_address_form.address2_new.value;
		var address3Val = document.checkout_add_address_form.address3_new.value;
		var townVal = document.checkout_add_address_form.town_new.value;
		var countyVal = document.checkout_add_address_form.county_new.value;
		var countryVal = document.checkout_add_address_form.country_new.value;
		var postcodeVal = document.checkout_add_address_form.postcode_new.value;
		var redirect_to = document.checkout_add_address_form.redirect_to.value;
		
		var error = "";
		
		if(!address1Val)
			{
				
				error = "<ul><li>Please enter your house/building name/number.</li>";
			}
		if(!townVal)
			{
				if(!error)
					{
						error = "<ul>";
					}
				error += "<li class='list-error'>Please enter your town.</li>";
			}
		if(!countyVal)
			{
				if(!error)
					{
						error = "<ul>";
					}
				error += "<li class='list-error'>Please enter your county/province.</li>";
			}
		if(!countryVal)
			{
				if(!error)
					{
						error = "<ul>";
					}
				error += "<li class='list-error'>Please select your country.</li>";
			}
			
		if(countryVal == '826')
			{
				// this is the UK so check for postcode
				
				if(!postcodeVal)
					{
						if(!error)
							{
								error = "<ul>";
							}
						error += "<li class='list-error'>Please enter your postcode.</li>";
					}
			}
		
		if(error)
			{
				error += "</ul>";
				$.prompt(2,error);
			}
	
		if(!error) {
			
			$.post(ROOT + "/account/forms/add_address.php",
   				{ address_id: idVal, address1: address1Val, address2: address2Val, address3: address3Val, town: townVal, county: countyVal, country: countryVal, postcode: postcodeVal },
   					function(data){
	   					
	   					if(data)
							{
								$.prompt(2,data);
							}
						else
							{
								window.location.href = redirect_to+"?new="+idVal;
							}
						});
		}
		
		return false;
	});								
	
	
	$("FORM#edit_address_form BUTTON").click(function(){		
		
		var idVal = document.edit_address_form.address_id.value;
		
		var address1Val = document.edit_address_form.address1_new.value;
		var address2Val = document.edit_address_form.address2_new.value;
		var address3Val = document.edit_address_form.address3_new.value;
		var townVal = document.edit_address_form.town_new.value;
		var countyVal = document.edit_address_form.county_new.value;
		var countryVal = document.edit_address_form.country_new.value;
		var postcodeVal = document.edit_address_form.postcode_new.value;
		var redirect_to = document.edit_address_form.redirect_to.value;
		
		var error = "";
		
		if(!address1Val)
			{
				
				error = "<ul><li>Please enter your house/building name/number.</li>";
			}
		if(!townVal)
			{
				if(!error)
					{
						error = "<ul>";
					}
				error += "<li class='list-error'>Please enter your town.</li>";
			}
			
		if(!countyVal)
			{
				if(!error)
					{
						error = "<ul>";
					}
				error += "<li class='list-error'>Please enter your county/province.</li>";
			}
		if(!countryVal)
			{
				if(!error)
					{
						error = "<ul>";
					}
				error += "<li class='list-error'>Please select your country.</li>";
			}
			
		if(countryVal == '826')
			{
				// this is the UK so check for postcode
				
				if(!postcodeVal)
					{
						if(!error)
							{
								error = "<ul>";
							}
						error += "<li class='list-error'>Please enter your postcode.</li>";
					}
			}
		
		if(error)
			{
				error += "</ul>";
				$.prompt(2,error);
				alert('hh');
			}
	
		if(!error) {
			
			$.post(ROOT + "/account/forms/update_address.php",
   				{ address_id: idVal, address1: address1Val, address2: address2Val, address3: address3Val, town: townVal, county: countyVal, country: countryVal, postcode: postcodeVal },
   					function(data){
	   					
	   					if(data)
							{
		alert('ll');
								$.prompt(2,data);
							}
						else
							{
								window.location.href = redirect_to+"?new="+idVal;
							}
						});
		}
		
		return false;
	});								
	
	
	$("IMG.delete_address_button").click(function(){		
		
		var delete_address_id = $(this).attr('id');
		
		$.prompt("Delete Address?",'Are you sure you want to delete this address?',{
		    buttons:{
		        Delete:1,
		        Cancel:2
		    },
		    callback:function(buttonVal) {
			    
			    if(buttonVal == 1)
			    	{
				    	// delete it
						if(delete_address_id) {
							
							$.post(ROOT + "/account/forms/delete_address.php",
				   				{ address_id: delete_address_id },
				   					function(data){
					   					
					   					$('TR#'+delete_address_id).hide();
					   					$.prompt(1,"Address successfully deleted.");
								});
						}
				    	
			    	}
			    else
			    	{
				        // cancel it
				        // do nothing.
			        }
		    }
		});

		
		
		
		
		return false;
	});								
	
		   
});






