
// opens url in the specified target: "self", "top", "blank"
function tvgs_open_url(url, target)
{
	
	
	if (target == 'top' || target == '_top')
	{
		
		top.location.href = url;
		
		return;
	
	}
	else if (target == 'blank' || target == '_blank')
	{
		
		window.open(url);
		
		return;
	
	}
	else if (target == null || target == '' || target == 'self' || target == '_self')
	{
		
		//alert('url redirect:' + url);
		window.location.href = url;
		
		return;
	
	}
	else
	{
	
		return;
		
	}
	
}


// looks for the first form node enclosing the submit element, then submits the form
function tvgs_get_enclosing_form_element(nested_submit_element)
{
	
	// set search parameters
	var candidate_element = nested_submit_element.parentNode;


	while(candidate_element)
	{
	
		if (candidate_element.nodeName == 'FORM')
		{
			
			//candidate_element.submit();
			
			
			return candidate_element;
			
		}
		else if (candidate_element.nodeName == 'HTML')
		{
			
			return false;
			
		}
		else
		{
		
			// update search element
			candidate_element = candidate_element.parentNode;

		}
		
	}
	
	
	return false;

}



function background_color_transition(element)
		{
		
			var colors = [];
			colors[0] = '#88007D';
			colors[1] = '#880078';
			colors[2] = '#890073';
			colors[3] = '#8A006C';
			colors[4] = '#8B0065';
			colors[5] = '#8C005E';
			colors[6] = '#8D0056';
			colors[7] = '#8E004F';
			colors[8] = '#8F0047';
			colors[9] = '#900041';
			colors[10] = '#91003B';
			colors[11] = '#910035';
			
			
			
			var steps = colors.length;
			var step = 0;
			
			function color_step()
			{
				
				if (step == steps)
				{
				
					clearInterval(interval_id);
					
					//alert('interval cleared');
					return;
				}
				
				
				
				element.style.backgroundColor = colors[step];
				
				
				step = step + 1;
				//alert(step);
			
			}
			
			
			
			
			//instruction = 'this.style.backgroundColor='';
			
			var interval_id = setInterval(color_step,70);
			
			element.name = interval_id;
			
			//alert(interval_id);
			
		
		
		
		}


	
		function color_pulse(color_list, element, style_attribute, speed)
		{
		/*
		
		 // color list example
			var colors = [];
			colors[0] = '#88007D';
			colors[1] = '#880078';
			colors[2] = '#890073';
			colors[3] = '#8A006C';
			colors[4] = '#8B0065';
			colors[5] = '#8C005E';
			colors[6] = '#8D0056';
			colors[7] = '#8E004F';
			colors[8] = '#8F0047';
			colors[9] = '#900041';
			colors[10] = '#91003B';
			colors[11] = '#910035';
			
			*/
			
			var steps = color_list.length - 1;
			var step = 0;
			var direction = 'up';
			
			function color_step()
			{
				 
					// determine step
				 if (steps > 0)
					{
						
	
									if (step < steps && direction == 'up')
									{
										
										step = step + 1;
										
									
										
										//clearInterval(interval_id);
										
										//alert('interval cleared');
										//return;
									
									}
									else if (step == steps)
									{
										direction = 'down';
										step = step - 1;
										
									}
									else if (step > 0 && direction == 'down')
									{
										
										step = step - 1;
										
									}
									else if (step == 0)
									{
										direction = 'up';
										step = step + 1;
										
									}
				
						}
				
				
				  eval('element.style.' + style_attribute + ' = color_list[step];');
			  	//element.style.style_attribute = colors[step];
				
				
			  
			
			}
			
			
			
			
			//instruction = 'this.style.backgroundColor='';
			
			window.interval_id = setInterval(color_step, speed);
			
			//element.name = interval_id;
			
			//alert(interval_id);
			
		
		
		
		}
		
		
		






/* This function switches display of two page elements (one disappears, the other appears... and vice versa) */


