// JavaScript Document
var root
root = 'http://www.hong-kong-web-design.com/clients/relishdelivery/'

function msg(loc) {
    if(confirm("Are you sure you want to delete?"))
    {
        location = loc;
    }
}

function goback(loc) {
    location = loc;
}
function printpage()
{
window.print();
}
function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
//		elem.focus();
		return false;
	}
}

//calculate order total
/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function CalculateTotal(frm) {
    var order_total = 0

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {		
		
        // Get the current field
        form_field = frm.elements[i]

        // Get the field's name
        form_name = form_field.name		

        // Is it a "item" field?
        if (form_name.substring(0,4) == "item") {			
			
            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))
			
            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
    }

    // Display the total rounded to two decimal places
    //document.getElementById("total").value = round_decimals(order_total, 2)
	
	document.getElementById("total").value = order_total
	
	addToTotal(frm);
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
//////////////////////////////////////////////////////////////////////
/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail Address")
	   return false
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail Address")
	   return false
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail Address")
		return false
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail Address")
		return false
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail Address")
		return false
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail Address")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail Address")
		return false
	 }
	
	 return true					
	}
//validate order
function validateForm(){
	var nameID=document.orderForm.cname
	var addressID=document.orderForm.address
	var deliveryID=document.orderForm.deliveryArea
	var emailID=document.orderForm.email
	var phoneID=document.orderForm.phone
	var dateID=document.orderForm.deliveryDate
	var totalID=document.orderForm.total
	
	// Run through all the form fields
    for (var i=0; i < document.orderForm.elements.length; ++i) {
		 // Get the current field
        form_field = document.orderForm.elements[i]

        // Get the field's name
        form_name = form_field.name	
		
		if (form_name.substring(0,4) == "item") {
			if (form_field.value!=""){
				isNumeric(form_field, "Only numbers allowed, please check your quantities")
				
			}
			
		}
	}

	if ((nameID.value==null)||(nameID.value=="")){
		alert("Please enter your name")
		nameID.focus()
		return false
	}
	if ((addressID.value==null)||(addressID.value=="")){
		alert("Please enter your address")
		addressID.focus()
		return false
	}
	if ((deliveryID.value==null)||(deliveryID.value=="")){
		alert("Please select your district")
		deliveryID.focus()
		return false
	}
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	if ((phoneID.value==null)||(phoneID.value=="")){
		alert("Please enter your contact phone number")
		phoneID.focus()
		return false
	}
	if ((dateID.value==null)||(dateID.value=="")){
		alert("Please enter the delivery date")
		dateID.focus()
		return false
	}
	if (totalID.value < 500){
		alert("Minimum order is $500")
		totalID.focus()
		return false
	}

	return true
}
//validate subscribe
function validateSubscribe(){
	var nameID=document.subForm.cname
	var emailID=document.subForm.email

	if ((nameID.value==null)||(nameID.value=="")){
		alert("Please enter your name")
		nameID.focus()
		return false
	}

	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	

	return true
}
//validate order confirmation
function validateOrder()
{
	
	// set var radio_choice to false
	var radio_choice = false;

	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < document.orderConf.deliveryArea.length; counter++)
	{
	// If a radio button has been selected it will return true
	// (If not it will return false)
		if (document.orderConf.deliveryArea[counter].checked)
		{
			radio_choice = true; 
		}
	}

	if (!radio_choice)
	{
		// If there were no selections made display an alert box 
		alert("Please select a delivery area.");
		return (false);
	}
	
	return true;
}

//add delivery cost to total
function addToTotal(frm)
{
  var delivery;
  var total;
  
  var val = frm.deliveryArea.value;
  var foodTotal = frm.total.value;
  
  //var total = parseInt(val) + foodTotal;
  //alert( "foodTotal = " + typeof parseInt(foodTotal) );
 
  if ((val=="Central") || (val=="Mid Levels") || (val=="Sai Ying Pun") || (val=="Sheung Wan"))
  {
	  delivery = 0;
  }
  if ((val=="Admiralty") || (val=="Wan Chai") || (val=="Pok Fu Lam") || (val=="Kennedy Town"))
  {
	  delivery = 50;
  }
  if ((val=="Happy Valley") || (val=="Causeway Bay") || (val=="North Point"))
  {
	  delivery = 100;
  }
  if ((val=="Quarry Bay") || (val=="TST") || (val=="The Peak"))
  {
	  delivery = 200;
  }
  
  if (delivery!=null)
  {
  	frm.deliveryTotal.value = delivery;
  }
  
  if ((delivery!=null)&&(foodTotal!=null)&&(foodTotal!=""))
  {
  	total = delivery + parseInt(foodTotal);
  
  	frm.orderTotal.value = total;
  }
	
}