/* Types
 * vf - video files
 * ve - video embeds
 * pf - picture files
 * pe - picture embeds
 * -- onpaste, IE only
 */
var objMultiFields = {
  vf : { count : 1, name : "video_files[]", label : "Video file", ftype : "file", fsize : 30, fevent : 'onchange', fclass : 'isFile=video', ftitle : 'Video file'},
  ve : { count : 1, name : "video_emb[]", label : "Video emb", ftype : "text", fsize : 43, fevent : 'onpaste', fclass : 'isURL', ftitle : 'Embed video' },
  pf : { count : 1, name : "pic_files[]", label : "Pic file", ftype : "file", fsize : 30, fevent : 'onchange', fclass : 'isFile=image', ftitle : 'Picture' },
  pe : { count : 1, name : "pic_emb[]", label : "Pic emb", ftype : "text", fsize : 43, fevent : 'onpaste', fclass : 'isURL', ftitle : 'Embed picture' }
};
/*
 * Create additional fields with button "delete"
 * @param string type of MultiField : vf, ve, pf, pe
 */
function addMultiField(type) {
  var inf = objMultiFields[type];
  var objContainer = document.getElementById(type + "Container");
  var objLabel = document.createElement("LABEL");
  var objField = document.createElement("INPUT");
  objLabel.id = type + "Label" + inf.count;
  objLabel.innerHTML = _formatLabel(type, inf.count);
  objContainer.appendChild(objLabel);
  objField.id = type + "Field" + inf.count;
  objField.name = inf.name;
  objField.type = inf.ftype;
  objField.size = inf.fsize;
  objField.className = inf.fclass;
  // for validator
  objField.title = inf.ftitle;
  // for validator

  objField[inf.fevent] = function() {
    addMultiField(type);
  };
  objContainer.appendChild(objField);
  inf.count++ ;
}
/* *
 * Delete created additional fields
 */
function delMultiField(type, index) {
  var inf = objMultiFields[type];
  var objContainer = document.getElementById(type + "Container");
  var objLabel = document.getElementById(type + "Label" + index);
  var objField = document.getElementById(type + "Field" + index);
  objLabel.parentNode.removeChild(objLabel);
  objField.parentNode.removeChild(objField);
  /* fix numbers, ids */
  if (index < inf.count - 1) {
    for (var i = index + 1; i < inf.count; i ++ ) {
      // alert(i);
      objLabel = document.getElementById(type + "Label" + i);
      objField = document.getElementById(type + "Field" + i);
      objLabel.id = type + "Label" + (i - 1);
      objField.id = type + "Field" + (i - 1);
      objLabel.innerHTML = _formatLabel(type, i - 1);
    }
  }
  inf.count -- ;
}
// common "delete" label format
function _formatLabel(type, index) {
  return '<a href="javascript:delMultiField(\''+type+'\','+index+')" style="float: none" >&times;</a> ' + objMultiFields[type].label + ' ' + (index + 1);
}
// change rent label text by sale / rent type
function changeRentLabel(goal) {
  $("rentLabel").innerHTML = (goal.value == 'sale') ? "* Sale Price" : "<nobr>* Monthly Rent</nobr>";
  $("monthlyrent").title = (goal.value == 'sale') ? "Sale Price" : "Monthly rent";
}
/* ------------------------------ ADDRESS -------------------------------- */
var AddressMode = typeof AddressMode == "undefined" ? 0 : 1;
//  0 - simple, 1 - advanced
var AddressResult = typeof AddressResult == "undefined" ? 0 : 1;
// 0 - not founded, 1 - founded
// switch Address mode
function switchAddressMode(mode)
{
  if (mode == AddressMode) return false;
  AddressMode = mode;
  if (AddressMode == 0)
  {
    // simple
    $("fsAddrAdv").style.display = "none";
    $("fsAddrSimple").style.display = "";
    $("aAddrAdv").innerHTML = "advanced";
    $("aAddrSimple").innerHTML = "<u>simple</u>";
  }
  else
  {
    // advanced
    $("fsAddrAdv").style.display = "";
    $("fsAddrSimple").style.display = "none";
    $("aAddrAdv").innerHTML = "<u>advanced</u>";
    $("aAddrSimple").innerHTML = "simple";
  }
}
function switchUPanel(a) {
  if ($('divUPanel').style.display == '') {
    $('divUPanel').style.display = 'none';
    a.innerHTML = 'show';
  } else {
    $('divUPanel').style.display = '';
    a.innerHTML = 'hide';
  }
  if (AddressMode == 0) {
    $('addrSimple').focus();
  } else {
    $('address').focus();
  }
}