function tvgs_display_switch(id1,id2)
{
	
	// check if elements exist
	if (document.getElementById(id1) && document.getElementById(id2))
	{
	
		// modified to always show one or the other
		// the original (escaped below) made it possible to accidentally show or hide both elements
		
		var el1 = document.getElementById(id1);
		var el2 = document.getElementById(id2);
	
		if ( el1.style.display != 'none' )
		{
			el1.style.display = 'none';
			el2.style.display = '';
		}
		else 
		{
			el1.style.display = '';
			el2.style.display = 'none';
		}
		
		/*
		if ( el2.style.display != '' )
		{
			el2.style.display = '';
		}
		else
		{
			el2.style.display = 'none';
		}
		*/
	
	}
	
}

// compatibility fix
function switchDisplay(id1,id2)
{

	tvgs_display_switch(id1,id2);

}



// shows an element by id
function tvg_show_element_by_id(id1)
{
	
	// check if element exists
	if (document.getElementById(id1))
	{
	
		
	
		var el1 = document.getElementById(id1);
		
		//if (document.getElementById('sm_js_console')) document.getElementById('sm_js_console').innerHTML += 'Popup type 1: status of "' + id1 + '" display is "' + el1.style.display + '".<br>';
		/*
		if ( el1.style.display != 'none' )
		{
				
				return;
				
		}
		else
		{
		*/
			el1.style.display = '';
		
		//}
		
	}
	
}


// hides an element
function tvg_hide_element(el1)
{
	
	// check if element exists
	if (el1)
	{
	
		
	
		el1.style.display = 'none';
		
		if (document.getElementById('sm_js_console')) document.getElementById('sm_js_console').innerHTML += 'General function: hiding element.<br>';
	
	}
	
}


// shows or hides an element based on its current display state
function tvgs_display_toggle(id1)
{
	
	// check if element exists
	if (document.getElementById(id1))
	{
	
		var el1 = document.getElementById(id1);
		
		
		if (el1.style.display != 'none')
		{
				
			el1.style.display = 'none';
				
		}
		else
		{
	
			el1.style.display = '';
		
		}
		
	}
	
}


// compatibility fix
function tvg_display_toggle(id1,id2)
{

	tvgs_display_toggle(id1,id2);

}




// shows or hides an image by growing and shrinking
// todo: calculate step trajectory
// note: the image will be really gone from the screen when shrunk, and have its display set to none
// note: step_size defaults to 1 pixel
// note: axis defaults to "both" or "xy" when omitted, and accepts "horizontal", "h", "x", "vertical", "v", "y"
function tvgs_img_pop(img_id, step_size, axis)
{
	
	// check if element exists
	if (document.getElementById(img_id))
	{
	
		var image = document.getElementById(img_id);
		
		// check if its an image
		if (image.nodeName == 'IMG')
		{
			
			// get original height
			if (image.style.height != '')
			{
				var image_height = image.style.height.replace(/px/, '') * 1; // style values are strings
			}
			else
			{
				var image_height = image.height; // properties are integers
			}
			
			
			// get original width
			if (image.style.width != '')
			{
				var image_width = image.style.width.replace(/px/, '') * 1; // style values are strings
			}
			else
			{
				var image_width = image.width; // properties are integers
			}
			
			
			// set pixel step size	
			if (step_size == null || step_size < 1)
			{
				var img_pop_step_size = 1;
			}
			else
			{
				var img_pop_step_size = Math.round(step_size);
			}
			
			
			// set direction
			if (axis == 'horizontal' || axis == 'x') axis = 'h';
			if (axis == 'vertical' || axis == 'y') axis = 'v';
			if (axis == 'both' || axis == 'xy') axis = null;
		

			// set pop parameters
			// grow or shrink is based on visibility
			if (image.style.display != 'none')
			{

				var pop_action = 'shrink';
				
				// shrink from image width
				var adjusted_width = image_width;
				var adjusted_height = image_height;
			
			}
			else
			{

				var pop_action = 'grow';
				
				// grow from 0 pixels
				var adjusted_width = 0;
				var adjusted_height = 0;
				
				// inverted adjustment for growing from axis
				if (axis == 'h') adjusted_height = image_height;
				if (axis == 'v') adjusted_width = image_width;
			
			}

			
			// repeater
			function img_pop_step()
			{
				
				if (pop_action == 'shrink')
				{
					
					// shrink
					if (axis == 'h' || axis == null) adjusted_width = adjusted_width - img_pop_step_size;
					if (axis == 'v' || axis == null) adjusted_height = adjusted_height - img_pop_step_size;
					
					if(adjusted_width < 1 || adjusted_height < 1)
					{

						// hide image and restore size
						image.style.display = 'none';
						image.style.width = image_width + 'px';
						image.style.height = image_height + 'px';
						
						clearInterval(img_pop_interval_id);
						
						return;
					
					}
					else
					{
					
						// shrink width
						image.style.width = adjusted_width + 'px';
						image.style.height = adjusted_height + 'px';
						
					}
					
				}
				else
				{
					
					// grow
					if (axis == 'h' || axis == null) adjusted_width = adjusted_width + img_pop_step_size;
					if (axis == 'v' || axis == null) adjusted_height = adjusted_height + img_pop_step_size;
					
					if (adjusted_width >= image_width) adjusted_width = image_width;
					if (adjusted_height >= image_height) adjusted_height = image_height;
					
					if(adjusted_width == image_width && adjusted_height == image_height)
					{
					
						// end growing
						image.style.width = image_width + 'px';
						image.style.height = image_height + 'px';
						
						clearInterval(img_pop_interval_id);
						
						return;
					
					}
					else
					{
						
						// grow width
						image.style.width = adjusted_width + 'px';
						image.style.height = adjusted_height + 'px';
						
						// make image visible
						if (image.style.display == 'none')
						{
							 image.style.display = '';
						}
						
					}
				
				}
			
			}
			
			
			// start animation
			img_pop_interval_id = setInterval(img_pop_step, 30);
			
		}
		
	}
	
}





