function frmValidate(inForm) {
	spanID = "msgBlock";
	isValid = true;
	errorMsg = "";
	clearErHandler(inForm);
	for (i=0; i<inForm.length; i++) {
		if(inForm[i].id.indexOf("_req")>0){
			if(inForm[i].value =="") {
				errorMsg = errorMsg + "<dt><b>" + document.getElementById(inForm[i].id + "_lb").innerHTML + "</b></dt>";
				document.getElementById(inForm[i].id + '_lb').className="textlabelreq";
				document.getElementById(inForm[i].id + '_lb').childNodes[0].innerHTML = "! *";
				errorMsg = errorMsg + "<dd>This field is required.</dd>";
				isValid = false;
			} else {
				if(inForm[i].id.indexOf("email")>-1){
					if(echeck(inForm[i].value) != "") {
						errorMsg = errorMsg + "<dt><b>" + document.getElementById(inForm[i].id + '_lb').innerHTML + "</b></dt>";
						document.getElementById(inForm[i].id + '_lb').className="textlabelreq";
						document.getElementById(inForm[i].id + '_lb').childNodes[0].innerHTML = "! *";
						errorMsg = errorMsg + echeck(inForm[i].value);
						isValid = false;
					}
				}
			}
		}
	}
	if(isValid == false) {
		createErrorBlock(spanID);
	} else {
		inForm.emailFrom.value = inForm.emailAddress_req.value;
		inForm.emailBody.value = createMsgBody(inForm);
		inForm.submit();;
	}
}

function createMsgBody(inForm){
	strBodyMsg = "";
	for (i=0; i < inForm.length; i++) {
		if(inForm[i].type != "hidden" ) {
			try {
				var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
				var elem = document.getElementById('id');
			if(!hasInnerText){
				strBodyMsg = strBodyMsg + document.getElementById(inForm[i].id + '_lb').elem.textContent + " " + inForm[i].value + "\n";
			} else {
				strBodyMsg = strBodyMsg + document.getElementById(inForm[i].id + '_lb').elem.innerText + " " + inForm[i].value + "\n";
			}
			} catch (er){
				strBodyMsg = strBodyMsg + inForm[i].name + ": " + inForm[i].value + "\n";
			}
		}
	}
	for (i=0; i < inForm.length; i++) {
		if (inForm[i].type != "hidden"){
			x=0;
			while(x < inForm.length){
			try{
				inForm[i].value = "";
				var objecttodestroy = document.getElementById(inForm[i].id);
				var parentelement = objecttodestroy.parentNode;
				parentelement.removeChild(objecttodestroy);
			} catch (er) {
			x++
			}
		}
	}
}
return(strBodyMsg);
}

function clearErHandler(inForm){
	spanID = "msgBlock";
	document.getElementById(spanID).style.display = "none";
	for (i=0; i < inForm.length; i++) {
		if(inForm[i].id.indexOf("_req") > 0){
			document.getElementById(inForm[i].id + '_lb').className="textlabel";
			document.getElementById(inForm[i].id + '_lb').childNodes[0].innerHTML = "*";
		}
	}
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return ("<dd>Your email is not in a valid format.</dd>")
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return ("<dd>Invalid E-mail ID</dd>")
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return ("<dd>Invalid E-mail ID</dd>")
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return ("<dd>Invalid E-mail ID</dd>")
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return ("<dd>Invalid E-mail ID</dd>")
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return ("<dd>Invalid E-mail ID</dd>")
	}
	if (str.indexOf(" ")!=-1){
		return ("<dd>Invalid E-mail ID</dd>")
	}
	return "";
}

function createErrorBlock(spanID) {
	msgBlock_html = "";
	document.getElementById(spanID).style.display = "block";
	msgBlock_html = msgBlock_html + '<h3 class="a700">Sorry, But An Error Has Been Made.</h3>';
	msgBlock_html = msgBlock_html + '<div class="b701">';
	msgBlock_html = msgBlock_html + '<div class="w03">';
	//msgBlock_html = msgBlock_html + '<p><b>$Primary error message goes here, see use cases.</b></p>';
	msgBlock_html = msgBlock_html + '<p><b>Please check the following field(s):</b></p>';
	msgBlock_html = msgBlock_html + '<dl class="error">';
	msgBlock_html = msgBlock_html + errorMsg;
	msgBlock_html = msgBlock_html + '</dl></div></div></span>';
	document.getElementById(spanID).innerHTML = msgBlock_html;
}
