var ScrollTimer;
var pageLoaded = false;
var base_href;

setPageLoaded(true);

function openPopup(url,width,height,scroll,name)
{
  var winl = (screen.width-width)/2;
  var wint = (screen.height-height)/2;
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  if(!scroll)
    scroll = 'no';
  if(!name)
    name = '';

  var popup = window.open(url,name,'width=' + width + ',height=' + height + ',top=' + wint + ',left=' + winl + ',scrollbars=' + scroll + ',menubars=no,toolbar=no,location=no,status=no');
  if(!popup)
  {
    alert('U heeft een popup blocker. Deactiveer de popup blocker voor deze website om van alle functionaliteiten gebruik te kunnen maken');
    return false;
  }
  return popup;
}

function setPageLoaded(loaded)
{
  if (checkPageLoaded("setPageLoaded(" + loaded + ")") == true)
  {
    if (loaded == true)
      pageLoaded = true;
    else
      pageLoaded = false;
  }
}

function checkPageLoaded(functionToRun)
{
  if(pageLoaded!==true)
  {
    if (document.addEventListener)
    {
      document.addEventListener("DOMContentLoaded", function(){pageLoaded=true; if(functionToRun){eval(functionToRun)}},false);
    } 
    else if (document.all && !window.opera)
    {
      document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
      var contentloadtag = document.getElementById("contentloadtag");
      contentloadtag.onreadystatechange=function(){
        if (this.readyState=="complete"){
          pageLoaded = true;
          if(functionToRun) eval(functionToRun);
        }
      }
    } 
    else 
    {
      window.onload=function(){
        pageLoaded = true;
        if(functionToRun) eval(functionToRun);
      }
    }
    
    return false;
  } else {
    return true;
  }
}

function getBaseHref()
{
  if (typeof(base_href) == 'undefined')
  {
    base_href = document.getElementsByTagName('base').item(0);
  }
  return base_href;
}

function include_js(src) {
  if (checkPageLoaded("include_js('" + src + "')") == true) {
    var base = getBaseHref().href;
    var html_doc = document.getElementsByTagName('head').item(0);
    var scripts = html_doc.getElementsByTagName('script');
    
    for (var i = 0; i < scripts.length; i++)
    {
      if (scripts[i].src.replace(base,'') == src) return false;
    }
    
    var file = document.createElement("script");
    
    file.setAttribute('language', "javascript");
    file.setAttribute('type', "text/javascript");
    file.setAttribute('src', src);
    
    html_doc.appendChild(file);
    return false;
  }
}

