function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function validRequired2(formField,error_message)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert(error_message);
		formField.focus();
		result = false;
	}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

function permissioncheckbox(formfield)
{

   if (formfield.checked)
   {
   return true;
   }
   else
   {
   alert ("Please check the terms and conditions box to confirm that you agree on behalf of your party.");
   return false;
   }
}

function validlist(formField,error_message)
{
	var result = true;
	//alert(formField.value);
	if (formField.value == "0")
	{
		alert(error_message);
		formField.focus();
		result = false;
	}
	
	return result;
}

//----------------------------------------------------------------------------------------------
/*Just add water Scripts*/
//------------------------------------------------------------------------------------------------

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr,message){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The " + message + " format should be : dd/mm/yyyy" )
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month for the " + message)
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day for the " + message)
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear + "for the " + message)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date for the " + message)
		return false
	}
return true
}

function validDate(datefield,label)
{
	var dt=datefield;
	var message = label;
	
	if (datefield.value.length > 0)
	{
	    if (isDate(dt.value,message)==false)
	    {
		    dt.focus()
		    return false;
	    }
	}
    return true;
 }

function validRequiredDate(datefield,label)
{
	var dt=datefield;
	var message = label;
	if (isDate(dt.value,message)==false)
	    {
		    dt.focus()
		    return false;
	    }
    return true;
 }
 //make sure dates are logically chronological
 function dateComparison(date1,date2)
 {
     //alert("in function");
     var day1 = parseInt(date1.value.substring(0,2),10);
     var day2 = parseInt(date2.value.substring(0,2),10);
     var mon1 = parseInt(date1.value.substring(3,5),10);
     var mon2 = parseInt(date2.value.substring(3,5),10);
     var yr1 = parseInt(date1.value.substring(6,10),10);
     var yr2 = parseInt(date2.value.substring(6,10),10);  
    
     var startDate = new Date(yr1,mon1,day1);
     var endDate = new Date (yr2,mon2,day2);
     if (startDate>endDate)
         {
         alert("Your end date is earlier than your start date");
         date2.focus()
         return false;
         }
     return true;
 }

// end of dehydrated scripts
//----------------------------

 
//-----------------------------------------------------------------------------------------------------------------------------------------
function validateContactForm(theForm)
{
	if (!validRequired(theForm.cname,"Name"))
		return false;

	if (!validEmail(theForm.cemail,"Email Address",false))
		return false;
				
	
	return true;
}


function validateBookingForm(theForm)
{

	
	if (!validRequired(theForm.cf_name,"Name"))
		return false;
	if (!validRequired(theForm.cf_add1,"Address"))
		return false;
	if (!validRequired(theForm.cf_postcode,"Postcode"))
		return false;
	if (!validRequired(theForm.cf_telephone,"Telephone"))
		return false;
	if (!validEmail(theForm.cf_email,"Email Address",false))
		return false;
    if (!validRequiredDate(theForm.cf_datefrom,"Start Date"))
		    return false;
    if (!validRequiredDate(theForm.cf_dateto,"End Date"))
		    return false;
    if (!dateComparison(theForm.cf_datefrom,theForm.cf_dateto))
		    return false;			
	if (!permissioncheckbox(theForm.cf_termsAndConditions))
		return false;
	return true;
}



