var BASE_URL = '/index.php/';
var COMFIRMATION_DELETE = "Are you sure you want to delete?";
var USER_DELETE_CONFIRMED = "User has been deleted successfully";
var USER_DELETE_FAILED ="User has not been deleted";
var LISTING_DELETE_CONFIRMED = "Listing has been deleted successfully";
var LISTING_DELETE_FAILED ="Listing has not been deleted";

function paymentSwitch(payment_method)
{
    if(payment_method =='credit_card')
    {
        window.location =BASE_URL+"user/payment";
    }
    else if(payment_method =='pay_pal')
    {
        window.location =BASE_URL+"user/pppayment";
    }
}

function importpaymentSwitch(payment_method)
{
    if(payment_method =='credit_card')
    {
        window.location =BASE_URL+"xmlimport/payment";
    }
    else if(payment_method =='pay_pal')
    {
        window.location =BASE_URL+"xmlimport/pppayment";
    }
}

function setAvailablity()
{
    $.ajax({
        type: "POST",
        url: BASE_URL+ "user/homeavailablity/save",
        data: $("#home_availablity").serialize(),

        success: function(msg)
        {
            $('#test').html(msg);
            fix();
        }
    });
}

function deleteAvailability(trackId,listingId)
{
    $.ajax({
        type: "POST",
        url: BASE_URL+ "user/deleteAvailability",
        data: "trackId="+trackId+"&listingId="+listingId,
        success: function(msg)
        {
            $('#test').html(msg);
            
            var ic = $('#inner_content').height();
            $('#content').css('height', ic );
            var c = $('#content').height();
            $('#sidebar').css('height', c );

        }
    });
}

function continentList()
{
    $.ajax({
        type: "POST",
        url: BASE_URL+ "worldlocations/getContinentList",
        success: function(msg)
        {
            //alert (msg);
            $('#continentList').html(msg);
        }
    });
}

function getCountryList(continentId)
{
    $('#countryimage').show();
    $.ajax({
        type: "POST",
        url: BASE_URL+ "worldlocations/getCountryList",
        data: "continentId="+continentId,
        success: function(msg)
        {
            $('#countryList').html(msg);
            $('#countryimage').hide();
        }
    });
}

function getStateList(countryId)
{
    $.ajax({
        type: "POST",
        url: BASE_URL+ "worldlocations/getStateList",
        data: "countryId="+countryId,
        success: function(msg)
        {
            $('#stateList').html(msg);
        }
    });
}

function getCityList(stateId,countryId)
{
    //alert(stateId);
    if(stateId=='na')
    {
        showDiv('state_not_available');
        return;
    }
    var result = stateId.split('|');

    $.ajax({
        type: "POST",
        url: BASE_URL+ "worldlocations/getCityList",
        data: "stateId="+result[1]+"&countryId="+countryId,
        success: function(msg)
        {
            $('#cityList').html(msg);
        }
    });
}

function getCityNa(city)
{
    if(city=='na')
        showDiv('city_not_available');
    fix();
    return;

}

function showDiv(divId)
{
    if(document.getElementById)
    { // DOM3 = IE5, NS6
        document.getElementById(divId).style.display = 'block';
    }
    else
    {
        if(document.layers)
        { // Netscape 4
            document.divId.style.display = 'block';
        }
        else
        { // IE 4
            document.all.divId.style.display = 'block';
        }
    }
}

function hideDiv(divId)
{
    if(document.getElementById)
    { // DOM3 = IE5, NS6

        document.getElementById(divId).style.display = 'none';
    }
    else
    {
        if(document.layers)
        { // Netscape 4
            document.divId.display = 'none';
        }
        else
        {   // IE 4
            document.all.divId.style.display = 'none';
        }
    }
}

function userList(page)
{

    $.ajax({

        type: "POST",
        url: BASE_URL+ "admin/user/searchUser",
        data: "page=" + page,
        success: function(msg)
        {
            $('#result_container').html(msg);
        }
    });
}

function deleteUser(userIdHash)
{
    var confirmDelete   = confirm(COMFIRMATION_DELETE);
    if (!confirmDelete) return;
    $.ajax(
    {
            type: "POST",
            url: BASE_URL+ "admin/user/deleteUser",
            data: "idhash=" + userIdHash,

            success: function(msg)
            {
                if(msg=='success')
                {
                    alert(USER_DELETE_CONFIRMED);
                    window.location =BASE_URL+"admin/user/searchUser/new";
                }
                else alert(USER_DELETE_FAILED);
            }
        }
        );
}

function editAccessLevel(idHash,step)
{

}

function addanotherDateRange()

{
    html = "<p>"+
    "<label for='fromdate'>My home is available from</label>"+
    "<input type='text'  value='' id='fromdate' name='fromdate[]' class='date'>"+
    "</p>"+
    "<br>"+
    "<p>"+
    "<label for='todate'>To</label>"+
    "<input type='text'  value='' id='todate' name='todate[]' class='date'>"+
    "</p>";
    $(html).appendTo("#home_availablity_date_picker");
    $('.date').datePicker();

}

function ajaxFileUpload()
{
    $("#loading").ajaxStart(function(){
        $(this).show();
    })
    .ajaxComplete(function(){
        $(this).hide();
    });

    $.ajaxFileUpload
    (
    {
            url:BASE_URL+'photoupload/upload',
            secureuri:false,
            fileElementId:'fileToUpload',
            dataType : "text",
            success: function(data)
            {
                $("#image_details_form_container").html(data);
            }
        }
        )
}

function sidebarFix()
{
    var h = $("#content").outerHeight() * 1.0 + 150;
    //alert(h)
    $("#sidebar").css("height",h) ;
}


var popUpWin=0;

function popUp(URLStr, left, top, width, height)
{
    if(popUpWin)
    {
        if(!popUpWin.closed) popUpWin.close();
    }
    popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=1,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function imagesubmitloading()
{
    $('#image_upload_form').hide();
    $('#loading_img').show();
    $('#image_upload_form').submit();
    var h = $('#sidebar').height()- 98;
    $('#sidebar').css('height', h );
}
function reload_(mode)
{
    alert(mode);
    window.location.href = window.location;
}

function fix()
{
    // run code
    offset=0;
    if($.browser.msie) offset =0;
    var h = $('#content').height()+ offset;
    $('#sidebar').css('height', h );
}

function switchforum(id)
{
    window.location.href = BASE_URL +'forums/subforum/'+id;
}

function displayImage(id)
{
    alert (id);
    $.ajax(
    {
            type: "POST",
            url: BASE_URL+ "listing/images",
            data: "id=" + id,
            success: function(msg)
            {
            // window.location =BASE_URL+"listing/images/"+id;
            //else alert(USER_DELETE_FAILED);
            }
        }
        );
}
function adminDeleteListing(listingId)
{
    var confirmDelete   = confirm(COMFIRMATION_DELETE);
    if (!confirmDelete) return;
    $.ajax(
    {
        type: "POST",
        url: BASE_URL+ "admin/homelisting/deleteListing",
        data: "id=" + listingId,
        success: function(msg)
        {
            if(msg==1)
            {
                alert(LISTING_DELETE_CONFIRMED);
                window.location.href = BASE_URL+ "admin/homelisting";
            }
            else alert(LISTING_DELETE_FAILED);

        }
    }
    );
}