// Chris Pyper 2006
// Utility functions

var XMLREQUEST_COMPLETE = 4;

// a convenience function for AJAX work
function newXMLHTTPRequest(url,meth,callbackFunc,async)
{
    var xmlreq = false;

    if (window.XMLHttpRequest)
    {
        // Non-MS browsers
        xmlreq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // MS browsers
        try {
            xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e1) {
            try {
                // Try for an older version
                xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                // Complete and utter failure.
            }
        }
    }

    if ( xmlreq )
    {
        meth = ( meth == null ) ? "GET" : meth.toUpperCase();
        async = ( async == null ) ? true : async;
        if ( callbackFunc != null )
        {
            xmlreq.onreadystatechange = function() {
                if ( xmlreq.readyState == XMLREQUEST_COMPLETE )
                {
                    if ( xmlreq.status == 200 ) // HTTP status code 200 = OK
                    {
                        callbackFunc(xmlreq.responseXML,xmlreq.responseText);
                    }
                    else
                    {
                        alert("XMLHTTP: "+ xmlreq.status +" "+ xmlreq.statusText  +"]");
                    }
                }

            };
        }

        // set up the request
        // note: this doesn't actually submit the request
        // a call to the xmlreq.send() method is needed for that.
        xmlreq.open(meth,url,async);

        if ( meth == "POST" )
            xmlreq.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    }

    return( xmlreq );
}

// Eseentially URL encode
function encodeHtml(data)
{

	return encodeURIComponent(data);
/*
        encodedHtml = escape(data);
        encodedHtml = encodedHtml.replace(/\//g,"%2F");
        encodedHtml = encodedHtml.replace(/\?/g,"%3F");
        encodedHtml = encodedHtml.replace(/=/g,"%3D");
        encodedHtml = encodedHtml.replace(/&/g,"%26");
        encodedHtml = encodedHtml.replace(/@/g,"%40");

        return encodedHtml;
*/
}

// return a random number between 0 and max with an option to force ints
// Used to prevent cachching of requests
function getRandom(max,forceInt)
{
    var num;

    if ( !max )
        max = 10;

    if ( forceInt )
        num = Math.floor( Math.random()*max );
    else
        num = Math.random() * max;

    return( num );
}

// returns hexadecimal color representation from a number or a rgb-style color.
function convertColor(v) 
{
    // returns the hex representation of one byte (2 digits)
    function hex(d) {
        return (d < 16) ? ("0" + d.toString(16)) : d.toString(16);
    };

    if (typeof v == "number") {
        // we're talking to IE here
        var r = v & 0xFF;
        var g = (v >> 8) & 0xFF;
        var b = (v >> 16) & 0xFF;
        return "#" + hex(r) + hex(g) + hex(b);
    }

    if (v.substr(0, 3) == "rgb") {
        // in rgb(...) form -- Mozilla
        var re = /rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)/;
        if (v.match(re)) {
            var r = parseInt(RegExp.$1);
            var g = parseInt(RegExp.$2);
            var b = parseInt(RegExp.$3);
            return "#" + hex(r) + hex(g) + hex(b);
        }
        // doesn't match RE?!  maybe uses percentages or float numbers
        // -- FIXME: not yet implemented.
        return null;
    }

    if (v.substr(0, 1) == "#") {
        // already hex rgb (hopefully :D )
        return v;
    }

    // if everything else fails ;)
    return null;
}

// translate a string
function translate(key)
{
	var str = "";

	if ( ISTRINGS[EDITORUSERLANGUAGE] && ISTRINGS[EDITORUSERLANGUAGE][key] ) 
	{
		str = ISTRINGS[EDITORUSERLANGUAGE][key];
	}
	else if ( ISTRINGS['en'][key] )
	{
		str = ISTRINGS['en'][key];
	}
	else
	{
		str = key;
	}

	return str;
}

