$(function(){

var total_con=$("#num_rows").text()/$("#rows_num").val();
var max_con=2;
var total_rows;

var current_con=1;
var table=$("#table_name").html();
var sort_by="id";
var sort="2"; //asc
var num_rows=$("#rows_num").val();
var search_field;
var search_field_type;
var search_type;
var search;
var var_encrypt='';

$('.encrypt_var').each(function(){
var_encrypt+=$(this).attr('exam_id')+","+$(this).val()+"####";
	
});



search_field=$("#search_field").val();
search_field_type=$("#search_field option:selected").attr("type");
search_type=$("#search_type").val();









//alert(current_con);
//alert(total_con);





$(".view_image").live("mouseover",function(){
//alert("S");
$(this).next(".view_image_big").show();

});

$(".view_image").live("mouseleave",function(){
//alert("Z");
$(this).next(".view_image_big").hide();

});

////////////////////////////current con/////////////////////////////
////////////////////////////current con/////////////////////////////


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

//alert("s");

//alert(current_con);
//alert(total_con);



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

current_con=$(this).attr("alt");




table=$("#table_name").html();
search_field=$("#search_field").val();
search_field_type=$("#search_field option:selected").attr("type");
search_type=$("#search_type").val();


if($("#search_text_options").val()=="" || $("#search_text_options").val()==null){
search=$("#search_text").val();	
}else{
search=$("#search_text_options").val();	
}





end=(parseInt(current_con)* parseInt(num_rows))-1;
start=parseInt(end)-(parseInt(num_rows)-1);
current_mod=(current_con-1)%max_con;
num_rows=$("#rows_num").val();




$.post(website_url+"/"+"decrypt.php",{

value:$("#table_name").html()

},function(page){
	

page="load_"+page+"_search.php";


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



end=num_rows;
//alert("ggg0");
//alert(page);
//alert(table);
//alert(var_encrypt); 
//alert(num_rows);
//alert(search_field);
//alert(search_field_type); 
//alert(search_type);
//alert(sort);
//alert(sort_by); 




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

//alert("num_rows"+num_rows);

$.post(website_url+"/"+page,{
"table":table,	
"start":start,	
"var_encrypt":var_encrypt,
"num_rows":num_rows ,
'search':search ,
'search_field':search_field ,
'search_field_type':search_field_type ,
'search_type':search_type ,
'sort':sort ,
'sort_by':sort_by  


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

$("#load_view").html(all_data[0]);

total_num_rows=all_data[1];

//alert(num_rows);
//alert(all_data[1]);






});




});











});





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


