function Validation(form)
{
	this.formObject = $('#'+form).get(0);

	this.getFormObjectsAsMap = function() { return this.formObject.elements; }
	
	this.getFormValuesAsMap = function()
	{
		var params = new Object();
		var inputs = this.formObject.elements;

		for (var i=0; i<inputs.length; i++)
		{
			var input = inputs[i]; 
			if (input.type == "radio" || input.type == "checkbox")
			{
				// For radios and checkboxes, include them only if checked 
				if (input.checked == true)
				{
					if (params[input.name] == null) params[input.name] = new Array(); 
					params[input.name].push(this.removeHTMLTags(input.value)); 
				}
			}
			else if (input.type == "select-multiple")
			{
				// For multi-selects we have to check each value 
				for (var j=0; j<input.options.length; j++)
				{
					if (input.options[j].selected == true)
					{ 
						if (params[input.name] == null) params[input.name] = new Array(); 
						params[input.name].push(this.removeHTMLTags(input.options[j].value)); 
					}
				}
			}
			else if (input.type == "submit" || input.type == "button" || input.type == "reset")
			{
				// Don't include button types in the submit 
			} 
			else if (input.name != undefined && input.value != undefined)
			{
				// Encode the rest as name=value 
				if (params[input.name] == null) params[input.name] = new Array(); 
				params[input.name].push(this.removeHTMLTags(input.value)); 
			}
		}
		return params;
	}

	this.getFormValuesAsQueryString = function()
	{
		var params = this.getFormValuesAsMap(this.formObject);
		var queryString = "";
		var value = '';
		for (var name in params)
		{
			var values = params[name]; 
			for (var i=0; i<values.length; i++)
			{
				value = values[i];
				queryString += "&" + name + "=" + encodeURIComponent(value); 
			}
		}
		return queryString;
	}

	this.isEmpty = function (value) { var val = value+''; if(val.length > 0) { return false; } else { return true; } }

	this.isNumeric = function (value) { var val = value+''; if(val.match(/^[0-9]+$/)) { return false; } else { return true; } }

	this.isAlphabet = function (val) { if(val.match(/^[a-zA-Z]+$/)) { return false; } else { return true; } }

	this.isAlphanumeric = function (val) { if(val.match(/^[0-9a-zA-Z]+$/)) { return false; } else { return true; } }

	this.lengthRestriction = function (value, min_val, max_val) { var val = value+'';  if(val.length >= min_val && val.length <= max_val) { return false; } else { return true; } }

	this.emailValidator = function (val)
	{
		val = String(val);
		if(val.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/)) { return false; } else { return true; }
	}

	this.removeHTMLTags = function(str)
	{
		var strInputCode = str;
		var re = /(<([^>]+)>)/gi;
		var strTagStrippedText = strInputCode.replace(re, "");
		return strTagStrippedText;
	}
}

function validationEditPref()
{
	var validationObject = new Validation('alertForm');
	var formObjects = validationObject.getFormValuesAsMap();
	var error = false;
	var errorMessage = '';
	if(validationObject.isEmpty(formObjects.query))
	{
		error = true;
		errorMessage += 'Het invullen van een zoekterm is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.name))
	{
		error = true;
		errorMessage += 'Naam is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.company_name))
	{
		error = true;
		errorMessage += 'Bedrijfsnaam is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.email))
	{
		error = true;
		errorMessage += 'Email is verplicht<br>';
	}
	else
	{
		if(!validationObject.isEmpty(formObjects.email))
		{
			if(validationObject.emailValidator(formObjects.email))
			{
				error = true;
				errorMessage += 'Ongeldig email-adres<br>';
			}
		}
	}

	if(!validationObject.isEmpty(formObjects.phone))
	{
		if(validationObject.isNumeric(formObjects.phone))
		{
			errorMessage += 'Telefoonnummer dient alleen uit cijfers te bestaan<br>';
		}
		if(validationObject.lengthRestriction(formObjects.phone, 10, 10))
		{
			errorMessage += 'Telefoonnummer dient uit 10 cijfers te bestaan<br>';
		}
	}
	if(error)
	{
		$.prompt(errorMessage);
	}
	return error;
}
function validationAlert()
{
	var validationObject = new Validation('alertForm');
	var formObjects = validationObject.getFormValuesAsMap();
	var error = false;
	var errorMessage = '';
	if(validationObject.isEmpty(formObjects.query))
	{
		error = true;
		errorMessage += 'Het invullen van een zoekterm is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.name))
	{
		error = true;
		errorMessage += 'Naam is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.company_name))
	{
		error = true;
		errorMessage += 'Bedrijfsnaam is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.email))
	{
		error = true;
		errorMessage += 'Email is verplicht<br>';
	}
	else
	{
		if(!validationObject.isEmpty(formObjects.email))
		{
			if(validationObject.emailValidator(formObjects.email))
			{
				error = true;
				errorMessage += 'Ongeldig email-adres<br>';
			}
		}
	}
	if(applicationObject.doubleEmail)
	{
		error = true;
		errorMessage += 'Email-adres bestaat al!<br>';	
	}
	
	if(!validationObject.isEmpty(formObjects.phone))
	{
		if(validationObject.isNumeric(formObjects.phone))
		{
			errorMessage += 'Telefoonnummer dient alleen uit cijfers te bestaan<br>';
		}
		if(validationObject.lengthRestriction(formObjects.phone, 10, 10))
		{
			errorMessage += 'Telefoonnummer dient uit 10 cijfers te bestaan<br>';
		}
	}
	if(error)
	{
		$.prompt(errorMessage);
	}
	return error;
}
function validationFollowUp()
{
	var validationObject = new Validation('alertForm');
	var formObjects = validationObject.getFormValuesAsMap();
	var error = false;
	var errorMessage = '';
	if(validationObject.isEmpty(formObjects.query))
	{
		error = true;
		errorMessage += 'Het invullen van een zoekterm is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.name))
	{
		error = true;
		errorMessage += 'Naam is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.company_name))
	{
		error = true;
		errorMessage += 'Bedrijfsnaam is verplicht<br>';
	}
	if(validationObject.isEmpty(formObjects.email))
	{
		error = true;
		errorMessage += 'Email is verplicht<br>';
	}
	else
	{
		if(!validationObject.isEmpty(formObjects.email))
		{
			if(validationObject.emailValidator(formObjects.email))
			{
				error = true;
				errorMessage += 'Ongeldig email-adres<br>';
			}
		}
	}
	
	if(!validationObject.isEmpty(formObjects.phone))
	{
		if(validationObject.isNumeric(formObjects.phone))
		{
			errorMessage += 'Telefoonnummer dient alleen uit cijfers te bestaan<br>';
		}
		if(validationObject.lengthRestriction(formObjects.phone, 10, 10))
		{
			errorMessage += 'Telefoonnummer dient uit 10 cijfers te bestaan<br>';
		}
	}
	
	var interests = [];
	$('#interests :selected').each(function(i, selected) {
		interests[i] = $(selected).text();
	});
	
	if(interests.length < 1)
	{
		error = true;
		errorMessage += 'Geef aan waarin u geïnteresseerd bent<br>';
	}
	
	if(error)
	{
		$.prompt(errorMessage);
	}
	return error;
}

