//*******************
// Form Constructor *
//*******************
function Form(ID)
{
	this.formRoot		= document.getElementById(ID);
	this.formElements	= this.formRoot.elements;
	this.feedbackNode	= document.getElementById('cs_feedback_info');
	
	// set strings
	this.hideClass		= 'hide';
	this.showClass		= 'show';
	this.errorClass		= 'error';
}

//**********************
// Function check form *
//**********************
Form.prototype.check = function()
{
	var error = false;
	
	// get all elements
	for(var b=0;b<this.formElements.length;b++)
	{
		var formElement = this.formElements[b];
		
		this.feedbackNode.className = this.hideClass;

		// only if title attribute is found on non hidden field
		if(formElement.title && formElement.type != 'hidden')
		{
			// reset error class
			formElement.className = '';
		
			// empty fields or starting with space
			if(!formElement.value || formElement.value.charAt(0) == ' ')
			{
				formElement.className = this.errorClass;
				error = true;
			}

			// postal code
			else if(formElement.title == 'postalcode')
			{
				if(formElement.value && !this.checkPostalcode(formElement))
				{
					formElement.className = this.errorClass;
					error = true;
				}
			}
			
			// e-mail
			else if(formElement.title == 'email')
			{
				if(formElement.value && !this.checkEmail(formElement))
				{
					formElement.className = this.errorClass;
					error = true;
				}
			}
			
			// phone, fax and mobile number
			else if(formElement.title == 'phonenumber' || formElement.title == 'faxnumber' || formElement.title == 'mobilenumber')
			{
				if(formElement.value && !this.checkPhone(formElement))
				{
					formElement.className = this.errorClass;
					error = true;
				}
			}
			
			// bank number
			else if(formElement.title == 'banknumber')
			{
				if(formElement.value && !this.checkBankNr(formElement))
				{
					formElement.className = this.errorClass;
					error = true;
				}
			}
			
			// passwords
			else if(formElement.title == 'password_1' || formElement.title == 'password_2')
			{
				if(formElement.value && !this.checkPassword())
				{
					formElement.className = this.errorClass;
					error = true;
				}		
			}
		}
	}
	
	// set error output
	if(error)
	{
		this.feedbackNode.className = this.showClass;
	}
	
	// submit form
	//return true;
}

//****************************
// Function check postalcode *
//****************************
Form.prototype.checkPostalcode = function(strPostalcode)
{
	var validRegExp_1 = /^[1-9][0-9]{3} [a-z|A-Z]{2}$/;
	var validRegExp_2 = /^[1-9][0-9]{3}[a-z|A-Z]{2}$/;
	var strPostalcode = strPostalcode.value;
	if(strPostalcode.search(validRegExp_1) == -1 && strPostalcode.search(validRegExp_2) == -1) 
	{
		return false;
	} 
	return true; 
}

//**********************
// Function checkEmail *
//**********************
Form.prototype.checkEmail = function(strEmail)
{
	var validRegExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var strEmail = strEmail.value;
	
	if(strEmail.search(validRegExp) == -1) 
	{
		return false;
	} 
	return true; 
}

//**********************
// Function checkPhone *
//**********************
Form.prototype.checkPhone = function(strPhone)
{
	var validChars = "0123456789";
	var character;

	for(var z=0;z<strPhone.value.length;z++) 
	{ 
		character = strPhone.value.charAt(z); 
		if(validChars.indexOf(character) == -1 || strPhone.value.length < 10) 
		{
			return false;
		}
	}
	return true;
}

//***********************
// Function checkBankNr *
//***********************
Form.prototype.checkBankNr = function(strBankNr)
{
	var validChars = "0123456789";
	var character;

	for(var z=0;z<strBankNr.value.length;z++) 
	{ 
		character = strBankNr.value.charAt(z); 
		if(validChars.indexOf(character) == -1 || strBankNr.value.length < 8) 
		{
			return false;
		}
	}
	return true;
}

//*************************
// Function checkPassword *
//*************************
Form.prototype.checkPassword = function()
{
	// equal passwords with a minimum of 6 characters
	if((this.password_1.value != this.password_2.value)  || this.password_1.value.length < 6) 
	{
		return false;
	}

	return true;
}

//****************
// Function send *
//****************
Form.prototype.send = function()
{
	this.formRoot.submit();
}
