//created by RunTime Technologies
//--- added match variable to match 2 (or more) form elements
//modified 9/24/1999
//--- added requirement checking for radio,selects,checkboxes
//--- added legal check type to block illegal chars in a text string
//modified 1-31-2002 -- localize all messages, Bob Matsuoka;
//modified 8-23-2002 -- add isOneChecked logic and function, Jon Gill;
//mod 9-20-2002 -- option to submit even on already submitted, Bob M;
//mod 10-11-2002 -- checkAll now looks for either the presense of a message or a "check" type -- specifically useful for makeLegal, which returns its own separate message;
//mod 11-1-2002 -- isEmail now checks for the presence of a '.', but does not require that it's after the 3rd character, i.e., jw.family@buddy.sohonet.com was failing check
//mod 11-6-2002 -- fixed syntax errors in isLegal, Jon Gill;

var MSG_NOTSUBMITTED = 'The form was not submitted because:';
var MSG_ALREADYSUBMITTED = 'This form has already been submitted.  Are you sure you wish to submit it again (if no, please click \'cancel\' and wait.  If so, click \'OK\'.)?';
var MSG_MAKELEGAL = 'One or more illegal characters have been detected and replaced.';
var MSG_SQLDATEMIN = 'You cannot enter dates before Jan 1, 1753.';
var ILLEGALCHARS = ' \\/@#$%<>:?|^&*()\"\'!';
var DIGITS = '0123456789';
var SPECIAL = '\\/@#$%<>:?|^&*(){}[]\"\'!_-+=.,';
var ALPHA = 'abcdefghijklmnopqrstuvwxyz';
var URLTYPES = new Array('mailto','http','https','ftp','gopher','wais','jdbc','file','news','ldap');