/* for popups, fades the active part */

function switchDisplayFade(id1,id2)
{
	
	// check if elements exist
	if (document.getElementById(id1) && document.getElementById(id2))
	{
	
		// modified to always show one or the other
		// the original (escaped below) made it possible to accidentally show or hide both elements
		
		var el1 = document.getElementById(id1);
		var el2 = document.getElementById(id2);
	
		if ( el1.style.display != 'none' )
		{
			fade_out_element(el1, 10);
			el2.style.display = '';
		}
		else 
		{
			el1.style.display = '';
			fade_out_element(el2, 10);
		}
		
		
		
		
		/*
		if ( el2.style.display != '' )
		{
			el2.style.display = '';
		}
		else
		{
			el2.style.display = 'none';
		}
		*/
	
	}
	
}


function fade_out_element(id, step_size)
{

	// check if elements exist
	if (document.getElementById(id))
	{
	
		var element = document.getElementById(id);
		
	
	
	
	
		// work with 2 counters for different browsers
	
		//var start_opacity = 1;
		//var step_opacity_size = step_size / 100;
		
		var start_filter = 100;
		var step_filter_size = step_size;
		
		
		
		
		// decrease opacity first step
		//start_opacity = start_opacity - step_opacity_size;
		//start_filter = start_filter - step_filter_size;
		
		
		// make style strings first step
		//var step_opacity_string = '0.' + start_filter;
		//var step_filter_string = 'alpha(opacity=' + start_filter + ')';
			
			
		
		function opacity_step()
		{
			
			// decrease opacity
			//start_opacity = start_opacity - step_opacity_size;
			start_filter = start_filter - step_filter_size;
		
			//if (document.getElementById('sm_js_console')) document.getElementById('sm_js_console').innerHTML += 'Popup: stepping opacity to ' + step_opacity_string + ' / ' + step_filter_string + '.<br>';	
			
			// intercept end
			if (start_filter <= 0)
			{
				
				// stop fading
				clearInterval(window.fade_out_interval_id);
				window.fade_out_interval_id = 0;
				
				// hide element
				element.style.display = 'none';
				
				// restore opacity
				element.style.opacity = '1';
				element.style.filter = 'alpha(opacity=100)';
			
				//return;
				
			}
			else
			{
			
				// make style strings
				step_opacity_string = '0.' + start_filter;
				step_filter_string = 'alpha(opacity=' + start_filter + ')';
				
				
				// update element
				element.style.opacity = step_opacity_string;
				element.style.filter = step_filter_string;
			
			}
			
		
		}
		
		
		if (!window.fade_out_interval_id || window.fade_out_interval_id == 0)
		{
		
			window.fade_out_interval_id = setInterval(opacity_step, 50);
		
		}
		
		
		
	}
	
	//element.name = interval_id;
	
	//alert(interval_id);

}




