String.prototype.trim = function() {
  		return this.replace(/^\s*|\s*$/g, "")
}

function checkEmail(str) {
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  	var isOK = !r1.test(str) && r2.test(str);
//	if (!isOK) alert("Email id ("+str+") is invalid\nCheck the format and remove comma at the end if any");
	return true; //isOK;
 }

function checkForm(form) {
	returnVal = true;
	for (i=0; i < form.elements.length; i++) {
		ele = form.elements[i];
		if (ele.name.substr(0, 2)=="x_" && ele.value.trim()=="" ) {
			alert(ele.name.substr(2)+" is required. Please provide a valid value!");
			ele.focus();
			returnVal = false;
			break;
		}

		if ((ele.name.substr(0, 2)=="x_" || ele.value.trim()!="" )
		   && (ele.name.toLowerCase().indexOf("email") !=-1))  {
			if ( ele.value.indexOf(",") != -1 ) {
				emails = ele.value.split(",");
				for ( i = 0; i<emails.length; i++) {
					returnVal = checkEmail(emails[i].trim());
						if (!returnVal) break;
					}
			} else returnVal = checkEmail(ele.value.trim());

			if (!returnVal) {
					alert("Email id ("+ele.value.trim()+") is invalid\nCheck the format and remove comma at the end if any");
					ele.focus();
					break;
			}
		}
	}
	return returnVal;
}