function searchAddress()
{
  var addr = $('addrSimple').value;
  var a = addr.split(",");
  if (a.length < 3) return;
  if (addr.length > 0) NMap.searchAddress(addr, onFinishAddressSearch);
}
function onFinishAddressSearch(r)
{
  if ( !r)
  {
    //$('address').value = '';
    //$('city').value = '';
    //$('state').value = '';
    //$('zip').value = '';
    //$('neighborhood').value = '';
    //$('coords').value = '';
    $('addrError').style.color = "red";
    $('addrError').innerHTML = "Address not found";
    AddressResult = 0;
  }
  else
  {
    $('address').value = r.address;
    $('addrSimple').value = r.address_full;
    $('city').value = r.city;
    $('state').value = r.state;
    $('zip').value = r.zip;
    $('neighborhood').value = r.neighborhood;
    $('neighborhood_id').value = r.neighborhood_id;
    $('coords').value = r.coords;
    $('addrError').style.color = "green";
    $('addrError').innerHTML = "Ok";
    AddressResult = 1;
  }
}
function onSelectNeighborhood(n, point)
{
  // Map event
  $('address').value = '';
  $('city').value = "New York";
  $('state').value = "NY";
  $('zip').value = "";
  $('neighborhood').value = n.name;
  $('neighborhood_id').value = "";
  $('coords').value = point;
  copyAdv2Simple();
  if (AddressMode == 0) {
    $('addrSimple').focus();
  } else {
    $('address').focus();
  }
  // alert("Enter address");
}
function copySimple2Adv()
{
  var a = $('addrSimple').split(",");
  $('address').value = typeof(a[0]) != "undefined" ? a[0].trim() : "";
  $('city').value = typeof(a[1]) != "undefined" ? a[1].trim() : "";
  if (typeof(a[2] != "undefined")) {
    var s = a[2].split(" ");
    $('state').value =  s[0].trim() + (typeof(s[1]) != "undefined" ? " "+s[1] : "");
  }
}
function copyAdv2Simple()
{
  var s = $('address').value;
  //s += (s != "" ? ", " : "") + $('neighborhood').value;
  s += (s != "" ? ", " : "") + $('city').value;
  s += (s != "" ? ", " : "") + $('state').value +" " + $('zip').value;
  $('addrSimple').value = s;
}
String.prototype.trim = function()
{
  var s = this.toString();
  s = s.replace(/^\s+/, "");
  s = s.replace(/\s+$/, "");
  return s;
}


/* validate Form
function checkUploadForm(form) {
if(uploadFormSubmitted) {alert('Listing submitted already!');  return false; }
var error = false;
for(var i = 0; i < form.elements.length; i ++ ) {
var e = form.elements[i];
if (typeof(e.ready) != "undefined" && e.ready == false) error = true;
}
if (error) alert("You've entered value not existing in database");
return ! error;
}
*/

var suggesters = new Array();
var perc = null;
var fee = null;

function tocent(a)
{
  fee = a;
  var ca = parseInt(a);
  var fa = parseFloat(a);
  var dp = fa - ca;
  dp = Math.round(parseInt(dp * 1000) / 10) / 100;
  dp = dp + "";
  if(dp.length == 3)dp = dp + "0";
  if(dp == "0")dp = "";
  else dp = dp.substr(1);
  return (ca + dp);
}

function topercent(a)
{
  var perc = a;
  var ca = parseInt(a);
  var fa = parseFloat(a);
  var dp = fa - ca;
  dp = Math.round(parseInt(dp * 1000) / 10) / 100;
  return (ca + dp);
}

function n_load()
{
  /*
  suggesters["state"] = new set_suggest('state', GetState, true);
  suggesters["city"] = new set_suggest('city', GetCity, true);
  suggesters["cross_street1"] = new set_suggest('cross_street1', GetStreet1, false);
  suggesters["cross_street2"] = new set_suggest('cross_street2', GetStreet1, false);
  suggesters["neighborhood"] = new set_suggest('neighborhood', GetNbh, true);
  */

  $('bfee_type').onchange = function()
  {
    if($('bfee_type').value == 0)
    {
      $('bfee_sign').innerHTML = '$';
      if($('monthlyrent').value && $('bfee').value)$('bfee').value = tocent($('monthlyrent').value * perc / 100);
    }
    else
    {
      $('bfee_sign').innerHTML = '%';
      if($('monthlyrent').value != 0 && $('bfee').value)
      $('bfee').value = topercent(fee / parseFloat($('monthlyrent').value) * 100);
    }
  }

  $('monthlyrent').onchange = $('bfee').onchange = function()
  {
    perc = fee = $('bfee').value;
  }
  ;
  if( ! fee)fee = $('bfee').value;
}
addLoadEvent(n_load);