// *******************************************************************************************
//	FORMCHECKS (basic)
// *******************************************************************************************
function SetFFStyle(el, status) {
	if (status == 'Bad') {
		// Backup current classname in cookie
		if (el.className != 'FormCheckBad') {		
			var expires = CookieExpires();
			Set_Cookie("ContactClassName", el.className,expires);
		}

		// Change classname to bad
		el.className = 'FormCheckBad';
	} else {
		// Get classname from cookie	
		var ContactClassName = Get_Cookie("ContactClassName");

		if (ContactClassName != 'FormCheckBad' && ContactClassName != '') {
			// Restore classname from cookie
			el.className = ContactClassName;
		} else {
			// Restore to default class
			el.className = 'FormCheckGood';
		}
	}
}

function CheckField(fieldname, message) {
	if(fieldname) {
		if (fieldname.value == "") {
			SetFFStyle(fieldname, 'Bad');
			message = '- '+message+'\n';
			return message;
		} else {
			SetFFStyle(fieldname, 'Good');
			return '';
		}
	}
}

function CheckCheckbox(fieldname, message) {
	if(fieldname) {
		if (fieldname.checked != true) {
			message = '- '+message+'\n';
			return message;
		} else {
			return '';
		}
	}
}

function CheckEmail(fieldname, message) {
	if(fieldname) {
		if (fieldname.value == "") {
			SetFFStyle(fieldname, 'Bad');
			message = '- '+message+'\n';
			return message;
		} else if (IsValidEmail(fieldname.value) == false) {
			SetFFStyle(fieldname, 'Bad');
			message = '- '+message+' is not a valid e-mailaddress !\n';
			return message;
		} else {
			SetFFStyle(fieldname, 'Good');
			return '';
		}
	}
}

function CheckNumbers(fieldname, message, numbers) {
	if(fieldname) {
		if (fieldname.value == "" || IsValidNumbers(fieldname.value, numbers) == false ) {
			SetFFStyle(fieldname, 'Bad');
			message = '- '+message+'\n';
			return message;
		} else {
			SetFFStyle(fieldname, 'Good');
			return '';
		}
	}
}

function CheckBirthday(fieldnameDay, fieldnameMonth, fieldnameYear, message) {
	if(fieldnameDay && fieldnameMonth && fieldnameYear ) {
		if (IsValidDate(fieldnameDay.value, fieldnameMonth.value, fieldnameYear.value) == false) {
			SetFFStyle(fieldnameDay, 'Bad');
			SetFFStyle(fieldnameMonth, 'Bad');
			SetFFStyle(fieldnameYear, 'Bad');			
			message = '- '+message+' is not a valid date !\n';
			return message;
		} else {
			SetFFStyle(fieldnameDay, 'Good');
			SetFFStyle(fieldnameMonth, 'Good');
			SetFFStyle(fieldnameYear, 'Good');			
			return '';
		}
	}
}

function CheckPassword(fieldname1, fieldname2, minLength) {
	if(fieldname1 && fieldname2) {
		// VALIDATE (minimum password lenght)
		if ( fieldname1.value.length < minLength || fieldname2.value.length < minLength ) {
			SetFFStyle(fieldname1, 'Bad');
			SetFFStyle(fieldname2, 'Bad');			
			message = '- The password lenght had to be at least "'+minLength+'" characters long !\n';
			return message;
		}
		// VALIDATE (are they the same)
		if (fieldname1.value != fieldname2.value) {
			SetFFStyle(fieldname1, 'Bad');
			SetFFStyle(fieldname2, 'Bad');			
			message = '- The passwords are not the same !\n';
			return message;
		} else {
			SetFFStyle(fieldname1, 'Good');
			SetFFStyle(fieldname2, 'Good');
			return '';
		}
	}
}

// *******************************************************************************************
//	FORMCHECKS (validators)
// *******************************************************************************************

function IsValidEmail(str) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(str))
		return true;
	else
		return false;
}

function IsValidNumbers(str, numbers) {
	if (numbers == "") {
		var filter  = /^([0-9])$/;
	}
	else if (numbers == 1) {
		var filter  = /^([0-9]{1})$/;
	}
	else if (numbers == 2) {
		var filter  = /^([0-9]{2})$/;
	}
	else if (numbers == 3) {
		var filter  = /^([0-9]{3})$/;
	}
	else if (numbers == 4) {
		var filter  = /^([0-9]{4})$/;
	}

	if (filter.test(str))
		return true;
	else
		return false;
}

function IsValidDate(strDay, strMonth, strYear) {

	// REMOVE (leading zeros)(day & month)
	if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
	if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)

	var month	= parseInt(strMonth);
	var day		= parseInt(strDay);
	var year	= parseInt(strYear);

	// VALIDATE (all datefields)
	if (day == "" || month == "" || year == "" || isNaN(day) || isNaN(month) || isNaN(year))
		return false;

	// SET (number of days in month)
	var daysInMonth = GetNumberOfDaysInMonth(12)

	// VALIDATE (day)
	if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > GetDaysInFebruary(year)) || day > daysInMonth[month])
		return false;

	// VALIDATE (month)
	if (strMonth.length < 1 || month < 1 || month > 12)
		return false;

	// VALIDATE (year)
	if (strYear.length != 4 || year == 0)
		return false;

	return true;
}

		function GetDaysInFebruary (year) {
			// February has 29 days in any year evenly divisible by four,
			// EXCEPT for centurial years which are not also divisible by 400.
			return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
		}
		
		function GetNumberOfDaysInMonth(n) {
			for (var i = 1; i <= n; i++) {
				this[i] = 31
				if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
				if (i==2) {this[i] = 29}
		   } 
		   return this;
		}

// *******************************************************************************************
//	FORMCHECK (contact)
// *******************************************************************************************
function FormContact() {
	// FORM
	var f = document.formcontact;

	// MESSAGE
	var header = 'The following fields are not correct:\n\n';

	// CHECK FIELDS
	var message = '';
	message += CheckField(f.name,	'Name');
	message += CheckEmail(f.email,	'E-mail');
	message += CheckField(f.message,'Message');

  	if (message != "") {
		alert(header+''+message);
		return false
	}
}

// *******************************************************************************************
//	FORMCHECK (guestbook)
// *******************************************************************************************
function FormGuestbook() {
	// FORM
	var f = document.formguestbook;

	// MESSAGE
	var header = 'The following fields are not correct:\n\n';

	// CHECK FIELDS
	var message = '';
	message += CheckField(f.name,	'Name');
	message += CheckEmail(f.email,	'E-mail');
	message += CheckField(f.message,'Message');

  	if (message != "") {
		alert(header+''+message);
		return false
	}
}

// *******************************************************************************************

