function TrimValue(x)
{
	while(''+x.charAt(0)==' ')
	{x=x.substring(1,x.length);}
	return x
}
function _is_required(obj){
	/*
	Parameters:
		String
	Returns:
		boolean
	Purpose:
	 Ensures that incoming value is not blank
	*/
	if(obj.length == 0){
		return false;
	}
	return true;
}

function _is_numeric(obj){
	/*
	Parameters:
		String
	Returns:
		boolean
	Purpose:
		Ensures that incoming value is numeric
	*/
	if(isNaN(obj) || obj.length == 0){
		return false;
	}
	return true;
}

function _is_range(low, high, obj){
	/*
	Parameters:
		String
	Returns:
		boolean
	Purpose:
	 Ensures that incoming value is with in specific range
	*/
	if(obj < low || obj > high){
		return false;
	}
	return true;
}

function _is_email(S){
	/*
	Parameters:
		String
	Returns:
		boolean
	Purpose:
	 	Ensures that incoming value is consistant with "user@domain.com"
	*/
	//return /[A-Za-z0-9][A-Za-z0-9\-\.\_]*\@[A-Za-z0-9][A-Za-z0-9\-\.]*\.[A-Za-z]{2,3}$/.test(obj)
	var R=false;
	if (typeof(S) != "undefined")
	{if (/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(S))
		R=S;
	}
	return R;
}

function _is_phone(obj){
	/*
	Parameters:
		String
	Returns:
		boolean
	Purpose:
		Ensures that incoming value is consistant with "555-555-1234"
	*/
	return /[0-9]{3}\-[0-9]{3}-[0-9]{4}$/.test(obj)
}

function _is_multi_phone(obj1, obj2, obj3){
	/*
	Parameters:
		String, String, String
	Returns:
		boolean
	Purpose:
		takes the values of 3 text boxes and ensures that 
		they are consistant with a phone number.
		obj1 = area code
		obj2 = prefix
		obj3 = last 4 digits
	*/
	if(!_is_numeric(obj1)){return false;}
	if(!_is_numeric(obj2)){return false;}
	if(!_is_numeric(obj3)){return false;}
	if(!_is_range(111,999,obj1)){return false;}
	if(!_is_range(111,999,obj2)){return false;}
	if(!_is_range(0,9999,obj3)){return false;}
	return _is_phone(obj1 + "-" + obj2 + "-" + obj3);
}

function _is_ssn(obj){
	return /[0-9]{3}\-[0-9]{2}-[0-9]{4}$/.test(obj)
}

function _alpha_only(obj){
	/*
	Parameters:
		String
	Returns:
		boolean
	Purpose:
	 	Ensures that incoming value is alpha characters only
	*/
	return /[A-Za-z][A-Za-z]*$/.test(obj)
}

function _is_variable(obj){
	/*
	Parameters:
		String
	Returns:
		boolean
	Purpose:
	 	Ensures that incoming value is alpha characters only
	*/
	return /[A-Za-z][A-Za-z0-9_]*$/.test(obj)
}

function _is_day(in_year, in_month, in_day){
	/* Ripped From Macromedia's Cold Fusion (CFFORM) */
	/*
	Parameters:
		int, int, int
	Returns:
		boolean
	Purpose:
	 	validates that number of days in month match the maximum 
		amount of days in that month
	*/
	maxDay = 31;

	if (in_month == 4 || in_month == 6 || in_month == 9 || in_month == 11){
		maxDay = 30;
	}else{
		if (checkMonth == 2){
			if(in_year % 4 > 0){
				maxDay =28;
			}else{
				if(in_year % 100 == 0 && in_year % 400 > 0){
					maxDay = 28;
				}else{
					maxDay = 29;
				}
			}
		}
	}
	return _is_range(1, maxDay, in_day);
}

function _is_date(obj){
	/* Ripped From Macromedia's Cold Fusion (CFFORM) */
	/*
	Parameters:
		string
	Returns:
		boolean
	Purpose:
	 	ensures value is a valid date:
			mm/dd/yyyy
			 mm/d/yyyy
			 m/dd/yyyy
			  m/d/yyyy
			  mm/dd/yy
			   mm/d/yy
			   m/dd/yy
			    m/d/yy
		  				
	*/
	dSplit = obj.indexOf('/');
	if(dSplit == -1 || dSplit == obj.length){
		return false
	}

	dMonth = obj.substring(0, dSplit);
	if(dMonth == 0){
		return false;
	}

	dSplit = obj.indexOf('/', (dSplit + 1));
	if(dSplit == -1 || (dSplit  + 1) == obj.length){
		return false;
	}
	
	dDay = obj.substring((dMonth.length + 1), dSplit);
	if(dDay.length == 0){
		return false;
	}
	
	dYear = obj.substring(dSplit + 1);
	
	if(!_is_numeric(dMonth)){
		return false;
	}else{
		if(!_is_range(1, 12, dMonth)){
			return false;
		}else{
			if(!_is_numeric(dYear)){
				return false;
			}else{
				if(!_is_range(0, 9999, dYear)){
					return false;
				}else{
					if(!_is_numeric(dDay)){
						return false;
					}else{
						if(!_is_day(dYear, dMonth, dDay)){
							return false;
						}else{
							return true;
						}
					}
				}
			}
		}
	}
}

function checkAll(fld,checkfld)
{
	if (typeof(checkfld) != "undefined")
	{
		for (i = 0; i < checkfld.length; i++) 
		{checkfld[i].checked = fld.checked;}	
	}
}
function checkAllChecked(obj,fld,checkfld)
{
	var count = 0
	for (i = 0; i < checkfld.length; i++) 
	{if (checkfld[i].checked == true){count = count+1;}}
	if (checkfld.length == count){fld.checked = true}
	else {fld.checked = false}
}

function validate_form(obj,h,w)
{	
	if ('undefined' == typeof(h))
		h = 217
	if ('undefined' == typeof(w))
		w = 399

	var errorMsg = '';
	var checkFlag = false;
	for (var i=0;i< obj.elements.length;i++)
	{
    	var e = obj.elements[i];
		var name = e.name;
		if ('undefined' != typeof(name))
		{
			var type = e.type;
			var val = e.value;
			var nametext = name.toLowerCase();
			if (e.required)
			{
				if(TrimValue(val)=='')
				{errorMsg = errorMsg +'|'+e.validationmsg;
				//alert(name +'  '+ val+'   '+type +'  '+e.required + ' '+e.validationmsg);
				}
				else if (nametext.indexOf('email') > 0)
				{
					if(!_is_email(val))
					errorMsg = errorMsg +'|valid email address';
				}
				
			}
		}
	}
	if (errorMsg != "") 
	{
		errorMsg ="_____________________________|" + "You failed to correctly fill in your:|_____________________________||" + 
		errorMsg + "||_____________________________" + "| Please re-enter and submit again!";
		//OpenErrorWin("ErrorWin","200","200","","","no","no","no","no","no",'/ErrorWin.cfm?msg='+errorMsg+'&r='+Math.round(Math.random()*1000000),"modal");
		Err('/ErrorWin.cfm?msg='+errorMsg+'&r='+Math.round(Math.random()*1000000),true,errorMsg,'Error',h,w);
		return false;
	}
	else return true;
}