/* ----------------------------------------------------------------------
// Javascript form validation
--------------------------------------------------------------------*/

var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
var emptyString = /^\s*$/

/*-----------------------------------------
trim
-----------------------------------------*/

function trim(str) {
  return str.replace(/^\s+|\s+$/g, '');
};


/*-----------------------------------------
commonCheck
-----------------------------------------*/

var proceed = 2;  

function commonCheck(vfld, reqd) {
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server

  if (emptyString.test(vfld.value)) {
      return false;
    } else {
      return true;
  }
  return proceed;
}

/*-----------------------------------------
validatePresent
-----------------------------------------*/

function validatePresent(vfld) {
  var stat = commonCheck(vfld);
  if (stat != proceed) {
  	return stat;
  } else {
  	return true;
  }
};

/*-----------------------------------------
validateEmail
-----------------------------------------*/

function validateEmail(vfld) {
  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var email = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/
  if (!email.test(tfld)) {
    return false;
  } else {
  	return true;
  }
};


/*-----------------------------------------
validateTel
-----------------------------------------*/

function validateTel(vfld) {
  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var telnr = /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/
  if (!telnr.test(tfld)) {
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<10) {
    return false;
  } else {
    return true;
  }
};

/*-----------------------------------------
validateNumeric
-------------------------------------------*/

function validateNumeric(vfld) {	
	var tfld = trim(vfld.value);
	var num = /^[1-9]{1}$|^[1-4]{1}[0-9]{1}$|^50$/

	if (!num.test(tfld)) {
		return false;
	} else {
		return true;
	}
};

/*-----------------------------------------
validateDate
-------------------------------------------*/
/*
function validateDate(vfld) {	
	var date = new Date(vfld.value);
	var myDate = date.formatDate("m/d");

	if (myDate < "06/01" || myDate > "08/31") {
		return false;
	} else {
		return true;
	}
};
*/
/*-----------------------------------------
validateWeekend
-------------------------------------------*/

function validateWeekend(vfld) {	
	var date = new Date(vfld.value);
	var day = date.getDay();

	if (day == 0 || day == 6) {
		return false;
	} else {
		return true;
	}
};

/*-----------------------------------------
validateDateInstance
-------------------------------------------*/

function validateDateInstance(vfld) {	
	var date = new Date(vfld.value);
	var day = date.getDay();
	var myDate = date.formatDate("m/d");
	var myMsg = "";
	
	//if (myDate < "06/01" || myDate > "08/31") {
		//myMsg += "Sorry, you cannot make online reservations for this month.";
		//myMsg += "\nCall us at (***) 465-3697 for weekend reservations.";
		//return alert(myMsg);
	} else if (day == 0 || day == 6) {
		myMsg += "Sorry, you cannot make online reservations for weekends at this time.";
		myMsg += "\nCall us at (319) 465-3697 for weekend reservations.";
		return alert(myMsg);
	} else {
		return true;
	}
};
