function CheckForm(form)
{
	var i;
	var arr;
	var str;
	var x;
	
	for (i = 0; i < form.elements.length; i++)
	{
		if(form.elements[i].name.substr(0,4) == "CHK_")
		{			
			str = GetFieldValueByName(form,form.elements[i].name.substr(4));
			
			if(str != null)
			{
				str = Trim(str);
				arr = form.elements[i].value.split("|");			
				
				switch(arr[0])
				{
					case "STR":
						//check mandatory field							
						if ((str.length == 0) && (arr[3] == "True"))
						{
							alert("The field >" + arr[4] + "< must be filled in");
							return false;
						}
						
						//check min length	
						if (str.length < parseInt(arr[1]))
						{
							alert("The value in field >" + arr[4] + "< is too short");
							return false;
						}
						
						//check max length
						if (str.length > parseInt(arr[2]))
						{
							alert("The value in field >" + arr[4] + "< is too long");
							return false;
						}
						break;
						
						case "ALPHA":
						//check mandatory field							
						if ((str.length == 0) && (arr[3] == "True"))
						{
							alert("The field >" + arr[4] + "< must be filled in");
							return false;
						}
						
						//check min length	
						if (str.length < parseInt(arr[1]))
						{
							alert("The value in field >" + arr[4] + "< is too short");
							return false;
						}
						
						//check max length
						if (str.length > parseInt(arr[2]))
						{
							alert("The value in field >" + arr[4] + "< is too long");
							return false;
						}
						if(!(IsAlpha(str)))
						{
							alert("The field>" + arr[4] + "< must be only alpha characters");
							return false;
						}
						break;
											
					case "INT":
																
						//check mandatory field
						if ((str.length == 0) && (arr[3] == "True"))
						{
							alert("The field >" + arr[4] + "< must be filled in");
							return false;
						}
						
						//check sanity
						if(!(IsNumeric(str)))
						{
							alert("The value in field >" + arr[4] + "< is numeric only");
							return false;
						}
												
						//check min value
						if (arr[1] != "x")
						{
							if (parseInt(str) < parseInt(arr[1]))
							{
								alert("The value in field >" + arr[4] + "< is too small");
								return false;
							}
						}
						
						//check max length
						if (arr[2] != "x")
						{
							if (parseInt(str) > parseInt(arr[2]))
							{
								alert("The value in field >" + arr[4] + "< is too big");
								return false;
							}
						}
						break;
					
					case "FLOAT":
																		
						//check mandatory field
						if ((str.length == 0) && (arr[3] == "True"))
						{
							alert("The field >" + arr[4] + "< must be filled in");
							return false;
						}
						
						//check sanity
						if(!(IsNumeric(str)))
						{
							alert("The value in field >" + arr[4] + "< is numeric only");
							return false;
						}
												
						//check min value
						if (arr[1] != "x")
						{
							if (parseFloat(str) < parseFloat(arr[1]))
							{
								alert("The value in field >" + arr[4] + "< is too small");
								return false;
							}
						}
						
						//check max length
						if (arr[2] != "x")
						{
							if (parseFloat(str) > parseFloat(arr[2]))
							{
								alert("The value in field >" + arr[4] + "< is too big");
								return false;
							}
						}
						
						break;
										
						
					case "DATE":
						
						//check mandatory field
						if ((str.length == 0) && (arr[3] == "True"))
						{
							alert("The field >" + arr[4] + "< must be filled in");
							return false;
						}
						
						//check sanity
						//Removed by ARufkahr 10/6/08 users can only enter through calendar control
						//if(!(IsDate(str)))
						//{
						//	alert("The value in field >" + arr[4] + "< is not a valid date (MM/DD/YYYY)");
						//	return false;
						//}
						
						//check min value
						if (arr[1] != "x")
						{
							if (GetDate(str) < GetDate(arr[1]))
							{
								alert("The date in field >" + arr[4] + "< is less than the minimum date");
								return false;
							}
						}
						
						//check max length
						if (arr[2] != "x")
						{
							if (GetDate(str) > GetDate(arr[2]))
							{
								alert("The value in field >" + arr[4] + "< is later than the maximum date");
								return false;
							}
						}
						
						break;
						
					case "EMAIL":
						
						//check mandatory field
						if ((str.length == 0) && (arr[3] == "True"))
						{
							alert("The field >" + arr[4] + "< must be filled in");
							return false;
						}
						
						if (!(validateEmail(str)))
						{
							alert("The field>" + arr[4] + "< must be a valid email address");
							return false;
						}
						
						//check min length	
						if (str.length < parseInt(arr[1]))
						{
							alert("The value in field >" + arr[4] + "< is too short");
							return false;
						}
						
						//check max length
						if (str.length > parseInt(arr[2]))
						{
							alert("The value in field >" + arr[4] + "< is too long");
							return false;
						}
						
						break;
						
						case "PHONE":
																
						//check mandatory field
						if ((str.length == 0) && (arr[3] == "True"))
						{
							alert("The field >" + arr[4] + "< must be filled in");
							return false;
						}
						
						//check sanity
						if(!(IsNumeric(str)))
						{
							alert("The value in field >" + arr[4] + "< is numeric only");
							return false;
						}
												
						if (str.length < parseInt(arr[1]))
						{
							alert("The value in field >" + arr[4] + "< is too short");
							return false;
						}
						
						//check max length
						if (str.length > parseInt(arr[2]))
						{
							alert("The value in field >" + arr[4] + "< is too long");
							return false;
						}
													
						break;
					
				}
			}
		}
	}
	return true;
}

