// *******************************************************************************************
//	COOKIES ()
// *******************************************************************************************

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function CookieExpires() {
	var today	= new Date();
	var expires = new Date(today.getTime() + (365*24*60*60*1000));
	
	return expires;
}

// *******************************************************************************************
//	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 geen geldig e-mailadres !\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 geen geldige datum !\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 = '- Het wachtwoord moet minimaal "'+minLength+'" tekens hebben !\n';
			return message;
		}
		// VALIDATE (are they the same)
		if (fieldname1.value != fieldname2.value) {
			SetFFStyle(fieldname1, 'Bad');
			SetFFStyle(fieldname2, 'Bad');			
			message = '- De wachtwoorden zijn niet gelijk !\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 (login)
// *******************************************************************************************

function FormLogin() {
	// FORM
	var f = document.formloginbar;

	// MESSAGE
	var header = 'De volgende velden zijn niet correct ingevuld:\n\n';

	// CHECK FIELDS
	var message = '';
	message += CheckEmail(f.loginemail,	'E-mailadres');
	message += CheckField(f.logincode,	'Wachtwoord');

  	if (message != "") {
		alert(header+''+message);
		return false
	}
}

// *******************************************************************************************
//	FORMCHECK (contact)
// *******************************************************************************************

function FormContact() {
	// FORM
	var f = document.form_contact;

	// MESSAGE
	var header = 'De volgende velden zijn niet correct ingevuld:\n\n';

	// CHECK FIELDS
	var message = '';
	message += CheckField(f.naam,		'Naam');
	message += CheckEmail(f.email,		'E-mailadres');
	message += CheckField(f.bericht,	'Bericht');

  	if (message != "") {
		alert(header+''+message);
		return false;
	}
}

// *******************************************************************************************
//	FORMCHECK (card)(step 1)
// *******************************************************************************************

function FormSendCardAfzender() {
	// FORM
	var f = document.form_send_afzender;

	// MESSAGE
	var header = 'De volgende velden zijn niet correct ingevuld:\n\n';

	// CHECK FIELDS
	var message = '';
	message += CheckField(f.afzender_naam,		'Naam');
	message += CheckEmail(f.afzender_email,		'E-mailadres');

  	if (message != "") {
		alert(header+''+message);
		return false;
	}
}

function FormSendCardBericht() {
	// FORM
	var f = document.form_send_bericht;

	// MESSAGE
	var header = 'De volgende velden zijn niet correct ingevuld:\n\n';

	// CHECK FIELDS
	var message = '';
	message += CheckField(f.afzender_bericht, 'Bericht');

  	if (message != "") {
		alert(header+''+message);
		return false;
	}
}

function FormSendCardOntvangers() {
	// FORM
	var f = document.form_send_ontvangers;

	// MESSAGE
	var header  = 'De volgende ontvangers zijn niet correct ingevuld:\n\n';

	// FIRST ROW (1)
	var message = '';
	message += CheckField(document.getElementById('ontvanger1naam'),  '1e Naam (verplicht)');
	message += CheckEmail(document.getElementById('ontvanger1email'), '1e E-mailadres (verplicht)');

	// ROWS ()
	for(var row=2;row <= 25;row++) {
		message += ValidateReceiver(row);
	}

  	if (message != "") {
		alert(header+''+message);
		return false;
	}
}

	function ValidateReceiver(row) {
		var message ='';
		if (document.getElementById('ontvanger'+row+'naam').value != "") {
			message += CheckField(document.getElementById('ontvanger'+row+'naam'),  row+'e Naam');
			message += CheckEmail(document.getElementById('ontvanger'+row+'email'), row+'e E-mailadres');
		} else if (document.getElementById('ontvanger'+row+'email').value != "") {
			message += CheckField(document.getElementById('ontvanger'+row+'naam'),  row+'e Naam');
			message += CheckEmail(document.getElementById('ontvanger'+row+'email'), row+'e E-mailadres');
		} else if (document.getElementById('ontvanger'+row+'naam').value == "" && document.getElementById('ontvanger'+row+'email').value == "") {
			SetFFStyle(document.getElementById('ontvanger'+row+'naam'),  'Good');
			SetFFStyle(document.getElementById('ontvanger'+row+'email'), 'Good');
		}
	
		return message;
	}

function FormSendCardVersturen() {
	// FORM
	var f = document.form_send_versturen;

	// MESSAGE
	var header = 'Geef aan dat u de gegevens goed heeft gecontroleerd !!!';

	// CHECK FIELDS
	message = CheckCheckbox(f.gegevens_controle, 'Controle');

  	if (message != "") {
		alert(header);
		return false;
	}
}

function FormValidateViewcard() {
	// FORM
	var f = document.form_bekijken;

	// MESSAGE
	var header = 'De volgende velden zijn niet correct ingevuld:\n\n';

	// CHECK FIELDS
	var message = '';
	message += CheckField(f.code1,	'Code 1');
	message += CheckField(f.code2,	'Code 2');

  	if (message != "") {
		alert(header+''+message);
		return false;
	}
}

// *******************************************************************************************

function FormTextareaChangeHeight(obj, minheight, maxheight)
{
	var height = obj.scrollHeight;

	if (document.all) /* IE */
		var height_new	= eval(height + 10);
	else
		var height_new	= height;

	// VALIDATE (min height)	
	if (height_new < minheight)
	{
		// CORRECT (height)			
		height_new = minheight;
	}

	// VALIDATE (max height)
	if (height_new > maxheight)
	{
		// CORRECT (height)	
		height_new = maxheight;
	}
	
	// UPDATE (new height)
	obj.style.height = height_new+'px';
}

function FormTextareaTrim(obj, minheight, maxheight)
{
	var txt = obj.value

	// TRIM (txt)
	txt = txt.replace(/^(\s)*/, '');
	txt = txt.replace(/(\s)*$/, '');

	// UPDATE (text)
	obj.value = txt;
	
	// VALIDATE (height)
	FormTextareaChangeHeight(obj, 10, 10); /* first make area short (important for FF) */
	FormTextareaChangeHeight(obj, minheight, maxheight);
}

function FormTrim(obj)
{
	var txt = obj.value

	// TRIM (txt)
	txt = txt.replace(/^(\s)*/, '');
	txt = txt.replace(/(\s)*$/, '');

	// UPDATE (text)
	obj.value = txt;
}

// *******************************************************************************************
