function validate(f,err) { //v4.0
var p,t,val,errors;
errors="";
 for (i=0; i<(f.length); i++) {
 t=f[i]; 
 msg=t.title
 switch (t.type){
	case "text":
	{
		val=t.name;
		p=val.split("_");
		for(var j=0;j<p.length-1;j++)
		{
			 switch (p[j])
			 {		

				case "r":
				{				
					if(t.value.Trim()=="")
					{
						errors =msg;
						alert(errors);
						t.focus();
						return false;
					}
					break;
				 }				
				case "ee":
				{		
					if(t.value != "")
					{
						var filter=/^.+@.+\..{2,4}$/
						if(!filter.test(t.value.Trim()))
						{			
							errors =msg;
							alert(errors);
							t.focus();
							return false;							
						}
					}	
					break;
				 }
				 case "p":
				 {		
					varpostcode = t.value
						if(varpostcode != "")
						{
						varerrnum = true	
						varerralpha = true
							for(x=0;x<varpostcode.length;x++)
							{			
								if(!(!(varpostcode.charCodeAt(x)>57 | varpostcode.charCodeAt(x) < 48) || !(varpostcode.charCodeAt(x)>90 | varpostcode.charCodeAt(x)<65) || !(varpostcode.charCodeAt(x)>122 | varpostcode.charCodeAt(x)<97) || varpostcode.charCodeAt(x) == 32))
								{
								alert("Please enter a valid postcode")
								t.focus()
								return false
								}
								if(varpostcode.charCodeAt(x) != 32)
								{ 
									if(varpostcode.charCodeAt(x)>57 | varpostcode.charCodeAt(x) < 48)
									{
										varerrnum = false
									}				
									if(!(!(varpostcode.charCodeAt(x)>122 | varpostcode.charCodeAt(x) < 97) || !(varpostcode.charCodeAt(x)>90 | varpostcode.charCodeAt(x) < 65)))
									{
										varerralpha = false
									}	
								}			
							}
							if(varerrnum | varerralpha)
							{
								alert("Please enter a valid postcode")
								t.focus()
								return false
							}
							if(!(varpostcode.charAt(3)==" " || varpostcode.charAt(4)==" "))
							{
								alert("Please enter the Postcode in the required format")
								t.focus()
								return false
							}
							//commented out because there are some exceptions to this i.e. certain BL postcodes : all applications are checked manually anyway...
							//if(!( (varpostcode.charAt(0) == "b" && varpostcode.charAt(1) == "B") || (varpostcode.charAt(0) == "B" && varpostcode.charAt(1) == "b") || (varpostcode.charAt(0) == "B" && varpostcode.charAt(1) == "B") || (varpostcode.charAt(0) == "b" && varpostcode.charAt(1) == "b") ))
							//{
								//alert("Your postcode needs to begin with BB in order to register with us")
								//t.focus()
								//return false
							//}		
						}
						break;
				 }
				 case "po":
				 {		
					varpostcode = t.value
						if(varpostcode != "")
						{
						varerrnum = true	
						varerralpha = true
							for(x=0;x<varpostcode.length;x++)
							{			
								if(!(!(varpostcode.charCodeAt(x)>57 | varpostcode.charCodeAt(x) < 48) || !(varpostcode.charCodeAt(x)>90 | varpostcode.charCodeAt(x)<65) || !(varpostcode.charCodeAt(x)>122 | varpostcode.charCodeAt(x)<97) || varpostcode.charCodeAt(x) == 32))
								{
								alert("Please enter a valid postcode")
								t.focus()
								return false
								}
								if(varpostcode.charCodeAt(x) != 32)
								{ 
									if(varpostcode.charCodeAt(x)>57 | varpostcode.charCodeAt(x) < 48)
									{
										varerrnum = false
									}				
									if(!(!(varpostcode.charCodeAt(x)>122 | varpostcode.charCodeAt(x) < 97) || !(varpostcode.charCodeAt(x)>90 | varpostcode.charCodeAt(x) < 65)))
									{
										varerralpha = false
									}	
								}			
							}
							if(varerrnum | varerralpha)
							{
								alert("Please enter a valid postcode")
								t.focus()
								return false
							}
							if(!(varpostcode.charAt(3)==" " || varpostcode.charAt(4)==" "))
							{
								alert("Please enter the Postcode in the required format")
								t.focus()
								return false
							}
								
						}
						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;					
					}
					for(var n=0;n<countno.length;n++)
					{
						if(isNaN(countno[n])==true)
						{
							alert("You must have to enter number");
							t.focus();
							return false;							
						}
						if(countno[n]< 0 || countno[n]>255)
						{
							alert("Number must be less then 255 and greater then 0"+countno[n]);
							t.focus();
							return false;							
						}							
					}
					break;
				 }
				 
			 }

		}
	}	
	
case "password":
	{
		val=t.name;
		p=val.split("_");
		for(var j=0;j<p.length-1;j++)
		{				
			 switch (p[j])
			 {		
				case "r":
				{				
					if(t.value.Trim()=="")
					{
						errors =msg;
						alert(errors);
						t.focus();
						return false;
					}
					break;
				 }	
				 case "c":
				 {
					 u=f[i+1]; 	
					 if(t.value.Trim()!= "" && u.value.Trim()!= "")
					 {
						if (t.value != u.value)
						{
						   alert("Password and Confirm Password should match");       
						   t.focus();
						   return false;
						}
					 }
					break;
				 }
			 }

		}
	}
	
	
     case "textarea":
     {
		val=t.name;
		p=val.split("_");
		for(var j=0;j<p.length-1;j++)
		{
			switch (p[j])
			 {		

				case "r":
				{
					//alert(Asc(t.value));
					if(t.value.Trim()=="")
					{
						errors =msg;
						alert(errors);
						t.focus();
						return false;						
					}
					if(t.value.length > p[j+1])
					{
						errors =msg;
						alert(errors);
						t.focus();
						return false;						
					}
					break;
				 }

				case "l":
				{
					if(t.value.length > p[j+1])
					{
						errors =msg;
						alert(errors);
						t.focus();
						return false;						
					}
					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(t.value.Trim()=="")
					{
						errors =msg;
						if(errors == "")
						errors = err
						alert(errors);
						t.focus();
						return false;
					}
					break;
				 }
			}
		}
	}
  } 
 } 
}

function strtrim() 
{
   return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
String.prototype.Trim = strtrim;

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


//Function for selecting a value from dropdown control
function  selectdata(strcombopath,strcompdata)
    {
     with (strcombopath)
    {
    for(i=0;i<length;i++)
    if(options[i].value == strcompdata) options[i].selected=true
    }
   }
//Auto Select function ends here

//function for clearing fields
function clearfields(f)
{
f.reset()
noofcontrols=f.length
	for(index = 0;index < noofcontrols; index++)
	{
		element=f[index]	
		
		if(element.type == "text" || element.type == "textarea" || element.type == "password")
			element.value=""
		if(element.type == "checkbox")		
			element.checked=false
		if(element.type == "select-one")		
			element.options[0].selected=true
		/*if(element.type == "select-multiple")		
			element.options[0].selected=true	*/
	}	
}
//End of Clear function