//*******************
// Form Constructor *
//*******************
function Form(ID)
{
	// set strings
	this.feedbackClass	= 'cs_feedback_info';
	this.hideClass		= 'cs_hide';
	this.showClass		= 'cs_show';
	this.errorClass		= 'cs_error';
	
	this.formRoot		= document.getElementById(ID);
	this.formElements	= this.formRoot.elements;
	this.feedbackNode	= this.getFeedbackNode();
}

//*****************************
// Function get feedback node *
//*****************************
Form.prototype.getFeedbackNode = function()
{
	var divElements		= this.formRoot.getElementsByTagName('div');
	var feedbackNode	= null;
	
	for (var a=0; a<divElements.length; a++)
	{
		var divElement = divElements[a];
		
		if (divElement.className == this.feedbackClass)
		{
			feedbackNode = divElement;
		}
	}
	
	return feedbackNode;
}

//**********************
// Function check form *
//**********************
Form.prototype.check = function()
{
	// set vars
	this.error			= false;
	var code_present	= false;
	
	// get all elements
	for(var b=0;b<this.formElements.length;b++)
	{
		var formElement = this.formElements[b];
		
		this.feedbackNode.style.display = 'none';

		// 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;
				this.error = true;
			}

			// postal code
			else if(formElement.title == 'postalcode')
			{
				if(formElement.value && !this.checkPostalcode(formElement))
				{
					formElement.className = this.errorClass;
					this.error = true;
				}
			}
			
			// e-mail
			else if(formElement.title == 'email')
			{
				if(formElement.value && !this.checkEmail(formElement))
				{
					formElement.className = this.errorClass;
					this.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;
					this.error = true;
				}
			}
			
			// bank number
			else if(formElement.title == 'banknumber')
			{
				if(formElement.value && !this.checkBankNr(formElement))
				{
					formElement.className = this.errorClass;
					this.error = true;
				}
			}
			
			// passwords
			else if(formElement.title == 'password_1' || formElement.title == 'password_2')
			{
				if(formElement.value && !this.checkPassword())
				{
					formElement.className = this.errorClass;
					this.error = true;
				}		
			}
			
			// code
			else if(formElement.title == 'code')
			{
				var code_present = true;
				
				// check security image
				this.checkSecurityImage(formElement);
			}
		}
	}
	
	if (!code_present)
	{
		// set error output
		if(this.error)
		{
			this.feedbackNode.style.display = 'block';
			return;
		}
		
		// submit form
		this.send();
	}
}

//****************************
// 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 checkPassword *
//*************************
Form.prototype.checkSecurityImage = function(obj)
{
	var _this	= this;
	var myConn	= new XHConn();
	
	if (!myConn) alert("XMLHTTP not available. Please try a newer/better browser.");
	
	// return result when done
	var fnWhenDone = function (oXML)
	{
		// check if value is identical to session code
		var session_code	= oXML.responseText;
		var input_code		= obj.value.toUpperCase();
		
		// error
		if (input_code != session_code)
		{
			obj.className						= _this.errorClass;
			_this.feedbackNode.style.display	= 'block';
			_this.error							= true;
		}
		
		// submit form
		else
		{
			if(!_this.error)
			{
				_this.send();
			}
		}
	}
	
	// make the connection
	myConn.connect('../classes/002_contentmanager/security_image/http/session_code.php', 'POST', '', fnWhenDone);
}

//****************
// Function send *
//****************
Form.prototype.send = function()
{
	this.formRoot.submit();
}