
// JScript source code

var mobjSilverlightControl = null;
var mblnIsIE = document.all;

// Provide support for contained components to search the application script
var mobjApp = window;


//contains calls to silverlight.js, example below loads Page.xaml
function createSilverlight()
{
    //
	Silverlight.createObject(
	    "ClientBin/"+window.VWGXAP+".xap",
	    document.getElementById("SilverlightControlHost"),
	    "SilverlightControl",
	    {width:'100%', height:'100%', background:'white', version:'2.0', isWindowless :'true'},
	    {onError:Main_OnError, onLoad:Main_OnLoad },
	    null,
	    null);               
}

var mintFormsCurrentBookmarkID = 0;

function Bookmarks_NavigateUrl(objDocument)
{
    
    var intBookmarkID = parseInt(Web_GetQueryStringParam(objDocument.location.href,"id"));
    
    objDocument.title = Bookmarks_GetTitle(objDocument.location.href);
    
    if(objDocument.title == "undefined")
    {
        objDocument.title = "Home";
    }
    
    
    //When nevigating backward the intBookmarkID is 1 below the mintFormsCurrentBookmarkID
    if(mintFormsCurrentBookmarkID!=intBookmarkID)
    {
        mintFormsCurrentBookmarkID = intBookmarkID;
        
        if(mobjSilverlightControl)
        {
            //calls the silverlight Main.xaml.cs OnBookmarkNavigateUrl
            //that loads the prev page(bookmark)
            intReturnValue = mobjSilverlightControl.Content.Main.OnBookmarkNavigateUrl(intBookmarkID);
        }
    
	}

	
}

/// <method name="Array.Web_GetQueryStringParams">
/// <summary>
/// Gets an query string values hashtable from a url.
/// </summary>
/// <param name="strUrl">The URL to extract params from.</param>
function Web_GetQueryStringParams(strUrl)
{
    var arrUrl = String(strUrl).split("?");
    var arrQuery = String((arrUrl.length==1)?arrUrl[0]:arrUrl[1]).split("&");
    var objQuery = {};
    for(var intIndex=0;intIndex<arrQuery.length;intIndex++)
    {
        var arrValue = String(arrQuery[intIndex]).split("=");
        objQuery[arrValue[0]]=arrValue[1];
    }
    return objQuery;
}
/// </method>

/// <method name="Array.Web_GetQueryStringParam">
/// <summary>
/// Gets an query string param value from a url.
/// </summary>
/// <param name="strUrl">The URL to extract params from.</param>
/// <param name="strParam">The param to extract.</param>
function Web_GetQueryStringParam(strUrl,strParam)
{
    return Web_GetQueryStringParams(strUrl)[strParam];
}
/// </method>

function Bookmarks_GetTitle(strUrl)
{
    return unescape(Web_GetQueryStringParam(strUrl,"title"));
}

/// </method>

/// <method name="SETBOOKMARK">
/// <summary>
/// //set the VWG_BookmarksBox IFrame with a new id and title
/// </summary>
/// <param name="intBookmarkID">The new page index(ID).</param>
/// <param name="strBookmarkTitle">The new page title.</param>
function SETBOOKMARK(intBookmarkID,strBookmarkTitle)
{
    mintFormsCurrentBookmarkID = intBookmarkID;
    
    var objBookmarksFrame1 = document.getElementById("VWG_BookmarksBox");
    if(objBookmarksFrame1)
    {
        //set the VWG_BookmarksBox IFrame with a new id and title
        objBookmarksFrame1.contentWindow.document.location.href = "Resources.Gizmox.WebGUI.Forms.Commons.Common.Bookmarks.htm.wgx?id="+intBookmarkID + "&title="+escape(strBookmarkTitle);
    }
}


/// </method>

/// <method name="setWindowFocus">
/// <summary>
/// //set the focus on the main silverlight window
/// </summary>
function setWindowFocus()
{
    window.focus();
}



function Main_OnLoad(objSender,objEvent)
{
    mobjSilverlightControl = document.getElementById('SilverlightControl');
    if (mobjSilverlightControl)
    {
        mobjSilverlightControl.focus();
    }
}

function Main_OnError(objSender,objEvent)
{

}


function Main_OnContextMenu(objSender,objEvent)
{
    var intReturnValue = 0;
    if(mobjSilverlightControl)
    {
        intReturnValue = mobjSilverlightControl.Content.Main.OnMouseRightButtonClick(objEvent.clientX,objEvent.clientY);
    }
    
    if(intReturnValue==1)
    {    
        if (mblnIsIE) 
        {
		    objEvent.returnValue = false;
	    }
	    else 
	    {
		    objEvent.preventDefault();
		    objEvent.stopPropagation();
		    return false;
	    }
	}
}