function include_css(src) {
  if (checkPageLoaded("include_css('" + src + "')") == true) {
    var base = getBaseHref().href;
    var html_doc = document.getElementsByTagName('head').item(0);  
    var styles = html_doc.getElementsByTagName('link');
        
    for (var i = 0; i < styles.length; i++)
    {
      if (styles[i].href.replace(base,'') == src) return false;
    }
    
    var file = document.createElement("link");
    
    file.setAttribute('rel', "stylesheet");
    file.setAttribute('type', "text/css");
    file.setAttribute('href', src);
    
    html_doc.appendChild(file);
    return false;
  }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookieValue(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return false;
}

function getCssStyle(elem,style)
{
  if(elem.style && elem.style[style])
  {
    return elem.style[style];
  }
  else
  {
    if(elem.currentStyle && elem.currentStyle[style])
    {
      return elem.currentStyle[style];
    }
    else if(window.getComputedStyle && (value=window.getComputedStyle(elem,null)[style]))
    {
      return value;
    }
  }
  return false;
}

function openFloorPlan(uuid,type,width,height,stylesheet)
{
  if(!width || width.length<50)
    width = 620;
  if(!height || width.length<50)
    height = 550;
  if(!type)
    type = 'bb';
    
  var url = 'frontend-g4/tools/floorplan.php?uuid=' + uuid + '&type=' + type + '&uri=' + obtainURI(); 
  if(stylesheet && stylesheet.length>1)
  {
    url = url + '&stylesheet=' + stylesheet;
  }
    
  openPopup(url,width,height);
}

function openIpix(uuid,type,width,height,stylesheet)
{
  if(!width || width.length<50)
    width = 468;
  if(!height || width.length<50)
    height = 500;
  if(!type)
    type = 'bb';
    
  var url = 'frontend-g4/tools/ipix_popup.php?uuid=' + uuid + '&type=' + type; 
  if(stylesheet && stylesheet.length>1)
  {
    url = url + '&stylesheet=' + stylesheet;
  }
    
  openPopup(url,width,height);
}

function ScrollLeft(id)
{
  ScrollTimer = setInterval("document.getElementById('" + id + "').scrollLeft -= 2", 15);
}

function ScrollRight(id)
{
  ScrollTimer = setInterval("document.getElementById('" + id + "').scrollLeft += 2", 15);
}

function ScrollUp(id)
{
  ScrollTimer = setInterval("document.getElementById('" + id + "').scrollTop -= 2", 15);
}

function ScrollDown(id)
{
  ScrollTimer = setInterval("document.getElementById('" + id + "').scrollTop += 2", 15);
}

function stopScroll()
{
  clearInterval(ScrollTimer);
}

function openImages(uuid,type,number,width,height,stylesheet)
{
  if(!width || width<520)
    width = 520;
  if(!height || height<475)
    height = 475;
    
  imagewidth = width - 20;
  imageheight = height - 133;
  
  if(!type)
    type = 'bb';
    
  var url = 'frontend-g4/tools/image_popup.php?uuid=' + uuid + '&type=' + type + '&number=' + number + '&uri=' + obtainURI() + '&width=' + imagewidth + '&height=' + imageheight;
  if(stylesheet && stylesheet.length>1)
  {
    url = url + '&stylesheet=' + stylesheet;
  }
    
  openPopup(url,width,height);
}

function openGmap(uuid,type,number,width,height,stylesheet)
{
  if(!width || width<520)
    width = 520;
  if(!height || height<475)
    height = 475;
  if(!type)
    type = 'bb';
    
  var url = 'frontend-g4/tools/gmap_popup.php?uuid=' + uuid + '&type=' + type + '&number=' + number;
  if(stylesheet && stylesheet.length>1)
  {
    url = url + '&stylesheet=' + stylesheet;
  }
    
  openPopup(url,width,height);
}

function openGmapSpecific(city,street,nr,type,number,width,height,stylesheet)
{
  if(!width || width<520)
    width = 520;
  if(!height || height<475)
    height = 475;
  if(!type)
    type = 'bb';
    
  var url = 'frontend-g4/tools/gmap_popup.php?city=' + city + '&street=' + street + '&nr=' + nr + '&type=' + type + '&number=' + number;
  if(stylesheet && stylesheet.length>1)
  {
    url = url + '&stylesheet=' + stylesheet;
  }
    
  openPopup(url,width,height);
}

function printPreview(uuid,type,width,height,stylesheet)
{
  if(!width || width.length<50)
    width = 550;
  if(!height || width.length<50)
    height = 600;
  if(!type)
    type = 'bb';
    
  var url = 'frontend-g4/tools/print_preview.php?uuid=' + uuid + '&type=' + type + '&uri=' + obtainURI();
  if(stylesheet && stylesheet.length>1)
  {
    url = url + '&stylesheet=' + stylesheet;
  }
    
  openPopup(url,width,height,'yes');
}

function obtainURI()
{
  var a = document.URL.split("//");
  a = (a[1] ? a[1] : a[0]).split("/");
  for (var i=0;i<a.length;i++)
  {
    if(a[i].indexOf('.')!=-1 && a[i]!='sarge.redbus.yes-co.nl' && a[i]!='frontend-g4.yes-co.nl' && a[i]!='devel.redbus.yes-co.nl')
    {
      return a[i];
    }
  } 
}

function obtainGetVariabel(varname)
{
  if((document.URL.indexOf('?')!=-1) && (document.URL.indexOf(varname + '=')!=-1))
  {
    var a = document.URL.split('?');

    if(a[1].indexOf('&')!=-1)
    {
      var vars = a[1].split('&');
      for (var i=0;i<vars.length;i++)
      {
        var current = vars[i].split('=');
        if(current[0]==varname)
        {
          return current[1];
        }
      }
    }
    else
    {
      var current = a[1].split('=');
      if(current[0]==varname)
      {
        return current[1];
      }
    }
    
  }
  return false;
}

function setActiveClassOn(object,className)
{
  var newClass = object.className + " " + className;
  object.className = trim(newClass);
}

function setActiveClassOff(object,className)
{
  var oldClass = object.className.replace(" " + className,"");
  object.className = trim(oldClass);
}

function placeContent(id,content)
{
  if(document.getElementById(id))
  {
    document.getElementById(id).innerHTML = content;
  }
}

function toggleClass(id,class1,class2)
{
  element = document.getElementById(id);
  if (element)
  {
    if (element.className == class1)
    {
      element.className = class2;
    } else {
      element.className = class1;
    }
  }
}

function toggleDisplayById(id)
{
  if(document.getElementById(id))
  {
    if(document.getElementById(id).style.display=='block')
      document.getElementById(id).style.display = 'none';
    else
      document.getElementById(id).style.display = 'block';
  }
}

function hideByClassName(className,tagName)
{
  if(!tagName)
  {
    tagName = '*';
  }
  
  var elems = window.document.body.getElementsByTagName(tagName);
  if(elems)
  {
    for(var i=0;i<elems.length;i++)
    {
      if(elems[i].className.trim() == className.trim())
      {
        elems[i].style.display = 'none';
      }
    }
  }
}

function getElementByClassName(className,tagName)
{
  if(!tagName)
  {
    tagName = '*';
  }
  
  var elems = window.document.body.getElementsByTagName(tagName);
  for( var i=0;i<elems.length;i++)
  {
    if(elems[i].className.trim() == className.trim())
    {
      return elems[i];
    }
  }
  return false;
}

function getElementsByClassName(oElm, strTagName, strClassName)
{
  if(oElm.getElementsByTagName)
  {
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++)
    {
      oElement = arrElements[i];      
      if(oRegExp.test(oElement.className))
      {
        arrReturnElements.push(oElement);
      }   
    }
    return (arrReturnElements)
  }
  var emtyResult = new Array();;
  return emtyResult;
}

