function external_links(rootid)
{
	var root = document;
	if (rootid && !(root = document.getElementById(rootid))) return false;

	var c = root.getElementsByTagName('a'), a, i = 0, attr;
	var re = /(^|\s)external(\s|$)/;
	while ((a = c[i++]))
	{
		attr = a.getAttribute('rel');
		if (attr && re.test(attr))
		{
			a.target = '_blank';
		}
	}

	return true;
}

/**
 * Opens a popup window
 *
 * Arguments:
 *    url		url to display in the window
 *    width		width of the window
 *    height	height of the window
 *    options	options string
 *    name		name of the window
 */
function OpenWindow(url, width, height, options, name) {

   if ( !width ) width = 630;
   if ( !height ) height = 500;
   if ( !options ) options ="scrollbars=yes,menubar=no,toolbar=no,location=no,status=no,resizable=yes";
   if ( !name ) name = "outsideSiteWindow";

   var newWin = window.open( url, name, "width=" + width + ",height=" + height + "," + options );
}

function animateVeilingPrice(priceStart, priceStop) {
	var priceDelta = (priceStart - priceStop) / 20;
	animateVeilingPriceStep(priceStart, priceStop, priceDelta);
}

function animateVeilingPriceStep(priceStart, priceStop, priceDelta) {
	var priceEl = document.getElementById('veiling_price');
	if(priceEl == null) return;

	priceStart = Math.max(priceStart - priceDelta, priceStop);
	priceEl.innerHTML = "&euro; " + priceStart.toFixed(2).replace('.', ',');

	if(priceStart > priceStop) {
		setTimeout("animateVeilingPriceStep("  + priceStart + ", " + priceStop + ", " + priceDelta + ");", 100);
	}
}

function animateVeilingTime(time, timeDelta, days) {
	var timeEl = document.getElementById('veiling_time');
	if(timeEl == null) return;

	iVeiling_remaining = Math.max(time - (new Date().getTime() / 1000) - timeDelta, 0);
	iVeiling_remaining_days = Math.floor(iVeiling_remaining / (60 * 60 * 24)).toString();
	iVeiling_remaining_hours = Math.floor((iVeiling_remaining % (60 * 60 * 24)) / (60 * 60)).toString();
	iVeiling_remaining_minutes = Math.floor((iVeiling_remaining % (60 * 60)) / (60)).toString();
	iVeiling_remaining_seconds = Math.floor((iVeiling_remaining % (60))).toString();

	if(iVeiling_remaining_minutes.length == 1) iVeiling_remaining_minutes = "0" + iVeiling_remaining_minutes;
	if(iVeiling_remaining_seconds.length == 1) iVeiling_remaining_seconds = "0" + iVeiling_remaining_seconds;

	timeEl.innerHTML = iVeiling_remaining_days + ' ' + days + ', ' + iVeiling_remaining_hours + ":" + iVeiling_remaining_minutes + ":" + iVeiling_remaining_seconds;

	if(iVeiling_remaining <= 0) {
		window.location = '/veiling.php';
		return;
	}

	setTimeout("animateVeilingTime("  + time + ", " + timeDelta + ", '" + days + "');", 1000);
}

var elStylesheet = null;

function setStylesheet(title) {
	var i, a;
	for (i=0; (a = document.getElementsByTagName("link")[i]); i++){
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")){
			a.disabled = true;
			if (a.getAttribute("title") == title) a.disabled = false;
		}
	}

	setCookie('stylesheet', title);

	if (elStylesheet != null) {
		removeClass(elStylesheet, 'active');
	}

	elStylesheet = document.getElementById('font-' + title);
	if (elStylesheet != null) {
		addClass(elStylesheet, 'active');
	}
}

// Reloads lister from sort selector
function loadWin(){
   var index=document.compare.sortOption.selectedIndex;
   var value=document.compare.sortOption.options[index].value;
   document.compare.app_action.value = 'reSort';
   window.location.href=value;
   //document.compare.submit();
}


