function strpos( haystack, needle, offset){  
    // Finds position of first occurrence of a string within another    
    //   
    // version: 810.1317  
    // discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_strpos  
  
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // +   improved by: Onno Marsman      
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);  
    // *     returns 1: 14  
    var i = (haystack+'').indexOf( needle, offset );   
    return i===-1 ? false : i;  
} 

function serarchSpecOffer(locationn,city,district,room,m2,pricem2,price,zn){
  if(zn==1) window.location = locationn+"&city="+city+"&district="+district+"&room="+room+"&m2="+m2+"&pricem2="+pricem2+"&price="+price;
  else{
    var posS = strpos(locationn.toString(), '&city=', 0);
    var newLocationn = locationn.toString().substring(0,posS);
    window.location = newLocationn+"&city="+city+"&district="+district+"&room="+room+"&m2="+m2+"&pricem2="+pricem2+"&price="+price;
  }
}

function openWindow(link) // inspired by porneL - http://pornel.net/pups/
{
  try
  {
    var new_window = window.open(link, '_blank');
    if(new_window) return false;
  }
  catch(e){}
  return true;
}

function checkRequiredFields(form_id)
{
  ok = true;
  fields = document.getElementsByTagName('input');
  for(var i in fields) if(fields[i].className=='text required')
  {
    if(fields[i].parentNode.parentNode.parentNode.id==form_id && !fields[i].value) ok = false;
  }
  fields = document.getElementsByTagName('textarea');
  for(var i in fields) if(fields[i].className=='required')
  {
    if(fields[i].parentNode.parentNode.parentNode.id==form_id && !fields[i].value) ok = false;
  }
  if(ok)
  {
    document.getElementById('submit_'+form_id).disabled = 'disabled';
    return true;
  }
  alert('Musisz wypełnić wszystkie wymagane pola (oznaczone gwiazdką).');
  return false;
}

function setTopImage(img)
{
  document.getElementById('mainOfferImage').src = 'gfx/img/g_'+img;
  document.getElementById('mainOfferImageA').href = 'gfx/img/'+img;
  return false;
}

time = 5000;

function changeOffer()
{
  offers = document.getElementsByTagName('div');
  first_offer = 0;
  changed = false;
  for(var i in offers) if(offers[i].className=='so')
  {
    if(!first_offer) first_offer = i;
    if(offers[i].style.display=='block')
    {
      offers[i].style.display = 'none';
      changed = true;
    }
    else if(changed)
    {
      offers[i].style.display = 'block';
      changed = false;
    }
  }
  if(changed) offers[first_offer].style.display = 'block';
  setTimeout('changeOffer()', time);
}

function initializeCalculator()
{
  document.getElementById('fc_submit').style.display = 'none';
  setTimeout('updateCalculator()', 10);
}

function updateCalculator()
{
  price = Math.round(document.getElementById('fc_price').value * 100) / 100;
  brokerage = Math.round(document.getElementById('fc_brokerage').value * document.getElementById('fc_price').value) / 100;
  if(price>0 || brokerage>0)
  {
    tax1 = Math.round(price * 2) / 100;
    document.getElementById('fc_tax1').value = tax1;
    if(price <= 3000) tax2 = 100;
    else if(price <= 10000) tax2 = 100 + ((price-3000) * 0.03);
    else if(price <= 30000) tax2 = 310 + ((price-10000) * 0.02);
    else if(price <= 60000) tax2 = 710 + ((price-30000) * 0.01);
    else if(price <= 1000000) tax2 = 1010 + ((price-60000) * 0.005);
    else tax2 = (5710 + ((price - 1000000)*0.0025));
    tax2 = Math.round(tax2 * 100) / 100;
    if(document.getElementById('fc_property').value<3) tax2 = tax2 / 2;
    document.getElementById('fc_tax2').value = tax2;
    vat = Math.round(tax2 * 22) / 100;
    document.getElementById('fc_vat').value = vat;
    document.getElementById('fc_brokerage2').value = brokerage;
    brokerage_vat = Math.round(document.getElementById('fc_brokerage2').value * 22) / 100;
    document.getElementById('fc_brokerage_vat').value = brokerage_vat;
    if(document.getElementById('fc_property').value==1) payment = 0;
    else payment = 200;
    document.getElementById('fc_payment').value = payment;
    ap = Math.round((tax1 + tax2 + vat + brokerage + brokerage_vat + payment + 100) * 100) / 100;
    document.getElementById('fc_additional_payments').value = ap;
    sum = Math.round((price + ap) * 100) / 100;
    document.getElementById('fc_sum').value = sum;
  }
  setTimeout('updateCalculator()', 10);
}
