function trim(xstr) {
  var stringVar = new String(xstr);
  while (stringVar.substring(0,1) == ' ')
    stringVar = stringVar.substring(1,stringVar.length);
  
  while (stringVar.substring(stringVar.length-1, stringVar.length) == ' ')
    stringVar = stringVar.substring(0, stringVar.length-1);
  return stringVar;
}

function isNullOrBlank(xstr) {
  return ( xstr == null || trim(xstr) == "");
}

function isValidEmail(email) {
  var emailAddress = email;
  if (window.RegExp) {
    var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var reg1 = new RegExp(reg1str);
    var reg2 = new RegExp(reg2str);
    if (!reg1.test(emailAddress) && reg2.test(emailAddress)) {
      return true;
    }
    return false;
  } else { //For older browsers :( not much of a validation
    var atPosition = emailAddress.indexOf("@");
    var dotPosition = emailAddress.indexOf(".", atPosition); 
    if ( atPosition > 0 && dotPosition > 0) {  return true; }
    return false;
          }
    }

function isValidZip(zip, country) {
  return (country == 'US' ? isNumeric(zip) && zip.length == 5 : true); 
}

function isNumeric(value) {
  var numericRegexp = new RegExp("^[0-9]+$");
  return numericRegexp.test(value);
}

function validateTourInquiryForm(theForm) {


  if (isNullOrBlank(theForm.first_name.value) ||  theForm.first_name.value.length < 2) {
    alert ("Please enter a valid value for the \"First Name\" field");
    theForm.first_name.focus();
    return false;
  }

  if (isNullOrBlank(theForm.last_name.value) ||  theForm.last_name.value.length < 2) {
    alert ("Please enter a valid value for the \"Last Name\" field");
    theForm.last_name.focus();
    return false;
  }

  if (!isValidEmail(theForm.email.value)) {
    alert("Please enter a valid \"Email\" Address");
    theForm.email.focus();
    return false;
  }

  if (isNullOrBlank(theForm.telephone.value) ||  theForm.telephone.value.length < 10) {
    alert ("Please enter a valid value for the \"telephone\" field");
    theForm.telephone.focus();
    return false;
  }

  return true;
}

function validateContactUsForm(theForm) {


  if (isNullOrBlank(theForm.first_name.value) ||  theForm.first_name.value.length < 2) {
    alert ("Please enter a valid value for the \"First Name\" field");
    theForm.first_name.focus();
    return false;
  }

  if (isNullOrBlank(theForm.last_name.value) ||  theForm.last_name.value.length < 2) {
    alert ("Please enter a valid value for the \"Last Name\" field");
    theForm.last_name.focus();
    return false;
  }

  if (isNullOrBlank(theForm.address1.value) ||  theForm.address1.value.length < 2) {
    alert ("Please enter a valid value for the \"Mailing Address\" field");
    theForm.address1.focus();
    return false;
  }

  if (isNullOrBlank(theForm.city.value) ||  theForm.city.value.length < 2) {
    alert ("Please enter a valid value for the \"City\" field");
    theForm.city.focus();
    return false;
  }

  if (isNullOrBlank(theForm.zip.value) ||  theForm.zip.value.length < 2) {
    alert ("Please enter a valid value for the \"Postal Code\" field");
    theForm.zip.focus();
    return false;
  }

  if (!isValidEmail(theForm.email.value)) {
    alert("Please enter a valid \"Email\" Address");
    theForm.email.focus();
    return false;
  }

  if (isNullOrBlank(theForm.telephone.value) ||  theForm.telephone.value.length < 10) {
    alert ("Please enter a valid value for the \"telephone\" field");
    theForm.telephone.focus();
    return false;
  }

  
  return true;
}

function validateServicesInquiryForm(theForm) {


  if (isNullOrBlank(theForm.first_name.value) ||  theForm.first_name.value.length < 2) {
    alert ("Please enter a valid value for the \"First Name\" field");
    theForm.first_name.focus();
    return false;
  }

  if (isNullOrBlank(theForm.last_name.value) ||  theForm.last_name.value.length < 2) {
    alert ("Please enter a valid value for the \"Last Name\" field");
    theForm.last_name.focus();
    return false;
  }

  if (!isValidEmail(theForm.email.value)) {
    alert("Please enter a valid \"Email\" Address");
    theForm.email.focus();
    return false;
  }

  if (isNullOrBlank(theForm.telephone.value) ||  theForm.telephone.value.length < 10) {
    alert ("Please enter a valid value for the \"telephone\" field");
    theForm.telephone.focus();
    return false;
  }

  return true;
}

function openPrintWin(url) {
  window.open(url + '?print','','scrollbars=no,menubar=yes,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no,scrollbars=yes'); 
}

function execPrint() {
  window.print();
}

