/*

	e2d_common_check.js
	-------------------
	
	Several forms all use the same block of common elements for input
	so this code will check all of those in a standard way to
	avoid duplication.
	
	Note that this supports the use of non-machine-readable security graphic.

*/

// -------------------------------------------------------------

/*
	
	The common items are (* means compulsory):
	
		Name *
		Job Title
		Company
		Email *
		Email Confirm *
		Phone
		Security Code (Captcha) *
		
		
		checkAddress is passed in from the calling function, having been
		set from the PHP $checkAddress passed via the onSubmit() call.
		
		0 = no Address checking
		1 = check Address but completion is optional
		2 = check Address and it is compulsory 
	
*/

function checkCommonElements(theForm, codecheck, checkAddress) {
	
	// The "allowed" vars will all be identical to those in the non-common block	
	var allValid = true;

	var NameAllowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ-'" ;
	var AddressAllowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789()ƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ-'+.,:;&[]#/\n\r" ;
	var JobTitleAllowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789()ƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ-'&.#/" ;	
	var CoAllowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789()ƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ-'&+.#/" ;
	var PhoneAllowed = "0123456789 ()-+.#/" ;
	var EmailAllowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ@.-" ;
	var SecCodeAllowed = "0123456789" ;	
	
		
	
	// ------------------------------------
	// Name
	// ------------------------------------
	
	if ( validateField(theForm.Name.value,"Name", NameAllowed, "characters", 3, 50) == false ) {
		theForm.Name.focus() ;
		return ( false ) ;
	}

	// ------------------------------------
	// Job Title
	// ------------------------------------
	
	if ( validateField(theForm.Job_Title.value,"Job Title", JobTitleAllowed, "characters", 0, 40) == false ) {
		theForm.Job_Title.focus() ;
		return ( false ) ;
	}
	
	// ------------------------------------
	// Company/Organisation
	// ------------------------------------
	
	if ( validateField(theForm.Organisation.value,"Organisation", CoAllowed, "characters", 0, 60) == false ) {
		theForm.Organisation.focus() ;
		return ( false ) ;
	}
	
	// ------------------------------------
	// Phone
	// ------------------------------------
	
	
	if ( validateField(theForm.Phone.value,"Phone Number", PhoneAllowed, "symbols and numeric characters", 0, 20) == false ) {
		theForm.Phone.focus() ;		
		return ( false ) ;		
	}
	
  
	// ------------------------------------
	// Email
	// ------------------------------------
	
	if ( validateField(theForm.Email.value,"Email Address", EmailAllowed, "alphanumeric characters", 6, 75) == false ) {
		theForm.Email.focus() ;	
		return ( false ) ;	
	}
	  
	if ( emailcheck( theForm.Email.value ) == false ) {
		theForm.Email.focus();
		return ( false );
	}
	
	if ( theForm.Email_Confirm_xx.value == "" )
	{
		alert( "Please confirm your Email address" );
		theForm.Email_Confirm_xx.focus();
		return ( false );
	}

	if ( theForm.Email.value != theForm.Email_Confirm_xx.value )
	{
		alert( "The Email addresses you have entered don't match" );
		theForm.Email_Confirm_xx.focus();
		return ( false );

	}
	
	
	// ------------------------------------
	// Address : IF REQUIRED
	// ------------------------------------
	
	
	// alert("checkAddress=[" + checkAddress + "]") ;
	
	if ( checkAddress > 0.0 ) {
		// Address is to be checked
		
		// alert("Checking Address ...");
		
		if ( checkAddress > 1.0 ) {
			// mandatory address fields
			
			if ( validateField(theForm.Address.value,"Address", AddressAllowed, "alphanumeric characters", 5, 100) == false ) {
				theForm.Address.focus() ;		
				return ( false ) ;		
			}
			
			if ( validateField(theForm.PostCode.value,"Post Code/Zip", AddressAllowed, "alphanumeric characters or the word NONE", 0, 12) == false ) {
				theForm.PostCode.focus() ;		
				return ( false ) ;		
			}			
			
			if ( validateField(theForm.Country.value,"Country", AddressAllowed, "alphanumeric characters", 2, 50) == false ) {
				theForm.Country.focus() ;		
				return ( false ) ;		
			}			
			
			
		}	
		else {
			// optional Address fields

			if ( validateField(theForm.Address.value,"Address", AddressAllowed, "alphanumeric characters", 0, 100) == false ) {
				theForm.Address.focus() ;		
				return ( false ) ;		
			}
			
			if ( validateField(theForm.Post_Code.value,"Post Code/Zip", AddressAllowed, "alphanumeric characters", 0, 12) == false ) {
				theForm.Post_Code.focus() ;		
				return ( false ) ;		
			}			
			
			if ( validateField(theForm.Country.value,"Country", AddressAllowed, "alphanumeric characters", 0, 50) == false ) {
				theForm.Country.focus() ;		
				return ( false ) ;		
			}			
			
		}	
		
	}
	
		
	// ------------------------------------
	// Security Code
	// ------------------------------------
	
	// alert("Checking Code ...");
	
	if ( validateField(theForm.Enquiry_Code_xx.value,"Graphic Code", SecCodeAllowed, "numeric characters", 5, 5) == false ) {
		theForm.Enquiry_Code_xx.focus() ;	
		return ( false ) ;	
	}

	var codegiven = theForm.Enquiry_Code_xx.value;

	if ( codecheck != codegiven )
	{
		alert("The Graphic Code is incorrect");
	    theForm.Enquiry_Code_xx.focus();
	    return (false);
	}


	// ------------------------------------
	// end of checks
	// ------------------------------------

	return (true);
		
	
}

// -------------------------------------------------------------

