/*	filename: http://images.military.com/gibill/gibill_validator.js
	savedate: 05MAR08 */


function addClass(target, classValue) {
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	if (!pattern.test(target.className)) {
		if (target.className == "") {
			// if target has no class yet, give it ours
			target.className = classValue;
		} else {
			// otherwise, append ours
			target.className += " " + classValue;
		}
	}
	return true;
}

function removeClass(target, classValue) {
	var removedClass = target.className;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	removedClass = removedClass.replace(pattern, "$1");
	removedClass = removedClass.replace(/ $/, "");
	
	target.className = removedClass;
	
	return true;
}

function signalError() {
	var x=document.getElementById("right"); // div#right
	addClass(x, "rightErr");

	var y=document.getElementById("chkHeader"); // h3#chkHeader
	var txt=document.createTextNode("Fields in RED Below Are Required");
	var oldTxt = y.replaceChild(txt, y.firstChild);


}

function checkMail(thisField, emailLabel, required)
{
   var result = true;
   var x = thisField.value.toLowerCase();
   var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
   var invalidEmails = new Array ("unknown@unknown.com","none@none.com","noemail@aol.com", "noemail@yahoo.com", "noemail@hotmail.com", "noemail@netzero.com", "noemail@juno.com", "noemail@netscape.com", "noemail@prodegy.net", "noemail@msn.net");
   var isEmpty = false ;
   if(x == "" || x == null) { isEmpty = true; }

   if(required && isEmpty)
   {
	// alert('Please enter a value for the "' + emailLabel + '" field.');
	thisField.focus();
	result = false;
   }

   if(result && !isEmpty && !filter.test(x))
   {
	// alert('Please enter a valid ' + emailLabel + ' value.');
	thisField.focus();
	result = false;
   }

   for(i=0; i<invalidEmails.length; i++)
   {
	if(result && invalidEmails[i] == x)
   	{
		// alert('Please enter a valid ' + emailLabel + ' value.');
		thisField.focus();
		result = false;
   	}
   }

   return result;
}

function validZipcode(formField, fieldLabel, strCountry, required)
{
	var result = true;
	var filter = /(^\d{5}[\s-]?$)|(^\d{5}[\s-]?\d{4}$)/;
	var filterCA = /^[a-zA-Z]\d[a-zA-Z][\s]?\d[a-zA-Z]\d$/; // kludge to check Canadian zips too
	var x = formField.value;
	var isEmpty = false ;

	if(strCountry == "US")
	{
		if(x == "" || x == null) { isEmpty = true; }

		if(required && isEmpty)
		{
			// alert('Please enter a value for the "' + fieldLabel + '" field.');
			formField.focus();
			result = false;
		}

		if(result && !isEmpty && !(filter.test(x) || filterCA.test(x)) ) // kludge to check Canadian zips too
		{
			// alert('Please enter a valid zip code.');
			formField.focus();
			result = false;
		}
	}
	return result;
}
function validateform() {
	// alert('Initiating form validation.');
	with(document.entryform) {
		var foundErrors = false;
		var validate_status_code	= document.getElementById("selectEduGoal");
		var validate_service_code	= document.getElementById("selectService");
		var validate_email		= document.getElementById("inputEmail");
		var validate_zipcode		= document.getElementById("inputZipCode");
		var validate_rb_agree		= document.getElementById("rb_agree");
		
		var valElem_ed1=document.getElementById("chkEduGoal");
		var valElem_ed2=document.getElementById("chkEduGoal2");
		var valElem_svc1=document.getElementById("chkService");
		var valElem_svc2=document.getElementById("chkService2");
		var valElem_em1=document.getElementById("chkEmail");
		var valElem_em2=document.getElementById("chkEmail2");
		var valElem_zc1=document.getElementById("chkZIP");
		var valElem_zc2=document.getElementById("chkZIP2");
		var valElem_rba=document.getElementById("chkAgree");
		
		if(validate_status_code.selectedIndex <= 0 ) {
			addClass(valElem_ed1, "red");
			addClass(valElem_ed2, "");
			// alert("Please select your education goal.");
			// return false;
			foundErrors = true;
		} else {
			removeClass(valElem_ed1, "red");
			removeClass(valElem_ed2, "");
		}
		
		if(validate_service_code.selectedIndex <= 0 ) {
			addClass(valElem_svc1, "red");
			addClass(valElem_svc2, "");
			// alert("Please select your Service.");
			// return false;
			foundErrors = true;
		} else {
			removeClass(valElem_svc1, "red");
			removeClass(valElem_svc2, "");
		}
		
		if(!checkMail(validate_email, "Email", true)) {
			addClass(valElem_em1, "red");
			addClass(valElem_em2, "");
			// return false;
			foundErrors = true;
		} else {
			removeClass(valElem_em1, "red");
			removeClass(valElem_em2, "");
		}
		
		if(!validZipcode(validate_zipcode, "Zip Code", "US", true)) {
			addClass(valElem_zc1, "red");
			addClass(valElem_zc2, "");
			// return false;
			foundErrors = true;
		} else {
			removeClass(valElem_zc1, "red");
			removeClass(valElem_zc2, "");
		}
		
		if(validate_rb_agree.checked == false) {
			addClass(valElem_rba, "red");
			// alert("We're sorry: in order to submit the form , you must review and agree to the Military.com Service Agreement");
			// return false;
			foundErrors = true;
		} else {
			removeClass(valElem_rba, "red");
		}
		
		if(foundErrors == true ) {
			signalError();
			return false;
		}
		formsource.value = GetCookie("SRCT");
	}//with
	return true;
}//validateform
