// *********************************************************************************
//  PWM:Verify script
//  Created by Paul Morton 
//  Created on 3/17/2010
//  Implementation:
//
//**  Site Level:
//  1.  Include pwm-scripts.js file (this file) in your scripts directory
//
//**  Page level
//  1.  Include the file in the page you want to verify in a scripts tag
//          <scripts type="text/javascript" src="/scripts/pwm-scripts"></scripts>
//      This can be done at a site level if using some header file for every page.
//  2.  Include this statement somewhere that it will run after the form 
//      loads (ie body:onload or in script tag after form).
//			PWM_CheckFlds(document.forms[0]);
//      For JQuery users just add this to the 
//           $(document).ready(function()
//
//  	if(typeof( window[ 'PWM_Verify' ] ) != "undefined")
//	                   PWM_CheckFlds(document.forms[0]);
//
//**  Form Level
//  1.  Replace your submit button to a button object with the name "Go"
//      Example: <input type="button" name="Go"	value="SUBMIT" />
//      ********  Working on eliminating this requirement.  ***********
//
//**  Field Level
//  1.  Include the Attribute PWM:Verify="A,B,Text String" in any field you 
//          want to make sure is not blank.
//       A is 1 if you want to verify on field blurring
//       B is 1 if you want to verify on form Submit
//       Text String is the message you want to be displayed if the field is blank.
//       Example: <input name="Name" type="text" value="" size="34" 
//                                   PWM:Verify="1,1,You must enter a name!" />
//       This will verify when a field is blurred, and when the form is submitted.
// *********************************************************************************    
var PWM_Verify = "1";  // used to detect the existence of the script file.

// function checks a particular field
function PWM_checkfld(obj1) {
	var msglist = obj1.getAttribute("PWM:Verify").split(',', 3);
	divname = obj1.name + 'Div';
	if (obj1.value == '') {
		obj1.style.background = '#FFFFB8';
		document.getElementById(divname).innerHTML = msglist[2];
	} else {
		obj1.style.background = '#ffffff';
		document.getElementById(divname).innerHTML = '';
	}
}

// function loops through fields and sets up on blur and on click events
// Also creates span tag to hold error message.
function PWM_CheckFlds(obj1) {
	if (obj1){
		for (i = 0; i < obj1.elements.length; i++) {
			if (obj1.elements[i].getAttribute("PWM:Verify")) {
				var msglist = obj1.elements[i].getAttribute("PWM:Verify").split(
						',', 3);
				if (msglist[0] == 1)
					obj1.elements[i].onblur = function onblur(event) {
					PWM_checkfld(this)
				};
				if (msglist[1] == 1)
					obj1.Go.onclick = function onclick(event) {
					PWM_SetErrs(obj1)
				};
				newdiv = document.createElement("SPAN");
				newdiv.setAttribute("name", obj1.elements[i].name + "Div");
				newdiv.setAttribute("id", obj1.elements[i].name + "Div");
				newdiv.setAttribute("style", "color: #FF0000; font-size: 9px");
				newdiv.setAttribute("align", "left");
				obj1.elements[i].parentNode.insertBefore(newdiv,
						obj1.elements[i].nextSibling);
			}
		}
	}
}

function PWM_SetErrs(obj1) {
	SubCheck = 0;
	for (i = 0; i < obj1.elements.length; i++) {
		if (obj1.elements[i].getAttribute("PWM:Verify")) {
			var msglist = obj1.elements[i].getAttribute("PWM:Verify").split(
					',', 3);
			if (msglist[1] == 1) {
				PWM_checkfld(obj1.elements[i]);
				if (obj1.elements[i].value == '') 
					SubCheck = 1;
			}
		}
	}
	if (SubCheck == 0)
		obj1.submit();
}
