function trim(s) {
   s = s.replace(/(^\s*)|(\s*$)/gi,"");
   s = s.replace(/[ ]{2,}/gi," ");
   s = s.replace(/\n /,"\n");

   return s;
}

function chkNumeric(objName, comma, period, hyphen, space) {
	// only allow 0-9 be entered, plus any values passed
	// (can be in any order, and don't have to be comma, period, hyphen or space)
	// if all numbers allow commas, periods, hyphens or whatever,
	// just hard code it here and take out the passed parameters

	var checkOK = "0123456789" + comma + period + hyphen + space;
    var checkStr = trim(objName.value);
    var allValid = true;
    var decPoints = 0;
    var allNum = "";

    for (i=0; i<checkStr.length; i++) {
		ch = checkStr.charAt(i);

        for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
        if (ch != ",")
			allNum += ch;
   }
	if (!allValid || checkStr.length==0)
		return false;
}

function validate_digit(field, comma, period, hyphen, space){
	if (chkNumeric(field, comma, period, hyphen, space) == false)
       return false;
	else
		return true;
}

function validate_required(field) {
	var chkField = trim(field.value);

	if (chkField == "")
		return false;
	else
		return true;
}

function validate_phone(field, comma, period, hyphen, space){
	var chkField = trim(field.value);

    if (chkField.length != 8)
		return false;
   
    if (chkNumeric(field, comma, period, hyphen, space) == false)
		return false;
	else
		return true;
}

function validate_email(field) {
	var chkField = trim(field.value);
    var spos = chkField.indexOf("@");
    var dotpos = chkField.lastIndexOf(".");
    var length = chkField.length;

    // check space within the email address
    if (chkField.search(/\s/) > -1)
		return false;

	// check existing of "@" and "."
	if (spos<1 || dotpos-spos<2 || length-dotpos==1)
		return false;
	else
		return true;
}

function validate_form(thisform) {
   with (thisform){
		// Verify input in B01 field   
		if (validate_digit(B01, '', '', '', '')==false){
			alert("Please input valid quantity");
			B01.select();
			B01.focus();
			return false;
		}

		// Verify input in B02 field
        if (validate_digit(B02, '', '', '', '')==false){
			alert("Please input valid quantity");
			B02.select();
			B02.focus();
			return false;
        }

		// Verify input in B03 field
        if (validate_digit(B03, '', '', '', '')==false){
			alert("Please input valid quantity");
			B03.select();
			B03.focus();
			return false;
        }

		// Verify non-zero input in all product fields
		if (B01.value==0 && B02.value==0 && B03.value==0){
			alert("Zero quantity is not allowed.");
			B01.select();
			B01.focus();
			return false;         
		}

		// Verify minimum order
//		if (document.getElementById("Qty").innerHTML < 2) {
//			alert("Sorry, minimum order is 2 cases.");
//			B01.select();
//			B01.focus();
//			return false;         
//		}

        // Verify input in Name field
		if (validate_required(Name)==false){
			alert("Please input name.");
			Name.select();
			Name.focus();
			return false;
        }
		
		// Verify input in Contact number field
        if (validate_required(Phone)==false){
			alert("Please input contact number.");
			Phone.select();
			Phone.focus();
			return false;
		}
		if (validate_phone(Phone,'', '', '', '')==false){
			alert("Please input a valid contact number.");
			Phone.select();
			Phone.focus();
			return false;
        }
		
		// Verify input in Delivery address field
		if (validate_required(Address)==false){
			alert("Please input delivery address.");
			Address.select();
			Address.focus();
			return false;
		}

		// Verify input in Email field
		if (validate_required(Email)==false){
			alert("Please input email address.");
			Email.select();
			Email.focus();
			return false;
		}

		if (validate_email(Email)==false) {
			alert("Please input a valid e-mail address.");
			Email.select();
			Email.focus();
			return false;
		}
		
		//Assign Qty, Total, Delivery & Gtotal value
		Qty.value = document.getElementById("Qty").innerHTML;	
		Total.value = document.getElementById("Total").innerHTML;			
		Delivery.value = document.getElementById("Delivery").innerHTML;
		Gtotal.value = document.getElementById("Gtotal").innerHTML;		
	}
}

function update_grandtotal() {
    with (document.order) {
		document.getElementById("Delivery").innerHTML = 0;
		document.getElementById("Gtotal").innerHTML = 0;				

		if (eval(document.getElementById("Qty").innerHTML) > 0) {
			if ((Area.value == "HK Island") && eval(document.getElementById("Total").innerHTML)<600)
				document.getElementById("Delivery").innerHTML = 80;
			if ((Area.value =="Kowloon" || Area.value =="New Territories") && eval(document.getElementById("Total").innerHTML)<800)
				document.getElementById("Delivery").innerHTML = 200;
			if (Area.value =="Others") {
				document.getElementById("Delivery").innerHTML = "To be confirmed";
				document.getElementById("Gtotal").innerHTML = "To be confirmed";
			}
		}
		if (Area.value !="Others")
			document.getElementById("Gtotal").innerHTML = eval(document.getElementById("Total").innerHTML) + eval(document.getElementById("Delivery").innerHTML);
	}
}

function update_total() {
	with (document.order) {
		// Fix NULL input for each product
		if (B01.value =='') B01.value = 0;
		if (B02.value =='') B02.value = 0;
		if (B03.value =='') B03.value = 0;		

		if (validate_digit(B01, '', '', '', '')==true && validate_digit(B02, '', '', '', '')==true && validate_digit(B03, '', '', '', '')==true){
			document.getElementById("Qty").innerHTML = eval(B01.value) + eval(B02.value) + eval(B03.value);
			// Change color of the Quantity field to reflect minimum order
			if (eval(document.getElementById("Qty").innerHTML) < 1)
				document.getElementById("Qty").style.color="#ff0000";
			else
				document.getElementById("Qty").style.color="#008f4c";

			document.getElementById("Total").innerHTML = eval(B01.value)*208 + eval(B02.value)*148 + eval(B03.value)*218;
			update_grandtotal();
		} else {
			document.getElementById("Qty").innerHTML = 0;
			document.getElementById("Delivery").innerHTML = 0;
			document.getElementById("Total").innerHTML = 0;
			document.getElementById("Gtotal").innerHTML = 0;			
		}
	}
}
function clear_form() {
	document.getElementById("Qty").innerHTML = 0;
	document.getElementById("Qty").style.color="#ff0000";
	document.getElementById("Total").innerHTML = 0;
	document.getElementById("Delivery").innerHTML = 0;
	document.getElementById("Gtotal").innerHTML = 0;
}