var total_pages;
var page_personalia;
var elementShow		= new Array;
var flash_left = 0;

var oldlayer;
var newlayer;

var ajax_method		= "post";
var dynamicImgCon = false;

// -------------------------------------------------------------------------------------
// Functions for new content layout
function toggle_xfrm ( frm, val ) {
	frm = $( 'extraform_' + frm );
	
	frm.style.display = val;
}

function toggle_ja_bg ( id, answer ) {
	var elem		= document.getElementById ( 'div_ja_' + id );
	var top_table	= document.getElementById ( 'tt_ja_' + id );
	var tooltip		= Array ( document.getElementById ( 'uitt_tt_' + id ), document.getElementById ( 'extra_tt_' + id ) );
	
	if ( answer == "ja" ) {
		class_elem				= elem.className + " div_bg";
		class_top_table			= "div_bg";
	}
	else {
		class_elem				= "div_janee div_ja";
		class_top_table			= "";
	}
	
	elem.className		= class_elem;
	top_table.className	= class_top_table;
}

function full_page_radio_check ( ) {
	var i = 0;
	
	while ( i < document.forms.length ) {
		var y		= 0;
		var frm	= document.forms [i];
		
		while ( y < frm.elements.length ) {
			elem	= frm.elements [y];
			is_ja	= (elem.id).indexOf ( 'ja' ) !== -1;
			
			if ( elem.type == "radio" && (elem.id).length < 11 && is_ja && elem.checked ) {
				xfrm = document.getElementById ( 'extraform_' + elem.name );
				
				if ( xfrm != null ) {
					toggle_xfrm ( elem.name, 'block' );
					toggle_ja_bg ( elem.name, 'ja' );
				}
			}
			
			y++;
		}
		
		i++;
	}
}

function toggle_tooltip ( elem, id, obj ) {
	var tooltip 	= document.getElementById ( elem + '_tt_vraag' + id );
	var frm			= document.getElementById ( 'extraform_vraag' + id );
	var frmbottom	= document.getElementById ( 'extraform_vraag' + id + '_bottom' );
	var c_style		= tooltip.style.display;
	
	var n_style		= ( c_style != 'block' ? 'block' : 'none' );
	
	
	// --------------------------------------
	// Get the right position for the tooltip ( IE-6 only! )
	if ( obj != null && is_IE ( '6.0' ) ) {
		var frm_hidden	= frm.style.display != 'block';
		var offs		= ( frm_hidden ? 0 : frmbottom.offsetTop );
		
		var px			= offs + ( frm_hidden ? 46 : 48 );
		
		tooltip.style.bottom = px;
	}
	// --------------------------------------
	
	tooltip.style.display = n_style;
}

function ie6__hack_hidden_input ( ) {
	var i = 0;
	
	while ( i < document.forms.length ) {
		var y		= 0;
		var frm	= document.forms [i];
		
		while ( y < frm.elements.length ) {
			elem	= frm.elements [y];
			
			if ( elem.type == "hidden" ) {
				elem.className = "hidden_input";
			}
			
			y++;
		}
		
		i++;
	}
}
// -------------------------------------------------------------------------------------


function open_br_window ( theURL, winName, features ) {
  window.open ( theURL, winName, features );
}

function in_array ( stringToSearch, arrayToSearch ) {
	for ( s = 0; s < arrayToSearch.length; s++ ) {
		thisEntry = arrayToSearch [s].toString ();
		if ( thisEntry == stringToSearch ) {
			return true;
		}
	}
	return false;
}

function alert_wrong_change ( page ) {
	switch ( page ) {
		case -1:	
			str = 'vorige';
			break;
		case total_pages + 1:
			str = 'volgende';
			break;
	}
	
	alert ( 'Er is geen ' + str + ' pagina' );
}

function change_page ( action ) {
	var cp = current_page;
	
	action == '+' ? cp++ : cp--;
	
	if ( !in_array ( cp, pages_to_show ) ) {
		while ( !in_array ( cp, pages_to_show ) && valid_cp (cp) ) {
			action == '+' ? cp++ : cp--;
		}
	}
	
	if ( 0 > cp || cp > total_pages ) {
		
	
		
	}
	else {
		
		if(cp!=0){
			if($('FlashContainer')) $('FlashContainer').hide();
			if($('dynamicImgCon')) $('dynamicImgCon').hide();
		}else{
			if($('FlashContainer')) $('FlashContainer').show();
			
			if(dynamicImgCon){
				if($('dynamicImgCon')) $('dynamicImgCon').show();
			}
		}
	
		if((typeof urchinTracker != 'undefined')){ 
			urchinTracker	( '/enqueteEM/pagina_' + cp );
		}
		set_page		( cp, current_page );
	}
}

