var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		/*this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";*/
		//this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		}
	]

};
BrowserDetect.init();
var webbrowser = BrowserDetect.browser;

//alert( BrowserDetect.browser );

function isdefined( variable){
	return (typeof(variable) == "undefined")?  false: true;
}

function firebug(){
	if ( window.console && window.console.firebug ) {
		return true;
	}else{
		return false;
	}
}

function displayElement ( elm, disp )
{
  if ( typeof ( elm ) == 'string' )
    elm = document.getElementById ( elm );

  if ( elm )
  {
    if ( disp )
    {
      if ( navigator.userAgent.indexOf ( 'MSIE' ) == -1 )
      {
        if ( elm.tagName.toLowerCase ( ) == 'tr' )
          elm.style [ 'display' ] = 'table-row';
        else if ( elm.tagName.toLowerCase ( ) == 'tbody' )
          elm.style [ 'display' ] = 'table-row-group';
        else if ( elm.tagName.toLowerCase ( ) == 'table' )
          elm.style [ 'display' ] = 'table';
        else
          elm.style [ 'display' ] = 'block';
      }
      else{
        elm.style [ 'display' ] = 'block';
				//alert(elm.style [ 'display' ]);
			}
    }
    else
      elm.style [ 'display' ] = 'none';
  }

  return elm;
}

/* ************************************ */

function fieldAction(event,element,defaultValue){
	if(event == 'focus' && element.value == defaultValue){
		element.value = '';
	}
	if(event == 'blur' && element.value == ''){
		element.value = defaultValue;
	}
}
function leadingZero(n, totalDigits) {
	n = n.toString();
	var pd = '';
	if (totalDigits > n.length) {
		for (i=0; i < (totalDigits-n.length); i++) {
			pd += '0';
		}
	}
	return pd + n.toString();
}
function daysInMonth(month,year){
	return 32 - new Date(year, month, 32).getDate();
}
function futureTime(day,month,year){
	day = eval(day);
	month = eval(month);
	year = eval(year);
	daysMonth = daysInMonth(month,year);
	day += 14;
	if(day > daysMonth){
		leftDays = (day - daysMonth);
		day = leftDays;
		month++;
	}
	if(month > 12){
		month = 1;
		year++;
	}
	return leadingZero(day,2) + '-' + leadingZero(month,2) + '-' + leadingZero(year,2);
}
function calculatePhases(value){
	thisDate = value.split('-');
	date1 = futureTime(thisDate[0],thisDate[1],thisDate[2]);
	document.getElementById('phaseDate2').innerHTML = 'Te beginnen op: <b>' + date1 + '</b>';

	thisDate = date1.split('-');
	date2 = futureTime(thisDate[0],thisDate[1],thisDate[2]);
	document.getElementById('phaseDate3').innerHTML = 'Te beginnen op: <b>' + date2 + '</b>';

	thisDate = date2.split('-');
	date3 = futureTime(thisDate[0],thisDate[1],thisDate[2]);
	document.getElementById('phaseDate4').innerHTML = 'Te beginnen op: <b>' + date3 + '</b>';
	return false;
}
function selectDay(id){
	for(i=0;i<=6;i++){
		$('dayHeader_'+i).removeClass('active');
		$('dayHeader_'+i).removeClass('arrowV');
		$('dayHeader_'+i).addClass('arrowH');
		$('day_'+i).style.display = 'none'
	}
	$('dayHeader_'+id).removeClass('arrowH');
	$('dayHeader_'+id).addClass('active');
	$('dayHeader_'+id).addClass('arrowV');
	$('day_'+id).style.display = ''
}

var periodical, dummy;
var thePageID = 0;
var theActionID = 0;

