
// ----------------------------------------------------------------------------------------------------------
//
// The different error messages
//
// ---------------------------------------------------------------------------------------------------------- 

function Error(Language,theErrorId)
{
	var Messages = new Array();
	
	Messages["nl"] = new Array();
	Messages["fr"] = new Array();
	
	Messages["nl"]["rekeningnr1"] = "Een geldig bankrekeningnummer bestaat uit 12 cijfers. \nEnkel cijfers, geen spaties of tekens."; //
	Messages["fr"]["rekeningnr1"] = "Un numéro de compte financier valable consiste en 12 chiffres. \nUniquement les chiffres, pas de blancs ou de signes."; //
	
	Messages["nl"]["rekeningnr2"] = "%1 is geen geldig bankrekeningnummer. \nEnkel cijfers, geen spaties of tekens."; //
	Messages["fr"]["rekeningnr2"] = "%1 n'est pas un numéro de compte financier valable. \nUniquement les chiffres, pas de blancs ou de signes."; //
	
	Messages["nl"]["rekeningnr3"] = "Bankrekeningnummer of IBAN en Bic nummer zijn verplicht"; //
	Messages["fr"]["rekeningnr3"] = "Vous devez indiquer un numéro de compte ou IBAN et BIC."; //
	
	Messages["nl"]["rijksregisternr"] = "Rijksregisternummer of KBO nummer zijn verplicht"; //
	Messages["fr"]["rijksregisternr"] = "Vous devez indiquer un numéro d’identification du registre national ou numéro BCE."; //	

	Messages["nl"]["akkoord"] = "Bankrekeningnummer of IBAN en Bic nummer zijn verplicht"; //
	Messages["fr"]["akkoord"] = "Vous devez indiquer un numéro de compte ou IBAN et BIC."; //
	
	Messages["nl"]["BTW1"] = "dit is geen geldig nummer, een geldig BTW nummer bestaat uit 2 letters gevolgd door 10 cijfers.  \nGeen spaties of tekens."; //
	Messages["fr"]["BTW1"] = "n'est pas une numéro de TVA valable. Une numéro de TVA valable consiste en 2 lettres suivi de 10 chiffres. \nPas de blancs ou de signes."; //
	
	Messages["nl"]["BTW2"] = "dit is geen geldig nummer, een geldig BTW nummer bestaat uit 2 letters gevolgd door 10 cijfers.  \nGeen spaties of tekens."; //
	Messages["fr"]["BTW2"] = "n'est pas une numéro de TVA valable. Une numéro de TVA valable consiste en 2 lettres suivi de 10 chiffres. \nPas de blancs ou de signes."; //
	
	Messages["nl"]["IBAN1"] = "De lengte moet groter zijn dan 5 karakters."; //
	Messages["fr"]["IBAN1"] = "La séquence doit compter plus de 5 caractères."; //
	Messages["nl"]["IBAN2"] = "Positie 1 en 2 mogen geen cijfers zijn."; //
	Messages["fr"]["IBAN2"] = "Les positions 1 et 2 ne peuvent pas être des chiffres."; //
	Messages["nl"]["IBAN3"] = "Positie 3 en 4 mogen geen cijfers zijn."; //
	Messages["fr"]["IBAN3"] = "Les positions 3 et 4 ne peuvent pas être des chiffres."; //
	Messages["nl"]["IBAN4"] = "Enkel cijfers en hoofdletters zijn toegelaten."; //
	Messages["fr"]["IBAN4"] = "Seuls des chiffres et des lettres capitales sont autorisés."; //
	Messages["nl"]["IBAN5"] = "Geen geldig IBAN nummer"; // 
	Messages["fr"]["IBAN5"] = "Numéro IBAN invalide"; //				  
		
		
	Messages["nl"]["int"] = "Alleen cijfers zijn toegelaten!"; //
	Messages["fr"]["int"] = "Uniquement les chiffres sont autorisés!"; //
	
	Messages["nl"]["ZipCode"] = "%1 is geen geldige postcode!"; // 
	Messages["fr"]["ZipCode"] = "%1 n'est pas un code postal valable!"; //
	
	Messages["nl"]["Real"] = "Alleen waarden zoals '44' of '55.25' zijn toegelaten!"; // 
	Messages["fr"]["Real"] = "Uniquement les valeurs comme '44' ou '55.25' sont autorisées!"; //
	
	Messages["nl"]["Email"] = "Het email adres dat u invoerde is niet juist!"; // 
	Messages["fr"]["Email"] = "L'email address semble incorrect!"; //
	
	Messages["nl"]["Verplicht"] = "Gelieve een waarde in te vullen!"; //
	Messages["fr"]["Verplicht"] = "Veuillez indiquer une valeur!"; //
	
	Messages["nl"]["VerplichtCheck"] = "Gelieve de voorwaarden goed te keuren!"; //
	Messages["fr"]["VerplichtCheck"] = "Veuillez accepter les conditions!"; //
	
	Messages["nl"]["VerplichtCheckDakisolatie"] = "Gelieve uw akkoord/niet-akkoord voor het doorgeven van uw gegevens aan de Vlaamse overheid aan te duiden"; //
	Messages["fr"]["VerplichtCheckDakisolatie"] = "Veuillez marquer votre accord/désaccord pour la transmission de vos données aux autorités flamandes."; //	
	
	Messages["nl"]["Pdf"] = "U kan enkel pdf bestanden toevoegen!"; // 
	Messages["fr"]["Pdf"] = "Vous pouvez ajouter uniquement des fichiers pdf!"; //
	
	Messages["nl"]["Image"] = "U kan alleen bestanden van het type gif, jpg of png toevoegen!"; //
	Messages["fr"]["Image"] = "Vous pouvez ajouter uniquement des fichiers du type gif, jpg ou png!"; //
	
	Messages["nl"]["FileSize"] = "U kan alleen bestanden kleiner dan 4 Mb toevoegen!"; //
	Messages["fr"]["FileSize"] = "Vous pouvez ajouter uniquement des fichiers plus petite que 4Mb!"; //
	
	Messages["nl"]["OptionExist"] = "U hebt dit reeds toegevoegd!"; //
	Messages["fr"]["OptionExist"] = "Vous avez déja ajouté ce fichier!";//
	
	Messages["nl"]["OptionMax"] = "U kan maximaal 5 files toevoegen!"; //
	Messages["fr"]["OptionMax"] = "Vous pouvez ajouter maximaux 5 fichiers!"; //
	 
	Messages["nl"]["TelMail"] = "U moet een telefoonnummer of e-mail adres opgeven! \n Het is niet noodzakelijk om beide op te geven!"; //
	Messages["fr"]["TelMail"] = "Vous devez indiquer un numéro de téléphone ou un e-mail adresse! \n il n'est pas nécessaire de donner les deux!"; //
	
	Messages["nl"]["Titel"] = "De volgende fouten moeten worden gecorrigeerd:"; //
	Messages["fr"]["Titel"] = "Les erreurs suivantes doivent etre corrigees:"; //
	
	return Messages[Language][theErrorId].toString();
}

