function getElementsByClass(node,searchClass,tag) {
  var classElements = new Array();
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("\b"+searchClass+"\b");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function get_tag(xmlDoc,tagName)
{
  nodes = xmlDoc.getElementsByTagName(tagName);
  if(nodes.length>0) {
    if(nodes[0].firstChild && nodes[0].firstChild.data)
      return nodes[0].firstChild.data;
  }
  return "";
}

function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
        if (xmlHttp.overrideMimeType) {
            //xmlHttp.overrideMimeType('text/xml');
            xmlHttp.overrideMimeType('text/html');
         }
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function killFocus()
{
	// get round a tinyMCE bug - only kill focus when there's a textarea in the doc
	textareaArr=document.getElementsByTagName("textarea");
	if(textareaArr.length>0) {
		textareaArr[0].focus();
		textareaArr[0].blur();
	}
}

function show_page(tag,url,callbackfunc) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!");
		return;
	}
	document.getElementById(tag).innerHTML = "<img id=\"waiting\" src=\"images/wait16trans.gif\">";
	d=new Date();
	if(url.indexOf('?')==-1)
		url += "?foo=" + d.getTime();
	else
		url += "&foo=" + d.getTime();
	
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			document.getElementById(tag).innerHTML=xmlHttp.responseText;
			try {
				killFocus();
				$(".mceEditor").tinymce({
					script_url : 'tinymce/jscripts/tiny_mce/tiny_mce.js',
					theme : "advanced",
					theme_advanced_toolbar_location : "top", 
					theme_advanced_toolbar_align : "left", 
					theme_advanced_statusbar_location : "bottom",
					convert_fonts_to_spans : true,
					theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
					theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,preview,|,forecolor,backcolor",		
					theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,fullscreen",		
					theme_advanced_buttons4 : "iframe,|,insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
					plugins : "advimagescale,safari,iframe,pagebreak,style,layer,table,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,contextmenu,paste,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
					extended_valid_elements : "iframe[src|width|height|name|align]",
					content_css : "teksti.css",
					advimagescale_append_to_url: true, /* apply dimensions to image URL on resize */
				    advimagescale_url_width_key: 'xsize',  /* apply width to URL as w= param */
				    advimagescale_url_height_key: 'ysize' /* apply height to URL as h= param */
				});
				/*tinyMCE.init({
					theme : "advanced",
					mode : "specific_textareas",
					editor_selector : "mceEditor",
					content_css: "tiny.css",
					theme_advanced_toolbar_location : "top", 
					theme_advanced_toolbar_align : "left", 
					theme_advanced_statusbar_location : "bottom",	
					onchange_callback : "mceOnChangeHandler",
					handle_event_callback : "mceEventHandler",
					plugins : "safari"
				}); */
			} catch (ex) {
			
			}
			if(callbackfunc!=null)
				callbackfunc();
		}
	};
	xmlHttp.send(null);
}

function show_page_in_iframe(divId,url)
{
	theDiv=document.getElementById(divId);
	theDiv.innerHTML='<iframe id="sp_frame" frameborder="0" vspace="0" hspace="0" marginwidth="0" marginheight="0" style="width:100%;height:100%;border:0px;margin:0px;padding:0px;"></frame>';
	document.getElementById('sp_frame').src=url;
}

var current_menuitem=null;

function activate_menuitem(selection) {
	if(selection!=current_menuitem){
		var divs = document.getElementsByTagName("div");
		current_menuitem=selection;
		for(var i=0;i<divs.length;i++) {
			if(divs[i].className=='valikko') {
				if (selection == divs[i]) {
					selection.style.border='1px solid #bdbab3';
					selection.style.backgroundColor = "#000000";
					selection.style.color = "#ffffff";
				} else {
					divs[i].style.backgroundColor = "#6b716f";
					divs[i].style.border='1px solid #bdbab3';
					divs[i].style.color = "#fff";
				}
			}
		}
		return true;
	}
	return false;
}
var current_tab=null;

function activate_tab(selection)
{
	if(current_tab!=null) {
		current_tab.className="tab";
	}
	current_tab=selection;
	selection.className="tab_selected";
	return true;
}

function do_menu_bgcolor_in(thediv) {
	if(thediv!=current_menuitem)
		thediv.style.border='1px solid #e54125';
}


function do_menu_bgcolor_out(thediv) {
	if(thediv!=current_menuitem)
		thediv.style.border='1px solid #bdbab3';
}

var pBAreturnFunc;

