/*
 * Codestars form validation
 * By Floris Weijenburg (http://www.florisweijenburg.nl)
 * Copyright (c) 2008 Floris Weijenburg
 * All programming rights reserved.
*/  

CS.validation.forms = function(targetField, nextFunction) 
{
	this.Init(targetField, nextFunction);
}

CS.validation.forms.prototype.Init = function(targetField, nextFunction)
{
	this.form = document.getElementById(targetField);
	this.nextFunction = nextFunction;
	var inputFields = new Array((this.form).getElementsByTagName('input'));
	var textAreaFields = new Array((this.form).getElementsByTagName('textarea'));
	
	this.elementCollection = new Array();
	this.elements = inputFields.concat(textAreaFields);
	
	this.ajaxHandler = 'ajax/ajaxDataHandler.php';
	this.ajaxRequest = false;
	
	this.ErrorList = new Object();
	this.ErrorList['count'] = 0; 
	
	YAHOO.util.Event.onDOMReady(function() 
	{ 
		this.Validate();
		return this.ReturnResult();
	}
	.bind(this));
}

CS.validation.forms.prototype.Validate = function()
{
	var inputFields = this.elements;
	
	// Loop through element types
	for (var i = 0; i < inputFields.length; i++) 
	{
		// Loopt through elements
		for (var j = 0; j < inputFields[i].length; j++) 
		{
			if (inputFields[i][j].lang != '') 
			{
				var methodName = (inputFields[i][j]).lang;
				var methodValue = (inputFields[i][j]).value;
				var inputName = (inputFields[i][j]).name;
				
				// Is the form content shown?
				if(inputFields[i][j].disabled != true)
				{
					this[methodName](methodValue, inputName);
				}
			}
		}
	} 
}
 
CS.validation.forms.prototype.Email = function(elementValue, inputName)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if (!filter.test(elementValue)) 
	{
		this.SetError(inputName, 'Het e-mail adres is onjuist.');
	}
	else { return true; }
}

CS.validation.forms.prototype.NotEmpty = function(elementValue, inputName)
{
	if(trim(elementValue) == '')
	{
		this.SetError(inputName, 'Dit veld mag niet leeg zijn.');
		return false;
	}
	else { return true; }
}

CS.validation.forms.prototype.SetError = function(key, value)
{
	this.ErrorList[key] = value;
	this.ErrorList['count']++;
}

CS.validation.forms.prototype.ReturnResult = function()
{
	// Should we wait for an Ajax request to finish?
	
	if (this.ajaxRequest) {
		Ajax.Responders.register(
		{
			onComplete: function()
			{
				this.GetResult();
			}.bind(this)
		});
	}
	else 
	{ this.GetResult(); }
}

CS.validation.forms.prototype.GetResult = function()
{
	this.ClearElements(this.form, 'errorMessage');
	
	if (this.ErrorList['count'] > 0) // Parse error messages 
	{
		for (inputName in (this.ErrorList)) 
		{
			var input = document.getElementsByName(inputName)[0];
			var errorMessage = document.createTextNode(this.ErrorList[inputName]);
			var messageCont = document.createElement('span');
	
			if (input) 
			{
				messageCont.className = 'errorMessage';
				messageCont.appendChild(errorMessage);
				
				if (input.type == 'textarea') 
				{ input.value = this.ErrorList[inputName]; }
				else 
				{ input.parentNode.appendChild(messageCont); }
			}
		}
	}
	else 
	// The input is valid... 
	// .. Are we going to submit the form or continue using JavaScript?
	{
		if(this.nextFunction != null)
		{ 
			window[this.nextFunction]();
		}
		else { this.form.submit(); }
	}	
}

CS.validation.forms.prototype.ClearElements = function(container, tag) 
{
	var elements = new Array();
	elements = $(container.id).getElementsByClassName(tag);
	var length = elements.length;
	
	for (var i = 0; i < length; i++) 
	{ 
		var elem = $(container.id).getElementsByClassName(tag)[0];
		elem.parentNode.removeChild(elem)
	}
}