function GetMessage( theErrorId, Param )
{

	// Retrieve the current language --> "nl" or "fr"
	var Temp = document.location.pathname;
		
	//if ( Temp.charAt(0) == "/" ) Temp = Temp.slice(1);
		
	//var Language = Temp.split("/")[0].toLowerCase();
	
	// Retrieve the corresponding error message	
	// The next part could be done quicker and easier like this:
	//
	// var Message = Error(Language,theErrorId);
	//
	// But the comeet application doesn't know witch 
	// language we use. so we are going to show both of them
	
	var Message;
	
	//switch(Language)
	//{
	//	case "nl":		
	//	case "fr":
	//		Message = Error(Language,theErrorId);
	//		break;
			
	//	default: 
	//		Message = Error("nl",theErrorId);
	//		break;
	//}


 // field 'language' is used in inschrijvingsform.aspx. 
	if  ((document.getElementById('language').getAttribute("lang") == "fr") || (Temp.indexOf("/fr") > -1) || (Temp.indexOf("/depose_plainte") > -1))
		Message = Error("fr",theErrorId);
	else 
		Message = Error("nl",theErrorId);
		
		
		
	// If necessary replace %1 by the passed parameter
	Message = Message.split("%1").join(Param);
	

	// return the message
	return Message;
	
}

