var alertMsg = "";

startValidate = function(form){
	alertMsg = "";
	var formScope = form.length;
	var valid = 0;
	for (i = 0; i < formScope; i++){
		var preValid = valid;
		var itemName = form.elements[i].id;
		if (itemName.charAt(0) == "V"){
			var validStop = itemName.indexOf("_") - 1;
			var idMessage = itemName.substr((validStop + 2));
			switch(itemName.substr(1,validStop)){
				case "date":
					if (dateValidate(form.elements[i]) == 1){
						alertMsg = alertMsg + " " + idMessage + " must only contain numbers, /'s and -'s.\n";
						valid++;
					}
					break
				case "ssn":
					if (ssnValidate(form.elements[i]) == 1){
						alertMsg = alertMsg + " " + idMessage + " must only contain numbers, spaces and -'s.\n";
						valid++;
					}
					break
				case "phone":
					if (phoneValidate(form.elements[i]) == 1){
						alertMsg = alertMsg + " " + idMessage + " must contain an area code, spaces, -'s and ()'s.\n";
						valid++;
					}
					break
				case "email":
					if (emailValidate(form.elements[i]) == 1){
						alertMsg = alertMsg + " " + idMessage + " must be a proper email address.\n";
						valid++;
					}
					break
				case "alpha":
					if (alphaValidate(form.elements[i]) == 1){
						alertMsg = alertMsg + " " + idMessage + " must only contain alpha characters.\n"
						valid++;
					}
					break
				case "numeric":
					if (numericValidate(form.elements[i]) == 1){
						alertMsg = alertMsg + " " + idMessage + " must only contain numeric characters.\n"
						valid++;
					}
					break
				case "html":
					if (htmlValidate(form.elements[i]) == 1){
						alertMsg = alertMsg + " " + idMessage + " can not contain HTML tags.\n";
						valid++;
					}
				default:
					if (form.elements[i].value == ""){
						valid = valid + 1;
						alertMsg = alertMsg + " " + idMessage + " is required.\n";
					}
			}
		}
	}
	if (valid > 0){
		alert(valid + " fields are invalid:\n" + alertMsg);
		return false;
	} else {
		return true;
	}
}

dateValidate = function(item){
	var value = item.value;
	
	if (value == ""){
		return 1;
	}
	
	//remove / - .
	value = value.replace(/\//gi,"");
	value = value.replace(/-/gi, "");
	value = value.replace(/\./gi, "");
	
	if (isNaN(value) == true){
		return 1;
	}
	
	if (value.length < 4){
		return 1;
	}
			
	return 0;
}

ssnValidate = function(item){
	var value = item.value;
	
	if (value == ""){
		return 1;
	}
	
	//remove -, spaces
	value = value.replace(/-/gi, "");
	value = value.replace(/ /gi, "");
	
	if (isNaN(value) == true){
		return 1;
	}
	
	if (value.length != 9){
		return 1;
	}
	
	return 0;
}

phoneValidate = function(item){
	var value = item.value;
	
	if (value == ""){
		return 1;
	}
	
	//remove () - . spaces
	value = value.replace(/\(/gi, "");
	value = value.replace(/\)/gi, "");
	value = value.replace(/-/gi, "");
	value = value.replace(/\./gi, "");
	value = value.replace(/ /gi, "");
	
	if (isNaN(value) == true){
		return 1;
	}
	
	if (value.length != 10){
		return 1;
	}
	
	return 0;
}

emailValidate = function(item){
	var value = item.value;
	
	var reEmail = new RegExp("[A-Za-z0-9_\.]+@{1}[A-Za-z0-9\-]+?\.[A-Za-z]{2,6}");
	
	if (value == ""){
		return 1;
	}

	if (value.search(reEmail) == -1){
		return 1;
	}
	
	return 0;
}

alphaValidate = function(item){
	var value = item.value;
	var badStuff = "!@#$%^&*()_+-={}|[]\:;'<>?,./~`"
	if (value == ""){
		return 1;
	}
	for (j = 0; j < value.length; j++){
		if (isNaN(value.charAt(j)) == false || badStuff.indexOf(value.charAt(j)) != -1) {
			return 1;			
		}
	}
	
	return 0;
}

numericValidate = function(item){
	var value = item.value;
	
	if (value == ""){
		return 1;
	}
	
	if (isNaN(value)){
		return 1;
	}
	
	return 0;
}

htmlValidate = function(item){
	var value = item.value;
	
	if (value == ""){
		return 1;
	}
	
	var badStuff = new Array();
	badStuff[0]="<a";
	badStuff[1]="<b";
	badStuff[2]="<div";
	badStuff[3]="<u";
	badStuff[4]="<i";
	badStuff[5]="<strong";
	badStuff[6]="<span";
	badStuff[7]="<font";
	badStuff[8]="<table";
	badStuff[9]="<tr";
	badStuff[10]="<td";
	badStuff[11]="<form";
	badStuff[12]="<input";
	badStuff[13]="<select";
	badStuff[14]="<p";
	badStuff[15]="<h";
	badStuff[16]="<br";
	
	for (var i = 0;i<badStuff.length;i++){
		if(value.indexOf(badStuff[i]) != -1){
			return 1
		}
	}
	
	return 0;	
}