var checkNa = false;
var curStatus ='';
var url = window.location.host;
var idPrice = 0;
var idFeature = 0;


/**
* @descr    Submit the edit hotel form
*/
function funcAddSubmit( val )
{
    error='';
    
    $("input[@id^='uname']").each(
        function(i)
        {
            var reg_repl=/\s/;
            if(( this.type == 'text' ) && (this.value == ''))
            {
                this.value = $("#name"+i).val().replace(reg_repl, '_');
            }
            
            var reg_test=/\W/;
            
            if ((reg_test.test(this.value)))      //(this.value.length > 26) || 
            {
                this.style.background="#ffdddd";
                error = error + "<font color='red'>Names for url can contain only [A-Za-z0-9_]</font><br />";
            }
        }
    );
    
    $("input[@id^='name']").each(
        function(i)
        {
            if(( this.type == 'text' ) && (this.value == ''))
                error = error + "<font color='red'>Name (" + this.name + ") cann't be empty</font><br />";
        }
    );
      
    if( checkNa )
    {
        error = error  + "<font color='red'>You cann't take this name</font><br />";
    }
    
    if( $("#district").val() == '0' )
    {
        error = error + "<font color='red'>Select the district</font><br />";
    }
    
    $("input[@id^='location']").each(
        function(i)
        {
            if(( this.type == 'text' ) && (this.value == ''))
                error = error + "<font color='red'>Location (" + this.name + ") cann't be empty</font><br />";
        }
    );
    
    $("input[@id^='descr']").each(
        function(i)
        {
            if(( this.type == 'text' ) && (this.value == ''))
                error = error + "<font color='red'>Description (" + this.name + ") cann't be empty</font><br />";
        }
    );
    
    $("input[@id^='hotel_email']").each(
        function(i)
        {
            if(( this.type == 'text' ) && (this.value == ''))
                error = error + "<font color='red'>Hotel email (" + this.name + ") cann't be empty</font><br />";
            else
            {
                if (!validateEmail(this.value))
                    error = error + "<font color='red'>Incorrect e-mail (" + this.name + ")</font><br />";
            }
        }
    );
        
    if( error == '' )
    {
        $("form").submit();
    }
    else
    {
        $("#errors").html(error);
    }
}

/**
* @descr    Submit the edit hotel form
*/
function funcSubmit( val )
{
    error='';
    
    $("input[@id^='uname']").each(
        function(i)
        {
            var reg_repl=/\s/;
            if(( this.type == 'text' ) && (this.value == ''))
            {
                this.value = $("#name"+i).val().replace(reg_repl, '_');
            }
            
            var reg_test=/\W/;
            
            if ((reg_test.test(this.value)))                 //(this.value.length > 26) || 
            {
                this.style.background="#ffdddd";
                error = error + "<font color='red'>Names for url can contain only [A-Za-z0-9_]</font><br />";
            }
        }
    );
    
    
    $("input[@id^='name']").each(
        function(i)
        {
            if(( this.type == 'text' ) && (this.value == ''))
                error = error + "<font color='red'>Name cann't be empty</font><br />";
        }
    );
      
    if( checkNa )
    {
        error = error  + "<font color='red'>You cann't take this name</font><br />";
    }
    
    if( $("#district").val() == '0' )
    {
        error = error + "<font color='red'>Select the district</font><br />";
    }
    
    if( $("#currency").val() == '0' )
    {
        error = error + "<font color='red'>Select the currency</font><br />";
    }
    
    $("input[@id^='location']").each(
        function(i)
        {
            if(( this.type == 'text' ) && (this.value == ''))
                error = error + "<font color='red'>Location (" + this.name + ") cann't be empty</font><br />";
        }
    );
    
    $("input[@id^='descr']").each(
        function(i)
        {
            if(( this.type == 'text' ) && (this.value == ''))
                error = error + "<font color='red'>Description (" + this.name + ") cann't be empty</font><br />";
        }
    );
    
    $("input[@id^='hotel_email']").each(
        function(i)
        {
            if(( this.type == 'text' ) && (this.value == ''))
                error = error + "<font color='red'>Hotel email (" + this.name + ") cann't be empty</font><br />";
            else
            {
                if (!validateEmail(this.value))
                    error = error + "<font color='red'>Incorrect e-mail (" + this.name + ")</font><br />";
            }
        }
    );
    
    //alert($("#edit_hotel_form").validate().form()+ "==");
    if (!$("#edit_hotel_form").validate().form())
    {
        error = error + "<font color='red'>Incorrect coordinates</font><br />";
    }
    
    if( error == '' )
    {
        $("form").submit();
    }
    else
    {
        $("#errors").html(error);
    }
}

