// Begin number of characters limiter  
function taLimit() {
	var taObj=event.srcElement;
	if (taObj.value.length==taObj.maxLength*1) return false;
}

function taCount(visCnt) { 
	var taObj=event.srcElement;
	if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
	if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}
//  End 

function explain(msg) {
newwin = window.open('','help_window','scrollbars,top=150,left=150,width=250,height=250');
if (!newwin.opener) newwin.opener = self;
with (newwin.document)
{
open();
write('<html>');
write('<body onLoad="focus()"><p><input type="button" value="click to close" onClick=window.close()></p>');
write('<p>' + msg + '</p>');
write('</body></html>');
close();
   }
}

function openNew(url) {
	var win = window.open(url, "details", "scrollbars,top=50,left=50,alwaysRaised=1,height=350,width=400");
	win.focus();
}


function openPrint(url) {
	var win = window.open(url, "print", "scrollbars,menubar,top=50,left=50,alwaysRaised=1,height=400,width=400");
	win.focus();
}


function openFeedback(url) {
	var win = window.open(url, "feedback", "scrollbars,top=20,left=20,alwaysRaised=1,height=450,width=400,screenX=400,screenY=400,top=400,left=400");
	win.focus();
}

function openTell(url) {
	var win = window.open(url, "tellfriend", "scrollbars,top=20,left=20,alwaysRaised=1,height=475,width=400,screenX=400,screenY=400,top=400,left=400");
	win.focus();
}

var submitcount=0;

function validateForm (form) {
  for (var e = 0; e < form.elements.length; e++) {
    var el = form.elements[e];
if (el.name == 'clientstreet2' || 
		el.name == 'clientzip' || 
		el.name == 'clientaddinfo' || 
		el.name == 'clienttel' || 
		el.name == 'clientnumchild' || 
		el.name == 'clientchildages') {
		// do nothing 
	 } else {

    if (el.type == 'text' || el.type == 'textarea' ||
        el.type == 'password' || el.type == 'file' ) { 
      if (el.value == '') {
        alert('Please fill out the text field ' + el.name);
        el.focus();
        return false;
      }
    }
    else if (el.type.indexOf('select') != -1) {
      if (el.selectedIndex == 0) {
        alert('Please select a value of the select field ' + el.name);
        el.focus();
        return false;
      }
    }
    else if (el.type == 'radio') {
      var group = form[el.name];
      var checked = false;
      if (!group.length)
        checked = el.checked;
      else
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
      if (!checked) {
        alert('Please check one of the radio buttons ' + el.name);
        el.focus();
        return false;
      }
    }
    else if (el.type == 'checkbox') {
      var group = form[el.name];
      if (group.length) {
        var checked = false;
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
        if (!checked) {
          alert('Please check one of the checkboxes ' + el.name);
          el.focus();
          return false;
        }
      }
    }
   }
  }

  if (submitcount == 0)
      {
      submitcount++;
  return true;
      }
   else 
      {
      alert("This form has already been submitted.  Please wait, Thanks!");
      return false;
      }

  return true;
}

/////////////////////////////////////////////////

// booking form field validation
function validateBookConfForm (form) {
  for (var e = 0; e < form.elements.length; e++) {
    var el = form.elements[e];

if (el.name == 'booking_comments' || el.name == 'booking_deposit') {
	 } else {
    if (el.type == 'text' || el.type == 'textarea' ) { 
      if (el.value == '') {
        alert('Please fill out the text field ' + el.name);
        el.focus();
        return false;
      }
    }

    else if (el.type.indexOf('select') != -1) {
      if (el.selectedIndex == 0) {
        alert('Please select a value of the select field ' + el.name);
        el.focus();
        return false;
      }
    }

    else if (el.type == 'radio') {
      var group = form[el.name];
      var checked = false;
      if (!group.length)
        checked = el.checked;
      else
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
      if (!checked) {
        alert('Please check one of the radio buttons ' + el.name);
        el.focus();
        return false;
      }
    }
   }
  }
  return true;
}
// End

/////////////////////////////////////////////////

// custom form field validation
function validateCustomForm (form) {
  for (var e = 0; e < form.elements.length; e++) {
    var el = form.elements[e];

if (el.name == 'clientinterests' || el.name == 'clientchild' || el.name == 'clientchildages' || el.name == 'clientaddinfo' || el.name == 'clienttel' || el.name == 'clientstreet2' || el.name == 'clientzip') {
	 } else {
    if (el.type == 'text' || el.type == 'textarea' ) { 
      if (el.value == '') {
        alert('Please fill out the text field ' + el.name);
        el.focus();
        return false;
      }
    }

    else if (el.type.indexOf('select') != -1) {
      if (el.selectedIndex == 0) {
        alert('Please select a value of the select field ' + el.name);
        el.focus();
        return false;
      }
    }

    else if (el.type == 'radio') {
      var group = form[el.name];
      var checked = false;
      if (!group.length)
        checked = el.checked;
      else
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
      if (!checked) {
        alert('Please check one of the radio buttons ' + el.name);
        el.focus();
        return false;
      }
    }
   }
  }
  return true;
}
// End

function validate(field) {
var valid = "0123456789 -()+"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are accepted!");
field.focus();
field.select();
   }
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function checkEmail (field) {
  if (!isValidEmail(field.value)) {
    alert('Please enter email address in the correct format e.g. you\@you.com');
    field.focus();
    field.select();
  }
}

// checks length of text in a textarea
function checkMaxLength(field, maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
}