$(function(){
total_con=$("#total_con").text();
total_rows=$("#total_rows").text();
max_con=$("#max_con").text();
cat_id=$("#cat_id").attr("value");
cat_name=$("#cat_name").attr("value");
current_con=1;
i=0;







////////////////////////////next_search/////////////////////////////
////////////////////////////next_search/////////////////////////////


$("#next_num_search").live("click",function(){
	
if(current_con<total_con){

current_con++;
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$(this).attr("search");
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
end=(Math.round((eval(current_con)))* Math.round((eval(num_rows))))-1;
start=Math.round((eval(end)))-(Math.round((eval(num_rows)))-1);




current_mod=(current_con-1)%max_con;
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();



//alert(current_con);
//alert(table);
//alert(num_rows);
//alert(search);

//alert(end);
//alert(start);
//alert(cat_id);
//فى حالة عدم وجود الرقم التالى
if(current_mod!=0){
$(".num_con_search").removeClass("num_active");
$("#num_con_search"+current_con).addClass("num_active");
$("#next_num_search").attr("alt",Math.round((eval($("#next_num_search").attr("alt"))))+1);	
$("#prev_num_search").attr("alt",Math.round((eval($("#prev_num_search").attr("alt"))))+1);

}else{


from=Math.round((eval((current_con))))-1 + Math.round((eval((max_con)))) ;
to=(Math.round((eval(from)))-Math.round((eval(max_con))))+1;	

//alert(from);
//alert(to);
$(".num_con_search").show();
for(i=from;i>=to;i--){
before=i-max_con;

$("#num_con_search"+before).attr("alt",i);
$("#num_con_search"+before).text(i);
$("#num_con_search"+before).attr("id","num_con_search"+i);	

if(i>total_con){ $("#num_con_search"+i).hide(); }
}

$(".num_con_search").removeClass("num_active");
$("#num_con_search"+current_con).addClass("num_active");

}

page=$("#load_page").html();
page=page+".php";
$("#load_wait").show();

$("#result_view").load(page,{
"cat":'',
'search':search ,'search_type':search_type ,'sort':sort ,'sort_by':sort_by ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"cat_id":cat_id ,
"cat_name":cat_name ,
"comp":comp ,
"depart":depart 
},function(){

if (end>total_rows){ end=total_rows	}
$("#rows_range").html((end+1)+"-"+(start+1));

$("#load_wait").hide();


	
});
}

});





$("#next_num_search_cart").live("click",function(){
	
if(current_con<total_con){

	current_con++;
	table=$(this).attr("lang");
	num_rows=$(this).attr("max");
	search=$(this).attr("search");
	search_type=$(this).attr("search_type");
	sort=$(this).attr("sort");
	sort_by=$(this).attr("sort_by");
	end=(Math.round((eval(current_con)))* Math.round((eval(num_rows))))-1;
	start=Math.round((eval(end)))-(Math.round((eval(num_rows)))-1);
	search_equal=$("#search_value_type").val();
	order_status_value=$("#order_status_value").val();
	payment_status_value=$("#payment_status_value").val();



	current_mod=(current_con-1)%max_con;
	comp=$("#main_company_list").val();
	depart=$("#main_depart_list").val();



	//alert(current_con);
	//alert(table);
	//alert(num_rows);
	//alert(search);

	//alert(end);
	//alert(start);
	//alert(cat_id);
	//فى حالة عدم وجود الرقم التالى
	if(current_mod!=0){
	$(".num_con_search_cart").removeClass("num_active");
	$("#num_con_search"+current_con).addClass("num_active");
	$("#next_num_search").attr("alt",Math.round((eval($("#next_num_search").attr("alt"))))+1);	
	$("#prev_num_search").attr("alt",Math.round((eval($("#prev_num_search").attr("alt"))))+1);

	}else{


	from=Math.round((eval((current_con))))-1 + Math.round((eval((max_con)))) ;
	to=(Math.round((eval(from)))-Math.round((eval(max_con))))+1;	

	//alert(from);
	//alert(to);
	$(".num_con_search").show();
	for(i=from;i>=to;i--){
	before=i-max_con;

	$("#num_con_search"+before).attr("alt",i);
	$("#num_con_search"+before).text(i);
	$("#num_con_search"+before).attr("id","num_con_search"+i);	

	if(i>total_con){ $("#num_con_search"+i).hide(); }
	}

	alert('Z');
	$(".num_con_search_cart").removeClass("num_active");
	$("#num_con_search"+current_con).addClass("num_active");

	}

	page=$("#load_page").html();
	page=page+".php";
	$("#load_wait").show();

	$("#result_view").load(page,{
	"cat":'',
	'search':search ,'search_type':search_type ,'sort':sort ,'sort_by':sort_by ,
	"table":table ,
	"start":start ,
	"end":end ,
	"num_rows":num_rows ,
	"cat_id":cat_id ,
	"cat_name":cat_name ,
	"comp":comp ,
	"depart":depart ,
	"search_value_type":search_equal,
	"order_status_value":order_status_value,
	"payment_status_value":payment_status_value
	},function(){

	if (end>total_rows){ end=total_rows	}
	$("#rows_range").html((end+1)+"-"+(start+1));

	$("#load_wait").hide();


		
	});
	}

});






////////////////////////////prev_search/////////////////////////////
////////////////////////////prev_search/////////////////////////////



$("#prev_num_search").live("click",function(){	
//alert(current_con);

if(current_con>1){
current_con--;
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$(this).attr("search");
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
end=(Math.round((eval(current_con)))* Math.round((eval(num_rows))))-1;
start=Math.round((eval(end)))-(Math.round((eval(num_rows)))-1);

current_mod=(current_con)%max_con;
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();


//فى حالة عدم وجود الرقم التالى
if(current_mod!=0){
$(".num_con_search").removeClass("num_active");
$("#num_con_search"+current_con).addClass("num_active");
$("#next_num_search").attr("alt",Math.round((eval($("#next_num_search").attr("alt"))))-1);	
$("#prev_num_search").attr("alt",Math.round((eval($("#prev_num_search").attr("alt"))))-1);

//alert(current_con);
}else{

from=current_con ;
to=Math.round((eval((current_con))))-Math.round((eval((max_con))))+1;	
//alert(from);
//alert(to);
$(".num_con_search").show();
for(i=from;i>=to;i--){
before=Math.round((eval(i)))+Math.round((eval(max_con)));

$("#num_con_search"+before).attr("alt",i);
$("#num_con_search"+before).text(i);
$("#num_con_search"+before).attr("id","num_con_search"+i);	

if(i>total_con){ $("#num_con_search"+i).hide(); }
}

$(".num_con_search").removeClass("num_active");
$("#num_con_search"+current_con).addClass("num_active");

}

page=$("#load_page").html();
page=page+".php";

$("#load_wait").show();

$("#result_view").load(page,{
"cat":'',
'search':search ,'search_type':search_type ,'sort':sort ,'sort_by':sort_by ,

"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"cat_id":cat_id ,
"cat_name":cat_name,
"comp":comp ,
"depart":depart 
},function(){

if (end>total_rows){ end=total_rows	}
$("#rows_range").html((end+1)+"-"+(start+1));
$("#load_wait").hide();


	
});
}

});



$("#prev_num_search_cart").live("click",function(){	
//alert(current_con);

if(current_con>1){
current_con--;
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$(this).attr("search");
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
end=(Math.round((eval(current_con)))* Math.round((eval(num_rows))))-1;
start=Math.round((eval(end)))-(Math.round((eval(num_rows)))-1);
search_equal=$("#search_value_type").val();
order_status_value=$("#order_status_value").val();
payment_status_value=$("#payment_status_value").val();
current_mod=(current_con)%max_con;
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();


//فى حالة عدم وجود الرقم التالى
if(current_mod!=0){
$(".num_con_search_cart").removeClass("num_active");
$("#num_con_search"+current_con).addClass("num_active");
$("#next_num_search").attr("alt",Math.round((eval($("#next_num_search").attr("alt"))))-1);	
$("#prev_num_search").attr("alt",Math.round((eval($("#prev_num_search").attr("alt"))))-1);

//alert(current_con);
}else{

from=current_con ;
to=Math.round((eval((current_con))))-Math.round((eval((max_con))))+1;	
//alert(from);
//alert(to);
$(".num_con_search").show();
for(i=from;i>=to;i--){
before=Math.round((eval(i)))+Math.round((eval(max_con)));

$("#num_con_search"+before).attr("alt",i);
$("#num_con_search"+before).text(i);
$("#num_con_search"+before).attr("id","num_con_search"+i);	

if(i>total_con){ $("#num_con_search"+i).hide(); }
}

$(".num_con_search").removeClass("num_active");
$("#num_con_search"+current_con).addClass("num_active");

}

page=$("#load_page").html();
page=page+".php";

$("#load_wait").show();

$("#result_view").load(page,{
"cat":'',
'search':search ,'search_type':search_type ,'sort':sort ,'sort_by':sort_by ,

"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"cat_id":cat_id ,
"cat_name":cat_name,
"comp":comp ,
"depart":depart ,
"search_value_type":search_equal,
"order_status_value":order_status_value,
"payment_status_value":payment_status_value
},function(){

if (end>total_rows){ end=total_rows	}
$("#rows_range").html((end+1)+"-"+(start+1));
$("#load_wait").hide();


	
});
}

});





////////////////////////////num_con_search/////////////////////////////
////////////////////////////num_con_search/////////////////////////////

$(".num_con_search").live("click",function(){

//alert("S");

total_con=$("#total_con").text();
if ($(this).attr("class")!='num_con_search num_active'){

$(".num_con_search").removeClass("num_active");
$(this).addClass("num_active");

current_con=$(this).attr("alt");
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$(this).attr("search");
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
page=$("#load_page").html();
page=page+".php";
$("#load_wait").show();

end=(Math.round((eval(current_con)))* Math.round((eval(num_rows))))-1;
start=Math.round((eval(end)))-(Math.round((eval(num_rows)))-1);

//alert(current_con);
//alert(table);
//alert(num_rows);
//alert(search);
//alert(page);
//alert(end);
//alert(start);
//alert(cat_id);


$("#next_num").attr("alt",Math.round((eval(current_con)))+1);
$("#prev_num").attr("alt",Math.round((eval(current_con)))-1);

$("#load_wait").show();

$("#result_view").load(page,{
"cat":'',
'search':search ,'search_type':search_type ,'sort':sort ,'sort_by':sort_by ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"comp":comp ,
"depart":depart 
},function(){
	
	
	
	
$("#load_wait").hide();




});	

}
//alert("S");
});






$(".num_con_search_cart").live("click",function(){

//alert("S");

total_con=$("#total_con").text();
if ($(this).attr("class")!='num_con_search num_active'){

$(".num_con_search_cart").removeClass("num_active");
$(this).addClass("num_active");

current_con=$(this).attr("alt");
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$(this).attr("search");
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
page=$("#load_page").html();
search_equal=$("#search_value_type").val();
order_status_value=$("#order_status_value").val();
payment_status_value=$("#payment_status_value").val();

page=page+".php";
$("#load_wait").show();

end=(Math.round((eval(current_con)))* Math.round((eval(num_rows))))-1;
start=Math.round((eval(end)))-(Math.round((eval(num_rows)))-1);

//alert(current_con);
//alert(table);
//alert(num_rows);
//alert(search);
//alert(page);
//alert(end);
//alert(start);
//alert(cat_id);


$("#next_num").attr("alt",Math.round((eval(current_con)))+1);
$("#prev_num").attr("alt",Math.round((eval(current_con)))-1);

$("#load_wait").show();

$("#result_view").load(page,{
"cat":'',
'search':search ,
'search_type':search_type ,
'sort':sort ,
'sort_by':sort_by ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"comp":comp ,
"depart":depart ,
"search_value_type":search_equal,
"order_status_value":order_status_value,
"payment_status_value":payment_status_value

},function(data){
	
	
	//alert(data);
	
$("#load_wait").hide();




});	

}
//alert("S");
});










$("#main_company_list_internal").bind("change",function(){
comp=$(this).val();	
$("#main_depart_list_internal").load("ajax_load_depart.php",{

"comp":comp 

},function(data){
	
	//alert(data);
	
});	
	
});
	
	
$("#main_company_list").bind("change",function(){
total_con=$("#total_con").text();

current_con=$(this).attr("alt");
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$("#search_text").val();
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();
page=$("#load_page").html();
page=page+".php";
$("#load_wait").show();

end=0;
start=0;

//alert(current_con);
//alert(table);
//alert(num_rows);
//alert(search);
//alert(page);
//alert(end);
//alert(start);
//alert(cat_id);




$("#load_wait").show();

$("#result_view").load(page,{
"cat":'',
'search':search ,
"table":table ,
'search':search ,'search_type':search_type ,'sort':sort ,'sort_by':sort_by ,
"end":end ,
"num_rows":num_rows ,
"comp":comp , 
"depart":depart
},function(){
	
$("#main_depart_list").load("ajax_load_depart.php",{

"comp":comp 

},function(){
	
});

	//alert("xxx");
$("#num_container").load("ajax_num_container.php",{
"table":table ,
'search':search ,
"comp":comp ,
"depart":depart
	
},function(){ 

$("#num_rows").html($("#total_con").html());

});


	
$("#load_wait").hide();

});	


});


$("#main_depart_list").bind("change",function(){
total_con=$("#total_con").text();

current_con=$(this).attr("alt");
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$("#search_text").val();
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();
page=$("#load_page").html();
page=page+".php";
$("#load_wait").show();

end=0;
start=0;

//alert(current_con);
//alert(table);
//alert(num_rows);
//alert(search);
//alert(page);
//alert(end);
//alert(start);
//alert(cat_id);




$("#load_wait").show();

$("#result_view").load(page,{
"cat":'',
'search':search ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"comp":comp , 
"depart":depart
},function(){
	
	
	//alert("xxx");
$("#num_container").load("ajax_num_container.php",{
"table":table ,
'search':search ,
"comp":comp ,
"depart":depart
	
},function(){ 

$("#num_rows").html($("#total_con").html());

});


	
$("#load_wait").hide();

});	


});


$("#search_btn").bind("click",function(){
total_con=$("#total_con").text();

current_con=$(this).attr("alt");
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$("#search_text").val();
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();
page=$("#load_page").html();
page=page+".php";
$("#load_wait").show();

end=0;
start=0;

//alert(current_con);
//alert(table);
//alert(num_rows);
//alert(search);
//alert(page);
//alert(end);
//alert(start);
//alert(cat_id);




$("#load_wait").show();

$("#result_view").load(page,{
"cat":'',
'search':search ,'search_type':search_type ,'sort':sort ,'sort_by':sort_by ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"comp":comp , 
"depart":depart
},function(){
	
	
	//alert("xxx");
$("#num_container").load("ajax_num_container.php",{
"table":table ,
'search':search ,
"comp":comp ,
"depart":depart
	
},function(){ 

$("#num_rows").html($("#total_con").html());

});


	
$("#load_wait").hide();

});	


});




$(".search_type").click(function(){
	
	if($(this).val()=='orders.country'){
		$("#main_address_list").show();
		$("#payment_methods_list").hide();
		$("#search_text").hide();
		$("#search_text").val("");
		
	}else if($(this).val()=='orders.payment_method'){
		$("#payment_methods_list").show();
		$("#main_address_list").hide();
		$("#search_text").hide();
		$("#search_text").val("");
		
	}else{
		$("#main_address_list").hide();
		$("#payment_methods_list").hide();
		$("#search_text").show();
		$("#search_text").val("");
		
	}
	
});


$("#main_address_list,#payment_methods_list").change(function(){
	
	$("#search_text").val($(this).val());
});

$(".order_status_all").change(function(){
	
	if ($(this).prop('checked')==true){ 
		$(".order_status").attr("checked","checked");
		$("#order_status_value").val("/0/-1/-2/-3/1/2/3");
		
	}else{
		$(".order_status").removeAttr("checked");	
		$("#order_status_value").val("");
	}
});

$(".payment_status_all").change(function(){
	
	if ($(this).prop('checked')==true){ 
		$(".payment_status").attr("checked","checked");	
		$("#payment_status_value").val("1-1-1-1-1-1");
		$(".payment_status").val("1");	
	}else{
		$(".payment_status").removeAttr("checked");	
		$("#payment_status_value").val("0-0-0-0-0-0");
		$(".payment_status").val("0");	
	}
});


$(".payment_status").change(function(){
	if($(this).val()=="1"){
		$(this).val("0");
	}else{
		$(this).val("1");
	};
	$("#payment_status_value").val($(".payment_status:eq(0)").val() + "-" + $(".payment_status:eq(1)").val() + "-" + $(".payment_status:eq(2)").val() + "-" + $(".payment_status:eq(3)").val() + "-" + $(".payment_status:eq(4)").val() + "-" + $(".payment_status:eq(5)").val() );
	
});


$(".order_status").change(function(){
	
	order_status_concatinate='';
	$('.order_status:checked').map(function() {
		  order_status_concatinate+= $(this).val();
	}).get().join();
	$("#order_status_value").val(order_status_concatinate);
		
});



$("#search_btn_cart").bind("click",function(){
total_con=$("#total_con").text();

current_con=$(this).attr("alt");
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$("#search_text").val();
search_value_type=$("#search_value_type").val();
search_type=$(".search_type:checked").val();
payment_status_value=$("#payment_status_value").val();
order_status_value=$("#order_status_value").val();
sort=$("#sort").val();
sort_by=$("#sort_by").val();
comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();
page=$("#load_page").html();
page=page+".php";
$("#load_wait").show();

end=0;
start=0;

//alert(current_con);
//alert(table);
//alert(num_rows);
//alert(search);
//alert(page);
//alert(end);
//alert(start);
//alert(cat_id);




$("#load_wait").show();
//alert(page);
$("#result_view").load(page,{
"cat":'',
'search':search ,
'search_value_type':search_value_type,
'search_type':search_type ,
'payment_status_value':payment_status_value,
'order_status_value':order_status_value,
'sort':sort ,
'sort_by':sort_by ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"comp":comp , 
"depart":depart
},function(){
	

$("#num_container").load("ajax_num_container_cart.php",{

'search':search ,
'search_value_type':search_value_type,
'search_type':search_type ,
'payment_status_value':payment_status_value,
'order_status_value':order_status_value,
'sort':sort ,
'sort_by':sort_by ,
"start":start ,
"end":end ,
"num_rows":num_rows 


	
},function(data){ 
alert(data);
$("#num_rows").html($("#total_rows").html());

});


	
$("#load_wait").hide();

});	


});


	
	
	
});