/* SCREEN TINT DIALOG */
function initWindowTint(){
	arrayPageSize = getPageSize();
	$('tintScreen').style.height = ( ( arrayPageSize[1] ) + 'px' );
	$('tintScreen').style.width = ( ( arrayPageSize[2] ) + 'px' );
}
function showImage(srcImg,x,y,hideTint){
	//console.log('tmp');
	initWindowTint();

	$('tintScreen').style.zIndex = 100;
	$('window').style.zIndex = 200;

	$('tintScreen').removeClass('tintHidden');
	$('tintScreen').addClass('tintDisplay');


	$('window').style.width = x + 'px';
	$('window').style.height = y + 'px';
	$('window').style.marginLeft = '-' + ( x / 2 ) + 'px';
	$('window').style.marginTop = '-' + ( y / 2 ) + 'px';
	$('window').removeClass('windowHidden');
	$('window').addClass('windowDisplay');
	$('window').innerHTML = '<div style="position:absolute;z-index:20;right:0;top:-30px;"><a onclick="hideImage('+hideTint+')" style="color:white;cursor:pointer;">Venster sluiten</a></div><img style="" src="' + srcImg + '" />';
}
function hideImage(t){
	if( t == 1 ){
		$('tintScreen').removeClass('tintDisplay');
		$('tintScreen').addClass('tintHidden');
	}
	$('tintScreen').style.zIndex = 0;

	$('window').removeClass('windowDisplay');
	$('window').addClass('windowHidden');
}

function showPage( pageID , actionID ){
	initWindowTint();
	thePageID = pageID;
	theActionID = actionID;
	var tmp = false;

	var myRequest = new Request.HTML({
		method: 'get',
		url: 'index.php',
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			$('windowContent').innerHTML = responseHTML;
		}
	});
	myRequest.send('a=' + theActionID + '&p=' + thePageID);
	$('window').removeClass('windowHidden');
	$('window').addClass('windowDisplay');
	$('tintScreen').removeClass('tintHidden');
	$('tintScreen').addClass('tintDisplay');
	return false;
}
function hidePage(){
	$('tintScreen').addClass('tintHidden');
	$('tintScreen').removeClass('tintDisplay');
	$('window').removeClass('windowDisplay');
	$('window').addClass('windowHidden');
	return false;
}

function showRecipe( url ){
	initWindowTint();

	var myRequest = new Request({
		method: 'get',
		url: 'index.php',
		onSuccess: function(responseText) {
			$('windowContentArticle').innerHTML =  responseText;
		}
	});
	myRequest.send( url );

	$('windowArticle').removeClass('windowHidden');
	$('windowArticle').addClass('windowDisplayRecipe');
	$('tintScreen').removeClass('tintHidden');
	$('tintScreen').addClass('tintDisplay');
	//return false;
}
function hideRecipe(){
	$('windowContentArticle').innerHTML =  '<div style="text-align:center;margin-top:220px;"><img src="/layout/default/gfx/ajax-loader.gif" /></div>';
	$('tintScreen').removeClass('tintDisplay');
	$('tintScreen').addClass('tintHidden');
	$('windowArticle').removeClass('windowDisplayRecipe');
	$('windowArticle').addClass('windowHidden');
	//return false;
}