function isSSN(ssn)
{
	if (ssn.length==0)
		return true;
	ssn=ssn.replace(/-/g,"");
	ssn=ssn.replace(/\s/g,"");
	ssn=ssn.replace(/\//g,"");
	if(ssn.length != 9 || !isInt(ssn))
		return false;
	return true;
}
function isLen(value,len)
{
	return (value.length <= len);
}
function isMinLen(value,minlen)
{
	return (value.length >= minlen);
}
function isEmail(e)
{
	var passed = true;
	var arr = e.split(',');
	for (var i = 0; i < arr.length; i++)
	{
		if (!isValidEmailAddress(arr[i])) passed = false;
	}
	return passed;
}
//lightweight; just tests basic structure
function isValidEmailAddress(e)
{
	if (e.length == 0) return true;
	if(e.indexOf('@') < 1) return false;
	if(e.indexOf('.') == -1) return false;
	return true;
}
function isURL(value)
{
	if (value == null || value.length == 0) return false;
	var parts = value.split(":");
	if (parts.length < 2) return false;
	for (var i = 0; i < URLTYPES.length; i++)
	{
		if (parts[0] == URLTYPES[i]) return true;
	}
	return false;
}
function isTime(t)
{
	var c = t.indexOf(":");
	if(t.indexOf(" ")!=-1||c==-1||t.indexOf("-")!=-1)return false;
	hrs=t.substring(0,c);
	mins=t.slice(c+1);
	if(!isInt(hrs)||!isInRange(hrs,0,23)||hrs.lenght>2||mins.length>2||!isInt(mins)||!isInRange(mins,0,59))
		return false;
	return true;
}
function isInt(n)
{
	if(n.indexOf(' ')!=-1)
		return false;
	if(isNaN(n))
		return false;
	if(n.indexOf('.')!=-1)
		return false;
	return true;
}
function isIntList(il)
{
	srcval = il;
	allowedchars = DIGITS + ',';
	for (var q = 0; q < il.length; q++)
	{
		currchar = srcval.charAt(q);
		if (allowedchars.indexOf(currchar) < 0)
			return false;
	}
	return true;
}
function isFloat(n)
{
	if(n.indexOf(' ')!=-1) return false;
	if(isNaN(n)) return false;
	return true;
}
function isMoney(m)
{
	if(m.length==0)
		return true;
	if(m.indexOf(' ')!=-1)
		return false;
	if(m.indexOf('$')!=-1&&m.indexOf('$')!=0)
		return false;
	m=m.replace(/,/g,'');
	m=m.replace(/\$/,'');
	if(isNaN(m))
		return false;
	return true;
}
function isDate(d)
{
	if (d.length==0)
		return true;
	var m=d.indexOf('-');
	if(m==-1)
		return false;
	var y=d.indexOf('-',(m+1));
	if(y==-1)
		return false;
	var month=d.substring(0,(m));
	var day=d.substring((m+1),(y));
	var year=d.substr(y+1);
	var d30='4,6,9,11';
	d30=d30.split(",");
	var d31='1,3,5,7,8,10,12';
	d31=d31.split(",");
	if(!isInt(day))
		return false;
	if(!isInt(month))
		return false;
	if(!isInt(year))
		return false;
	if(day<1)
		return false;
	if(month<1||month>12)
		return false;
	//check months with 30 days
	for(var q=0;q<d30.length;q++)
	{
		if(d30[q]==month)
		{
			if(day>30)
				return false;
		}
	}
	//check months with 31 days
	for(q=0;q<d31.length;q++)
	{
		if(d31[q]==month)
		{
			if(day>31)
				return false;
		}
	}
	//check January
	if(month==2&&year%4!=0&&(day>28))
		return false;
	if(month==2&&year%4==0&&(day>29))
		return false;
	if(year.length!=4)
		return false;
	if(year<1753)
	{
		//sql server requirement
		// alert(MSG_SQLDATEMIN);
		return false;
	}
	return true;
}
function isEmpty(a)
{
	a=a.replace(/ /g,'');
	if(a.length==0)
		return true;
	return false;
}
function isChecked(f,v)
{
	for(var x=0;x<f.elements.length;x++)
	{
		if(f.elements[x].name==v.name&&f.elements[x].checked)
			return true;
	}
	return false;

}
function isOneChecked(f,v)
{
	for(var x=0; x < f.elements[v.name].length; x++ )
	{
		if(f.elements[v.name][x].checked)
			return true;
	}
	return false;

}
function isSelected(v,index)
{
	//ind is the min index that must be selected
	//default is -1 for multi select and 0 for single selects
	if(v.selectedIndex >= index) return true;
	return false;
}
		
function makeLegal(source)
{
	//if user defines the chars use them, otherwise use the standard set
	illegalChars=(source.illChars)?source.illChars:ILLEGALCHARS;
	var illegal_char_exists = false;
	var legal_string = '';
	for (var c = 0; c < source.value.length; c++)
	{
		curr_char = source.value.substr(c, 1);
		if (illegalChars.indexOf(curr_char) > -1)
		{
			legal_string += '_';
			illegal_char_exists = true;
		}
		else
		{
			legal_string += curr_char;
		}
	}
	if (illegal_char_exists)
	{
		source.value = legal_string;
		alert(MSG_MAKELEGAL + '(' + illegalChars + ')' );
	}
}
function isLegal(v)
{
	if (v.length == 0) return true;
	//alert(v.name);
	//if user defines the chars use them, otherwise use the standard set
	var illegalChars = (v.illChars)?v.illChars:ILLEGALCHARS;
	// alert(illegalChars);
	//loop through string to check against set of illegal chars, one char at a time
	for (var i = 0; i < v.value.length; i++)
	{
		var curr_char = v.value.substr(i, 1);
		if (illegalChars.indexOf(curr_char) == -1) return false;
	}
	return true;
}
function isIllegal(v)
{
	if (v.length == 0) return false;
	//alert(v.name);
	//if user defines the chars use them, otherwise use the standard set
	var illegalChars = (v.illChars)?v.illChars:ILLEGALCHARS;
	if (illegalChars.length == 0) return false;
	// alert(illegalChars);
	//loop through string to check against set of illegal chars, one char at a time
	for (var i = 0; i < v.value.length; i++)
	{
		var curr_char = v.value.substr(i, 1);
		if (illegalChars.indexOf(curr_char) > -1) return true;
	}
	return false;
}
function isOneOf(v, fieldlist)
{
	var result = false;
	if (v.value.length > 0) return true;
	var fields = fieldlist.split(',');
	for (var j = 0; j < fields.length; j++)
	{
		var current = eval('v.form.'+fields[j]+'.value');
		if (current.length > 0) return true;
	}
	return result;
}
function isInList(v, list)
{
	var result = false;
	var arr = list.split(',');
	for (var j = 0; j < list.length; j++)
	{
		if (v == arr[j]) result = true;
	}
	return result;
}
function stripNonNumeric(value)
{
	var result = '';
	for (var i = 0; i < value.length; i++)
	{
		var current = value.charAt(i);
		if (DIGITS.indexOf(current) > -1) result += current;
	}
	return result;
}
function isCcno(v)
{
	var cc = stripNonNumeric(v);
	var len = cc.length;
	if (len < 13 || len > 19) return false;
	var sum = 0;
	var mul = 1;
	for (var i = len - 1; i >= 0; i--)
	{
	    var digit = eval(cc.charAt(i));
	    digit *= (mul == 1) ? mul++ : mul--;
	    sum += (digit >= 10) ? (digit % 10) + 1 : digit;
	}
	return (sum % 10) == 0;
}
function isInRange(v, low, high)
{
	if (v == null || v.length == 0 || isNaN(v)) return false;
	var result = true;
	if (low != null && v < low) result = false; 
	//alert(high);
	//alert(v);
	//alert(high != null && v > high);
	if (high != null && v > high) 
		result = false; 
	return result;
}
function isAlpha(text)
{
	for (var i = 0; i < text.length; i++)
		if (ALPHA.indexOf(text.charAt(i)) == -1)
			return false;
	return true;
}
function hasAlpha(text)
{
	for (var i = 0; i < text.length; i++)
		if (ALPHA.indexOf(text.charAt(i)) > -1)
			return true;
	return false;
}
function isNumeric(text)
{
	var allowedchars = DIGITS + '.';
	for (var i = 0; i < text.length; i++)
		if (allowedchars.indexOf(text.charAt(i)) == -1)
			return false;
	return true;
}
function hasNumeric(text)
{
	var allowedchars = DIGITS + '.';
	for (var i = 0; i < text.length; i++)
		if (allowedchars.indexOf(text.charAt(i)) > -1)
			return true;
	return false;
}
function isSpecial(text)
{
	for (var i = 0; i < text.length; i++)
		if (SPECIAL.indexOf(text.charAt(i)) == -1)
			return false;
	return true;
}
function hasSpecial(text)
{
	for (var i = 0; i < text.length; i++)
		if (SPECIAL.indexOf(text.charAt(i)) > -1)
			return true;
	return false;
}
function appendMsg(msg)
{
	return '--- '+msg+'\n';
}
function renderMsgs(msgs)
{	
	var result = '';
	for (var i = 0; i < msgs.length; i++)
		result += '--- '+msgs[i]+'\n';

	return result;
}
function renderMsgsHtml(msgs)
{	
	var result = '<ul>';
	for (var i = 0; i < msgs.length; i++)
		result += '<li>'+msgs[i]+'</li>';

	result += '</ul>';
	return result;
}
//******************* FUNCTION TO LOOP THROUGH FORM *********************
function checkAll(f)
{//begin function
	var counter = 0;
	var msgs = new Array();
	msg='';
	for(i = 0; i < f.elements.length; i++)
	{//begin loop through form elements
		//addmsg control makes sure validation msg is only shown once per field, in the case of 2 validation types
		var addmsg = false;
		//exempt control turns on if exception values are provided and the field's current value equals one of the exception values
		var exempt = false;
		v = f.elements[i];
		// max length
		if (v.len && !isLen(v.value,v.len))
			addmsg = true;
		// minimum length
		if (v.minlen && !isMinLen(v.value,v.minlen))
			addmsg = true;
			
		if(typeof(v.exceptionlist) != 'undefined') //don't validate excepted strings
		{
			exceptvals = v.exceptionlist.split(',');
			for(j = 0; j < exceptvals.length; j++)
			{
				if(v.value == exceptvals[j])
				{
					exempt = true;
					break;
				}
			}
		}
		if (!exempt && (v.msg || v.check))
		{
			//if validating radio or checkbox
			if(v.type=='radio'||v.type=='checkbox')
			{
				if(v.req&&!isChecked(f,v))
					addmsg = true;
			}
			else if(v[0] && typeof v[0] != 'undefined' && v[0].type=='checkbox')
			{
				if(v.req&&!isOneChecked(f,v))
					addmsg = true;
			}
			//if validating a single select
			else if(v.type=='select-one')
			{
				var currindex = 1;
				if(v.reqindex != null) currindex = v.reqindex;
				if(v.req&&!isSelected(v, currindex))
					addmsg = true;
			}
			//if validating a multi select
			else if(v.type=='select-multiple')
			{
				var currindex = 0;
				if(v.reqindex != null) currindex = v.reqindex;
				if(v.req&&!isSelected(v,currindex))
					addmsg = true;
			}
			//if validating any other type of form element (text,ta,password,file)
			else
			{
				if(v.match && v.value!=v.match.value) //validates matching values
					addmsg = true;
				else if(v.oneof && !isOneOf(v, v.oneof)) //validates one of required fields
					addmsg = true;
				else
				{
					if(v.check=='legal'&&!isLegal(v))//validate that uses legal characters
						addmsg = true;
					if(v.check=='illegal'&&isIllegal(v))//validate that only uses legal characters
						addmsg = true;
	 				if(v.req==1&& isEmpty(v.value)) //check required
						addmsg = true;
					if(v.check=='list' && v.list && !isInList(v.value, v.list)) //check value in list
						addmsg = true;
					if(v.check=='ssn' && !isSSN(v.value))//check ssn
						addmsg = true;
					if(v.check=='ccno' && !isCcno(v.value))//check credit card number (mod 10)
						addmsg = true;
					if(v.check=='email' && !isEmail(v.value))//check email
						addmsg = true;
					if(v.check=='url' && !isURL(v.value))//check url
						addmsg = true;
					if(v.check=='date' && !isDate(v.value))//check dates
						addmsg = true;
					if(v.check=="time" && (!isTime(v.value)&&v.value.length>0)) //check times
						addmsg = true;
					if(v.check=='money' && !isMoney(v.value))//check money
						addmsg = true;
					if(v.check=='intlist' && !isIntList(v.value))//check integer list
						addmsg = true;
					if(v.check=='range' && !isInRange(v.value, v.rangelow, v.rangehigh))//check integer list
						addmsg = true;
					if(v.check=='int' && (v.value.length!=0 && (!isInt(v.value) || !isInRange(v.value,v.mn,v.mx))))//check ints
						addmsg = true;
					if(v.check=='float' && (v.value.length!=0 && (!isFloat(v.value) || !isInRange(v.value,v.mn,v.mx))))//check floats
						addmsg = true;
					if(v.check=='alphanumeric' && (!(hasAlpha(v.value) && hasNumeric(v.value))))
						addmsg = true;
					if(v.check=='alphanumericspecial' && (!(hasAlpha(v.value) && hasNumeric(v.value) && hasSpecial(v.value))))
						addmsg = true;
					if(v.check=='alphaspecial' && (!(hasAlpha(v.value) && hasSpecial(v.value))))
						addmsg = true;
				}
			}
		}
		if(addmsg) msgs[msgs.length] = v.msg;
	}//end loop through form elements
	if(msgs.length>0)
	{
		var hasUnicode = false;
		if (MSG_NOTSUBMITTED.indexOf('&#') > -1)
			hasUnicode = true;
		if (!hasUnicode)
		{
			for (var i = 0; i < msgs.length; i++)
			{
				if (msgs[i].indexOf('&#') > -1)
				{
					hasUnicode = true;
					break;
				}
			}
		}
		if (hasUnicode)
			vAlert(MSG_NOTSUBMITTED+renderMsgsHtml(msgs));
		else
			alert(MSG_NOTSUBMITTED+'\n'+renderMsgs(msgs));
		if (f.submitted) f.submitted.value = 0;
		return false;
	}
	if(f.submitted)
	{// does "submitted" field exist?  if so, make sure form is only submitted once
		if (f.submitted.value == 1)
		{
			if (confirm(MSG_ALREADYSUBMITTED))
				return true;
			return false;
		}
		f.submitted.value = 1;
		return true;
	}
	else return true;
}
function vAlert(text)
{
	var docstruct = '<html><body onblur="window.focus()"><div class="validatemsg" style="padding: 20px 20px 20px 20px;">'+text+'</div><div style="text-align: center;"><form><input type="button" value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" onclick="window.close()"></form></div></body></html>'
	var vAlertWin = window.open('','vAlertWindow',getAllPopupWinSizeParams(350,450));
	vAlertWin.document.clear();
	vAlertWin.focus();
	vAlertWin.document.write(docstruct);
	vAlertWin.document.close();	
}
//end checkAll
//&copy; SOHOnet 1999;
//special thanks to Andrey Akselrod for all of his help with this project;
//without him it would not have been possible!;


