var months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
		'September', 'October', 'November', 'December'];

function addslashes (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // +   improved by: Nate
    // +   improved by: Onno Marsman
    // +   input by: Denny Wardhana
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: 'kevin\'s birthday'
 
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\u0000/g, "\\0");
}

function trim (str, charlist) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: DxGx
    // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'
    // *     example 3: trim(16, 1);
    // *     returns 3: 6
 
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

// string buffer
function StringBuffer() { 
	this.buffer = []; 
} 

StringBuffer.prototype.append = function append(string) { 
	this.buffer.push(string); 
	return this; 
}; 

StringBuffer.prototype.toString = function toString() { 
	return this.buffer.join(""); 
}; 

// Array.contains function
Array.prototype.contains = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
}

// generic function for form validation
var checkNow = false;
function registerValidator(target, validator, use_callbacks) {
    var element = document.getElementById(target);
    if (element) {
        var validationID = element.id + "_validationResult";
        var validationSpan = document.createElement('span');
        validationSpan.id = validationID;
        
        element.parentNode.appendChild(validationSpan);
        
        var f = null;
        if (use_callbacks) {
            f = function() {
                var callback = function(result) {
                	if (result) {
    	                if (result.ok) {
    	                    validationSpan.innerHTML = '<img src="/media/valid.gif" width="12" />';
    	                } else {
    	                    validationSpan.innerHTML = '<img src="/media/invalid.gif" width="12" />';
    	                    if (result.message) {
    	                        validationSpan.innerHTML += '<br />' + result.message;
    	                    }
    	                }
                    }
                };
                
                validator(element, callback);
            };
        } else {
            f = function() {
                var result = validator(element);
                
                if (result) {
	                if (result.ok) {
	                    validationSpan.innerHTML = '<img src="/media/valid.gif" width="12" />';
	                } else {
	                    validationSpan.innerHTML = '<img src="/media/invalid.gif" width="12" />';
	                    if (result.message) {
	                        validationSpan.innerHTML += '<br />' + result.message;
	                    }
	                }
                }
            };
        }
        if (element.addEventListener) {
            element.addEventListener("blur", f, false);
            element.addEventListener("keyup", f, false);
        } else if (element.attachEvent) {
            element.attachEvent("onblur", f);
            element.attachEvent("onkeyup", f);
        }
        
        if (checkNow) {
            f();
        }
    }
}

//ad stuff
sas_tmstp=Math.round(Math.random()*10000000000);sas_masterflag=1;
function SmartAdServer(sas_pageid,sas_formatid,sas_target) {
if (sas_masterflag==1) {sas_masterflag=0;sas_master='M';} else {sas_master='S';};
document.write('<scr'+'ipt SRC="http://www2.smartadserver.com/call/pubj/' + sas_pageid + '/' + sas_formatid + '/'+sas_master + '/' + sas_tmstp + '/' + escape(sas_target) + '?"></scr'+'ipt>');
}

//send2friends
function checkPage(fmobj,chkbox) {
  for (var i=0;i<fmobj.elements.length;i++) {
    var e = fmobj.elements[i];
    if ( e.type=='checkbox' && !e.disabled ) {           //&& (e.name == parseFloat(e.name)
      e.checked = chkbox.checked;
    }
  }
}

//showComposemessage
function checkCC(fmobj,chkbox) {
  for (var i=0;i<fmobj.elements.length;i++) {
    var e = fmobj.elements[i];
    //alert(e.name+ ' ' + e.name.indexOf('(cc)')); 
    if ( e.type=='checkbox' && !e.disabled && e.name.indexOf('_cc') >0 ) {           //&& (e.name == parseFloat(e.name)
      e.checked = chkbox.checked;
    }
  }
}