function filter_form_subm(mode, url, uclass)
{
    document.getElementById('filter_form').action = 'http://' + url + '/admin/viewHotels/search/0/' + mode + '/' + $("#" + mode).val() + '/' + uclass + '.htm'; 
    document.getElementById('filter_form').submit();    
}

function search_form_subm(url, uclass)
{
    document.getElementById('search_form').action = 'http://' + url + '/admin/viewHotels/search/0/name/' + $("#search_name").val() + '/' + uclass + '.htm'; 
    document.getElementById('search_form').submit();    
}

/**
* @descr    delete hotel image using ajax
*/
function DeleteFile( name, id, num )
{

    $.post(
        "http://"+url+"/admin/index.php",
        {
            s:  "ajax", 
            ev: "delHotelImage",
            name: name,
            id: id
        },
        function( data )
        {
            if( data )
            {
                $('#file_'+num).empty();
            }
        }
    );
}

/**
* @descr    delete hotel price using ajax
*/
function DeletePrice( id, num )
{
    $.post(
        "http://"+url+"/admin/index.php",
        {
            s:  "ajax", 
            ev: "delHotelPrice",
            id: id
        },
        function( data )
        {
            if( data )
            {
                $('#price_'+num).empty();
            }
        }
    );
}

/**
* @descr    delete hotel feature using ajax
*/
function DeleteHotelFeature( id, num )
{
    $.post(
        "http://"+url+"/admin/index.php",
        {
            s:  "ajax", 
            ev: "delHotelFeature",
            id: id
        },
        function( data )
        {
            if( data )
            {
                $('#feature_'+num).empty();
            }
        }
    );
}

/**
* @descr    add the area to input new hotel price
*/
function addPrice(langs)
{
    var i = idPrice+1;
    $('#div_price'+idPrice).before('<div id="div_price'+i+'"></div>');
    var html = "<table width='90%'>";
    for (li = 0; li < langs.length; li++)
    {
        html += "<tr><td>Name - " + langs[li] + "</td><td colspan='2'><input type='text' name='price[" + langs[li] + "][" + i + "]' id='price" + i + "_" + langs[li] + "' /></td></tr>";
    }
    html += "<tr><td>From </td><td><input type='text' name='low_price[" + i + "]' id='low_price" + i + "' />";
    html += "To<input type='text' name='high_price[" + i + "]' id='high_price" + i + "' />";
    html += "</td><td align='right'><input type='button' id='del_price" + i + "' value='Delete' onclick='deleteCurPrice(" + i + ")'/></td></tr></table><br>";
    $('#div_price'+i).html(html);
    idPrice++;
}

/**
* @descr    free the area of hotel price
*/
function deleteCurPrice(id)
{
    $('#div_price'+id).empty();
}

/**
* @descr    add the area to select new hotel feature
*/
function addHotelFeature(features)
{
    var i = idFeature+1;
    $('#div_feature'+idFeature).before('<div id="div_feature' + i + '"></div>');
    var html = "<table width='90%'><tr><td><select id='hotel_feature" + i + "' name='hotel_feature[" + i + "]'>"
    html += "<option value='0'>Select feature</option>";
    for (li = 0; li < features.length; li++)
    {
        html += "<option value='" + features[li]['id'] + "'>" + features[li]['name'] + "</option>";
    }
    html += "</select></td><td align='right'> <input type='button' id='del_feature" + i + "' value='Delete' onclick='deleteCurHotelFeature(" + i + ")'/></td></tr></table>";
    $('#div_feature'+i).html(html);
    idFeature++;
}

/**
* @descr    free the area of hotel feature
*/
function deleteCurHotelFeature(id)
{
    $('#div_feature'+id).empty();
}

function randomSpecialOffer()
{
    $.post(
        "http://"+url+"/index.php",
        {
            s:  "ajax", 
            ev: "randomSpecialOffer"
        },
        function( data )
        {
            //alert(data);
            if( data )
            {
                //alert('44'); 
                $('#special_offer').html(data);
            }
        }
    );   
    return false; 
}

function randomHotelProposal()
{
    $.post(
        "http://"+url+"/index.php",
        {
            s:  "ajax", 
            ev: "randomHotelProposal"
        },
        function( data )
        {
            //alert(data);
            if( data )
            {
                //alert('44'); 
                $('#hotel_proposal').html(data);
            }
        }
    );   
    return false; 
}




function changeTowns(country_id)
{
    $.post(
        "http://"+url+"/admin/index.php",
        {
            s:  "ajax", 
            ev: "changeTowns",
            id: country_id
        },
        function( data )
        {
            $('#town').html(data);
        }
    );   
    return false; 
}