$("#next_num_search_new").live("click",function(){

total_con=$("#num_rows").text()/$("#rows_num").val();

//alert(current_con);
//alert(total_con);

if(current_con<total_con){



current_con++;




//alert("jj");


$table=table=$("#table_name").html();
search_field=$("#search_field").val();
search_field_type=$("#search_field option:selected").attr("type");
search_type=$("#search_type").val();


if($("#search_text_options").val()=="" || $("#search_text_options").val()==null){
search=$("#search_text").val();	
}else{
search=$("#search_text_options").val();	
}


var_encrypt='';




end=(parseInt(current_con)* parseInt(num_rows))-1;
start=parseInt(end)-(parseInt(num_rows)-1);
current_mod=(current_con-1)%max_con;
num_rows=$("#rows_num").val();




$.post(website_url+"/"+"decrypt.php",{

	value:$("#table_name").html()

},function(page){
//alert(page);	

page="load_"+page+"_search.php";


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

end=num_rows;


//alert(sort);
//alert(sort_by);
////alert(search_field);
//alert(search_field_type);
//alert(search_type);
//alert(search);
//alert(table);
//alert(num_rows);
////alert(current_con);
////alert(total_con);
//alert(num_rows);	
//alert("current_con"+current_con);
//alert("max_con"+max_con);
//alert(var_encrypt);


current_mod=(current_con)%max_con;

//alert(current_mod);


//فى حالة عدم وجود الرقم التالى
if(current_mod!=0){
$(".num_con_search_new").removeClass("num_active");
$("#num_con_search_new"+current_con).addClass("num_active");
$("#next_num_search_new").attr("alt",parseInt($("#next_num_search_new").attr("alt"))-1);	
$("#prev_num_search_new").attr("alt",parseInt($("#prev_num_search_new").attr("alt"))-1);

//alert("xx");
}else{
//alert("xxx");
from=current_con ;
to=parseInt((current_con))+parseInt((max_con))-1;	
//alert(from);
//alert(to);
$(".num_con_search_new").show();
for(i=from;i>=to;i--){
before=parseInt(i)+parseInt(max_con);

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

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

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

}




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

//alert("num_rows"+num_rows);

$.post(website_url+"/"+page,{
"table":table,		
"start":start ,
"var_encrypt":var_encrypt,
"num_rows":num_rows ,
'search':search ,
'search_field':search_field ,
'search_field_type':search_field_type ,
'search_type':search_type ,
'sort':sort ,
'sort_by':sort_by 


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

all_data=data.split('####');


$("#load_view").html(all_data[0]);

total_num_rows=all_data[1];

//alert(num_rows);
//alert(all_data[1]);


total_rows=total_num_rows;


});




});









}

});







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


$("#prev_num_search_new").live("click",function(){

total_con=$("#num_rows").text()/$("#rows_num").val();

//alert(current_con);
//alert(total_con);

if(current_con>1){



current_con--;






table=$("#table_name").html();
search_field=$("#search_field").val();
search_field_type=$("#search_field option:selected").attr("type");
search_type=$("#search_type").val();


if($("#search_text_options").val()=="" || $("#search_text_options").val()==null){
search=$("#search_text").val();	
}else{
search=$("#search_text_options").val();	
}



end=(parseInt(current_con)* parseInt(num_rows))-1;
start=parseInt(end)-(parseInt(num_rows)-1);
current_mod=(current_con-1)%max_con;
num_rows=$("#rows_num").val();




$.post(website_url+"/"+"decrypt.php",{

	value:$("#table_name").html()

},function(page){
//alert(page);	

page="load_"+page+"_search.php";


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

end=num_rows;


//alert(sort);
//alert(sort_by);
////alert(search_field);
//alert(search_field_type);
//alert(search_type);
//alert(search);
//alert(table);
//alert(num_rows);
////alert(current_con);
////alert(total_con);
//alert(num_rows);	
//alert("current_con"+current_con);
//alert("max_con"+max_con);



current_mod=(current_con)%max_con;

//alert(current_mod);


//فى حالة عدم وجود الرقم التالى
if(current_mod!=0){
$(".num_con_search_new").removeClass("num_active");
$("#num_con_search_new"+current_con).addClass("num_active");
$("#next_num_search_new").attr("alt",parseInt($("#next_num_search_new").attr("alt"))-1);	
$("#prev_num_search_new").attr("alt",parseInt($("#prev_num_search_new").attr("alt"))-1);

//alert("xx");
}else{
//alert("xxx");
from=current_con ;
to=parseInt((current_con))+parseInt((max_con))+1;	
//alert(from);
//alert(to);
$(".num_con_search_new").show();
for(i=from;i>=to;i--){
before=parseInt(i)+parseInt(max_con);

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

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

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

}




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

//alert("num_rows"+num_rows);

$.post(website_url+"/"+page,{
"table":table,		
"start":start ,
"var_encrypt":var_encrypt,
"num_rows":num_rows ,
'search':search ,
'search_field':search_field ,
'search_field_type':search_field_type ,
'search_type':search_type ,
'sort':sort ,
'sort_by':sort_by 


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

all_data=data.split('####');



$("#load_view").html(all_data[0]);

total_num_rows=all_data[1];

//alert(num_rows);
//alert(all_data[1]);


total_rows=total_num_rows;


});




});









}

});




$("#print_all").live("click",function(){
	


$.post(website_url+"/"+"decrypt.php",{

	value:$("#table_name").html()

},function(table){
//alert(table);	



table=$("#table_name").html();
search_field=$("#search_field").val();
search_field_type=$("#search_field option:selected").attr("type");
search_type=$("#search_type").val();
search=$("#search_text").val();
end=(parseInt(current_con)* parseInt(num_rows))-1;
start=parseInt(end)-(parseInt(num_rows)-1);
current_mod=(current_con-1)%max_con;
num_rows=$("#rows_num").val();

	
$.post(website_url+"/"+'print.php',{
	
"table":table ,	
"start":start ,
"var_encrypt":var_encrypt,
"end":end ,
"num_rows":num_rows ,
'search':search ,
'search_field':search_field ,
'search_field_type':search_field_type ,
'search_type':search_type ,
'sort':sort ,
'sort_by':sort_by 
	
	
},
function(data) {
window.open("print.php?table="+table + "&start=" + start + "&end=" + end + 
					  "&num_rows=" + num_rows + "&search="+search + 
					  "&search_field=" + search_field + "&search_field_type="+ search_field_type +
					  "&search_type=" + search_type + "&sort=" + sort + "&sort_by=" + sort_by+ "&dir=0" );
});	
	

});



});





//alert("sx");



$("#print_selected").live("click",function(){
	
	
records="";
records_ids="";
records_count=0;


$("input:checkbox[name=u_ckeckbox]:checked").each(function(index) {
	records+=$(this).attr("alt")+",";
		

records_count++;
});

if(records_count==0){	
alert("لم تقم باختيار اى سجل")	;
exit();
}

	
	

$.post(website_url+"/"+"decrypt.php",{

	value:$("#table_name").html()

},function(table){
//alert(page);	



table=$("#table_name").html();
search_field=$("#search_field").val();
search_field_type=$("#search_field option:selected").attr("type");
search_type=$("#search_type").val();


if($("#search_text_options").val()=="" || $("#search_text_options").val()==null){
search=$("#search_text").val();	
}else{
search=$("#search_text_options").val();	
}


end=(parseInt(current_con)* parseInt(num_rows))-1;
start=parseInt(end)-(parseInt(num_rows)-1);
current_mod=(current_con-1)%max_con;
num_rows=$("#rows_num").val();

	
$.post(website_url+"/"+'print_selected.php',{
	

	
},
function(data) {
window.open("print_selected.php?ids="+records+"&table="+table + "&start=" + start + "&end=" + end + 
					  "&num_rows=" + num_rows + "&search="+search + 
					  "&search_field=" + search_field + "&search_field_type="+ search_field_type +
					  "&search_type=" + search_type + "&sort=" + sort + "&sort_by=" + sort_by+ "&dir=0" );
});	
	

});



});







$("#delete_selected").live("click",function(){
	
	
records="";
records_ids="";
records_count=0;


$("input:checkbox[name=u_ckeckbox]:checked").each(function(index) {
	records+=$(this).attr("alt")+",";
		
records_count++;
});

if(records_count==0){	
alert("لم تقم باختيار اى سجل")	;
exit();
}





$.post(website_url+"/"+"delete_selected.php",{table:table_name,id:records},function(data){


$("input:checkbox[name=u_ckeckbox]:checked").each(function(index) {
	
   $(this).parent().parent().remove();
});



alert("تم الالغاء");


$("#num_rows").html($("#num_rows").html()-check_length);

//alert(data);

});


});





$("#rows_num").change(function(){
	






//total_con=$("#total_con").text();
current_con=1;
table=$("#table_name").html();
sort_by="id";
sort="1";
num_rows=$("#rows_num").val();
search_field=$("#search_field").val();
search_field_type=$("#search_field option:selected").attr("type");
search_type=$("#search_type").val();


if($("#search_text_options").val()=="" || $("#search_text_options").val()==null){
search=$("#search_text").val();	
}else{
search=$("#search_text_options").val();	
}


end=(parseInt(current_con)* parseInt(num_rows))-1;
start=parseInt(end)-(parseInt(num_rows)-1);




page="";

$.post(website_url+"/"+"decrypt.php",{

	value:$("#table_name").html()

},function(page){
//alert("r");
//alert(page);	

page="load_"+page+"_search.php";


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




//alert("strt"+start)

//alert(search_field);
//alert(search_field_type);
//alert(search_type);
//alert(search);
////alert(current_con);
//alert(table);
//alert("num_rows"+num_rows);
//alert(sort);
//alert(sort_by);



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

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





$.post(website_url+"/"+page,{
"table":table,	
"start":start ,
"var_encrypt":var_encrypt,
"end":end ,
"num_rows":num_rows ,
'search':search ,
'search_field':search_field ,
'search_field_type':search_field_type ,
'search_type':search_type ,
'sort':sort ,
'sort_by':sort_by 


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

all_data=data.split('####');


$("#load_view").html(all_data[0]);

total_num_rows=all_data[1];
//alert(num_rows);
//alert("total_num_rows"+all_data[1]);


total_rows=total_num_rows;
current_con=1;




$("#load_num_cont").load("num_container_new.php",{

"total_num_rows":total_num_rows ,
"num_rows":num_rows

});





});




});




});





///////////report////////////////////



$("#print_direction").click(function(){
	
if($(this).attr("dir")=="0"){
myWindow = window.open($(this).attr("link"), "طباعة", "width=1050, height=730");
myWindow.resizeTo(1050, 730);
$(this).attr("dir","1")	;
//alert("s");
}else{
myWindow = window.open($(this).attr("link"), "طباعة", "width=730 ,height=1050");
myWindow.resizeTo(730, 1050);
$(this).attr("dir","0")	;	
//alert("r");
}	
	
	
});



$('.page_title').keypress(function(e) {

if(e.which == 13) {

$(".page_title").val($(this).val());

}

});



$('#print_refresh_button').click(function() {
    location.reload();
});





$(".record_delete").click(function(){


if($(this).is(':checked')){
$(".td_del"+$(this).attr("del")).show();	

}else{
	
$(".td_del"+$(this).attr("del")).hide();		
}

});






$(".record_sort").click(function(){

//alert($("#table_name").html());


if($(this).attr("alt")=="0" || $(this).attr("alt")=="2"){

$(this).css("background","#fff url('../img/record_sort_asc.png') center center no-repeat");	
$(this).attr("alt","1");
}else if($(this).attr("alt")=="1"){

$(this).css("background","#fff url('../img/record_sort_desc.png') center center no-repeat");
$(this).attr("alt","2");	

}




//total_con=$("#total_con").text();
current_con=1;
table=$("#table_name").html();
sort_by=$(this).attr("record");
sort=$(this).attr("alt");
num_rows=$("#rows_num").val();
search_field=$("#search_field").val();
search_field_type=$("#search_field option:selected").attr("type");
search_type=$("#search_type").val();


if($("#search_text_options").val()=="" || $("#search_text_options").val()==null){
search=$("#search_text").val();	
}else{
search=$("#search_text_options").val();	
}



end=(parseInt(current_con)* parseInt(num_rows))-1;
start=parseInt(end)-(parseInt(num_rows)-1);




//alert(end);







page="";

$.post(website_url+"/"+"decrypt.php",{

	value:$("#table_name").html()

},function(page){
//alert(page);	

page="load_"+page+"_search.php";


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




//alert("strt"+start)

//alert(search_field);
//alert(search_field_type);
//alert(search_type);
//alert(search);
////alert(current_con);
//alert(table);
//alert("num_rows"+num_rows);
//alert(sort);
//alert(sort_by);



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

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





$.post(website_url+"/"+page,{
"table":table,		
"start":start ,
"var_encrypt":var_encrypt,
"end":end ,
"num_rows":num_rows ,
'search':search ,
'search_field':search_field ,
'search_field_type':search_field_type ,
'search_type':search_type ,
'sort':sort ,
'sort_by':sort_by 


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

all_data=data.split('####');


$("#load_view").html(all_data[0]);

total_num_rows=all_data[1];
//alert(num_rows);
//alert("total_num_rows"+all_data[1]);


total_rows=total_num_rows;
current_con=1;




$("#load_num_cont").load("num_container_new.php",{

"total_num_rows":total_num_rows ,
"num_rows":num_rows

});





});




});



	





});







$("#g_checkbox").live("click",function(){
	

if($('#g_checkbox').is(":checked")){

	$('.u_ckeckbox').attr("checked","checked");

}else{

	$('.u_ckeckbox').removeAttr("checked");	
};



});

$("#close").click(function(){
$("#big-div-cont").hide();	
$("#over").hide();
$(".over_loading").hide();
});



$(".water_mark").click(function(){
//alert(website_url);	
if($(this).is(':checked')){
$.post(website_url+"/cp/"+"ajax_watermark_session.php",{"id":"1"},function(data){});
}else{
$.post(website_url+"/cp/"+"ajax_watermark_session.php",{"id":"0"},function(data){});	
}	

});


$(".cp_body").hide();
$(".cp_body_tall").hide();
var tot="";
var x=$(".privlidge").length;
for(i=0;i<=x;i++){
xx=Math.round(eval(i));	
if($(".privlidge").eq(xx).is(':checked')){
tot=tot+$(".privlidge").eq(xx).attr("value");	
}	
}
$("#privlidge_total").val(tot);		


$(".cp_row_title").click(function(){

if($(this).attr("alt")=='0'){
$(this).attr("alt","1") ;

}else{
$(this).attr("alt","0") ;	
}





$("#cp_body"+$(this).attr("lang")).slideToggle();
if ($("#cp_arrow"+$(this).attr("lang")).attr("src")=='../img/cp/up.png'){
$("#cp_arrow"+$(this).attr("lang")).attr("src","../img/cp/down.png");	


}else{
$("#cp_arrow"+$(this).attr("lang")).attr("src","../img/cp/up.png");
$(this).css({"background":"#FF6663"});			
}
$(".cp_row_title[alt=0]").css({"background":"#0088FF"});
});	

$(".privlidge").click(function(){
tot="";
for(i=0;i<=x;i++){
xx=Math.round(eval(i));	
if($(".privlidge").eq(xx).is(':checked')){
tot=tot+$(".privlidge").eq(xx).attr("value");	
}	
}
$("#privlidge_total").val(tot);	
});



//generate code and name

code='';





$("#stage").change(function(){
if($(this).val()!="1"){
$("#year>option[value='4']").hide();
$("#year>option[value='5']").hide();
$("#year>option[value='6']").hide();	
}else{
$("#year>option[value='4']").show();
$("#year>option[value='5']").show();
$("#year>option[value='6']").show();
}	

	
});
$("#depart").change(function(){

$("#title").val($("option[value="+$("#depart").val()+"]").html());
$("#desc").val($("option[value="+$("#depart").val()+"]").html());

en_desc=''
if($("option[value="+$("#depart").val()+"]").html()=='غرف النوم'){
en_desc='Bed Rooms'	
}else if($("option[value="+$("#depart").val()+"]").html()=='غرف السفرة'){
en_desc='Dining Rooms'		
}else if($("option[value="+$("#depart").val()+"]").html()=='غرف الاطفال'){
en_desc='Kids Rooms'		
}else if($("option[value="+$("#depart").val()+"]").html()=='الصالونات'){
en_desc='Salon Rooms'		
}else if($("option[value="+$("#depart").val()+"]").html()=='الانتريهات'){
en_desc='Living Rooms'		
}else if($("option[value="+$("#depart").val()+"]").html()=='الانتيكات'){
en_desc='Antiques'		
}else if($("option[value="+$("#depart").val()+"]").html()=='اثاث مكتبى'){
en_desc='Office Furniture'		
}else if($("option[value="+$("#depart").val()+"]").html()=='الترابيزات'){
en_desc='Tables'		
}else if($("option[value="+$("#depart").val()+"]").html()=='شازلونجات'){
en_desc='Shazlongs'		
}else if($("option[value="+$("#depart").val()+"]").html()=='المطابخ'){
en_desc='kitchrens'		
}else if($("option[value="+$("#depart").val()+"]").html()=='اقمشة وستائر'){
en_desc='Curtains'		
}


$("#title_en").val(en_desc);
$("#desc_en").val(en_desc);


code='';
if ($("#depart").val()!=''){	
	
if($("#depart").val()=="1"){code='A' ; }
else if($("#depart").val()=="2"){code='B' ; }
else if($("#depart").val()=="3"){code='C' ; }
else if($("#depart").val()=="4"){code='D' ; }
else if($("#depart").val()=="5"){code='E' ; }
else if($("#depart").val()=="6"){code='F' ; }
else if($("#depart").val()=="7"){code='G' ; }
else if($("#depart").val()=="8"){code='H' ; }
else if($("#depart").val()=="9"){code='I' ; }
else if($("#depart").val()=="10"){code='J' ; }
else if($("#depart").val()=="11"){code='K' ; }
else if($("#depart").val()=="12"){code='L' ; }
else if($("#depart").val()=="13"){code='M' ; }
else if($("#depart").val()=="14"){code='N' ; }
else if($("#depart").val()=="15"){code='O' ; }
else if($("#depart").val()=="16"){code='P' ; }
else if($("#depart").val()=="17"){code='Q' ; }
else if($("#depart").val()=="18"){code='R' ; }
else if($("#depart").val()=="19"){code='S' ; }
else if($("#depart").val()=="20"){code='T' ; }
else if($("#depart").val()=="21"){code='U' ; }
else if($("#depart").val()=="22"){code='Q' ; }
else if($("#depart").val()=="23"){code='W' ; }
else if($("#depart").val()=="24"){code='X' ; }
else if($("#depart").val()=="25"){code='Y' ; }
else if($("#depart").val()=="26"){code='Z' ; }

$("#code_generator").load("code_generator.php",{"depart":$("#depart").val()},function(){
//alert($("#depart").val());
$code_gen="";

﻿   
code_num=$("#code_generator").html();
//alert(code_num);
code_num=code_num.substring(2,code_num.length);
if(isNaN(code_num) || code_num=='﻿ ' || code_num=='' || code_num=='﻿  ' || code_num=='﻿  ' ||code_num=='﻿   ' ){
code_num=1;	
}else{
code_num=parseInt(code_num)+1;	
}









if(isNaN(code_num)){
$("#code").val(code+"1");	
}else{
$("#code").val(code+code_num);
}




});

}else{
$("#code").val('');	
}

});

$(".wrapper").click(function(){
$(this).next().trigger("click");
});

//alert("Ds");




$("input[value=job]").click(function(){
$("#search_text").hide();
$("#specific_job_list").hide();
$("#main_job_list").show();

});

$(".read").live("click",function(){
	
id=$(this).attr("read_id");	
type=$(this).attr("type");	
table=$(this).attr("table");	
if(type=="1"){ type="0"; }else{ type="1"; }
//alert(type);

$.post(website_url+"/"+"make_read.php",{"id":id,"type":type,"table":table},function(data){

$("#read"+id).attr("type",type);	
if(type=="1"){ $("#read"+id).find('img').attr("src","../img/read.png"); }
else{ $("#read"+id).find('img').attr("src","../img/unread.png"); }
alert("تم التحديث");


});	

});		


$(".read_recipient").live("click",function(){
	
id=$(this).attr("read_recipient_id");	
type=$(this).attr("type");	
table=$(this).attr("table");	
if(type=="1"){ type="0"; }else{ type="1"; }
//alert(type);

$.post(website_url+"/"+"make_read_recipient.php",{"id":id,"type":type,"table":table},function(data){

$("#read_recipient"+id).attr("type",type);	
if(type=="1"){ $("#read_recipient"+id).find('img').attr("src","../img/read.png"); }
else{ $("#read_recipient"+id).find('img').attr("src","../img/unread.png"); }
alert("تم التحديث");


});	

});



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

id=$(this).attr("approve_id");	
type=$(this).attr("type");
table=$(this).attr("table");	
if(type=="1"){ type="0"; }else{ type="1"; }


$.post(website_url+"/"+"make_approve.php",{"id":id,"type":type,"table":table},function(data){

$("#approve"+id).attr("type",type);	
if(type=="1"){ $("#approve"+id).find('img').attr("src","../img/approve_yes.png"); }
else{ $("#approve"+id).find('img').attr("src","../img/approve_no.png"); }
alert("تم التحديث");


});	

});	

