/* Validate Forms */
function validate_text(this_value, this_name) 
{
	if (this_value == '') 
	{
		return this_name+'\n';
	} 
	else 
	{
		return '';
	}
}

function validate_select(this_value, this_name, this_error_choice) 
{
	if (this_value == this_error_choice) 
	{
		return this_name+'\n';
	}
	else 
	{
		return '';
	}
}

function validate_select_multiple(objField, intMin, strName) 
{
	intLenOption = objField.length;
	if (intLenOption < intMin)
	{
		return strName + '\n';	
	}	
	else
	{
		return '';
	}
}


function validate_email(this_value) {
	var re = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	if (!re.test(this_value)) {
		return 'Invalid email address \n';
	} else {
		return '';
	}
}

function with_space(strValue)
{
	var i, isWithSpace, strValue, intStrLength, chrToken

	isWithSpace  = false;
	strValue     = strValue.toString()
	intStrLength = strValue.length
	for (i = 0; i < intStrLength; i++) {
		chrToken = strValue.charAt(i)
		if (chrToken == " ") 
		{
			isWithSpace = true
		}
	}
	return isWithSpace
}

function validate_password(strPasswd, strConfirmPasswd) 
{
	strErrorMsg = ''
	if (strPasswd != strConfirmPasswd) 
	{
        strErrorMsg = 'Password doesn\'t match \n'
	}
	else if (strPasswd.length < 4)
	{
		strErrorMsg = 'Password should not be less than 4 characters \n'
	}
	else if (with_space(strPasswd))
	{
		strErrorMsg = 'Password should not be contain spaces \n'
	}
	else
	{
		strErrorMsg = ''
	}
	return strErrorMsg
}

function is_integer(value) {
	var blnInt = true;
	inputStr = value.toString();
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);			
		if ((oneChar < "0" || oneChar > "9")) {
			blnInt = false;
		}
	}
	return (blnInt);
}

function is_float(value)
{
     var template = "x.ff";
     new_value = value/1;
     if(isNaN(new_value)) 
	 {	
	 	return false
	 }
     if ((pos = value.indexOf(".")) > 0)
     {
          templatepos = template.indexOf(".");
          tmpf = template.length-templatepos-1;
          valf = value.length-pos-1;
          if(valf > tmpf) return false;
          else return true;
     }
     return true;
}

function display_error_msg(strErrorMsg) 
{
	strErrorMsg = 'Please fill out the following fields correctly : \n' + strErrorMsg;
	alert(strErrorMsg);
	return true;
}

/* Specific Form Validation */
function validate_reservation_function(objForm)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.last_name.value, 'Last Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.first_name.value, 'First Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.email.value, 'Email');
	strErrorMsg = strErrorMsg + validate_text(objForm.tel_no.value, 'Telephone');
	strErrorMsg = strErrorMsg + validate_text(objForm.address.value, 'Address');
	strErrorMsg = strErrorMsg + validate_select(objForm.room_type.options[objForm.room_type.selectedIndex].value, 'Room Type', '');
	
	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}

function validate_reservation_guest(objForm)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.last_name.value, 'Last Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.first_name.value, 'First Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.email.value, 'Email');
	strErrorMsg = strErrorMsg + validate_text(objForm.tel_no.value, 'Telephone');
	strErrorMsg = strErrorMsg + validate_text(objForm.address.value, 'Address');
	strErrorMsg = strErrorMsg + validate_select(objForm.room_type.options[objForm.room_type.selectedIndex].value, 'Room Type', '');
	
	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}

function validate_email(objForm)
{
	strErrorMsg = ''
//	strErrorMsg = strErrorMsg + validate_email(objForm.email.value);
	
	if (objForm.email.value == '') 
	{
		alert('Please enter email address!');
		return false
	}
	else
	{
		return true
	}
}
function validate_contact_us_form(objForm)
{
	strErrorMsg = ''
	strErrorMsg = strErrorMsg + validate_text(objForm.email.value, 'Email');
	strErrorMsg = strErrorMsg + validate_text(objForm.name.value, 'Name');
	strErrorMsg = strErrorMsg + validate_text(objForm.subject.value, 'Subject');
	strErrorMsg = strErrorMsg + validate_text(objForm.message.value, 'Message');

	if (strErrorMsg) 
	{
		display_error_msg(strErrorMsg);
		return false
	}
	else
	{
		return true
	}
}