
/***********************************************************/
/* CSS */
function cssLoad(arrCSS){
	for(var i=0; i<arrCSS.length; i++){
		$('head').prepend('<link rel="stylesheet" type="text/css" href="' + arrCSS[i] + '.css"/>');
	}
}


/***********************************************************/
/* SCRIPTS */
function scriptsLoad(arrScripts){
	for(var i=0; i<arrScripts.length; i++){
		$('head').append('<script type="text/javascript" src="' + arrScripts[i] + '.js"/>');
	}
}


/***********************************************************/
/* UTF */
function decode(text){
	return unic_text(html_unic(text));
}
function encode(text){
	return unic_html(text_unic(text));
}
function decode_nl2br(text){
	return nl2br(unic_text(html_unic(text)));
}
function encode_nl2br(text){
	return nl2br(unic_html(text_unic(text)));
}

function nl2br(text){
	text = escape(text);
	re_nlchar = "";
	str = "";
	
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	}else if(text.indexOf('%26%2310%3B') > -1){	//&#10;
		re_nlchar = /%26%2310%3B/g ;
	}
	
	//alert(text + " - " + (re_nlchar.toString()).length);
	if((re_nlchar.toString()).length > 0){
		str = unescape(text.replace(re_nlchar,'<br />'));
	}
	else{
		str = unescape(text);
	}	
	return str;	
}

function text_unic(str) {
    return escape(str);
};

function unic_text(str) {
    return unescape(str);
};

function unic_utf8(str) {
    nc = "";
    for (i = 0; i < str.length; i++) {
        s = str.substr(i, 6);
        if (arr = s.match(/%u[0-9A-F][0-9A-F][0-9A-F][0-9A-F]/i)) {
            c = arr[0].substr(2, 4);
            c = unic_u(c);
            i += 5;
        } else {
            c = str.charAt(i);
        };
        nc += c;
    };
    return nc;
};

function unic_u(str) {
    num = parseInt(str, 16);
    c1 = (num & parseInt("1111000000000000", 2)) >> 12;
    c2 = (num & parseInt("0000111111000000", 2)) >> 6;
    c3 = (num & parseInt("0000000000111111", 2));
    c1 = dec2hex(c1 | parseInt("11100000", 2));
    c2 = dec2hex(c2 | parseInt("10000000", 2));
    c3 = dec2hex(c3 | parseInt("10000000", 2));
    str = "";
    str += "%" + c1.substr(2);
    str += "%" + c2.substr(2);
    str += "%" + c3.substr(2);
    return str;
};

function unic_html(str) {
    nc = "";
    for (i = 0; i < str.length; i++) {
        s = str.substr(i, 6);
        if (arr = s.match(/%u[0-9A-F][0-9A-F][0-9A-F][0-9A-F]/i)) {
            c = arr[0].substr(2, 4);
            c = "&#" + parseInt(c, 16) + ";";
            i += 5;
        } else if (arr = s.match(/^%[0-9A-F][0-9A-F]/i)) {
            c = arr[0].substr(1, 2);
            c = "&#" + parseInt(c, 16) + ";";
            i += 2;
        } else {
            c = str.charAt(i);
        };
        nc += c;
    };
    return nc;
};

function html_unic(str) {
    nc = "";
    for (i = 0; i < str.length; i++) {
        s = str.substr(i, 8);
        if (arr = s.match(/^&#[0-9]+;/)) {
            c = arr[0].replace(/[&#;]/g, "");
            c = dec2hex(c);
            c = "%u" + c;
            c = c.replace(/^%u00/, "%");
            i += arr[0].length - 1;
        } else {
            c = str.charAt(i);
        };
        nc += c;
    };
    return nc;
};

function utf8_unic(str) {
    dc = "";
    for (i = 0; i < str.length; i++) {
        s = str.substr(i, 9);
        if (arr = s.match(/%E[0-9A-F]%[89AB][0-9A-F]%[89AB][0-9A-F]/i)) {
            c1 = arr[0].substr(1, 2);
            c2 = arr[0].substr(4, 2);
            c3 = arr[0].substr(7, 2);
            c = utf8_u(c1, c2, c3);
            i += 8;
        } else {
            c = str.charAt(i);
        };
        dc += c;
    };
    return dc;
};

function utf8_u(c1, c2, c3) {
    c = 0;
    c += (parseInt(c1, 16) & parseInt("00001111", 2)) << 12;
    c += (parseInt(c2, 16) & parseInt("00111111", 2)) << 6;
    c += (parseInt(c3, 16) & parseInt("00111111", 2));
    c = dec2hex(c);
    c = "%u" + c;
    return c;
};

function dec2hex(dec) {
    h = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
    hex = '';
    hex += h[Math.floor(dec / 0x1000)]; dec = dec % 0x1000;
    hex += h[Math.floor(dec / 0x100)]; dec = dec % 0x100;
    hex += h[Math.floor(dec / 0x10)]; dec = dec % 0x10;
    hex += h[Math.floor(dec / 0x1)]; dec = dec % 0x1;
    return hex;
};