//ارسال البريد الالكترونى
				
			
			$("#send_button").click(function(){
			email=$("#send_email").val();
			subject=$("#subject").val();
			message=$("#send_message").val();
		
			$.post(website_url+"/"+"ajax_send_mail.php",{"subject":subject,"message":message,"email":email},function(data){
			
			});
			});	
			
	$("#send_multi_button").click(function(){		

$.ajax({
    type : 'POST',
    url : website_url+"/"+'ajax_send_mail_multi.php',
    data: {
            data: $("#mail_multi").serialize()
          },
    success: function(data){ }
});

});


$('.print_row').live("click",function (event) {
        event.preventDefault();
        window.open($(this).attr("pop"), "طباعة", "width=595, height=842, scrollbars=yes");
		
    });
    
    
    
    
$('.pop').live("click",function (event) {
        event.preventDefault();
        window.open($(this).attr("pop"), "عرض", "width=400, height=842, scrollbars=yes");
		
    });
    
  

 
    
$("#search_field").change(function(){

if($("#search_field option:selected").attr("arr")!="0"){	
   	
   	
   	
//alert($("#search_field option:selected").attr("arr"));

$("#search_text_options").load("options_from_array.php",{

arr:$("#search_field option:selected").attr("arr")

});

$("#search_text").hide();
$("#search_text_options").show();

}else{
$("#search_text").show();	
$("#search_text_options").hide();
$("#search_text_options").html("");
}


});



