<!--//JAVASCRIPT :  Form Validator : Chaos:Xionix: MaasMedia : (c)2004
/*
<!-- !IMPLEMENT THUS -->

<form name="__frmContact" action="scr/thankyou.php" method="POST" enctype="multipart/form-data" onSubmit="return Rvalidate(this)">
<input type="hidden" name="__pg_status" value="Contact Us"/>

<input type="submit" name="__Submit" value="Submit"/>
</form>





//	Easy Form Validator. - ch@os.maas.com.au
//
//	-Instructions : On the HTML form, Enter the Names of the <INPUT> Tags Thus : 'RSN__fieldname'
//		eg : <input type="text" name="RSN__fieldname" maxlength="100" size="25" value="" />
//
//	This creates the Validation Codes which are then handled by the appropriate Javascript Code
//	in the Switch(VHCodes){ case 'XX' : break; } Code blocks.
//
//	Set the <FORM> tag thus 
//	<form action="outputhandler.php" method="POST" name="formname" enctype="multipart/form-data" 
//			onSubmit="return Rvalidate(this)">
//	<input type="hidden" name="__pg_status" value="PageName"/>
//
//	<input type="submit" name="_Submit" value="Submit">
//	This routes the Output of the form through the Javascript Handler When the Submit Event occurs.
//	If the Javascript Handler routines return TRUE the Submit Event completes Execution and the 
//	Form Sends a http call to the Handling Script. - see EML_MOD.INC.PHP
// 	to recieve and process the output
//
//	NOTE: any places where a Page Divider is Desirable in the Final Email : USE
//		a <input type="hidden" name="__Name_of_the_Field" value="---"/>
//			Value must be set to "---" in order to trip the emlmod.inc.php [thankyou.php] into Bold Character RTF 
//			in the Email.
//
*/

//DEBUG : LOAD - alert("Validator Loaded");

