/*
	Toggles object visibility
*/
function toggleVis(pObj)
{
	var aObj = document.getElementById(pObj);
	
	if (!aObj)
		return;
		
	if (aObj.style.display != 'block')
		aObj.style.display = 'block';
	else
		aObj.style.display = 'none';
}

/*
	Hides an object
*/
function hideObj(pObj)
{
	var aObj = document.getElementById(pObj);
	
	if (!aObj)
		return;
		
	aObj.style.display = 'none';
}

/*
	Unhides an object
*/
function showObj(pObj)
{
	var aObj = document.getElementById(pObj);
	
	if (!aObj)
		return;
		
	aObj.style.display = 'block';
}

/*
	Checks if a text can be converted to an integer
*/
function isInt(pStr)
{
	var chars = "0123456789";

	for (var i = 0; i < pStr.length; i++) 
	{ 
		if (chars.indexOf(pStr.charAt(i)) == -1) 
		{
			return false;
		}
	}

return true;
   
}

/*
	Gets the value of an input field
*/
function getVal(pObjName)
{
	var obj = document.getElementById(pObjName);
	return obj.value;
}

/*
    Sets the value for an input field
*/
function setVal(pObjName, pVal)
{
    var obj = document.getElementById(pObjName);
    obj.value = pVal;
}

/*
	Sets an object's innerHTML
*/
function setContent(pObj, pContent)
{
	var obj = document.getElementById(pObj);
	
	if (obj)
		obj.innerHTML = pContent;
}

/*
    Get the mouse coordinates in a window
*/
window.mousePos = function(e) {
	var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
     
    if (e.pageX || e.pageY)
    {
		posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
		posx = e.clientX + document.body.scrollLeft
                           + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
                           + document.documentElement.scrollTop;
    }
     
    return {x:posx,y:posy}
}

/*
    Get the document dimensions
*/
document.dimensions = function(){
    var D = document;
    var dx = 0;
    var dy = 0;
    
    dy = Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
    
    dx = Math.max(
        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
    );
      
    return {w:dx,h:dy};
}

