//form validation
function ValidateForm(form, lang, email) 
{
	//alert(form);
	var validate_email = email; // This fieldname is for email format validation
	var form = document[form];
	var lang = lang.toLowerCase();
	var error = text = fieldnames = langfieldnames = validemail = strIntro = confirm_validate_email = "";
	var required = form.required_fields.value;
	var fieldarray = required.split("|");
	if (email.indexOf(",") > 0)
	{
		aryemail = email.split(",");
		validate_email = aryemail[0];
		confirm_validate_email = aryemail[1];
		//alert(validate_email);
		//alert(confirm_validate_email);
	}
	
	for (i=0; i<fieldarray.length; i++)
	{
		req_field_value = fieldarray[i].split("^");
		fieldnames = (fieldnames + "|" + req_field_value[0]);
		langfieldnames = (langfieldnames + "|" + req_field_value[1]);
	}
	langfieldnames = langfieldnames.substring(1,langfieldnames.length); 
	langarray = langfieldnames.split("|");
	fieldnames = fieldnames.substring(1,fieldnames.length);
	array = fieldnames.split("|");
	
	for (i=0; i<array.length; i++)
	{
		if (form[array[i]].type == "radio" || form[array[i]].type == "checkbox" || typeof(form[array[i]].type) == "undefined")
		{
			//Radio field
			var bolSelected;
			if (typeof(form[array[i]].type) != "undefined")
			{
				// Single field radio/checkbox
				//alert(array[i] + ", " + form[array[i]].type + ", " + typeof(form[array[i]].type) + ", " + "single");
				intLength = 0;
				//A single named field
				if (form[array[i]].checked == true)
				{
					//alert("Checked");
					bolSelected = true;
				}
			}
			else
			{
				//multiple field radio/checkbox
				intLength = form[array[i]].length;
				for (j = 0; j < intLength; j++)
				{
					//alert(intLength + typeof(intLength) + form[array[i]].name + "=" + j + ", " + form[array[i]][j].checked);
					//alert(form[array[i]][j].checked);
					if (form[array[i]][j].checked == true)
					{
						bolSelected = true;
					}			
				}				
			}
			if (bolSelected != true)
			{
				//alert("create error");
				error++;
				if (error < 2)
				{
					if (intLength == 0)
					{
						form[array[i]].focus();
					}
					else
					{
						form[array[i]][0].focus();
					}
				}
				text += " - " + replace(langarray[i], "_", " ") + "\n";
				
			}
			bolSelected = false;			
		}
		else if (form[array[i]].type == "select-one")
		{
			//select-one field
			if (form[array[i]].options[form[array[i]].selectedIndex].value != "")
			{
				bolSelected = true;
			}
			if (bolSelected != true)
			{
				error++;
				if (error < 2)
				{
					form[array[i]].focus();
				}
				text += " - " + replace(langarray[i], "_", " ") + "\n";
			}
			bolSelected = false;
		}
		else if (form[array[i]].type == "select-multiple")
		{
			//select-multiple field
			intLength = form[array[i]].length;
			for (j = 0; j < intLength; j++)
			{
				if (form[array[i]][j].selected == true)
				{
					if(form[array[i]][j].value != "")
					{
						bolSelected = true;
					}
				}		
			}
			if (bolSelected != true)
			{
				error++;
				if (error < 2)
				{
					form[array[i]].focus();
				}
				text += " - " + replace(langarray[i], "_", " ") + "\n";
			}
			bolSelected = false;
		}
		else
		{
			//Text or hidden field
			if ((array[i] == validate_email))
			//if ((array[i] == validate_email))
			{
				/*emailfieldvalue = form[array[i]].value;
				atloc = emailfieldvalue.indexOf('@');
				dotloc = emailfieldvalue.indexOf('.',atloc);
				emaillength = emailfieldvalue.length;
				
				if ((atloc < 1) || (dotloc < (atloc + 2)) || ((emaillength - 3) < dotloc))
				{
					error++;
					if (error < 2)
					{
						form[array[i]].focus();
					}
					text += " - ##validemail## \n"; 
				}*/
				var emailfieldvalue=new String(form[validate_email].value);
				var filter=/^[a-z0-9]([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$/i;
				if (!filter.test(emailfieldvalue))
				{
					error++;
					if (error < 2)
					{
						form[array[i]].focus();
					}
					text += " - ##validemail## \n";
				}
				//check confirm addy matches
				
	//alert(confirm_validate_email);
				if (confirm_validate_email != "")
				{
					if (form[validate_email].value != form[confirm_validate_email].value)
					{
						error++;
						text += " - ##confirmvalidemail## \n";
					}
				}
			}
			else if (form[array[i]].value == "")
			{
				error++;
				if (error < 2)
				{
					form[array[i]].focus();
				}
				text += " - " + replace(langarray[i], "_", " ") + "\n";
			}
		} 
	}
	if (error) 
	{
		switch (lang)
		{
			//resolve languages
			case "fr":
				validemail = "adresse e-mail valide";
				confirmvalidemail = "email addresses must match";
				text = replace(text, "##validemail##", validemail); //Do Not edit this line
				text = replace(text, "##confirmvalidemail##", confirmvalidemail); //Do Not edit this line
				if (error > 1)
				{
					//plural error
					strIntro = "Veuillez remplir les champs suivants:\n";
				}
				else
				{
					//single error
					strIntro = "Veuillez remplir le champ suivant:\n";
				}				
				//alert ("Veuillez remplir le (ou les) champ(s) suivant(s):\n" + text);
			break;
			case "de":
				validemail = "Gültige E-Mail-Adresse";
				confirmvalidemail = "email addresses must match";
				text = replace(text, "##validemail##", validemail); //Do Not edit this line
				text = replace(text, "##confirmvalidemail##", confirmvalidemail); //Do Not edit this line
				if (error > 1)
				{
					//plural error
					strIntro = "Bitte füllen Sie folgendes Felder aus:\n";
				}
				else
				{
					//single error
					strIntro = "Bitte füllen Sie folgende Feld aus:\n";
				}				
				//alert ("Bitte füllen Sie folgende(s) Feld(er) aus:\n" + text);
			break;
			case "il":
				validemail = "ëú&#197;áú îééì îìàä";
				confirmvalidemail = "email addresses must match";
				text = replace(text, "##validemail##", validemail); //Do Not edit this line
				text = replace(text, "##confirmvalidemail##", confirmvalidemail); //Do Not edit this line
				if (error > 1)
				{
					//plural error
					strIntro = "àðà îìà àú äùã&#197;ú äáàéí:\n";
				}
				else
				{
					//single error
					strIntro = "àðà îìà àú äùãä äáà:\n";
				}
				text = replace(text, "- ", "");
				text = replace(text, "\n", " -\n");
				//alert ("You must fill in the following field(s):\n" + text);
			break;
			case "it":
				validemail = "Indirizzo email valido";
				confirmvalidemail = "email addresses must match";
				text = replace(text, "##validemail##", validemail); //Do Not edit this line
				text = replace(text, "##confirmvalidemail##", confirmvalidemail); //Do Not edit this line
				if (error > 1)
				{
					//plural error
					strIntro = "Compila i seguenti campi:\n";
				}
				else
				{
					//single error
					strIntro = "Compila il seguente campo:\n";
				}
			break;
			case "es":
				validemail = "Dirección de e-mail válida";
				confirmvalidemail = "email addresses must match";
				text = replace(text, "##validemail##", validemail); //Do Not edit this line
				text = replace(text, "##confirmvalidemail##", confirmvalidemail); //Do Not edit this line
				if (error > 1)
				{
					//plural error
					strIntro = "Debe rellenar el los campos siguientes:\n";
				}
				else
				{
					//single error
					strIntro = "Debe rellenar el campo siguiente:\n";
				}				
				//alert ("Debe rellenar el (los) campo(s) siguiente(s):\n" + text);
			break;
			case "nl":
				validemail = "Geldig email adres";
				confirmvalidemail = "email addresses must match";
				text = replace(text, "##validemail##", validemail); //Do Not edit this line
				text = replace(text, "##confirmvalidemail##", confirmvalidemail); //Do Not edit this line
				if (error > 1)
				{
					//plural error
					strIntro = "Kunt u de volgende velden invullen a.u.b.:\n";
				}
				else
				{
					//single error
					strIntro = "Kunt u het volgende veld invullen a.u.b.:\n";
				}				
				//alert ("Kunt u de volgende veld(en) invullen a.u.b.:\n" + text);
			break;
			case "at":
				validemail = "Gültige E-Mail Adresse";
				confirmvalidemail = "email addresses must match";
				text = replace(text, "##validemail##", validemail); //Do Not edit this line
				text = replace(text, "##confirmvalidemail##", confirmvalidemail); //Do Not edit this line
				if (error > 1)
				{
					//plural error
					strIntro = "Bitte füllen Sie folgendes Felder aus:\n";
				}
				else
				{
					//single error
					strIntro = "Bitte füllen Sie folgende Feld aus:\n";
				}
				//alert ("Bitte füllen Sie folgende(s) Feld(er) aus:\n" + text);
			break;
			default : 
				validemail = "Valid Email Address";
				confirmvalidemail = "email addresses must match";
				text = replace(text, "##validemail##", validemail); //Do Not edit this line
				text = replace(text, "##confirmvalidemail##", confirmvalidemail); //Do Not edit this line
				if (error > 1)
				{
					//plural error
					strIntro = "You must fill in the following fields:\n";
				}
				else
				{
					//single error
					strIntro = "You must fill in the following field:\n";
				}
				//alert ("You must fill in the following field(s):\n" + text);
		}
		alert (strIntro + text);
		return false; 
	}
	else 
	{
		return true;
	}
}

function replace(string, substring1, substring2)
{
	stringparts = string.split(substring1);
	string = stringparts.join(substring2);	
	return string;	
}

function wipemhfield(objField)
{
	if ((objField.value == "e-mail address") || (objField.value == "Password") || (objField.value == "Type key word here"))
	{
		objField.value = "";
	}
}
