var bidUpdater;
var map;
var geoCoder;
var auctionCenter;


function empCalculate() {
  var label, price;
  var empAskingPrice, empDownPayment;
  var isOK = true;
  var res = '';

var interest		 = document.getElementById('empInterestRate').value;
var empAskingPrice   = document.getElementById('empAskingPrice').value;
var empDownPayment   = document.getElementById('empDownPayment').value;

var newInter=interest.split('_');

if(empAskingPrice == null || empAskingPrice.length==0){
	 alert("Enter Sales price");
}
if(empDownPayment == null || empDownPayment.length==0){
	 alert("Enter empDownPayment ");
}
if(interest == null || interest.length==0){
	 alert("Select Interest ");
}
  empAskingPrice = empAskingPrice.replace(",","");
  empDownPayment = empDownPayment.replace(",","");
  //var interest = $F('empInterestRate').split('_');
  
   /* ['empAskingPrice', 'empDownPayment'].each(function(e) {
    label = $('label-' + e);
    label.removeClassName('error');
    
    price = number_clear($F(e));
  if (!isPositiveMoney(price)) {
      elementAddError([e]);
      isOK = false;
    }
    else {
      $(e).value = number_format(price, 0);
      eval(e + ' = ' + price);
    }
  });*/
  
 // if (isOK)
  //res = '$' + number_format(getEmp(interest[0] * 12, interest[1], empAskingPrice - empDownPayment), 0);
    res =  getEmp(newInter[0] * 12, newInter[1], empAskingPrice - empDownPayment);    
//alert(empAskingPrice - empDownPayment);
   if(isNaN(res)){res =0;}
	document.getElementById('emp-result').innerHTML= '$' +res;
  //$('emp-result').update(res);//
}

// monthes, percent, amount
function getEmp(term, rate, pay) {
	   
  if (pay <= 0)
    return 0;
  rate = rate / 100 / 12;
  var ir = Math.pow(1 + rate, term);
  return Math.ceil((rate * ir * pay) / (ir - 1)); 
}

function watchingAdd() {
  if (isBuyer) {
    Effect.Fade('watch-property');
    new Ajax.Request(url.get('watchingAdd'), {
      parameters: {
        id: auctionID, 
        redirect: false
      },
      onSuccess: function(transport) {
        $('auction-watch-property-container').update(lang.get('alreadyWatching'));
      }
    });
  }
  else
    changePage(url.get('loginBuyer'));  
}

function contactSeller() {
  $('contact-seller-sent').hide();
  if (isLogged)
    Effect.toggle($('contact-seller-container'), 'blind', {duration:.5});
  else
    changePage(url.get('loginNotLogged'));  
} 

function contactSellerSend() {
  $('contact-seller-sent').hide();
  if (elementCheck(['subj', 'body']))
  {
    new Ajax.Request(url.get('contactSeller'), {
      parameters: { 
        id: auctionID,
        subj: $F('subj'), 
        body: $F('body')
      },
      onSuccess: function(transport) {
        Effect.Fade($('contact-seller-container'), {duration:.5});
        Effect.Appear($('contact-seller-sent'), {duration:.5, queue: 'end'});
      }
    });
  }
}

function initBidUpdater(interval) {
  bidUpdater= new PeriodicalExecuter(function(bu) {
    var b = $('bids');
    var p = $('price');
    var m = $('auction-minbid');
    var bc = $('bid-count');
    var an = $('auction-note');
    var bp = $('buyer-position-container');
    var countDownText = '';

    if (!b) return false;

    new Ajax.Request(url.get('auctionBids'), {
      parameters: {
        id: auctionID,
        bidCount: bc.innerHTML
      },
      onSuccess: function(transport) {
        if ($('new-bid-count'))
          $('new-bid-count', 'new-price', 'new-auction-note', 'new-auctionActive', 'new-minbid', 'new-buyer-position').invoke('remove');

        b.update(transport.responseText + b.innerHTML);
        var i = 0;
        b.select('.auction-bid').each(function(e) {
          if (i < st.get('lastBidCount'))
            Effect.Appear(e, {duration:.5});
          else
            Effect.Fade(e, {duration:.5});
          i = i + 1;
        });

        if (bc && bc.innerHTML != $('new-bid-count').innerHTML) {
          bc.hide();
          bc.update($('new-bid-count').innerHTML);
          Effect.Appear(bc, {duration:.5});
        }

        if (p && p.innerHTML != $('new-price').innerHTML) {
          p.hide();
          p.update($('new-price').innerHTML);
          Effect.Appear(p, {duration:.5});
        }
        
        if (m && m.innerHTML != $('new-minbid').innerHTML) {
          m.hide();
          m.update($('new-minbid').innerHTML);
          Effect.Appear(m, {duration:.5});
          minBid = parseInt(number_clear($('new-minbid').innerHTML).substr(1), 10);
        }
        
        if (bp && bp.innerHTML != $('new-buyer-position').innerHTML) {
          bp.hide();
          bp.update($('new-buyer-position').innerHTML);
          Effect.Appear(bp, {duration:.5});
        }

        switch ($('new-auction-note').innerHTML) {
          case '2': countDownText = lang.get('countDownText1'); break;
          case '1': countDownText = lang.get('countDownText2'); break;
          default: countDownText = ''; break;
        }

        if ($('new-auctionActive').innerHTML == '0') {
          new Ajax.Request(url.get('auctionCloseFinished'), {
            onSuccess: function(transport) {
              changePage(window.location.href);
            }
          });
        }
        else if (countDownText.stripTags() != an.innerHTML.stripTags()) {
          an.update(countDownText);
          if ($('bid-countdown-text'))
            Effect.Pulsate($('bid-countdown-text'), {duration:5, pulses: 5});
        }
      }
    }); 
  }, interval);
  return bidUpdater;
}

function mapSetCenter(latLng) {
  if (!latLng) return false;
  
  var auctionIcon = new GIcon({
    image: mapHomeIcon.get('url'),
    iconSize: new GSize(mapHomeIcon.get('width'), mapHomeIcon.get('height')),
    iconAnchor: new GPoint(8, 8),
    infoWindowAnchor: new GPoint(8, 8)
  });
  var marker = new GMarker(latLng, {icon: auctionIcon});
  auctionCenter = latLng;
  map.setCenter(latLng, 13);
  map.addOverlay(marker);
}


function showBidForm() {
  Effect.toggle($('auction-bid-container'), 'blind', {duration:.5});
  Effect.toggle($('auction-bidform-container'), 'blind', {duration:.5, queue: 'end'}); 
}