function Rvalidate(formObj){

//CODES: R__ = Required, [] = optional
//				[R]S__ = Only Alpha Characters
//				[R]N__ = Only Numeric Characters
//				[R]SN__ = Any Alpha-Numeric or Puctuation is OK
//				[R]E__ = Validate an Email Address
//				RCH__ = Checkbox Validation : if NOT Required, just pass values through.
//				[R]GP__ = Radio Group	: if NOT Required, just pass values through.

		//:VARIABLES:Variables Reset Every time the Form is Submitted
	var i=0; msg=''; isOK='';isOK=new Array(); isEmail=false; allOK=true;  valueSelected = false; 
	//:DEBUG:		alert("Init All OK "+allOK);	//:DEBUG:
	

	for(i=0;i<formObj.elements.length;i++){

			//:DEBUG: alert('Name : '+formObj.elements[i].name);
			//:DEBUG:alert('Vals '+formObj.elements[i].value);
						
		if((formObj.elements[i].value != undefined)) // STOPS bad Values getting Parsed
		{//if it's good to go....	
			
					//THIS LINE: Gets the Validation Handling Codes from the Field Names on the formObj : See the SWITCH(nme);
			VHCodes = formObj.elements[i].name.substring(0,formObj.elements[i].name.indexOf('__'));  

						// Gets the FieldNames from the RAW Form Textfield names - Codes and '__' Removed.
				fieldname = new String();
				fieldname = formObj.elements[i].name.substring(formObj.elements[i].name.indexOf('__')+2);
				//alert('Fname :'+fieldname);

					//Fit up Fieldnames for the Error Messages.
				fnmconc='';	//	This Code Rips The '_'-underscore's from the Fieldnames. Ready for the ERROR MSG's
				for(c=0;c<fieldname.length;c++){
					if(fieldname.charAt(c) == '_')fnmconc+=' ';
					else fnmconc+= fieldname.charAt(c)
				}fieldname = fnmconc;

			//alert('Fname '+fieldname);

					//ACTION CODES from the Textfield Names on the Send Form.
					switch(VHCodes){
			
					case 'R':	//Parses for Required Only
											Rtest = Required(formObj.elements[i].value);
											if(!Rtest){
												msg+=" - A Value is Required for "+fieldname+'...\n';
												isOK[i] = false;
											}else isOK[i] = true;
											
											break;
											
					case 'RS': // Parses for Required String - Alpha Chars Only
											Rtest = Required(formObj.elements[i].value);
											if(!Rtest){
												msg+=" - An Alphabetic Value is Required for "+fieldname+'... No Numbers\n';
												isOK[i]=false;
											}else{
													fldval = new String(formObj.elements[i].value);
													rex = /[\d]/gi; //Any Digit
													stripval = fldval.replace(rex,''); // Replaced with null space
													formObj.elements[i].value = stripval;
													//:DEBUG:		alert('RS Stripped : '+i+'  '+stripval);
											 	isOK[i] = true;
											 }
											break;
											
					case 'RN': // Parses for Required Numbers - Numeric Only
											Rtest = Required(formObj.elements[i].value);
											if(!Rtest){
												msg+=" - A Numerical Value is Required for "+fieldname+'... No Characters\n';
												isOK[i]=false;
											}else{
												fldval = new String(formObj.elements[i].value);
												rex = /[\D|^-|^\/]/gi; //Any Character
												stripval = fldval.replace(rex,'');// Replace all Alpha Charac with null space.
												formObj.elements[i].value = stripval;
												//:DEBUG: 		alert('RN Stripped '+i+' '+stripval);			
												
											 isOK[i] = true;
											 }
											break;
											
					case 'RSN':// Parses for Required  - Alpha,Numeric and Punctuation Chars : All Characters are OK
											//alert('String NUmbers and Required');
											Rtest = Required(formObj.elements[i].value);
											if(!Rtest){
												msg+=" - You Must Enter a Value for the field of "+fieldname+"... \n";
												isOK[i]=false;
											}else{
												isOK[i] = true;
											}
											break;
											
					case 'RE': // Parses for Required Email - Email Address only.
											//:DEBUG: 	alert('Required Email Address');
											Rtest = Required(formObj.elements[i].value);
											if(!Rtest){
													msg+=" - You Must Enter a valid Email address in the "+fieldname+" ... \n";
													isOK[i]=false;
											}else{
										
													//if(isEmail)isOK[i]=true;
													if(ParseEML(formObj.elements[i].value))isOK[i]=true;
													else isOK[i]=false;												
													}
											break;
											
					case 'S': 
									// Parses for String - ENFORCED Alpha Chars Only
													fldval = new String(formObj.elements[i].value);
													rex = /[\d]/gi; //Any Digit
													stripval = fldval.replace(rex,''); // Replaced with null space
													formObj.elements[i].value = stripval;
													//:DEBUG:		alert('RS Stripped : '+i+'  '+stripval);
											 	isOK[i] = true;
											break;
											
					case 'N': // Parses for Numbers - ENFORCED Numeric Only
												fldval = new String(formObj.elements[i].value);
												rex = /[\D^-^\/]/gi; //Any Character
												stripval = fldval.replace(rex,'');// Replace all Alpha Charac with null space.
												formObj.elements[i].value = stripval;
												//:DEBUG: 		alert('RN Stripped '+i+' '+stripval);			
												
											 isOK[i] = true;
											break;
											
					case 'SN':// Parses for Required  - Alpha,Numeric and Punctuation Chars : All Characters are OK
												isOK[i] = true;
											break;
											
					case 'E': // Parses for Required Email - Email Address only.
											//:DEBUG: 	alert('Required Email Address');
											if(formObj.elements[i].value != '' && formObj.elements[i].value != undefined){
													if(ParseEML(formObj.elements[i].value))isOK[i]=true;
													else isOK[i]=false;												
											}else{
													isOK[i]=true;
											}		
											break;
											

					case 'RGP': 
												//DEBUG:	alert(formObj.elements[i].value+' checked ='+formObj.elements[i].checked);
											if(!valueSelected){
											//NOTE:2DO: Can't Check for Required : as Yet 	
												
												if(!formObj.elements[i].checked){
															formObj.elements[i].value='';
															//isOK[i] = false; // don't want it specified in the Final Check Array. IT HAS NO VALUE
															msg+=" - You Must Select an Option for the "+fieldname+" ... \n";
															
													}else	{
															//DEBUG:		alert('Value Set : '+formObj.elements[i].value);
															isOK[i] = true;
															valueSelected=true;
															//DEBUG:		alert('Good a value : '+formObj.elements[i].value);
													}
												//}	//end For							
											
											}//end if(valueSelected)
																
										break;						
										
				case 'GP':		
										//alert('GP Instantiated ');	
										//RadioGrp = new Array();
										RadioGrp = formObj.elements[i];
										//	alert('__Pets Present'+RadioGrp.checked);
										
											if(RadioGrp.checked) formObj.elements[i].value = RadioGrp.value;
											else formObj.elements[i].value = null;
										
										//		alert("Pet  value = "+ RadioGrp.value);
										isOK[i] = true;
																
										break;						

				case 'RCH':
										Rtest = Required(formObj.elements[i].value);
										if(!Rtest){
											msg+=" - You Must Check one of the Checkboxes for the field of "+fieldname+"... \n";
											isOK[i]=false;
										}else	isOK[i] = true;
										
										break;

				default:	isOK[i] = true;//alert('No Action Code');
									break;
				
					}//END SWITCH

				//if(isOK[i]==false)allOK=false; // ERROR CATCHER - Trips the AllOK var Stopping send

							//:DEBUG:	if(!allOK)alert(fieldname+' '+allOK);	//:DEBUG:
		}//IF	- Bad Values No Parse
	}//END FOR formObj.elements.array.length


		//DEBUG:		alert('isOK array = '+isOK[7]);
	if(isOK.length > 0){
		for(i=0;i<isOK.length;i++){
			//DEBUG:	alert('isOK '+isOK[i]);
				if(isOK[i]==false){
					allOK=false
				}else if(allOK)allOK=true;
							else{ 
								allOK=false;
								//break;
							}
								
		}//end For
	}//end If
	
	if(allOK){				//DEBUG:		alert('Sending');

		for(i=0;i<formObj.elements.length;i++){
							//THIS CODE: Replaces the Underscores back into the Fieldnames (Minus the Validation Handling Codes) therefore 
							//Formatting the Fieldname for final single Underscore stripping in EML_MOD.INC.PHP
			if(formObj.elements[i].name != '' && formObj.elements[i].name != undefined){
						//DEBUG:		alert(formObj.elements[i].name); 
				formObj.elements[i].name = formObj.elements[i].name.substring(formObj.elements[i].name.indexOf('__')+2);
						//DEBUG:		alert('Final Fieldname : '+formObj.elements[i].name+' : I = '+i);
			}//end Replacer
		}//end For

		return(true);
		//DEBUG:		return(false);
		
	}else{
		Display_Errors(msg);
		return(false);			//DEBUG:	alert('send FAIL');
	}
	

	
}//--END VALIDATE