function placeExternalContent(id,url,width,height)
{
  var content = '<iframe width="' + width + '" height="' + height + '" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" src="' + url + '"></iframe>';
  placeContent(id,content);
}

function wait(duration)
{
  if ( (duration == null) || (duration < 0)) {return;}

  var later = (new Date()).getTime() + duration;

  while(true)
  {
    if ((new Date()).getTime() > later)
    {
      break;
    }
  }
}

function trim(s)
{
  if(s.length > 0)
  {
    var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (obj.test(s))
    {
      s = s.replace(obj, '$2');
    }
    var obj = / +/g;
    s = s.replace(obj, " ");
    if (s == " ")
    {
      s = "";
    }
  }
  return s;
}

String.prototype.trim=function(){return this.replace(/(^\s+)|\s+$/g,"");}; 

function goodchars(e,good,noEnter)
{
  var key, keychar;
  key = getkey(e);
  if (key == null)
  {
    return true;
  }

  keychar = String.fromCharCode(key);
  keychar = keychar.toLowerCase();
  good = good.toLowerCase();

  if (good.indexOf(keychar) != -1 || (noEnter==false && key==13) || (key==null) || (key==0) || (key==8) || (key==9) || (key==27))
  {
    return true;
  }

  return false;
}

function getkey(e)
{
  if (window.event)
  {
    return window.event.keyCode;
  }
  else if (e)
  {
    return e.which;
  }
  return null;
}

function in_array(array,value)
{
  for (var i = 0; i < array.length; i++)
  {
    if (array[i] == value)
    {
      return true;
    }
  }
  return false;
}

function iframe(targetId, frameSrc, frameWidth, frameHeight, frameScrolling, frameBorder)
{
  if (frameScrolling)
    frameScrolling = "yes";
  else
    frameScrolling = "no";

  if (frameBorder)
    frameBorder = 1;
  else
    frameBorder = 0;

  var iframe = document.createElement("iframe");
  iframe.src = frameSrc;
  iframe.style.width = frameWidth;
  iframe.style.height = frameHeight;
  iframe.scrolling = frameScrolling;
  iframe.frameBorder = frameBorder;
  document.getElementById(targetId).appendChild(iframe);
}

function getWindowSize()
{
  var width = 0, height = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    width = window.innerWidth;
    height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    width = document.documentElement.clientWidth;
    height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    width = document.body.clientWidth;
    height = document.body.clientHeight;
  }
  
  return new Array(width, height);
}

function removeElement(id)
{
  if(elem = document.getElementById(id))
  {
    elem.parentNode.removeChild(elem);
  }
}

/*
* Do an AJAX request
*
* @param string url
* @param string parameters
* @param string onreadystate function
* @param string requestType (POST or GET)
* @return void
*/
function remoteRequest(url, parameters, onreadystate, requestType)
{
  var funcParameters =remoteRequest.arguments;
  
  // Asynchroic request is default, Synchronic request should not be used when wanting to support all versions of firefox!
  var asynchronicRequest = true;
  if(funcParameters[3] != undefined)
    asynchronicRequest = (funcParameters[3]) ? true : false;

  http_request = false;
  error        = false;
  if (window.XMLHttpRequest)
  {
    try
    {
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType)
      {
        http_request.overrideMimeType('text/xml');
      }
    } catch (e) {
      error = true;
    }
  }
  else if (window.ActiveXObject)
  {
    try
    {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        error = true;
      }
    }
  }
  
  if(!error)
  {
    eval("http_request.onreadystatechange = " + onreadystate + ";");
    http_request.open(((requestType!='GET') ? 'POST' : 'GET'), url, asynchronicRequest);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
  }
  else
  {
    eval(onreadystate+"(true);");
  }
}

function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  }
}