function showArticle( url ){
	initWindowTint();

	var myRequest = new Request({
		method: 'get',
		url: 'index.php',
		onSuccess: function(responseText) {
			$('windowContentArticle').innerHTML =  responseText;
		}
	});
	myRequest.send( url );

	$('windowArticle').removeClass('windowHidden');
	$('windowArticle').addClass('windowDisplayArticle');
	$('tintScreen').removeClass('tintHidden');
	$('tintScreen').addClass('tintDisplay');
	return false;
}
function hideArticle(){
	$('windowContentArticle').innerHTML =  '<div style="text-align:center;margin-top:220px;"><img src="/layout/default/gfx/ajax-loader.gif" /></div>';
	$('tintScreen').removeClass('tintDisplay');
	$('tintScreen').addClass('tintHidden');
	$('windowArticle').removeClass('windowDisplayArticle');
	$('windowArticle').addClass('windowHidden');
	return false;
}
function showWindow(pageID,actionID,additionalText){
	initWindowTint();
	thePageID = pageID;
	theActionID = actionID;
	var tmp = false;
	//periodical = getText.periodical(1 * 500, this);
	var myRequest = new Request.HTML({
		method: 'get',
		url: 'index.php',
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			//console.log( isdefined( additionalText ) );
			//console.log( additionalText );
			$('windowContent').innerHTML = responseHTML;
			if( isdefined( additionalText ) ){
				if( additionalText == 'startDiet' ){
					$('windowContent').innerHTML += '<br/><br/><form method="post" name="phasePlan" action="index.php?a=' + actionID + '"><div><input class="blueBtn" type="submit" value="Dieet Starten" style="float:left;margin-right:10px;" /><input onclick="hideWindow()" class="blueBtn" type="button" value="Annuleren" style="float:left;" /><div style="clear:both;"></div></div></form>';
				}
			}

			var h = $('windowContent').offsetHeight;
			$('window').style.height = h + 'px';
			$('window').style.marginTop = '-' + ( h / 2 ) + 'px';

			var w = $('windowContent').offsetWidth;
			$('window').style.width = w + 'px';
			$('window').style.marginLeft = '-' + ( w / 2 ) + 'px';

		}
	});
	myRequest.send('a=' + theActionID + '&action=doAjax&pageID=' + thePageID + '&ajaxPopup=1');
	$('window').removeClass('windowHidden');
	$('window').addClass('windowDisplay');
	$('tintScreen').removeClass('tintHidden');
	$('tintScreen').addClass('tintDisplay');
}
function showWindow2(pageID,actionID){
	initWindowTint();
	thePageID = pageID;
	theActionID = actionID;
	var tmp = false;
	//periodical = getText.periodical(1 * 500, this);

	var myRequest = new Request.HTML({
		method: 'get',
		url: 'index.php',
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			$('windowContent').innerHTML = responseHTML;
		}
	});
	myRequest.send('a=' + theActionID + '&action=doAjax&pageID=' + thePageID);
	$('window').removeClass('windowHidden');
	$('window').addClass('windowDisplay2');
	$('tintScreen').removeClass('tintHidden');
	$('tintScreen').addClass('tintDisplay');
}
function hideWindow2(){
	$('windowContent').innerHTML =  '<div style="text-align:center;margin-top:220px;"><img src="/layout/default/gfx/ajax-loader.gif" /></div>';
	$('tintScreen').removeClass('tintDisplay');
	$('tintScreen').addClass('tintHidden');
	$('window').removeClass('windowDisplay2');
	$('window').addClass('windowHidden');
}
function hideWindow(){
	$('windowContent').innerHTML =  '<div style="text-align:center;margin-top:220px;"><img src="/layout/default/gfx/ajax-loader.gif" /></div>';
	$('tintScreen').removeClass('tintDisplay');
	$('tintScreen').addClass('tintHidden');
	$('window').removeClass('windowDisplay');
	$('window').addClass('windowHidden');
}
/*var getText = (function() {
	var myRequest = new Request.HTML({
		method: 'get',
		url: 'index.php',
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			$('windowContent').innerHTML = responseHTML;
		}
	});
	myRequest.send('a=' + theActionID + '&action=doAjax&pageID=' + thePageID);
});*/
function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
		//console.log( window.innerHeight + window.scrollMaxY );
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
		//console.log( document.body.scrollHeight );
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
		//console.log( document.body.offsetHeight );
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		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
	//console.log( yScroll );
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;

}

