function validate(f) 
{ //v4.0
var p,t,val,errors;
errors="";
 for (i=0; i<(f.length); i++) {
 t=f[i];  
 msg1=t.language
 msg= t.title
 switch (t.type){
	case "text": case "password":
	{
		val=t.name;
		p=val.split("_");
		for(var j=0;j<p.length-1;j++)
		{
			 switch (p[j])
			 {		
				case "r":
				{				
					if(Trim(t.value)=="")
					{
						errors = "Das Formular ist unvollständig ausgefüllt. Bitte geben Sie Ihren " + t.title					
						alert(errors);
						t.focus();
						return false;
					}
					break;
				 }
				case "e":
				{	
					if (t.value=="")
					{
						//errors =msg;
						errors = "Das Formular ist unvollständig ausgefüllt. Bitte geben Sie Ihren " + t.title					
						alert(errors);
						t.focus();
						return false;
					}			
					if(t.value.indexOf("@")==-1 || t.value.indexOf(".")==-1)
					{
						errors ="Please provide valid Email Address";
						alert(errors);
						t.focus();
						return false;
						break;
					}
					break;
				}
				case "m":
				{				
					if(t.value != ""){
					if(t.value.indexOf("@")==-1 || t.value.indexOf(".")==-1)
					{
						//errors =msg;
						errors ="Please provide valid Email Address";
						alert(errors);
						t.focus();
						return false;
						break;
					}
					}
					break;
				 }
				 case "i":
				{
					var countno;
					countno=t.value.split(".")
					if(countno.lengt>4)
					{
					alert("Please provide a valid address eg:(192.168.1.202)");
					t.focus();
					return false;
					break;
					}
					for(var n=0;n<countno.length;n++)
					{
						if(isNaN(countno[n])==true)
						{
							alert("You must have to enter number");
							t.focus();
							return false;
							break;
						}
						if(countno[n]< 0 || countno[n]>255)
						{
							alert("Number must be less than 255 and greater than 0 "+countno[n]);
							t.focus();
							return false;
							break;
						}	
						
					}
					break;
				 }
				 
			 }

		}
	}
     case "textarea":
     {
		val=t.name;
		p=val.split("_");
		for(var j=0;j<p.length-1;j++)
		{
			switch (p[j])
			 {		

				case "r":
				{
					var enterKey
					enterKey = escape(t.value);
					//alert(enterKey)
					if(Trim(enterKey)== "%0D%0A")
					{
						//errors =msg;
						errors="Das Formular ist unvollständig ausgefüllt. Bitte geben Sie Ihren " + t.title
						alert(errors);
						t.value = "";
						t.focus();
						return false;
						break;
					}
					if(Trim(t.value)=="")
					{
						errors="Das Formular ist unvollständig ausgefüllt. Bitte geben Sie Ihren " + t.title
						//errors =msg;						
						alert(errors);
						t.value = "";
						t.focus();
						return false;
						break;
					}
					if(t.value.length > p[j+1])
					{
						errors ="You have exceeded the max limit for this field.\n Kindly enter in the " + t.title + "\n and it should not be more than " + p[j+1] + " characters.";
						alert(errors);
						t.focus();
						return false;
						break;
					}
					break;
				 }

				case "l":
				{
					if(t.value.length > p[j+1])
					{
						//errors =msg;
						errors ="You have exceeded the max limit for this field.\n Kindly enter in the " + t.title + "\n and it should not be more than " + p[j+1] + " characters.";
						alert(errors);
						t.focus();
						return false;
						break;
					}
					break;
				 }

			}
			
		}
	}
     case "select-one":{
		val=t.name;
		p=val.split("_");
		for(var j=0;j<p.length-1;j++)
		{
			 switch (p[j])
			 {		

				case "r":
				{
					if(Trim(t.value)=="")
					{
						//errors =msg;
						errors="Das Formular ist unvollständig ausgefüllt. Bitte geben Sie Ihren " + t.title
						alert(errors);
						t.focus();
						return false;
					}
					break;
				 }
			}
		}
	}
  } 
 } 
}




function RTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for(j = String.length - 1; j >= 0; j--)
	{
		if (String.substr(j, 1) != ' ' &&
			String.substr(j, 1) != '\t')
		break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}

function LTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for (i = 0; i < String.length; i++)
	{
		if (String.substr(i, 1) != ' ' &&
		    String.substr(i, 1) != '\t')
			break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}

function Trim(String)
{
str=String;
  while (str.charAt(str.length - 1)==" ")
    str = str.substring(0, str.length - 1);
  while (str.charAt(0)==" ")
    str = str.substring(1, str.length);
  return str;
}

function FormatNumber(Expression, NumDigitsAfterDecimal)
{
	var iNumDecimals = NumDigitsAfterDecimal;
	var dbInVal = Expression;
	var bNegative = false;
	var iInVal = 0;
	var strInVal
	var strWhole = "", strDec = "";
	var strTemp = "", strOut = "";
	var iLen = 0;

	if (dbInVal < 0)
	{
		bNegative = true;
		dbInVal *= -1;
	}

	dbInVal = dbInVal * Math.pow(10, iNumDecimals)
	iInVal = parseInt(dbInVal);
	if ((dbInVal - iInVal) >= .5)
	{
		iInVal++;
	}
	strInVal = iInVal + "";
	strWhole = strInVal.substring(0, (strInVal.length - iNumDecimals));
	strDec = strInVal.substring((strInVal.length - iNumDecimals), strInVal.length);
	while (strDec.length < iNumDecimals)
	{
		strDec = "0" + strDec;
	}
	iLen = strWhole.length;
	if (iLen >= 3)
	{
		while (iLen > 0)
		{
			strTemp = strWhole.substring(iLen - 3, iLen);
			if (strTemp.length == 3)
			{
				strOut = "," + strTemp + strOut;
				iLen -= 3;
			}
			else
			{
				strOut = strTemp + strOut;
				iLen = 0;
			}
		}
		if (strOut.substring(0, 1) == ",")
		{
			strWhole = strOut.substring(1, strOut.length);
		}
		else
		{
			strWhole = strOut;
		}
	}
	if (bNegative)
	{
		return "-" + strWhole + "." + strDec;
	}
	else
	{
		return strWhole + "." + strDec;
	}
}

function Asc(string)
{
	var symbols = " !\"#$%&'()*+'-./0123456789:;<=>?@";
	var loAZ = "abcdefghijklmnopqrstuvwxyz";
	symbols += loAZ.toUpperCase();
	symbols += "[\\]^_`";
	symbols += loAZ;
	symbols += "{|}~";
	var loc;
	loc = symbols.indexOf(string);
	if (loc > -1)
	{ 
		Ascii_Decimal = 32 + loc;
		return (32 + loc);
	}
	return (0);
}


//date validation code begins here
var dtCh= "/";
var minYear=2003;
var maxYear=2050;

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid date")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm(cdt){
	if (isDate(cdt)==false){
		//dt.focus()
		return false
	}
    return true
 }

function ValidateDate(d,m,y){
	var newdt
	newdt = d +"/"+m +"/"+y
	if (isDate(newdt)==false){
		return false
	}
	return true
}

//date validation code ends here