// ----------------------------------------------------------------------------------------------------------
//
// With a span
//
// ---------------------------------------------------------------------------------------------------------- 
function ShowSpanError ( theInput, theSpan, theErrorId )
{	
	// Make the inputbox red
	SetColorInput( theInput, "#ff9999" );
	
	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Param = theInput.value;
		
	// We retrieve the correct span
	var theError = document.getElementById(theSpan);
	
	if (theError == null) return;
	
	// Pass error string
	theError.innerHTML = GetMessage(theErrorId, Param);
	
	// Show error message
	theError.style.display = '';
	
	// Error!!!
	return false;
}

function HideSpanError ( theInput, theSpan )
{
	// Make the inputbox normal again
	SetColorInput( theInput, "#ffffff" );
	
	// We retrieve the correct span
	var theError = document.getElementById(theSpan);	
	
	if (theError == null) return;
	
	// Empty error string
	theError.innerHTML = '';
	
	// Hide error message
	theError.style.display = 'none';
	
	// Everything OK!!!
	return true;
}

// ----------------------------------------------------------------------------------------------------------
//
// With an alert
//
// ---------------------------------------------------------------------------------------------------------- 
function ShowError ( theInput, theErrorId )
{	
	// Make the inputbox red
	SetColorInput( theInput, "#ff9999" );
	
	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Param = theInput.value;
	
	// Pass error string
	alert ( GetMessage(theErrorId, Param) );
		
	// Error!!!
	return false;
}

function HideError ( theInput )
{
	// Make the inputbox normal again
	SetColorInput( theInput, "#ffffff" );
	
	// Everything OK!!!
	return true;
}

// ----------------------------------------------------------------------------------------------------------
//
// Change Color
//
// ---------------------------------------------------------------------------------------------------------- 

function SetColorInput( theInput, theColor ) 
{
	switch ( theInput.type )
	{
		case 'text':
		case 'file':	
		//theInput.style.border= "1px solid #7F9DB9";
			//theInput.style.height= "18px";
			theInput.style.backgroundColor= theColor;
			break;
		case 'textarea':
		case 'select-one':
			//theInput.style.border= "1px solid #7F9DB9";
			theInput.style.backgroundColor= theColor;
			break;
		case 'radio':
		case'checkbox':
			theInput.style.backgroundColor= theColor;
			break;		
	}

}


// ----------------------------------------------------------------------------------------------------------
//
// General check email adres
//
// ---------------------------------------------------------------------------------------------------------- 
function checkEmail (theInput)
{
	var Result = isEmail(theInput);
		
	if ( Result != '' )  return ShowError(theInput, Result);

	// If we've gotten this far, everything's valid!
	return HideError ( theInput ); 
}

function checkEmailS (theInput, theSpan)
{
	var Result = isEmail(theInput);
	
	if ( Result != '' ) return ShowSpanError(theInput, theSpan, Result);

	// If we've gotten this far, everything's valid!
	return HideSpanError ( theInput, theSpan ); 
}

function isEmail (theInput) 
{
	
	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Value = theInput.value;
	
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	var checkTLD=1;

	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat=/^(eu|com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */
	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */
	if(Value.length > 0)
	{
		/* Begin with the coarse pattern to simply break up user@domain into
		different pieces that are easy to analyze. */
		var matchArray=Value.match(emailPat);
		
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		if (matchArray==null) return 'Email';

		var user=matchArray[1];
		var domain=matchArray[2];

		// Start by checking that only basic ASCII characters are in the strings (0-127).
		for (i=0; i<user.length; i++) 
		{
			if (user.charCodeAt(i)>127) return 'Email';
		}
		
		for (i=0; i<domain.length; i++) 
		{
			if (domain.charCodeAt(i)>127) return 'Email';
		}

		// See if "user" is valid 
		if (user.match(userPat)==null) return 'Email';

		/* if the e-mail address is at an IP address (as opposed to a symbolic
		host name) make sure the IP address is valid. */
		var IPArray=domain.match(ipDomainPat);
		
		if (IPArray!=null) 
		{
			// this is an IP address
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) return 'Email';
			}
			
			return ''; 
		}

		// Domain is symbolic name.  Check if it's valid.
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		
		for (i=0;i<len;i++) 
		{
			if (domArr[i].search(atomPat)==-1) return 'Email';
		}

		/* domain name seems valid, but now make sure that it ends in a
		known top-level domain (like com, edu, gov) or a two-letter word,
		representing country (uk, nl), and that there's a hostname preceding 
		the domain or country. */
		if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) return 'Email';
	
		// Make sure there's a host name preceding the domain.
		if (len<2) return 'Email';

	}

	// If we've gotten this far, everything's valid!
	return ''; 
}