//=========================================================================================
//DEBUGGER WINDOW SECTION
//=========================================================================================

/// <summary>
/// Debug related parameters
/// </summary>
var mobjDebugWindow = null;
var mblnDebugWindow = false;

/// <method name="Debug_Initialize">
/// <summary>
/// Initializes debug mode
/// </summary>
function Debug_Initialize()
{
    // Try to get debug window (could throw exception if not same domain top)
	try
	{
	
	    // If there is a get debug window method
	    if(top.Debug_GetDebugWindow)
	    {
	        // Set that there is a debug window
	        mblnDebugWindow = true;
	        
	        // Try to get debug window
	        mobjDebugWindow = top.Debug_GetDebugWindow();
	        
	        // Set the current application window
	        top.Debug_SetApplicationWindow(window);
	        
	        // Enable events debugging
	        mblnDebugEvents = true;
	    }
	}
	catch(e)
	{
	}
}
/// </method>

/// <method name="Debug_EnsureWindow">
/// <summary>
/// Gets the current debug window
/// </summary>
function Debug_EnsureWindow()
{
    
    // If there should be a debug window and window was not cached
    if(mblnDebugWindow && !mobjDebugWindow)
    {
        // Cache debug window
        mobjDebugWindow = top.Debug_GetDebugWindow();
    }
}
/// </method>

/// <method name="DEBUGLOG">
/// <summary>
/// Writes a string to the log
/// </summary>
/// <param name="strText">The text to write to the log</param>
function DEBUGLOG(strText)
{
    // 
    Debug_EnsureWindow();

    // If there is a valid debug window
    if(mobjDebugWindow!=null)
    {
        mobjDebugWindow.Debug_Log(strText);
    }    
}
/// </method>

/// <method name="DEBUGLOGEVENT">
/// <summary>
/// Writes an event to the event viewer
/// </summary>
/// <param name="strSource"></param>
/// <param name="strMessage"></param>
/// <param name="strBody"></param>
function DEBUGLOGEVENT(strSource,strSubject,strBody)
{
    // 
    Debug_EnsureWindow();

    // If there is a valid debug window
    if(mobjDebugWindow!=null)
    {
        mobjDebugWindow.Debug_LogEvent(strSource,strSubject,strBody);
    }    
}
/// </method>


/// <method name="Events_RaiseEventsThroughForm">
/// <summary>
/// Sends the event buffer to the application server using a form field.
/// </summary>
function Events_RaiseEventsThroughForm(objDocument)
{
        if(mobjSilverlightControl)
        {
            //calls the silverlight Main.xaml.cs RaiseEventsThroughForm
            mobjSilverlightControl.Content.Main.RaiseEventsThroughForm(objDocument);
        }

    
}
/// </method>

/// <method name="Events_EndFormResponseValidation">
/// <summary>
/// Disables the mechanism to recover from form response error when it is not needed any more
/// </summary>
function Events_HandlesResponseFromForm(objDocument)
{
        if(mobjSilverlightControl)
        {
            //calls the silverlight Main.xaml.cs HandlesResponseFromForm
            mobjSilverlightControl.Content.Main.HandlesResponseFromForm(objDocument);
        }
}
/// </method>


//COPY TO CLIPBOARD
function COPYTOCLIPBOARD(strType,strValue)
{

    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

    var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
    if (!clip)
    {
        return false;
    }
    
    var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
    if (!trans)
    {
        return false;
    }
    
    trans.addDataFlavor('text/unicode');

    var str = new Object();
    var len = new Object();

    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    var copytext=strValue;
    str.data=copytext;

    trans.setTransferData("text/unicode",str,copytext.length*2);
    var clipid=Components.interfaces.nsIClipboard;
    if (!clip)
    {
        return false;
    }
    clip.setData(trans,null,clipid.kGlobalClipboard);
    return true;

}


//COPY FROM CLIPBOARD
function COPYFROMCLIPBOARD(strType,strValue)
{

    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

    var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard); 
    if (!clip) return false;
    
    var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
    if (!trans) return false;    
    trans.addDataFlavor("text/unicode");
    
    clip.getData(trans, clip.kGlobalClipboard);  
  
    var str       = new Object();  
    var strLength = new Object();  
      
    trans.getTransferData("text/unicode", str, strLength);  
    
    if (str)    
        str       = str.value.QueryInterface(Components.interfaces.nsISupportsString);  
        
    if (str) 
        pastetext = str.data.substring(0, strLength.value / 2);  
        
        return pastetext;
    
}



