
function myreverse(value) {
    for (var reversed = '',i=value.length-1;i>-1;i=i-1) reversed += value.charAt(i);
    return reversed;
}
function filter_prijs(bedrag) {
    bedrag.replace(/[^0123456789,\.]/g, '');
    bedrag = bedrag.toString().replace(/\,/g, '.');

    if (! bedrag) {
        return "";
    }
    if (bedrag > 9999999) {
        return "9.999.999,00";
    }

    // helaas geen sprintf beschibaar. Dan maar met de hand formatteren.
    decimal = Math.round((bedrag * 100) % 100) + "";

    if (isNaN(decimal)) {
        return "";
    }

    // Vul nullen aan.
    while (decimal.length < 2) decimal = "0" + decimal;
    natural = Math.floor(bedrag) + "";
    if (!natural)
        natural = "0";
    return natural + "," + decimal;
}
function change_zoekbalk_form_action(url) {
    if ((url != '') && (url != '-')) {
        document.zoekbalk.action = '/' + url + '/';
    } else {
        document.zoekbalk.action = '/markt/';
    }
}

function inputfield_onfocus(element, content) {
    if (element.value == content) {
    element.value = '';
        element.setAttribute('class', ' ');
        element.className = ' ';
    }
}

$(document).ready(function(){

    // Add handlers after document is ready to be manipulated

    $("#pcpl").autocomplete("/callback/postalcode", {
      minChars: 2, width: 280, scrollHeight: 200,
          formatItem: function(row) {
          return row[0].replace(/^\d+:/gi, '');
        },
          formatResult: function(row) {
          return row[0].replace(/^\d+:/gi, '');
        }
      });

    // plaats_val is defined inline in site_header.tt, because it needs
    // template var interpolation

    $("#pcpl").result( function(event, data, formatted) {
        if (data) {
          $("#pc_id").val(data[0].replace(/:.+$/gi, ''));
          plaats_val = data[0].replace(/^\d+:/gi, '');
          $("#afstand").removeAttr("disabled");
          $("#afstand").focus();
        }
        else {
          $("#pc_id").val('');
          $("#afstand").attr("disabled","disabled");
        }
      });

    $("#pcpl").blur( function() {
        $("#pcpl").val(plaats_val);

        if ($("#pc_id").val() == '') {
          $("#afstand").attr("disabled","disabled");
        }
        else {
          $("#afstand").removeAttr("disabled");
        }
      });

  });
var img_cache = {
  'view': {},
  'normal': {},
  'preview': {}
};

function show_photo(nr, i, elem, imgname, width, height, phototype) {
    // Anything changed?
    if (document.getElementById("activephoto" + nr).src == "http://img.2dehands.be.thijso/f/" + phototype + "/" + imgname) {
        return;
    }

    var current_photo = document.getElementById("currentphoto" + nr);
    if (current_photo) { current_photo.id = "foobar"; };
    elem.id = "currentphoto" + nr;
    anchphoto = document.getElementById("activephotoa" + nr);
    curimg = document.getElementById("activephoto" + nr);

    curimg.style.visibility = 'hidden';
    anchphoto.href="http://img.2dehands.be.thijso/f/normal/" + imgname;
    anchphoto.rel="lyteshow[imgs];http://img.2dehands.be.thijso/f/tiny/" + imgname;

    if (img_cache[phototype][i] == undefined) {
      var img = new Image();
      img.src = "http://img.2dehands.be.thijso/f/" + phototype + "/" + imgname;
      img_cache[phototype][i] = img;
    }

    var img = img_cache[phototype][i];
    curimg.src = img.src;

    var l = Math.round((380/2) - ((width)/2));
    var t = Math.round((380/2) - ((height)/2));
    if (l > 0) {
      var left = l + 'px';
      curimg.style.left = left;
    }
    else {
      curimg.style.left = '0px';
    }
    if (t > 0) {
      var top = t + 'px';
      curimg.style.top = top;
    }
    else {
      curimg.style.top = '0px';
    }
    curimg.style.width = width + 'px';
    curimg.style.height = height + 'px';

    curimg.style.visibility = 'visible';
}

// We only need this for IE
function img_preload(nr,basesrc) {
  var img = new Image();
  img.src = "http://img.2dehands.be.thijso/f/normal/" + basesrc;
  img_cache['normal'][nr] = img;
}


// disable allow_bieden checkbox if 'gratis' option is selected
// enable again if another option is selected
function enableDisable(o, sName, b) {
    var o = o.form.elements[sName];
    if (o) {
        o.disabled = b;
        o.checked = !b;
    }
}
// onload function
// we need to keep allow_bieden disabled after a submit to the same page
function generic() {
    if (document.getElementById('prijs_gratis') && document.forms[0].allow_bieden) {
        if (document.getElementById('prijs_gratis').checked == true) {
            document.forms[0].allow_bieden.disabled = 1;
        }
    }
}
//setup onload function
if(typeof window.addEventListener != 'undefined') {
    // gecko, safari, konqueror and standard
    window.addEventListener('load', generic, false);
} else if(typeof document.addEventListener != 'undefined') {
    // opera 7
    document.addEventListener('load', generic, false);
} else if(typeof window.attachEvent != 'undefined') {
    // win/ie
    window.attachEvent('onload', generic);
}


