
/* ESS.fi global JavaScript functions
Version 2.0
Last modified: 28.11.2007
Modifier: Nicklas Koski
----------------------------------- */

/* Show e-mail address
----------------------------------- */

mhosts = new Array("ess.fi");
function maddr(name, host) {
	ma = name + "@" + mhosts[host];
	document.writeln('<a href="mailto:' + ma + '">' + ma + '</a> | ');
}

/* Article comments
----------------------------------- */

function setDisplay(id, type)
{
    var el = document.getElementById(id);
    if ( el != null )
    {
        el.style.display = type;
    }
}

/* Utilities
----------------------------------- */

	function openwin(url, win, params) {
		if (url != null) {
			window.open(url, win, params);
		}
	}
	
	function openExtras() {
	var root = document.getElementById("leftnav");
	var links = root.getElementsByTagName("LI");
	var subs = root.getElementsByTagName("UL");
	for(i = 0; i < links.length; i++) if(links[i].className.indexOf("open") > -1) links[i].className = links[i].className.substring(links[i].className.indexOf(" "));
	for(i = 0; i < subs.length; i++) subs[i].className = "leftnavsub";
	document.getElementById("extras").className="leftnavsub chosen";
	}

/* Pop up window
----------------------------------- */

function avaaIkkuna (osoite,leveys,korkeus,tyokalut,osoiterivi,hakemistot,tila,valikko,palkit,skaalaus) {
		uusiIkkuna = window.open(osoite,'PopUp','width=' + leveys + ',height=' + korkeus + ',toolbar=' + tyokalut + ',location=' + osoiterivi + ',directories=' + hakemistot + ',status=' + tila + ',menubar=' + valikko + ',scrollbars=' + palkit + ',resizable=' + skaalaus);
		uusiIkkuna.location.href = osoite;
	}

/* Load flash. Fix IE bug.
----------------------------------- */

function writeFlashMovieCode (width, height, align, bgcolor, uri) {
	document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"");
	document.write("codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\"");
	document.write("width=\"" + width + "\"");
	document.write("height=\"" + height + "\"");
	document.write("align=\"" + align + "\"");
	document.write("<param name=\"allowScriptAccess\" value=\"always\" />");
	document.write("<param name=\"movie\" value=\"" + uri + "\" />");
	document.write("<param name=\"quality\" value=\"high\" />");
	document.write("<param name=\"bgcolor\" value=\"" + bgcolor + "\" />"); 
	document.write("<param name=\"menu\" value=\"false\" />");
	document.write("<embed src=\"" + uri + "\"");
	document.write("menu=\"false\"");
	document.write("quality=\"high\"");
	document.write("bgcolor=\"" + bgcolor + "\"");
	document.write("width=\"" + width + "\"");
	document.write("height=\"" + height + "\"");
	document.write("align=\"" + align + "\"");
	document.write("type=\"application/x-shockwave-flash\"");
	document.write("pluginspage=\"http://www.macromedia.com/go/getflashplayer\"");
	document.write("allowScriptAccess=\"always\"");
	document.write("swLiveConnect=\"true\">");
	document.write("</embed>");
	document.write("</object>");
}

/* Search
----------------------------------- */

/* Control search form */
function checkHeadForm(theForm){
	if (checkTheForm(theForm))
		postForm(theForm);
}

/* Control search form */
function checkTheForm(theForm){
	if(!checkTooManyClauses(theForm.query)){
		return false;
	}else{
		/*theForm.querystring.value=replaceChar(theForm.query);
		theForm.querystringstriped.value = theForm.querystring.value;*/
		return true;
	}
}

/* Replace special charachters */
function replaceChar(theField){
	tmp=theField.value;
	var newstring = "";
	for(i=0; i<tmp.length;i++){
		if(tmp.charAt(i)=='+'){
			newstring += '%2B';
		}else if(tmp.charAt(i)=='"'){
			newstring += '%22';
		}else{
			newstring += tmp.charAt(i);
		}
	}
	return newstring;
}

/* If 2 charachters and not wildcard */
function checkTooManyClauses(theField){
	if(theField.value.length==2 && theField.value.charAt(1)=='*'){
		alert("Haku antaa liian monta osumaa!");
		theField.focus();
		return false;
	}else{
		return true;
	}
}

/* At least 2 charachters */
function checkTheLength(theField, emptyOK){
	var error = false;
	if(!emptyOK){
		if(theField.value.length<2){
			error = true;
		}
	} else {
		if(theField.value.length<2 && theField.value.length>0){
			error = true;
		}
	}

	if(error){
		alert("Syötä hakuehto. (Vähintään 2 merkkiä).");
		theField.focus();
		return false;
	}else{
		return true;
	}
}

function next(){
	postForm(document.nextform);
}

function prev(){
	postForm(document.prevform);
}

function page(num){
	document.pageform.page.value=num;
	postForm(document.pageform);
}

function thesearchform(){
	postForm(document.thesearchform);
}

function advancedsearchform(){
	postForm(document.advancedsearchform);
}

function changeOrder(theOrder){
	document.sortorderChange.order.value=theOrder;
	postForm(document.sortorderChange);
}

function postForm(theForm){
	theForm.submit();
}

function move(pagenum){
	document.moveform.page.value=pagenum;
	postForm(document.moveform);
}
