function validate(){
	//Allow A-Z a-z 0-9
	//var PASSWORD = '[^A-Za-z0-9]+';
	var PASSWORD = '[^A-Za-z0-9~`!@#$%\^&*()_+=/. ,<>:;-\?"{}[]]+';

 	//Allow A-Z a-z 0-9 space	
	var ALPHA_NUMERIC = '[^A-Za-z0-9 ]+';
		
	//Allow A-Z a-z 0-9 ! @ # $ % & * ( ) _ + = / . space , < > : ; - { } [ ] "
	//Also allow '
	var ALPHA_NUMERIC_PLUS = '[^A-Za-z0-9~`!@#$%\^&*()_+=/. ,<>:;-\?"{}[]]+';
 	
	//Allow A-Z a-z space
	var ALPHA		  ='[^A-Za-z ]+';
	
	//Allow A-Z a-z . space , -
 	var ALPHA_PLUS	  ='[^A-Za-z .,-]+';
 	
	//Allow 0-9
	var NUMERIC		  ='[^0-9]+';
 	
	//Allow 0-9 space $ . : ( ) - + 
 	var NUMERIC_PLUS  ='[^0-9 $%.:()-+]+';
 	
	//Allow the date format dd/mm/yyyy where
	//dd can be from 1-31
	//mm can be from 1-12
	//yyyy can be from 1960-2049
	var DATE		  = /^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[012])\/(196\d|20[0-4]\d)/;
 	
	//Allow the format userid@hostname.typeoforg.country[optional] where
	//user id can contain A-Z a-z 0-9 _ and only one or no . or - consequently 
	//hostname can contain A-Z a-z 0-9 _
	//typeoforg can contain A-Z a-z 0-9 but at least 2 characters long
	//country can contain A-Z a-z 0-9 but at least 2 characters long
	
	var EMAIL 		  = /^\w+([.\-_]?\w+)([.\-_]?\w+)[-]*@\w+(\.\w{2,})+$/;
 	
	//Allow 0-9 .
	var DOUBLE	      ='[^0-9.]+';
	
	//Allow 0-9 .
	var PRICE	      =/^\d+(\.\d{2})?$/;
	
	
	var error = "Please check the following fields: \n";				//Store the first sentence of the error message
	var str = "";														//Store all error messages
	var pattern;														//Store the pattern of the field
	var value;															//Store the value of the field
	var mandatory;														//Store a string to specify whether the field is mandatory
	var same;														
	var offendingChar; 													//Store invalid value
 	var prevOffendingChar;
	var fields = new Array();											//Store different types of fields
	var minchar = 6;													//Store the min no. of char password must have								
	
	fields[0] = document.getElementsByTagName('input');
	fields[1] = document.getElementsByTagName('textarea');
	fields[2] = document.getElementsByTagName('select');	
	
	for(var i=0; i<fields.length; i++){	
		for(var j = 0; j < fields[i].length; j++) { 
			pattern = fields[i].item(j).getAttribute('pattern'); 
			
			if (pattern != null) { 
				same = fields[i].item(j).getAttribute('same'); 
				
				fields[i].item(j).style.background = "#FFFFFF";  
				value = fields[i].item(j).value; 
				mandatory = fields[i].item(j).getAttribute('mandatory');
				
				//Check if the field is mandatory			
				if(mandatory != null && mandatory == "true"){
					if(value == null || value == ""){
						fields[i].item(j).style.background = "#FFFFCC";  
						str += fields[i].item(j).getAttribute('display') + " should not be blank \n";
					}
				}
				
				if(pattern == "alphanumeric")
					pattern = ALPHA_NUMERIC;
				else if(pattern == "numeric")
					pattern = NUMERIC;
				else if(pattern == "alpha")
					pattern = ALPHA;
				else if(pattern == "alphaplus")
					pattern = ALPHA_PLUS;
				else if(pattern == "numericplus")
					pattern = NUMERIC_PLUS;
				else if(pattern=="date")
					pattern = DATE;
				else if(pattern =="double")
					pattern = DOUBLE;
				else if (pattern =="email")
					pattern = EMAIL;
				else if(pattern == "alphanumericplus")
					pattern = ALPHA_NUMERIC_PLUS;
				else if(pattern == "password")
					pattern = PASSWORD;
				else if(pattern == "price")
					pattern = PRICE;
				
				if(value != "" && value.length != 0){     			
					if(pattern == DATE || pattern == EMAIL || pattern == PRICE){       								
						if(pattern.test(value) == false){
							offendingChar = value;     									
						}else{
							offendingChar = null;
						} 
					}else{				
						offendingChar = value.match(pattern);
						
						//Allow ' if the pattern is ALPHA_NUMERIC_PLUS
						if(pattern == ALPHA_NUMERIC_PLUS && value.indexOf("'") != -1){
							offendingChar = null;
						}

						//Check if password contains at least 6 characters
						if(pattern == PASSWORD){	
							if(value.length < minchar){
								str += fields[i].item(j).getAttribute('display') + " should have at least " + minchar + " characters\n";	
								offendingChar = "";
							}
						}
					}
								
					if(offendingChar != null) {     				
						//Add up all error messages 
						if(pattern != PASSWORD){
							str += "Invalid value ('" + value + "') supplied for " + fields[i].item(j).getAttribute('display') + "\n";
						}
						
						if(pattern == PASSWORD && value.length >= minchar){
							//Hide the value for password if invalid
							str += "Invalid value supplied for " + fields[i].item(j).getAttribute('display') + "\n";
						}
						
						//Notify the user by changing background color of the field to yellow 
						fields[i].item(j).style.background = "#FFFFCC"; 
					}else{
						if(same != null && same != "" && prevOffendingChar == null){
							//Check if the current field is a duplication of the previous field
							if(fields[i].item(j).value != fields[i].item(j-1).value){
								str += fields[i].item(j).getAttribute('display') + " should be the same as " + fields[i].item(j-1).getAttribute('display') + "\n";
								//Notify the user by changing background color of the field to yellow 
								fields[i].item(j).style.background = "#FFFFCC"; 
							}
						}
					}
					
					prevOffendingChar = offendingChar;
				}   		  
			} 
		}
	}
		
 	if (str != "") { 
 		alert(error + str );  		
		return false; 
 	} else { 
    	return true; 
 	}
}