$(".sub_staff_stage").each(function(index) {
	//alert($(this).html());
	$(".include_list option:contains('"+$(this).html()+"')").remove();
   
});


$(".image_category").change(function(){
	
	$.post(website_url+"/"+"ajax_category_session.php",{

		value:$(this).attr("value")

	},function(data){
		
		//alert(data);
	});
	
	
});





$('.remove_multi').live('click',function(){
    var pic = $(this).attr("value")+"###"; // assume you can get pic's full name
	//alert(pic);
	//alert($(this).parent().parent().parent().find('.multiple_values').val());
    var value = $(this).parent().parent().parent().find('.multiple_values').val();
	//alert(value);
	//var strippedText = $("<div/>").html(value).text();
	//alert(strippedText);
    var index = value.indexOf(pic);
	//alert(index);
    // if there is a comma right after pic string in value
    // add a comma after the string that gonna to be replaced
    // or add a comma behind it
    // var replace = value[index+pic.length] == ',' ? pic + ',' : ',' + pic;
    var replace = value[index+pic.length] == '###' ? pic + '###' : value[index-1] == '###' ? '###' + pic : pic;
    value = value.replace(replace, '');
    //alert(value);
	$(this).parent().parent().parent().find('.multiple_values').val(value);
	$(this).parent().remove();
});


