
//Javascript document
/**
 * This is the javascript validating a form
 * in the Rib N Reef website.
 *
 * @author Danny Wu - Go Montreal
 * @version 06.01.20
 */

/**
*Checks for alphabetic characters
*/

//color setting in case there is an error
var txt_color = "000000";
var bg_color = "FFFF99";

function isLetter(c)
{
	//if the character passes the evaluation, it isn't a letter
	if(((c < "a") || (c > "z")) && ((c < "A") || (c > "Z")))
	{
		return false;
	}//end if
	return true;
}//end isAlpha()

/**
*Checks for alphabetic characters, hyphens and spaces
*/
function isAlpha(c)
{
	//if the character passes the evaluation, it is not a letter, hyphen or space
	if(((c < "a") || (c > "z")) && ((c < "A") || (c > "Z")) && ((c != "-") && (c != " ")))
	{
		return false;
	}//end if
	return true;
}//end isAlpha()


function isNumber(field)
{   
	with(field){
		var i;
		for (i = 0; i < value.length; i++)
		{   
			// Check that current character is number.
			var c = value.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}//end with
}

/**
*Checks for alphanumeric characters, spaces, hyphens, periods and pound
*/
function isAlphaNum(c)
{
	//if the character passes the evaluation, then it isn't an alphanumeric character, space, period or number sign
	if(((c < "0") || (c > "9")) && (((c < "a") || (c > "z")) && ((c < "A") || (c > "Z")) && (c != " ") && (c != "-") && (c != ".") && (c != "#")))
	{
		return false;
	}//end if
	return true;
}//end isAlphaNum()

/**
 * Test if variable is empty.
 */
function isEmpty(value) {
   if ((value.length==0) ||  (value==null)) {
      return true;
   }
   else { return false; }
}//end isEmpty method


/**
 * validate_required, overloaded method, which verify if
 * the field is empty.
 * @param field    field for the text
 * @param alerttxt text you want to display
 */
function validate_required(field, alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
		  {alert(alerttxt);return false}
		else {return true}
	}
}//end validate_required method

/**
 * validate_required, original method, which verify if
 * the field is empty.
 * @param field    field for the text
 */
function validate_required(field)
{
	with (field)
	{
		if (value==null||value=="")
		  {return false}
		else {return true}
	}
}//end validate_required method


/**
 * Validation of the email.
 *
 *
 * @param field    field for the text
 * @param alerttxt text you want to display
 */ 
function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
		  {alert(alerttxt);return false}
		else {return true}
	}
}//end validate_email method



/**
 * validate_form validates the whole form
 * 
 * @param thisform the form
 * @author Danny Wu :P
 */
function validate_form(thisform)
{
	var SINGLE_ERR = false; //show color of the empty fields one at the time, true will show all of them.
	var isFalse = false;
	
	with(thisform){
		
		if (validate_required(name)==false) //name
  		{
			name.focus();
			errorColor(name);
			if(SINGLE_ERR){return false;}else{isFalse = true;}
		}//end if
		
		if (validate_email(email,"Email/Courriel: 'xxx@xxx.xxx'")==false) //email
  		{
			email.focus();
			errorColor(email);
			if(SINGLE_ERR){return false;}else{isFalse = true;}
		}//end if
		
		if(!isNumber(day) || validate_required(day)==false || day.value>31){ //digit number
			day.focus();
			errorColor(day);
			if(SINGLE_ERR){return false;}else{isFalse = true;}
		}//end if*/
		
		//HOURS AND MINUTES
		if(!isNumber(hours) || validate_required(hours)==false || hours.value>23){ //digit number
			hours.focus();
			errorColor(hours);
			if(SINGLE_ERR){return false;}else{isFalse = true;}
		}//end if*/
		
		if(!isNumber(minutes) || validate_required(minutes)==false || minutes.value>59){ //digit number
			minutes.focus();
			errorColor(minutes);
			if(SINGLE_ERR){return false;}else{isFalse = true;}
		}//end if*/
		
		date = new Date();
		cur_day = date.getDate();
		cur_year = date.getYear();
		cur_month = date.getMonth() + 1;
		
		if(month.value == 0){
			errorColor(month);
			if(SINGLE_ERR){return false;}else{isFalse = true;}
		}//end if
		
		if(year.value == 0){
			errorColor(year);
			if(SINGLE_ERR){return false;}else{isFalse = true;}
		}else if (month.value <= cur_month && cur_year <= year.value){
			if(day.value == cur_day){
				alert("We need at least 24 hours of delay before processing your reservation.");
			}else if(day.value < cur_day || month.value < cur_month){
				alert("Current day: " + date+ "\n\nThe entered day is invalid.\nPlease try again.");
			}//end if
		}//end if 
		//validation of the date
		
				
		//is all of them are false then return false
		if(isFalse){
			return false;
		}

	}//end with
	
	return true;
}//** END of Function **

/**
 * This for will call this function
 * and return the fields to an error colors.
 */
function errorColor(c)
{
	c.style.color = "000000";
	c.style.backgroundColor = "FFFF99";
}//end changeColor()

/**
 * This for will call this function
 * and return the fields to their original colors.
 */
function changeColor(c)
{
	c.style.color = "000000";
	c.style.backgroundColor = "FFFFFF";
}//end changeColor()