
function CheckForm(_name) {
	// Validation classes:
	// :: inputstring -> a textfield input with the class 'inputstring', checks to make sure it not empty
	// :: inputemail -> a textfield input with the class 'inputemail', checks to make sure it is a valid email address
	// :: textareastring -> a textarea with the class 'textareastring', checks to make sure it is not empty

// create array to contain items with an error
    var error_items = new Array();
	// clear error messages
	document.getElementById('errormsg').style.margin = '0';
	document.getElementById('errormsg').innerHTML = "";
    // get the form that needs to be validated
    var inputs = document.getElementsByTagName('input');
	// and iterate through them...
	for (var i=0;i<inputs.length;i++){
		if (inputs[i].className.indexOf('inputstring')>-1){
			inputs[i].className = inputs[i].className.replace("error","");
			if(inputs[i].value == ''){
				inputs[i].className += " error";
				var newErrorObj = new Object();
				newErrorObj.tagtype = 'input';
				newErrorObj.thisname = inputs[i].name;
				newErrorObj.thisclass = 'inputstring';
				error_items[error_items.length++] = newErrorObj;
			}
		} else if (inputs[i].className.indexOf('inputemail')>-1){
			inputs[i].className = inputs[i].className.replace("error","");
			if(!checkEmail(inputs[i].value)){
				inputs[i].className += " error";
				var newErrorObj = new Object();
				newErrorObj.tagtype = 'input';
				newErrorObj.thisname = inputs[i].name;
				newErrorObj.thisclass = 'inputemail';
				error_items[error_items.length++] = newErrorObj;
			}
		} else if (inputs[i].className.indexOf('inputpassword')>-1){
			inputs[i].className = inputs[i].className.replace("error","");
			if(!checkPassword(inputs[i].value)){
				inputs[i].className += " error";
				var newErrorObj = new Object();
				newErrorObj.tagtype = 'input';
				newErrorObj.thisname = inputs[i].name;
				newErrorObj.thisclass = 'inputpassword';
				error_items[error_items.length++] = newErrorObj;
			}
		}
	}
    var textareas = document.getElementsByTagName('textarea');
	// and iterate through them...
	for (var i=0;i<textareas.length;i++){
		if (textareas[i].className.indexOf("textareastring")>-1){
			textareas[i].className = textareas[i].className.replace("error","");
			if(textareas[i].value == ''){
				textareas[i].className += " error";
				var newErrorObj = new Object();
				newErrorObj.tagtype = 'input';
				newErrorObj.thisname = textareas[i].name;
				newErrorObj.thisclass = 'textareastring';
				error_items[error_items.length++] = newErrorObj;
			}
		}
	}
	if(error_items.length>0){
		document.getElementById('errormsg').style.margin = '10px 0 0';
		document.getElementById('errormsg').innerHTML += "<strong>Please fill in the following fields correctly:</strong><ul>";
		for(var i=0;i<error_items.length;i++){
			document.getElementById('errormsg').innerHTML += "<li>" + error_items[i].thisname + "</li>";
		}
		document.getElementById('errormsg').innerHTML += "</ul>";
		return false;
	} else {
		return true;	
	}
}

function clearField(_theField, _theText) {
	if(_theField.value == _theText){
		_theField.value = "";		
	}
}

function checkEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Invalid E-mail ID")
		return false;
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		//alert("Invalid E-mail ID")
		return false;
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Invalid E-mail ID")
		return false;
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		//alert("Invalid E-mail ID")
		return false;
	 }	
	 if (str.indexOf(" ")!=-1){
		//alert("Invalid E-mail ID")
		return false;
	 }
	 return true;				
}

function checkPassword (strng) {
	var error = "";
	if (strng == "") {
		//alert("Please enter a password");
		return false;
	}
	var illegalChars = /[\W_]/; // allow only letters and numbers
	if ((strng.length < 5) || (strng.length > 20)) {
		//alert("The password needs to be between 5 and 20 characters long.");
		return false;
    }
	else if (illegalChars.test(strng)) {
		//alert("The password contains illegal characters. Only letters and numbers will be accepted.");
		return false;
	}
	return true;
}