function confirmLink(theLink)
{
    var is_confirmed = confirm('Вы действительно \n желаете УДАЛИТЬ ?');
    return is_confirmed;
}

function DrawLocation(parent)
{
	var sel = '';
	var ret = '<select name="town"><option value="" style="color:#98916C;">Не важно</option>';
    arr = array.split("|");
	for (var i = 0; i < arr.length-1; i++) {
		arr[i] = arr[i].split("*");
		if (arr[i][1] == parent.value)  {
            ret += '<option value="'+arr[i][0]+'">'+arr[i][2]+'</option>';
        }
	}
	ret += '</select>';
	document.getElementById('t').innerHTML = ret;
}

function setPointer(theRow, thePointerColor)
{
    if (thePointerColor == '' || typeof(theRow.style) == 'undefined') {
        return false;
    }
    if (typeof(document.getElementsByTagName) != 'undefined') {
        var theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        var theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    for (var c = 0; c < rowCellsCnt; c++) {
        theCells[c].style.backgroundColor = thePointerColor;
    }
    return true;
}


function AddRes(id,txt)
{
	var tr, td, num;
    var num_ = 12;
	tbody = parent.document.getElementById("total");
    num = num_ - tbody.rows.length;
    if (num > 0) {
	    tr = tbody.insertRow(tbody.rows.length);
	    td = tr.insertCell(tr.cells.length);
	    td.innerHTML = '<table width="100%"><tr><td class="description">'+txt+'<input type="hidden" name="para[]" value="'+id+'"></td><td width="100%" class="bg_dotted"><img src="/i/0.gif" width="1" height="30"></td><td class="tot">'+(tbody.rows.length)+'</td></tr></table>';

        td = tr.insertCell(tr.cells.length);
	    td.setAttribute("align", "right");
        td.setAttribute("width", "20");
	    //td.onclick = function () {moveTo(this,"up"); Mesto();}
	    td.innerHTML = '<img onclick="moveTo(this,\'up\');Mesto();" src="/forum/images/buttons/collapse_thead.gif" border="0" title="вверх" style="cursor:pointer;">';

        td = tr.insertCell(tr.cells.length);
	    td.onclick = function () {moveTo(this,"dn"); Mesto();}
	    td.innerHTML = '<img onclick="moveTo(this,\'dn\');Mesto();" src="/forum/images/buttons/collapse_thead_collapsed.gif" border="0" title="вниз" style="cursor:pointer;">';

        td = tr.insertCell(tr.cells.length);
	    td.innerHTML = '&nbsp;<img src="/i/del.gif" border="0" width="9" height="8" alt="убрать" onclick="RemRes(\''+id+'\', \''+txt+'\'); DelT(this); Mesto();" style="cursor:pointer;">';

        //косметический подсчёт
        num = num_ - tbody.rows.length;
        parent.document.getElementById("ostalos_").innerHTML = 'осталось выбрать '+num;
        if (num <= 0) parent.document.getElementById("subb_").disabled=false;
    }
}

function moveTo(obj, how)
{
while (obj.tagName != 'TD') obj = obj.parentNode;
var num_tr_new;
var tbody = document.getElementById("total");
var num_tr = obj.parentNode.sectionRowIndex;

if (how == 'up') num_tr_new = num_tr-1;
if (how == 'dn') num_tr_new = num_tr+1;

if ((how == 'dn' && num_tr_new!=tbody.rows.length) || (how == 'up' && num_tr!=0)) {
    tbody.replaceChild(tbody.replaceChild(
    tbody.rows[num_tr_new].cloneNode(true),
    tbody.rows[num_tr]).cloneNode(true),
    tbody.rows[num_tr_new]);
    parent.document.getElementById("subb_").disabled=false;
    }
}

function RemRes(id,txt)
{
	var tr, td, num;
    var num_ = 12;
	tbody = total.document.getElementById("result");
    if (tbody) {
	    tr = tbody.insertRow(tbody.rows.length);
	    tr.onmouseover = function () {setPointer(this, '#FFF6E9');}
	    tr.onmouseout = function () {setPointer(this, 'transparent');}
	    td = tr.insertCell(tr.cells.length);
	    td.innerHTML = txt;
	    td = tr.insertCell(tr.cells.length);
	    td.innerHTML = '<img src="/forum/images/buttons/collapse_tcat.gif" title="добавить" onclick="AddRes(\''+id+'\',\''+txt+'\'); DelTR(this);" style="cursor:pointer;">';
        //косметический подсчёт
        num = num_ - document.getElementById("total").rows.length+1;
        if (parent.document.getElementById("subb_"))
        if (num > 0) {
            parent.document.getElementById("ostalos_").innerHTML = 'осталось выбрать '+num;
            parent.document.getElementById("subb_").disabled=true;
        }
        else parent.document.getElementById("ostalos_").innerHTML = '';
    }
}

function DelTR(this_)
{
    var num_ = 12;
	tbody = parent.document.getElementById("total");
    num = num_ - tbody.rows.length;
    if (num > 0) {
	    tr = this_;
	    while (tr.tagName != 'TR') tr = tr.parentNode;
	    tr.parentNode.removeChild(tr);
    }
}

function DelT(this_)
{
    tr = this_;
    while (tr.tagName != 'TR') tr = tr.parentNode;
    tr.parentNode.removeChild(tr);
}

function Mesto()
{
    var n = 1;
	for (var i = 0; i < document.all.length; i++) {
		if (document.all[i].className == 'tot') {
			document.all[i].innerHTML = n;
			n++;
			}
		}
}

function clearTable(tbody) {
    while (tbody.rows.length > 0) {
        tbody.deleteRow(0);
    }
}


// Moo funcitons

function sendForm(formId, reqId) {
    if (reqId == '') var reqId = 'req';
    var myAjax = new Ajax('/ajax.php', {data: $(formId), method: 'post', update: $(reqId)}).request();
}

function load(url, reqId)
{
    if (reqId == '') var reqId = 'req';
    // $(reqId).setHTML('<img src="/i/0.gif" width="1" height="16">');
    // $(reqId).addClass('ajax');
    var myAjax = new Ajax('/ajax.php', {method: 'post', data: url, update: reqId, onComplete: function(){$(reqId).removeClass('ajax');}}).request();
}

function send(url)
{
    var myAjax = new Ajax('/ajax.php', {method: 'post', data: url}).request();
}

function removeElem(elem) {
    if ($(elem)) $(elem).remove();
}

function krest(id, this_) {
    if ($('krest')) $('krest').remove();
    var w = 780;
    var h = 450;

    new Element('div', {
        'styles': {
            'left': window.getSize()['size']['x']/2 - w/2,
            'top': $(this_).getPosition()['y'] + 14,
            'width': w,
            'height': h
            },
        'id': 'krest'}
    ).setHTML('').injectInside($$('body')[0]);
    load('do=roundKrest&id='+id, 'krest');
}

function addCity(this_)
{
    removeElem('cityRow'); 
    new Element('li', {'id': 'cityRow'}).setHTML('Загрузка локаций').inject($('country').getParent().getParent());
    load('do=loadCity&country='+ this_.value, 'cityRow');
}




