/* This file contains generic javascript functions to be applied anywhere */

// function openPopup(theURL,winName, features)
// Desc: opens popup window
// Parameters:
// theURL : url of the page you want to open (String)
// winName : name of the new window (String)
// OPTIONAL features
// width : width of window in pixels
// height : height of window in pixels
// toolbar : values yes/no
// location : yes/no
// status : yes/no
// menubar : yes/no
// scrollbars : yes/no
// resizable : yes/no

// EXAMPLE : <a href="#" onClick = "openPopup('http://www.javascript.com', 'javascript', 'width=500, height=500');">Open window</a>
// NB make sure to separate each of the 'features' with a comma

function openPopup(theURL,winName, features) {
    window.open(theURL,winName,features);
    void(0);
}
// function clearField(val)
// should be called from an onFocus event
// clears an input textbox f if the value is val
function clearField(f, val) {
	// alert(field);
    if (f.value == val)
		f.value = "";
}
// function showHide
// @param	div_id	The id of the element to hide/show
function showHide(div_id){
	div = document.getElementById(div_id);
	div.style.display == 'none'?div.style.display = 'block':div.style.display = 'none';
}
// function getURL(form)
// desc: takes user to url selected from select form
// @param	form : reference to the calling form
function goThere(element){
	loc = element.options[element.options.selectedIndex].value;
	if(loc){
		window.top.location.href = loc;
	}
}
// function maxLength (obj)
// checks length of textarea
function ismaxlength(obj, mlength){
	//var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}
// Framework hack to prevent double id attributes
// function removeInputError(obj, defaultVal)
// desc: removes the error msg when a field receives focus
// implementation: onfocus="removeInputError(this);"
function removeInputError(obj, defaultVal){
		obj.className='correcting_input';
		return true;
	}
// function setInputError(obj, defaultVal)
// desc: restores the error msg when a field loses focus and error has not been corrected
// implementation: onblur="setInputError(this,'{naam}');" >> where {naam} should be replaced with the appropriate template variable
function setInputError(obj, defaultVal){
	if(defaultVal==obj.value ) obj.className='error_input';
	return true;
}
// function setBackImg (image , id)
// desc: replaces the background image of element with id 'id' with the image 'image'
// implementation: onclick="setBackImg('wap_mobiel_buienradar.gif','waptop');void(0);"
function setBackImg (image , id) {
  document.getElementById(id).style.backgroundImage = 'url('+image+')';
}
// function submit_confirm(message)
// ask the user for confirmation with the message (message)
function submit_confirm(message){
    return (confirm(message) ? true : false);
}

/**
 * @param element	Can be either a string or a reference to a DOM element. When it's a string it has to be the id of a
 *					DOM element.
 *					The passed element should be either a <a href> or an <input>. This function will make sure it can
 *					only be clicked once.
 *
 * @return void
 */
function submitOnce(element)
{
	if (typeof(element) == 'string')
	{
		if (!document.getElementById(element))
		{
			return;
		}
		var el = document.getElementById(element);
	}
	else
	{
		var el = element;
	}
	if (el.tagName.toUpperCase() == 'A')
	{
		el.onclick = function () { this.onclick = function() { return false; } }
	}
	else if (el.tagName.toUpperCase() == 'INPUT')
	{
		el.onclick = function () { this.disabled = true; }
	}
}