var ValidateElement = new Array;
var ValidateFlags = new Array;
var ValidatorCount = 0;
//=================================================================//
function getObj(name)
{
  if (document.getElementById) //IE
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all) //NETSCAPE 6
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers) //NETSCAPE 4 & MOZILLA
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}
//=================================================================//
function chk2txt(chk,txt) {
 txt.value='';
 for (var i=0;i<chk.length;i++) if (chk[i].checked) txt.value+=chk[i].value + ",";
}

//=================================================================//
function IsText(string) {
    if (!string) return false;
    var Chars = "%$£&!'";
    for (var i = 0; i < string.length; i++) {
       if (Chars.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
} 
//=================================================================//
function IsNumeric(string) {
    if (!string) return false;
    var Chars = "-0123456789.";
    for (var i = 0; i < string.length; i++) {
       if (Chars.indexOf(string.charAt(i)) == -1)
          return true;
    }
    return false;
} 
//=================================================================//
function isEmpty(s) {
    val = new String(s.value);
    return ((val == null) || (val.length == 0)) }
//=================================================================//

function validate(formNo) {
	if (formNo == null) formNo = 0;
	PassValidate = true;
	PassValidateMessage = '';
	for (var i = 0; i < ValidateElement.length; i++) {
		var ErrorSummary = new getObj("errorSummary");
		ErrorSummary.style.visibility = 'hidden';
		var ErrorMarker = new getObj(["ErrorMarker_" + ValidateElement[i][0]]);
		ErrorSummary.obj.innerHTML = '';
		ErrorMarker.style.visibility = 'hidden';
		// Check for Null
		if (ValidateFlags[i][0] != "false") {
			if (isEmpty(document.forms[formNo][ValidateElement[i][0]])) { 
				PassValidate = false;
				PassValidateMessage = PassValidateMessage + "<LI>" + ValidateElement[i][1] + " is a required value.</LI>"
				ErrorMarker.style.visibility = 'visible';
				document.forms[formNo][ValidateElement[i][0]].focus();
				//break
			}
		}
		// Check required text
		if (ValidateFlags[i][1] != "false") {
			if (IsText(document.forms[formNo][ValidateElement[i][0]].value)) {
				PassValidate = false;
				PassValidateMessage = PassValidateMessage + "<LI>" + ValidateElement[i][1] + " must not contain the characters %$£&!'.</LI>"
				ErrorMarker.style.visibility = 'visible';
				document.forms[formNo][ValidateElement[i][0]].focus();
				//break;
			}
		}
		// Check valid email
		if (ValidateFlags[i][2] != "false") {
			if (document.forms[formNo][ValidateElement[i][0]].value.indexOf("@")==-1) {
				PassValidate = false;
				PassValidateMessage = PassValidateMessage + "<LI>" + ValidateElement[i][1] + " is not a valid email address.</LI>"
				ErrorMarker.style.visibility = 'visible';
				document.forms[formNo][ValidateElement[i][0]].focus();
				//break;
			}
		}
		// Check valid integer
		if (ValidateFlags[i][3] != "false") {
			if (IsNumeric(document.forms[formNo][ValidateElement[i][0]].value)) {
				PassValidate = false;
				PassValidateMessage = PassValidateMessage + "<LI>" + ValidateElement[i][1] + " must be a numeric value.</LI>"
				ErrorMarker.style.visibility = 'visible';
				document.forms[formNo][ValidateElement[i][0]].focus();
				//break;
			}
		}
	}
	if (PassValidate == false) {
		ErrorSummary.style.visibility = 'visible'
		ErrorSummary.obj.innerHTML = "Please address the following errors:<UL>"+PassValidateMessage+"</UL>";
		//alert(PassValidateMessage)
	} else {
	document.forms[formNo].submit();
	}
}
//=================================================================//
function addNewValidator(fieldName,fieldTitle,bRequired,bText,bEmail,bInt) {
	// add to array
	ValidateElement[ValidatorCount] = new Array(fieldName,fieldTitle);
	ValidateFlags[ValidatorCount] = new Array(bRequired,bText,bEmail,bInt);
	//write indicator class
	document.write("<span id='ErrorMarker_"+fieldName+"' class='validatoritem'>&nbsp;*</span>")
	//incriment validator count
	ValidatorCount++;
}