// JavaScript Document
//----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;
	 
	 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) ||els[i].value=='Comments' || els[i].value=='Email-id' || els[i].value=='Name')
		  {
		   	
			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" && els[i].id!='cat_id')
		 {
			  
			  for(j=1;j<=els[i].length;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;
				}
		 }
		 var chkFlag = false;
		 if(els[i].id=='cat_id')
		 {
			 len = document.blog.cat_id.length;
			  for(j=0;j<len;j++)
			  {
					 i = i+1; 
					if(document.blog.cat_id[j].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(els[i].type == "password" && els[i].value.length<6)
		{
			 alert("Password length must be 6 or more character !");
				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" && 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 charecter!");
				  els[i].focus();
				  return false;
				}
			
		 }
		  if(bh != null && bh == "url" && els[i].value != "" )
		 {
		     	var str=els[i].value;
				var filter=/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/i
				if (! filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert("Please enter a valid url!");
				  els[i].focus();
				  return false;
				}
			
		 }
		 if(bh != null && bh == "alpha_n" && els[i].value != "" )
		 {
		     	var str=els[i].value;
				var filter=/^[A-Za-z0-9]+$/i
				if (!filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert(msg +"can't contained special charecter!");
				  els[i].focus();
				  return false;
				}
			
		 }
		 if(bh != null && bh == "phone" && els[i].value != "" )
		 {
		     	var str=els[i].value;
				var filter=/^[0-9 -]+$/i
				if (!filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert("Enter a valid phone number!");
				  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_ -.,]+$/i
				if (!filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert(msg +"can't contained special charecter ."+'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!");*/
				  alert("Enter valid email address!");
				  els[i].focus();
				  return false;
				}
		 }
		 if(bh!= null && bh =='date')
		{
			 var str=els[i].value;
			var filter =/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/i

			if (! filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert("Enter valid Date(mm/dd/yyyyy)!");
				  els[i].focus();
				  return false;
				}
		}
	}
	//eval(form).submit();
}

