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()
{
    //alert();
    $('#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()
{
    //alert();
    // run code
    /*
    offset=-3;
    if($.browser.msie) offset = -10;
    var h = $('#content').height() - 100;
    $('#sidebar').height(h);
    */
     if ($('#sidebar').height() && $('#content').height()) {
            if($.browser.msie)
            {
                createCSSClass('#sidebar', 'height:' + ($('#content').height()) + 'px!important');
            }
            else
            {
              $("#sidebar").css("cssText", "height:" + ($('#content').height()) + "px!important;");
            }
        }
}

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);

        }
    }
    );
}

function createCSSClass(selector, style)
{
 // using information found at: http://www.quirksmode.org/dom/w3c_css.html
 // doesn't work in older versions of Opera (< 9) due to lack of styleSheets support
 if(!document.styleSheets) return;
 if(document.getElementsByTagName("head").length == 0) return;
 var stylesheet;
 var mediaType;
 if(document.styleSheets.length > 0)
 {
  for(i = 0; i<document.styleSheets.length; i++)
  {
   if(document.styleSheets[i].disabled) continue;
   var media = document.styleSheets[i].media;
   mediaType = typeof media;
   // IE
   if(mediaType == "string")
   {
    if(media == "" || media.indexOf("screen") != -1)
    {
     styleSheet = document.styleSheets[i];
    }
   }
   else if(mediaType == "object")
   {
    if(media.mediaText == "" || media.mediaText.indexOf("screen") != -1)
    {
     styleSheet = document.styleSheets[i];
    }
   }
   // stylesheet found, so break out of loop
   if(typeof styleSheet != "undefined") break;
  }
 }
 // if no style sheet is found
 if(typeof styleSheet == "undefined")
 {
  // create a new style sheet
  var styleSheetElement = document.createElement("style");
  styleSheetElement.type = "text/css";
  // add to <head>
  document.getElementsByTagName("head")[0].appendChild(styleSheetElement);
  // select it
  for(i = 0; i<document.styleSheets.length; i++)
  {
   if(document.styleSheets[i].disabled) continue;
   styleSheet = document.styleSheets[i];
  }
  // get media type
  var media = styleSheet.media;
  mediaType = typeof media;
 }
 // IE
 if(mediaType == "string")
 {
  for(i = 0;i<styleSheet.rules.length;i++)
  {
   // if there is an existing rule set up, replace it
   if(styleSheet.rules[i].selectorText.toLowerCase() == selector.toLowerCase())
   {
    styleSheet.rules[i].style.cssText = style;
    return;
   }
  }
  // or add a new rule
  styleSheet.addRule(selector,style);
 }
 else if(mediaType == "object")
 {
  for(i = 0;i<styleSheet.cssRules.length;i++)
  {
   // if there is an existing rule set up, replace it
   if(styleSheet.cssRules[i].selectorText.toLowerCase() == selector.toLowerCase())
   {
    styleSheet.cssRules[i].style.cssText = style;
    return;
   }
  }
  // or insert new rule
  styleSheet.insertRule(selector + "{" + style + "}", styleSheet.cssRules.length);
 }
}