//============================================================================================

function Replace(string,replacechar,replacewith) 
{
	var temp = "";
	var i;
	
	string = '' + string;
	splitstring = string.split(replacechar);
	for(i = 0; i < splitstring.length; i++)
	{
		if(i < (splitstring.length -1))
			temp += splitstring[i] + replacewith;
		else
			temp += splitstring[i];
	}
	return temp;
}

//============================================================================================

function GetFieldValueByName(form,name)
{
	var i;
	
	for (i=0;i<form.elements.length;i++)
	{
		if(form.elements[i].name == name)
			return (form.elements[i].value);
	}
	return null;
}

/*

======================= STANDARD HELPER FUNCTIONS BELOW =======================================

*/
function IsNumeric(str)
{
	var i;
	for(i=0;i<str.length;i++)
	{
		if(!(	((str.charAt(i) <= '9') && (str.charAt(i) >= '0')) ||
				(str.charAt(i) == ' ') || (str.charAt(i) == '.') || (str.charAt(i) == '-')))
			return(false);
	}
	return(true);
}

//============================================================================================

function IsAlphaNumeric(str)
{
	var i;
	for(i=0;i<str.length;i++)
	{
		if(!(	((str.charAt(i) <= '9') && (str.charAt(i) >= '0')) ||
				((str.charAt(i) <= 'z') && (str.charAt(i) >= 'a')) ||
				(str.charAt(i) == ' ') || (str.charAt(i) == '-') ||
				(str.charAt(i) == 'ü') || (str.charAt(i) == 'ä') ||
				(str.charAt(i) == 'ö') || (str.charAt(i) == 'Ü') ||
				(str.charAt(i) == 'Ä') || (str.charAt(i) == 'Ö') ||
				(str.charAt(i) == 'é') || (str.charAt(i) == 'ß') ||
				((str.charAt(i) <= 'Z') && (str.charAt(i) >= 'A')) ))
			return(false);
	}
	return(true);
}

//============================================================================================

function IsAlpha(str)
{
	var i;
	
	for(i=0;i<str.length;i++)
	{
		if(!(	((str.charAt(i) <= 'z') && (str.charAt(i) >= 'a')) ||
				(str.charAt(i) == ' ') || (str.charAt(i) == '-') ||
				(str.charAt(i) == 'ü') || (str.charAt(i) == 'ä') ||
				(str.charAt(i) == 'ö') || (str.charAt(i) == 'Ü') ||
				(str.charAt(i) == 'Ä') || (str.charAt(i) == 'Ö') ||
				(str.charAt(i) == 'é') || (str.charAt(i) == 'ß') ||
				((str.charAt(i) <= 'Z') && (str.charAt(i) >= 'A')) ))
			return(false);
	}
	return(true);
}

//============================================================================================

function Trim(str)
{
	//trim leding spaces
	while(true)
	{
		if(str.charAt(0) == ' ')
			str = str.substr(1);
		else
			break;
	}
	
	//trim trailing spaces
	while(true)
	{
		if(str.charAt(str.length-1) == ' ')
			str = str.substr(0,str.length-1);
		else
			break;
	}
	return(str);	
}

//============================================================================================

function IsDate(argDate)
{
	var date_split;
	var i;
	var tdate, tmonth, tyear;
	
	date_split = argDate.split('/');
	
	//check for date parts
	if(date_split.length != 3)
		//alert("Alert 1");
		return(false);
		
	//check for zero values
	for(i=0;i<date_split.length;i++)
	{
		if(parseInt(date_split[i],10) == 0)
			//alert("Alert 2");
			return(false);
	}
	
	//check for 4-digit year
	if(date_split[2].length != 4)
		//alert("Alert 3");
		return(false);
		
	//check for valid date, e.g. 02/29/1997
	tdate = parseInt(date_split[1],10);
	tmonth = parseInt(date_split[0],10);
	tyear = parseInt(date_split[2],10);
	
	var date = new Date(parseInt(date_split[2],10),parseInt(date_split[0],10)-1,parseInt(date_split[1],10));
	
	if(date.getDate() != tdate)
		//alert("Alert 4");
		return(false);
	
	if(date.getMonth() != (tmonth-1))
		//alert("Alert 5");
		return(false);
	
	if(date.getFullYear() != tyear)
		//alert("Alert 6");
		return(false);
		
	return(true);
}


function GetDate(argDate)
{
	//use IsDate() first !!!!
	
	var date_split;	
	var tdate, tmonth, tyear;
	
	date_split = argDate.split('/');
	
		
	
	tdate = parseInt(date_split[1],10);
	tmonth = parseInt(date_split[0],10);
	tyear = parseInt(date_split[2],10);
	
	return date = new Date(parseInt(date_split[2],10),parseInt(date_split[0],10)-1,parseInt(date_split[1],10));
		
}
function check_num(id,desc) {
 if (isNaN(document.getElementById(id).value)) 
 	{
	alert("The field >" +desc+ "< must be numeric");
	document.getElementById(id).style.background='#FF0000';
		if (document.getElementById("Submit"))
		{
			document.getElementById("Submit").disabled = "disabled";
		}
	}
 else
 	{
	document.getElementById(id).style.background='#FFFFFF';
		if (document.getElementById("Submit"))
		{
			document.getElementById("Submit").removeAttribute('disabled');
		}
	}	
}
function validateEmail(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}