//check fields Salim Dirani & Joseph Yammin, 18/may/2006
//checks all fields for validation.
//this function remove the white white spaces form the beginning and the end of a string.
function whiteTrim(string){
	//remove the white spaces from the beginning.
	while(string.substring(0,1) == " "){
		string = string.substring(1,string.length)
	}
	//remove the white spaces from the end
	while(string.substring(string.length-1, string.length) == " "){
		string = string.substring(0, string.length-1);
	}
	
	return string;
	
}
//check if the field is empty
function isEmpty(string){
	string = whiteTrim(string);
	if(string == "" || string == null){
	return true;
	}
	return false;
}
//check if the e-mail is valid
function isEmail(string){
	string = whiteTrim(string);
	var re = /^[a-z]{1}(\.?\w+)*@[a-z0-9](\.?[0-9a-z-]+)*\.[a-z]{2,4}$/i;
	var isValid =re.test(string);
	if (isValid == true){
		return true;
	}else{
	return false;	
	}
}
//check if the password is six characters long
function isPassword(string){
	string = whiteTrim(string);
	var re = /^[\w-?!.,]+$/i;
	var isValid = re.test(string);
	if(string.length >= 6){
		if(isValid == true){
			return true;
		}else{
			return false;
		}
	}
	return false;
}
//check for other field
function isName(string){
	string = whiteTrim(string);
	var re = /^[\w- ?,.]+$/i;
	var isValid = re.test(string);
		if(isValid == true){
			return true;
		}else{
			return false;
		}
	return false;
}
//escape the single and double quotes
function parseTextarea(string){
string = whiteTrim(string);
string = string.replace('"', '\"');
string = string.replace("'", "\'");
return string;
}

//this is the main function 
function checkFields(form){
	var bSubmit;
	forml = form.length;
	for(i=0;i<forml;i++){
		formType = form[i].type;
		if(formType == "checkbox" || formType == "radio" || formType == "button" || formType == "hidden" || formType == "submit"){
			continue;
			}
			if(formType == "text" || formType == "password"){
				var fieldvalue = form[i].name;
				var elError = document.getElementById(fieldvalue + "Failed");
				switch(fieldvalue){
					case "txtEmailR2":
					case "txtEmailR3":
					case "txtEmailR4":
					case "txtEmailR":
					var empty = isEmpty(form[i].value)
					var valid = isEmail(form[i].value)
					
					if(empty == true || valid == false){
						//var message = "Please enter a valid email befor procceding"
						//insertMessage(message, form[i]);
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					break;
					case "txtUsername":
					var empty = isEmpty(form[i].value)
					var valid = isName(form[i].value)
					if(empty == true || valid == false){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					break;
					case "txtFirstname":
					var empty = isEmpty(form[i].value)
					var valid = isName(form[i].value)
					if(empty == true || valid == false){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					break;
					case "txtSearchR":
					var empty = isEmpty(form[i].value)
					var valid = isName(form[i].value)
					if(empty == true || valid == false){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					break;
					case "txtLastnameR":
					var empty = isEmpty(form[i].value)
					var valid = isName(form[i].value)
					if(empty == true || valid == false){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					break;
					case "txtPassword2":
					case "txtPassword":
					var empty = isEmpty(form[i].value)
					var valid = isPassword(form[i].value)
					if(empty == true || valid == false){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					break;
					
					case "txtOccupationR":
					var empty = isEmpty(form[i].value)
					if(empty == true || valid == false){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					break;
					
					case "txtCountryR":
					var empty = isEmpty(form[i].value);
					var valid = isName(form[i].value);
					if(empty == true || valid == false){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					break;
					
					default:
					var valid = isName(form[i].value);
					var empty = isEmpty(form[i].value);
					if(valid == false && empty == false){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
					
					}
				
				}else if(form[i].tagName == "TEXTAREA"){
					var fieldvalue = form[i].name;
					var elError = document.getElementById(fieldvalue + "Failed");
					var taValue = parseTextarea(form[i].value);
					form[i].value = taValue;
					var empty = isEmpty(form[i].value);
					if(empty == true){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
				}else if(form[i].type == 'select-one' && form[i].name == "topic"){
					var nSelectedValue = form[i].selectedIndex;
					var selValue = form[i].options[nSelectedValue].value;
					var empty = isEmpty(selValue);
					var fieldvalue = form[i].name;
					var elError = document.getElementById(fieldvalue + "Failed");
					if(empty == true || selValue == "0"){
						elError.className = "error";
						bSubmit = false;
					}else{
						elError.className = "hidden";
					}
				}
		}
		if(bSubmit == false){
			scroll(0,200);
			return false;
			}
			return true;
	}