// <![CDATA[

function buildForm() {
	/*** Construction of ValidForm object. *******************************
	 * Usage: addElement("name of the form element", TYPE_CONSTANT, Required field)
	 * 
	 * Available TYPE_CONSTANT's are:
	 * 	LIBFRM_STRING
	 * 	LIBFRM_TEXT
	 * 	LIBFRM_TELFAX
	 * 	LIBFRM_NUMERIC
	 * 	LIBFRM_INTEGER
	 * 	LIBFRM_WORD
	 * 	LIBFRM_EMAIL
	 * 	LIBFRM_PASSWORD
	 * 	LIBFRM_ZIPCODE_NL
	 * 	LIBFRM_PHONE_NL
	 * 	LIBFRM_SIMPLEURL
	 * 	LIBFRM_DATE
	 */
	  
	/*** ValidForm object holding form that needs validation. ************
	 * Usage: objForm.addElement("name of form element", 
	 *      "id of form element", element type, required, 
	 *      maximum input length, minimum input length, 
	 *      "id of matching element").
	 */
	var objForm = new ValidForm("reservationform");
	objForm.addElement("firstname", "firstname", LIBFRM_STRING, true);
	objForm.addElement("lastname", "lastname", LIBFRM_STRING, true);
	objForm.addElement("deliverdate", "deliverdate", LIBFRM_DATE, true);
	objForm.addElement("pickupdate", "pickupdate", LIBFRM_DATE, true);
	objForm.addElement("car", "car", LIBFRM_STRING, true);
	objForm.addElement("payment", "payment", LIBFRM_STRING, true);
	objForm.addElement("plocation", "plocation", LIBFRM_STRING, false);
	objForm.addElement("dlocation", "dlocation", LIBFRM_STRING);
	objForm.addElement("dispatch", "dispatch", LIBFRM_STRING, true);
	objForm.addElement("user_id", "user_id", LIBFRM_INTEGER, true);
	
	/*** Error object holding input errors. ******************************
	 * Usage: objErrors.addError("name of the form element", type error, 
	 *      required error, too long error, too short error, match error).
	 */
	var objErrors = new FormErrors();
	objErrors.mainError = "Er zijn fouten opgetreden bij het plaatsen van uw reservering. Controleer de markeerde velden en probeer het opnieuw.";
	objErrors.addError("firstname", "Voer uw voornaam in.", "Dit is een verplicht veld.");
	objErrors.addError("lastname", "Voer uw achternaam in.", "Dit is een verplicht veld.");
	objErrors.addError("deliverdate", "Selecteer een datum (DD-MM-JJJJ).", "Dit is een verplicht veld.");
	objErrors.addError("pickupdate", "Selecteer een datum (DD-MM-JJJJ).", "Dit is een verplicht veld.");
	objErrors.addError("car", "Selecteer een wagen uit de lijst.", "Dit is een verplicht veld.");
	objErrors.addError("paymeny", "Selecteer een betaalmethode uit de lijst.", "Dit is een verplicht veld.");
	objErrors.addError("plocation", "Selecteer een lokatie uit de lijst.", "Dit is een verplicht veld.");
	objErrors.addError("dlocation", "Selecteer een lokatie uit de lijst.", "Dit is een verplicht veld.");
	objErrors.addError("dispatch", "U heeft ongeldige tekens in uw invoer gebruikt.", "Dit is een verplicht veld.");
	objErrors.addError("user_id", "U heeft ongeldige tekens in uw invoer gebruikt.", "Dit is een verplicht veld.");
	objForm.errors = objErrors;

	//*** ValidForms object holding forms that need validation.
	objValidForms = new ValidForms();
	
	//*** Add the form to the validation object.
	objValidForms.addForm(objForm);
}

function externalLinks() {
	var objCurrent;
	var objReplacement;

	if (document.getElementsByTagName) {
		var objAnchors = document.getElementsByTagName("a");
		for (var iCounter=0; iCounter<objAnchors.length; iCounter++) {
			if (objAnchors[iCounter].getAttribute("href") && objAnchors[iCounter].getAttribute("rel") == "external") {
				objAnchors[iCounter].onclick = function(event){return launchWindow(this, event);}
				objAnchors[iCounter].onkeypress = function(event){return launchWindow(this, event);}
				if (document.replaceChild) {
					objCurrent = objAnchors[iCounter].firstChild;
					if (objCurrent.nodeType == 3) { // Text node
						objAnchors[iCounter].title = (objAnchors[iCounter].title != "") ? objAnchors[iCounter].title + " (opent in een nieuw venster)" : objCurrent.data + " opent in een nieuw venster";
					} else if (objCurrent.alt) { // Current element is an image
						objReplacement = objCurrent;
						objReplacement.alt = objCurrent.alt + " (opent in een nieuw venster)";
						try {
							objAnchors[iCounter].replaceChild(objReplacement, objCurrent);
						} catch(e){}
					}
				}
			}
		}
	}
}

function launchWindow(objAnchor, objEvent) {
	var iKeyCode;

	if (objEvent && objEvent.type == "keypress") {
		if (objEvent.keyCode) {
			iKeyCode = objEvent.keyCode;
		} else if (objEvent.which) {
			iKeyCode = objEvent.which;
		}

		if (iKeyCode != 13 && iKeyCode != 32) {
			return true;
		}
	}

	return !window.open(objAnchor);
}

//*** Declare ValidForms object globally.
var objValidForms;

//*** Build the validation forms when the DOM is loaded.
$(document).ready(function(){
	externalLinks();
	buildForm();
});

// ]]>
