// JavaScript Document

var errorField = '';

function setErrorField (str) {
	if (!errorField) { errorField = str; }
}

function checkField(strField, strMessage) {
	if (!getObj(strField).value) {
		setErrorField(strField);
		return strMessage; 
		}
	else { return ''; }
}

function getObj(objName)
{
  if (document.getElementById)
  {
  	return document.getElementById(objName);
  }
  else if (document.all)
  {
	return document.all[objName];
  }
  else if (document.layers)
  {
   	return document.layers[objName];
  }
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function validateTestimonialForm () {
	var bError = false;
	var strError = "";

	var strErrorComment = 'Please check the following errors:\n\n';

	var strErrorName = ' - Your name is required.\n';
	var strErrorEmail = ' - Your email is not valid.\n';
	var strErrorPhone = ' - Your phone is required.\n';
	var strErrorAddress = ' - Your address is required.\n';
	var strErrorCity = ' - Your city is required.\n';
	var strErrorState = ' - Your state is required.\n';
	var strErrorZipCode = ' - Your zip code is required.\n';
	var strErrorSubject = ' - A subject is required.\n';
	var strErrorMessage = ' - A message is required.\n';

	if (getObj("txtfullname").value == "") { strError += strErrorName; bError = true; }
	if (isValidEmail(getObj("txtemail").value) == false) { strError += strErrorEmail; bError = true; }
	if (getObj("txtphone").value == "") { strError += strErrorPhone; bError = true; }
	if (getObj("txtaddress").value == "") { strError += strErrorAddress; bError = true; }
	if (getObj("txtcity").value == "") { strError += strErrorCity; bError = true; }
	if (getObj("ddlstate").value == "") { strError += strErrorState; bError = true; }
	if (getObj("txtzipcode").value == "") { strError += strErrorZipCode; bError = true; }
	if (getObj("txtsubject").value == "") { strError += strErrorSubject; bError = true; }
	if (getObj("txtmessage").value == "") { strError += strErrorMessage; bError = true; }

	if (bError) { alert( strErrorComment + strError); }

	return !(bError);
	}

function validateReservationForm() {
	var bError = false;
	var strError = "";
	
	//var strErrorComment = 'Please check the following errors:\n\n';
	var strErrorComment = '';

	var strErrorLocation = ' - Location is required.\n';
	var strErrorDate = ' - Date is required.\n';
	var strErrorMPD = 'Online reservations temporarily unavailable\nat our Palm Desert restaurant.\nPlease contact the restaurant directly\nat 760-340-6865.';

	if (getObj("RestaurantID").value == "") { strError += strErrorLocation; bError = true; }
	//if (getObj("RestaurantID").value == "3113") { strError += strErrorMPD; bError = true; }	
	if (getObj("txtDateEvent").value == "") { strError += strErrorDate; bError = true; }

	if (bError) { alert( strErrorComment + strError); }

	return !(bError);
	}