// validate.js 1.991
// a generic form validator 
// by Brian Lalonde http://webcoder.info/downloads/
// Slightly modified by the one and only Ben Balzarini :-)
// This work is licensed under the Creative Commons Attribution-Share Alike 3.0 License. 
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ 
// or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.

 
 
 
function validate_required(fieldName,fld)
{ 
 
if (fld==null||fld=="")
  {alert('The '+fieldName+' must not be blank.');return false;}
else {return true;}

} 
function requireChecked(fieldName,fld)
{ // require a checkbox to be checked
	if(fld.disabled) return true;
	if(!fld.checked)
	{ alert('The '+fieldName+' checkbox must be checked.'); return false; }
	return true;
}

function requireConfirmation(fieldName,fld,confirmfld)
{ // require fields to match
	if(fld.disabled) return true;
	if(fld.value != confirmfld.value)
	{ alert( 'The '+fieldName+' field does not match the '+fieldname(confirmfld)+' field.'); return false; }
	return true;
}

function requireRadio(fieldName,radios)
{ // require at least one radio in this group to be checked
	if(!radios.length) return true; // invalid parameter
	var visible= false, enabled= false;
	for(var i= 0; i < radios.length; i++)
	{
		if(!enabled) enabled= !radios[i].disabled;
		if(radios[i].checked) return true;
		else if(typeof(radios[i].offsetWidth) == 'undefined' || radios[i].offsetWidth > 0) visible= true;
	}
	if(!visible||!enabled) return true; // no visible/enabled options in this group
	alert('You must select one of the '+radios[0].name+' options.');
	return false;
}

function requireLength(fieldName,fld,min,max)

