
//-------------- Page Management Functions ------------------------------------------
function navigate(location)
{
    window.location='index.php?fa='+location;

}

function submit_form()
{
    document.getElementById("lang_id").value = document.getElementById("page_language").value;
    document.frm2.submit();
}

function ShowChild(page_id,parent)
{  

    if(document.getElementById("arrow_shape_"+page_id).value=="0")
    {
        jQuery.facebox("<img src='../scripts/facebox/loading.gif'/>Loading....");
        
        $.ajax({            
                type: "GET",
                url:"views/ajax/showChildren.php?page_id=" + page_id,
                success: function(html)
                {
                        document.getElementById("icon_arrow_"+page_id).src = "images/down_arrow.png";
                        $('#'+page_id+'').after(html);                          
                        document.getElementById("arrow_shape_"+page_id).value="1";
                        
                        jQuery.facebox.close();
                }
        });
    }
    else
    {   
        //alert(page_id);
        document.getElementById("icon_arrow_"+page_id).src = "images/right_arrow.png";
        jQuery.facebox("<img src='../scripts/facebox/loading.gif'/>Loading....");
        $.ajax({
                type: "POST",
                url:"views/ajax/showChildren.php?action=get_children&id=" + page_id,
                success: function(html)
                {
                    
                    var op = html.split(",");
                    //alert(op.toString());
                    for(var i=0;i<op.length;i++)
                    {
                        //alert(op[i]);
                        if(document.getElementById(op[i]) != null)
                            $('#'+op[i]).empty().remove()
                    }
                    //alert(op.length);
                    //if(op.length==1)
//                    {
//                        op.length = 0;
//                    }
                    
                    jQuery.facebox.close();
                }
       });
       
       //if(op.length==1)
//       {
//            op.length = 0;
//       }

        //$('#'+page_id+'_').remove();        
        document.getElementById("arrow_shape_"+page_id).value="0";
        //document.getElementById("child_list_"+page_id).style.display="none";

    }
    
    return;
}
 
//------------ Show Child 2 ------------------------------ 
function ShowChild2(page_id,parent)
{   
    $.ajax({
            type: "POST",
            url:"views/ajax/showChildren.php?page_id=" + page_id,
            success: function(html)
            {
                    document.getElementById("icon_arrow_"+page_id).src = "images/down_arrow.png";
                    $('#'+page_id+'').after(html);                          
                    document.getElementById("arrow_shape_"+page_id).value="1";
            }
    });
    
}
//------------------------------


function deletePage(page_id,message)
{
    if(confirm(message))
    {
        window.location = "index.php?fa=page_management&action=deletepage&id="+page_id;
    }
}

 function createPage(page_id)
 {
    window.location = "index.php?fa=page_management&action=new&parent="+page_id;
 }

 function showContent()
 {
     if(!document.getElementById("redirect").checked)
         document.getElementById("text_editor").style.display='table-row';
     else
         document.getElementById("text_editor").style.display='none';
 }

 function deleteFile(page_id,path,msg1,msg2,msg3,msg4)
 {
     if(confirm(msg1 + path + msg2))
     {
         $.ajax({
                type: "POST",
                url:"views/ajax/deleteUserFile.php?fb=1",
                data:"page_id=" + page_id + "&path=" + path,
                success: function(html){
                    if(html == "1")
                        jQuery.facebox(msg3);
                    else
                        jQuery.facebox(msg4);

                    window.location.reload();
                }
        });
     }
 }

 function showThumb(src)
 {
     jQuery.facebox("<img src="+src+" />");
 }

 
function changePageStatus(page_id,status,msg_confirm,msg_ok)
{
    if(confirm(msg_confirm))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=page_management&action=change_status&fb=1",
            data:"page_id=" + page_id + "&status=" + status ,
            success: function(html){
                alert(msg_ok);
                window.location.reload();
            }
    });
    }
}

function changeInDDL(page_id,status,msg_confirm,msg_ok)
{

    if(confirm(msg_confirm))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=page_management&action=changeInDDL&fb=1",
            data:"page_id=" + page_id + "&status=" + status ,
            success: function(html){
                alert(msg_ok);
                window.location.reload();
            }
    });
    }
}

function clonePage(page_id,msg_confirm)
{
    if(confirm(msg_confirm))
        window.location="index.php?fa=page_management&action=clone&id="+page_id+"";
}

