//------------------------ common --------------------------//
function all_select(obj1,obj2)
{
	
	for(i=0;i<obj2.length;i++)
	{
		if(obj1.checked==true)
		{
			obj2[i].checked = true; 
		}
		if(obj1.checked==false)
		{
			obj2[i].checked = false; 
		}
	}
}
//----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))
		  {
		   	
			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 == "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 == "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 == "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 == "date")
		 {
		    var str=els[i].value;
				//var filter = /^\d{4}\-\d{1,2}\-\d{1,2}$/
var filter =  /^([1-9][0-9]{3})[-]((0[1-9])|(1[0-2]))[-]((0[1-9])|([12][0-9])|(3[0-1]))$/;
				if (! filter.test(str))
				{
				  /*alert(msg +"Enter valid email address!");*/
				  alert("Invalid date!");
				  els[i].focus();
				  return false;
				}
		 }
	}
	//eval(form).submit();
}

//-------------------------end ------------------------------//

/// ------------------- For category -------------------------//

function check_name_func(val,id)
{
	 var url ="categoryAjax.php?action=checkname&cat_id="+id+"&cat_name="+val;
	 
	 makeRequest('div_cat_name',url);
}
function cquick_edit(val,id,div)
{
	document.getElementById("show"+id).innerHTML=' <input name="quick_cname" value="'+val+'" id="quick_cname" type="text" class="admin_textbox_small"><br><input name="edit" type="button" class="css_btns_large" value="Edit" onClick="javascript:cat_name_edit('+id+','+"'"+div+"'"+');" />&nbsp;&nbsp; <input name="cancle" type="button" class="css_btns_large" value="Cancle" onClick="javascript:cat_name_edit_cancle('+"'"+val+"'"+','+id+');" />';
}
function cat_name_edit_cancle(val,id)
{
	document.getElementById("show"+id).innerHTML=val;
}
function cat_name_edit(id,div)
{
	var name= document.getElementById('quick_cname').value;
	var url ="categoryAjax.php?action=catnameedit&cat_id="+id+"&cat_name="+name;
	makeRequest(div,url);
	alert('<input type="button"  name="qedit" class="css_btns" value="Quick Edit" onclick="javascript:cquick_edit('+"'"+name+"',"+id+",'"+div+"'"+');" />');
	document.getElementById("showm"+id).innerHTML='<input type="button"  name="qedit" class="css_btns_large" value="Quick Edit" onclick="javascript:cquick_edit('+"'"+name+"',"+id+",'"+div+"'"+');" />';
}
function categoryOrder(setpage,query,fname,orderby,div)
{
	var url = "categoryAjax.php?action=orderbyCat&query="+query+"&setpage="+setpage+"&fname="+fname+"&orderby="+orderby;
	makeRequest(div,url);
}
function positionUpdateCat(setpage,query,position_action,id,position,div)
{
	var url = "categoryAjax.php?action=orderbyCat&query="+query+"&setpage="+setpage+"&position_action="+position_action+"&position="+position+"&update_id="+id;
	makeRequest(div,url);
}
function form_submit(purpose)
{
	if(purpose=='delete')
	{
			if(confirm('If you delete this category then all subcategories and all products under those subcategories will be deleted .So are you sure that you want to delete this Category?'))
			{
				document.cat.act.value=purpose;
				document.cat.submit();
			}
	}
	
	else if(confirm('Are you sure that you want to '+purpose+' this record'))
	{
		document.cat.act.value=purpose;
		document.cat.submit();
	}
}
function single_delete(id)
{
	
	if(confirm('If you delete this category then all subcategories and all products under those subcategories will be deleted .So are you sure that you want to delete this Category?'))
	{
		document.cat.cat_ids[0].checked =true;
		document.cat.cat_ids[0].value=id;
		document.cat.act.value='delete';
		alert(document.cat.cat_ids[0].value);
		document.cat.submit();
	}

}
function back_prev()
{
	window.location.href="categories.php";
}
/// ---------------------------------------------------------//
/// ------------------- For subcategory -------------------------//
function check_subcat(val,id)
{
	cat_id = document.getElementById("cat_id").value;
	var url ="categoryAjax.php?action=checksubcatname&sub_cat_id="+id+"&sub_cat_name="+val+"&cat_id="+cat_id;
	
	 makeRequest('div_cat_name',url);
}