// Adds item(s) to the keeplist
function addMyList(){
   document.compare.app_action.value = 'addMyList';
   document.compare.action = "/mijnlijst.php";
   document.compare.submit();
}


// Removes item(s) to the keeplist
function removeMyList(){
   document.compare.app_action.value = 'removeMyList';
   document.compare.action = "/mijnlijst.php";
   document.compare.submit();
}

// Removes item(s) from the viewlist
function removeViewList(){
   document.compare.app_action.value = 'removeMyList';
   document.compare.action = "/bekeken.php";
   document.compare.submit();
}


// Compare items
function doCompareSubmit(){
   var inputs = document.getElementsByName('fh_secondid[]');
   var i = inputs.length;
   var checked = 0;

   while(i--) {
      if(inputs[i].checked)
	     checked++;
   }

   if(checked < 1) {
	  alert(lCompareCheckMin);
   } else if (checked > 4) {
	  alert(lCompareCheckMax);
   } else {
      document.compare.app_action.value = 'doCompare';
      document.compare.submit();
   }
}


// Adds a bookmark (todo -> passing $url_path & $shortname)
function addbookmark(url_path, shortname) {
   bookmarkurl = url_path;
   bookmarktitle="Belvilla - " + shortname;
   if (window.sidebar) {
      window.sidebar.addPanel(bookmarktitle, bookmarkurl,"");
   } else if (window.external) {
      window.external.AddFavorite(bookmarkurl,bookmarktitle);
   } else {
      return true;
   }
}


// Enables text click for textbox in detail
function thisClick(d) {
   var el_collection=eval("document.forms.book.elements['fh_book_period[]']");
   for (r=0; r < el_collection.length; r++) {
      if (r==d) {
         if (el_collection[r].checked == true) {
            el_collection[r].checked = false;
         } else {
            el_collection[r].checked = true;
         }
      }
   }
}

function refresh_book(){
  // for now just sumit form, will have some
  // nice code to fresh the values only later
  document.book.submit();
}

function refresh_book_cb(el){
  // for now just sumit form, will have some
  // nice code to fresh the values only later

  if(el.checked == true){
    setCookie('insur_check', 'y');
  }else{
    setCookie('insur_check', 'n');
  }
  var cb_extra_risc = document.getElementById('extra_risc_yes');
  if ( cb_extra_risc == null ) return false;

  if(el.checked != true){
      cb_extra_risc.checked = false;
  }
  
  document.book.submit(); 
  document.book.submit();
}

function map_DoFSCommand(command, args) {
	if(command == "toggleTabSearch") {
		toggleTabSearch(args);
	} else if(command == "doCountry") {
		var country = args;

		if(typeof(fh_location) === "undefined") {
			window.location = "/index.php?fh_location=//"+FSCommand_location+"/"+FSCommand_lang+"/land="+country+"&fh_country="+country+"&clearperiod=1";
			return;
		} else if(fh_location.indexOf('land=') > -1) {
			fh_location = fh_location.replace(/\/land=[0-9]*/, '/land=' + country);
		} else {
			fh_location = fh_location.replace('&', '/land=' + country + '&');
		}

		window.location = "/index.php?fh_location=" + fh_location;
} else if(command == "doArea") {
		var country = args.substring(0, args.indexOf('|'));
		var area = args.substring(args.indexOf('|') + 1);

		if(typeof(fh_location) === "undefined") {
			window.location = "/index.php?fh_location=//"+FSCommand_location+"/"+FSCommand_lang+"/land="+country+"/gebiednaam="+area+"&fh_country="+country+"&clearperiod=1";
			return;
		} else if(fh_location.indexOf('gebiednaam=') > -1) {
			fh_location = fh_location.replace(/\/gebiednaam=[0-9]*/, '/gebiednaam=' + area);
		} else {
			fh_location = fh_location.replace('&', '/gebiednaam=' + area + '&');
		}

		window.location = "/index.php?fh_location=" + fh_location;
	}
}

