/* File: dutchrep_forms.js
 *
 * JavaScript code for the forms in serveral The Dutch Republic websites.
 *
 * Copyright 2002-2010 The Dutch Republic, All Rights Reserved.
 */
 





/* Function: LeftString
 *
 */
function LeftString(str, n) {
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else
		return String(str).substring(0,n);
}



/* Function: RightString
 *
 */
function RightString(str, n) {
	if (n <= 0)
		 return "";
	else if (n > String(str).length)
		 return str;
	else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}



/* Function: ValidateGenericForm
 *
 */
function ValidateGenericForm(theForm) {
	var wrongFields = new Array();
	theMessage = "";
	numberOfWrongTextFields = 0;
	
	// Loop trough the fields to check if they are empty while they should not
	formFields = theForm.elements;
	for (i=0; i < formFields.length; i++) {
		if (formFields[i].name.indexOf("*") != -1) {
			// There is a * in the name, check it...
			if (formFields[i].value.length < 1) {
				// theMessage = "Voordat het formulier verzonden kan worden moet u eerst alle velden waarbij een sterretje staat (*) iets invullen.";
				theMessage = "The information is the fields marked with an asteric * are required.";
			}
		}
	}
	
	// Loop trough the fields to check the email address, if any... only do this when the previous check did not cause any problems...
	if (theMessage == "") {
		formFields = theForm.elements;
		for (i=0; i < formFields.length; i++) {
			if (formFields[i].name.indexOf("@") != -1) {
				// There is a @ in the name, it should be an email adress, check it...
				if ((formFields[i].value.indexOf("@") == -1) || (formFields[i].value.indexOf(".") == -1)) {
					// theMessage = "U heeft geen geldig e-mail adres ingevoerd.";
					theMessage = "The email address you submitted is not valid.";
				}
			}
		}
	}
	
	// Make a callback to specific form-check
	if (theMessage == "") {
		theMessage = CallBackToForm(theForm);
	}
		
	// Return
	if (theMessage == "") {
		return true;
	} else {
		// theMessage = theMessage + " Probeert u het nog eens."
		theMessage = theMessage + " Please try again."
		alert(theMessage);
		return false;
	}
}




/* Function: ValidateGenericFormWithUrchin
 *
 */
function ValidateGenericFormWithUrchin(theForm, okUrchinPage, notOKUrchinPage) {
	formOK = ValidateGenericForm(theForm);
	
	if (formOK == true) {
		if (okUrchinPage != '') {
			urchinTracker(okUrchinPage);
		}
	} else {
		if (notOKUrchinPage != '') {
			urchinTracker(notOKUrchinPage);
		}
	}

	return formOK;
}



/* Function: ValidateDownloadFormWithUrchin
 *
 */
function ValidateDownloadFormWithUrchin(theForm, okWithEmailUrchinPage, okNoEmailUrchinPage, notOKUrchinPage) {
	if (true) { // check box checked
		if (false) { // email ok
			// Todo: Save e-mail with ajax-like call
			/* Vraag me wel af hoe dat dan gaat, want hoe zet ik de feedback weer in de pagina, is nog een uitzoekpuntje */
			
			// Call Google-analytics, if there is anything to call...
			if (okWithEmailUrchinPage != '') {
				urchinTracker(okWithEmailUrchinPage);
			}
			formOK = false;
		} else {
			// User checked the checkbox for receiving emails but did not supply a correct email address
			
			// Call Google-analytics, if there is anything to call...
			if (notOKUrchinPage != '') {
				urchinTracker(notOKUrchinPage);
			}
			formOK = false;
		}
	}	else {
		formOK = true;

		// Call Google-analytics, if there is anything to call...
		if (okNoEmailUrchinPage != '') {
			urchinTracker(okNoEmailUrchinPage);
		}
	}

	return formOK;
}



/* Function: CreateDatePopup
 *
 */
function CreateDatePopup(prefix, startYear, endYear, selectedYear) {
	s = "";
	
	s += "<select name='" + prefix + "Dag'>";
	s += "<option value=''></option>";
	for (i = 1; i <= 31; i++) {
		s += "<option value='";
		if (i < 10) {
				s += "0";
		}
		s += i + "'>" + i + "</option>";
	}
	s += "</select>";
	
	s += "<select name='" + prefix + "Maand'>";
	s += "<option value=''></option>";
	s += "<option value='01'>Januari</option>";
	s += "<option value='02'>Februari</option>";
	s += "<option value='03'>Maart</option>";
	s += "<option value='04'>April</option>";
	s += "<option value='05'>Mei</option>";
	s += "<option value='06'>Juni</option>";
	s += "<option value='07'>Juli</option>";
	s += "<option value='08'>Augustus</option>";
	s += "<option value='09'>September</option>";
	s += "<option value='10'>Oktober</option>";
	s += "<option value='11'>November</option>";
	s += "<option value='12'>December</option>";
	s += "</select>";
	
	s += "<select name='" + prefix + "Jaar'>";
	s += "<option value=''></option>";
	for (i = startYear; i <= endYear; i++) {
		if (i == selectedYear) {
			s += "<option selected value='" + i + "'>" + i + "</option>";
		} else {
			s += "<option value='" + i + "'>" + i + "</option>";
		}
	}
	s += "</select>";
	
	// alert(s);
	return s;
}