// ----------------------------------------------------------------------------------------------------------
//
// General check from the bank account number
//
// ---------------------------------------------------------------------------------------------------------- 
function checkBankNr (theInput)
{
	var Result = isBankNr(theInput);
	
	if ( Result != '' ) return ShowError(theInput, Result);

	// If we've gotten this far, everything's valid!
	return HideError ( theInput ); 
}

function checkBankNrS (theInput, theSpan)
{
	var Result = isBankNr(theInput);
	
	if ( Result != '' ) return ShowSpanError(theInput, theSpan, Result);

	// If we've gotten this far, everything's valid!
	return HideSpanError ( theInput, theSpan ); 
}

function isBankNr (theInput)
{
	var Pattern = /^[0-9 ]{12}$/;
	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Value = theInput.value;

	// Test to see if input is empty.
	if(Value.length > 0)
	{
		if(! Pattern.test(Value)) return 'rekeningnr1';
	
		// split the value into two parts, to do some calculation with it.
		var theNumber = Value.substring(0,10); 
		var theControl = Value.substring(10,12);
		
		if(theNumber % 97 != theControl % 97) return 'rekeningnr2';		
	}
	
	
	// No error --> put everything in its original state
	return ''; 
}

// ----------------------------------------------------------------------------------------------------------
//
// General check from the IBAN number
//
// ---------------------------------------------------------------------------------------------------------- 
function isIbanNr (theInput)
{
	 var IbanValue = theInput.value;
		
     if (IbanValue.length < 5) { return 'IBAN1'}; 
    // if (IbanValue.length > 1)
     {
     s = IbanValue.substring(4) + IbanValue.substring(0, 4); 

    for (charcounter = 0,r = 0; charcounter < s.length; charcounter++ ) 
    { 
            c = s.charCodeAt(charcounter); 
            
            if (48 <= c && c <= 57) 
            {                 
                    if (charcounter == s.length-4 || charcounter == s.length-3) {return 'IBAN2';} 
                    k = c - 48; 
            } 
            else if (65 <= c && c <= 90) 
            { 
                    if (charcounter == s.length-2 || charcounter == s.length-1) { return 'IBAN3'; } 
                    k = c - 55; 
            } 
            else { return 'IBAN4'; } 
            if (k > 9) 
                    r = (100 * r + k) % 97; 
            else 
                    r = (10 * r + k) % 97; 
    }

    if (r != 1) { return 'IBAN5'} ;  
    }
      
	// No error --> put everything in its original state
	return ''; 
}

// ----------------------------------------------------------------------------------------------------------
//
// General check from the BIC number
//
// ---------------------------------------------------------------------------------------------------------- 
function isBicNr (theInput)
{
   
    if (document.getElementById('txtAanvragerIbanNr').value != '' && document.getElementById('txtAanvragerBicNr').value == '' ) { return 'Verplicht'} ;  
        
	// No error --> put everything in its original state
	return ''; 
}



// ----------------------------------------------------------------------------------------------------------
//
// General check from the tax number
//
// ---------------------------------------------------------------------------------------------------------- 
function checkBTWNr (theInput)
{
	var Result = isBTWNr(theInput);
	
	if ( Result != '' ) return ShowError(theInput, Result);

	// If we've gotten this far, everything's valid!
	return HideError ( theInput ); 
}

function checkBTWNrS (theInput, theSpan)
{
	var Result = isBTWNr(theInput);
	
	if ( Result != '' ) return ShowSpanError(theInput, theSpan, Result);

	// If we've gotten this far, everything's valid!
	return HideSpanError ( theInput, theSpan ); 
}



