// JavaScript Document

function displaycurrency (number)

{

var stringindex;
var stringlength;

number=number.toString();
stringindex=number.indexOf(".");
stringlength=number.length;
lastcharacteroffset=stringlength-1;
if ((lastcharacteroffset-stringindex) < 2  && (stringindex !=-1))
number="$"+number+"0";
if ((lastcharacteroffset-stringindex) >=2 && (stringindex !=-1))
number="$"+number;
if (stringindex ==-1)
number="$"+number+".00";



return number;


}




function calculatetotal ()

{

var number_of_people=document.booking.no_of_people.value;
var venture_index=document.booking.venture_chosen.selectedIndex;


selected_values= new Array();
selected_values[0]=120;
selected_values[1]=20;
selected_values[2]=30;
selected_values[3]=80;
selected_values[4]=250;


var totalcost;
//totalcost=(venturecost+passengerservicefare)*people;
totalcost=(selected_values[venture_index]*number_of_people);
//alert(totalcost);
totalcost=displaycurrency(totalcost);
document.forms["booking"].totalcost.value=totalcost;
return totalcost;


}


function is_empty(form_field_value)

{

invalid_counter=0;

var invalid_characters= new Array()
invalid_characters[0]=" ";
invalid_characters[1]="\n";
invalid_characters[2]="\t";
invalid_characters[3]="\b";
invalid_characters[4]="\f";



	if (form_field_value=="")
		{
		return true;
		}  
	for (var counter=0; counter<form_field_value.length; counter++)
		{
		for (var charcounter=0; charcounter<invalid_characters.length; charcounter++)
		  {
			if (form_field_value.charAt(counter)==invalid_characters[charcounter])
				invalid_counter++;
				
		  }
		}

	if (invalid_counter==form_field_value.length)
	   {
		return true;
	   }
	   
	 return false;

}



function not_phone_no(phone_number)

{
	for (var counter=0; counter<phone_number.length; counter++)
	   {
		if((!parseFloat(phone_number.charAt(counter))) && (phone_number.charAt(counter)!=" ") && (phone_number.charAt(counter)!="0"))
		{
		return true; 
	   		
		}
	   }
	return false;

}


function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/

var objRegExp =/^[a-z]{1}[\w\d\-\._]*@{1}[\w\d\-_]+\.{1}[a-z]{3}$|^[a-z]{1}[\w\d\-\._]*@{1}[\w\d\.\-_]+\.{1}[\w\d\-_]+\.{1}[a-z]{2,}$/i;
  //check for valid email
  return objRegExp.test(strValue);
  
}


function not_a_number(number)
{
for (var counter=0; counter<number.length; counter++)
	{
	if ((!parseFloat(number.charAt(counter)))  && (number.charAt(counter)!="0"))
	return true;
	
	}
return false;
}




var hoursNotice=12; 
//--------------------------------------------------------------------
//This represents the required amount of notice for bookings and can be changed to what ever you like 
//--------------------------------------------------------------------
var secondsNotice=hoursNotice*3600000; //convert to milliseconds
//--------------------------------------------------------------------

function notEnoughNotice(myStringDate)

{

var myDateObject=new Date((new Date()).getTime()); 
//this represents the current date and time
//and is used to check that the required notice is provided

myStringDate=myStringDate.split('-'); //break up the string into a form we can use to convert it into a timestamp
myStringDate[1]=myStringDate[1]-1; //In Javascript months start at 0 for January and end at 11 for December

var selectedDate= new Date((new Date(myStringDate[2],myStringDate[1],myStringDate[0],8,0,0)).getTime());

if ((selectedDate-myDateObject)<secondsNotice)
return true;

return false;

}



function validDateFormat(myStringDate)

{

var days_in_month= new Array();
days_in_month[1]=31;
days_in_month[2]=28;
days_in_month[3]=31;
days_in_month[4]=30;
days_in_month[5]=31;
days_in_month[6]=30;
days_in_month[7]=31;
days_in_month[8]=31;
days_in_month[9]=30;
days_in_month[10]=31;
days_in_month[11]=30;
days_in_month[12]=31;

var objRegExp= /^[0-9]{2}\-{1}[0-9]{2}\-{1}[0-9]{4}$/i
if (objRegExp.test(myStringDate))
	{
		myStringDate=myStringDate.split('-')
		if (((myStringDate[2] % 4 ==0) && (myStringDate[2] % 100 !=0)) || (myStringDate[2] % 400 ==0))
		days_in_month[2]=29;	
		if ((myStringDate[0] <1) || (myStringDate[0]>days_in_month[myStringDate[1]-0]))
		return false;
		if ((myStringDate[1] <1) || (myStringDate[1]>12))
		return false;
		if (myStringDate[2] <2004)
		return false;
		
		return true;	
	}
else return false;

}



function Validate()
{
   var bookingform=document.forms["booking"];
   var error = 0;

   if((bookingform.venture_chosen.selectedIndex==2) && (((not_a_number(bookingform.no_of_people.value)) || (bookingform.no_of_people.value<4)))) error =13;
   if(not_a_number(bookingform.no_of_people.value) || (bookingform.no_of_people.value<1)) error =11;
   if(is_empty(bookingform.no_of_people.value)) error =10;
   if(is_empty(bookingform.email_address.value)) error =12;
   if((!is_empty(bookingform.date_of_booking.value)) && (validDateFormat(bookingform.date_of_booking.value)) && (notEnoughNotice(bookingform.date_of_booking.value))) error = 8;
   if((!is_empty(bookingform.date_of_booking.value)) && (!validDateFormat(bookingform.date_of_booking.value))) error =7;
   if(is_empty(bookingform.date_of_booking.value)) error =6;
   if((!is_empty(bookingform.phone_no.value)) && (not_phone_no(bookingform.phone_no.value))) error =4;
   if((!is_empty(bookingform.email_address.value)) && (!validateEmail(bookingform.email_address.value))) error = 3;
   if(is_empty(bookingform.last_name.value)) error = 2;
   if(is_empty(bookingform.first_name.value)) error = 1;
	
   switch(error)
   {
   case (1):
      alert("Please enter your first name in the space provided");
	  bookingform.first_name.focus();
	  return false;
   case (2):
   	  alert("Please enter your last name in the space provided");
	  bookingform.last_name.focus();
	  return false;
   case (3):
      alert("Please enter a valid email address.");
      bookingform.email_address.focus();
	  return false;
   case (4):
      alert("Please enter a valid phone number containing only numbers and spaces.");
      bookingform.phone_no.focus();
	  return false;
   case (6):
      alert("Please enter a date for your booking");
	  bookingform.date_of_booking.focus();
	  return false;
   case (7):
   	  alert("Please enter a valid date in the format dd-mm-yyyy");
	  bookingform.date_of_booking.focus();
	  return false;
   case (8):
   	  alert("Bookings must be made at least "+hoursNotice+" hours in advance.\n"+"Please select another date");
	  bookingform.date_of_booking.focus();
	  return false;
   case (10):
      alert("Please enter the number of people for your booking");
      bookingform.no_of_people.focus();
	  return false;
   case (11):
      alert("Bookings must be made for 1 or more people ");
      bookingform.no_of_people.focus();
	  return false;
   case (12):
      alert("Please enter your email address in the space provided");
      bookingform.email_address.focus();
	  return false;
   case (13):
      alert("A minimum of 4 people are required for the Taranaki Adventure Package");
	  bookingform.no_of_people.focus();
	  return false;
   
   default:
      return true;
   }
}

