//----trim spaces function---->
function trimspace(str)
{
  var len= str.length;
  if (len != 0)
  {
    for(var i=0;i<len;i++)
    {
      if(str.indexOf(" ")==0)
        str=str.substring(1,len);
    }
    strtrim=str;
  }
  else
  {
    strtrim=str;
  }
  return strtrim;
}

//-----main function----->
function validate2(form)
{
  
   var els = form.elements;
	 var elen = els.length;
	 
	 for(var i = 0; i < elen; i++)
	 {
	    var av = els[i].getAttribute('required');
			var bh = els[i].getAttribute('behaviour');
			var msg = els[i].getAttribute('message');
			if(msg == "" || msg == null){
			  msg = av;
			}
			if(msg == "" || msg == null){
				  msg = els[i].name;
			}
			
		  if(av != null && (els[i].type == "textarea" || els[i].type == "text" ||  els[i].type == "select-one" || els[i].type == "password") && !trimspace(els[i].value))
		  {
		   	  document.getElementById("txtHint").innerHTML='';
			alert(msg + " field can't be left blank!");
				els[i].value = '';
				els[i].focus();
				return false;
		 }
		 
		 //----check box----->
		 var chkFlag = false;
		 if(av != null && els[i].type == "checkbox")
		 {
			  for(j=1;j<=bh;j++)
			  {
					if(j != 1) { i = i+1; }
					if(els[i].checked == true)
					{
						chkFlag = true; 
					}
				}
			
			  if(chkFlag == false)
				{
					alert(msg + " field can't be left blank!");
					return false;
				}
		 }
		
		 if(els[i].type == "password" && els[i-1].type == "password" && (els[i].value != els[i-1].value))
		 {
			  alert(msg + " does not match with Password!");
				els[i].focus();
				return false;
		 }
		 if(bh != null && bh == "numeric" && isNaN(els[i].value))
		 {
		    alert(msg + " field accept only numeric value!");
				els[i].value = '';
				els[i].focus();
				return false;
		 }
		 if(bh != null && bh == "alphanumeric" && els[i].value != "" && !isNaN(els[i].value))
		 {
		    alert(msg + " field accept alphanumeric value!");
				els[i].value = '';
				els[i].focus();
				return false;
		 }
		  if(bh != null && bh == "alpha" && els[i].value != "" )
		 {
		     	var str=els[i].value;
				var filter=/^[A-Za-z ]+$/i
				if (! filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert(msg +" can't contained numeric value and special character!");
				  els[i].focus();
				  return false;
				}
			
		 }
		  if(bh != null && bh == "alpha_n3" && els[i].value != "" )
		 {
		     	var str=els[i].value;
				var filter=/^[A-Za-z0-9_ -.,?!\n]+$/i
				if (!filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert(msg +" can't contained special character ."+'You can use alphabate, space, "-",".",",","?","!" and "_")!');
				  els[i].focus();
				  return false;
				}
			
		 }
		 
		 if(bh != null && bh == "email")
		 {
		    var str=els[i].value;
				var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
				if (! filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				   document.getElementById("txtHint2").innerHTML='';
				  alert("Enter valid email address!");
				  els[i].focus();
				  return false;
				}
		 }
		 
		 
		  if(bh != null && bh == "date")
		 {
			 var str=els[i].value;
			// alert(str);
			 
		    
				//var filter = /^\d{4}\-\d{1,2}\-\d{1,2}$/
							var filter =  /^((0[1-9])|([12][0-9])|(3[0-1]))[-]((0[1-9])|(1[0-2]))[-]([1-9][0-9]{3})$/;  // dd-mm-yyyy
//var filter =  /^([1-9][0-9]{3})[-]((0[1-9])|(1[0-2]))[-]((0[1-9])|([12][0-9])|(3[0-1]))$/;  // yyyy-mm-dd
				if (! filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert("Invalid date!");
				  els[i].focus();
				  return false;
				}
			
		 }

	}
	//eval(form).submit();
}