$(".add_multi_block").click(function(){
	//alert("S");
	multi_block_value1=$(this).parent().find(".multi_block_value1").val();
	multi_block_value2=$(this).parent().find(".multi_block_value2").val();
	
	current_multi_value=$(this).parent().parent().find(".multiple_values").val();
	if(multi_block_value1=="" || multi_block_value2==""){alert("أدخل قيمة"); return false;} 
	$(this).parent().parent().find(".multi_values_container").append("<div class='remove_multi_block'>"+"<span class='remove_multi' value='"+multi_block_value1+ "&&&" + multi_block_value2 + "' ></span><span class='edit_multi' ></span><span class='save_multi' ></span><input value='"+multi_block_value2+"' current='"+multi_block_value2+"' disabled class='disabled_input remove_multi_text' /><br><input value='"+multi_block_value1+"' current='"+multi_block_value1+"' disabled class='disabled_input remove_multi_link'></div>");
	//$(this).parent().parent().find(current_multi_value + '###' + multi_block_value1 + " " + multi_block_value2);
	$(this).parent().parent().find(".multiple_values").val(current_multi_value  + multi_block_value1  + "&&&" + multi_block_value2 + '###');
	
	//alert(current_multi_value  + multi_block_value1  + "&&&" + multi_block_value2 + '###');
	$(this).parent().find(".multi_block_value1").val("");
	$(this).parent().find(".multi_block_value2").val("");

});


