$().ready( function() {

  if (document.referrer!=undefined && document.referrer!="") {    
    googleQuery = processReferrer();    
  } else {
    googleQuery = "";
  }
  
  if (googleQuery != undefined && googleQuery != '') {  
    $.ajax({
      type: "GET",
      url: "/nf/AjaxResponse/quickPosting",
      data: "categoryId=" + categoryId + "&locationId="+ locationId + "&referrer=" + googleQuery + "&rnd=" + Math.random(),
      success: function(resp){ 
        $("#" + quickPostingTarget).html(resp);
      }
    });
  }
});

function getProvinces(countryId, provinceId, target) {
  $.ajax({
    type: "GET",
    url: "/nf/AjaxResponse/getProvinces",
    data: "locationId=" + countryId + "&provinceId=" + provinceId,
    success: function(resp) {
      $(target).html(resp);
    }
  });
}

function showHiddens() {
  $("div.qfHidden").fadeIn("slow");
  disableKeyup();
}

function disableKeyup() { 
  $("#qfTitle").unbind("keyup")
  $("#qfDescription").unbind("keyup")
}

function addFileInput(id, txtFileInput, txtRemove, max) {
  if ($("div#files p").size() < max) {
    nId = id + 1;
    $("div#files").append("<p id='img_p_" + nId + "'><label for='image_" + nId + "' class='optional'>" + txtFileInput + "</label> <input type='file' name='image_" + nId + "' id='image_" + nId + "' onChange=\"addFileInput(" + nId + ", '" + txtFileInput + "', '" + txtRemove + "', "+ max +");\" /> <input type='button' value='" + txtRemove + "' onClick=\"$('p#img_p_" + nId + "').remove();\" name='remove_" + nId + "' /></p>");
  }
}

function autoSuggestCallback(obj) {

  var ids      = obj.id.split("|");
  var country  = ids[0] || 1;
  var province = ids[1] || 0;

  var countryCombo = $("select#qfCountry");

  $("select#qfCountry option:selected").removeAttr('selected');
  $("select#qfCountry option[value='" + country + "']").attr('selected', 'selected');

  getProvinces(country, province, $("#qfProvinces"));

  $("select#qfProvince option:selected").removeAttr('selected');
  $("select#qfProvince option[value='" + province + "']").attr('selected', 'selected');
}