function isBTWNr (theInput)
{
	//var Pattern = /^[0-9 ]{12}$/;
	var Pattern = /^[A-Z]{2}[0-9]{10}$/; 	

	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Value = theInput.value;
	// Test to see if input is empty.
	if(Value.length > 0)
	{	
		Value = (Value).replace(/[\s-.]*/g,'');
		theInput.value = Value;
		if(! Pattern.test(Value)) return 'BTW1';

		// split the value into two parts, to do some calculation with it.
		var theNumber = Value.substring(3,10); 
		var theControl = Value.substring(10,12);
	
		if(theNumber % 97 != 97 - theControl) return 'BTW2';
	}
	// No error --> put everything in its original state
	return ''; 
}

// ----------------------------------------------------------------------------------------------------------
//
// General check for integer
//
// ---------------------------------------------------------------------------------------------------------- 
function checkInteger (theInput)
{
	
	var Result = isInteger(theInput);
	
	if ( Result != '' ) return ShowError(theInput, Result);

	// If we've gotten this far, everything's valid!
	return HideError ( theInput ); 
}

function checkIntegerS (theInput, theSpan)
{
	
	var Result = isInteger(theInput);
	
	if ( Result != '' ) return ShowSpanError(theInput, theSpan, Result);

	// If we've gotten this far, everything's valid!
	return HideSpanError ( theInput, theSpan ); 
}

function isInteger(theInput)
{
	var Pattern = /^\d+$/;

	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Value = theInput.value;
	
	// Test to see if input is empty.
	if(Value.length > 0)
	{
		if(!Pattern.test(Value)) return 'int';
	}
	// No error --> put everything in its original state
	return ''; 
	
	
}

// ----------------------------------------------------------------------------------------------------------
//
// General check for Zipcode
//
// ---------------------------------------------------------------------------------------------------------- 
function checkPostcode (theInput)
{
	var Result = isPostcode(theInput);
	
	if ( Result != '' ) return ShowError(theInput, Result);

	// If we've gotten this far, everything's valid!
	return HideError ( theInput ); 
}

function checkPostcodeS (theInput, theSpan)
{
	var Result = isPostcode(theInput);
	
	if ( Result != '' ) return ShowSpanError(theInput, theSpan, Result);

	// If we've gotten this far, everything's valid!
	return HideSpanError ( theInput, theSpan ); 
}

function isPostcode(theInput)
{
	var Pattern = /^[0-9 ]{4}$/;
	
	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Value = theInput.value;
	
	// Test to see if input is empty.
	if(Value.length > 0)
	{
		if(!Pattern.test(Value)) return 'ZipCode';
	}
	// No error --> put everything in its original state
	return ''; 
}

// ----------------------------------------------------------------------------------------------------------
//
// General check for real
//
// ---------------------------------------------------------------------------------------------------------- 
function checkReal (theInput)
{
	var Result = isReal(theInput);
	
	if ( Result != '' ) return ShowError(theInput, Result);

	// If we've gotten this far, everything's valid!
	return HideError ( theInput ); 
}

function checkRealS (theInput, theSpan)
{
	var Result = isReal(theInput);
	
	if ( Result != '' ) return ShowSpanError(theInput, theSpan, Result);

	// If we've gotten this far, everything's valid!
	return HideSpanError ( theInput, theSpan ); 
}

function isReal(theInput)
{	
	var Pattern = /^[0-9]+(\.{0,1}[0-9]+)*$/;
	
	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Value = theInput.value;
	
	// Test to see if input is empty.
	if(Value.length > 0)
	{
		if(! Pattern.test(Value)) return 'Real';
	}
	// No error --> put everything in its original state
	return ''; 
}

// ----------------------------------------------------------------------------------------------------------
//
// General check for real
//
// ---------------------------------------------------------------------------------------------------------- 
function checkEmpty (theInput)
{
	// We don't given an alert error! that would be annoying!
	var Result = isEmpty(theInput);
	
	if ( Result == '' ) return HideError ( theInput ); 
}

