function submitEditOptions() {
	document.details.page.value="options";
}

function submitFriend() {
	if (document.friendForm.yourName.value == "" || document.friendForm.yourName.value == "Your name" || document.friendForm.friendEmail.value == "" ) {
		alert("Please complete the email form");
		return false;
	}
	if (!ValidateEmail(document.friendForm.friendEmail)) {
      alert ("Invalid Email make sure its of the form xx@xx.xx (example: yourname@yourserver.com)");
      return false;
  }
  document.friendForm.submit();
	return true;
}

function submitNewsletter() {
	if (document.newsletterForm.newsletterEmail.value != "") {
		if (!ValidateEmail(document.newsletterForm.newsletterEmail)) {
							               alert ("Invalid Email make sure its of the form xx@xx.xx (example: yourname@yourserver.com)");
							                return false;
							                }
//						      else
//						      {
//								alert("Thank you for subscribing to our newsletter");
//								document.newsletter.submit();
//									}
		
	} else {
		alert ("Please enter your email address to subscribe to the newsletter");
		return false;
	}
  document.newsletterForm.submit();
	return true;
}

function submitEmailMe() {
	if (document.emailMeForm.emailMeEmail.value != "") {
		if (!ValidateEmail(document.emailMeForm.emailMeEmail)) {
		   alert ("Invalid Email make sure its of the form xx@xx.xx (example: yourname@yourserver.com)");
			return false;
			}		
	} else {
		alert ("Please enter your email address");
		return false;
	}
  document.emailMeForm.submit();
	return true;
}

function submitCallMe() {
	if (checkInternationalPhone(document.callMeForm.callMePhone.value)==false){
		alert("Please Enter a Valid Phone Number");
		return false;
	}
  document.callMeForm.submit();
	return true;
}

function submitLogin() {
	document.login.submit();
}

function submitPackage() {
	if (document.bookingform.num_people.value == "") {
		alert("We require the number of people who will be attending");
		return false;
	} 
	
	if (isInteger(document.bookingform.num_people.value) ==false){
		alert("We require the number of people who will be attending in numbers only");
		return false;
	}
	
	else {
		return true;
	}
}

function submitDetails() {
	if ( document.detailsForm.title.value == "" || document.detailsForm.forename.value == "" || document.detailsForm.lastname.value == "" || document.detailsForm.phone.value == "" || document.detailsForm.email.value == "" || document.detailsForm.date.value == "" ) {
		alert ("Please complete all fields");
		return false;
	}
	
	if (checkInternationalPhone(document.detailsForm.phone.value)==false){
		alert("Please Enter a Valid Phone Number");
		return false;
	}

	if (!ValidateEmail(document.detailsForm.email)) {
      alert ("Invalid Email make sure its of the form xx@xx.xx (example: yourname@yourserver.com)");
      return false;
  }
  
    if (isDate(document.detailsForm.date.value)==false) {
    alert("Please enter a calender date in the form dd/mm/yyyy");
		return false;
  }

    if (document.detailsForm.tc.checked==false) {
    alert("You must read and agree to terms and conditions");
		return false;
  }

	 else {
	 	return true;
	}
}

function submitEnquery() {
	if ( document.detailsForm.name.value == "" || document.detailsForm.phone.value == "" || document.detailsForm.email.value == "" || document.detailsForm.date.value == "" ) {
		alert ("Please complete all fields");
		return false;
	}
	
	if (checkInternationalPhone(document.detailsForm.phone.value)==false){
		alert("Please Enter a Valid Phone Number");
		return false;
	}
	
	if (!ValidateEmail(document.detailsForm.email)) {
      alert ("Invalid Email make sure its of the form xx@xx.xx (example: yourname@yourserver.com)");
      return false;
  }
  
    if (isDate(document.detailsForm.date.value)==false) {
    alert("Please enter a calender date in the form dd/mm/yyyy");
		return false;
  }

    if (document.detailsForm.tc.checked==false) {
    alert("You must read and agree to terms and conditions");
		return false;
  }

	 else {
	 	return true;
	}
}

function submitLocation() {
//VALIDATE RADIO BUTTON SELECTION
var btn = valButton(document.locationForm.location);
if (btn == null) 
	{
	alert('No location selected');
	return false;
	}
else 
	{
	return true;
	}
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}

function popUp (url) {
	day = new Date();
	id = day.getTime();
	eval ("page" + id + " = window.open(url, '" + id + "', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, width=600, height=200');");
}

function popEmailme () {
	popUp ('emailme.php');
}

function popCallme () {
	popUp ('callme.php');
}

function popForgot () {
	popUp ('forgot.php');
}

//var elm = getElementByTag('input');
//elm.focus();

function ValidateEmail(field) {
        invalidChars = " /:,;";
        str = field.value;

        // check for illegal characters

        for (i = 0; i != invalidChars.length; i++) {
                badChar = invalidChars.charAt(i);
                if (str.indexOf(badChar, 0) != -1)
                        return false;
                }

        // check for at least one '@'

        atPos = str.indexOf("@", 1);
        if (atPos == -1)
                return false;

        // make sure only one '@'

        if (str.indexOf("@", atPos+1) != -1)
                return false;

        // make sure there is at least one '.' after '@'

        periodPos = str.indexOf(".", atPos);
        if (periodPos == -1)
                return false;

        return true;

}

var minDigitsInPhoneNumber = 6;
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";

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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInPhoneNumber);
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;






// DATE VALIDATION HERE
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){
	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 date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}