function findPlaceHolders() {
	var detect = navigator.userAgent.toLowerCase(); 
	if (detect.indexOf("safari") > 0) return false;
	
	var inputs = document.getElementsByTagName("input");
	for (var i=0;i<inputs.length;i++) {
		if (inputs[i].getAttribute("type") == "text") {
			if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
				if (inputs[i].value == "") {
					inputs[i].value = inputs[i].getAttribute("placeholder");
					inputs[i].style.color = "#BBBBBB";
				}
				inputs[i].onfocus = function() {
					if (this.value == this.getAttribute("placeholder")) {
						this.value = "";
						this.style.color = "#000000";
					}
					return false;
				}
				inputs[i].onblur = function() {
					if (this.value.length < 1) {
						this.value = this.getAttribute("placeholder");
						this.style.color = "#BBBBBB";
					}
				}
			}
		} else if (inputs[i].getAttribute("type") == "password") {
			if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
				if (detect.indexOf("msie") > 0) {
					// IE is a bitch
					/*var original = inputs[i];
					original.style.color = '#000000';
					
					var replacement = document.createElement('input');
					replacement.type = "text";
					replacement.tabindex = "2";
					replacement.value = original.getAttribute("placeholder");
					//replacement.style = original.style;
					replacement.style.color = "#BBBBBB";
					replacement.style.fontSize = original.style.fontSize;
					replacement.style.width = original.style.width;
					replacement.style.padding = original.style.padding;
					
					if (original.value == "") {
						original.parentNode.replaceChild(replacement, original);
					}
					replacement.onfocus = function() {
						replacement.parentNode.replaceChild(original, replacement);
						setTimeout(function() { original.focus(); }, 10);
						return false;
					}
					original.onblur = function() {
						if (this.value.length < 1) {
							original.parentNode.replaceChild(replacement, original);
						}
					}*/
					// such a bitch actually, that this didn't work either. so just ignore the placeholder then.
				} else {
					if (inputs[i].value == "") {
						inputs[i].type = "text";
						inputs[i].value = inputs[i].getAttribute("placeholder");
						inputs[i].style.color = "#BBBBBB";
					}
					inputs[i].onfocus = function() {
						if (this.value == this.getAttribute("placeholder")) {
							this.value = "";
							this.type = "password";
							this.style.color = "#000000";
						}
						return false;
					}
					inputs[i].onblur = function() {
						if (this.value.length < 1) {
							this.type = "text";
							this.value = this.getAttribute("placeholder");
							this.style.color = "#BBBBBB";
						}
					}
				}
			}
		}
	}
}

function getRandomText(name) {
  //random text Kees
  var ArrText = new Array();

 ArrText[0] = "een connaisseur toch?";
  ArrText[1] = "Goed dat je er bent.";
  ArrText[2] = "je ziet er goed uit vandaag..";
  ArrText[3] = "we know you know..";
  ArrText[4] = "No worries, we zijn smiley-vrij.";
  ArrText[5] = "Vandaag al een tip gedeeld?";
  ArrText[6] = "Goed dat je terug bent";
  ArrText[7] = "Goede dag vandaag?";
  ArrText[8] = " Looking great..";
  ArrText[9] = " Heb je 'n goed adres voor de gids?";
  ArrText[10] = "al eens in Maastricht geweest?";
  ArrText[11] = "Spannend dat EK..";
  ArrText[12] = "Sparkx hero!";



  IntRnd = Math.floor(Math.random()*11);

  document.getElementById('RandomText').innerHTML =  "Hi "+ name +",<BR> "+ArrText[IntRnd];
}

//MENU STUFF
function ShowSubMenu(intSubMenu) {
  for (var i=1;i<=100;i++)  {
    try {
      document.getElementById('menu_item_' + i).style.display = 'none';
    } catch(e) {
    }
  }

  try {
    document.getElementById('menu_item_' + intSubMenu).style.display = 'block';
    document.getElementById('menu_item_' + intSubMenu).style.left = findLeftPos(document.getElementById('TopMenu' + intSubMenu)) + 'px';
    document.getElementById('menu_item_' + intSubMenu).style.top = findTopPos(document.getElementById('TopMenu' + intSubMenu)) + 'px';
  } catch(e) {
    //alert("error " + intSubMenu);
  }
}

