$(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++;
current_id=$(this).attr("current_id");
search_type=$(this).attr("search_type");
type=$(this).attr("type");
table=$(this).attr("lang");
num_rows=$(this).attr("max");
search=$(this).attr("search");
end=(Math.round((eval(current_con)))* Math.round((eval(num_rows))))-1;
start=Math.round((eval(end)))-(Math.round((eval(num_rows)))-1);

page=$("#load_page").html();
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
page=page+".php";


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,{
"current_id":current_id,
"type":type,
"current_type":current_type,
"cat":'',
'search':search ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"cat_id":cat_id ,
"cat_name":cat_name ,
"comp":comp ,
"depart":depart ,
"search_type":search_type,
"sort":sort,
"sort_by":sort_by
},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");

current_id=$(this).attr("current_id");
search_type=$(this).attr("search_type");
type=$(this).attr("type");
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();
page=$("#load_page").html();
search_type=$(this).attr("search_type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
page=page+".php";


//فى حالة عدم وجود الرقم التالى
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,{
"current_id":current_id,
"type":type,
"current_type":current_type,	
"cat":'',
'search':search ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"cat_id":cat_id ,
"cat_name":cat_name,
"comp":comp ,
"depart":depart ,
"search_type":search_type,
"type":type,
"sort":sort,
"sort_by":sort_by
},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(){

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");
current_id=$(this).attr("current_id");
current_type=$(this).attr("current_type");
type=$(this).attr("type");


comp=$("#main_company_list").val();
depart=$("#main_depart_list").val();
page=$("#load_page").html();
search_type=$(this).attr("search_type");
type=$(this).attr("type");
sort=$(this).attr("sort");
sort_by=$(this).attr("sort_by");
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,{
"current_id":current_id,
"type":type,
"current_type":current_type,
"cat":'',
'search':search ,
"table":table ,
"start":start ,
"end":end ,
"num_rows":num_rows ,
"comp":comp ,
"depart":depart ,
"search_type":search_type,
"type":type,
"sort":sort,
"sort_by":sort_by

},function(){
	
	
	
	
$("#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();
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,
"search_type":search_type,
"sort":sort,
"sort_by":sort_by
},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();
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_rows").html());

});


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

});	


});





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

current_con=$(this).attr("alt");
table='polls';
num_rows=$(this).attr("max");
search=$("#search_text").val();

type=$("#main_forms_type").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 ,
"type":type
},function(){
	
	
//alert("xxx");
$("#num_container").load("ajax_num_type_container.php",{
"table":table ,
'search':search ,
"type":type

	
},function(){ 

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

});


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

});	


});




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

current_con=$(this).attr("alt");
table='polls';
num_rows=$(this).attr("max");
search=$("#search_text").val();

type=$("#main_polls_type").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 ,

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

	
},function(){ 

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

});


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

});	


});


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

current_con=$(this).attr("alt");
table='polls';
num_rows=$(this).attr("max");
search=$("#search_text").val();

type=$("#main_polls_type_results").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 ,
"type":type
},function(){
	
	
//alert("xxx");
$("#num_container").load("ajax_num_type_results_container.php",{
"table":table ,
'search':search ,
"type":type

	
},function(){ 

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

});


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

});	


});









$("#search_btnx").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();
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();

});	


});


	
});