function addListItem( a ){
	if( $( 'prodLabel' ).value != '' ){
		var myRequest = new Request.HTML({
			method: 'get',
			url: 'index.php',
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				$('items').innerHTML = responseHTML;
				$( 'prodLabel' ).value = '';
			}
		});
		myRequest.send('a=' + a + '&action=doAjax&pageID=' + a + '&do=add&label=' + $( 'prodLabel' ).value);
	}else{
		$( 'errorMsg' ).innerHTML = 'Geen productnaam ingevoerd.';
	}
}
function addToList( id , label , elm , week , a ){
	if( elm.className == 'check_unchecked' ){
		elm.className = 'check_checked';
		var myRequest = new Request.HTML({ method: 'get', url: 'index.php' });
		myRequest.send('a=' + a + '&action=doAjax&do=addToList&templateID=' + id + '&week=' + week);
	}else{
		elm.className = 'check_unchecked';
		var myRequest = new Request.HTML({ method: 'get', url: 'index.php' });
		myRequest.send('a=' + a + '&action=doAjax&do=delFromList&templateID=' + id + '&week=' + week);
	}
	return false;
}
function ajax_removeToCart( artID ){
	var myRequest = new Request({
		method: 'get',
		url: 'index.php?a=11&action=doAjax&ajax=delCart&articleID=' + artID,
		onSuccess: function(responseText) {
			$('sCart').innerHTML =  responseText;
		}
	});
	myRequest.send();
	return false;
}

////////////////////////////////////

function showXtiveText( a , p ){
	initWindowTint();
	var tmp = false;
	//periodical = getText.periodical(1 * 500, this);

	var myRequest = new Request.HTML({
		method: 'get',
		url: 'index.php?ajaxPopup=1',
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			$('windowContent').innerHTML = responseHTML;
		}
	});
	myRequest.send( 'a=' + a + '&p=' + p );
	$('window').removeClass('windowHidden');
	$('window').addClass('windowDisplay2');
	$('tintScreen').removeClass('tintHidden');
	$('tintScreen').addClass('tintDisplay');
}

var initAccordion = function ( num ){
	var stretchers = $$( 'div.atStart' );
	var togglers = $$( 'h2.atStart' );
	var accordion = new Accordion( togglers , stretchers , {
		display: num ,
		opacity: false ,
		onActive: function( togglers , stretchers ){},
		onBackground: function( togglers , stretchers ){}
	} , $( 'sidebar' ) );
}

function printPopup( str ){
	var content=$( 'popup' ).innerHTML
	var pwin=window.open('http://www.ease4life.nl/?xtiveText=' + str,'print_content','width=100,height=100');

	//pwin.document.open();
	///pwin.document.write('<html><head><link href="http://www.ease4life.nl/layout/default/style.css" rel="stylesheet" type="text/css"  media="screen" /></head><body>'+content+'</body></html>');
	//pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
	//pwin.document.close();

	setTimeout(function(){pwin.close();},1000);
}

