//Adaptive Image - basket validation functions - Tolway Fixings

//order submit form handler
function order_submit_check(frm) {
var arrErrors=new Array();	//array of error messages
	
	if(frm.TF_mode.value == 'quote') { //check high value enquiry
		if(!confirm("You have requested to place a high value order enquiry. We will contact you to discuss your enquiry further.\n\nIf you wish to continue with this request click 'OK', otherwise click 'Cancel'.")) return false;
	}
	
	if (!IsPhoneNum(trim(frm.tel.value))) { arrErrors[arrErrors.length] = 'Valid delivery contact telephone number is required.'; frm.tel.focus(); }
	if (trim(frm.cont.value) == '') { arrErrors[arrErrors.length] = 'Delivery contact name is required.'; frm.cont.focus(); }
	
	if(frm.del_sel_id == undefined) { //cash/till
		arrErrors = check_details(frm,arrErrors);
	}
	else {
		for(i = 0; i < frm.del_sel_id.length; i++) {
			if(frm.del_sel_id[i].checked) { //selected
				if(frm.del_sel_id[i].value == '0') arrErrors = check_details(frm,arrErrors); //alternative
				else break; //preselected
			}
		} //end for
	}
	
	//invoice
	if(frm.inv_sel_id != undefined) { //not cash/till and no selection
		chkFlag = false;
		if(frm.inv_sel_id.length != undefined) {
			for(i = 0; i < frm.inv_sel_id.length; i++) {
				if(frm.inv_sel_id[i].checked) {
					chkFlag = true;
					break; //exit for
				}
			}
		}
		else {
			if(frm.inv_sel_id.checked) {
				chkFlag = true;
			}
		}
		if(!chkFlag) { arrErrors[arrErrors.length] = 'An invoice address must be selected.'; }
	}
	
	//order contact
	if (frm.ord_tel != undefined && !IsPhoneNum(trim(frm.ord_tel.value))) { arrErrors[arrErrors.length] = 'Valid order contact telephone number is required.'; frm.ord_tel.focus(); }
	if (frm.ord_name != undefined && trim(frm.ord_name.value) == '') { arrErrors[arrErrors.length] = 'Order contact name is required.'; frm.ord_name.focus(); }
	if (trim(frm.ponum.value) == '') { arrErrors[arrErrors.length] = 'Purchase order ref is required.'; frm.ponum.focus(); }
	
	// result
	if (arrErrors.length > 0) {	//errors
		alert('Please correct the following:\n\n   ' + arrErrors.reverse().join('\n   '));
  		return false;
	}
	else { //no errors
		frm.btn_place.disabled = 'disabled'; //disable submit button to stop additional clicks
		if(frm.btn_quote != undefined) frm.btn_quote.disabled = 'disabled'; //disable submit button to stop additional clicks
		if(frm.TF_mode.value == 'quote') {
			frm.btn_quote.value = 'Please wait...'; //change button (page can take a while to process)
		}
		else frm.btn_place.value = 'Please wait...'; //change button (page can take a while to process)
		return true;
	} 
	
return false;
}

//check delivery address form data
function check_details(frm,arrErrors) {

	// validate and set message (reverse order focus)
	if (!IsPcode(trim(frm.pcode.value.toUpperCase()))) { arrErrors[arrErrors.length] = 'Valid delivery postcode is required.'; frm.pcode.focus(); }
	if (trim(frm.addr3.value) == '') { arrErrors[arrErrors.length] = 'Delivery town is required.'; frm.addr3.focus(); }
	if (trim(frm.addr2.value) == '') { arrErrors[arrErrors.length] = 'Delivery street is required.'; frm.addr2.focus(); }
	if (trim(frm.addr1.value) == '') { arrErrors[arrErrors.length] = 'Delivery address is required.'; frm.addr1.focus(); }
	
	return arrErrors;
	
}


//enable the delivery inputs (for preselected delivery details)
function disable_del(frm,obj) {

	frm.pcode.disabled = 'disabled';
	frm.pcode.value = '';
	frm.addr3.disabled = 'disabled';
	frm.addr3.value = '';
	frm.addr2.disabled = 'disabled';
	frm.addr2.value = '';
	frm.addr1.disabled = 'disabled';
	frm.addr1.value = '';
	
}

//disable the delivery inputs (for preselected delivery details)
function enable_del(frm) {
	
	frm.pcode.disabled = false;
	frm.addr3.disabled = false;
	frm.addr2.disabled = false;
	frm.addr1.disabled = false;
	
}

//add to basket form submission handler - listing.php & details.php
function basket_add_check(frm) {
var arrErrors=new Array();	//array of error messages
	// validate and set message (reverse order focus)
	if (IsInt(frm.qty.value) && Number(frm.qty.value) > 0) { //is integer greater than zero
		
		if ((Number(frm.qty.value) % Number(frm.minq.value)) != 0 ) { //is not multiple of unit
			arrErrors[arrErrors.length] = 'Quantity must be a multiple of ' + frm.minq.value; frm.qty.focus();
		}
		
	}
	else {
		arrErrors[arrErrors.length] = 'Quantity must be a whole number greater than zero'; frm.qty.focus();
	}
	
	//result
	if (arrErrors.length > 0) {	//errors
		alert(arrErrors.reverse().join('\n   '));
  		return false;
	}
	else {	//no errors
		return true;
	}
return false;
}

//event - show calculated price onkeyup qty field - details.php only
function calc_price(frm) {
	if (IsNum(frm.qty.value) && Number(frm.qty.value) >= Number(frm.minq.value)) {
		if (Number(frm.qty.value) % Number(frm.minq.value) == 0) {
			frm.price.value = '\u00A3' + round_decimals((frm.qty.value*frm.your_unit.value),2); //javascript rounding bug workaround
		}
		else {
			frm.price.value = "\u00A30.00";
		}
	}
	else {
		frm.price.value = "\u00A30.00";
	}
return;
}

//check adjacent radio option
function CheckAdjRadio(obj) {
	
	obj = obj.previousSibling;
	if(obj.nodeName != "INPUT") obj = obj.previousSibling; //skip over #text node
	if(obj.nodeName == "INPUT") {
		obj.click();	//fire radio button click
	}
	
}