$(".add_multi_block3").click(function(){
	//alert("S");
	multi_block_value1=$(this).parent().find(".multi_block_value1").val();
	multi_block_value2=$(this).parent().find(".multi_block_value2").val();
	multi_block_value3=$(this).parent().find(".multi_block_value3").val();
	
	current_multi_value=$(this).parent().parent().find(".multiple_values").val();
	if(multi_block_value1=="" || multi_block_value2=="" || multi_block_value3==""){alert("أدخل قيمة"); return false;} 
	$(this).parent().parent().find(".multi_values_container").append("<div class='remove_multi_block'>"+"<span class='remove_multi' value='"+multi_block_value1+ "&&&" + multi_block_value2 + "&&&" + multi_block_value3 + "' ></span><span class='edit_multi' ></span><span class='save_multi3' ></span><input value='"+multi_block_value2+"' current='"+multi_block_value2+"' disabled class='disabled_input remove_multi_text' /><br><input value='"+multi_block_value1+"' current='"+multi_block_value1+"' disabled class='disabled_input remove_multi_link'><br><input value='"+multi_block_value3+"' current='"+multi_block_value3+"' disabled class='disabled_input remove_multi_time'></div>");
	//$(this).parent().parent().find(current_multi_value + '###' + multi_block_value1 + " " + multi_block_value2);
	$(this).parent().parent().find(".multiple_values").val(current_multi_value  + multi_block_value1  + "&&&" + multi_block_value2 + "&&&" + multi_block_value3 + '###');
	
	//alert(current_multi_value  + multi_block_value1  + "&&&" + multi_block_value2 + '###');
	$(this).parent().find(".multi_block_value1").val("");
	$(this).parent().find(".multi_block_value2").val("");
	$(this).parent().find(".multi_block_value3").val("");

});



