// JavaScript Document
	
	//changeQty( int, int, form, form_bit(int), form_bit(float), string(float), int )
	function changeQty( amount, product_code, shopping_cart, quantity, sub_total, individual_cost, product_weight ){ 
		var pq = parseInt(quantity.value, 10); 
		var order_weight = parseInt( shopping_cart.initial_weight.value );
		//the following cases should leave as soon as possible 
		if( pq == 0 && amount == -1 ){ 
			//make sure that the value does not drop below 0 
			return;
		}else if( pq >= 999 && amount == 1 ){
			//make sure that the value does not go over 999 
			return;
		}
		
		//first thing is to find out what the new qty and price difference is going to be
		var price_difference = 0;
		if( amount == -999 ){
			//special case of amount=-999 where it calls for deletion of the product
			//lets update the shopping trolly's amount
			element = document.getElementById( "st_items" );
			element.value= parseInt(element.value, 10) - pq;
			//we now need to find the amount of money to change the shopping cart figures by
			price_difference = 0 - (pq * parseFloat(individual_cost) );
			//we now change the orders weight
			order_weight = order_weight - ( product_weight * pq );
			//we can now store the new quantity of this product needed
			pq = 0;
		}else{ 
			//lets update the shopping trolly's amount
			element = document.getElementById( "st_items" );
			element.value= parseInt(element.value, 10) + amount;
			//we now need to find the amount of money to change the shopping cart figures by
			price_difference = amount * parseFloat(individual_cost);
			//we now change the orders weight
			order_weight = order_weight + ( product_weight * amount );
			//we can now store the new quantity of this product needed
			pq = pq + amount; 
		} 
		
		//now we change the value in the text box so that the user can see the new quantity of this product
		quantity.value = pq;
	
		//now we update the sub total of the boxes
		sub_total.value = "£" + (pq * parseFloat(individual_cost) ).toFixed(2);
		
		//now we need to re-evaluate the grand total(not including p&p)
		var new_sub_total = parseFloat(shopping_cart.initial_total.value) + price_difference;
		//due to some odities over the language we must make sure that the tiny numerical errors are removed from the decimal 
		new_sub_total = Math.round(new_sub_total*100)/100
		
		//now we need to re-evaluate the post and packaging
		var new_post_packaging = newPPCost( order_weight );
		
		//we need to get the discount amount
		var discount_perc = parseFloat(shopping_cart.discount_perc.value);
			
		//with all this new data we can update the shopping cart
		shopping_cart.initial_total.value = (new_sub_total).toFixed(2);
		
		var disc_amount =  new_sub_total * discount_perc;
		
		shopping_cart.initial_weight.value = order_weight;
		shopping_cart.discount_total.value = "(£" +(disc_amount).toFixed(2) +")";
		shopping_cart.post_packaging.value = "£" + (new_post_packaging).toFixed(2);		
		shopping_cart.subtotal.value = "£" + (new_sub_total).toFixed(2);		
		shopping_cart.grand_total.value = "£" + (new_sub_total + new_post_packaging - disc_amount).toFixed(2);
		
		
		//before we finish we need to alter the sc value
		var found = false;
		
		for( var i = 0; i < sc.length/6 && !found ; i++ ){
			if( product_code.toString() == sc.substring( (i*6), (i*6)+3 ) ){
				//we have found the string for aleteration
				var newString = "";
				if( pq < 10 ){
					newString = "00" + pq;
				}else if( pq < 100 ){
					newString = "0" + pq;
				}else if( pq < 1000 ){
					newString = pq;
				}else{
					newString = "999";
				}
				
				//now to change the sc
				if( newString == "000" ){
					sc = sc.replace( sc.substring( (i*6), (i*6)+6 ), "" );
					
					//as the product is no longer wanted that row must be hidden from thier view.
					element = document.getElementById( "row_" + product_code );
					element.style.display = 'none';
				}else{
					newString = sc.substring( (i*6), (i*6)+3 ) + newString;
					sc = sc.replace( sc.substring( (i*6), (i*6)+6 ), newString );
				}
				
				found = true;
			}
		}
		
		//this alters the shopping cart details for paypal
		shopping_cart.amount.value = (new_sub_total - disc_amount).toFixed(2);
		shopping_cart.handling.value = (new_post_packaging).toFixed(2);
		rebuild_pp_custom();
		shopping_cart.cancel_return.value = "http://www.hipohyfryd.co.uk/index.php?sc=" + sc;
		
		//we also need to update the shopping trolly subtotal
		element = document.getElementById( "st_subtotal" );
		element.value= (new_sub_total).toFixed(2);
		
		//finaly the cookie needs updating
		createCookie( 'sc', sc, 1 );
		
		return; 
	}
	
	//this functions verrifies the discount code		
	function verify_disc_code(){
		//build the valid_code array
		//each code contains a new array ("discount code", "discount text", "discount %age")
		var valid_codes = new Array();
		//valid_codes[0]=new Array( "vegv20", "Your 20% discount has been aproved.", 0.2 ); //until 9th November
		//valid_codes[1]=new Array( "wwwwww", "Your 25% discount has been aproved.", 0.25 );
		
		//access the users code
		var user_code_tb = document.getElementById("disc_code");
		var user_code = user_code_tb.value;
		
		//now to check the users code with the valid ones
		var found = false;
		
		for( i = 0; i < valid_codes.length; i++){
			if( valid_codes[i][0] == user_code ){
				found = true;
				
				//reveal the discount line in the shopping cart
				var disc_object = document.getElementById("disc_row");
				disc_object.style.display = '';

				//remove the discount text and replace it with success section
				disc_object = document.getElementById("disc_button_text");
				disc_object.style.display = 'none';
					
				disc_object = document.getElementById("disc_success_text");
				disc_object.style.display = '';
				
				document.getElementById("gift_success").value = valid_codes[i][1];			
				
				//we now modify the forms discount related entries
				document.shopping_cart.discount_perc.value = valid_codes[i][2];
				document.shopping_cart.discount_code.value = valid_codes[i][0];
				
				//now we work get the initial amount, discount amount, p&p(handling) and the grand total
				var initial_amount = parseFloat(document.shopping_cart.initial_total.value);
				var disc_amount =  (initial_amount * valid_codes[i][2]).toFixed(2);
				var p_and_p = parseFloat(document.shopping_cart.handling.value);
				var grand_total = initial_amount - disc_amount + p_and_p;
		
				//we now modify the information what the user sees
				document.shopping_cart.discount_total.value = "(£" +disc_amount +")";
				document.shopping_cart.grand_total.value = "£" +(grand_total).toFixed(2);
	
				//we now alter the shopping cart details for paypal
				document.shopping_cart.amount.value = (initial_amount - disc_amount).toFixed(2);
				rebuild_pp_custom();
					
			}
		}
			
		if( !found ){
			user_code_tb.value ='invalid code';
		}
			
		return;
	}
	
	//this function rebuids paypals custom value		
	function rebuild_pp_custom(){
		document.shopping_cart.custom.value = "order:"+ sc 
												+"Disc:" +document.shopping_cart.discount_code.value 
												+"Source:" +document.getElementById("source_question").value
												+"Mailing:" +document.getElementById("mailing_question").checked;
	}