//Limit the character count
function limitCharCount(element,count) {
	if(element.value.length > count) {
		var string = element.value;
		element.value = string.substr(0,count);
		alert("Character limit reached!");
	}
}

//Validate emial
function isEmail (s) {
	var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
	return String(s).search (isEmail_re) != -1;
}

function validateRadioButton(aform,name,message) {
	myOption = -1;

	var numRadioButtons =  eval("aform."+name+".length");
		
	for (i=0;i<numRadioButtons;++i) {
		if (eval("aform."+name+"["+i+"].checked")) {
			return "";
		}
	}
	
	return message + '\n';
}

//Validate Form
function validateForm(aform) {
	var user_level;
	var el = aform.elements;
	var errorMsg = '';
	var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
	
	var interestErr = 'Select at least 1 Area of Interest' + '\n';
	var interestFlag = false;
	
	for(var i = 0 ; i < el.length ; ++i) {
		var googleToolbar = el[i].title;
		
		if(googleToolbar.substr(0,11) == "Your Google") {continue;}
		
		if(el[i].name == 'email') {
				if(isEmail(el[i].value) == false) {
					errorMsg += 'Invalid Email' + '\n';
				}
		} else if(el[i].type == 'checkbox') {
			if(el[i].checked == false && el[i].title.length > 0) {
				errorMsg += el[i].title + '\n';
			}/* else if(el[i].checked == true && (el[i].id == 'interestCheckbox')) { //Erase the error msg
				interestErr = '';
			} else if(el[i].checked == false && (el[i].id == 'interestCheckbox')) { //if interest occurs, mark it
				interestFlag = true;
			}*/
		} else if (el[i].type == 'select-one') {
			if(el[i].selectedIndex < 1 && el[i].title.length > 0) {
				errorMsg += el[i].title + '\n';
			}
		} else if (el[i].type == 'radio') {
			/*if(el[i].title == "Product" && productFlag == true) {
				productFlag = false;
				errorMsg += validateRadioButton(aform,'itemProducts','Product');
			} else if (el[i].title == "Industry" && industryFlag == true) {
				industryFlag = false;
				errorMsg += validateRadioButton(aform,'itemIndustries','Industry');
			}*/
		/*} else if (el[i].name == 'content') { //FCK Editor
			var FCKString = FCKeditorAPI.GetInstance('content').GetXHTML();
			if(FCKString.length == 0) { 
				errorMsg += el[i].title + '\n';
			}*/
		} else if (el[i].title.length > 0) {
			if(el[i].value.length == 0) {
				errorMsg += el[i].title + '\n';
			}
		}
	}
	
	/*if(interestFlag && interestErr) {
		errorMsg += interestErr + '\n';
	}*/
	
	if(errorMsg.length > 0) {
		errorMsg = 'Please complete all required fields below:\n'+errorMsg;
		alert(errorMsg);
		return false;
	} else {
		return true;
	}
	
	return false;
}

function checkForOther(element) {
	var optionValue = element.options[element.selectedIndex].value;
	if(optionValue == "Other") {
		document.getElementById('other-heard').style.display = '';
		document.getElementById('other-heard-input').title = 'How did you hear about us (other)?';
	} else {
		document.getElementById('other-heard').style.display = 'none';
		document.getElementById('other-heard-input').title = '';
	}
}

