function openDescription(idTour) {
    $.post("/php/getDescription.php", "idTour="+idTour, requestOpenDescription, "xml");
}
function requestOpenDescription(xml) {
    $("div#popUp").empty();
    $("div#popUp").html("<div id=\"descriptionDialog\" title=\""+$("head",xml).attr("name")+"\">"+$("head",xml).text()+"</div>");
    $("#descriptionDialog").dialog({
        autoOpen: true,
        modal: true,
        width: 750,
        buttons: {"Закрыть": function() {$(this).dialog("close");}},
        close: function(event, ui) {$(this).dialog("destroy");}
    });
    $("input#dateTour").datepicker($.datepicker.regional["ru"]);
}
function validReg(reg_user_name, reg_password, reg_password1, reg_mail) {
    if (reg_user_name.length < 4) return "Слишком короткий логин";
    if (reg_password != reg_password1) return "Введенные пароли не совпадают";
    if (reg_password.length < 6) return "Слишком короткий пароль";
    if (!isValidEmail(reg_mail, true)) return "Неправильный формат электронной почты";
    return "ok";
}
function openRegForm(z) {
    $("#popUp").html("<div id=\"regForm\" title=\"Регистрация\">\n\
                      <form>\n\
		      <label for=\"reg_user_name\">Логин:</label><br/>\n\
		      <input type=\"text\" name=\"reg_user_name\" id=\"reg_user_name\"/><br/>\n\
		      <label for=\"reg_password\">Пароль:</label><br/>\n\
		      <input type=\"password\" name=\"reg_password\" id=\"reg_password\"/><br/>\n\
                      <label for=\"reg_password1\">Повторить пароль:</label><br/>\n\
		      <input type=\"password\" name=\"reg_password1\" id=\"reg_password1\"/><br/>\n\
                      <label for=\"reg_mail\">E-mail:</label><br/>\n\
                      <input type=\"text\" name=\"reg_mail\" id=\"reg_mail\"/><br/>\n\
	              <input type=\"hidden\" id=\"sendZ\" value=\""+z+"\"/>\n\
                      </form>\n\
                      </div>");
    $("#regForm").dialog({
        autoOpen: true,
        modal: true,
        width: 350,
        height: 370,
        buttons: {"Отправить": function() {
                                   reg_user_name = $("#reg_user_name").val();
                                   reg_password = $("#reg_password").val();
                                   reg_password1 = $("#reg_password1").val();
                                   reg_mail = $("#reg_mail").val();
                                   txt = validReg(reg_user_name, reg_password, reg_password1, reg_mail);
                                   z = $("#sendZ").val();
                                   if (txt != "ok") {
                                       errorUp(txt);
                                   } else {
                                       $.post("php/editUserTour.php", "reg_user_name="+reg_user_name+"&reg_password="+reg_password+"&reg_mail="+reg_mail+"&userDo=add&z="+z, requestEditUser, "xml");
                                       $(this).dialog("close");
                                       $("#popUp").empty();
                                   }    
                               },
                  "Отмена": function() {
                                $(this).dialog("close");
                            }
        }
    });
}
function errorUp(txt) {
    $("#errorUp").html("<div id=\"errorWin\" title=\"Внимание ошибка!\">"+txt+"</div>");
    $("#errorWin").dialog({
        autoOpen: true,
        modal: true,
        width: 380,
        height: 200,
        buttons: {"Ок": function() {
                            $(this).dialog("close");
                            $("#errorUp").empty();
                        }
        }
    });    
}
function isValidEmail (email, strict) {
    if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
    return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}
function testReg(idCurrent) {
    if (idCurrent != -1) {
        str = $("form#zakazTura").serialize();
        $.post("php/registrator.php", str+"&do=zayava&z=0", requestSendZayava, "xml");
    } else {
        openRegForm(1);
    }
}
function requestSendZayava(xml) {
    $("#errorUp").html("<div id=\"errorWin\" title=\"Внимание!\">"+$("head",xml).text()+"</div>");
    $("#errorWin").dialog({
        autoOpen: true,
        modal: true,
        width: 380,
        height: 300,
        buttons: {"Ок": function() {
                            $(this).dialog("close");
                            $("#errorUp").empty();
                            window.location.reload();
                        }
        }
    });
}
function logOut() {
    $.post("/php/logOut.php", "", requestLogOut, "xml");
}
function requestLogOut(xml) {
    window.location.reload();
}