$(".multi_block_select").change(function(){
	//alert(website_url + "/ar/" + $("option:selected", this).attr("link"));
	//$(this).parent().find(".multi_block_value1").val(website_url + "/ar/course/" + $("option:selected", this).attr("link") + "/" + $("option:selected", this).val());
	$(this).parent().find(".multi_block_value1").val($("option:selected", this).attr("link"));
	
});



$(".edit_multi").live("click",function(){
	
	$(this).parent().find("input").removeAttr("disabled");
	$(this).hide();
	$(this).parent().find(".save_multi").show();
	$(this).parent().find(".save_multi3").show();
	//alert("sa");

});


$(".save_multi").live("click",function(){
	
	$(this).parent().find("input").attr("disabled","disabled");
	$(".save_multi").hide();
	$(this).parent().find(".edit_multi").show();
	
	var string =$(this).parent().parent().parent().find(".multiple_values").val();
	//alert(string);
	current_string=$(this).parent().find(".remove_multi_link").attr("current") + "&&&" + $(this).parent().find(".remove_multi_text").attr("current") ;
	//alert(current_string);
	new_string=$(this).parent().find(".remove_multi_link").val() + "&&&" + $(this).parent().find(".remove_multi_text").val() ;
	//alert(new_string);	
	var replaced_string = string.replace(current_string,new_string);
	//alert(replaced_string);
	$(this).parent().find(".remove_multi").attr("value",new_string);
	$(this).parent().find(".remove_multi_link").attr("current",$(this).parent().find(".remove_multi_link").val());
	$(this).parent().find(".remove_multi_text").attr("current",$(this).parent().find(".remove_multi_text").val());	
	$(this).parent().parent().parent().find(".multiple_values").val(replaced_string);


});



$(".save_multi3").live("click",function(){
	
	$(this).parent().find("input").attr("disabled","disabled");
	$(".save_multi3").hide();
	$(this).parent().find(".edit_multi").show();
	
	var string =$(this).parent().parent().parent().find(".multiple_values").val();
	//alert(string);
	current_string=$(this).parent().find(".remove_multi_link").attr("current") + "&&&" + $(this).parent().find(".remove_multi_text").attr("current") + "&&&" + $(this).parent().find(".remove_multi_time").attr("current") ;
	//alert(current_string);
	new_string=$(this).parent().find(".remove_multi_link").val() + "&&&" + $(this).parent().find(".remove_multi_text").val() + "&&&" + $(this).parent().find(".remove_multi_time").val() ;
	//alert(new_string);	
	var replaced_string = string.replace(current_string,new_string);
	//alert(replaced_string);
	$(this).parent().find(".remove_multi").attr("value",new_string);
	$(this).parent().find(".remove_multi_link").attr("current",$(this).parent().find(".remove_multi_link").val());
	$(this).parent().find(".remove_multi_text").attr("current",$(this).parent().find(".remove_multi_text").val());
	$(this).parent().find(".remove_multi_time").attr("current",$(this).parent().find(".remove_multi_time").val());	
	$(this).parent().parent().parent().find(".multiple_values").val(replaced_string);


});




$("#adv_search").click(function(){
	if($(this).attr("show")==0){
		
		$("#adv_div").show();
		$(this).attr("show",'1');
	
	}else{

		$("#adv_div").hide();
		$(this).attr("show",'0');
		
	}
});



	
	

$("#rerefuse_payment_update").click(function(){
	//alert($("#bill_id").html());
	//alert($("#payment_status").val());
	$.ajax({
		type: 'POST',
		url: website_url+"/"+"ajax_update_rerefuse_status.php",
		data:{
			"id":$(this).attr("num"),
			"payment_number":$(this).attr("payment"),
			"order_id":$(this).attr("order_id")
			}
	}).done(function( data ) {
		//alert(data);
		if(data=='done'){
			alert("تم التعديل");
		}else{
			alert("لم يتم التعديل");
		}
		
		});
	
});




$("#payment_status_update").click(function(){
	//alert($("#bill_id").html());
	//alert($("#payment_status").val());
	$.ajax({
		type: 'POST',
		url: website_url+"/"+"ajax_update_payment_status.php",
		data:{
			"bill_id":$("#bill_id").html(),
			"payment_status":$("#payment_status").val()
			}
	}).done(function( data ) {
		//alert(data);
		if(data=='done'){
			alert("تم التعديل");
		}else{
			alert("لم يتم التعديل");
		}
		
		});
	
});