{
	 
	// set minimum and/or maximum field lengths
	if(!fld.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	var len= fld.length;
	 
	if(min > -1 && len < min)
	{ alert('The '+fieldName+' field must be at least '+min+
		' characters long; it is currently '+len+' characters long.'); return false; }
	if(max > -1 && len > max)
	{ alert('The '+fieldName+' field must be no more than '+max+
		' characters long; it is currently '+len+' characters long.'); return false; }
	return true;
}

 

function allowChars(fieldName,fld,chars)
{  // provide a string of acceptable chars for a field
	if(fld.disabled) return true;
	for(var i= 0; i < fld.length; i++)
	{
		if(chars.indexOf(fld.charAt(i)) == -1)
			if(fld.charAt(i) == " ")
				{ alert('The '+fieldName+' field may not contain spaces.'); return false; }
			else
				{ alert('The '+fieldName+' field may not contain "'+fld.charAt(i)+'" characters.'); return false; }
	}
	return true;
}



function disallowChars(fieldName,fld,chars)
{ // provide a string of unacceptable chars for a field
	if(fld.disabled) return true;
	for(var i= 0; i < fld.length; i++)
	{
		if(chars.indexOf(fld.charAt(i)) != -1)
		{ alert ('The '+fieldName+' field may not contain "'+fld.charAt(i)+'" characters.'); return false; }
	}
	return true;
}

function checkEmail(fieldName,fld)
{ // simple email check
	if(!fld.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	var phony= /@(\w+\.)*example\.(com|net|org)$/i;
	if(phony.test(fld))
	{ alert('Please enter your email address in the '+fieldName+' field.'); return false; }
	var emailfmt= /^\w+([.-]\w+)*@\w+([.-]\w+)*\.\w{2,8}$/;
	if(!emailfmt.test(fld))
	{ alert  ('The '+fieldName+' field must contain a valid email address.'); return false; }
	return true;
}

function checkIntRange(fieldName,fld,minVal,maxVal,sep)
{
	if(!fixInt(fld)) return false;
	var val= parseInt(fld);
	if(val < minVal) { alert('The '+fieldName+' field must be no less than '+minVal+'.'); return false; }
	if(val > maxVal) { alert('The '+fieldName+' field must be no greater than than '+maxVal+'.'); return false; }
	return true;
}

function checkFloatRange(fieldName,fld,minVal,maxVal,sep)
{
	if(!fixFloat(fld)) return false;
	var val= parseFloat(fld);
	if(val < minVal) { alert('The '+fieldName+' field must be no less than '+minVal+'.'); return false; }
	if(val > maxVal) { alert('The '+fieldName+' field must be no greater than than '+maxVal+'.'); return false; }
	return true;
}

function fixInt(fieldName,fld,sep)
{ // integer check/complainer 
	if(!fld.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	var val= fld;
	if(typeof(sep)!='undefined') val= val.replace(new RegExp(sep,'g'),'');
	val= parseInt(val);
	if(isNaN(val))
	{ // parse error 
		alert('The '+fieldName+' field must contain a whole number.');
		return false;
	}
	fld= val;
	return true;
}

function fixFloat(fieldName,fld,sep)
{ // decimal number check/complainer 
	if(!fld.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	var val= fld;
	if(typeof(sep)!='undefined') val= val.replace(new RegExp(sep,'g'),'');
	val= parseFloat(fld);
	if(isNaN(val))
	{ // parse error 
		alert('The '+fieldName+' field must contain a number.');
		return false;
	}
	fld.value= val;
	return true;
}

 

 
function fixDate(fieldName,fld)
{ // tenacious date correction 
	if(!fld.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	var val= fld;
	var dt= new Date(val.replace(/\D/g,'/'));
	if(!dt.valueOf())
	{ // the date was unparseable 
		alert('The '+fieldName+' field has the wrong date.');
		return false;
	}
	fld.value= (dt.getMonth()+1)+'/'+dt.getDate()+'/'+dt.getFullYear();
	return true;
}

function fixRecentDate(fieldName,fld,minyear)
{ // tenacious date correction 
	if(!fld.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	var val= fld;
	var dt= new Date(val.replace(/\D/g,'/'));
	if(!dt.valueOf())
	{ // the date was unparseable 
		alert('The '+fieldName+' field has the wrong date.');
		return false;
	}
	while(dt.getFullYear() < minyear) { dt.setFullYear(dt.getFullYear()+100); }
	fld.value= (dt.getMonth()+1)+'/'+dt.getDate()+'/'+dt.getFullYear();
	return true;
}

 
 
function fixPhone(fieldName,fld,defaultAreaCode,sep,noext)
{ alert("phone"+fld);// tenacious phone # correction 
	if(!fld.length||fld.disabled) return true; // blank fields are the domain of requireValue 
	if(typeof(sep)=='undefined') sep= '-';
	if(typeof(defaultAreaCode)!='undefined') defaultAreaCode= defaultAreaCode + sep;
	var ext= '', val= fld.toLowerCase();
	if(val.indexOf('x') > 0)
	{
		if(!noext) ext= ' x'+val.substr(val.indexOf('x')).replace(/\D/g,'');
		val= val.substr(0,val.indexOf('x'));
	}
	val= val.replace(/\D/g,'');
	if(val.length == 7)
	{
		fld= defaultAreaCode + val.substring(0,3) + sep + val.substring(3,20) + ext;
		return true;
	}
	if(val.length == 10)
	{
		fld= val.substring(0,3) + sep + val.substring(3,6) + sep + val.substring(6,20) + ext;
		return true;
	}
	if(val.length < 7)
	{
		alert('The phone number you supplied for the '+fieldName+' field was too short.');
		return false;
	}
	if(val.length > 10)
	{
		alert('The phone number you supplied for the '+fieldName+' field was too long.');
		return false;
	}
	alert('The phone number you supplied for the '+fieldName+' field was wrong.');
	return false;
}


function disallowSpecialChars(fieldName,fld){
 var iChars = "~!@#$%^&*()+=-[]\\\';,./{}|\":<>?"; 
        for (var i = 0; i < fld.length; i++) { 
                if (iChars.indexOf(fld.charAt(i)) != -1) { 
                alert ('The '+fieldName+' field has special characters. \nThese are not allowed.\n'); 
                return false; 
   
		}
		}
		}