function checkEmptyS (theInput, theSpan)
{
	var Result = isEmpty(theInput);
	
	if ( Result != '' ) return ShowSpanError(theInput, theSpan, Result);

	// If we've gotten this far, everything's valid!
	return HideSpanError ( theInput, theSpan ); 
}

function isEmpty(theInput)
{
	if(theInput.value.length == 0) return 'Verplicht';

	return ''; 
}

function checkRequiredEmpty(theInput)
{				
	if ( hasAttribute( theInput, 'required') == true )
	{
		checkEmpty (theInput);
	}
}


function disableButton(name,theForm, pbFunc){
	if(checkRequiredFields(theForm)){
		theForm.elements[name].disabled=true;
		eval(pbFunc);
	}
}
// ----------------------------------------------------------------------------------------------------------
//
// Check to see that all necessary fields are filled in
//
// ---------------------------------------------------------------------------------------------------------- 
function checkRequiredFields(theForm, requiredFields)
{		
	var theResult = true;
	var theMessages = new Array;

	theMessages[theMessages.length] = ( GetMessage( 'Titel', '' ) + "\r\n" );
	
	var theCtrl =  new Array();
	theCtrl[0]=theForm.elements['txtInstallatieStraat'];
	theCtrl[1]=theForm.elements['txtInstallatieStraatNummer'];
	theCtrl[2]=theForm.elements['txtInstallatiePostCode'];
	theCtrl[3]=theForm.elements['txtInstallatieGemeente'];
	if(theCtrl[0] || theCtrl[1] || theCtrl[2] || theCtrl[3] ){
		if(theCtrl[0].value!="" || theCtrl[1].value!="" || theCtrl[2].value!="" || theCtrl[3].value!=""){
			for(i=0; i<theCtrl.length; i++){
				if ( theCtrl[i].getAttributeNode('required') == null ) theCtrl[i].setAttribute('required','');
			}
		}else{
			for(i=0; i<theCtrl.length; i++){
				if ( theCtrl[i].getAttributeNode('required') != null ) theCtrl[i].removeAttribute('required');
			}
		}
	}
	for(i=0; i<theForm.elements.length; i++)
	{
		var theCtrl = theForm.elements[i];
		var theColor = '#ffffff';
		var li = "      -  ";
		var theName = '';
		
		if ( requiredFields != null )
		{			
			if ( requiredFields[theCtrl.name] == true )
			{
				if ( theCtrl.getAttributeNode('required') == null ) theCtrl.setAttribute('required');
			}
			else
			{
				if ( theCtrl.getAttributeNode('required') != null ) theCtrl.removeAttribute('required');
				if(theCtrl.value.length == 0) HideError ( theCtrl ); 
			}
		}
		
		if ( hasAttribute(theCtrl, "DisplayName") == true ) theName = theCtrl.getAttribute("DisplayName") + ": ";
		
		switch ( theCtrl.type )
		{
			case 'text':
			case 'textarea':
			case 'file':
			case 'select-one':
					
					for (y=0;y<theCtrl.attributes.length;y++)
					{
						var theAttribute = theCtrl.attributes[y];
						var theErrorCode = '';
						
											
						switch( theAttribute.nodeName.toLowerCase() )
						{
							case 'required':
									theErrorCode = isEmpty(theCtrl);
									break;
							
							case 'email':
									theErrorCode = isEmail(theCtrl);
									break;
							
							case 'integer':
									theErrorCode = isInteger(theCtrl);
									break;
							
							case 'real':
									theErrorCode = isReal(theCtrl);
									break;
							
							case 'postcode':
									theErrorCode = isPostcode(theCtrl);
									break;
							
							case 'btwnr':
									theErrorCode = isBTWNr(theCtrl);
									break;
							
							case 'banknr':
								 if ((document.getElementById('txtAanvragerBankRekeningNr').value == '') && (document.getElementById('txtAanvragerIbanNr').value == '')) 	
								 	theErrorCode = 'rekeningnr3';
								 else
								 {
									if (document.getElementById('txtAanvragerIbanNr').value == '')	
										theErrorCode = isBankNr(theCtrl);
								 }																									
								break;
									
							case 'pdf':
									theErrorCode = isPDF(theCtrl);
									break;
									
							case 'ibannr':
								
								 if ((document.getElementById('txtAanvragerIbanNr').value == '') && (document.getElementById('txtAanvragerBankRekeningNr').value == '')) 	
								 {	theErrorCode = 'rekeningnr3';}
								 else
								{
								  if (document.getElementById('txtAanvragerBankRekeningNr').value == '')	
										theErrorCode = isIbanNr(theCtrl);
								 }
								 	
								break;	
									
							case 'bicnr':
									theErrorCode = isBicNr(theCtrl);
									break;										
							case 'rijksregisternr':
								 if ((document.getElementById('txtRijksregisternummer').value == '') && (document.getElementById('txtKBOnummer').value == '')) 	
								 {	theErrorCode = 'rijksregisternr';
									break;
								 }							
							
							case 'kbonr':																							
								 if ((document.getElementById('txtRijksregisternummer').value == '') && (document.getElementById('txtKBOnummer').value == '')) 	
								 {	theErrorCode = 'rijksregisternr';
									break;
								 }	
							
							 							
						}
						if(theErrorCode.length != 0) 
						{
							theColor = "#ff9999";
						
							var theErrorMessage = li + theName + GetMessage( theErrorCode, theCtrl.value );
							if ( theMessages.indexOf( theErrorMessage ) == -1 ) theMessages[theMessages.length] = theErrorMessage;
						}				
					}
										
					break;
					
			case 'checkbox':
			
					for (y=0;y<theCtrl.attributes.length;y++)
					{
						var theAttribute = theCtrl.attributes[y];
						var theErrorCode = '';
											
						switch( theAttribute.nodeName.toLowerCase() )
						{							
							case 'required':
									if (!theCtrl.checked) theErrorCode = 'VerplichtCheck';
									
									break;
							
							case 'required_dakisolatie':
								if ((!document.getElementById('cbxAkkord2').checked) && (!document.getElementById('cbxAkkord3').checked)) 	
									theErrorCode = 'VerplichtCheckDakisolatie';
									
									break;
									
						}
						
						if(theErrorCode.length != 0) 
						{
							theColor = "#ff9999";
						
							var theErrorMessage = li + theName + GetMessage( theErrorCode, theCtrl.value );
							if ( theMessages.indexOf( theErrorMessage ) == -1 ) theMessages[theMessages.length] = theErrorMessage;
						}	
					}
		}
		
		
		
		// Adjust the color
		SetColorInput ( theCtrl, theColor );
		
		if ( theColor == "#ff9999" ) theResult = false;
	}		
	

	// Show Error
	if ( theResult == false ) 
	{
		var MessageString = '';
		
		for(i=0; i<theMessages.length; i++) 
		{
			MessageString += ( theMessages[i] + "\r\n" );
		}
		
		

		alert ( MessageString );
	}
	
	return theResult;	
}