CS.validation.forms.prototype.NotExistingUserName = function(elementValue, inputName) 
{
	var url = config.rootFolder + this.ajaxHandler + "?cmd=UserExists&value=" + elementValue;
	this.ajaxRequest = true;
	
	if(this.NotEmpty(elementValue, inputName))
	{ 
		if(elementValue.length < 3)
		{
			this.SetError(inputName, 'De gebruikersnaam moet minimaal 3 karakters lang zijn.');
		}
			
		var space = strpos(elementValue, " ", 0);
		var dot = strpos(elementValue, ".", 0);
		
		if(space !== false || dot !== false)
		{
			this.SetError(inputName, 'De gebruikersnaam mag geen punten of spaties bevatten.');
		}
		
		new Ajax.Request(url, 
		{
		  onSuccess: function(transport) 
		  {
			  if(trim(transport.responseText) == "")
			  // The user name is already in use
			  {
			  	this.SetError(inputName, 'De gebruikersnaam is al in gebruik.');
			  } else { return true; }
		  }.bind(this)
		});	
	}
}

CS.validation.forms.prototype.Password = function(elementValue, inputName)
{
	var minTextLength = 8;
	var currentItem = new Object();
	var passwords = new Array();
	
	currentItem[elementValue] = inputName;
	this.elementCollection.push(currentItem);
	
	if (this.NotEmpty(elementValue, inputName)) 
	{
		if (elementValue.length < minTextLength) 
		{
			this.SetError(inputName, 'Het opgegeven wachtwoord is te kort.');
			// return;
		}

		if (this.elementCollection.length == 2) 
		// Match given passwords
		{
			for (var i = 0; i < this.elementCollection.length; i++) 
			{
				for(value in this.elementCollection[i])
				{
					passwords.push(value);
				}
			}
			if (passwords[0] != passwords[1]) 
			{
				this.SetError(inputName, 'De opgegeven wachtwoorden komen niet overeen.');
				// return;
			}
		} 	
	}
}

CS.validation.forms.prototype.Date = function(elementValue, inputName)
{
	if(!this.NotEmpty(elementValue, inputName))
	{
		this.SetError(inputName, 'U heeft geen datum opgegeven.');
	}
	else
	{
		//var filter=/^(((0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d)?)$/i;
			
		if (!filter.test(elementValue)) 
		{
			this.SetError(inputName, 'Het formaat van de datum is onjuist (dd-mm-yyyy).');
		}
		else { return true; }
	}
}

CS.validation.forms.prototype.Zipcode = function(elementValue, inputName)
{
	if(!this.NotEmpty(elementValue, inputName))
	{
		this.SetError(inputName, 'U heeft geen postcode opgegeven.');
	}
	else
	{
		var filter=/^(^[1-9]{1}[0-9]{3}\s?[A-Z]{2}$)$/i;
			
		if (!filter.test(elementValue)) 
		{
			this.SetError(inputName, 'Het formaat van uw postcode is onjuist (Bv: 1234AB).');
		}
		else { return true; }
	}
}

CS.validation.forms.prototype.StreetNr = function(elementValue, inputName)
{
	if(!this.NotEmpty(elementValue, inputName))
	{
		this.SetError(inputName, 'Uw straatnummer ontbreekt.');
	}
	else
	{
		return true;
	}
}

CS.validation.forms.prototype.IsCustomerNr = function(elementValue, inputName)
{
	if(!this.NotEmpty(elementValue, inputName))
	{
		this.SetError(inputName, 'Uw klantnummer ontbreekt.');
	}	
	else 
	{
		var element = document.getElementsByName(inputName);
		
		if(IsNumeric(element[0].value))
		{
			return true;
		}
		else
		{
			this.SetError(inputName, 'Uw klantnummer moet numeriek zijn.');
		}
	}
}
