/* <script language="JavaScript" type="text/javascript">					  */
/* ************************************************************************** */
/* JavaScript file split up in the following:                                 */
/* * GENEREL Functions     : generic functions                                */
/* * SPECIAL Functions     : functions specific pages                         */
/* * WINDOW Open Functions : functions for pop-up windows                     */
/*                                                                            */
/* ************************************************************************** */

/* ************************************************************************** */
/* GENEREL ****************************************************************** */
/* ************************************************************************** */

function showPageTitle(str) // writes str to pageTitle-object
{
	var obj = parent.document.getElementById('pageTitle');
	document.title += ' - ' + str;

	obj.innerHTML = '' + str + '';
}

function showNumberOfHits(str) // writes str to numberOfHits-object
{
	var obj = parent.document.getElementById('numberOfHits');

	obj.innerHTML = '' + str + '';
}

function imageToggle(imgId, imgSrc) // replaces src of imgId with imgSrc 
{
	var img = document.getElementById(imgId); // image object

	img.src = imgSrc;
}

function isInt(str) // returns true if str is an int. Unsexy, but it works
{
	var i = parseInt(str);		// parseInt changes str if it is not a clean num

	if (isNaN(i)) return false; // if i is NaN then return false for sure!

	i = i.toString ();			// then we compare the string i to the original 
	if (i != str) return false; // str to see if they differ else...

	return true; 				// the str is an integer!
}

/* ************************************************************************** */
/* SPECIAL ****************************************************************** */
/* ************************************************************************** */

/* Print-functions: --------------------------------------------------------- */

function printPage(toggle) // prepare printing, and toggle criterias on/off
{
	if (toggle) // first we toggle the criterias ON...
	{
		toggleCriterias(); // see toggleCriterias() function elsewhere
	}

	print(); // then we call print()

	if (toggle) // and then we toggle the criterias OFF...
	{
		toggleCriterias(); // see toggleCriterias() function elsewhere
	}
}

/* Search- and keypage-functions: ------------------------------------------- */

function sendForm() // submits form automatically when a element is activated
{
	var subm = false;									// validation var

	// start validation before submitting:
	// if we're working in a text-box with a value
	if (window.event && this.event.srcElement.type == 'text')
	{
		if (checkForm(event.srcElement.value)) // see checkForm function below
		{
			subm = true;
		}
	}
	else
	{
		subm = true;
	}
	
	// everything's okay? okay, then submit
	if (subm)
	{
		document.getElementById('searchForm').submit();
	}
}

function resetForm() // resets the searchform and the results-page
{
	document.getElementById('searchForm').reset();
	document.getElementById('searchForm').submit();
}

function checkForm(val) // returns true if val is validated and found okay
{
	var retval = false;

	// start validating for int, str, len, etc...
	// int?
	if (isInt(val)) // check if val is an integer - see isInt function above
	{
		retval = true;
	}

	return retval;
}

/* Preview-functions: ------------------------------------------------------- */

// these functions applies the string '&preview=true' to the querystring of 
// the searchpage. The reason for this is, that the searchpage is called via
// the post-method and the vars on the post-method-array must ONLY CONTAIN 
// the key data from the form. Any other vars needed to be sent via the form
// must dynamically be sent via get-method.
// setPreview is slightly different from setPreviewAdv in that it has more than 
// one var in the querystrijng, thus it needs a '&' where the other nees a '?'
var formActionUrl = ""; // variable for setPreview() and setPreviewAdvanced()
function setPreview() //Key-search. Enable thumbnail view on results
{
	var formObj = document.getElementById('searchForm');	// form object
	var formAct = formObj.action;							// formaction
	var isChecked = document.getElementById('imgs').checked	// status-bool
	var act;												// temp var
	
	if (formActionUrl == "")
	{
		formActionUrl = formAct;
	}

	if (isChecked)
	{
		act = formActionUrl + '&preview=true';
	}
	else
	{
		act = formActionUrl;
	}
	document.getElementById('searchForm').action = act;
	document.getElementById('searchForm').submit();
}

function setPreviewAdv() //Key-adv-search. Enable thumbnail view on results
{
	var formObj = document.getElementById('searchForm');	// form object
	var formAct = formObj.action;							// formaction
	var isChecked = document.getElementById('imgs').checked	// status-bool
	var act;												// temp var

	if (formActionUrl == "")
	{
		formActionUrl = formAct;
	}

	if (isChecked)
	{
		act = formActionUrl + '?preview=true';
	}
	else
	{
		act = formActionUrl;
	}
	document.getElementById('searchForm').action = act;
	document.getElementById('searchForm').submit();
}

/* Keypage-functions: ------------------------------------------------------- */

function showHelp(divId) // shows the divId (keyHelpBody) element
{
	var tip = document.getElementById('keyHelpDiv');// main helpdiv
	var txt = document.getElementById(divId);		// current help-item

	txt.style.visibility = 'visible';
	tip.style.visibility = 'visible';
	tip.style.top = yOffset() + 10; // see yOffset() function below
}