//--------------------------------
function Display_Errors(msg){
	Emsg="ERROR - \n The Data in the Follwing Fields has Errors\n Please Re-Enter the Following fields...\n\n"+msg;
	alert(Emsg);
	return(true);
}

//--------------------------------
function Required(val){
	if(val != '')return val;
	else return(false);
}

function ParseEML(EML){

	atPos = EML.indexOf('@');
	if(atPos == -1){
		msg+="Email Address must have the '@' symbol \n Please Re-enter the Email Address.";
	}else{
		//alert(formObj.elements[i].value.indexOf('@')+' IS EMIAL'); // HAS '@' Symbol
		//  au.ey.com

		dotpos = EML.indexOf('.',atPos)+1;
		strleng = EML.length;
		dotCount = strleng - dotpos;
			//DEBUG:	alert(dotCount);
		if(dotCount >= 6 ){
				Ldotpos = EML.lastIndexOf('.')+1;								
				dotCount = strleng - Ldotpos;
				//:DEBUG:alert('Last Dot pos : '+dotCount);	//:DEBUG:
				if(dotCount == 2){
					isEmail = true;
				}else{
					msg+="Email Address is badly formed around the country code .nn \nNo More than 2 characters after the last Dot\n";
					isEmail = false;
				}//fi dotCount
			}else{
			dotCount = strleng - dotpos;
			if(dotCount == 3){
					//DEBUG:	alert("One Dot ONLY");
				isEmail = true;
			}else{
				 isEmail = false;
				 msg+="Email Address is badly formed around the .com \n";
			}//END if dotCount
		}//END if DotCount >= 6
	}//End if atPos.value == @)


			return(isEmail);
}
//-->	


