/*--------------------------------------------------------------------------------
IsFilledIn :: controle of een veld een waarde bevat of niet
	INPUT:
	theField:			het veld
	theStyle:			de standaard stijl van het veld
	theErrorStyle:		de style bij foutieve waarde
	theErrorMessage:	de foutboodschap
	isRequired:			is het veld verplicht in te vullen (duh, in deze functie dus genegeerd...)
	
	OUTPUT:
	true / false
--------------------------------------------------------------------------------*/
function IsFilledIn(theField,theStyle,theErrorStyle,theErrorMessage,isRequired)
{
	var theValue = theField.value.toLowerCase();
	theField.className=theStyle;
	theField.title = "";
	
	if (theValue == "")
	{
		//--- if is empty
		theField.className=theErrorStyle;
		theField.title=theErrorMessage;
		theField.focus();
		return false;
	}
	return true;
}










/*--------------------------------------------------------------------------------
IsFilledInEmail :: controle of een veld een geldig emailadres bevat
	INPUT:
	theField:			het veld
	theStyle:			de standaard stijl van het veld
	theErrorStyle:		de style bij foutieve waarde
	theErrorMessage:	de foutboodschap
	isRequired:			is het veld verplicht in te vullen
	
	OUTPUT:
	true / false
--------------------------------------------------------------------------------*/
function IsFilledInEmail(theField,theStyle,theErrorStyle,theErrorMessage,isRequired)
{
	var theValue = theField.value.toLowerCase();
	theField.className=theStyle;
	theField.title = "";
	
	if (theValue == "" && isRequired == 0)
	{
		return true;
	}
	else
	{
		if (theValue == "" || !(IsValidEmail(theValue)))
		{
			theField.className=theErrorStyle;
			theField.title=theErrorMessage;
			theField.focus();
			return false;
		}
		else
		{
			return true;
		}
	}
}










/*--------------------------------------------------------------------------------
IsFilledInNumeric :: controle of een veld een geldig GEHEEL getal bevat
	INPUT:
	theField:			het veld
	theStyle:			de standaard stijl van het veld
	theErrorStyle:		de style bij foutieve waarde
	theErrorMessage:	de foutboodschap
	isRequired:			is het veld verplicht in te vullen
	
	OUTPUT:
	true / false
--------------------------------------------------------------------------------*/
function IsFilledInNumeric(theField,theStyle,theErrorStyle,theErrorMessage,isRequired)
{
	var theValue = theField.value.toLowerCase();
	theField.className=theStyle;
	theField.title = "";
	
	if (theValue.indexOf(",") >= 0)
	{
		theValue = theValue.replace(",",".");
		theField.value = theValue;
	}
	
	if (theValue == "" && isRequired == 0)
	{
		return true;
	}
	else
	{
		if (theValue == "" || isNaN(theValue) || theValue.indexOf(".") >= 0)
		{
			theField.className=theErrorStyle;
			theField.title=theErrorMessage;
			theField.focus();
			return false;
		}
		else
		{
			return true;
		}
	}
}









/*--------------------------------------------------------------------------------
IsFilledInDecimal :: controle of een veld een geldig getal bevat
	INPUT:
	theField:			het veld
	theStyle:			de standaard stijl van het veld
	theErrorStyle:		de style bij foutieve waarde
	theErrorMessage:	de foutboodschap
	isRequired:			is het veld verplicht in te vullen
	
	OUTPUT:
	true / false
--------------------------------------------------------------------------------*/
function IsFilledInDecimal(theField,theStyle,theErrorStyle,theErrorMessage,isRequired)
{
	var theValue = theField.value.toLowerCase();
	theField.className=theStyle;
	theField.title = "";
	
	if (theValue.indexOf(",") >= 0)
	{
		theValue = theValue.replace(",",".");
		theField.value = theValue;
	}
	
	if (theValue == "" && isRequired == 0)
	{
		return true;
	}
	else
	{
		if (theValue == "" || isNaN(theValue))
		{
			theField.className=theErrorStyle;
			theField.title=theErrorMessage;
			theField.focus();
			return false;
		}
		else
		{
			return true;
		}
	}
}








/*--------------------------------------------------------------------------------
IsPasswordsEqual :: controle of twee velden (bedoeld voor paswoorden) dezelfde waarden bevatten
	INPUT:
	theField1:			het eerste veld
	theField2:			het tweede veld
	theStyle:			de standaard stijl van het veld
	theErrorStyle:		de style bij foutieve waarde
	theErrorMessage:	de foutboodschap
	
	OUTPUT:
	true / false
--------------------------------------------------------------------------------*/
function IsPasswordsEqual(theField1,theField2,theStyle,theErrorStyle,theErrorMessage)
{
	var theValue1 = theField1.value.toLowerCase();
	var theValue2 = theField2.value.toLowerCase();
	theField1.className=theStyle;
	theField1.title = "";
	theField2.className=theStyle;
	theField2.title = "";
	
	if (theValue1 == theValue2)
	{
		return true;
	}
	else
	{
		theField1.value = "";
		theField2.value = "";
		theField1.className=theErrorStyle;
		theField2.className=theErrorStyle;
		theField2.focus();
		theField1.focus();
		alert(theErrorMessage);
		return false;
	}
}









/*--------------------------------------------------------------------------------
IsValidEmail :: controle of een emailadres een geldig adres kan zijn
	INPUT:
	sEmail
	
	OUTPUT:
	true / false
--------------------------------------------------------------------------------*/
function IsValidEmail(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}

	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
	    return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
			return false
		    }
	    }
	    return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   return false
	}

	if (len<2) {
	   return false
	}

	return true;
}









	
/*--------------------------------------------------------------------------------
Form Actions:
	SubmitTheForm	posten
	ClearTheForm	alle velden leeg maken
	ResetTheForm	formulier herstellen naar oorspronkelijke vorm
--------------------------------------------------------------------------------*/
function SubmitTheForm(theForm,theAction)
{
	theForm.action = theAction;
	theForm.submit();
}

function ClearTheForm(theForm,theBasicStyle)
{
	for (var i=0;i<theForm.elements.length;i++)
	{
		if(theForm.elements[i].type != "button" && theForm.elements[i].type != "hidden")
		{
			theForm.elements[i].value = "";
			theForm.elements[i].title = "";
			theForm.elements[i].className = theBasicStyle;
		}
	}
	theForm.elements[0].focus();
}
	
function ResetTheForm(theForm,theBasicStyle)
{
	for (var i=0;i<theForm.elements.length;i++)
	{
		if(theForm.elements[i].type != "button" && theForm.elements[i].type != "hidden")
		{
			theForm.elements[i].title = "";
			theForm.elements[i].className = theBasicStyle;
		}
	}
	theForm.reset();
	theForm.elements[0].focus();
}
