function defaultValue1(textarea,event)
{    
    if(event=='onfocus')
    {
        if(textarea.value=='Destination Keyword or Property ID'){textarea.value='';}                                                                                
    }
    else if(event=='onblur')
    {
        if(textarea.value=='') {textarea.value='Destination Keyword or Property ID';}
    }

}

function getgbs(id)
{
    $("#gbsdiv").fadeOut(500);
    var type=$("#type").val();                    
    $.ajax({
            type: "POST",
            url: base_url+"sth/sthc/getGBS/"+id+"/"+type,
            // data: "action=update",
            complete: function(data){
                    $("#gbsdiv").html(data.responseText).show();
                    $("#gbsdiv").fadeIn(500);
            }
    });
} 

function charRemaining(tField, nField, maxAllowed) {
 if (tField.value.length > maxAllowed) {
 tField.value = tField.value.substring(0, maxAllowed);
 }
 nField.value = maxAllowed - tField.value.length;
}

/*========================= get property images & show  it to users =============================*/
function getImages()
{
    $.ajax({
            type: "POST",
            url: base_url+"sth/sthc/getImagesAjax",
            // data: "action=update",
            complete: function(data){
                    $("#gallery").html(data.responseText).show();
            }
    });
}

/*========================= In amenities tab for(internet access) =============================*/
function unCheckBox(ID,ID1) {
 if (!document.getElementById(ID1).checked) {
 document.getElementById(ID).checked = false;
 }
 }
 function CheckBox(ID) {
 document.getElementById(ID).checked = true;
 }

function unCheckConnections()
{
    unCheckBox('internet_access_1','refcode_135');
    unCheckBox('internet_access_2','refcode_135');
    unCheckBox('internet_access_3','refcode_135');
    unCheckBox('internet_access_4','refcode_135');
}

/*================= Amenities ===============*/
function validateBedroom()
{                    
    if($("[name='king_qty']").val()=='0' &&
       $("[name='queen_qty']").val()=='0' &&
       $("[name='double_qty']").val()=='0' &&
       $("[name='twin_qty']").val()=='0' &&
       $("[name='child_bed_qty']").val()=='0' &&
       $("[name='baby_crib_qty']").val()=='0' &&
       $("[name='sleep_sofa_qty']").val()=='0')
       {return false}
       else
       {return true}
}

function validateBathroom()
{

    if($("[name='toilet']").is(':checked')==false &&
       $("[name='comb_tub_shower']").is(':checked')==false &&
       $("[name='tub']").is(':checked')==false &&
       $("[name='shower']").is(':checked')==false &&
       $("[name='jetter_tub']").is(':checked')==false &&
       $("[name='bidet']").is(':checked')==false &&
       $("[name='outdoor_shower']").is(':checked')==false)
       {return false}
       else
       {return true}
}

function resetBathroom()
{
   $("[name='toilet']").removeAttr("checked");
   $("[name='comb_tub_shower']").removeAttr("checked");
   $("[name='tub']").removeAttr("checked");
   $("[name='shower']").removeAttr("checked");
   $("[name='jetted_tub']").removeAttr("checked");
   $("[name='bidet']").removeAttr("checked");
   $("[name='outdoor_shower']").removeAttr("checked");
}

function resetBedroom()
{
       $("[name='king_qty'] option:selected");
       $("[name='queen_qty'] option:selected");
       $("[name='double_qty'] option:selected");
       $("[name='twin_qty'] option:selected");
       $("[name='child_bed_qty'] option:selected");
       $("[name='baby_crib_qty'] option:selected");
       $("[name='sleep_sofa_qty'] option:selected");
}

function getbathtable()
{
    $("#bathajax").fadeOut(500);
    $.ajax({
            type: "POST",
            url: base_url+"sth/sthc/getBathInfo",
            // data: "action=update",
            complete: function(data){

                    $("#bathajax").html(data.responseText).show();

                    $("#bathtext").attr({value:""});

                    $("#bathclick").attr({disabled:false, value:"Add"});

                    $("#bathajax").fadeIn(500);
            }
    });
}

function getbedtable()
{
    $("#bedajax").fadeOut(500);
    $.ajax({
            type: "POST",
            url: base_url+"sth/sthc/getBedInfo",
            complete: function(data){

                    $("#bedajax").html(data.responseText).show();

                    $("#bedclick").attr({disabled:false, value:"Add"});

                    $("#bedajax").fadeIn(500);
            }
    });
}

function getratetable()
{
    $("#ajaxrate").fadeOut(500);
    $.ajax({
            type: "POST",
            url: base_url+"sth/sthc/getRateInfo",
            // data: "action=update",
            complete: function(data){
                    //alert('display');                        
                    $("#ajaxrate").html(data.responseText).show();

                    $(".ratetext").attr({value:""});

                    $("#rateclick").attr({disabled:false, value:"Add"});

                    $("#ajaxrate").fadeIn(500);
            }
    });
}

