/********************************************************************************\
 *	Open a popup window															*
 *	Params	-> url = url to open in the popup									*
 * 			-> w_name = popup name												*
 * 			-> width, height = popup size										*
 * 			-> opt_str = popup extra params, if = '' then use default params	*
\********************************************************************************/
function open_popup (url, w_name, width, height, opt_str) 
{
	var options = "width="+width+",height="+height;
	if(opt_str == '')
		options += ",resizable=yes,scrollbars=yes,toolbar=no,status=no,directories=no,menubar=no";
	else
		options += ","+opt_str;
	window_detail=window.open(url, w_name, options);
	window_detail.moveTo(10, 10);
	window_detail.focus();
}

function return_open_popup (url, w_name, width, height, opt_str)
{
	var options = "width="+width+",height="+height;
	if(opt_str == '')
		options += ",resizable=yes,scrollbars=yes,toolbar=no,status=no,directories=no,menubar=no";
	else
		options += ","+opt_str;
	window_detail=window.open(url, w_name, options);
	window_detail.moveTo(10, 10);
	window_detail.focus();
	return window_detail;
}

/********************************************************************************\
 *	Return PHP $HTTP_GET_VARS in an array like get_var name => value			*
 *	Params -> window_obj = valid window object Ex: parent.frames.content		*
\********************************************************************************/
function retrieve_get_vars(window_obj)
{
	//var str_url = parent.frames.send.location.href;
	var get_vars = window_obj.location.search;

	//remove ?
	get_vars = get_vars.substring(1,get_vars.length);

	//split get_vars
	var prim_array = get_vars.split("&");

	var array_get_vars = new Array();
	
	//build array like var_name => value
	for(key in prim_array)
	{
		var sec_array = prim_array[key].split("=");
		array_get_vars[sec_array[0]] = sec_array[1];
	}
	
	return array_get_vars;
}

/********************************************************************************\
 *	Remove space at start and/or end of a string							 	*
 *	Params -> str = string to trim												*
 *			  mode = 'l' -> trim start, 'r' -> trim end, 						*
 * 					 'lr' -> trim start and end.								*
\********************************************************************************/
function js_trim(str, mode)
{
	var trimed = '';
	if(mode == 'l')
		trimed = str.replace(/^\s+/,'');
	
	if(mode == 'r')
		trimed = str.replace(/\s+$/,'');
		
	if(mode == 'lr')
	{
		trimed = js_trim(str,'l');
		trimed = js_trim(trimed,'r');
	}
	return trimed;
}


/********************************************************************************\
 *	Test if a string is a valid email address. 									*
 *	Params -> email = string to test											*
\********************************************************************************/
function test_email(email)
{
	var reg = /^\S+@\S+\.\S+$/;
	if(!reg.exec(email))
		return false;
	else
		return true;
}

/********************************************************************************\
 *	Open an alert window which displays all properties of the given object. 	*
 *	Useful in developpement phase.												*
 *	Params -> obj = valid Javascript object or array							*
\********************************************************************************/
function get_object_properties(obj)
{
	if(!obj)
	{
		alert("Invalid object given to get_object_properties(obj)");
		return false;
	}
	
	var str = '';
	for(key in obj)
	{
		str += key+" | ";
		//str += eval(obj+"."+key+";");
	}
	alert(str);
	//document.write(str);
	return true;
}

/********************************************************************************\
 *	Like PHP function print_r in a alert window								 	*
 *	Params -> array = array														*
\********************************************************************************/
function array_print(array)
{
	var str = new String();
	for(key in array)
	{
		str += key+" => "+array[key]+"\n";
	}
	alert(str);
}

/********************************************************************************\
 *	Comes from the CMS														 	*
\********************************************************************************/
function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "; path=/") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

/********************************************************************************\
 *	Comes from the CMS														 	*
\********************************************************************************/
function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	
	return unescape(dc.substring(begin + prefix.length, end));
}

/********************************************************************************\
 *	Comes from the CMS														 	*
\********************************************************************************/
function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" + 
		//((path) ? "; path=" + path : "") +
		((path) ? "; path=" + path : "; path=/") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function buildArgsArray(args)
{
	if(args.length%2)
		alert("Attention!\n"+args.callee+" => arguments impairs");
		
	var argsArray = new Array();
	for(i = 0 ; i < (args.length-1) ; i = i+2)
	{
		argsArray[args[i]] = args[i+1];
	}
	return argsArray;
}

function preload_image(src)
{
	var new_img = new Image();
	new_img.src	= src;
	return new_img;
}

function swap_img(img_name, new_img)
{
	if(document.images[img_name])
	{
		document.images[img_name].src = new_img.src;
		return true;
	}
	else
		return false;
}

function send ( )
{
	// Ueberpruefen, ob ein Name eingegeben wurde.
	if ( document.forms.message._anrede.value == "" )
	{
		// Fehlermeldung anzeigen.
		alert("Bitte geben Sie Ihre Anrede ein.");
		
		// Feld fokusieren.
		document.forms.message._anrede.focus();
	}
	else
	// Ueberpruefen, ob Nachricht.
	if ( document.forms.message._name.value == "" )
	{
		// Fehlermeldung anzeigen.
		alert("Bitte geben Sie Ihren Namen ein.");
	
		// Feld fokusieren.
		document.forms.message._name.focus();
	}
	else
	// Ueberpruefen, ob Nachricht.
	if ( document.forms.message._email.value == "" )
	{
		// Fehlermeldung anzeigen.
		alert("Bitte geben Sie Ihre E-Mail ein.");
	
		// Feld fokusieren.
		document.forms.message._email.focus();
	}
	else
	// Ueberpruefen, ob Nachricht.
	if ( document.forms.message._plz.value == "" )
	{
		// Fehlermeldung anzeigen.
		alert("Bitte geben Sie Ihre PLZ ein.");
	
		// Feld fokusieren.
		document.forms.message._plz.focus();
	}
	else
	// Ueberpruefen, ob Nachricht.
	if ( document.forms.message._ort.value == "" )
	{
		// Fehlermeldung anzeigen.
		alert("Bitte geben Sie Ihre Stadt ein.");
	
		// Feld fokusieren.
		document.forms.message._ort.focus();
	}
	else
	// Ueberpruefen, ob Kundengruppe.
	if ( document.forms.message._kunde_ist.value == "" )
	{
		// Fehlermeldung anzeigen.
		alert("Bitte treffen Sie eine Auswahl: Endverwender, Fachhändler oder Architekt.");
		
		// Feld fokusieren.
		document.forms.message._kunde_ist.focus();
	}
	else
	// Ueberpruefen, ob Nachricht.
	if ( document.forms.message._nachricht.value == "" )
	{
		// Fehlermeldung anzeigen.
		alert("Bitte geben Sie Ihre Nachricht ein.");
	
		// Feld fokusieren.
		document.forms.message._nachricht.focus();
	}
	else
	{
		// Formular absenden.
		document.forms.message.submit();				
	}
}

function toggleBlock(block,mode)
{
	if (mode == 'show')
	{
		document.getElementById(block).style.display="block";
	}
	else if (mode == 'hide')
	{
		document.getElementById(block).style.display="none";
	}
	else // default (mode == 'toggle')
		if (document.getElementById(block).style.display == "none")
		{
			document.getElementById(block).style.display="block";
			//document.getElementById("bullet_" + block).src="Bilder/bullet_on.png";
		}
		else
		{
			document.getElementById(block).style.display="none";
			//document.getElementById("bullet_" + block).src="Bilder/bullet.png";
		}
	return true;
}
