function gebi(id) {
    return document.getElementById(id);
}

function delCookie(name) {
    setCookie(name, '', 0);
}
function setCookie(cookieName, cookieValue, nDays) {
    var today  = new Date();
    var expire = new Date();
    if (nDays == null) {
       nDays=1;
    }
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}
 
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}


function previewBig(a) {
    img = new Image();
    img.src = a.href;
    if (img.height != 0 && img.width != 0) {
        window.open(img.src, 'w', 'height=' + img.height + ',width=' + img.width);
    }
    else {
        alert('Большое изображение недоступно');
    }
    return false;
}

function getXMLHttpObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        alert("Your browser does not support AJAX.");
    }
    return false;
}

function sendAjaxRequest(url, callback, sync) {
    if (typeof(sync) == 'undefined') {
        sync = false;
    }
//    alert(url);
    req  = getXMLHttpObject();
    if (!req || !url) {
        return false;
    }
//    req.onreadystatechange = callback;
    req.open('GET', url, sync);
    req.send(null);
    i = 0;
    while(req.readyState != 4 ) {
        if ( i++ > 1000000 ) break;
    }
    
    callback();
    return true;
}

function reloadSelect(id, data, onchangefunc) {
    sel = gebi(id); //document.createElement('select');
    if (!sel) {
        //window.status += id + ' NF.';
	return;
    }
    sel.options.length = 0;
//sel.options = new Array();
/*  
    i = 0;
    while(sel.options[i]) {
        sel.options[i] = null;
	i++;
    }
/* */
/**
* data array is array of array(value, title)
*/
//    alert(data[0][1]);
try {
    for(key in data) {
        if (typeof(data[key]) == 'function') continue;
//    alert(key+data[key]);
        sel.options[key] = new Option(data[key][1], data[key][0]);
//        sel.options[key] = '2';
    }
}
catch(e) {
    alert('fill error');
//    for(key in e) alert(key + e[key]);
}
    if (sel.options.length > 0) {
        sel.disabled = false;
    }
    sel.onchange = onchangefunc;
}
function loadToSelect(data) { //req, selectid, firstoption, onchangefunc) {
        req = data[4];
	i = 0;
    while(req.readyState != 4 ) {
        if ( i++ > 1000000 ) break;
        //window.status = req.readyState;
    }
//    alert(req.responseText);
//alert(1);
//    if (req.readyState == 4) {
        data = dataset;
	selectid = data[1];
	firstoption = data[2];
	onchangefunc = data[3];
        tmp = req.responseText.split('|');
	tmp.sort();
//    alert(req.responseText);
	data = new Array();
	data.push(new Array('', firstoption));
	for(key in tmp) {
            if (typeof(data[key]) == 'function') {
//	        window.status += '.';
                continue;
	    }
            if (parseInt(key) != key) {
//	        window.status += '.';
                continue;
            }
//	    window.status += key + '.';
            //if (key == 'inArray') continue;
	    data.push(new Array(tmp[key], tmp[key]));
	}
	reloadSelect(selectid, data, onchangefunc);
//    }
}
									    

function PageQuery() {
    q = window.location.search;
    if(q.length > 1) {
        this.q = q.substring(1, q.length);
    }
    else {
        this.q = null;
    }
    this.keyValuePairs = new Array();
    if(q) {
        for(var i=0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
        for(var j=0; j < this.keyValuePairs.length; j++) {
            if(this.keyValuePairs[j].split("=")[0] == s) {
                return this.keyValuePairs[j].split("=")[1];
            }
        }
        return false;
    }
    this.getParameters = function() {
        var a = new Array(this.getLength());
        for(var j=0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; }
}


