var img_type		= ".gif";	// variable used to identify the image type, this is for GIF format
var last_img_over	= "";		// variable to remember which image was last over
var clicked			= "";		// variable to remember which image was last click

/**
 * Mouseover for images, changes "m_..." to "mo_..."
 */
function msover(path, img_name)
{
	if ((navigator.userAgent.substring(0,9) == "Mozilla/3") ||
		(navigator.userAgent.substring(0,9) == "Mozilla/4"))
	{
    	if (img_name != clicked)
    	{
			last_img_over = img_name;
			image_name = path + "/" + "mo_" + img_name + img_type;
			document[img_name].src = image_name;
		}
		window.status = document[img_name].alt;
	}
}

/**
 * Mouseout for images, changes "mo_..." to "m_..."
 */
function msout(path, img_name)
{
	if ((navigator.userAgent.substring(0,9) == "Mozilla/3") ||
		(navigator.userAgent.substring(0,9) == "Mozilla/4"))
	{
		if (img_name != clicked)
		{
			document[last_img_over].src = path + "/" + "m_" + last_img_over + img_type;
		}
	}
}

/**
 * Mouseclick for images, changes the previous clicked image back to "m_..."
 * and the current to "mc_..."
 */
function msclick(path, img_name)
{
	if ((navigator.userAgent.substring(0,9) == "Mozilla/3") ||
		(navigator.userAgent.substring(0,9) == "Mozilla/4"))
	{
		if (clicked != "")
		{
			document[clicked].src = path + "/" + "m_" + clicked + img_type;
		}
		clicked = img_name;
		image_clicked = path + "/" + "mc_" + img_name + img_type;
		document[img_name].src = image_clicked;
	}
}

/**
 * Shows the bijschrift for a photo
 */
function ShowBijschrift(number)
{
	var layer = getElement('bijschr_layer' + number);
	var table = getElement('bijschr_table' + number);
	layer.style.top = getTop(table);
	layer.style.left = getLeft(table)+3;
	layer.style.display = "";
}

/**
 * Shows the bijschrift for a photo
 */
function ShowBijschriftWithOffset(number, x, y)
{
	var layer = getElement('bijschr_layer' + number);
	var table = getElement('bijschr_table' + number);
	layer.style.top = getTop(table)+5+y;
	layer.style.left = getLeft(table)-120+x;
	layer.style.display = "";
}

/**
 * Hides the bijschrift
 */
function HideBijschrift(number)
{
	var layer = getElement('bijschr_layer' + number);
   	layer.style.display = "none";
}

/**
 * Shows a dropdown-menu (sub) below an element (main)
 */
function showSubs(mainName, subName)
{
	var mainElement = getElement(mainName);
	var subElement  = getElement(subName);

	if (mainElement && subElement)
	{
		mainLeft = getLeft(mainElement);
		mainTop  = getTop(mainElement);

		subElement.style.top = mainTop + mainElement.offsetHeight;
		subElement.style.left = mainLeft;

		subElement.style.display = "";
	}
}

/**
 * Hides the dropdown-menu below an element
 */
function hideSubs(subName)
{
	var element = getElement(subName);
	if (element)
		element.style.display="none";
}

/**
 * Returns the element in a document which name is the input parameter
 */
function getElement(name)
{
	var ret = null;
	if (document.layers)
	{
		return document.layers[name];
	}
	else if (document.all)
	{
		return document.all[name];
	}
	if (document.getElementsByName)
	{
		var elements = document.getElementsByName(name);
		if (elements.length == 0)
		{
			ret = null;
		}
		else if (elements.length == 1)
		{
			ret = elements[0];
		}
		else
		{
			ret = elements;
		}
	}
	if (ret == null && document.getElementById)
	{
		ret = document.getElementById(name);
	}
	return ret;
}

/**
 * Gets the x-coordinate for an object
 */
function getLeft(obj)
{
	var curleft = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	curleft += obj.offsetLeft;
	
	return curleft;
}

/**
 * Get the y-coordinate for an object
 */
function getTop(obj)
{
	var curtop = 0;

	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	curtop += obj.offsetTop;

	return curtop;
}

/**
 * Tries to set the selectedindex of the selectbox 'obj' to the item which value equals 'val'
 */
function setPopUp(obj, val)
{
	for (i=0; i<obj.length; i++)
	{
		if (obj.options[i].value == val)
		{
			obj.selectedIndex = i;
			break;
		}
	}
}

/**
 * Clears the popup by removing all available options
 */
function clearPopup(p)
{
	p.selectedIndex = 0;
	for(var i=p.length-1; i>=0; i--)
	{
		p.options[i] = null;
	}
}