function getdealstable()
{
    $("#dealsajax").fadeOut(500);
    $.ajax({
            type: "POST",
            url: base_url+"sth/sthc/getHotDealsInfo",
            complete: function(data){

                    $("#dealsajax").html(data.responseText).show();
                    $("#dealsajax").fadeIn(500);
            }
    });
}
                

function resetdealsinput()
{
       $("[name='hot_deals_desc']").html('');
       $("[name='from_date']").val('');
       $("[name='to_date']").val('');
}

jQuery().ready(function($){  
   
   $(".poplight").click(function(){       
       popupOpen($(this));
       
   });
   
   
   
   $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
          $('#fade , .popup_block').fadeOut(function() {
            $('#fade').remove();
            $('.error').css('visibility','hidden');
            $("div#map").css('display','block'); //in pdp page
    }); //fade them both out
   });
   
   $("#submit_location").click(function(){
        
        if($("#location").val()!='Type Destination or Property ID' && $("#location").val()!=''){
            var url1="Keywords:"+$("#location").val()+"/";
        
            $("#keywordSearchForm").attr("action",base_url+'vacation-rentals/'+url1);                    
            $("#keywordSearchForm").submit();
        }
    });
   
   
});
function popupOpen(link)
{
    var popID = link.attr('rel'); //Get Popup Name
    var popURL = link.attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({'width': Number( popWidth )}).prepend('');

    //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({ 
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 

    return false;
}

function validate_rates()
{
    if($('input[name=season_name]').val()=='')
    {
        alert('Please enter season name');
        return false;
    }
    else{
        var from_date = $('input[name=from_date]').val();                
        if(from_date == '')
        {
            alert('Please enter begin date');
            return false;
        }
        else
        {
            if(isValidDate(from_date))
            {
                var to_date = $('input[name=to_date]').val();                
                if(to_date == '')
                {
                    alert('Please enter end date');
                    return false;
                }
                else
                {
                    if(isValidDate(to_date))
                    {
                        if(createDate(from_date) > createDate(to_date))
                        {
                            alert('Begin date must be less than end date');
                            return false;
                        }
                        else
                        {
                            var weekly = $('input[name=weekly_rate]').val();
                            //weekly = parseInt(weekly);
                            
                            if(!isNaN(weekly))
                                return true;
                            else
                            {
                                alert('Please enter numeric value in weekly rates');    
                                return false;
                            }
                        }
                    }
                    else
                    {
                        alert('Please enter valid date format \'yyyy-mm-dd\'');
                        return false;
                    }                    
                }
            }
            else
            {
                alert('Please enter valid date format \'yyyy-mm-dd\'');
                return false;
            }
            alert('Please provide season name');
            return false;
        }
    }
    //console.log(isValidDate('1961-12-25'));    
}

function validate_hot_deal()
{
    var desc = $('#hotdealsform textarea[name=hot_deals_desc]').val();
    if(desc == '')
    {
        alert('Please enter hot deals description');
        return false;
    }
    else
    {
        var from_date = $('#hotdealsform input[name=from_date]').val();
        if(from_date == '')
        {
            alert('Please enter begin date');
            return false;
        }
        if(isValidDate(from_date))
        {
            var to_date = $('#hotdealsform input[name=to_date]').val();                
            if(to_date == '')
            {
                alert('Please enter end date');
                return false;
            }
            else
            {
                if(isValidDate(to_date))
                {
                    if(createDate(from_date) > createDate(to_date))
                    {
                        alert('Begin date must be less than end date');
                        return false;
                    }
                    else
                        return true;
                }
                else
                {
                    alert('Please enter valid date format \'yyyy-mm-dd\'');
                    return false;
                }                    
            }
        }
        else
        {
            alert('Please enter valid date format \'yyyy-mm-dd\'');
            return false;
        }
    }
    
        
}

function isValidDate(date)
{
    var matches = /^(\d{4})[-\/](\d{2})[-\/](\d{2})$/.exec(date);
    if (matches == null) return false;
    var d = matches[3];
    var m = matches[2] - 1;
    var y = matches[1];
    var composedDate = new Date(y, m, d);
    return composedDate.getDate() == d &&
            composedDate.getMonth() == m &&
            composedDate.getFullYear() == y;
}

function createDate(date)
{
    var matches = /^(\d{4})[-\/](\d{2})[-\/](\d{2})$/.exec(date);
    if (matches == null) return false;
    var d = matches[3];
    var m = matches[2] - 1;
    var y = matches[1];
    var composedDate = new Date(y, m, d);
    return composedDate;
}




