var csvalcustom=false
var csyear; var csmonth

function awCMcstest(){
	// use to test the web sever is serving this file
	alert("Successful call to cdgval.js");
	}
	
function cstrim(s){
	// equivalent to vb trim
	var whitespace = " \t\n\r";
	if ((s == null) || (s.length == 0)){return "";}
	while (s.length>0){
		var c = s.charAt(0);
		if (whitespace.indexOf(c) == -1){break;}
		else {s = s.substring(1)}	
	}
	while (s.length>0){
		var c = s.charAt(s.length-1);
		if (whitespace.indexOf(c) == -1){break;}
		else {s = s.substring(0, s.length-1)}
	}
return s;
}

function awCMcsval(form){
	// iterates through the form elements checking for v_fields,
	// looking up their matching fields and calling csfield for each one
	var s=""; var fname; var valtype; var ss
	var sline = "__________________________________________\n\n"
	var x = form.length;
	var foundfield = false;
	x = x -1
	for (var i=0;i<=x;i++) {
		fname = form.elements[i].name
		if (fname.substring(0,2)=="v_")
		{
			valtype = form.elements[i].value
			valtype = valtype.toUpperCase()
			fname = fname.substring(2)
			for (var j=0;j<=x;j++){
				if (form.elements[j].name==fname)
				{
					// do something different for a check box
					if (form.elements[j].type == "checkbox")
					{
						s = s + csfield(form.elements[j].checked, valtype, fname);
					}
					else
					{
						s = s + csfield(form.elements[j].value, valtype, fname);
					}
				}
			}
		}
	}
	if (s!="") {
		ss = sline + "This form has error(s) and cannot be submitted.\n"
		ss = ss + "Please correct the following error(s) and re-submit:\n"
		s = ss + sline + s
		alert(s);
		return false;}
	else {
	return true;}
}

function csfield(v, valtype, fname){
	// validation of a single field, against a validation string
	// which can have multiple elements eg "MONEYPOSITIVE"
	var s=""; var isminus; var cursign = "£$p"; var digits= ".01234567890"
	v = cstrim(v);
	
	if (v == "")
	{
		if ((valtype.search("REQUIRED")!=-1) && (v == ""))
		{
			s = s + fname + " may not be left blank\n"
		}
					
		if ((valtype.search("TERMS")!=-1)) // && ( v == "" ))
		{
			s = s + "we are unable to proceed unless you accept our terms and conditions.\nPlease read our terms and check the " + fname + " box\n"; 
		}
	
	}
	else
	{
		if ((valtype.search("NUMBER")!=-1)||(valtype.search("MONEY")!=-1))
		{
			if (v.charAt(0)=="+")
			{
				v = v.substring(1);
			}
			if (valtype.search("MONEY")!=-1)
			{
				if (v.charAt(0)=="-")
				{
					isminus=true;
					v=v.substring(1);
				}
				if (cursign.indexOf(v.charAt(0))!=-1)
				{
					v = v.substring(1);
				}	
				if (v.charAt(0)=="+")
				{
					v = v.substring(1);
				}
				if (cursign.indexOf(v.charAt(v.length -1))!=-1)
				{
					v = v.substring(0, v.length-1);
				}
			}
			if (v=="")
			{
				v = 0;
			}
			if (v.charAt(0)=="-")
			{
				isminus=true;
				v=v.substring(1);
			}
			for (var i=0;i<v.length;i++)
			{
				if (digits.indexOf(v.charAt(i)) == -1)
				{
					s= s + fname + " may only contain digits (" + v.charAt(i) + " is not)\n";
					break;
				}
			}
			if (v.indexOf(".", Math.max(0, v.indexOf(".")+1))!=-1)
			{
				s =s + fname + " may only have one decimal place (" + v + " does not)\n";
			}	
			if ((isminus==true) && (v!=0))
			{
				v = "-" + v;
			}
			if (s == "")
			{
				if ((valtype.search("INTEGER")!=-1) && (Math.round(v)!= v))
				{
					s= s + fname + " must be a whole number (" + v + " is not)\n";
				}
				if ((valtype.search("NONZERO")!=-1) && (v == 0))
				{
					s= s + fname + " cannot be zero\n";
				}
				if ((valtype.search("POSITIVE")!=-1) && (v < 0))
				{
					s= s + fname + " must be positive (" + v + " is not)\n";
				}
				if ((valtype.search("NEGATIVE")!=-1) && (v > 0))
				{
					s = s + fname + " must be negative (" + v + " is not)\n";
				}
				if ((valtype.search("YYYY"))!=-1)
				{
					csyear=v;
				}
				if ((valtype.search("MOY"))!=-1)
				{
					csmonth=v;
				}
				if (((valtype.search("DOM"))!=-1) && (csmonth!=""))
				{
					switch(csmonth)
					{
						case "04":
						case "06":
						case "09":
						case "11":
							if (v=="31")
							{
								s= s + fname + " - no 31st day of month " + csmonth + "\n";
							}
							break;
						case "02":
							if (v=="31")
							{
								s= s + fname + " - no 31st day of February\n";
							}
							if (v=="30")
							{
								s= s + fname + " - no 30th day of February\n";
							}
							if ((v=="29") && (csyear!=""))
							{
								if (csyear%4!=0)
								{
									s= s + fname + " - no 29th day of February in " + csyear +"\n";
								}
							}
							break;
					} // switch (csmonth)
				} //DOM 
			}// (s == "")
		}//NUMBER or MONEY		
		else
		{
			if (valtype.search("EMAIL")!=-1)
			{
				//var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
				//var regex = new RegExp(emailReg);
				//if (regex.test(v)==false){
				 var addressPattern =  /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
				if (addressPattern.test(v)==false)
				{
					s = s + fname + " must be a valid e-mail address (" + v + " is not)\n";
				}
			}	
		}
		//DATE ?use a drop down box?
		//URL
		//POSTCODE
		//ZIP
		//IMAGE - check file extensions
		if (csvalcustom)
		{
			s = s + awCMcscustom(v, valtype, fname);
		}			
	} // end of else of (v == "")
	
return s;
}