/*
 * Page js
 */

var		U_ROOT = "http://" + document.location.hostname;


Page = {};

// Call to XHR with $_GET params
Page.get = function (/* data, container, onComplete, async */)
{
    var         p_data = null;
    var         p_container = null;
    var         p_complete = function () {};
    var         p_async = true;

    // Get arguments
    if (arguments.length > 0)
	p_data = arguments[0] + "&dyn=1";
    if (arguments.length > 1)
	p_container = $(arguments[1]);
    if (arguments.length > 2)
	p_complete = arguments[2];
    if (arguments.length > 3)
	p_async = arguments[3];

    // Load XHR
    new Request.HTML ({url: U_ROOT + "/", data: p_data, update: p_container, onComplete: p_complete, async: p_async, evalScripts: true}).get ();
}

// Call to XHR with $_POST params
Page.post = function (/* data, container, onComplete */)
{
    var         p_data = null;
    var         p_container = null;
    var         p_complete = function () {};

    // Get arguments
    if (arguments.length > 0)
    p_data = $(arguments[0]);
    if (arguments.length > 1)
    p_container = $(arguments[1]);
    if (arguments.length > 2)
    p_complete = arguments[2];

    // Save WYSIWYG contents if needed
    if ($$(".wysiwyg").length)
	Wysiwyg.save ();

    // Post form
    new Request.HTML ({url: U_ROOT + "/index.php?dyn=1", data: p_data, update: p_container, onComplete: p_complete, evalScripts: true}).post ();
}

// Visit page with given URL
Page.relocate = function (url)
{
    document.location = url;
}

// Reload current page
Page.reload = function ()
{
    document.location = document.location;
}

// View webpage with reference ref
Page.view = function (ref)
{
    Page.relocate (U_ROOT + '/?module=webpage&action=view&ref=' + ref);
}

// Open new window with url URL
Page.popup = function (url)
{
    window.open (url);
}

// Submit page and sync all WYSIWYG contents before
Page.submit = function (prefix, state, module)
{
    var	      pref = (prefix ? '-' + prefix : '');
    var	      frm = 'frm' + pref;
    
    if (state)
	$('state' + pref).value = state;
    if (module)
	$('module' + pref).value = module;
    Page.post (frm);
}

// Reset form
Page.reset = function (prefix)
{
    var	      frm = $('frm' + (prefix ? '-' + prefix : ''));

    frm.reset ();
}

// Submit form to  an hidden frame
Page.submitFile = function (prefix)
{
    var	      pref = (prefix ? '-' + prefix : '');
    var	      frm = $('frm' + pref);

    frm.onsubmit();
}

// Submit form to  an hidden frame
Page.onsubmit = function (prefix)
{
    var	      pref = (prefix ? '-' + prefix : '');
    var	      frm = $('frm' + pref);

    frm.onsubmit();
}