function hideHelp(divId) // hides the divId (keyHelpBody) element
{
	var tip = document.getElementById('keyHelpDiv');// main helpdiv
	var txt = document.getElementById(divId);		// current help-item

	tip.style.visibility = 'hidden';
	txt.style.visibility = 'hidden';
}

function yOffset() // the current page's y-offset (how much is scrolled down?)
{
	var retval;										// temporary var

	if (document.layers) // MOZ
	{
		retval = window.pageYOffset; // virker ikke 100 (?)
	}
	else if (document.all) // MSIE
	{
		retval = document.body.scrollTop;
	}
	
	return retval;
}

/* Keyresults-functions: ---------------------------------------------------- */

function toggleCriterias() // toggles the criterias visibility on/off
{
	var objHead = document.getElementById('toggle');// heading/toggle-'link'-obj
	var obj = document.getElementById('criterias');	// criterea-list-obj

	if (obj.style.visibility == 'hidden')
	{
		obj.style.visibility = 'visible'
		objHead.innerHTML = "Valgte kriterier:";
		obj.style.height = null;
	}
	else
	{
		obj.style.visibility = 'hidden';
		objHead.innerHTML = "Vis valgte kriterier";
		obj.style.height = 12;
	}
}

/* ************************************************************************** */
/* WINDOW ******************************************************************* */
/* ************************************************************************** */

function opgWin(address, w, b, isScrollable) // opens Oevelser-popup. 0502
{
	var winWidth	 =	w;
	var winHeight	 =	b;
	var scroll		 = (isScrollable) ? '1' : '0';
	var winTop		 = (screen.height - winHeight) / 2;
	var winLeft		 = (screen.width - winWidth) / 2;
	var winFile		 =	'oevelser/' + address;
	var winName		 =	'opgWin';
	var winParams	 =	'resizable=1, scrollbars=' + scroll + ', status=0, width=' + winWidth + ', height=' + winHeight + ', screenX=' + winTop + ', screenY=' + winLeft + ', top=' + winTop + ', left=' + winLeft + '';
	var winObj		 =	window.open(winFile, winName, winParams);
	
	winObj.focus();
}

function arkivWin(address, w, h) // opens Oevelser-popup. 0502
{
	var winWidth	 =	w + 30;
	var winHeight	 =	h + 30;
	var winTop		 = (screen.height - winHeight) / 2;
	var winLeft		 = (screen.width - winWidth) / 2;
	var winFile		 =	address;
	var winName		 =	'arkivWin';
	var winParams	 =	'resizable=1, scrollbars=0, status=0, width=' + winWidth + ', height=' + winHeight + ', screenX=' + winTop + ', screenY=' + winLeft + ', top=' + winTop + ', left=' + winLeft + '';
	var winObj		 =	window.open(winFile, winName, winParams);
	
	winObj.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n');
	winObj.document.write('<html>\n');
	winObj.document.write('<head>\n');
	winObj.document.write('<meta http-equiv="imagetoolbar" content="no">\n');
	winObj.document.write('<link href="screen.css" rel="stylesheet" type="text/css" media="screen">\n');
	winObj.document.write('<title>' + document.title + '</title>\n');
	winObj.document.write('</head>\n');
	winObj.document.write('<body>\n');
	winObj.document.write('<div style="width:100%; height:100%; text-align:center; padding:10px;">\n');
	winObj.document.write('<img src="' + address + '" style="width:' + address + 'px; height:' + address + 'px; cursor:hand; cursor: pointer;" alt="(klik for at lukke igen)" onclick="window.close();"/>\n');
	winObj.document.write('</div>\n');
	winObj.document.write('</body>\n');
	winObj.document.write('</html>\n');
	winObj.document.close();

	winObj.focus();
}

function picWin(address, alt) // opens popup with images (biotop/tavle). 0302
{
	var winWidth	 =	393;
	var winHeight	 =	555;
	var winTop		 = (screen.height - winHeight) / 2;
	var winLeft		 = (screen.width - winWidth) / 2;
	var winFile		 =	address;
	var winName		 =	'picWin';
	var winParams	 =	'resizable=1, scrollbars=0, status=0, width=' + winWidth + ', height=' + winHeight + ', screenX=' + winTop + ', screenY=' + winLeft + ', top=' + winTop + ', left=' + winLeft + '';
	var winObj		 =	window.open(winFile, winName, winParams);
	
	winObj.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n');
	winObj.document.write('<html>\n');
	winObj.document.write('<head>\n');
	winObj.document.write('<meta http-equiv="imagetoolbar" content="no">\n');
	winObj.document.write('<link href="screen.css" rel="stylesheet" type="text/css" media="screen">\n');
	winObj.document.write('<title>' + document.title + ' : ' + alt + '</title>\n');
	winObj.document.write('</head>\n');
	winObj.document.write('<body>\n');
	winObj.document.write('<img src="' + address + '" style="width:393px; height:555px; cursor:hand; cursor: pointer;" alt="' + alt + ' (klik for at lukke igen)" onclick="window.close();"/>\n');
	winObj.document.write('</body>\n');
	winObj.document.write('</html>\n');
	winObj.document.close();

	winObj.focus();
}