/*
	Initializes a color picker
	
	pObj	-	the name of the color picker's object
	pCallback	-	the name of the onclick function
*/
function initColorPicker(pObj, pCallback)
{
	var colors = Array('330000', '333300', '336600', '339900', '33CC00', '33FF00', '66FF00', '66CC00', '669900', '666600', '663300', '660000', 'FF0000', 'FF3300', 'FF6600', 'FF9900', 'FFCC00', 'FFFF00', '330033', '333333', '336633', '339933', '33CC33', '33FF33', '66FF33', '66CC33', '669933', '666633', '663333', '660033', 'FF0033', 'FF3333', 'FF6633', 'FF9933', 'FFCC33', 'FFFF33', '330066', '333366', '336666', '339966', '33CC66', '33FF66', '66FF66', '66CC66', '669966', '666666', '663366', '660066', 'FF0066', 'FF3366', 'FF6666', 'FF9966', 'FFCC66', 'FFFF66', '330099', '333399', '336699', '339999', '33CC99', '33FF99', '66FF99', '66CC99', '669999', '666699', '663399', '660099', 'FF0099', 'FF3399', 'FF6699', 'FF9999', 'FFCC99', 'FFFF99', '3300CC', '3333CC', '3366CC', '3399CC', '33CCCC', '33FFCC', '66FFCC', '66CCCC', '6699CC', '6666CC', '6633CC', '6600CC', 'FF00CC', 'FF33CC', 'FF66CC', 'FF99CC', 'FFCCCC', 'FFFFCC', '3300FF', '3333FF', '3366FF', '3399FF', '33CCFF', '33FFFF', '66FFFF', '66CCFF', '6699FF', '6666FF', '6633FF', '6600FF', 'FF00FF', 'FF33FF', 'FF66FF', 'FF99FF', 'FFCCFF', 'FFFFFF', '0000FF', '0033FF', '0066FF', '0099FF', '00CCFF', '00FFFF', '99FFFF', '99CCFF', '9999FF', '9966FF', '9933FF', '9900FF', 'CC00FF', 'CC33FF', 'CC66FF', 'CC99FF', 'CCCCFF', 'CCFFFF', '0000CC', '0033CC', '0066CC', '0099CC', '00CCCC', '00FFCC', '99FFCC', '99CCCC', '9999CC', '9966CC', '9933CC', '9900CC', 'CC00CC', 'CC33CC', 'CC66CC', 'CC99CC', 'CCCCCC', 'CCFFCC', '000099', '003399', '006699', '009999', '00CC99', '00FF99', '99FF99', '99CC99', '999999', '996699', '993399', '990099', 'CC0099', 'CC3399', 'CC6699', 'CC9999', 'CCCC99', 'CCFF99', '000066', '003366', '006666', '009966', '00CC66', '00FF66', '99FF66', '99CC66', '999966', '996666', '993366', '990066', 'CC0066', 'CC3366', 'CC6666', 'CC9966', 'CCCC66', 'CCFF66', '000033', '003333', '006633', '009933', '00CC33', '00FF33', '99FF33', '99CC33', '999933', '996633', '993333', '990033', 'CC0033', 'CC3333', 'CC6633', 'CC9933', 'CCCC33', 'CCFF33', '000000', '003300', '006600', '009900', '00CC00', '00FF00', '99FF00', '99CC00', '999900', '996600', '993300', '990000', 'CC0000', 'CC3300', 'CC6600', 'CC9900', 'CCCC00', 'CCFF00', '000000', '111111', '222222', '333333', '444444', '555555', '666666', '777777', '888888', '999999', 'AAAAAA', 'BBBBBB', 'CCCCCC', 'DDDDDD', 'EEEEEE', 'FFFFFF');
	
	var box = document.getElementById(pObj);
	var inner_html = '';
	var index = 0;
	
	for (var y = 1; y <= 8; y++)
	{
		for (var x = 1; x <= 29; x++)
		{
			inner_html += '<div class="cpkitem" style="background-color: #' + colors[index] + '" onclick="' + pCallback + '(\'' + colors[index] + '\')"></div>';
			index++;
		}
		
		inner_html += '<div class="cpkclr"></div>';
	}
	
	box.innerHTML = inner_html;
}

/*
    Get XMLHttp object for ajax requests
*/
function getHttpObj()
{
    var xmlHttp = null;
    // let's try firefox, opera 8+ and Safari
    try
    {
        xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        // all the rest stuff for IE ...
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e) {}
            }
        }
    }
    return xmlHttp;
}

/*
    Send get request with ajax
    pUrl:       ISIS friendly URL
    fOnStart:   function for before the get is performed
    fOnEnd:     function for operation finish. Takes 2 parameters: result string and error status
                fOnEnd(string pResultString, bool pError);
*/
function ajaxGet(pUrl, fOnStart, fOnEnd)
{
    var url = pUrl;
    var conn = getHttpObj();
    conn.open('GET', url, true);
    
    conn.onreadystatechange = function()
        {
            if (conn.readyState == 4)
            {
                var err = true;
                
                if (conn.status == 200)
                    err = false;
                    
                if (fOnEnd)
                    fOnEnd(conn.responseText, err);
            }
        }
        
    if (fOnStart)
        fOnStart();

    conn.send(null);
}

/*
    Get the position of an HTML element
*/
function getElementPos(pObj) {
    var curleft = curtop = 0;
    obj = document.getElementById(pObj);
    
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    
    return {'left': curleft, 'top': curtop};
}

/*
    Get the dimensions of an HTML element
*/
function getElementDimensions(pObj) {
    var x = 0;
    var y = 0;
    var obj = document.getElementById(pObj);
    y = obj.offsetHeight;
    x = obj.offsetWidth;
    return {'width': x, 'height': y};
}