function hasAttribute( theCtrl, Name )
{
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
	{
		// IE
		return ( theCtrl.getAttributeNode(Name) != null );
	} 
	else
	{
		// FireFox, ...
		return ( theCtrl.hasAttribute(Name) );
	}
	
	return false;
}

// ----------------------------------------------------------------------------------------------------------
//
// Check to see that the files are from the corrert type --> pdf, xls,...
//
// ---------------------------------------------------------------------------------------------------------- 
function checkPDF( theInput )
{

	var Result = isPDF(theInput);

	if ( Result != '' ) return ShowError(theInput, Result);
	


	// If we've gotten this far, everything's valid!
	return HideError ( theInput ); 
}

function checkPDFS( theInput, theSpan )
{
	var Result = isPDF(theInput);

	if ( Result != '' ) return ShowSpanError(theInput, theSpan, Result);

	// If we've gotten this far, everything's valid!
	return HideSpanError ( theInput, theSpan ); 	
}

function isPDF( theInput )
{
	var theExtension = '.pdf';
		
	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Value = theInput.value;
	
	// Test to see if input is empty.
	if(Value.length > 0)
	{
		while (Value.indexOf("\\") != -1)
			Value = Value.slice(Value.indexOf("\\") + 1);
		
		//alert("slice " + Value.slice(Value.indexOf(".")).toLowerCase())
		if( theExtension != Value.slice(Value.indexOf(".")).toLowerCase()) return 'Pdf';

	}	

	// No error --> put everything in its original state
	return ''; 
}

