
function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
 }
 
var http = createRequestObject();

// Delete an image
function sndDeleteImg(message, url) {
	if(confirm(message)){
		http.open('post', ''+url+'');
		http.onreadystatechange = handleResponseDeleteImg;
		http.send(null);
	}
}
function handleResponseDeleteImg() {
	if(http.readyState == 4){
		var response = http.responseText;
		var update = new Array();

		if(response.indexOf('|' != -1)) {
			update = response.split('|');
			document.getElementById('menu_left_gallery_content').innerHTML = response;
		}
	}
	if(http.readyState == 1){
		document.getElementById('menu_left_gallery_content').innerHTML = '<p>&nbsp;&nbsp;Loading...</p>';
	}
}
//================================

// Delete a file
function sndDeleteFile(message, url) {
	if(confirm(message)){
		http.open('post', ''+url+'');
		http.onreadystatechange = handleResponseDeleteFile;
		http.send(null);
	}
}
function handleResponseDeleteFile(formsign) {
	if(http.readyState == 4){
		var response = http.responseText;
		var update = new Array();

		if(response.indexOf('|' != -1)) {
			update = response.split('|');
			document.getElementById('menu_left_files_content').innerHTML = response;
		}
	}
	if(http.readyState == 1){
			document.getElementById('menu_left_files_content').innerHTML = '<p>&nbsp;&nbsp;Loading...</p>';
		}
}
//================================


function str2url(str,encoding,ucfirst){
	str = str.toUpperCase();
	str = str.toLowerCase();
	
	str = str.replace(/[\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5]/g,'a');
	str = str.replace(/[\u00E7]/g,'c');
	str = str.replace(/[\u00E8\u00E9\u00EA\u00EB]/g,'e');
	str = str.replace(/[\u00EC\u00ED\u00EE\u00EF]/g,'i');
	str = str.replace(/[\u00F2\u00F3\u00F4\u00F5\u00F6\u00F8]/g,'o');
	str = str.replace(/[\u00F9\u00FA\u00FB\u00FC]/g,'u');
	str = str.replace(/[\u00FD\u00FF]/g,'y');
	str = str.replace(/[\u00F1]/g,'n');
	str = str.replace(/[\u0153]/g,'oe');
	str = str.replace(/[\u00E6]/g,'ae');
	str = str.replace(/[\u00DF]/g,'ss');
	
	str = str.replace(/[^a-z0-9_\s\'\:\/\[\]-]/g,'');
	str = str.replace(/[\s\'\:\/\[\]-]+/g,' ');
	str = str.replace(/[ ]/g,'-');
	
	if (ucfirst == 1) {
		c = str.charAt(0);
		str = c.toUpperCase()+str.slice(1);
	}
	
	return str;
}

function invalid_field(check, to_id){
	if(getE(check).value != ""){
		document.getElementById(to_id).disabled = true;
	}else{
	document.getElementById(to_id).disabled = false;
	}
}

function copy2friendlyURL(from_id, to_id){
	getE(to_id).value = str2url(getE(from_id).value.replace(/^[0-9]+\./, ''), 'UTF-8');
}

function friendlyURL(to_id){
	getE(to_id).value = str2url(getE(to_id).value.replace(/^[0-9]+\./, ''), 'UTF-8');
}

function img_txt(){
	var type = document.forms[0].type;
	var number = type.options[type.selectedIndex].value;
	if(number == 2){
		getE('image').style.display = 'none';
		getE('text').style.display = 'block';
	}
	if(number == 1){
		getE('text').style.display = 'none';
		getE('image').style.display = 'block';
	}
}
 
function getE(name)
{
	if (document.getElementById)
		var elem = document.getElementById(name);
	else if (document.all)
		var elem = document.all[name];
	else if (document.layers)
		var elem = document.layers[name];
	return elem;
}
 
function setLocation(url){
    window.location.href = url;
}

function confirmSetLocation(message, url){
    if( confirm(message) ) {
        setLocation(url);
    }
    return false;
}

function alertSystemMessage(message, chk){
	getE(chk).checked = true;
   	alert(message);
}

function deleteConfirm(message, url) {
    confirmSetLocation(message, url);
}

function setElementDisable(element, disable){
    if($(element)){
        $(element).disabled = disable;
    }
}

function toggleParentVis(obj) {
    obj = $(obj).parentNode;
    if( obj.style.display == 'none' ) {
        obj.style.display = '';
    } else {
        obj.style.display = 'none';
    }
}

function toggleVis(obj) {
    obj = $(obj);
    if( obj.style.display == 'none' ) {
        obj.style.display = '';
    } else {
        obj.style.display = 'none';
    }
}

function hide_show_values() {
  	var myindex  = getE('type').selectedIndex
    var SelValue = getE('type').options[myindex].value
	if(SelValue==3 || SelValue==5){
		Element.show('values_id');
	}else{
		 Element.hide('values_id');
	}
}

function helpWindow(text, width, height){
	var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	var x = parseInt(screen.width / 2.0) - (width / 2.0);
	var y = parseInt(screen.height / 2.0) - (height / 2.0);
	scroll = "no";
	if (isMSIE) {
		width += 35;
		height += 55;
	}
	var win = window.open('', "help", "top=" + y + ",left=" + x + ",scrollbars="+ scroll +",modal=yes,width=" + width + ",height=" + height + ",resizable=no");
	win.document.write('<link href="css/main.css" rel="stylesheet" type="text/css"><body style="margin:20px;"><h1 id="help">Ajutor</h1><p>'+text+'</p></body>');
	win.document.close();
	win.focus();
}

function toggleValueElements(checkbox, container){
    if(container && checkbox){
        var elems = container.getElementsBySelector('select', 'input');
        elems.each(function(elem){elem.disabled=checkbox.checked});
    }
}

/********** MESSAGES ***********/
/*
Event.observe(window, 'load', function() {
    $$('.messages .error-msg').each(function(el) {
        new Effect.Highlight(el, {startcolor:'#E13422', endcolor:'#fdf9f8', duration:1});
    });
    $$('.messages .warning-msg').each(function(el) {
        new Effect.Highlight(el, {startcolor:'#E13422', endcolor:'#fdf9f8', duration:1});
    });
    $$('.messages .notice-msg').each(function(el) {
        new Effect.Highlight(el, {startcolor:'#E5B82C', endcolor:'#fbf7e9', duration:1});
    });
    $$('.messages .success-msg').each(function(el) {
        new Effect.Highlight(el, {startcolor:'#507477', endcolor:'#f2fafb', duration:1});
    });
});
*/
function syncOnchangeValue(baseElem, distElem){
    var compare = {baseElem:baseElem, distElem:distElem}
    Event.observe(baseElem, 'change', function(){
        if($(this.baseElem) && $(this.distElem)){
            $(this.distElem).value = $(this.baseElem).value;
        }
    }.bind(compare));
}

/********** Ajax session expiration ***********/
