﻿// JScript File
/******************************************************
********* Users JS SDK (MOPA) ****************
********* Created By: Mohamed Farouk (11 June 2007) *
********* Last Modified: 23 11 June 2007 ***************
******************************************************/
/// Globals //////
//Alias to document.getElementById
//alert()
function ret(oid)
{
    return document.getElementById(oid);    
}//ret

function nothing(){}

//Constants

//Global Variables
var PagesCount = 0;
var PageEditing = false;
var FCKeditorInstance = null;
var CurrentTab = -1;
var ImagesPath = "/Web/App_Themes/Themes/images/";

function xmlCls()
{
 this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 this.curCase = 0;
 this.curId = 0;
}//object
var xmlObj = null;

function RenderTabControl(divId)
{
    var _html = "<table width='100%' cellpadding='1' cellspacing='1'><tr><td height='5' colspan='3'></td></tr><tr><td width='40' class='TabBox'>"+
                '<img border="0" style="cursor:hand;" width="20" height="20" src="'+ImagesPath+'new.gif" onclick="AddNewPage()" type="button" title="أضف صفحة جديدة" /> '+
                "</td><td class='TabBox' width='80'>"+
                "<img border='0' style='cursor:hand;' width='16' height='16' src='"+ImagesPath+"edit2.gif' id='Button_Content_Edit_"+PagesCount+"' type='button' onclick='EditPage("+PagesCount+")' title='تعديل محتوى الصفحة'/>"+
                "</td><td width='90%' class='TabBox'>&nbsp;</td></tr></table>"+
                "<span id='hiddenContols'></span><table width='100%' cellpadding='0' cellspacing='0'><tr><td height='5'></td></tr><tr><td><div id='TabsDiv'></div></td></tr>"+
                "<tr><td class='TabContentTD' valign='top' id='DivsTD' style='height:100;'></td></tr></table>"+
                "<table width='100%' cellpadding='0' cellspacing='0'><tr><td style='display:none' id='ContentEditor'>"+
                "<table width='100%' cellpadding='1' cellspacing='1'><tr><td height='5' colspan='3'></td></tr><tr><td width='40' class='TabBox'>"+
                "<img border='0' style='cursor:hand;' width='20' height='20' src='"+ImagesPath+"save.gif' onclick='SavePage();' title='حفظ الصفحة'/>"+
                "</td><td width='100%' class='TabBox'>&nbsp;</td></tr></table>"+
                GetEditorHTML(0,"");
                //"<input type='button' onclick='SavePage()' value='Save'/></td></tr></table>";
    ret(divId).innerHTML = _html;
}//RenderTabControl

function LoadPages(forEdit)
{
    if(_xmlPath.indexOf(".xml")==-1)
        return;
    xmlObj = new xmlCls();
    var url = _xmlPath;
    var _action = forEdit?RenderPages:RenderViewPages;
	xmlObj.xmlDoc.onreadystatechange = _action;
	xmlObj.xmlDoc.load(url+"?rand="+Math.random());
}//LoadPagesForEdit

//////////////// Start End User View Mode //////////////////////////
var ViewPagesContents = new Array();
var ViewCurrentPage = 0;
var ViewTotalPages = 0;
function RenderViewPages()
{
	if(xmlObj.xmlDoc.readyState==4 || xmlObj.xmlDoc.readyState=="COMPLETED")
	{
		if(xmlObj.xmlDoc.parseError.errorCode == 0)
		{
            var _html = "";
            
            
			var nodeList = xmlObj.xmlDoc.getElementsByTagName("Page");
			for(j=0;j<nodeList.length;j++)
			{
				var _node = nodeList[j];
				//AddNewPage(_node.firstChild.nodeValue);
				ViewPagesContents[ViewPagesContents.length] = _node.firstChild.nodeValue;
				ViewTotalPages++;
			}//for
			if(nodeList.length>0)
			    RenderViewPage(false,0);
		}
		else
		{
			alert("Error While Loading XML");
		}
	}//if
}//displayResults
function RenderViewPage(isNext,pageIndex)
{
    if(pageIndex==null)
        ViewCurrentPage=isNext?ViewCurrentPage+1:ViewCurrentPage-1;
    else
        ViewCurrentPage=pageIndex;
    ret('PagingContentDiv').innerHTML = ViewPagesContents[ViewCurrentPage];
    //Handling Previuos
    if(ViewCurrentPage==0)//firstPage
    {
        ret('ViewPrev').src = "/Web/App_Themes/Themes/images/control_previous_disable.gif";
        ret('ViewPrev').disabled = true;
        ret('ViewPrev').style.cursor = "default";
    }//if
    else
    {
        ret('ViewPrev').src = "/Web/App_Themes/Themes/images/control_previous.gif";
        ret('ViewPrev').disabled = false;
        ret('ViewPrev').style.cursor = "hand";
    }//else
    
    //Handling Next
    if(ViewCurrentPage==ViewPagesContents.length-1)//last Page
    {
        ret('ViewNext').src = "/Web/App_Themes/Themes/images/control_next_disable.gif";
        ret('ViewNext').disabled = true;
        ret('ViewNext').style.cursor = "default";
    }//if
    else
    {
        ret('ViewNext').src = "/Web/App_Themes/Themes/images/control_next.gif";
        ret('ViewNext').disabled = false;
        ret('ViewNext').style.cursor = "hand";
    }//else
}//RenderViewPage
//////////////// End End User View Mode //////////////////////////

