//function to delete item from cart
//takes arg of item's position in cart session array

// to be set if checkout quantities warrant obtaining a price quote via phone
var deliveryQuoteStatus = false;

function deleteFromCart (val) {
	if (window.confirm('Click OK to delete item from shopping basket')) {
		document.frmDeleteCartItem.delete_reference.value = val;
		document.frmDeleteCartItem.submit();
	}
}


//function to change attribute of shop item
//takes arg of form reference value
function changeShopAttribute (form_ref) {
	
	var the_form = eval('document.frmCartItem' + form_ref);	//get ref to current item
	
	var sold_out_array = the_form.sold_out_status.value.split(',');	//turn string into array
	var crnt_item_status = sold_out_array[(the_form.select_attribute.selectedIndex)];	//get current item value
	the_form.is_sold_out.value = parseInt(crnt_item_status);	//set is sold out status
	
	if (crnt_item_status == -1) {
	
		window.alert('The product you have selected is currently sold out');
		the_form.reset();
		
	}
	else {
	
		var ok = window.confirm ('Click OK to change product size');
		
		if (ok) {
			

			//feed relevant data into send form
			var send_form = document.frmEditCartItemSize;
			
			
			send_form.super_product_id.value = the_form.super_product_id.value;
			send_form.product_quantity.value = the_form.product_quantity.value;
			send_form.array_position.value = form_ref;
			
			send_form.prev_product_id.value = the_form.product_id.value;
			send_form.new_product_id.value = the_form.select_attribute[the_form.select_attribute.selectedIndex].value;
			
			
			send_form.submit();
		}
		else {
			the_form.reset();
		}
		
	}
	
}


//function to set button status of recalculate to on
//called when quantity is changed
function setRecalc() {
	
	//window.alert ('set recalculate status');
	roll('document.mainContent.','recalculate','','on');	//call roll function to set recalc status to on
	roll('document.mainContent.','buy_online','','off');	//call roll function to set buy online status to off
	roll('document.mainContent.','pay_cheque','','off');	//call roll function to set pay by cheque status to off
	
	//document.images.recalculate.src = '../images/butt_recalculate_on.gif';
	//document.images.buy_now.src = '../images/butt_buy_now_off.gif';
}

// Function called when country selector changed
function ChangeCountry(sel, UK_ID){
	if (sel.options[sel.selectedIndex].value == UK_ID){
		showUK();
	} else {
		showForeignCountry();
	}
}
// Function called whan any country besides the UK is selected
function showForeignCountry() {
	window.open('delivery_quotation.asp','delivery_quotation','width=460px,height=234px,scrollbars=yes,toolbar=no,menubar=no');
	roll('document.mainContent.','buy_online','','off');	//call roll function to set buy online status to off
	roll('document.mainContent.','pay_cheque','','off');	//call roll function to set pay by cheque status to off
	document.getElementById("dvPostCode").style.display = "none";
	
}
// Function called when UK is selected
function showUK(){
	document.getElementById("dvPostCode").style.display = "block";
	//setRecalc();
}
// Function called when Postcode Selector changed
function ChangePostcode(sel){
	if (sel.options[sel.selectedIndex].value == ""){
		alert('Please select your postcode');
	} else {
		setRecalc();
	}
}