var toggleTabEl;
var toggleTabElLink;
var toggleTabMap = false;

function toggleTab(tab, id){
	var tabEl = document.getElementById('tab_' + tab);
	if ( tabEl == null ) return false;
	var tabElLink = document.getElementById('tablink_' + tab);
	if ( tabElLink == null ) return false;

	// Hide previous tab
	if ( toggleTabEl ) { toggleTabEl.style.display = 'none'; }
	if ( toggleTabElLink ){ removeClass(toggleTabElLink, 'active'); }

	// Show tab
	if ( tabEl ){ tabEl.style.display = ''; }
	if ( tabElLink ){ addClass(tabElLink, 'active'); }

	setCookie('toggleTabEl[' + id + ']', tab);

	toggleTabEl = tabEl;
	toggleTabElLink = tabElLink;

	// Fix for Google maps
	if(tab == "map" && map !== undefined && !toggleTabMap) {
		map.checkResize();
		map.setCenter(mapCenter, 11);
		toggleTabMap = true;
	}

	return true;
}

var toggleTabSearchEl;

function toggleTabSearch(tab){
	var tabEl = document.getElementById('tab_' + tab);
	if ( tabEl == null ) return false;

	// Hide previous tab
	if ( toggleTabSearchEl ) { toggleTabSearchEl.style.display = 'none'; }

	// Show tab
	if ( tabEl ){ tabEl.style.display = ''; }

	toggleTabSearchEl = tabEl;

	return true;
}

var markers = [];

function togglePoi(el, elType) {
	if(el.checked == false) {
		// POI verbergen
		var i = markers.length;
		while(i--) {
			if(markers[i].type == elType) {
				map.removeOverlay(markers[i]);
				markers[i].visible = false;
			}
		}
	} else {
		// POI weergeven
		var i = markers.length;
		while(i--) {
			if(markers[i].type == elType) {
				if(markers[i].visible == false) {
					markers[i].visible = true;
					map.addOverlay(markers[i]);
				}
			}
		}
	}
}

var toggleFacetDetailEl;
var toggleFacetDetailLiEl;

function toggleFacetDetail(facet, facetA) {
	var facetEl = document.getElementById('facet_' + facet);
	if ( facetEl == null ) return false;

	if(toggleFacetDetailEl != null) addClass(toggleFacetDetailEl, 'collapsed');
	removeClass(facetEl, 'collapsed');
	toggleFacetDetailEl = facetEl;

	if ( facetA == null ) return;
	facetLi = facetA.parentNode;

	if(toggleFacetDetailLiEl != null) removeClass(toggleFacetDetailLiEl, 'active');
	addClass(facetLi, 'active');
	toggleFacetDetailLiEl = facetLi;
}

function toggleTis(tis) {
	var tisEl = document.getElementById('tis' + tis);
	if(tisEl == null) return;

	tisEl.style.display = (tisEl.style.display == '') ? 'none' : '';
}

//
// Gets the cookie with the given name
//
function getCookie(sName) {
	var aCookie = document.cookie.split('; '), i = aCookie.length, aCrumb;
	while (i--)
	{
 		aCrumb = aCookie[i].split('=');
		if (sName == aCrumb[0])
			return aCrumb[1] != undefined ? unescape(aCrumb[1]) : null;
	}

	return null;
}

//
// Sets the cookie with the given name to the given value
//
function setCookie(sName, sValue) {
	document.cookie = sName + '=' + escape(sValue) + '; path=/; domain=.' + document.domain;
}

//
// Fader functions
//
var fader;
var faderEl;
var red;
var green;
var blue;
var redsteps;
var greensteps;
var bluesteps;

