//----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 validate(form)
{
  
   var els = form.elements;
	 var elen = els.length;
	   var test='';
	 for(var i = 0; i < elen; i++)
	 {
	  //  alert(els[i].value);
		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))
		  {
	
					alert(msg + " 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 + " 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 + " 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 + " accept alphanumeric value!");
				els[i].value = '';
				els[i].focus();
				return false;
		 }
		  
		 if(bh != null && bh == "alpha")
		 {
		    var str=els[i].value;
				var filter=/^[A-Za-z ]+$/i;
				if (! filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert("Enter valid Name!");
				  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!");*/
				  alert("Enter valid email address!");
				  els[i].focus();
				  return false;
				}
		 }
	}
	//eval(form).submit();
}


