function CheckCity()
{
    var val=$("input:radio[name=city]:checked").val();
    if(val==1 && !$("#inet").attr("checked")) $("#li_metro").css("display", "");
    else $("#li_metro").css("display", "none");
}

function CreateCookie(name, value, expiredays)
{
    var todayDate = new Date();
    todayDate.setDate(todayDate.getDate() + expiredays);
    document.cookie = name + ' = ' + value + '; expires=' +      
          todayDate.toGMTString() + ';';
}
 
function ReadCookie(cookiename)
{
    var numOfCookies = document.cookie.length;
    var nameOfCookie = cookiename + "=";
    var cookieLen = nameOfCookie.length;
    var x=0;
    while (x <= numOfCookies)
    {
        var y = (x + cookieLen);
        if (document.cookie.substring(x, y) == nameOfCookie)
        return(ExtractCookieValue (y));
        x = document.cookie.indexOf(" ", x) + 1;
        if (x == 0)
        break;
    }
    return null;
}

function ExtractCookieValue(val)
{
    if ((endOfCookie = document.cookie.indexOf(";", val)) == -1)
    {
        endOfCookie = document.cookie.length;
    }
    return unescape(document.cookie.substring(val, endOfCookie));
}

function OpenClose(_id)
{
    var displ=$("#"+_id).css("display");
    if(displ=="none") $("#"+_id).css("display", "");
    else $("#"+_id).css("display", "none");
}

/*$(document).ready(function()
{
	$(".line").click();
});*/

function CheckLength(_id, _kol)
{
    $("#"+_id).val().length>_kol
    {
        $("#"+_id).val($("#"+_id).val().substring(0, _kol))
    }
}

function CheckShareValue()
{
    if($("#share_value").val()=="")
        $("#share_value").val("0");
}

var flag_do=false;
function AddCenter()
{
    val=$("#sel_centers").val();
    if(val==0)
    { /*window.location="?Page=add_center";*/
        if(!flag_do)
        {
            $("#center_add").css("display", "");
            $("#center_add").dialog({ draggable: false, height: 260, width:530, modal:true, resizable:false });
            flag_do=true;
        }
        else
            $("#center_add").dialog('open');
    }
}
function CheckInet()
{
    if($("#inet").attr("checked"))
    {
        $("#li_metro").css("display", "none");
        $("#li_city").css("display", "none");
        $("#tc").css("display", "none");
        $("#sel_metro").val("не выбрано");
        $("#sel_centers").val("не выбрано");
        $("#ttable").css("display", "none");
        $("#inp_ttable").val("");
        $("#inet_addr").text("Интернет-адрес* : ");
    }
    else
    {
        $("#li_city").css("display", "");
        $("#li_metro").css("display", "");
        $("#ttable").css("display", "");
        $("#inet_addr").text("Адрес* : ");
        $("#tc").css("display", "");
    }
    CheckCity();
}

function SendCenterInfo()
{
    _cname=$("#cname").val(); if (jQuery.trim(_cname)=='') {alert("\"Название\"-обязательное поле");return false;}
    _caddress=$("#caddress").val(); if (jQuery.trim(_caddress)=='') {alert("\"Адрес\"-обязательное поле");return false;}
    _cmetro_id=$("#cmetro_id").val(); if (_cmetro_id<=0) {alert("\"Ближайшее метро\"-обязательное поле");return false;}
    _cttable=$("#cttable").val(); if (jQuery.trim(_cttable)=='') {alert("\"График работы\"-обязательное поле");return false;}
    $.post("index.php", {name:_cname, address:_caddress, metro_id:_cmetro_id, ttable:_cttable,
            ajax:true, action:"add_center" }, function (data){CloseDialog(data, _cname);});
}

function CloseDialog(_id, _name)
{
    $("#sel_centers").append( $("<option value=\""+_id+"\">"+_name+"</option>") );
    $("#sel_centers").val(_name);
    $("#center_add").dialog("close");
    document.forms['add_center_form'].reset();
}

function IncScore(_id, _name, _action)
{
    new_score=$('input:radio[name='+_name+_id+']:checked').val();
    if(new_score)
    {
        var ids=new Array();
        var ids_tmp=ReadCookie("ids");
        if(ids_tmp!=null)
        {
            ids=ids_tmp.split("|");
            
            for(var j=0; j<ids.length; j++)
            {
                if(ids[j]==_id) 
                {
                    alert("Вы уже голосовали сегодня за эту фотографию.");
                    return false;
                }
            }
            ids.push(_id);
        } else ids.push(_id);
        CreateCookie("ids", ids.join("|"), 1);
    
        new_score=parseInt(new_score);
        
        $.post("index.php", {id:_id, score:new_score, ajax:true, action:_action}, 
            function(data)
            {
                if(data==1)
                {
                    val=$("#score"+_id).text();
                    val_int=parseInt(val);
                    new_score+=val_int;
                    $("#score"+_id).text(new_score.toString());
                }
                else alert("Вы уже голосовали сегодня за эту фотографию.");
            }
        );
    }
}