function set_page ( new_p, old_p ) {
	oldlayer		= old_p;
	newlayer		= new_p;
	current_page	= new_p;
	
	do_page_change ( );
}

function do_page_change ( ) {
	var div_			= 'pagelayer_';
	var block			= 'block';
			
	oldlayer			= div_ + oldlayer;
	newlayer			= div_ + newlayer;
	
	if ( is_IE () ) block = 'inline-' + block;
	
	with ( document ) {
		// Hide current layer
		getElementById ( oldlayer ).style.display	= 'none';
		
		// Display new layer
		getElementById ( newlayer ).style.display	= block;
	}
	
	check_pagecurls ( current_page );
	
	set_progress ( current_page );
}

function do_flash ( movie, flhoogte, flbreedte, flleft, fltop ) {
	var h = flhoogte;
	var w = flbreedte;
	
	var flash_cont = document.createElement('div');
	flash_cont.setAttribute('id', 'FlashContainer');
	flash_cont.style.position = 'absolute';
	flash_cont.style.display = 'block'; 
	
	var myflashhtml = ('<object  classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+w+'" height="'+h+'" align="middle">');
	myflashhtml += ('		<param name="allowScriptAccess" value="sameDomain" />');
	myflashhtml += ('		<param name="allowFullScreen" value="false" />');
	myflashhtml += ('		<param name="movie" value="'+movie+'" />');
	myflashhtml +=  ('		<param name="quality" value="high" />');
	//document.write('		<param name="extraUrl" value="id=1" />');
	myflashhtml += ('		<!-- <param name="bgcolor" value="#ffffff" /> -->');
	myflashhtml += ('		<param NAME="wmode" Value="Transparent">	');
	myflashhtml += ('		<embed src="'+movie+'" wmode="Transparent" quality="high" width="'+w+'" height="'+h+'" name="scoot-media-props" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	myflashhtml += ('	</object>');
	
	flash_cont.innerHTML = myflashhtml;
	flash_cont.style.top = (fltop ) + 'px';
		
	document.body.appendChild(flash_cont);
	
	berekenFlashPos(flleft);
	
	if(document.body.addEventListener){
		window.addEventListener('load',function(){berekenFlashPos(flleft);},false);
		window.addEventListener('resize',function(){berekenFlashPos(flleft);},false);
	}else{
		window.attachEvent('onload',function(){berekenFlashPos(flleft);},false);
		window.attachEvent('onresize',function(){berekenFlashPos(flleft);});
	}

	
}

function berekenFlashPos(flleft){
	flash_cont =document.getElementById('FlashContainer');
	main_cont = document.getElementById('container');
	flash_left = flleft ? flleft : 0;
	
	if(main_cont && flash_cont){
		var myLeft = main_cont.offsetLeft;
		flash_cont.style.left = (myLeft+flleft)+'px';
	}
	
	/* Om te voorkomen dat hij in IE op pagina 1/2 de flash laat zien als die nog niet volledig is ingeladen. */
	if(current_page > 0){
		flash_cont.style.display = 'none';
	}
}



function check_pagecurls ( cp ) {
	var curl_l		= $( 'curl_l' );
	var curl_r		= $( 'curl_r' );
	
	if($( 'navb_l' ) != null && $( 'navb_r' ) != null){
		var navb_l		= $( 'navb_l' );
		var navb_r		= $( 'navb_r' );
		
		navb_l.style.display	= ( cp == 0 ? 'none' : 'block' );
		navb_r.style.display	= ( cp == total_pages ? 'none' : 'block' );  
	}
	
	curl_l.style.display	= ( cp == 0 ? 'none' : 'block' );
	curl_r.style.display	= ( cp == total_pages ? 'none' : 'block' );  
}

function set_progress ( page ) {
	var bar				= document.getElementById ( 'progressbar' );
	var barw			= ( page / total_pages ) * pbarsize;
	
	if ( barw == 0 ) barw = 1;
	
	bar.style.width = barw + 'px';
}

function is_IE ( version ) {
	var agent = navigator.userAgent;
	
	return ( version ? agent.match ( 'MSIE(.*?)' + version ) : agent.match ( 'MSIE' ) );
}

function valid_cp ( cp ) {
	if ( 0 > cp || cp > total_pages ) {
		return false;
	}
	else {
		return true;
	}
}

function arrow_navigation ( event ) {
	var key	= event.keyCode;
	var act	= "";
	
	if(key == 37 || key == 39 || key == 35){
		var ele = document.getElementById('FlashContainer');
		if(ele)	ele.style.display = 'none';
	}
	
	
	if ( current_page != page_personalia ) {
		if ( key == 37 )
			act = "-";
		else if ( key == 39 )
			act = "+";
		else if ( key == 35 )
			
			set_page ( total_pages, current_page );
			
		if ( act != "" )
			change_page ( act );
	}
	
	return true;
}

function getAddress ( ) {
	var postcode1	= $( 'postcode1' ).value;
	var postcode2	= $( 'postcode2' ).value;
	var huisnummer	= $( 'huisnummer' ).value;
	
	with ( $( 'straat' ) ) {
		value		= "";
		readOnly	= false;
	}
	with ( $( 'plaats' ) ) {
		value		= "";
		readOnly	= false;
	}
	
	if ( postcode1.match ( /^[1-9]{1}[0-9]{3}$/ ) && postcode2.match ( /^[a-zA-Z]{2}$/ ) && huisnummer != "" ) {
		new Ajax.Request ( "/enquetes/ajax/adresprefill.php",
			{
				method:		ajax_method,
				
				parameters:	{
					postcode1:	postcode1,
					postcode2:	postcode2,
					huisnummer:	huisnummer
				},
				
				onComplete:	function ( transport, json ) {
					if ( json [ 'straat' ]  ) {
						with ( $( 'straat' ) ) {
							value		= json [ 'straat' ];
							readOnly	= true;
						}
					}
					if ( json [ 'woonplaats' ] ) {
						with ( $( 'plaats' ) ) {
							value		= json [ 'woonplaats' ];
							readOnly	= true;
						}
					}
				}
			}
		);
		
	}
}

function getAddress2 ( ) {
	var postcode1	= $( 'postcode1' ).value;
	var postcode2	= $( 'postcode2' ).value;
	var huisnummer	= $( 'huisnummer' ).value;
	
	with ( $( 'straat' ) ) {
		value		= "";
		readOnly	= false;
	}
	with ( $( 'plaats' ) ) {
		value		= "";
		readOnly	= false;
	}
	with ( $( 'telabbo' ) ) {
		value		= "";
		readOnly	= false;
	}
	
	if ( postcode1.match ( /^[1-9]{1}[0-9]{3}$/ ) && postcode2.match ( /^[a-zA-Z]{2}$/ ) && huisnummer != "" ) {
		new Ajax.Request ( "/enquetes/ajax/adresprefill2.php",
			{
				method:		ajax_method,
				
				parameters:	{
					postcode1:	postcode1,
					postcode2:	postcode2,
					huisnummer:	huisnummer
				},
				
				onComplete:	function ( transport, json ) {
					$('telabbo').update();
					
					if ( json [ 'straat' ]  ) {
						with ( $( 'straat' ) ) {
							value		= json [ 'straat' ];
							readOnly	= true;
						}
					}
					if ( json [ 'woonplaats' ] ) {
						with ( $( 'plaats' ) ) {
							value		= json [ 'woonplaats' ];
							readOnly	= true;
						}
					}
					if ( json [ 'netnummer1' ] ) {
						with ( $( 'telabbo' ) ) {
							htmlOption = '<option value='+ json [ 'netnummer1' ] + ' >' + json [ 'netnummer1' ] + '</option>';
							$('telabbo').insert(htmlOption);
						}
					}
					if ( json [ 'netnummer2' ] ) {
						with ( $( 'telabbo' ) ) {
							htmlOption = '<option value='+ json [ 'netnummer2' ] + ' >' + json [ 'netnummer2' ] + '</option>';
							$('telabbo').insert(htmlOption);
						}
					}
					$('telabbo').insert('<option value=06>06</option>');
					
					$('telabbo').insert({top:'<option selected value=Kies>Kies</option>'});
				}
			}
		);
	}
}

/*
 * add the dynamic image in the div
 */
function addDynamicImg(img,left,top){
	img = img.stripTags();
	img = img.stripScripts();
	
	dynamicImgCon = true;
	
	$('dynamicImg').writeAttribute('src', img);
	$('dynamicImgCon').setStyle({
		left: left+'px',
		top: top+'px'
	});
	
	$('dynamicImgCon').show();
}