function multiToggleDisplay(elements,method,cookie_name)
{
	
	
	var display_method = '';
	
	for (var i in elements)
	{
		
		if (method == 'swap')
		{
		
			if ( elements[i].style.display != 'none' )
			{
			
				elements[i].style.display = 'none';
				
				// sample
				display_method = 'hide';
				
			}
			else 
			{
			
				elements[i].style.display = '';
				
				// sample
				display_method = 'show';
				
			}
			
		}
		else if (method == 'show')
		{
		
			 elements[i].style.display = '';
			 
			 // sample
			 display_method = 'show';
			
		}
		else if (method == 'hide')
		{
		 
			 elements[i].style.display = 'none';
			 
			 // sample
			 display_method = 'hide';
		
		}
		
	}
	
	
	
	if (cookie_name)
	{
		// the last element to be updated has been sampled for resulting display style
		// this is now set in a cookie, with a custom name
    	Set_Cookie( cookie_name, display_method, '', '/', '', '' );
	}
	
	
}





// check for display state of block identifiers and toggle on or off
function setBlockIdentifiers()
{
	
	
	
	if (Get_Cookie('block_identifier'))
	{
		
		
		if ( Get_Cookie('block_identifier') == 'show')
		{	
			
			// apply fetched display state
		  	multiToggleDisplay(tvg_get_elements_by_class('block_identifier', 'interface'), 'show', 'block_identifier');
			
		}
		else
		{	
		
			// assume it's the other way round
			multiToggleDisplay(tvg_get_elements_by_class('block_identifier', 'interface'), 'hide', 'block_identifier');
			
		}
	
	
	}



}

// check for display state of block identifiers and toggle on or off
function setMultiToggleDisplay(cookie_name,elements_class,elements_class_exeption)
{
	
	
	
	if (Get_Cookie(cookie_name))
	{
		
		
		if ( Get_Cookie(cookie_name) == 'show')
		{	
			
			// apply fetched display state
		  	multiToggleDisplay(tvg_get_elements_by_class(elements_class,elements_class_exeption), 'show', cookie_name);
			
		}
		else
		{	
		
			// assume it's the other way round
			multiToggleDisplay(tvg_get_elements_by_class(elements_class,elements_class_exeption), 'hide', cookie_name);
			
		}
	
	
	}



}







function getElementsByClass(searchClass,exeption,node,tag)
{
	
	//alert(searchClass);
	
	var teststring = 'test string test';
	
	//alert(teststring.indexOf('string'));
	
	var classElements = new Array();
	
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
		
	var els = node.getElementsByTagName(tag);
	//var elsLen = els.length;
	
	//var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	//var j = 0;
	for (var i in els)
	{
		
		//classElements[classElements.length] = els[i].className;
		
		if(els[i].className != '' && is_numeric(i))
		{
			
			var classname_to_check = els[i].className;
			
			//classElements[classElements.length] = i;
			
			//classElements[classElements.length] = classname_to_check.indexOf(searchClass);
		
		
			if ( classname_to_check.indexOf(searchClass) > -1 && classname_to_check.indexOf(exeption) == -1)
			{
				
				classElements[classElements.length] = els[i];
				
			}
		
		
		}	
		
	}
	
	//alert(classElements);
	
	
	
	
	return classElements;
}


