// JavaScript Document
function template_validation12()
{
	var counter=document.getElementById('field_counter').value;
	var option='';
	var er_count=0;
	
	if($.trim($("#upload").val())=='')
	{
			option+="<p>Image field are required</p>";
			er_count=er_count+1;
	}
	else
	{	
		 var a=$.trim($("#upload").val());
		  if(a.indexOf(".jpg") == -1 )
		  { 
				option+="<p>Image type is not valid</p>";
				er_count=er_count+1;
		  }
	}
	
	
	if($.trim($("#title").val())=='')
	{
		option+="<p>Name is required</p>";
		er_count=er_count+1;
	}
	
	if($.trim($("#url").val())=='')
	{
		option+="<p>Viedo url is required</p>";
		er_count=er_count+1;
	}
	else if(youtbe_url($("#url").val())==false)
	{
		option+="<p>viedo url is not valid youtube url</p>";
		er_count=er_count+1;
	}
	
	for(var i=0; i<counter;i++)
	{
		var cap=$.trim($("#cap"+i).val());
		var ckecking=cap.toLowerCase();
		var values=$.trim($("#val"+i).val());
		var types=$.trim($("#type"+i).val());
		if(values=='' && types!='select' && types!='datepicker' && types!='url')
		{
			option+="<p>"+cap+" is required</p>";
			er_count=er_count+1;
		}
		/*else if(types=='ckeditor')
		{
			var ck="val"+i
			alert(CKEDITOR.instances.ck.getData());
			return false;
			if($.trim($("#val"+i).val())=="")
			{
				option+=cap+" are required <br>";
				er_count=er_count+1;	
			}
		}*/
		else if(types=='url')
		{
			if(values=='')
			{
				option+="<p>"+cap+" is required</p>";
				er_count=er_count+1;
			}
			else if(isUrl(values)==false)
			{
				option+="<p>Invalid url of "+cap+"</p>";
				er_count=er_count+1;
			}
		}
		else if(types=='datepicker')
		{
			if($.trim($("#datepicker"+i).val())=='')
			{
				option+="<p>"+cap+" is required</p>";
				er_count=er_count+1;	
			}
		}
		else
		{
			if(ckecking.indexOf("password")>=0 || ckecking.indexOf("email")>=0)
			{
				if(ckecking.indexOf("password")>=0)
				{
					var nextcap=$.trim($("#cap"+(i+1)).val());
					var nextf=nextcap.toLowerCase();
					if(nextf.indexOf("password")>=0 && $.trim($("#val"+(i+1)).val())!='')
					{
						if(values!=$.trim($("#val"+(i+1)).val()))
						{
							option+="<p>"+cap+" and "+ nextcap+" are not match</p>";
							er_count=er_count+1;
						}
					}
				}
				else if(ckecking.indexOf("email")>=0)
				{
					if(!EmailCheck(values))
					{
						option+="<p>"+cap+" address is invalid</p>";
						er_count=er_count+1;
					}
				}
			}
			
			else if($.trim($("#type"+i).val())=='radio' || $.trim($("#type"+i).val())=='checkbox')
			{
				if($.trim($("#type"+i).val())=='radio')
				{
					var name=ckecking.replace(' ','_');
					var x=document.getElementsByName(name).length;
					var y=document.getElementsByName(name);			
					var k=0;
					for(var j=0; j<x; j++)
					{
						if(y[j].checked==true)
						{
							k=k+1;
						}
					}
					if(k==0)
					{
						option+="<p>Pleas select "+cap+"</p>";
						er_count=er_count+1;
					}
				}
				else if($.trim($("#type"+i).val())=='checkbox')
				{
					var name=ckecking.replace(' ','_');
					var x=document.getElementsByName(name+'[]').length;
					var y=document.getElementsByName(name+'[]');
					var k=0;
					for(var j=0; j<x; j++)
					{
						if(y[j].checked==true)
						{
							k=k+1;
						}
					}
					if(k==0)
					{
						option+="<p>Pleas select "+cap+"</p>";
						er_count=er_count+1;
					}
				}
			}
			else if($.trim($("#type"+i).val())=='file')
			{
				  var valid_extensions = /(.jpg)$/i;   
				  if(!valid_extensions.test(values))
				  { 
				   		option+="<p>"+cap+" is not type of jpg</p>";
						er_count=er_count+1;
				  }
			}
		}
	}
	
	if(er_count>0)
	{
		document.getElementById('mytable').style.display='block';
		$('#mytable').html(option);
		return false;
		
	}
	else
	{
		
		return true;	
	
	}
}

function isNumberKey(evt)
{
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
}

function EmailCheck(eAddr) 
{ 
   var ndxAt = ndxDot = 0;
   ndxAt  = eAddr.indexOf("@");
   ndxDot = eAddr.indexOf(".");
   if ( (ndxDot < 0) || (ndxAt < 0) )
      return false;
   else if (ndxDot < ndxAt)
      return false;
   else if ( (ndxDot - 2) <= ndxAt)
      return false;
   else if ( eAddr.length <= (ndxDot + 3) )
      return false;
   else
    	return true;                       
}

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	return regexp.test(s);
}

function youtbe_url(s) {
	var regexp = /http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/;
	return regexp.test(s);
}
