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();
				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.substr(0,7)=='valikko') {
				if (selection == divs[i])
					selection.className='valikko_selected';
				else
					divs[i].className='valikko';
			}
		}
		return true;
	}
	return false;
}
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 = "#dedeff";
					divs[i].style.color='#4682b4';
				}
			}
		}
		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='#dedeff';
	}	
}
function save_user_card(url)
{
	tinyMCE.triggerSave();
  	xmlHttp=GetXmlHttpObject();
  	if (xmlHttp==null)
  	{
    	alert ("Your browser does not support AJAX!");
    	return;
	}

	var i;
	var params="foo=";
	var myForm=document.getElementById("own_info_form");
	var inputArr=myForm.getElementsByTagName("input");
	for (i=0; i<inputArr.length; i++) {
	  if(inputArr[i].type=="checkbox") {
		if(inputArr[i].name != '' && inputArr[i].checked) 
			params=params+"&"+encodeURIComponent(inputArr[i].name)+"="+encodeURIComponent(inputArr[i].value);
	  } else if(inputArr[i].name != '')
	    params=params+"&"+encodeURIComponent(inputArr[i].name)+"="+encodeURIComponent(inputArr[i].value);
	}
	var selectArr=myForm.getElementsByTagName("select");
	for (i=0; i<selectArr.length; i++) {
	  if(selectArr[i].name != '')
	    params=params+"&"+encodeURIComponent(selectArr[i].name)+"="+encodeURIComponent(selectArr[i].value);
	}
	var textareaArr=myForm.getElementsByTagName("textarea");
	for (i=0; i<textareaArr.length; i++) {
	  if(textareaArr[i].name != '')
	    params=params+"&"+encodeURIComponent(textareaArr[i].name)+"="+encodeURIComponent(textareaArr[i].value);
	}

	xmlHttp.open("POST",url,true);
	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.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

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 bl_tableDnD=null;

function bl_init_business_location()
{
	if(bl_tableDnD!=null)
		delete bl_tableDnD;
	
	var table = document.getElementById('business_location_table');
	bl_tableDnD = new TableDnD();

	bl_tableDnD.init(table);

	var sbox=document.getElementById('new_business_location');
	if(sbox.length==0) {
		var new_business_location_row=document.getElementById('new_business_location_row');
		new_business_location_row.style.display='none';
	}
}

function bl_reinit_tablednd()
{
	delete bl_tableDnd;
	var table = document.getElementById('business_location_table');
	bl_tableDnD = new TableDnD();
	bl_tableDnD.init(table);		
}
function bl_delete_row(business_location_id)
{
	row=document.getElementById('row'+business_location_id);
	var newRow, newCol1, newCol2, newCol3, sect;
	if((sect = row.parentNode))
	{
		var optn = document.createElement('option');
		var sbox = document.getElementById('new_business_location');
		cells=row.getElementsByTagName("td");
		optn.text = cells[1].childNodes[0].nodeValue;
		optn.value = cells[0].childNodes[1].nodeValue;
		sbox.options.add(optn);
		if(navigator.appName.indexOf('Microsoft') > -1){
			var canSee = 'block';
		} else {
			var canSee = 'table-row';
		}
		var new_business_location_row=document.getElementById('new_business_location_row');
		new_business_location_row.style.display=canSee;
	}
	var tbl=document.getElementById('business_location_table');
	tbl.deleteRow(row.rowIndex);
		
}
function bl_add_row(button)
{
	var cell, newRow, newCol1, newCol2, newCol3, row, sect;
	var sbox=document.getElementById('new_business_location');
	if(sbox.length>0) {
		if((cell = button.parentNode) && (row = cell.parentNode) && (sect = row.parentNode) && sect.insertBefore)
		{
			newRow = document.createElement('tr');
			newCol1 = document.createElement('td');
			newCol2 = document.createElement('td');
			newCol3 = document.createElement('td');
			newRow.appendChild(newCol1);
			newRow.appendChild(newCol2);
			newRow.appendChild(newCol3);

			newRow.setAttribute('class','business_location_row');
			newRow.setAttribute('id','row'+sbox[sbox.selectedIndex].value);
			
			var colText1 = document.createTextNode(sbox[sbox.selectedIndex].value);
			var colText2 = document.createTextNode(sbox[sbox.selectedIndex].text);

			colHidInput = document.createElement('input');
			colHidInput.setAttribute('type', 'hidden');
			colHidInput.setAttribute('name', 'business_location_id');
			colHidInput.setAttribute('value', sbox[sbox.selectedIndex].value);
			newCol1.appendChild(colHidInput);

			colLink = document.createElement('a');
			colLink.href='#';
			colLink.appendChild(document.createTextNode('Poista'));
			var OnC='bl_delete_row('+sbox[sbox.selectedIndex].value+');return false;mceOnChangeHandler(null);';
			colLink.onclick=new Function(OnC);
			
			colLink.style.cursor='pointer';
			newCol3.appendChild(colLink);
			
			newCol1.appendChild(colText1);
			newCol2.appendChild(colText2);
			sbox.remove(sbox.selectedIndex);
			sect.insertBefore(newRow, row);
			bl_reinit_tablednd();
			if(sbox.length==0)
				row.style.display='none';
		}
	}
}