$(".product_status_update").click(function(){
	//alert("S");
	str='';	
	
	$(".product_status_value").each( function(){
		str +='/'+$(this).val();
	});
  
	
	
	
	$.ajax({
		type: 'POST',
		url: website_url+"/"+"ajax_update_product_status.php",
		data:{
			"id":$(this).attr("order_id"),
			"invoice_id":$(this).attr("invoice_id"),
			"product_status":$(this).prev(".product_status_value").val(),
			"product_status_all":str,
			}
	}).done(function( data ) {
		//alert(data);
		if(data=='done'){
			alert("تم التعديل");
		}else{
			alert("لم يتم التعديل");
		}
		
	});
		
		
	
	//alert(str);
	
});





$('.remove_single').live('click',function(){
    var pic = $(this).attr("value"); // assume you can get pic's full name
	
	//alert($(this).parent().parent().parent().find('.singleple_values').val());
    var value = $(this).parent().parent().parent().find('.singleple_values').val();
    var index = value.indexOf(pic);

    // if there is a comma right after pic string in value
    // add a comma after the string that gonna to be replaced
    // or add a comma behind it
    // var replace = value[index+pic.length] == ',' ? pic + ',' : ',' + pic;
    var replace = value[index+pic.length] == ',' ? pic + ',' : value[index-1] == ',' ? ',' + pic : pic;
    value = value.replace(replace, '');
   // console.log(value);
	$(this).parent().parent().parent().find('.singleple_values').val(value);
	$(this).parent().remove();
});


$(".add_single_block").click(function(){
	
	single_block_value=$(this).next(".single_block_value").val();
	current_single_value=$(this).parent().parent().find(".singleple_values").val();
	if(single_block_value==""){alert("أدخل قيمة"); return false;}
	$(this).parent().parent().find(".single_values_container").append("<div class='remove_single_block'>"+single_block_value+"<span class='remove_single' value='"+single_block_value+"' ></span></div>");
	$(this).parent().parent().find(current_single_value + ',' + single_block_value);
	$(this).parent().parent().find(".singleple_values").val(current_single_value + ',' + single_block_value);

	
	$(this).next(".single_block_value").val("");

});




$(".page_menu_change").live("change",function(){

		$.ajax({
			type: 'POST',
			url: website_url+"/cp/"+"ajax_page_menu_change.php",
			data:{
				"id":$(this).attr("id"),
				"menu":$(this).val()
				
				}
		}).done(function( data ) {
			//alert(data);
			if(data=='done'){
				alert("تم التعديل");
				
				$("#main_menu_list").change();
			}else{
				alert("لم يتم التعديل");
			}
			
		});

});


$("#main_menu_list").change(function(){
		$("#load_wait").show();
		$.ajax({
			type: 'POST',
			url: website_url+"/cp/"+"ajax_load_menus.php",
			data:{
				
				"menu":$(this).val()
				
				}
		}).done(function( data ) {
			//alert(data);
			if(data=='not_done'){
				alert("لم يتم التعديل");
				$("#load_wait").hide();
			}else{
				$("#load_wait").hide();
				$("#result_view").html(data);
			}
			
		});

});



 $(".show_details").on('click', function(e) {
	e.stopPropagation(); // لمنع وصول الحدث لباقي الصفحة
    $('#show_user_details')
      .css({
        top:  (eval(e.pageY)-412) + 'px',
        left:  '0px',
        display: 'block'
      });
	  
	  details_data="<table>";
	  details_data+="<tr><td>مستخدم الاضافة :</td><td>"+$(this).attr("user_created")+"</td></tr>";
	  details_data+="<tr><td>تاريخ الاضافة :</td><td>"+$(this).attr("created")+"</td></tr>";
	  details_data+="<tr><td>مستخدم التعديل :</td><td>"+$(this).attr("user_modified")+"</td></tr>";
	  details_data+="<tr><td>تاريخ التعديل :</td><td>"+$(this).attr("modified")+"</td></tr>";
	  details_data+="</table>";
	  
	 $("#show_user_details").html(details_data);

  });
  
   $("body").click(function(){
		$('#show_user_details').hide();  
		  
	  });


$(".poll_download").click(function () {
        const tableHtml = $("#result_view")[0].outerHTML;

        // إنشاء نموذج مخفي لإرسال البيانات إلى PHP
        const form = $('<form>', {
            method: 'POST',
            action: 'ajax_poll_download.php',
            target: '_blank' // فتح الاستجابة في نافذة جديدة (لظهور نافذة الحفظ)
        });

        const input = $('<input>', {
            type: 'hidden',
            name: 'table_html',
            value: tableHtml
        });

        form.append(input);
        $('body').append(form);
        form.submit();
        form.remove();
    });
	





});