//--------------- Category Management Functions -------------------------------------------------

//---------------- Box Management Functions ----------------------------------------------------
function deleteBox(box_id,path,msg_confirm,msg_ok)
{
 if(confirm(msg_confirm))
 {
     $.ajax({
            type: "POST",
            url:"index.php?fa=box_management&action=delete&fb=1",
            data:"box_id=" + box_id + "&path=" + path,
            success: function(html){
                alert(msg_ok);
                window.location.reload();
            }
    });
 }
}

function changeBoxStatus(box_id,status,msg_confirm,msg_ok)
{
    if(confirm(msg_confirm))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=box_management&action=change_status&fb=1",
            data:"box_id=" + box_id + "&status=" + status ,
            success: function(html){
                alert(msg_ok);
                window.location.reload();
            }
    });
    }
}

function processList()
{
    var output="";
    for(var i=0;i<document.getElementById("max_value").value;i++)
        {
            if(document.getElementById("checkbox_"+i).checked)
                output += document.getElementById("page_id_"+i).value+",";
        }
    document.getElementById("page_list").value = output;
    document.frmManagePage.submit();
    //alert(document.getElementById("page_list").value);
}



function deleteUser(user_id, message)
{
    if(confirm(message))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=user_management&action=delete&fb=1",
            data:"user_id=" + user_id ,
            success: function(html){
                window.location.reload();
            }
    });
    }
}

function validateUser(please_new_pwd,please_confirm_pwd)
{
    if(document.getElementById("newpasswd").value=="")
    {
        jQuery.facebox(please_new_pwd);please_new_pwd
        document.getElementById("newpasswd").focus();
        return;
    }

    if(document.getElementById("confirmpasswd").value=="")
    {
        jQuery.facebox(please_confirm_pwd);
        document.getElementById("confirmpasswd").focus();
        return;
    }

    if(document.getElementById("newpasswd").value != document.getElementById("confirmpasswd").value)
    {
        jQuery.facebox(please_confirm_pwd);
        document.getElementById("confirmpasswd").focus();
        return;
    }

    document.fromCHangePasswd.submit();
}


//------------ Visitor Management -------------------------------

function changeUserStatus(user_id,status,message)
{
    if(confirm(message))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=user_management&action=change_status&fb=1",
            data:"user_id=" + user_id + "&status=" + status ,
            success: function(html){
                window.location.reload();
            }
    });
    }
}

function viewMoreVisitorDetails(id)
{
    $.ajax({
        type: "POST",
        url:"index.php?fa=visitors_management&action=more&fb=1",
        data:"id=" + id ,
        success: function(html){
            jQuery.facebox(html);
        }
});

}

function changePassword(id,msg)
{
    if(confirm(msg+"?"))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=visitors_management&action=reset_password&fb=1",
            data:"id=" + id ,
            success: function(html){
                jQuery.facebox(html);
            }
        });
    }

}

function enablePassword()
{
    if(document.getElementById("enablepasswd").checked)
    {
        document.getElementById("password").disabled=false;
        document.getElementById("password").focus();
    }
    else
        document.getElementById("password").disabled=true;

}

function validatePref(please_new_pwd)
{
    if(document.getElementById("enablepasswd").checked)
    {
        if(document.getElementById("password").value=="")
        {
            alert(please_new_pwd);
            document.getElementById("password").focus();
        }
        else
            document.frmPref.submit();
    }
    else
        document.frmPref.submit();

}

/*function changePage(path)
{
    index_val = document.getElementById("page").selectedIndex
    page_id = document.getElementById("page").options[index_val].id
    window.location = path+"/"+page_id+".htm";
}*/
 function changePage(path)
{
    index_val = document.getElementById("page").selectedIndex
    page_id = document.getElementById("page").options[index_val].id
    window.location = path+"/"+page_id+".htm";
}

function subForm(fName, arNames, arData,goTo,e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
    if(unicode == 13){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
	}
}

function subForm2(fName, arNames, arData,goTo,e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
    if(unicode == 13){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
	}
}

function subFormRef(fName, arNames, arData,goTo,e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
    if(unicode == 13){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
	}
}

function deleteAlbum(id,msg)
{
    if(confirm(msg))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=gallery_management&action=delete_album&fb=1",
            data:"id=" + id ,
            success: function(html){
                window.location.reload();
            }
    });
    }
}