function tableFadeBg(elName, fade, fadeSteps) {
	faderEl = document.getElementById(elName);
    red = hexToR(fade); redsteps = Math.ceil((255-red)/fadeSteps);
	green = hexToG(fade); greensteps = Math.ceil((255-green)/fadeSteps);
	blue = hexToB(fade); bluesteps = Math.ceil((255-blue)/fadeSteps);
	inc = fadeSteps;
	step = 0;
	tableFadeBgExec();
}

function tableFadeBgExec() {
	try {
		var epct = step/inc;
		var spct = 1 - epct;
		var nRows = faderEl.rows.length;
		red = red+(redsteps*step) <= 255 ? red+(redsteps*step) : 255;
		green = green+(greensteps*step) <= 255 ? green+(greensteps*step) : 255;
		blue = blue+(bluesteps*step) <= 255 ? blue+(bluesteps*step) : 255;
		for(var j=2; j < nRows; j++) {
			faderEl.rows[j].style.backgroundColor = hex(red,green,blue);
		}
		if ( step < inc ) {
			fader = setTimeout('tableFadeBgExec()',50);
		}
		step++;
	} catch(err) {}
}

function hex(r, g, b) {
	var hexstring = "0123456789abcdef";
	var hex_color = hexstring.charAt (Math.floor(r / 16)) +	hexstring.charAt (r % 16)
		+ hexstring.charAt (Math.floor(g / 16)) + hexstring.charAt (g % 16)
		+ hexstring.charAt (Math.floor(b / 16)) + hexstring.charAt (b % 16);
	return '#'+hex_color;
}

function hexToR(h) { return parseInt((cutHex(h)).substring(0,2),16) }
function hexToG(h) { return parseInt((cutHex(h)).substring(2,4),16) }
function hexToB(h) { return parseInt((cutHex(h)).substring(4,6),16) }
function cutHex(h) { return (h.charAt(0)=="#") ? h.substring(1,7) : h}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true
}

function  onNewsbriefSubmit()
{
       var smpl_eml_nl="bijv. jansen@provider.nl"
       var smpl_eml_uk="e.g. smith@provider.co.uk"
       var smpl_eml_de="z.B. Steinberg@provider.de"
       var smpl_eml_it="bijv. jansen@provider.nl"
       var smpl_eml_pl="n.p.: jan.kowalski@onet.pl"

	if (echeck(document.nieuwsbrief.userdata_email.value))
	{
		return true;
	}
	else
	{
		//document.search_form.fh_search.value = document.nieuwsbrief.userdata_email.value;
		//document.search_form.submit.click();
              if ((document.nieuwsbrief.userdata_email.value==smpl_eml_nl)||(document.nieuwsbrief.userdata_email.value==smpl_eml_uk)||(document.nieuwsbrief.userdata_email.value==smpl_eml_de)||(document.nieuwsbrief.userdata_email.value==smpl_eml_pl)){
                return true;
              }
              else{
		  return false; 
              }
	}
}


function getAjaxDesc(lang)
{
	new Ajax.Updater('myTextArea', '/colldesc.php?id='+$('coll_id').value+'&lang='+lang+'&type=text', { method: 'get', onComplete: updateXinhaText });
	new Ajax.Updater('i', '/colldesc.php?id='+$('coll_id').value+'&lang='+lang+'&type=title', { method: 'get', evalScripts:true });
	$('lang').value = lang;

}

function updateXinhaText(req)
{
	xinha_editors.myTextArea.setHTML(req.responseText);
}

function setAjaxDesc(lang)
{
	var text2 = xinha_editors.myTextArea.getHTML();
	new Ajax.Updater('i', '/colldesc.php?id='+$('coll_id').value+'&lang='+lang+'&type=update', { method: 'post', parameters: { text: text2, title: $('title_input').value, count: $('count').value, links: $('link').value }});
}

var win=null;

function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
win=window.open(mypage,myname,settings);
}

function makeHTML(house){
	var request = new Ajax.Request('/ajax_houseinfo.php?house_id='+house, { method: 'get', evalScripts:true});
	alert('response='+request.transport.responseText);	
}