function RenderPages()
{
	if(xmlObj.xmlDoc.readyState==4 || xmlObj.xmlDoc.readyState=="COMPLETED")
	{
		if(xmlObj.xmlDoc.parseError.errorCode == 0)
		{
            var _html = "";
            
            
			var nodeList = xmlObj.xmlDoc.getElementsByTagName("Page");
			for(j=0;j<nodeList.length;j++)
			{
				var _node = nodeList[j];
				AddNewPage(_node.firstChild.nodeValue);
			}//for
			if(nodeList.length>0)
			    ChangeTab(1);
		}
		else
		{
			alert("Error While Loading XML");
		}
	}//if
}//displayResults

function ChangeTab(tabId)
{
    ret('TD_Tab_'+CurrentTab).className = "TabNormal";
    ret('Div_Page_Content_'+CurrentTab).style.display = "none";
    CurrentTab = tabId;
    ret('TD_Tab_'+CurrentTab).className = "TabSelected";
    ret('Div_Page_Content_'+CurrentTab).style.display = "inline";
    if(window.FCKeditorAPI)
    {
        var _fck = FCKeditorAPI.GetInstance('FCKeditor_g');
        if(_fck && _fck.EditorDocument && _fck.EditorDocument.body)
            _fck.EditorDocument.body.innerHTML = ret('Div_Page_Content_'+tabId).innerHTML;
    }//if
}//ChangeTab

function GetEditorHTML(pageId,value)
{
    var sBasePath = '/fckeditor/';

	if(!FCKeditorInstance)
	    FCKeditorInstance = new FCKeditor( 'FCKeditor_g' );
	FCKeditorInstance.BasePath	= sBasePath ;
	value = value?value:"";
	FCKeditorInstance.Value = value;
	FCKeditorInstance.Height = '300';
	FCKeditorInstance.HTMLRendered = FCKeditorInstance.CreateHtml();
	return FCKeditorInstance.HTMLRendered;
}//GetEditorHTML

function AddNewPage(html)
{
    html = html?html:"";
    PagesCount++;
    var _html = "<table width='100%' cellpadding='0' cellspacing='0'><tr align='center'>";
    for(i=0;i<PagesCount;i++)
    {
        var _class = PagesCount==(i+1)?"TabSelected":"TabNormal";
        _html += "<td width='30' id='TD_Tab_"+(i+1)+"' onclick='ChangeTab("+(i+1)+")' nowrap class='"+_class+"'>"+(i+1)+"</td><td width='1' class='TabEmpty'>&nbsp;</td>";
        if(ret('Div_Page_Content_'+(i+1)))
           ret('Div_Page_Content_'+(i+1)).style.display = 'none'; 
    }//for
    _html += "<td width='100%' class='TabEmpty'>&nbsp;</td></tr></table>";
    ret('TabsDiv').innerHTML = _html;
    ret('DivsTD').innerHTML += "<div id='Div_Page_Content_"+PagesCount+"'></div>";
    ret('hiddenContols').innerHTML += "<input type='hidden' name='Page_Content_Value_"+PagesCount+"'>";
    ret('Div_Page_Content_'+PagesCount).innerHTML = html+"&nbsp;";
    ret('Page_Content_Value_'+PagesCount).value = html;
    CurrentTab = PagesCount;
    ChangeTab(CurrentTab);
}//AddNewPage

function SavePage()
{
    var _html = "";
    var _fck = FCKeditorAPI.GetInstance('FCKeditor_g');
    var _content = _fck.EditorDocument.body.innerHTML;
    ret('savemode').value = "Yes";
    ret('Page_Content_Value_'+CurrentTab).value=_content;
    ret('ContentEditor').style.display = 'none';
    ret('Div_Page_Content_'+CurrentTab).innerHTML = _content;
}//SavePage

function EditPage()
{
    ret('ContentEditor').style.display = 'inline';
    var _fck = FCKeditorAPI.GetInstance('FCKeditor_g');
    _fck.EditorDocument.body.innerHTML = ret('Div_Page_Content_'+CurrentTab).innerHTML;
}//AddNewPage