function HideSubMenu(intSubMenu)
{
  MenuTimer = setTimeout("document.getElementById('menu_item_" + intSubMenu + "').style.display='none'", 450);
}

function KillTimeOut()
{
try
{
  clearTimeout(MenuTimer);
} catch(e) {
  return false;
}
  return true;
}

function findLeftPos(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
 }
  
  function findTopPos(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
//--------------------------------------------------------------  
function doSearch() {
 // alert(document.quickSearch.searchType.options[document.quickSearch.searchType.selectedIndex].value);
  if (document.quickSearch.quickSearchKeyword.value == "op naam en in recensies" ||
     document.quickSearch.quickSearchKeyword.value == "op naam, bedrijf, opleiding.." ||
     document.quickSearch.quickSearchKeyword.value == "") {
      alert("Lege zoekopdracht");
      return false;
  }

  if (document.quickSearch.searchType.options[document.quickSearch.searchType.selectedIndex].value == "guide") 
  {
      // if we're already in the guide
      if (typeof updateMarkers == "function")
      {
        // just update the thingy
        document.getElementById('globsearch').value = document.quickSearch.quickSearchKeyword.value;
        updateMarkers(true);
        
        return false;
      }
      else
      {
        // go there
	    window.location="/guide#globsearch="+document.quickSearch.quickSearchKeyword.value+"&globsearchold=&autofit=true&currentTab=resultsTab";
	    
	    return false;
	  }
  }
  else
  {
	  return true;
  }
}

function advSearch() { 
	if (document.quickSearch.searchType.options[document.quickSearch.searchType.selectedIndex].value == "guide") 
	  document.quickSearch.quickSearchKeyword.value = "op naam en in recensies";
	else 
	  document.quickSearch.quickSearchKeyword.value = "op naam, bedrijf, opleiding..";
	
	return false; 
} 

function submitEnter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

function printContentsOf(elm)
{
    if (!elm)
        return;
        
    var width = elm.style.width;
    var height = elm.style.height;
    var printingWindow = window.open('', "Print", 
        "width=" + width + ",height=" + height + ",top=250,left=345,toolbars=no,scrollbars=no,status=no,resizable=no");

    printingWindow.document.write('<link rel="stylesheet" href="/templates/front/default/css/styleNew.css" type="text/css">');
    printingWindow.document.write(elm.innerHTML);
    printingWindow.document.close();
    printingWindow.focus();
    printingWindow.print();
    printingWindow.close();
}

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 selectImage(sFilePath, sPicName) {
	pic = eval("document.getElementById('eventImage')");
	if (pic)
	{
		if (sFilePath != '')
		{
			MM_preloadImages(sFilePath);
			pic.src = sFilePath;
		}
		else
		{
			pic.src = "";
		}
	}
}

function showObject(div_name) {
    var dS = document.getElementById(div_name);    
    dS.style.display = 'inline';
}

function hideObject(div_name) {    
    var dS = document.getElementById(div_name);    
    dS.style.display = 'none';
}

function changeStyle(obj, new_style) { 
    document.getElementById(obj).className = new_style; 
}

function highlightItem(object) {
	unHighlightItems('menuLink');
	object.className = 'menulinks_selected';
}

function unHighlightItems(obj) {
	for(i=0; i < 5; i++) {
		document.getElementById(obj+'_'+i).className = 'menulinks'; 
	}
}

function checkForSubmenu() {
	var submenuOver = document.getElementById('subMenuOver');
	if(submenuOver.innerHTML != "") {
		hideObject('subMenu');
		showObject('subMenuOver');
	}
}

function hideSubmenu() {
	hideObject('subMenuOver');
	showObject('subMenu');
	//var submenuOver = document.getElementById('subMenuOver');
	//submenuOver.innerHTML = "";
}

function createLayer(layerName,flashID,xpos,ypos,fWidth,fHeight) {
	documentBody = document.getElementsByTagName("body")[0];
	layer = document.createElement("div");
	layer.setAttribute("id", layerName);	
	layer.style.position  = "absolute"; 
	layer.style.left      = xpos;
	layer.style.top		  = ypos;
	layer.innerHTML = "";
	documentBody.appendChild(layer);
}

function doPhotoslide(direction, fileLeftId, fileRightId, fileMiddleId) {
	var imageSizeMediumWidth = 125;
	var imageSizeMediumHeight = 65;
	var imageSizeLargeWidth = 150;
	var imageSizeLargeHeight = 113;
	
	if(document.getElementById('middlePhoto').className == 'imageMedium') {
		changeStyle('leftPhoto', 'imageMediumLeft');
		changeStyle('rightPhoto', 'imageMediumRight');
		changeStyle('middlePhoto', 'imageLarge');
		document.getElementById('photoslide_'+fileLeftId).width = imageSizeMediumWidth;
		document.getElementById('photoslide_'+fileLeftId).height = imageSizeMediumHeight;
		document.getElementById('photoslide_'+fileRightId).width = imageSizeMediumWidth;
		document.getElementById('photoslide_'+fileRightId).height = imageSizeMediumHeight;
		document.getElementById('photoslide_'+fileMiddleId).width = imageSizeLargeWidth;
		document.getElementById('photoslide_'+fileMiddleId).height = imageSizeLargeHeight;
	} else if(direction == 'left') {
		changeStyle('leftPhoto', 'imageLargeLeft');
		changeStyle('rightPhoto', 'imageMediumRight');
		changeStyle('middlePhoto', 'imageMedium');
		document.getElementById('photoslide_'+fileLeftId).width = imageSizeLargeWidth;
		document.getElementById('photoslide_'+fileLeftId).height = imageSizeLargeHeight;
		document.getElementById('photoslide_'+fileRightId).width = imageSizeMediumWidth;
		document.getElementById('photoslide_'+fileRightId).height = imageSizeMediumHeight;
		document.getElementById('photoslide_'+fileMiddleId).width = imageSizeMediumWidth;
		document.getElementById('photoslide_'+fileMiddleId).height = imageSizeMediumHeight;
    	//document.getElementById('photoLeftArrow').onclick = null;
	} else {
		changeStyle('leftPhoto', 'imageMediumLeft');
		changeStyle('rightPhoto', 'imageLargeRight');
		changeStyle('middlePhoto', 'imageMedium');
		document.getElementById('photoslide_'+fileRightId).width = imageSizeLargeWidth;
		document.getElementById('photoslide_'+fileRightId).height = imageSizeLargeHeight;
		document.getElementById('photoslide_'+fileLeftId).width = imageSizeMediumWidth;
		document.getElementById('photoslide_'+fileLeftId).height = imageSizeMediumHeight;
		document.getElementById('photoslide_'+fileMiddleId).width = imageSizeMediumWidth;
		document.getElementById('photoslide_'+fileMiddleId).height = imageSizeMediumHeight;
		//document.getElementById('photoRightArrow').onclick = null;
	}
}

function switchPhotoslide(direction, fileLeftId, fileRightId, fileMiddleId) {
	var endTime = 5;
	var pauseTime = 200;
	
	for(var i = 0; i < endTime; i++)
	{
		setTimeout("doPhotoslide('"+direction+"', '"+fileLeftId+"', '"+fileRightId+"', '"+fileMiddleId+"')", pauseTime);
	}
}


function insertSlideTitle(title) {
	document.getElementById('photoItemTxt').innerHTML = title;
}
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

/**
 * sprintf() for JavaScript v.0.4
 *
 * Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/>
 * Thanks to David Baird (unit test and patch).
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}