function picAdmWin(address, alt) // opens popup with images (biotop/tavle). adm
{
	var winWidth	 =	410;
	var winHeight	 =	570;
	var winTop		 = (screen.height - winHeight) / 2;
	var winLeft		 = (screen.width - winWidth) / 2;
	var winFile		 =	address;
	var winName		 =	'picAdmWin';
	var winParams	 =	'resizable=1, scrollbars=0, status=0, width=' + winWidth + ', height=' + winHeight + ', screenX=' + winTop + ', screenY=' + winLeft + ', top=' + winTop + ', left=' + winLeft + '';
	var winObj		 =	window.open(winFile, winName, winParams);
	
	winObj.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n');
	winObj.document.write('<html>\n');
	winObj.document.write('<head>\n');
	winObj.document.write('<meta http-equiv="imagetoolbar" content="no">\n');
	winObj.document.write('<link href="../screen.css" rel="stylesheet" type="text/css" media="screen">\n');
	winObj.document.write('<title>' + document.title + ' : ' + alt + '</title>\n');
	winObj.document.write('</head>\n');
	winObj.document.write('<body style="margin:4px;">\n');
	winObj.document.write('<img src="' + address + '" cursor:hand; cursor: hand;" alt="' + alt + ' (klik for at lukke igen)" onclick="window.close();"/>\n');
	winObj.document.write('</body>\n');
	winObj.document.write('</html>\n');
	winObj.document.close();

	winObj.focus();
}

function planteEditWin(address) // opens popup for main plante edit-form
{
	var winWidth	 =	1024;
	var winHeight	 =	800;
	var winTop		 = (screen.height - winHeight) / 2;
	var winLeft		 = (screen.width - winWidth) / 2;
	var winFile		 =	address;
	var winName		 =	'planteEditWin';
	var winParams	 =	'resizable=1, scrollbars=1, status=1, width=' + winWidth + ', height=' + winHeight + ', screenX=' + winTop + ', screenY=' + winLeft + ', top=' + winTop + ', left=' + winLeft + '';
	var winObj		 =	window.open(winFile, winName, winParams);

	winObj.focus();
}

//window.onerror = return true; // disable ALL errorreporting!
//window.onerror = errDebug; // eventlistener for errDebug
function errDebug(msg, url, lno) // debug js-messenger
{
	var winWidth	 =	350;
	var winHeight	 =	180;
	var winTop		 =	100;
	var winLeft		 =	100;
	var winFile		 =	'';
	var winName		 =	'debugwin';
	var winParams	 =	'resizable=0, scrollbars=0, status=0, width=' + winWidth + ', height=' + winHeight + ', screenX=' + winTop + ', screenY=' + winLeft + ', top=' + winTop + ', left=' + winLeft + '';
	var winStyle	 =	'style="margin:4px; background:#d0d0d0; color:#000000; font-size:10px; font-family:Courier New;"';
	var winObj		 =	window.open(winFile, winName, winParams);

	winObj.focus();

	winObj.document.writeln('<html><head><title>JavaScript Errormessage</title></head>');
	winObj.document.writeln('<body ' + winStyle + '><p><strong>JavaScript Errormessage:</strong><br>');
	winObj.document.writeln('<br>File:<br>')
	winObj.document.writeln('<a href="' + url + '" target="_blank">' + url + '</a><br>')
	winObj.document.writeln('Line: ' + lno + '<br>')
	winObj.document.writeln('Message:<br>' + msg + '</p>')
	winObj.document.writeln('</body></html>');
	winObj.document.close();

	return true
}


/************************************ send mail link */
// create mail-vars and load up the users mail-window.
function send_billeder(artsnavn)
{
	var email		= 'kontakt@danmarksflora.dk'; //window.prompt('Skriv modtagerens emailadresse her', '');
	var msg_title	= 'Fotoarkiv: ' + artsnavn;
	var msg_link	= escape(location.href);
	var subject		= msg_title;
	var message		= '(Vedhæft venligst billederne til denne e-mail.)';
		message    += '%0A%0A';
		message	   += 'Plante: ' + artsnavn + '' ;
		message	   += '%0A';
		message	   += 'Dato:';
		message	   += '%0A';
		message	   += 'Lokation:';
		message	   += '%0A';
		message	   += 'Fotograf:'; 
		message    += '%0A%0A';
		message	   += 'Med venlig hilsen' ;
		message    += '%0A%0A%0A%0A';
		message	   += '[ ' + msg_link + ' ]' ;

	send_mail(email, subject, message);
}

// load up a mail-window with email, subj and body pre-entered.
function send_mail(email, subject, message)
{
	var mailto_link = 'mailto:' + email + '?subject=' + subject + '&body=' + message;
	win = window.open(mailto_link,'mailto_win');

	if (win && win.open && !win.closed)
		win.close();
}
