/**
 * Redirect
 */
function go(url) {
    if (url) window.location.assign(url);
}
/**
 * Open new window
 */
function wopen(url, w, h, res, scroll) {
    if (!url) {
        event.cancelBubble = true;
        event.returnValue = false;
        return;
    }
    target = '_blank';
    if (!w) w = 500;
    if (!h) h = 400

    w = parseInt(w, 10);
    h = parseInt(h, 10);

    var aw = screen.availWidth;
    var ah = screen.availHeight;
    if (w > aw) w = aw;
    if (h > ah) h = ah;

    if (res) r = 0;
    else r = 1;

    if (scroll) s = 0;
    else s = 1;

    var left = Math.round((aw - w)/2);
    var top = Math.round((ah - h)/2);

    var wd = window.open(url, target, 'channelmode=0, directories=0, fullscreen=0, height='+h+'px, width='+w+'px, location=0, menubar=0, resizable='+r+', scrollbars='+s+', status=0, toolbar=0, top='+top+'px, left='+left+'px');
}

/**
 * Check correct email format
 */
function em_ch(obj) {
    if (!obj) return false;

    var ch = /^[._a-z0-9-]+@[.a-z0-9-]+\.[a-z]{2,4}$/i;
    if (!ch.test(obj.value)) return false;

    return true;
}

/**
 * Replace double spaces and trailing spaces
 */
function replace( obj ) {
    var r1 = /^( )+/g;
    var r2 = /( )+$/g;
    var r3 = /( {2})+/;

    obj.value = obj.value.replace(r1, '');
    obj.value = obj.value.replace(r2, '');
    obj.value = obj.value.replace(r3, ' ');
}

/**
 * Show subcategories
 */
function show ( id ) {
	for (i in ids) {
		el = document.getElementById('sub_'+ids[i]);
		el.className = 'subcat_off';
	}
	el = document.getElementById(id);
	el.className = 'subcat_on';
	return true;
}

/**
 * Send POST request (AJAX)
 */
function sending_post_request(request) {
    var req = new Subsys_JsHttpRequest_Js();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.responseJS) {
                if (req.responseJS.error) {
                    set_status(req.responseJS.error);
                } else {
                    set_status('Ready for operations...');
                    maingo(resurl);
                }
            }
        }
    }
    req.caching = false;
    req.open('POST', '/admins/action.php', true);
    req.send(request);
}


/**
 * Get download link
 */
function get_download_link(post_id) {
	var req = new Subsys_JsHttpRequest_Js();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.responseJS.res && download_timed_out == false) {
				sec = 0;
				request_ok = true;
				
			    timer = document.getElementById('download_timer');
				timer.style.display = 'none';	
				
				res_div = document.getElementById('download_result');
				res_div.style.display = 'block';
				res_div.innerHTML = req.responseJS.res;
			}
			if (req.responseJS.link && download_timed_out == false) {
				window.setTimeout('window.location.assign("'+req.responseJS.link+'")', 0);
			}
		}
	}
	req.caching = false;
	req.open('POST', '/download_prepare/'+post_id+'.html', true);
	req.send({ nzb_post_id: post_id });
}