function changeLayout(page_id,layout_id,message)
{
	$.ajax({
		type: "POST",
		url:"index.php?fa=page_management&action=changeLayout&fb=1",
		data:"page_id=" + page_id + "&layout_id=" + layout_id ,
		success: function(html){
			alert(message);
			window.location.reload();
		}
   	});
}

function deleteVisitor(user_id,msg)
{
    if(confirm(msg))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=visitors_management&action=delete&fb=1",
            data:"user_id=" + user_id ,
            success: function(html){
                window.location.reload();
            }
        });
    }
}

function showPanel()
{
    var html='';
    var val = document.getElementById("option_list").value;
    var count = document.getElementById("total").value;
    
    if(val==1)
    {
        
            var op = "";
            for(var i=0;i<count;i++)
            {
                if(document.getElementById("count_"+i).checked)
                {
                    op +=document.getElementById("count_"+i).name + ","
                }
            }
            
            $.ajax({
                type: "POST",
                url:"index.php?fa=visitors_management&action=delete_list&fb=1",
                data:"user_ids=" + op ,
                success: function(html){
                    window.location.reload();
                }
            });
    }
    
    if(val==2)
    {
        html = "<input type='text' name='mail_list' id='mail_list' value='' /><br><br><input type='button' onclick='savePanel()' value='Enregistrer' />";
        document.getElementById("contents").innerHTML = html;
    }
    
    if(val==3)
    {
        html = "<select id='status_options'><option value='0'>Hors ligne</option><option value='1'>En ligne</option></select><br><br><input type='button'  onclick='savePanel()' value='Enregistrer' />";
        document.getElementById("contents").innerHTML = html;
    }
    
    if(val==4)
    {
        var op = "";
        for(var i=0;i<count;i++)
        {
            if(document.getElementById("count_"+i).checked)
            {
                op +=document.getElementById("count_"+i).name + ","
            }
        }
        
        $.ajax({
            type: "POST",
            url:"index.php?fa=visitors_management&action=flag&fb=1",
            data:"user_ids=" + op ,
            success: function(html){
                window.location.reload();
            }
            });
    }
    
    if(val==5)
    {
        var op = "";
        for(var i=0;i<count;i++)
        {
            if(document.getElementById("count_"+i).checked)
            {
                op +=document.getElementById("count_"+i).name + ","
            }
        }
        
        $.ajax({
            type: "POST",
            url:"index.php?fa=visitors_management&action=flag&fb=0",
            data:"user_ids=" + op ,
            success: function(html){
                window.location.reload();
            }
            });
    }
}

function savePanel()
{
    var val = document.getElementById("option_list").value;
    var count = document.getElementById("total").value;    
    var op="";
    
    if(val==3)
    {
        for(var i=0;i<count;i++)
        {
            if(document.getElementById("count_"+i).checked)
            {
                op +=document.getElementById("count_"+i).name + ","
            }
        }
            
        $.ajax({
            type: "POST",
            url:"index.php?fa=visitors_management&action=define&fb="+document.getElementById("status_options").value,
            data:"user_ids=" + op ,
            success: function(html){
                alert("Data saved Successfully!");
                window.location.reload();
            }
            });
    }
    
    if(val==2)
    {
        for(var i=0;i<count;i++)
        {
            if(document.getElementById("count_"+i).checked)
            {
                op +=document.getElementById("count_"+i).alt + ","
            }
        }
            
        $.ajax({
            type: "POST",
            url:"index.php?fa=visitors_management&action=save_mail_list",
            data:"emails=" + op + "&list_name="+document.getElementById("mail_list").value ,
            success: function(html){
                alert("Data saved Successfully!");
                window.location.reload();
            }
            });
    }
    
}

function subFormElement(fName, arNames, arData,goTo,e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
    if(unicode == 13){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
	}
}

function subFormChart(fName, arNames, arData,goTo,e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
    if(unicode == 13){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
	}
}

function showImage(path)
{
    jQuery.facebox("<img src='../charts/chart_images/"+path+"'>");
}

