function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function mypop( uri, name, w, h ) {
  var l = (screen.width) ? (screen.width-w)/2 : 0;
  var t = (screen.height) ? (screen.height-h)/2 : 0;
  var features = 'height=' + h + ',width=' + w + ',top=' + t + ',left=' + l + ',scrollbars=0,resizable';
  window.open(uri,name,features);
}  
  function findObj(object) {
    if(document.all) myObject=eval('document.all[object]');
    else if(document.layers) myObject=eval('document.layers[object]');
    else if(document.getElementById && !document.all) myObject=eval('document.getElementById(object)');
    else return;
    return myObject;
  }
  function getObj(object) {
    return findObj( object );
  }  
  
  var view_X = 0;
  var view_Y = 0;
  var mouse_Xpos = 0;
  var mouse_Ypos = 0;
  var	page_X = 0;
  var	page_Y = 0;
  var scroll_X = 0;
  var scroll_Y = 0;
  var elmLeft = 0;
  var elmWidth = 0;
  var elmTop = 0;
  var IE = document.all?true:false

  /*
  if (!IE) {
    document.captureEvents(Event.MOUSEMOVE)
    document.captureEvents(Event.MOUSEUP);
  }
  document.onmousemove = getMouseXY;
  document.onmouseup = clickHandler;  
  */
  function clickHandler (evt) {
    if (document.layers)
      alert(evt.target + '1: ' + evt.target.constructor + ': ' + evt.target.id);
    else if (window.event && window.event.srcElement) {
      elmWidth = findPosX( window.event.srcElement );
      elmLeft = window.event.srcElement.offsetWidth;
      elmTop = findPosY( window.event.srcElement );
    } else if (evt && evt.stopPropagation && !window.opera) {
      if (evt.target.nodeType == 1) {
        elmWidth = findPosX( evt.target );
        elmLeft = evt.target.offsetWidth;
        elmTop = findPosY( evt.target );
      } else 
        alert(evt.target.parentNode + ': ' + evt.target.parentNode.nodeName + ': ' + evt.target.parentNode.id)
    }
    else if (window.opera && evt)
      alert(evt.target + '3: ' + evt.target.tagName + ': ' + evt.target.id);
  
    return true;
  }

  function findPosX(obj) {
  	var curleft = 0;
  	if (obj.offsetParent) {
  		while (obj.offsetParent) {
  			curleft += obj.offsetLeft
  			obj = obj.offsetParent;
  		}
  	} else if (obj.x)
  		curleft += obj.x;
  	//alert( curleft );
  	return curleft;
  }

  function findPosY(obj) {
  	var curtop = 0;
  	if (obj.offsetParent) {
  		while (obj.offsetParent) {
  			curtop += obj.offsetTop
  			obj = obj.offsetParent;
  		}
  	} else if (obj.y)
  		curtop += obj.y;
  	return curtop;
  }
  
  function getPageDims() {
    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight
    if (test1 > test2) {
    	page_X = document.body.scrollWidth;
    	page_Y = document.body.scrollHeight;
    } else {
    	page_X = document.body.offsetWidth;
    	page_Y = document.body.offsetHeight;
    }
    if( !IE ) {
      view_X = window.innerWidth;
      view_Y = window.innerHeight;
    } else {
      view_X = document.body.clientWidth;
      view_Y = document.body.clientHeight;
    }
  }
  
  // Main function to retrieve mouse x-y pos.s
  function getMouseXY(e) {
    if (IE) {
      mouse_Xpos = event.clientX + document.body.scrollLeft
      mouse_Ypos = event.clientY + document.body.scrollTop
    } else {
      mouse_Xpos = e.pageX
      mouse_Ypos = e.pageY
    }  
    // catch possible negative values in NS4
    if (mouse_Xpos < 0){mouse_Xpos = 0}
    if (mouse_Ypos < 0){mouse_Ypos = 0}  

    //get scrollo
    if (self.pageYOffset) {
    	scroll_X = self.pageXOffset;
    	scroll_Y = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
    	scroll_X = document.documentElement.scrollLeft;
    	scroll_Y = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers {
    	scroll_X = document.body.scrollLeft;
    	scroll_Y = document.body.scrollTop;
    }
    return true
  }
  
  function showCalendar( field ) {
    closeCalendar();
    getPageDims();
    
    //define help box width and height
    var helpWidth = 200;
    var helpHeight = 180;
    
    //now create our div
    var div = document.createElement( 'div' );
    div.style.width = helpWidth + 'px';
    div.style.Height = helpHeight + 'px';
    div.id = 'calendardiv_popup';
    
    
    //bepaal de xpos
    if( view_X + scroll_X - ( elmLeft + elmWidth ) > helpWidth )
      xpos = elmLeft + elmWidth + 10;
    else
      xpos = elmWidth - helpWidth - 10;    
    //bepaal de ypos
    if( view_Y + scroll_Y - elmTop > ( helpHeight / 2 ) )
      ypos = mouse_Ypos;
    else
      ypos = elmTop - ( helpHeight / 2 ) + 10;

    
    div.style.position = 'absolute';
    div.style.left = xpos + 'px';
    div.style.top  = ypos + 'px';

    //fill the calendar
    getCalendarContent( field );

    document.getElementsByTagName( 'body' )[0].appendChild( div );
    

    return true;
  }
  
  function getCalendarContent( field, year, month ) {
    year = isDefined( year ) ? year : '';
    month = isDefined( month ) ? month : '';
    
    FL.responseHandler = 'showCalendarContent';
    FL.handle( 'retrieve', '/modules/tools/calendar.php?field=' + field + '&year=' + year + '&month=' + month );    
  }
  
  function showCalendarContent( response ) {
    findObj( 'calendardiv_popup' ).innerHTML = URLDecode( response );
    fixPNG();
  }


  
  function closeCalendar() {
    if( document.getElementById( 'calendardiv_popup' )  )
      document.getElementsByTagName( 'body' )[0].removeChild( document.getElementById( 'calendardiv_popup' ));
    return true;  
  }



  
  function selectCalendarDate( field, day, month, year ) {
    if( isObject( findObj( field + '_day' ) ) )
      findObj( field + '_day' ).value = day;  
    if( isObject( findObj( field + '_month' ) ) )
      findObj( field + '_month' ).value = month;      
    if( isObject( findObj( field + '_year' ) ) )
      findObj( field + '_year' ).value = year;    
    
    closeCalendar();
  }

  function isFunction(a) { return typeof a=='function'; }
  function isObject(a) { return a&&typeof a=='object'||isFunction(a); }
  function isArray(a) { return isObject(a)&&a.constructor==Array; }
  function isUndefined(a) { return typeof a=='undefined'; }
  function isDefined(a) { return !isUndefined(a); }
  function isString(a) { return typeof a=='string'; }
  function isDigit(num) { if (num.length>1) return false; var string="1234567890"; return string.indexOf(num)!=-1; }

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


  function URLDecode( encoded )
  {
     // Replace + with ' '
     // Replace %xx with equivalent character
     // Put [ERROR] in output if %xx is invalid.
     var HEXCHARS = "0123456789ABCDEFabcdef"; 
     var plaintext = "";
     var i = 0;
     while (i < encoded.length) {
         var ch = encoded.charAt(i);
  	   if (ch == "+") {
  	       plaintext += " ";
  		   i++;
  	   } else if (ch == "%") {
  			if (i < (encoded.length-2) 
  					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
  					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
  				plaintext += unescape( encoded.substr(i,3) );
  				i += 3;
  			} else {
  				alert( 'Bad escape combination near ...' + encoded.substr(i) );
  				plaintext += "%[ERROR]";
  				i++;
  			}
  		} else {
  		   plaintext += ch;
  		   i++;
  		}
  	} // while
     
     return plaintext;
  }
function fixPNG() {
  
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])
  
  if ((version >= 5.5) && (document.body.filters)) 
  {
     for(var i=0; i<document.images.length; i++)
     {
        var img = document.images[i]
        var imgName = img.src.toUpperCase()
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
         var imgID = (img.id) ? "id='" + img.id + "' " : ""
         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
         var imgStyle = "display:inline-block;" + img.style.cssText 
         if (img.align == "left") imgStyle = "float:left;" + imgStyle
         if (img.align == "right") imgStyle = "float:right;" + imgStyle
         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
        
         var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
         img.outerHTML = strNewHTML

         i = i-1
      }
    }
  }
  
}  

function voteReview( rid, yn ) {
  
  var cook = getCookie( 'votes' );
  
  if( cook == null )
    cook = rid;
  else
    cook+= ',' + rid;
  
  setCookie( 'votes', cook, 365 );
  
  $.get( '/modules/tools/reviewvote.php?id=' + rid + '&yn=' + yn, function( data ) {
    if( data.length > 0 )
      $('#vote-' + rid ).html( data );   
  });
}

function setCookie( cookieName, cookieValue, nDays ) {
 var today = new Date();
 var expire = new Date();
 if( nDays==null || nDays == 0 ) 
   nDays = 1;
 
 expire.setTime( today. getTime() + 3600000 * 24 * nDays );
 document.cookie = cookieName + "=" + escape( cookieValue ) + ";expires=" + expire.toGMTString();
}


function getCookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;  
  
  if( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
    return null;  
  
  if( start == -1 ) 
    return null;
  
  var end = document.cookie.indexOf( ';', len );
  
  if( end == -1 ) 
    end = document.cookie.length;
  
  return unescape( document.cookie.substring( len, end ) );
}

function toggleRegions() {
  $('#narrow-regions').slideToggle();  
}