function checkImage( theInput )
{
	var theExtensions = new Array(".gif", ".jpg", ".png");
	var theExtension = "";
	var allowSubmit = false;
	
	// Retrieve the current value from the input box.  Maybe we will use it later in the error message.
	var Value = theInput.value;

	// Test to see if input is empty.
	if(Value.length > 0)
	{	
		while (Value.indexOf("\\") != -1)
			Value = Value.slice(Value.indexOf("\\") + 1);
		
		var theExtension = Value.slice(Value.indexOf(".")).toLowerCase();

		for (var i = 0; i < theExtensions.length; i++) 
		{
			if (theExtensions[i] == ext) 
			{ 
				allowSubmit = true; 
				break; 
			}
		}
		
		if (!allowSubmit)  return ShowError ( theInput, 'Image' );
	}
	// No error --> put everything in its original state
	return HideError ( theInput ); 
}
// ---------------------------------------------------------------------------------------------------------- 
//
//  keyCheck(eventObj, obj)
//  function to allow only numeric input characters
//
// ---------------------------------------------------------------------------------------------------------- 
function keyCheck(eventObj, obj)
{
	var keyCode
	if (document.all){ 
		keyCode=eventObj.keyCode
	}
	else{
		keyCode=eventObj.which
	}
	var str=obj.value
	if(keyCode==46){ 
		if (str.indexOf(".")>0){
			return false
		}
	}
	if((keyCode<48 || keyCode >58)   &&   (keyCode != 46)){ // Allow only integers and decimal points
		return false
	}
	return true
}

// ---------------------------------------------------------------------------------------------------------- 
//
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
//
// ---------------------------------------------------------------------------------------------------------- 
function hasOptions(obj) 
{
	if (obj!=null && obj.options!=null) { return true; }
	return false;
}

// ---------------------------------------------------------------------------------------------------------- 
//
//  This function selects all options that match the expression
//  passed in. Selected options that don't match will be un-selected.
//
// ---------------------------------------------------------------------------------------------------------- 
function selectOptions(theInput,theText) 
{
	//if the select is empty then exit this function
	if (!hasOptions(theInput))  return; 
		
	//loop all the options
	for (var i=0; i<theInput.options.length; i++) 
	{
		if ( theText.equalsIgnoreCase(theInput.options[i].text) ) 
		{
			//if equals then select
			theInput.options[i].selected = true;
		}
		else 
		{
			// when it isn't the same remove selection
			theInput.options[i].selected = false;
			
		}
	}
}

// ---------------------------------------------------------------------------------------------------------- 
//
//  This function tests all options that match the expression passed in.
//
// ---------------------------------------------------------------------------------------------------------- 

function ExistOptions(theInput, theText)
{
	//if the select is empty then exit this function
	if (!hasOptions(theInput))  return HideError ( theInput );  
		
	//loop all the options
	for (var i=0; i<theInput.options.length; i++) 
	{		
		if ( theText.equalsIgnoreCase(theInput.options[i].text)) return ShowError( theInput, 'OptionExist' );
	}
	
	return HideError ( theInput ); 
}


// ---------------------------------------------------------------------------------------------------------- 
//
//  Compare strings
//
// ---------------------------------------------------------------------------------------------------------- 

String.prototype.equalsIgnoreCase= myEqualsIgnoreCase;
String.prototype.equals=myEquals;

function myEquals(arg)
{
        return (this.toString()==arg.toString());
}

function myEqualsIgnoreCase(arg)
{               
        return (new String(this.toLowerCase())==(new String(arg)).toLowerCase());
}

// ---------------------------------------------------------------------------------------------------------- 
//
//  searches from the front of the array to the end, and takes an optional starting index parameter
//
// ---------------------------------------------------------------------------------------------------------- 
Array.prototype.indexOf = myIndexOf;

function myIndexOf(v,n)
{
  n = (n==null)?0:n; var m = this.length;
  for(var i = n; i < m; i++)
    if(this[i] == v)
       return i;
  return -1;
}






