	function check_availability(){
		document.VSF.iAction.value = "CheckAvailability"
		document.VSF.action = "shopPO.asp"
		document.VSF.target = "_self"
		document.VSF.submit()
	}
	function proceed_with_order(){
		// Validate essential fields
		
		if (validate_fields()==true)
			{
			if ( (parseInt(document.VSF.iAvailability.value) > 0)    ) //|| (document.VSF.DeliveryMethod(0).checked=="checked" )
				{
				document.VSF.iAction.value = ""
				document.VSF.action = "processOrder2.asp"
				document.VSF.target = "_blank"
				document.VSF.submit()
				}
			else
				{
				alert("Please select another delivery date") ;
				document.VSF.iAction.value = "" ;
				document.VSF.action = "shopPO.asp" ;
				document.VSF.target = "_self" ;
				document.VSF.submit() ;
				}
			}
		else
			{
				alert ("Errors were found. Please complete marked fields") ;
				document.VSF.iAction.value = "" ;
				document.VSF.action = "" ;
			}
	}
	
	function validate_fields(){
		// check essential fields for blank: CustomerName, CustomerContactNumber, RecipientsName, RecipientsContactNumber, DeliveryAddress, iDPC, DeliveryPostCode
		var validation = true 
		var errColor = "#FFA0A0"
		var goodColor = "#FFFFFF"

		// Customer Name
		
		var s = new String("CustomerName")
		if (null_len(s,4)==false)
			{
			validation = false
			}		

		// CustomerContactNumber
		s = "CustomerContactNumber"
		if (null_len(s,9)==false)
			{
			validation = false
			}		

		//CustomerEmail
		//must have at least 1 letter an @ and then 2 letter, a dot plus 2 letters, that makes 7 the minimum length
		s = "CustomerEmail"
		if (null_len(s,7)==false)
			{
			validation = false
			}
		//must contain an @ character and at least 1 dot, note indexof() returns a -1 if not found 
		var strE = new String(document.VSF.CustomerEmail.value)
		if (strE.indexOf("@") < 1)		
			{
			validation = false
			document.VSF.CustomerEmail.style.backgroundColor=errColor 	
			}
		if (strE.indexOf(".") < 4)		
			{
			validation = false
			document.VSF.CustomerEmail.style.backgroundColor=errColor 	
			}

		// RecipientsName
		s = "RecipientsName"
		if (null_len(s,4)==false)
			{
			validation = false
			}		

		// RecipientsContactNumber
		s = "RecipientsContactNumber"
		if (null_len(s,9)==false)
			{
			validation = false
			}		

		// DeliveryAddress
		s = "DeliveryAddress"
		if (null_len(s,3)==false)
			{
			validation = false
			}		
			
			
		//Post Code
		if (document.VSF.iDPC.value == "")
			{
			validation = false		
			document.VSF.DeliveryPostCode.style.backgroundColor=errColor 	
			} 
		else
			{
			document.VSF.DeliveryPostCode.style.backgroundColor=goodColor 	
			}
			
			
		return validation;
	}

	function null_len(c_name, c_len){
		var errColor = "#FFA0A0"
		var goodColor = "#FFFFFF"
		var validation = true
	
		if (document.VSF.elements[c_name].value == "")
			{
			validation = false		
			document.VSF.elements[c_name].style.backgroundColor=errColor 	
			} 
		else
			{
			document.VSF.elements[c_name].style.backgroundColor=goodColor 	
			}

		if (String(document.VSF.elements[c_name].value).length < c_len)
			{
			validation = false		
			document.VSF.elements[c_name].style.backgroundColor=errColor 	
			} 
		else
			{
			document.VSF.elements[c_name].style.backgroundColor=goodColor 	
			}
			
		return validation ;
	
	}

	function test_date(){
	// This function gets the Server Date from a stored hidden input called ServerDate
	// tests the time to see if it is after 1:00pm, if so adds a day to the PrefferredDelivery Date
	// then it tests the new date to make sure it doesn't occur on a special day like
	// Xmas, Boxing Day, Valentines Day, New Years Day using only the day and month values
	var server_date_string = new String(document.VSF.ServerTime.value)
	var pref_del_date = new String(document.VSF.PreferredDeliveryDate.value)
	// separate Server date and time
	var n
	n = server_date_string.indexOf(" ")
	var sTime, sDate
	var sYear, sMonth, sDay, sHour, sMin, sSec

	sDate = Left(server_date_string, n)
	// if number begins with 0, this function assumes octal - wierd, so need to say it has radix of 10
	sDay = parseInt(sDate.substr(0,2), 10)
	// month goes from 0-11 in js hence -1
	sMonth = parseInt(sDate.substr(3,2), 10) - 1
	sYear = parseInt(sDate.substr(6,4), 10)

	sTime = Right(server_date_string, n +1)
	sHour = parseInt(sTime.substr(0,2), 10)
	sMin  = parseInt(sTime.substr(3,2), 10)
	sSec  = parseInt(sTime.substr(6,2), 10)
	
	var server_date_date = new Date(sYear, sMonth, sDay, sHour, sMin, sSec)
	var server_day_date = new Date(sYear, sMonth, sDay, 0, 0, 0)
	
	var pYear, pMonth, pDay
	var pDate
	pDate = pref_del_date
	pDay = parseInt(pDate.substr(0,2), 10)
	pMonth = parseInt(pDate.substr(3,2), 10) - 1
	pYear = parseInt(pDate.substr(6,4), 10)
	var pref_date_date = new Date(pYear , pMonth , pDay , 0, 0, 0)

	var myDate = pref_date_date 
	
	// now some bacic validation
	// date can't be be before today
	
	if (pref_date_date < server_day_date)
		{
		alert("Date cannot be before today")
		pref_date_date = server_day_date ; //set to server date
		}
	// Can't order today if after 13:00
	if (pref_date_date == server_day_date)
		{
		if (sHour > 12) 
			{
			alert("Same day delivery cannot be ordered after 1:00pm")
			pref_date_date.setDate(pref_date_date.getDate() + 1) // next day
			}
		}
// Test for Valentines Day
// Remember months go from 0 - 11
//	sMonth = 1
//	sDay = 14
//	pMonth = 1
//	pDay = 14
	// if today is Valentines Day, can't do Same Day delivery
	if ((sMonth == 1) && (sDay==14)) // today is Valentines Day
		{
		if ((pMonth == 1) && (pDay==14)) // preferred delivery date is Valentines Day
			{
			alert("Same day delivery is not possible on Valentines Day over the internet")
			pref_date_date.setDate(pref_date_date.getDate() + 1) // set to next day
			}
		}
		
	// Now dates like Xmas, Boxing Day and New Years Day 
	// Xmas
	if ((pMonth == 11) && (pDay == 25) )
		{
		alert("Delivery is not possible on Christmas Day or Boxing Day")
		pref_date_date.setDate(pref_date_date.getDate() + 2) // set to day after Boxing day
		}
	// Boxing Day
	if ((pMonth == 11) && (pDay == 26) )
		{
		alert("Delivery is not possible on Boxing Day")
		pref_date_date.setDate(pref_date_date.getDate() + 1) // set to day after Boxing day
		}
	// New Years Day
	if ((pMonth == 0) && (pDay == 1) )
		{
		alert("Delivery is not possible on New Years Day")
		pref_date_date.setDate(pref_date_date.getDate() + 1) // set to day after New Years Day
		}

	// Output to HTML
	var webdate, wDay, wMonth, wYear
	
	wDay = fill_zero(wDay, pref_date_date.getDate())
	wMonth = fill_zero(wMonth, pref_date_date.getMonth() + 1)
	wYear = String(pref_date_date.getFullYear())
	
	webdate = wDay  + "/" + wMonth + "/" + wYear 
	document.VSF.PreferredDeliveryDate.value = webdate
	}
	
	function fill_zero(str, n){
	// Adds a leading zero as a string to numeric values less than 10
	str = String(n) ;
	if (n < 10)
		{
		str = "0" + str ;
		}
	return str ;
	}
	
	function Left(str, n){
		if (n <= 0)
		    return "";
		else if (n > String(str).length)
		    return str;
		else
		    return String(str).substring(0,n);
	}
	function Right(str, n){
	    if (n <= 0)
	       return "";
	    else if (n > String(str).length)
	       return str;
	    else {
	       var iLen = String(str).length;
	       return String(str).substring(n, iLen);
	    }
	}
	

