var http_request = false;
var dest_id;

function makeAjaxObject() {
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) {
	    // set type accordingly to anticipated content type
	    //http_request.overrideMimeType('text/xml');
	    http_request.overrideMimeType('text/html');
	}
    } else if (window.ActiveXObject) { // IE
	try {
	    http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	    try {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e) {}
	}
    }
    return http_request;
}

function alertContents() {
    if (http_request.readyState == 4) {
	if (http_request.status == 200) {
	    result = http_request.responseText;
	    if(result.match("timeoutnologin") == null) {
		document.getElementById(dest_id).innerHTML = result;
	    } else {
		window.top.location=window.location;
	    }
	} else {
	    alert('Error, please try again!');
	}
    }
}

function post_content(dest, url, form_id) {
    http_request = makeAjaxObject();
    if (!http_request) {
	alert("AJAX not supported.");
	return;
    }
    dest_id = dest;
    var full_url = '/modules/ajax.php' + url;
    var poststr = '';
    var myform = document.getElementById(form_id).elements;

    for(var n = 0; n < myform.length; n++) {
	if(myform[n].name) {
	    if(myform[n].type == 'checkbox') {
		if(myform[n].checked == true) {
		    poststr += escape(encodeURIComponent(myform[n].name));
		} else {
		    continue;
		}
	    } else {
		poststr += escape(encodeURIComponent(myform[n].name));
	    }
	} else {
	    continue;
	}
	poststr += '=';
	poststr += escape(encodeURIComponent(myform[n].value));
	if((n+1)<myform.length) poststr += '&';
    }

    http_request.onreadystatechange = alertContents;
    http_request.open('POST', full_url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", (poststr.length));
    http_request.setRequestHeader("Connection", "close");
    http_request.send(poststr);
}

function get_content(dest, url) {
    http_request = makeAjaxObject();
    if (!http_request) {
	alert("AJAX not supported.");
	return;
    }
    dest_id = dest;
    var full_url = '/modules/ajax.php' + url;

    http_request.onreadystatechange = alertContents;
    http_request.open('GET', full_url, true);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(null);
}

function nyit(obj) {
    mindet_csuk();
    var target = document.getElementById(obj);
    if (target.style.display == "none") {
	target.style.display = "";
    }
}

function mindet_csuk() {
    var divs, n;
    divs = document.body.getElementsByTagName('div');
    for(n=0; n<divs.length; n++) {
	if(divs[n].id.substring(0,4)=='dom_') {
	    document.getElementById(divs[n].id).innerHTML = "";
	    document.getElementById(divs[n].id).style.display = "none";
	}
    }
}

function get_users(domain, dest, ss) {
    if(domain == '') domain='x';
    get_content(dest, "?us="+domain+'&ss='+ss);
}

function get_torlendo_adat(domain, dest) {
    if(domain == '') domain='x';
    get_content(dest, "?dd="+domain);
}

function get_dlist(dest, u) {
    get_content(dest, "?dlist="+u);
}

function toggle_chk() {
    chk=document.getElementsByName("files[]");
    for(n=0;n<chk.length;n++) if(chk[n].checked) { chk[n].checked=false; } else { chk[n].checked=true; }
}

function load_pls_content(dest, pls, domain) {
    get_content(dest, '?d='+domain+'&pls='+pls);
}

function pls_add(dest, pls, file, domain) {
    get_content(dest,'?d='+domain+'&p='+pls+'&f='+file+'&act=add');
}

function pls_remove(dest, pls, file, domain) {
    get_content(dest,'?d='+domain+'&p='+pls+'&f='+file+'&act=del');
}

