
/* creates HTML Dilaog
 * resources needed:
 *     <script type="text/javascript" src="res/scripts/dialog/SDialogAPI.js"></script>
 *     <script type="text/javascript" src="res/scripts/dialog/SCustReportDialog.js"></script>
 *     <script type="text/javascript" src="res/scripts/dialog/SDialogDrag.js"></script>
 *     <script type="text/javascript" src="res/scripts/uifactory.js"></script>
 *     <link rel="stylesheet" href="res/styles/dialogLayer_winxp.css">
 *     res/images/closeBox_winxp.png
 */

/**
 * Dialog tag heirarchy:
 *  <DIV id:SWindow>
 *      <DIV  id="titlebar" >
 *          <img id="closebox"/>
 *          <span id="STitlebar">STitlebar</span>
 *      </DIV>
 *      <IFRAME> or <DIV> id = contentFrame
 *  </DIV>
 */
function DialogBox(sId,sTitle,x,y,iWidth,iHeight,sUrl,isIFrame,sFormName,fnHandleFocus,imgSrc)
{
    var onCloseFunc=null;
    var me=this;
    //var dialog ;

    this.init = function(sId,sTitle,x,y,iWidth,iHeight,sUrl,isIFrame,sFormName,imgSrc)
    {
        //alert("creating dialog");
        this.sId = sId;
        this.sTitle = sTitle;
        this.x = x;
        this.y = y;
        this.iWidth = iWidth;
        this.iHeight = iHeight;
        this.sUrl = sUrl;
        this.objContentArea=null; // either div or iframe object of dialogbox
        this.sFormName=sFormName;
        this.sOnLoadScript="";
        //this.sUserName=sUserName;
        var objDivWindow = document.createElement("div");
        objDivWindow.id = sId+"SWindow";
        objDivWindow.className = "SWindow";

        var objDivTitle = document.createElement("div");
        objDivTitle.id = sId+"titlebar";
        if( imgSrc)
        {
            var ImageSrc = document.createElement("img");
            ImageSrc.src="/res/images/"+imgSrc;
            objDivTitle.appendChild(ImageSrc);
        }
        objDivTitle.className = "titlebar";
	    if(fnHandleFocus)
	    {
		    objDivTitle.onclick=this.invokeFocusHandler;
		}
        var objImg = document.createElement("img");
        objImg.id = sId+"closebox";
        objImg.className = "closebox";
        objImg.src = "res/images/closeBox_winxp.png";
//        objImg.style.background = "transparent";
        objImg.onclick = this.hide;
        var objSpan = document.createElement("span");       
        objSpan.id = sId+"STitlebar";
        objSpan.className = "STitlebar";

        objSpan.innerHTML = sTitle;
        objDivTitle.appendChild(objImg);
        objDivTitle.appendChild(objSpan);

        if(isIFrame)
        {
            this.objContentArea = document.createElement("IFRAME");
            this.objContentArea.name = "iframe"+sId;
            this.objContentArea.id = "iframe"+sId;
            this.objContentArea.style.width = iWidth;
            this.objContentArea.style.height = iHeight;
            this.objContentArea.scrolling = "no";
            //this.objContentArea.style.overflowX="auto";
            this.objContentArea.style.overflowY="hidden";
	        if(fnHandleFocus)
			{
				this.objContentArea.onfocus=me.invokeFocusHandler;

//				this.objContentArea.contentWindow.document.onclick=this.invokeFocusHandler;
			}
        }
        else
        {
            this.objContentArea = document.createElement("div");
            this.objContentArea.style.width = iWidth;
            this.objContentArea.style.height = iHeight;
        }
        this.objContentArea.id = sId+"contentFrame";
        this.objContentArea.className = "contentFrame";
        this.objContentArea.style.backgroundColor = "#FFFFFF";
        objDivWindow.appendChild(objDivTitle);
        objDivWindow.appendChild(this.objContentArea);
	    //alert("dlgbox  "+objDivWindow.outerHTML);
//        if(isIFrame)
            document.forms[0].appendChild(objDivWindow);

        //initSpopUpAPI();
        /*initSpopUpAPI();
        initDrag();*/

        if(!this.dialog)
            this.dialog = new Dialog(this.sId,this.sUrl,this.x,this.y,this.iWidth,this.iHeight,sFormName);
    }
	this.invokeFocusHandler = function()
	{
		//alert("onfocus "+me)
		fnHandleFocus(me);
	}
    this.setOnClose= function(onClose)
    {
        this.onCloseFunc=onClose;
    }
    this.getContentPane = function()
    {
        return this.objContentArea;
    }
    /**
     *  Adds form content to contentpane.
     *  This method must be called after appending elements to form.
     **/
    this.addFormContent = function(objForm,objContentPane)
    {
        objForm.appendChild(objContentPane.parentNode);
    }
    /**
      * sets dialog title
     **/
    this.setTitle = function(sTitle)
    {
        var objSpan = this.objContentArea.parentNode.getElementsByTagName("SPAN")[0];
        objSpan.innerHTML = sTitle;
    }

    this.setUserName = function(sUserName)
    {
        this.sUserName=sUserName
    }

    this.setAdminName = function(sAdminName)
    {
        this.sAdminName=sAdminName
    }

	this.setSessionId = function(sSessionId)
    {
        this.sSessionId=sSessionId;
    }

    this.getTitle = function()
    {
        return this.objContentArea.parentNode.getElementsByTagName("SPAN")[0].innerHTML;
    }

    this.getUserName = function()
    {
        return this.sUserName;
    }

    this.getAdminName = function()
    {
        return this.sAdminName;
    }

    this.getSessionId = function()
    {
        return this.sSessionId;
    }

    // closes dialog
    this.hide = function()
    {
        this.sOnLoadScript = "";
        //alert(me.onCloseFunc);
       // alert(getCurrentFrame());
        //alert(typeof window["getCurrentFrame"]=="function");
        
        if(me.onCloseFunc)
        {
            if(typeof me.onCloseFunc=="function")
            {
                me.onCloseFunc();
            }
            else if(typeof window.objContentArea.contentWindow[me.onCloseFunc]=="function")
            {
                eval("me.objContentArea.contentWindow."+me.onCloseFunc);
            }
        }
       // alert(me.dialog)
        if(me.dialog)
           me.dialog.closeSDialog();
    }
    this.setURL = function(sURL)
    {
        this.sUrl = sURL;
        if(this.dialog)
            this.dialog.setURL(sURL);
    }
    // Calls the function present in iframe document. if iframe window not loaded yet, then it stores the function calling script in variable 'sOnLoadScript' that needs to be evaluated on page load of iframe.
    // params: 1 - handler name, 2 - fieldname, 3 - field value
    this.setFldValue = function(handler,sFieldName,sFieldValue)
    {
        //alert(handler+","+sFieldName+","+sFieldValue);
        //if(eval("this.objContentArea.contentWindow."+handler))
        if( this.objContentArea.contentWindow.document && this.objContentArea.contentWindow.document.forms.length > 0)
        {
            eval("this.objContentArea.contentWindow."+handler+"('"+sFieldName+"','"+sFieldValue+"');");
        }
        else
            this.sOnLoadScript = this.sOnLoadScript + handler+"('"+sFieldName+"','"+sFieldValue+"');";

    }
    // Calls the javascript function in iframe by passing parameter.
    // params : function name, parameters
    this.callFn = function(handler,sParam)
    {
        var sFunc = sParam ? handler +"('" + sParam + "');" : handler + "();" ;
        if(this.objContentArea.contentWindow && this.objContentArea.contentWindow.document.forms.length > 0)// && eval("this.objContentArea.contentWindow."+handler))
            eval("this.objContentArea.contentWindow."+sFunc);
        else
            this.sOnLoadScript = this.sOnLoadScript + sFunc;
        //document.getElementById(this.objContentArea.id).contentWindow.setURLParams("dashdg")
    }
    /*this.refreshForm = function()
    {
        if(this.objContentArea.contentWindow && this.objContentArea.contentWindow.document.forms[0])
            this.objContentArea.contentWindow.document.forms[0].reset();
    }*/
    // shows dialog
    this.show = function()
    {
        try
        {
            this.dialog.sFormName = this.sFormName; //assigning form name to dialog box to disable fields
            this.dialog.showDialog();

            /*alert("this.sOnLoadScript...."+(this.sOnLoadScript));
            if(this.sOnLoadScript)  //evaluating onload script if dialog is already created. if not onload script will be evaluated in dialogbox source onload function
            {
                if(this.objContentArea.contentWindow && this.objContentArea.contentWindow.document.forms.length > 0)
                {
                    eval("this.objContentArea.contentWindow."+this.sOnLoadScript);
                    this.sOnLoadScript = "";
                }
            }*/
        }
        catch(ex)
        {
            if(ex.description)
                alert("Error: "+ex.description);
            else
                alert("Error: "+ex);
        }
    }
    this.init(sId,sTitle,x,y,iWidth,iHeight,sUrl,isIFrame,sFormName,imgSrc);
}