function back_prev_sub()
{
	window.location.href="subCategories.php";
}
function cquick_edit_sub(val,id,div,parent_id)
{
	document.getElementById("show"+id).innerHTML=' <input name="quick_cname" value="'+val+'" id="quick_cname" type="text" class="admin_textbox_small"><br><input name="edit" type="button" class="css_btns_large" value="Edit" onClick="javascript:sub_cat_name_edit('+id+','+"'"+div+"'"+','+parent_id+');" />&nbsp;&nbsp; <input name="cancle" type="button" class="css_btns_large" value="Cancle" onClick="javascript:sub_cat_name_edit_cancle('+"'"+val+"'"+','+id+');" />';
}
function sub_cat_name_edit_cancle(val,id)
{
	document.getElementById("show"+id).innerHTML=val;
}
function sub_cat_name_edit(id,div,parent_id)
{
	var name= document.getElementById('quick_cname').value;
	var url ="categoryAjax.php?action=subcatnameedit&cat_id="+id+"&cat_name="+name+"&parent_id="+parent_id;
	makeRequest(div,url);
	
	document.getElementById("showm"+id).innerHTML='<input type="button"  name="qedit" class="css_btns_large" value="Quick Edit" onclick="javascript:cquick_edit_sub('+"'"+name+"',"+id+",'"+div+"'"+','+parent_id+');" />';
}
function subCategoryOrder(setpage,query,fname,orderby,div)
{
	var url = "categoryAjax.php?action=orderbySubCat&query="+query+"&setpage="+setpage+"&fname="+fname+"&orderby="+orderby;
	//alert(url);
	makeRequest(div,url);
}
function positionUpdateCatSub(setpage,query,position_action,id,position,div,parent_id)
{
	var url = "categoryAjax.php?action=orderbySubCat&query="+query+"&setpage="+setpage+"&position_action="+position_action+"&position="+position+"&update_id="+id+"&parent_id="+parent_id;
	makeRequest(div,url);
}
function form_submit_sub(purpose)
{
	if(purpose=='delete')
	{
			if(confirm('If you delete this category then all subcategories and all products under those subcategories will be deleted .So are you sure that you want to delete this Category?'))
			{
				document.cat.act.value=purpose;
				document.cat.submit();
			}
	}
	
	else if(confirm('Are you sure that you want to '+purpose+' this record'))
	{
		document.cat.act.value=purpose;
		document.cat.submit();
	}
}
function single_delete_sub(id)
{
	
	if(confirm('If you delete this category then all subcategories and all products under those subcategories will be deleted .So are you sure that you want to delete this Category?'))
	{
		document.cat.cat_ids[0].checked =true;
		document.cat.cat_ids[0].value=id;
		document.cat.act.value='delete';
		alert(document.cat.cat_ids[0].value);
		document.cat.submit();
	}

}
/// ---------------------------------------------------------//
/// ------------------- For product page -------------------------//

function back_prev_prod()
{
	window.location.href="products.php";
}
function getSubCat(val)
{
	 var url ="productAjax.php?action=getSubCat&cat_id="+val;
	
	 makeRequest('subcatDiv',url);
}
function getSearchSubCat(val)
{
	 var url ="productAjax.php?action=getSearchSubCat&cat_id="+val;
	
	 makeRequest('searchDiv',url);
}
function productOrder(setpage,query,fname,orderby,div)
{
	var url = "productAjax.php?action=orderbyProduct&query="+query+"&setpage="+setpage+"&fname="+fname+"&orderby="+orderby;
	makeRequest(div,url);
}
/// ---------------------------------------------------------//
