var editor;

function onDoubleClick( ev ) {
	// Get the element which fired the event. This is not necessarily the
	// element to which the event has been attached.
	var element = ev.target || ev.srcElement;

	// Find out the div that holds this element.
	element = element.parentNode;

	if ( element.nodeName.toLowerCase() == 'div'
		 && ( element.className.indexOf( 'editable' ) != -1 ) )
		replaceDiv( element );
}

function replaceDiv( div ) {
	if ( editor )
		editor.destroy();

	editor = CKEDITOR.replace( div,
                {
                    filebrowserBrowseUrl : 'js/ckfinder/ckfinder.html',
                    filebrowserImageBrowseUrl : 'js/ckfinder/ckfinder.html?Type=Images',
                    filebrowserFlashBrowseUrl : 'js/ckfinder/ckfinder.html?Type=Flash',
                    filebrowserUploadUrl : 'js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                    filebrowserImageUploadUrl : 'js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                    filebrowserFlashUploadUrl : 'js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
                }
            );
}

function onDoubleClickImageUrl( ev ) {
	// Get the element which fired the event. This is not necessarily the
	// element to which the event has been attached.
	var element = ev.target || ev.srcElement;

	// Find out the div that holds this element.
	element = element.parentNode;

	if ( element.nodeName.toLowerCase() == 'div'
		 && ( element.className.indexOf( 'imagediv' ) != -1 ) )
		replaceDivImageUrl( element );
}

function replaceDivImageUrl( div ) {
	if ( editor )
		editor.destroy();

	editor = CKEDITOR.replace( div,
                {
                    filebrowserBrowseUrl : 'js/ckfinder/ckfinder.html',
                    filebrowserImageBrowseUrl : 'js/ckfinder/ckfinder.html?Type=Images',
                    filebrowserFlashBrowseUrl : 'js/ckfinder/ckfinder.html?Type=Flash',
                    filebrowserUploadUrl : 'js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                    filebrowserImageUploadUrl : 'js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                    filebrowserFlashUploadUrl : 'js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
                    toolbar : [ ['Source', 'Image'] ]
                }
            );
}

function changeBackgroundColor(obj, color) {
    obj.style.backgroundColor = color;
}

function changeClass(obj, classname) {
    obj.className = classname;
}

function getDataOfEditor(divId) {
    var value = "";
    if(editor) {
        if(editor.name == divId) {
            value = editor.getData();
        } else {
            value = document.getElementById(divId).innerHTML;
        }
    } else {
        value = document.getElementById(divId).innerHTML;
    }
    return value;
}

function submitAction(action) {
	document.forms[0].action = action;
    document.forms[0].method = "POST";
	document.forms[0].submit();
}

function switchLanguage(newLangCode) {
    document.getElementById("lang").value = newLangCode;
    submitAction(document.location);
}

function toggleCheckbox(obj, effectObjId, valueObjId) {
    if(obj.checked == true) {
        document.getElementById(effectObjId).disabled = false;
        document.getElementById(valueObjId).value = "Y";
    } else {
        document.getElementById(effectObjId).disabled = true;
        document.getElementById(valueObjId).value = "N";
    }
}

var Utf8 = {

    // public method for url encoding
    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;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = 0;
        var c1 = 0;
        var c2 = 0;
        var c3 = 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;
    }
}