/*
 *  Copyright 2005 Flight Path Creative L.L.C.
 *	$Date: 2009-12-29 13:58:13 -0500 (Tue, 29 Dec 2009) $
 *	$Revision: 192 $
 *	$URL: svn://localserver/manager/Stuff/trunk/js/validator.js $
 */

function validateForm(f)
{
	var finalMsg = "";
	var sfEls = {
		input: f.getElementsByTagName("input"),
		textarea: f.getElementsByTagName("textarea")
	};
	for(var i in sfEls)
	{
		for(var j = 0; j < sfEls[i].length; j++)
		{
			var msg = "";
			var padded = " " + sfEls[i][j].className + " ";
	
			if(padded.indexOf(" notempty ") != -1)
			{
				if(sfEls[i][j].value == "")
					msg = "The field \"" + sfEls[i][j].attributes["title"].value + "\" must be filled out";
			}
	
			if(msg.length)
			{
				if(finalMsg.length)
					finalMsg += "\n";
				else
					sfEls[i][j].focus();
				finalMsg += msg;
			}
		}
	}

	if(finalMsg.length)
	{
		alert(finalMsg);
		return false;
	}
		
	return true;
}

function validateForm2(f)
{
	var finalMsg = "";
	var sfEls = {
		input: f.getElementsByTagName("input"),
		textarea: f.getElementsByTagName("textarea")
	};
	
	// required fields
	var lastChkbx = '';
	for(var i in sfEls)
	{
		for(var j = 0; j < sfEls[i].length; j++)
		{
			var msg = "";
			var padded = " " + sfEls[i][j].parentNode.className + " ";
			var condStr = " cond_";
			if(padded.indexOf(" required ") != -1) { // required text input
				
				if(padded.indexOf(condStr) != -1) { // conditionally required text
					idx = padded.indexOf(conStr) + conStr.length;
					len = (padded.length-padded.indexOf(conStr));
					cond = padded.substr(idx, len);
					condEl = document.getElementById(cond);
					if(condEl.attributes["type"] != undefined) {
						if(condEl.attributes["type"].value == "checkbox" || condEl.attributes["type"].value == "radio") {
							if(sfEls[i][j].attributes["type"].value == "checkbox" || sfEls[i][j].attributes["type"].value == "radio") {
								if(!condEl.checked && !sfEls[i][j].checked)
									msg = " " + sfEls[i][j].attributes["title"].value + " ";
							} else {
								if(!condEl.checked && sfEls[i][j].value == "")
									msg = " " + sfEls[i][j].attributes["title"].value + " ";
							}
						} else {
							if(sfEls[i][j].attributes["type"].value == "checkbox" || sfEls[i][j].attributes["type"].value == "radio") {
								if(condEl.value == "" && !sfEls[i][j].checked)
									msg = " " + sfEls[i][j].attributes["title"].value + " ";
							} else {
								if(condEl.value == "" && sfEls[i][j].value == "")
									msg = " " + sfEls[i][j].attributes["title"].value + " ";
							}
						}
					}
					
				} else { // standard  required text
					if(sfEls[i][j].value == "")
						msg = " " + sfEls[i][j].attributes["title"].value + " ";
				}
			}
			
			// required checkboxes, radio buttons
			var padded2 = " " + sfEls[i][j].parentNode.parentNode.parentNode.className + " ";
			if(padded2.indexOf(" required ") != -1) {
				if(sfEls[i][j].attributes["type"] != undefined) {
					if(sfEls[i][j].attributes["type"].value == "checkbox" || sfEls[i][j].attributes["type"] == "radio") {
						if(!validateValueSet(sfEls[i][j].attributes["name"])) {
							if(sfEls[i][j].attributes["name"].value != lastChkbx) {
								msg = " " + sfEls[i][j].attributes["title"].value + " ";
							}
							lastChkbx = sfEls[i][j].attributes["name"].value;
						}
					}
				}
			}
	
			if(msg.length)
			{
				if(finalMsg.length)
					finalMsg += "\n";
				else
					sfEls[i][j].focus();
				finalMsg += msg;
			}
		}
	}
	// required dropdowns
	var sEls = f.getElementsByTagName("select");
	for(i=0; i<sEls.length; i++) {
		var msg = "";
		var padded = " " + sEls[i].parentNode.className + " ";
		if(padded.indexOf(" required ") != -1) {

			if(!validateSelect(sEls[i])) {
				try {
					msg = " " + sEls[i].attributes["title"].value + " ";
				} catch(c) {
					alert(sEls[i].attributes['id'].value);
				}
			}
		}
		if(msg.length) {
			if(finalMsg.length)
				finalMsg += "\n";
			else
				sEls[i].focus();
			finalMsg += msg;
		}
	}

	if(finalMsg.length)
	{
		alert("Please complete or correct the following:\n" + finalMsg);
		return false;
	}
		
	return true;
}

function validateValueSet(n) {
	els = document.getElementsByName(n);
	for(i=0; i < els.length; i++) {
		if(e[i].checked)
			return true;
	}
	return false;
}
function validateSelect(s) {
	return (s.selectedIndex > 0);
}