function setType(id)
{
    var op = "";
    
    $.ajax({
            type: "POST",
            url:"views/ajax/generateHTML.php?action=new",
            success: function(html){
                op += "<fieldset>"+
                "<center>"+
                "<table width='80' class='xls2'>"+
                "<tr height='50'><td width='50px'>Type&nbsp;&nbsp;</td><td><select id='type'>"+
                    "<option value='1'>Type1</option>"+
                    "<option value='2'>Type2</option>"+
                "</select></td>"+
                "</tr>"+
                html+
                "<tr><td><input type='button' onclick='saveType("+id+")' value='OK'</td></tr>"+
                "</table>";
                
                if(document.getElementById(id).checked==true)
                {
                    jQuery.facebox(op);
                }
            }
    });
    
}

function saveType(id)
{
    document.getElementById("lbl_"+id).innerHTML = "Type("+document.getElementById("type").value+")";
    document.getElementById("type_"+id).value = document.getElementById("type").value;
    
    var lang_array = document.getElementById("var_list").value.split(",");
    lang_array.pop();
    var langs = "";
    
    for(var i=0;i<lang_array.length;i++)
    {
       langs += lang_array[i]+"="+document.getElementById("label_"+lang_array[i]).value+"/";
    }
    
    document.getElementById("labels_"+id).value = langs;
    document.getElementById(id).checked=true;
    jQuery.facebox.close();    
}

function saveLinks()
{
    var total = document.getElementById("total_count").value
    var val_array = new Array();
    
    for(var i=0;i<total;i++)
    {
       if(document.getElementById(i).checked==true) 
       {
            val_array.push(document.getElementById("id_"+i).value+"-"+document.getElementById("type_"+i).value + "-" + document.getElementById("labels_"+i).value)
       }
    }
    
    document.getElementById("final_array").value = val_array.toString();
    
    var total_ex = parseInt(document.getElementById("total_count_ex").value)
    var del_array = new Array();
    
    var j=parseInt(total);
    
    while(j<total_ex)
    {
        if(document.getElementById(j).checked==false)
            del_array.push(document.getElementById("id_"+j).value);
            
           j++; 
    }
    
    document.getElementById("del_array").value = del_array.toString();
    document.form1.submit();
   
}

function saveExtLinks()
{
    var total = document.getElementById("total_count_ex2").value
    var val_array = new Array();
    
    for(var i=0;i<total;i++)
    {        
        val_array.push(document.getElementById("lang_key_"+i).value+"-"+document.getElementById("lang_val_"+i).value)
    }
    
    document.getElementById("final_array_ex").value = val_array.toString();
    document.frmStatus.submit();
   
}


function setType2(fk_id)
{
    var html = "<fieldset>"+
                "<center>"+
                "<table width='80' class='xls2'>"+
                "<tr height='50'><td width='50px'>Type&nbsp;&nbsp;</td><td><select id='type'>"+
                    "<option value='1'>Type1</option>"+
                    "<option value='2'>Type2</option>"+
                "</select></td>"+
                "</tr>"+
                "<tr><td><input type='button' onclick='saveType2("+fk_id+")' value='OK'</td></tr>"+
                "</table>";
                
    //if(document.getElementById(id).checked==true)
    {
        jQuery.facebox(html);
    }
}

function saveType2(id)
{
    $.ajax({
            type: "POST",
            url:"index.php?fa=page_management&action=save_type",
            data:"id=" + id + "&type="+document.getElementById("type").value,
            success: function(html){
                jQuery.facebox('Saved!');
                
            }
    });
}

function subFormBanner(fName, arNames, arData,goTo,e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
    if(unicode == 13){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
	}
}

function deleteBanner(banner_id,path,msg_confirm,msg_ok)
{
 if(confirm(msg_confirm))
 {
     $.ajax({
            type: "POST",
            url:"index.php?fa=sponser_banner_management&action=delete&fb=1",
            data:"banner_id=" + banner_id + "&path=" + path,
            success: function(html){
                alert(msg_ok);
                window.location.reload();
            }
    });
 }
}

function changeSHLabels(id)
{
    jQuery.facebox("Loading...");
    $.ajax({
            type: "POST",
            url:"views/ajax/generateHTML.php?action=edit",
            data:"id=" + id,
            success: function(html){
                jQuery.facebox(html);
            }
    });
}