function progressBarAjax(init,returnFunc) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!");
		return;
	}
	pBAreturnFunc=returnFunc;
	fsButton=document.getElementById('fsButton');
	statusObj=document.getElementById("usDiv");
	if(init==1) {
		fsButton.disabled=true;
		fsButton.style.display='none';
		statusObj.style.display='block';
	}
	var url="UploadStatus?init=" +init;
	xmlHttp.open("GET", url , true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4)
		{
			if(parseInt(xmlHttp.responseText,10)>0) {
				statusObj.innerHTML=
					"<div style=\"width:50px;height:12px;padding:2px;border:1px solid black;background-color:#fff;\">"
					+"<div style=\"width:"+xmlHttp.responseText+";height:4px;background-color:#9999ff;\">"
					+"</div>"
					+"</div>";
			}
			if(xmlHttp.responseText.substr(0,4)!="Done" && xmlHttp.reponseText!="Database error" && xmlHttp.responseText!="Upload error" && xmlHttp.responseText!="Not an image")
				setTimeout('progressBarAjax(0,pBAreturnFunc)',100);
			else {
				fsButton.disabled=false;
				fsButton.style.display='block';
				statusObj.innerHTML="";
				statusObj.style.display='none';
				//if(xmlHttp.responseText=="Done")
				//	document.getElementById("fsFile").value="Lähetys onnistui!";
				if(xmlHttp.responseText=="Database error")
					alert("Tietokantavirhe - lähetys epäonnistui!");
				if(xmlHttp.responseText=="Upload error")
					alert("Lähetys epäonnistui!");
				if(xmlHttp.responseText=="Not an image")
					alert("Tiedosto ei ole kuvatiedosto!");
				var fileID,fileMimeType;
				if(xmlHttp.responseText.substr(0,4)=="Done") {
					fileID=xmlHttp.responseText.substr(5,xmlHttp.responseText.length-5);
					var firstSlashIdx=fileID.indexOf('/');
					fileMimeType=fileID.substr(firstSlashIdx+1,fileID.length-firstSlashIdx-1);
					fileID=fileID.substr(0,firstSlashIdx);
				}
				if(pBAreturnFunc!=null) 
					pBAreturnFunc(fileID,fileMimeType);
			}
		}
	};
	xmlHttp.send(null);
}
function reloadProfilePic(a,b) { document.getElementById('profile_pic').src+='#'; }

var current_tab=null;

function activate_tab(selection)
{
	if(current_tab!=selection) {
		var divs = document.getElementsByTagName("div");
		current_tab=selection;
		for(var i=0;i<divs.length;i++) {
			if(divs[i].className=='tab') {
				if (selection == divs[i]) {
					selection.style.backgroundColor = "#ffffff";
					selection.style.color='#6b716f';
				} else {
					divs[i].style.backgroundColor = "#6b716f";
					divs[i].style.color='#e0e0e0';
				}
			}
		}
		return true;
	}
	return false;
}


function do_tab_bgcolor_in(thediv) {
	if(thediv!=current_tab) {
		thediv.style.backgroundColor='#f1eba6';
	}
}


function do_tab_bgcolor_out(thediv) {
	if(thediv!=current_tab) {
		thediv.style.backgroundColor='#6b716f';
	}	
}

function loadXMLDoc(fname)
{
  var xmlDoc;
  // code for IE
  if (window.ActiveXObject) {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument) {
    xmlDoc=document.implementation.createDocument("","",null);
  } else {
    alert('Your browser cannot handle this script');
  }
  xmlDoc.async=false;
  xmlDoc.load(fname);
  return(xmlDoc);
}

function display_xml(xmlfile,xslfile,tag)
{
  var xml=loadXMLDoc(xmlfile);
  var xsl=loadXMLDoc(xslfile);

  // code for IE
  if (window.ActiveXObject) {
    ex=xml.transformNode(xsl);
    document.getElementById(tag).innerHTML=ex;
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument) {
    xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    var resultDocument = xsltProcessor.transformToFragment(xml,document);
    while(document.getElementById(tag).hasChildNodes())
    	document.getElementById(tag).removeChild(document.getElementById(tag).firstChild)
    document.getElementById(tag).appendChild(resultDocument);
  }
}

function save_own_info(url)
{
	tinyMCE.triggerSave();
  	xmlHttp=GetXmlHttpObject();
  	if (xmlHttp==null)
  	{
    	alert ("Your browser does not support AJAX!");
    	return;
	}

	var i;
	url+=Math.random() + "=";
	var myForm=document.getElementById("own_info_form");
	var inputArr=myForm.getElementsByTagName("input");
	for (i=0; i<inputArr.length; i++) {
	  if(inputArr[i].name != '')
	    url=url+"&"+encodeURIComponent(inputArr[i].name)+"="+encodeURIComponent(inputArr[i].value);
	}
	var selectArr=myForm.getElementsByTagName("select");
	for (i=0; i<selectArr.length; i++) {
	  if(selectArr[i].name != '')
	    url=url+"&"+encodeURIComponent(selectArr[i].name)+"="+encodeURIComponent(selectArr[i].value);
	}
	var textareaArr=myForm.getElementsByTagName("textarea");
	for (i=0; i<textareaArr.length; i++) {
	  if(textareaArr[i].name != '')
	    url=url+"&"+encodeURIComponent(textareaArr[i].name)+"="+encodeURIComponent(textareaArr[i].value);
	}
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4)
		{
			if(xmlHttp.responseText!="OK")
				alert("Virhe tallennettaessa!\n"+xmlHttp.responseText);
			else {
				document.getElementById('save_button').disabled=true;
			}
		}
	};

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