function tvg_get_elements_by_class(search_class, exception_class, search_node, search_tag)
{
	
	// set up search
	
	if (search_node == null) search_node = document;
	
	if (search_tag == null) search_tag = '*';
	
	var all_elements = search_node.getElementsByTagName(search_tag);

	
	if (document.getElementById('sm_js_console')) document.getElementById('sm_js_console').innerHTML += 'Class search: ' + search_class + ' <br>';
	
	
	// inspect elements
	
	var selected_elements = [];
	
	var classnames_to_check = [];
			
	var element_accepted = false;
	
	for (var i in all_elements)
	{
		
		if (is_numeric(i) && all_elements[i].className != '')
		{
			
			classnames_to_check = all_elements[i].className.split(' ');
			

			element_accepted = false;
			
			
			
			if (classnames_to_check == search_class)
			{
					
				element_accepted = true;
						
			}
			else
			{
			
				
				for (var j in classnames_to_check)
				{
					
					if (is_numeric(j))
					{
						
						if (classnames_to_check[j] == search_class)
						{
						
							element_accepted = true;
							
						}
						
						if (exception_class != null && classnames_to_check[j] == exception_class)
						{
							
							// break on exeption class
							element_accepted = false;
							break;
							
						}
					
					}
				
				}
				
				
				
			
			}
			
			if (element_accepted == true)
			{
				
				// add to array
				selected_elements[selected_elements.length] = all_elements[i];
				
				
				
			}
	
		}
		
	}


	return selected_elements;

} // end function tvg_get_elements_by_class





function is_numeric( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: David
    // +   improved by: taith
    // *     example 1: is_numeric(186.31);
    // *     returns 1: true
    // *     example 2: is_numeric('Kevin van Zonneveld');
    // *     returns 2: false
    // *     example 3: is_numeric('+186.31e2');
    // *     returns 3: true
 
    return !isNaN(mixed_var * 1);
}




/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
/*
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
*/

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




function setFieldValues()
{
	
	if (document.getElementById('basic_search_all_input'))
	{
		
		var field_1 = document.getElementById('basic_search_all_input');
	
		field_1.value = ' Snel zoeken...';
	
	}

}












function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
	 
	
// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}		

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
	
/*
// remember, these are the possible parameters for Set_Cookie:
// name, value, expires, path, domain, secure
Set_Cookie( 'test', 'it works', '', '/', '', '' );
if ( Get_Cookie( 'test' ) ) alert( Get_Cookie('test'));
// and these are the parameters for Delete_Cookie:
// name, path, domain
// make sure you use the same parameters in Set and Delete Cookie.
Delete_Cookie('test', '/', '');
( Get_Cookie( 'test' ) ) ? alert( Get_Cookie('test')) : 
alert( 'it is gone');	
*/




// check for sticky display state of two elements and set their display state
function applyStickyDisplayState(obj1,obj2)
{
	
	if ( Get_Cookie(obj1) || Get_Cookie(obj2) )
	{
	
		var el1 = document.getElementById(obj1);
		var el2 = document.getElementById(obj2);
	
		if ( Get_Cookie(obj1) == 'none')
		{	
			// apply fetched display state
			el1.style.display = 'none';
			el2.style.display = '';	
		}
		else
		{	
			// assume it's the other way round
			el1.style.display = '';
			el2.style.display = 'none';	
		}
	
	}

}

// switch display state of two elements and save settings to cookies
function switchDisplaySticky(obj1,obj2)
{
	
	var el1 = document.getElementById(obj1);
	var el2 = document.getElementById(obj2);

	if ( el1.style.display != 'none' )
	{
		el1.style.display = 'none';
		Set_Cookie( obj1, 'none', '', '/', '', '' );
	}
	else 
	{
		el1.style.display = '';
		Set_Cookie( obj1, '', '', '/', '', '' );
	}
	
	if ( el2.style.display != '' )
	{
		el2.style.display = '';
		Set_Cookie( obj2, '', '', '/', '', '' );
	}
	else
	{
		el2.style.display = 'none';
		Set_Cookie( obj2, 'none', '', '/', '', '' );
	}
	
}

