/** Set a cookie.
  * @param cookieName   Name of the cookie
  * @param cookieValue  Value of the cookie
  * @param nDays		Number of days to save the cookie
  */
function setCookie(cookieName,cookieValue,nDays)
{
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0)
		nDays=1;

	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

/** Read a cookie */
function readCookie(cookieName)
{
	var theCookie=""+document.cookie;

	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") 
		return "";

	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) 
		ind1=theCookie.length;

	var value = unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	if (value == ';')
		value = '';
	
	return value;
}

/** Returns the pressed char from a key event */
function getPressedChar(e)
{
	if(window.event) { // for IE, e.keyCode or window.event.keyCode can be used
		key = e.keyCode;
	}
	else if(e.which) { // netscape
		key = e.which;
	}
	else { // no event, so pass through
		return 0;
	}

	return key;
}

/** Go to a specific location */
function go(url)
{
	document.location = url;
}

/** Popup window (no toolbar, resizable, no scrollbars 
  * Please note that the caption may NOT have spaces 
  */
function popup(caption, url, width, height)
{
	window.open(url, caption, 'toolbar=no,resizable=yes,scrollbars=yes,left=0,top=0,screenX=0,screenY=0,width='+width+',height='+height);
}

/** Refresh (reload) the page */
function refresh()
{
	window.location.reload();
}

/** Select a value in the listbox.
  * @param list The select list that we should select a row in
  * @param value The row with this value will be selected
  */
function selectValue(list, value)
{
	for (i = 0; i < list.options.length; ++i)
	{
		if (list.options[i].value == value)
		{
			list.selectedIndex = i;
			return true;
		}
	}
	return false;
}

/** Checks if a date is correct */
function checkDate(inputDate, name)
{
	if (inputDate.value == '') { alert('Du måste ange ' + name + '.'); return false; };
	if (inputDate.value.substr(4,1) != '-') { alert('Du måste ange ett korrekt ' + name + '.'); return false; };
	if (inputDate.value.substr(7,1) != '-') { alert('Du måste ange ett korrekt ' + name + '.'); return false; };
	if (isNaN(inputDate.value.substr(0,4))) { alert('Du måste ange ett korrekt ' + name + '.'); return false; };
	if (isNaN(inputDate.value.substr(5,2))) { alert('Du måste ange ett korrekt ' + name + '.'); return false; };
	if (isNaN(inputDate.value.substr(8,2))) { alert('Du måste ange ett korrekt ' + name + '.'); return false; };
	if (inputDate.value.substr(8,2) > 31) { alert('Du måste ange ett korrekt ' + name + '.'); return false; };
	if (inputDate.value.substr(5,2) > 12) { alert('Du måste ange ett korrekt ' + name + '.'); return false; };
	if (inputDate.value.substr(0,4) < 1900) { alert('Du måste ange ett korrekt ' + name + '.'); return false; };
	return true;
}

/** Checks if a time is corect */
function checkTime(inputTime, name)
{
	if (inputTime.value == '') { alert('Du måste ange ' + name + '.'); return false; };
	if (inputTime.value.substr(2,1) != ':') { alert('Du måste ange en korrekt ' + name + '.'); return false; };
	if (isNaN(inputTime.value.substr(0,2))) { alert('Du måste ange en korrekt ' + name + '.'); return false; };
	if (isNaN(inputTime.value.substr(3,2))) { alert('Du måste ange en korrekt ' + name + '.'); return false; };
	if (inputTime.value.substr(0,2) > 23) { alert('Du måste ange en korrekt ' + name + '.'); return false; };
	if (inputTime.value.substr(3,2) > 59) { alert('Du måste ange en korrekt ' + name + '.'); return false; };
	return true;
}

/** Adds a value to a input text if it's empty when loosing focus */
function myBlur(box, value)
{
	if (box.value == "")
		box.value = value;
}

/** Removes the default value from a <input> text when it get's focus */
function myFocus(box, value)
{
	if (box.value == value)
	box.value = "";
}