function showPopup( str , w , h , close , params , callbackFunction , callbackParam ){

	var pageSize = getPageSize();

	//console.log( pageSize );
	if( params == 'xtiveText' ){
				$( 'popup_tint' ).style.display = 'block';
				$( 'popup' ).style.display = 'block';
		//return false;
		var strurl = str;
		var popupRequest = new Request.HTML({
			method: 'get',
			url: 'index.php?a=108&p=157&path=' + str,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				str = responseHTML;
				if( close != false ) str = '<img src="gfx/popup_close.png" class="hide" onclick="hidePopup();" />' + str;
				if( strurl == '/Bronnen:Algmemene Voorwaarden' ){
					str = '<img src="gfx/popup_save.png" class="save" onclick="window.location = \'https://www.ease4life.nl/pdf/ease4life_Algemene_Voorwaarden.pdf\';" />' + str;
					str = '<img src="gfx/popup_print.png" class="print" onclick="printPopup( \'' + strurl +'\' );" />' + str;
				}
				$( 'popup' ).innerHTML = str;
				var padding = 10;
				if( params == 'noPad' ){ padding = 0; }
				w = ( w + ( padding * 2 ) );
				h = ( h + ( padding * 2 ) );
				$( 'popup' ).style.padding = padding + 'px';
				$( 'popup' ).style.width = w + 'px';
				$( 'popup' ).style.height = h + 'px';
				$( 'popup' ).style.zIndex = 900;
				$( 'popup' ).style.marginLeft = '-' + ( ( w / 2 ) + padding ) + 'px';
				$( 'popup' ).style.marginTop = '-' + ( ( h / 2 ) + padding ) + 'px';

				if( $chk( callbackFunction ) ){
					callbackFunction();
				}
			}
		});
		popupRequest.send();
	}else if( params == 'ajax' ){
		var popupRequest = new Request.HTML({
			method: 'get',
			url: str,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				str = responseHTML;
				if( close != false ) str = '<img src="gfx/popup_close.png" class="hide" onclick="hidePopup();" />' + str;
				$( 'popup_tint' ).style.display = '';
				$( 'popup' ).style.display = '';
				$( 'popup' ).innerHTML = str;
				var padding = 10;
				if( params == 'noPad' ){ padding = 0; }
				w = ( w + ( padding * 2 ) );
				h = ( h + ( padding * 2 ) );
				$( 'popup' ).style.padding = padding + 'px';
				$( 'popup' ).style.width = w + 'px';
				$( 'popup' ).style.height = h + 'px';
				$( 'popup' ).style.zIndex = 900;
				$( 'popup' ).style.marginLeft = '-' + ( ( w / 2 ) + padding ) + 'px';
				$( 'popup' ).style.marginTop = '-' + ( ( h / 2 ) + padding ) + 'px';

				if( $chk( callbackFunction ) ){
					callbackFunction( callbackParam );
				}
			}
		});
		popupRequest.send();
	}else{
		if( close != false ) str = '<img src="gfx/popup_close.png" class="hide" onclick="hidePopup();" />' + str;
		$( 'popup_tint' ).style.display = '';
		$( 'popup' ).style.display = '';
		$( 'popup' ).innerHTML = str;
		var padding = 10;
		if( params == 'noPad' ){ padding = 0; }
		w = ( w + ( padding * 2 ) );
		h = ( h + ( padding * 2 ) );
		$( 'popup' ).style.padding = padding + 'px';
		$( 'popup' ).style.width = w + 'px';
		$( 'popup' ).style.height = h + 'px';
		$( 'popup' ).style.zIndex = 900;
		$( 'popup' ).style.marginLeft = '-' + ( ( w / 2 ) + padding ) + 'px';
		$( 'popup' ).style.marginTop = '-' + ( ( h / 2 ) + padding ) + 'px';

		if( $chk( callbackFunction ) ){
			callbackFunction();
		}
	}
}

function hidePopup(){
	$( 'popup_tint' ).style.display = 'none';
	$( 'popup' ).style.display = 'none';
	$( 'popup' ).innerHTML = '';
}

function reviewDay( theUrl ){
	var popupRequest = new Request.HTML({
		method: 'get',
		url: theUrl,
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			$('dayInfo').innerHTML = responseHTML;
		}
	});
	popupRequest.send();
}

function changeStep( phase ){
	$( 'step_3' ).style.display = '';
	if( phase == 1 ){
		$( 'msg_2_1' ).style.display = '';
		$( 'msg_3_1' ).style.display = '';
		$( 'step_2' ).style.display = '';
		//$( 'phaseIntro' ).style.display = '';
	}else{
		$( 'msg_2_1' ).style.display = 'none';
		$( 'msg_3_1' ).style.display = 'none';
		$( 'step_2' ).style.display = 'none';
		//$( 'phaseIntro' ).style.display = 'none';
	}
}

/**
 * Check if flash is enabled
 * @returns Boolean
 */
function checkFlash(){
	var check1 = navigator.mimeTypes[ "application/x-shockwave-flash" ];
	var check2 = navigator.plugins[ "Shockwave Flash" ];
	var check3 = navigator.plugins[ "Shockwave Flash 2.0" ];
	if( 
		typeof( check1 ) === 'undefined' && 
		typeof( check2 ) === 'undefined' && 
		typeof( check3 ) === 'undefined'
	)
		return false;
	return true;
}