/*************************************************************
* Page: validation_functions.js v 1.0  
* Description: All functions, regular expressions used to 
* validate forms
* 
* Hospitality Recruitment - Online Strategies
* Copyright (C) 2005 Online Strategies, All rights reserved
* Email  info@hospitalityonline.co.uk
* 
*************************************************************/

/*********************************************
* Start Text box and text area validations   *
*********************************************/

// validate that the field is NOT emppty
function is_empty(field)
{
	var re = /^\s*$/;
	return re.test(field.value);
}

// Check that the field contains no illegal charactors
// Allows _ \ and . otherwise other charactors are bounced
function is_alpha_numeric(field)
{
	var re = /^[-A-Za-z0-9_\.]+$/
	return re.test(field.value);
}

// validates that the entry is formatted as an e-mail address
// Checks for a @ sign, and atlaest 1 .
function is_valid_email(field)
{
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/
	return re.test(field.value);
}

//validates that the entry is a positive or negative number
function is_valid_phone(field)
{
	var re  = /^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3,4} ?\d{0,7} ?(x|xtn|ext|extn|extension)??\.? ?\d{1,5}?$/i;
	return re.test(field.value);
}

//validates that the entry is a positive or negative number
function is_valid_phone1(field)
{
	var re  = /^[\+]?[0-9-\ ]+$/;
	return re.test(field.value);
}


//validates that the entry is a positive or negative number
function is_number(field)
{
	var re  = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	return re.test(field.value);
}

//validates that the entry is a positive or negative number
function is_numeric(field)
{
	var re  = /^[0-9]+$/;
	return re.test(field.value);
}

// Function to match 2 text fields, make sure they have data and that they match.
function match_two_fields(field1, field2)
{
	if(field1.value != field2.value)
		return false;
	else
		return true;
}

// Function to check the lenght of a field
function check_length(field)
{
	if (field.value.length < 5 && field.value.length > 12)
		return false;
}

// Function to check the extention of a field
// type (word) checks for word document
// type (img) checks for image types (jpg,gig,png)
function check_extention(field, type)
{	
	if (type == "word"){
		var re = /(.doc$)|(.rtf$)|(.docx$)/;
	}
	else if (type = "img")
	{
		var re = /(.jpe?g$)|(.gif$)|(.png$)/;
	}
	else {
		alert ("Error");
	}
	return re.test(field.value);
}
/*********************************************
* Start Checkbox Validations   				 *
*********************************************/

// Function to group a bunch of check boxes
function group_it(obj)
{
	return (typeof obj[0] != 'undefined') ? obj : [obj];
}

// Function to validate that one of the groups are checked
function is_minimum_checked(min, grp)
{
	var checked = 0, i = grp.length;
	do
		if (grp[--i].checked)
			if (++checked >= min)
				return false;
	while (i);
	return true;
}
// Function to validate that one of the groups are checked
function is_minimum_selected(min, grp)
{
	var selected = 0, i = grp.length;
	do
		if (grp[--i].selected)
			if (++selected >= min)
				return false;
	while (i);
	return true;
}

// make sure that one checkbox is checked
function is_checked(field)
{
	if (field.checked)
		return false;
	else
		return true;
}
/*********************************************
* Start Select dropdown validations   		 *
*********************************************/

// validate that the user made a selection other than default (0)
function is_first_selected(field) 
{
    if (field.selectedIndex == 0)
		return false;
}

// validate that the user has checked one of the radio buttons
function is_radio_selected(radio) {
	var valid = false;
   	for (var i = 0; i < radio.length; i++) {
		if (radio[i].checked) {
			return true;
       	}
   	}
}

// Function to change the hidden field for the "onlick" action in the multi part forms.
function change_hidden_field(field, val){
	var e = document.getElementById(field);
	// Set the attribute to change
	e.setAttribute('name' , val);
}


// Function that converts m to mm
function format_date(string){
	if (string<10)
	{
		string='0'+string;
	}
 	return string;
}

function check_date_not_empty(day,month,year){
	if((year.selectedIndex == 0 && month.selectedIndex == 0 && day.selectedIndex == 0) ||
	   (year.selectedIndex == 0 && month.selectedIndex == 0) ||
	   (year.selectedIndex == 0 && day.selectedIndex == 0) ||
	   (year.selectedIndex == 0) ||
	   (month.selectedIndex == 0) ||
	   (day.selectedIndex == 0))
	{
		return false;	
	}
	else
	return true;
}
function is_end_date_less(s_day,s_month,s_year, s_day,s_month,s_year){
	if (e_year+e_month+e_day < s_year+s_month+s_day)
	{
		return false;
	}
	else
	return true;
}