/**
 * Adds a new option with a given label and value to a popup
 */
function addToPopup(p, label, value)
{
	var opt = new Option(label, value);
	p.options[p.options.length] = opt;
	return opt;
}

/**
 * Deletes all selected options from a popup
 */
function delSelectedFromPopup(p)
{
	for (var i=p.length-1; i>=0; i--)
	{
		if (p.options[i].selected)
		{
			p.options[i] = null;
		}
	}
}

/**
 * Opens a popup window with width = 'w' and height = 'h'
 */
function PopWin(vUrl, name, w, h)
{
	var ret = false;
	WindowDef = "title=no,toolbar=no,scrollbars=yes,resizable=yes,menubar=no,width="+w+",height="+h;
	var dummy = window.open(vUrl, name, WindowDef);
	if (dummy != null)
	{
		ret = true;
	}
	return ret;
}

/**
 * Test if a string contains non-space chars
 */
function isEmpty(string)
{
	return (string.replace(/ /g, "") == "");
}

/**
 * Maxlength for a textarea
 */
function textareaMaxlength(area, maxlength)
{
	var length = area.value.length;
	
	if (length > maxlength)
	{
		area.value = area.value.substring(0, maxlength);
	}
}

/**
 * Clears a form (types cleared: text, checkbox, select)
 */
function clearForm(form)
{
	for (var e=0; e<form.elements.length; e++)
	{
		var elem = form.elements[e];
		if (elem.type == "text")
		{
			elem.value = "";
			elem.disabled = false;
		}
		if (elem.type == "textarea")
		{
			elem.value = "";
			elem.disabled = false;
		}
		if (elem.type == "checkbox")
		{
			elem.checked = false;
		}
		if (elem.type == "select-one")
		{
			elem.selectedIndex = 0;
		}
	}
}

/**
 * Returns the checked radio item
 */
function getRadioChecked(radio)
{
	var ret = null;
	var radiogroup = getElement(radio);
	
	if (radiogroup != null)
	{
		// IE only: Check all the radio items
		for (var i = 0; i < radiogroup.length; i++)
		{
			if (radiogroup[i].checked)
			{
				ret = radiogroup[i];
				return ret;
			}
		}
	}
	return ret;
}

/**
 * Tests if the string "int" is an integer.
 */
function isInt(int)
{
	var regexp = "^(\\d)*$";
	var objRegExp = new RegExp(regexp, "");
	return objRegExp.test(int);
}

/**
 * Tests if the string "date" is a date of the form "dd-mm-yyyy".
 */
function isDMYdate(date)
{
	if (isEmpty(date))
	{
		return true;
	}

	var dateParts = date.split("-");
	
	if (dateParts.length != 3)
	{
		return false;
	}
	
	var strDay = dateParts[0];
	var strMonth = dateParts[1];
	var strYear = dateParts[2];
	strYr = strYear;
	
	if (strDay.charAt(0) == "0" && strDay.length > 1)
	{
		strDay = strDay.substring(1);
	}
	if (strMonth.charAt(0) == "0" && strMonth.length > 1)
	{
		strMonth = strMonth.substring(1);
	}
	
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0) == "0" && strYr.length > 1)
		{
			strYr=strYr.substring(1);
		}
	}
	
	if (!isInt(strDay))
	{
		return false;
	}
	if (!isInt(strMonth))
	{
		return false;
	}
	if (!isInt(strYr))
	{
		return false;
	}
	var day = parseInt(strDay);
	var month = parseInt(strMonth);
	var year = parseInt(strYr);
	
	var maxDays = 31;
	if (month == 4 || month == 6 || month == 9 || month == 11)
	{
		maxDays = 30;	
	}
	else if (month == 2)
	{
		maxDays = (((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) ? 29 : 28 );
	}

	if (strDay.length < 1 || day < 1 || day > maxDays)
	{
		return false;
	}
	if (strMonth.length < 1 || month < 1 || month > 12)
	{
		return false;
	}
	if (strYear.length != 4 || year == 0)
	{
		return false;
	}
	return true;
}

/**
 * Checks if the value of field is a date.
 * Empty values are allowed.
 */
function checkDate(field)
{
	var ret = false;
	if (field != null && (isEmpty(field.value) || isDMYdate(field.value)))
	{
		ret = true;
	}
	return ret;
}

/**
 * If visible is true, set the display style of elem to "block",
 * otherwise, the display style is set to "none"
 */
function setElemVisibility(elem, visible)
{
	var esd = "none";
	if (visible)
	{
		esd = "block";
	}
	
	if (elem != null && elem.style != null && elem.style.display != null)
	{
		elem.style.display = esd;
	}
}
