// Event attacher
// ----------------------------------------------

if(typeof(document.attachEvent) != 'function') {         

    document.events = [];
    document.attachEvent = function(type, reference) {
	    var evt = this.events;
	    if(!evt[type]) evt[type] = [];
	    evt[type][evt[type].length] = reference;

	    document[type] = function(e) { 
		    document.executeEvents(type, e);        
	    }
    }    

    document.executeEvents = function(type, e) {
	    for(var i in this.events[type]) {
		    this.events[type][i](e);
	    }    
    }
    window.onunload = function() { document.executeEvents('onunload'); }
    window.onload = function() { document.executeEvents('onload'); }
    window.onresize = function() { document.executeEvents('onresize'); }
    
}

function pageHit(){
                  
  p = escape(document.location.href);
  rfr = escape(document.referrer);
  if (rfr == "undefined"){ rfr = "";}
  sx = screen.width;
  sy = screen.height;
  sc = (navigator.appName.indexOf("Microsoft") > -1) ? screen.colorDepth : screen.pixelDepth;
  src = 'stats.php?p=' + p + '&rfr=' + rfr + '&sx=' + sx + '&sy=' + sy + '&sc=' + sc;
  src = '<img style="position:absolute;left:-10px;top:-10px;" src="' + src + '" width="1" height="1" border="0" alt="" />';
  document.getElementById('counter').innerHTML = src;  
  
}

document.attachEvent("onload", pageHit);

function setOrder(url_s, url_e)
{
	window.location = url_s + 'order/' + encodeMyHtml(document.getElementById('order_s').options[document.getElementById('order_s').selectedIndex].value) + '/' + url_e;
}

function setSearch(url_s, url_e)
{
	if (encodeMyHtml(document.getElementById('search').value) == '')
	{
		window.location = url_s + url_e;
	}
	else
	{
        var newLocation = url_s + '?search=' + encodeMyHtml(document.getElementById('search').value) + '' + url_e;
        //alert('url encoded! ' + newLocation);
		window.location = newLocation;
	}
}       
             
try{
    window.attachEvent("onload", init);     
}catch(e){};      
try{
    document.attachEvent("onload", init);     
}catch(e){};                       
         
// Init function
function init() {                     
    if (window.initLightbox) {
        initLightbox();                           
    }
}                           

function initLightbox() { myLightbox = new Lightbox(); }

function encodeMyHtml(value)
{
	encodedHtml = escape(value);
	encodedHtml = encodedHtml.replace(/\//g,"%2F");
	//encodedHtml = encodedHtml.replace(/\?/g,"%3F");
	//encodedHtml = encodedHtml.replace(/=/g,"%3D");
	//encodedHtml = encodedHtml.replace(/&/g,"%26");
    //encodedHtml = encodedHtml.replace(/@/g,"%40");      
    return encodedHtml;
} 

/**
*   Crossbrowser find position voor DOM object
**/
function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function stretchDivOrganisations() {
    iTop = findPos(document.getElementById('footer'))[1];
    
    iAbove = findPos(document.getElementById('organisations'))[1];
    
    document.getElementById('organisations').style.height = ( iTop - iAbove ) + "px";  

}

document.attachEvent("onload", stretchDivOrganisations);

function checkOrganisation(el) {
    var selected = el.selectedIndex;
    var name = el.options[selected].text;
    
    var textField = document.getElementById('new_organisation');
    if (name == 'Other ...') {
        textField.style.display = 'block';
    } else {
        textField.value = '';
        textField.style.display = 'none';    
    }
    
}