function saveEditedLabels(id)
{
    var lang_list = document.getElementById("var_list").value.split(",");
    lang_list.pop();
    var type = document.getElementById("type").options[document.getElementById("type").selectedIndex].value;
    
    var params = "";
    
    for(var i=0;i<lang_list.length;i++)
    {
        params +=  lang_list[i] + "=" + document.getElementById("label_"+lang_list[i]).value+","
    }
    
    jQuery.facebox("Loading...");
    $.ajax({
            type: "POST",
            url:"index.php?fa=page_management&action=save_edited_shortcut",
            data:"id=" + id + "&type=" + type+"&params="+params,
            success: function(html){                
                window.location.reload();
            }
    });
}

function showPagesList(lang)
{
	jQuery.facebox("Loading...");
	$.ajax({
		type: "POST",
		url:"views/ajax/show_page_list.php?listlang="+lang,
		success: function(html)
		{
			$(document).bind('beforeReveal.facebox', function() {
				$('#facebox .body').width('800px'); 
			});
			jQuery.facebox(html);
		}
	});
}

function subFormFileCat(fName, arNames, arData,goTo,e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
    if(unicode == 13){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
	}
}

function Paginator(page)
{
    var search_string="";
    
    if(document.getElementById("search_text").value!="")
        search_string=document.getElementById("search_text").value;
    
    jQuery.facebox("Loading...");
     $.ajax({
                type: "POST",
                url:"views/ajax/ajax_visitors.php?action=show_page&page="+page,
                data:"search_text="+search_string,
                success: function(html)
                {
                    document.getElementById("visitor_list").innerHTML=html;
                    document.getElementById("search_text").value=search_string
                    jQuery.facebox.close();
                }
        });
}

function searchVisitors()
{
    var old_key = document.getElementById("search_text").value;
    
    jQuery.facebox("Loading...");
     $.ajax({
                type: "POST",
                url:"views/ajax/ajax_visitors.php?action=search",
                data:"key="+document.getElementById("search_text").value,
                success: function(html)
                {
                    document.getElementById("visitor_list").innerHTML=html;
                    document.getElementById("search_text").value=old_key;
                    jQuery.facebox.close();
                }
        });
}

function deleteLang(lang,msg_confirm,msg_ok)
{
    if(confirm(msg_confirm))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=lang_management&action=delete&fb=1",
            data:"lang=" + lang ,
            success: function(html){
                alert(msg_ok);
                window.location.reload();
            }
        });
    }
}

function changeNewsletterFlag(user_id,status,flag_newsletter,unflag_newsletter)
{
    var message="";
    if(status==0)
        message = flag_newsletter;
    else
        message = unflag_newsletter;

    if(confirm(message))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=visitors_management&action=flag_single&fb=1",
            data:"user_id=" + user_id + "&status=" + status ,
            success: function(html){
                window.location.reload();
            }
    });
    }
}

function subFormImg(fName, arNames, arData,goTo,e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
    if(unicode == 13){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
	}
}

function subFormClick(fName, arNames, arData,goTo,e){
		for(i=0;i<arNames.length;i++){
			var newInput = document.createElement('div');
			newInput.innerHTML = "<input type='hidden' name='"+arNames[i]+"' value='"+arData[i]+"'>";
			document.getElementById(fName).appendChild(newInput);
		}

		document.forms[fName].action=goTo;
		document.forms[fName].submit();
}

function deleteGalleryImage(page_id,image_id)
{
        $.ajax({
            type: "POST",
            url:"index.php?fa=page_management&action=delete_gallery_image",
            data:"page_id=" + page_id + "&image=" + image_id,
            success: function(html){
                if(html.search('DONE')!=-1)
                    {
                        window.location.reload();
                    }
            }
        });
}

/*function deletePageBanner(page_id,msg)
{
    if(confirm(msg))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=page_management&action=delete_banner",
            data:"page_id=" + page_id,
            success: function(html){
                if(html.search('DONE')!=-1)
                    {
                        window.location.reload();
                    }
            }
        });
    }
}*/

function deletePageBanner(ban_id,msg)
{
    if(confirm(msg))
    {
        $.ajax({
            type: "POST",
            url:"index.php?fa=page_management&action=delete_banner",
            data:"ban_id=" + ban_id,
            success: function(html){
                if(html.search('DONE')!=-1)
                    {
                        window.location.reload();
                    }
            }
        });
    }
}


function changeFrameStatus(box_id,status)
{
	$.ajax({
		type: "POST",
		url:"index.php?fa=box_management&action=change_frame&fb=1",
		data:"box_id=" + box_id + "&status=" + status ,
		success: function(html){
			window.location.reload();
		}
	});
}
