// W2 Helper java script functions / objects

/* w2HelperObj is an object set by the base clay page - it has the following
** properties
** context - the root context of the application e.g. /MyWebApp
*/

//w2HelperObject = new Object();

// Context will be set by base clay page
//w2HelperObj.context = "";

function __w2disableActions(theAction)
{
	setTimeout(__w2disableTheActions,1);
}

function __w2disableTheActions()
{
    if (civicaFunctions.eggTimerInitialised)
    {
        civicaFunctions.showEggTimer();
    }

    var buttons = document.getElementsByTagName("input");
	for (var i=0;i<buttons.length;i++)
	{
		if (buttons[i].type == "submit")
		{
			buttons[i].disabled = true;

            if (buttons[i].className.indexOf("standardbutton") > -1)
            {
                buttons[i].className = "standardbuttondisabled";
            }
            else if (buttons[i].className.indexOf("linkButton") > -1)
            {
                buttons[i].className = "linkButtondisabled";
            }
            else
            {
                buttons[i].className = buttons[i].className + "disabled";
            }

        }
        else if (buttons[i].type == "image")
        {
            buttons[i].disabled = true;
        }
	}
}

var w2HelperFunctions = {

        documentLoaded: function()
        {
        }


}

function __w2enableAllActions()
{
    setTimeout(__w2enableTheActions,1);
}

function __w2enableTheActions()
{
    var buttons = document.getElementsByTagName("input");
	for (var i=0;i<buttons.length;i++)
	{
		if (buttons[i].type == "submit")
		{
			buttons[i].disabled = false;

            if (buttons[i].className.indexOf("standardbuttondisabled") > -1)
            {
                buttons[i].className = "standardbutton";
            }
            else if (buttons[i].className.indexOf("linkButtondisabled") > -1)
            {
                buttons[i].className = "linkButton";
            }
            else
            {
                buttons[i].className = buttons[i].className.replace("disabled", "");
            }

        }
        else if (buttons[i].type == "image")
        {
            buttons[i].disabled = false;
        }
	}

    // make sure any egg timer is removed
    if (civicaFunctions.eggTimerInitialised)
    {
        civicaFunctions.removeEggTimer();
    }
}

function __w2IsPotentialFileName(fname)
{
    // Trying to check something sensible entered.
    // IE hangs if not actually a c:\***** or \\ path.
    // Firefox 3 returns just the file name, not the full path, 
    // so checking for "." to "try" and prove we actually have
    // some sort of file entered as the upload file.
    return (fname.indexOf(":") == 1 || fname.substring(0,2) == "\\\\" || fname.indexOf(".") > 0);
}

/*
    wrapper for addEventListener as Firefox and IE do it differently
    e : the element you want to add the listner to
    type : type of event e.g. load, mouseover
    func : the function to fire on event
    capture : boolean, in W3c complient browsers allows you to specify if the event is fired down the
                        elements or up the element.  IE is always down
*/
function addListener(e, type, func, capture)
{
    if (e.addEventListener)
        e.addEventListener(type, func, capture);
    else if (e.attachEvent)
        e.attachEvent( "on" + type, func);
}

/*
    helper function to set focus on a forms first control
*/
function setFormsFirstControl(formId)
{
    // get the form
    var form = document.getElementById(formId);
    if (form == null) return;

    // look for first visible element
    for(i = 0; i < form.elements.length; i++)
	{
        // check for an input
        if (form.elements[i].type == 'text'
            || form.elements[i].type == 'select-one'
                || form.elements[i].type == 'select-many'
                    || form.elements[i].type == 'textarea')
        {
            form.elements[i].focus();
            return;
        }
	}
}

var civicaFunctions = {

    eggTimerInitialised : false,

    initialiseEggTimer : function()
    {
        // need to preload the image for this to work in firefox
        if (document.images)
        {
            var preLoadPic = new Image();
            preLoadPic.src = w2HelperObject.context+"/images/ani-busy.gif";
        }
        civicaFunctions.eggTimerInitialised=true;
    }
    ,

    showEggTimer : function()
    {
        var windowSize = civicaFunctions.getWindowSize();
        var scollOffSets = civicaFunctions.getScrollXY();

        var x = (windowSize[0]/2) + scollOffSets[0] - 32;
        var y = (windowSize[1]/2) + scollOffSets[1] - 32;

        // check if div already exists
        civicaFunctions.removeEggTimer();

        // create new div and img
        var newdiv = document.createElement('div');
        newdiv.id ='eggtimer.civica.co.uk';
        newdiv.style.width = 32;
        newdiv.style.height = 32;
        newdiv.style.position = "absolute";
        //newdiv.style.border = "2px solid #000";
        newdiv.style.left =x+"px";
        newdiv.style.top = y+"px"

        var img = document.createElement("img");
        img.src = w2HelperObject.context+"/images/throbber1.gif";
        img.alt = "Retrieving";
        img.style.width = 32;
        img.style.height = 32;

        // add to doc
        newdiv.appendChild(img);
        document.body.appendChild(newdiv);

    },

    removeEggTimer : function() {
        var div = document.getElementById('eggtimer.civica.co.uk');
        if (div!=null)
        {
            div.parentNode.removeChild(div);
        }
    }
    ,

    getWindowSize : function ()
    {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' )
        {
            myWidth = window.innerWidth;
            myHeight = window.innerHeight;
        }
        else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
        {
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
        }
        else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
        {
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
        }
        return [myWidth, myHeight];
    }
    ,
    getScrollXY : function ()
    {
        var scrOfX = 0, scrOfY = 0;
        if( typeof( window.pageYOffset ) == 'number' )
        {
            scrOfY = window.pageYOffset;
            scrOfX = window.pageXOffset;
        }
        else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
        {
            scrOfY = document.body.scrollTop;
            scrOfX = document.body.scrollLeft;
        }
        else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
        {
            scrOfY = document.documentElement.scrollTop;
            scrOfX = document.documentElement.scrollLeft;
        }
        return [ scrOfX, scrOfY ];
    }
}
