﻿function insert_text(elmID, text) {
	var elm = document.getElementById(elmID);
	if (elm == null) return false;
	if (document.selection) {
		//IE
		elm.focus();
		var sel = document.selection.createRange();
		sel.text = text;
	} else {
		//Mozilla/Firefox/Netscape 7+ support
		if (elm.selectionStart || elm.selectionStart == "0") {
			//Here we get the start and end points of the
			//selection. Then we create substrings up to the
			//start of the selection and from the end point
			//of the selection to the end of the field value.
			//Then we concatenate the first substring, text,
			//and the second substring to get the new value.
			var startPos = elm.selectionStart;
			var endPos = elm.selectionEnd;
			elm.value = elm.value.substring(0, startPos) + text + elm.value.substring(endPos, elm.value.length);
		} else {
			elm.value += text;
		}
	}
	elm.focus();
	return false;
}

function template_token_nextID(elmID) {
	var elm = document.getElementById(elmID);
	if (elm == null) return "";
	var txt = elm.innerText;
	if (txt == "") return "token_1";
	for(var i = 1; i < 100; i++) {
		if(txt.indexOf("id=\"token_" + i + "\"") == -1)
			return "token_" + i;
	}
	alert("Sunt peste 100 de token-uri in acest template!");
	return "";
}

function fieldset_open(elm_id) {
	var elm = document.getElementById(elm_id);
	if (elm == null) return false;
	if (elm.className == "open") 
		elm.className = "closed";
	else 
		elm.className = "open";
	return false;
}

function sel_invert(chk_name) {
	var col = document.getElementsByName(chk_name);
	for (var i = 0; i < col.length; i++)
		col[i].checked = !col[i].checked;
}

function sel_allnone(elm, chk_name) {
	if (elm != null) {
		var col = document.getElementsByName(chk_name);
		for (var i = 0; i < col.length; i++)
			col[i].checked = elm.checked;
	}
}

function sel_values(chk_name, chk_values) {
	var col = document.getElementsByName(chk_name);
	if (chk_values == "all_chk_values") {
		for (var i = 0; i < col.length; i++) {
			col[i].checked = true;
		}
	} else {
		chk_values = "," + chk_values + ",";
		for (var i = 0; i < col.length; i++) {
			var chk = col[i];
			if (chk.checked == false && chk.value != "" && chk_values.indexOf("," + chk.value + ",") != -1) chk.checked = true;
		}
	}
}

var URLEncDec = {
    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },
    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },
    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },
    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }
        return string;
    }
}

function eval_scripts(content) {
	var pattern = "<script[^>]*>([\\S\\s]*?)<\/script>";
	var reAll = new RegExp(pattern, "img");
	var reOne = new RegExp(pattern, "im");
	var m = content.match(reAll);
	for (var i = 0; i < m.length; i++) {
		eval((m[i].match(reOne) || ['',''])[1]);
	}
}

var to_selectall_elm = null;
function selectall(elm) {
	to_selectall_elm = elm;
	setTimeout("if(to_selectall_elm!=null){to_selectall_elm.select()}", 10);
}

function test_input_url(elm_id) {
	var elm = document.getElementById(elm_id);
	if (elm == null) return false;
	if (elm.value == "") return false;
	window.open(elm.value);
	return false;
}


//function goto(url) {
//	if (typeof(url) == "undefined")
//		document.location.reload();
//	else
//		if (url == "reload") 
//			document.location.reload();
//		else
//			document.location = url;
//	}