//check details of all checkout items
//takes arg of whether to go on and buy or to recalculate
function checkAllForm(check_type) {
	//quantity counter
	var quantityCount = 0;

	
	//get ref to cart total form
	var checkout_form = (document.layers) ? (document.layers.mainContent.document.frmCartTotal) : (document.frmCartTotal);
	
	//variables to hold comma seperated strings describing attributes of cart items
	var str_super_product_ids, str_product_ids, str_quantitys, str_array_positions;
	str_super_product_ids = '';
	str_product_ids = '';
	str_quantitys = '';
	str_array_positions = '';
	
	//get refs to cart item forms
	var i, cart_items, updates, new_quantity, first, is_num;
	
	cart_items = new Array();
	
	updates = false; //variable holding status of whether any updates have been made
	
	first = true;
	
	//loop through all items in cart
	for (i=0;i<no_cart_objects;i++) {
		
		//make lyr_ref dependent on browser
		var lyr_ref = (document.layers) ? ('document.layers.mainContent.') : ('');
		
		//make refrenect to cart item form
		//this will allow access to relevant details
		eval('cart_items[' + i + '] = ' + lyr_ref + 'document.frmCartItem' + i);
		
		//check to see whether quantity has been changed for current cart item
		//new_quantity = cart_items[i].quantityDD[cart_items[i].quantityDD.selectedIndex].value;
		
		
		new_quantity = cart_items[i].quantityDD.value;
		is_num = (new_quantity > 0) ? true : false;
		
		//window.alert(new_quantity)
		//window.alert(is_num);
		if (!is_num)
		{
			window.alert('Please ensure you have entered valid quantities');
			return
		}
		
		
		quantityCount += new_quantity;
		
		
		//window.alert(cart_items[i].product_price.value);
		if (new_quantity != cart_items[i].product_quantity.value) {

			updates = true;	//data has been changed so form must be updated
			
			//add update info to strings
			str_super_product_ids += (first) ? (cart_items[i].super_product_id.value) : (',' + cart_items[i].super_product_id.value);
			str_product_ids += (first) ? (cart_items[i].product_id.value) : (',' + cart_items[i].product_id.value);
			str_quantitys += (first) ? (new_quantity) : (',' + new_quantity);
			str_array_positions += (first) ? (i) : (',' + i);
			
			first = false;
			
		}
	}	
	
	//phone for price quote on delivery
	if (quantityCount > no_cart_objects) {
		deliveryQuoteStatus = true;
	}

	
	//set string arrays to form elements of checkout form
	checkout_form.edit_super_product_ids.value = str_super_product_ids;
	checkout_form.edit_product_ids.value = str_product_ids;
	checkout_form.edit_quantitys.value = str_quantitys;
	checkout_form.edit_array_positions.value = str_array_positions;
	
	//check country status
	var new_country = checkout_form.slctCountry[checkout_form.slctCountry.selectedIndex].value;
	//window.alert (new_country + ' - ' + checkout_form.current_country.value);
	if (new_country != checkout_form.current_country.value) {
		updates = true;
	}
	else if (new_country == 0) {
		window.alert ('Please select your appropriate country\'s Postage & Packaging');
		return
	}
	
	var new_postalarea = checkout_form.slctPostalArea[checkout_form.slctPostalArea.selectedIndex].value;
	if (new_postalarea != checkout_form.current_postalarea.value){
		updates = true;
	}
	else if (new_postalarea == "") {
		window.alert ('Please select your Postcode');
		return
	}
			
	//if buy button has been pressed
	if (check_type == 'buy') {
		
		//check to see whether updates need to be done
		if (updates) {
		
			if (window.confirm('You have changed your order details \nClick OK to recalculate the total cost \nbefore continuing with your purchase')) {
				checkout_form.submit();
			}
		
		}
		else {
			var send_form = document.frmSendToWorldPay;
			if (send_form.M_TransactionsCount.value > 0) {
				 
				//if ((deliveryQuoteStatus) && (send_form.M_TransactionsCount.value > 4)) {
				if (checkout_form.pallet_count.value > 4) {
					//alert("When ordering 5 or more pallets, \nplease contact us to see if you may be eligible for a cheaper delivery price.")
				}
				
				send_form.submit();
				 
			}
			else window.alert('There are no items in your shopping basket to purchase');
			//window.alert('no updates to form needed');
		}
		
	}
	
	//pay by check button has been pressed
	else if (check_type == 'pay_cheque') {
	
		//check to see whether updates need to be done
		if (updates) {
		
			if (window.confirm('You have changed your order details \nClick OK to recalculate the total cost \nbefore continuing with your purchase')) {
				checkout_form.submit();
			}
		
		}
		else {
		
				//if (deliveryQuoteStatus) {
				if (checkout_form.pallet_count.value > 4) {
					//alert("When ordering 5 or more pallets, \nplease contact us to see if you may be eligible for a cheaper delivery price.")
				}
		
			var cheque_form = document.frmPayByCheck;
			
			//if items in checkout
			if (cheque_form.no_objects.value > 0) {
				
				//loop through elements in form and append to querystring
				var query_string = '';
				var first = true;
				for (i=0;i<cheque_form.no_objects.value;i++) {
					query_string += ((first) ? '' : '&') + 'item_info_' + i + '=' + escape(cheque_form['item_info_' + i].value + '\n');
					first = false
				}
				
				//add other info to query string
				query_string += '&no_objects=' + escape(cheque_form['no_objects'].value);
				query_string += '&base_price=' + escape(cheque_form['base_price'].value);
				query_string += '&total_p_and_p=' + escape(cheque_form['total_p_and_p'].value);
				query_string += '&full_price=' + escape(cheque_form['full_price'].value);
				query_string += '&no_items=' + escape(cheque_form['no_items'].value);
				query_string += '&country_name=' + escape(checkout_form['slctCountry'][checkout_form['slctCountry'].selectedIndex].text);
				
				//set width and heights for popup
				var win_width=540;
				var win_height=500;
				
				//determine x y pos of centered window base on width and height
				//called from general_functions.js
				var win_pos = getWinXY(win_width,win_height);
				
				window.open(('print_out_cheque.asp?' + query_string),'print_cheque','width='+win_width+',height=' + win_height + win_pos + ',scrollbars=yes,toolbar=yes,menubar=yes');
				
			}
			else { 
				window.alert('There are no items in your shopping basket to purchase');
			}
			//window.alert('no updates to form needed');
			
		}
	
	}
	
	//if recalculate button has been pressed
	else if (check_type == 'recalculate') {
		

		if (updates) {
			checkout_form.submit();
		}
		else {
			window.alert('You have not changed any of your purchase details');
		}
		
	}
	
}


//form initialization function to set status of forms
function checkFormStatus() {
	
	//get ref to cart total form
	var checkout_form = (document.layers) ? (document.layers.mainContent.document.frmCartTotal) : (document.frmCartTotal);
	
	var country_id = parseInt(checkout_form.current_country.value);	//get ref to current country id
	var no_products = parseInt(document.frmSendToWorldPay.M_TransactionsCount.value);	//get ref to no items in checkout
	
	//window.alert (country_id + ' ' + no_products);
	
	if ((country_id > 0) && (no_products > 0)) {
		//window.alert('all ready');
		roll('document.mainContent.','recalculate','','off');	//call roll function to set recalc status to off
		roll('document.mainContent.','buy_online','','on');	//call roll function to set buy online status to on
		roll('document.mainContent.','pay_cheque','','on');	//call roll function to set pay by cheque status to on
	}
	else {
		//window.alert('need more details');
	}
	
	if (checkout_form.pallet_count.value > 4) {
					alert("When ordering 5 or more pallets, \nplease contact us to see if you may be eligible for a cheaper delivery price.")
				}
	
}