function changeDistricts(town_id)
{
    $.post(
        "http://"+url+"/admin/index.php",
        {
            s:  "ajax", 
            ev: "changeDistricts",
            id: town_id
        },
        function( data )
        {
            $('#district').html(data);
        }
    );   
    return false; 
}



function loadReservation(cont, country, town, district, hotel, submit)
{
    $('#hotel_descrip').html('<h3>Loading...</h3>');
    //$('#loading_mess').html('');
    /*$("#loading_mess").animate({
        height: 'toggle', opacity: 'toggle'
        }, "slow");   */
    $.post(
        "http://"+url+"/index.php",
        {
            s:  "ajax", 
            ev: "loadReservation",
            contin: cont,
            country: country,
            town: town,
            district: district,
            hotel: hotel,
            submit: submit
        },
        function( data )
        {
            //document.body.scrollTop = 10;
            $('#hotel_descrip').html(data);
            $('#hotel_url').attr('style','display:block'); 
            //$('#loading_mess').html('');
        }
    );   
    return false; 
}

function loadContactManager(hotel_id, is_special)
{
    $('#hotel_descrip').html('<h3>Loading...</h3>');

    $.post(
        "http://"+url+"/index.php",
        {
            s:  "ajax", 
            ev: "contactManager",
            hotel_id: hotel_id,
            is_special: is_special,
            submit: 0
        },
        function( data )
        {
            $('#hotel_descrip').html(data);
            $('#hotel_url').attr('style','display:block'); 
        }
    );   
    return false; 
}


function funcContactSubmit()
{
    error='';
    check_captcha = false; 
    $("#check_captcha").html("<font color='red'>verification...</font>");
    
    if( $("#h_name").val() == '' ) 
    {
        error = error + "<font color='red'>Hotel Name cann't be empty</font><br />";
    }
    
    if( $("#city_country").val() == '' ) 
    {
        error = error + "<font color='red'>City, Country cann't be empty</font><br />";
    }
    
    if( $("#y_name").val() == '' ) 
    {
        error = error + "<font color='red'>Your name cann't be empty</font><br />";
    }
    
    if( $("#y_email").val() == '' ) 
    {
        error = error + "<font color='red'>Email cann't be empty</font><br />";
    }
    else
    {   
        if (!validateEmail($("#y_email").val()))
        {
            error = error + "<font color='red'>Incorrect e-mail</font><br />"; 
        }
    }
    
    if( $("#comment").val() == '' ) 
    {
        error = error + "<font color='red'>Comments cann't be empty</font><br />";
    }

    if( $("#imgcode").val() == '' ) 
    {
        error = error + "<font color='red'>Security code cann't be empty</font><br />";
        $("#check_captcha").html("");
    }
    else
    {
        $.post(
            "http://"+url+"/check_captcha.php",
            { 
                code: $("#imgcode").val() 
            },
            function( data )
            {
                if( data == '0' )
                {
                    $("#check_captcha").html("<font color='red'>Security code is incorect</font>");
                    check_captcha = false;
                }
                if (data == '1')
                {
                    $("#check_captcha").html("<font color='red'>right</font>");
                    check_captcha = true;

                    if ( error == '' )
                    {
                        $.post(
                        "http://"+url+"/index.php",
                        {
                            s:  "ajax", 
                            ev: "ContactManager",
                            hotel_email: $('#hotel_email').val(),
                            h_name: $('#h_name').val(),
                            city_country: $('#city_country').val(),
                            y_name: $('#y_name').val(),
                            y_email: $('#y_email').val(),
                            y_phone: $('#y_phone').val(),
                            comment: $('#comment').val(),  
                            submit: 1
                        },
                        function( data )
                        {
                            $('#hotel_descrip').html(data);
                            //$("form").submit(); 
                        }
                        );
                    } 
                }
            }
        );
    }
    if (( error == '' ) && (check_captcha == true))
    {
        $.post(
        "http://"+url+"/index.php",
        {
            s:  "ajax", 
            ev: "ContactManager",
            hotel_email: $('#hotel_email').val(),
            h_name: $('#h_name').val(),
            city_country: $('#city_country').val(),
            y_name: $('#y_name').val(),
            y_email: $('#y_email').val(),
            y_phone: $('#y_phone').val(),
            comment: $('#comment').val(),  
            submit: 1
        },
        function( data )
        {
            $('#hotel_descrip').html(data);
        }
        );   
    }
    else
    {
        $("#errors").html(error);
    }
}
