/*	#################################################################
#																	#
#		handling the form evaluation								#
#																	#
################################################################## */


/*
*	contact form mandatory fields
*/
/* OLD MANDATORY FIELDS
var mandatoryFields = new Array ( 
								 	"FIRST_NAME", "LAST_NAME", "STREET", "ZIP", "CITY",
									"COUNTRY", "PHONE", "EMAIL", "SUBJECT", "MESSAGE" );
*/
var mandatoryFields = new Array ( 
								 	"FIRST_NAME", "LAST_NAME", "EMAIL", "SUBJECT", "MESSAGE" );


var freeFields = new Array ( 
								 	"STREET", "ZIP", "CITY", "COUNTRY", "PHONE" );

/*
*	function invoked when the form is submitted
*	checks for the input classes and if empty passes the error class (= highlight)
*	whenever a field isn't ok (= mostly empty) a variable is incremented (= count)
*	if count > 0 => there is at least one empty field the form will not be sent
*
*	if everything is ok the form/ ajax request is executed
*	therefor the data is sent to "sendEmail.php"
*/
function checkForm ( obj )
	{
		var count = 0;
		
		/*
		*	for all mandatory fields
		*	check for emptyness
		*/
		for ( var i = 0; i < mandatoryFields.length; i++  )
			{
				/*
				*	get the right html element and 
				*	pass it to the variable "element"
				*/
				var element = document.getElementById ( mandatoryFields[i] );
				
				/*
				*	whenever the input value 
				*	is empty go on width code
				*/
				if ( element.value == "" )
					{
						/*
						*	normal input fields
						*	FIRST_NAME, LAST_NAME, STREET, ZIP, CITY, COUNTRY, PHONE, EMAIL (= is checked twice, emptyness and syntax), SUBJECT
						*	if that fields have passed the classes listed below
						*/
						if ( element.className == "inputField" || element.className == "errorField" || element.className == "dimmField" )
							{
								/*
								*	so, if empty and the correct class
								*	get the error class and increment count (= at least 1 empty field)
								*/
								element.className = "errorField";
								count++;
							}
						
						/*
						*	do the same aas explained above
						*	just with the message field, as the class name differs a bit
						*/
						if ( element.className == "msgField" || element.className == "errorMSG" )
							{
								element.className = "errorMSG";
								count++;
							}
					}
					else
					{
						/*
						*	if the fields are not empty
						*	check again for the current classes
						*/
						if ( element.className == "inputField" || element.className == "errorField" || element.className == "dimmField"  )	
							{
								/*
								*	if the classes match with the above term
								*	get the class which fades the fields
								*/
								element.className = "dimmField";	
							}
						
						/*
						*	do the same just with the select box
						*	not active!!! not in use!!
						*/
						if ( element.className == "selectBox" || element.className == "errorSelect" || element.className == "dimmSelect" )
							{
								if ( element.value == "NONE" )
									{
										element.className = "errorSelect";
										count++;	
									}
									else
									{
											element.className = "dimmSelect";
									}
							}
						
						/*
						*	do the same just with the message box
						*/
						if ( element.className == "msgField" || element.className == "errorMSG" )
							{
								element.className = "dimmMSG";		
							}
						
						/*
						*	check the email syntax
						*/
						if ( element.id == "EMAIL" )
							{
								var elementMail = document.getElementById ( "EMAIL" );
								if ( EMail ( elementMail.value ) )
										{
											
										}
										else
										{
											elementMail.className = "errorField";
											count++;		
										}		
							}
					}
			}
		
		for ( var f = 0; f < freeFields.length; f++ )
			{
				var field = document.getElementById ( freeFields[f] );
				if ( field.value != "" )
					{
						field.className = "dimmField";
					}
			}
		
		/*
		*	if count == 0 => no empty fields and correct email
		*	get function "getFormData" form the xmlREQUEST
		*/
		if ( count > 0 )
			{
				var mandatoryField = document.getElementById ( "mandatory" );
				mandatoryField.style.fontWeight = "bold";
				return false;
			}
			else
			{
				getFormData ( obj, mandatoryFields, freeFields );
				return false;
			}
	}

/*
*	if a input field is empty it has the error class
*	when it gets the focus again, the normal class is loaded
*/
function onActive ( obj )
	{
		if ( obj.className == "inputField" || obj.className == "errorField" || obj.className == "dimmField" )
			{
				obj.className = "inputField";
			}
			
		if ( obj.className == "msgField" || obj.className == "errorMSG" || obj.className == "dimmMSG" )
			{
				obj.className = "msgField";	
			}
			
		if ( obj.className == "selectBox" || obj.className == "errorSelect" || obj.className == "dimmSelect" )
			{
				obj.className = "selectBox";	
			}
	}
	
/*
*	checks the email field for the right syntax
*/
function EMail(s)
	{
		var a = false;
		var res = false;
		
		if ( typeof ( RegExp ) == 'function' )
		{
			var b = new RegExp ( 'abc' );
			if ( b.test ( 'abc' ) == true )
				{ 
					a = true;
				}
			}
		
		if ( a == true )
			{
				reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
							   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
							   '(\\.)([a-zA-Z]{2,4})$');
				res = (reg.test(s));
			}
			else
			{
				res = (s.search('@') >= 1 &&
					 s.lastIndexOf('.') > s.search('@') &&
					 s.lastIndexOf('.') >= s.length-5)
			}
			
		return(res);
	}