
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function start_hide()
{
	document.write('<!--');
}

function end_hide()
{
	document.write('-->');
}

function new_window(url) {

link = window.open(url,"Link","toolbar=0,location=0,directories=0,status=0,width=200, height=370, menubar=0,scrollbars=0,resizable=0,left=200,top=200");

}

function openUrl(theURL, winName, width, height)
{
	if(!width) width = 800;
	if(!height) height = 600;

	//winName = 'Dialog ';
	features='width=' + width + ',height=' + height + ',directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes,top=0,left=0';
	msgWindow = window.open(theURL, winName, features);
	msgWindow.focus();
}

function closeAndReload()
{
	opener.window.focus();
	opener.window.location.reload();
	self.close();
	return false;
}

function closeAndReloadAttachments()
{
	opener.window.showAttachments();
	opener.window.focus();
	window.setTimeout('self.close()', 2000);
	return false;
}



function showAttachments()
{
	var articleID = document.getElementById("articleID").value;
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("get", "/admin/newsletter/archive/attachments.php?articleID="+ articleID, true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				var divAttachments = document.getElementById("divAttachments");
				divAttachments.innerHTML = oXmlHttp.responseText;
			} else {
				//statusText is not always accurate
			}
		}
	};
	oXmlHttp.send(null);
}


function removeAttachment(attachmentID, confirmText)
{
	if(!confirm(confirmText))
	{
		return false;
	}

	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("get", "/admin/newsletter/archive/delete_attachment.php?attachmentID=" + attachmentID, true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				showAttachments();
			} else {
				//statusText is not always accurate
				//alert('could not remove attachment');
			}
		}
	};
	oXmlHttp.send(null);
	return false;
}

function closeAndSubmit()
{
	opener.window.forms[0].submit();
	opener.window.focus();
	self.close();
	return false;
}

function closeWindow()
{
	opener.window.focus();
	self.close();
	return false;
}

function disableForm(theform)
{
  if (document.all || document.getElementById)
  {
	for (i = 0; i < theform.length; i++)
	{
	  var tempobj = theform.elements[i];
	  if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
	  tempobj.disabled = true;
	}
	return true;
  }
}

function tarkista_taytetyt()
{
	for (i=0; i<tarkista_taytetyt.arguments.length; i++)
	{
		if (!document.getElementById(tarkista_taytetyt.arguments[i]).value!='')
		{
			alert(tarkista_taytetyt.arguments[i] + ' puuttuu!');
			document.getElementById(tarkista_taytetyt.arguments[i]).focus();
			return false;
		}
	}
	return true;
}


function CheckFormFilled()
{
	for (i=1; i<CheckFormFilled.arguments.length; i++)
	{
		if (!document.getElementById(CheckFormFilled.arguments[i]).value!='')
		{
			errorBox(CheckFormFilled.arguments[i] + ' ' +CheckFormFilled.arguments[0]);
			document.getElementById(CheckFormFilled.arguments[i]).focus();
			return false;
		}
	}
	return true;
}

function openFullScreen(url)
{
	fs = window.open(url,"Link","toolbar=0,location=0,directories=0,status=0, menubar=0,scrollbars=0,resizable=0,left=0,top=0");
	fs.resizeTo(fs.screen.availWidth, fs.screen.availHeight);
}


function showScreen()
{
	var blurscreen = document.getElementById('blurscreen');
	blurscreen.setAttribute('class', 'displayNone');
	blurscreen.setAttribute('className', 'displayNone');

	var info = document.getElementById('info');
	info.setAttribute('class', 'displayNone');
	info.setAttribute('className', 'displayNone');

}




function InfoBox()
{
	this.setData = setData;
	this.addButton = addButton;
	this.showInfoBox = showInfoBox;
	this.setAlert = setAlert;
	this.setInfo = setInfo;
	this.addInputText = addInputText;

	var txtCaption = document.getElementById('infoboxData');
	var infoButtons = document.getElementById('infoButtons');
	var infoImg = document.getElementById('infoImg');

	var SwapImgNumber = 200;
	infoButtons.innerHTML = '';

	function setData(usrCaption, login)
	{				
		txtCaption.innerHTML = usrCaption;		
	}

	function addButton(butCaption, callBackFunc)
	{
		//infoButtons.innerHTML = infoButtons.innerHTML + '<input type="submit" value="' + butCaption +'" onclick="' + callBackFunc + '">&nbsp;';
		//infoButtons.innerHTML = infoButtons.innerHTML + '<a onclick="' + callBackFunc + '" href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'Image' + SwapImgNumber +'\',\'\',\'/images/'+butCaption+'_over.jpg\',1)"><img src="/images/'+butCaption+'.jpg" name="Image' + SwapImgNumber +'" width="113" height="56" border="0" id="Image' + SwapImgNumber +'" /></a>';
		infoButtons.innerHTML = infoButtons.innerHTML + '<span onclick="' + callBackFunc + '" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'Image' + SwapImgNumber +'\',\'\',\'/images/'+butCaption+'_over.jpg\',1)"><img src="/images/'+butCaption+'.jpg" name="Image' + SwapImgNumber +'" width="113" height="56" border="0" id="Image' + SwapImgNumber +'" style="cursor:pointer;" /></span>';
		SwapImgNumber++;
		//alert(butCaption);
	}

	function addInputText(butCaption)
	{
		//infoButtons.innerHTML = infoButtons.innerHTML + '<input type="submit" value="' + butCaption +'" onclick="' + callBackFunc + '">&nbsp;';
		txtCaption.innerHTML = txtCaption.innerHTML + '<br>' + butCaption + '<input type="text" id="infoBoxInputText" class="textfieldi">';
		SwapImgNumber++;
		//alert(butCaption);
	}

	function showInfoBox()
	{
		var blurscreen = document.getElementById('blurscreen');
		blurscreen.setAttribute('class', 'blurscreen');
		blurscreen.setAttribute('className', 'blurscreen');

		var info = document.getElementById('info');
		info.setAttribute('class', 'info');
		info.setAttribute('className', 'info');

		var textField = document.getElementById('infoBoxInputText');	// contrib by jr 12.06.2007
		if (textField) textField.focus();
	}

	function setAlert()
	{
		infoImg.src = "/images/alert.jpg";
	}

	function setInfo()
	{
		infoImg.src = "/images/info.jpg";
	}
}


function apu()
{
	var infobox = new InfoBox();
	infobox.setAlert();
	infobox.setData('Oletko aivan varma?');
	infobox.addButton('peruuta', 'alert(\'EN\');');
	infobox.addButton('ok', 'alert(\'OLEN\');');

	infobox.showInfoBox();
}
function showAlertScreen()
{
	apu();

	var blurscreen = document.getElementById('blurscreen');
	blurscreen.setAttribute('class', 'blurscreen');
	blurscreen.setAttribute('className', 'blurscreen');

	var info = document.getElementById('info');
	info.setAttribute('class', 'info');
	info.setAttribute('className', 'info');
}

function setActiveTab(tabNum)
{
	var curTab;
	var curFormPage;
	for(i=1;i<11;i++)
	{
		curTab = document.getElementById('form_tab_'+i);
		curFormPage = document.getElementById('form_page_'+i);
		if(curTab && curFormPage)
		{
			if(i == tabNum)
			{
				curTab.setAttribute('class', 'adminFormTabActive');
				curTab.setAttribute('className', 'adminFormTabActive');
				curFormPage.setAttribute('class', 'displayAll');
				curFormPage.setAttribute('className', 'displayAll');
			}
			else
			{
				curTab.setAttribute('class', 'adminFormTab');
				curTab.setAttribute('className', 'adminFormTab');
				curFormPage.setAttribute('class', 'displayNone');
				curFormPage.setAttribute('className', 'displayNone');
			}
		}

	}
}

function setActiveEditor(tabNum, maxNum)
{
	var curTab;
	var curEditor;    
	for(i=1;i<=maxNum;i++)
	{
		curTab = document.getElementById('Editor_Tab_'+i);
		curEditor = document.getElementById('Editor_'+i);        
		if(curTab)
		{
		   if(i == tabNum)
			{
				curTab.setAttribute('class', 'tab_third_container_active');
				curTab.setAttribute('className', 'tab_third_container_active');
				curEditor.setAttribute('class', 'displayAll');
				curEditor.setAttribute('className', 'displayAll');
			}
			else
			{
				curTab.setAttribute('class', 'tab_third_container');
				curTab.setAttribute('className', 'tab_third_container');
				curEditor.setAttribute('class', 'displayNone');
				curEditor.setAttribute('className', 'displayNone');
			}
		}

	} 
}

function leftNaviHide()
{
	var leftNaviArrow = document.getElementById('leftNaviArrow');
	if(leftNaviArrow.src.indexOf('arrow_up.png') == -1)
	{
		leftNaviHide2();
		new Effect.Move('left_navi_hidden', 0, 200, {duration:1, fps:25, from:0.0, to:1.0});
		leftNaviArrow.src = leftNaviArrow.src.replace('arrow_down.png', 'arrow_up.png');
	}
	else
	{
		new Effect.Move('left_navi_hidden', 0, -200, {duration:1, fps:25, from:0.0, to:1.0});
		leftNaviArrow.src = leftNaviArrow.src.replace('arrow_up.png', 'arrow_down.png');
		window.setTimeout('leftNaviHide2()', 1000);
	}

}


function leftNaviHide2()
{
	//alert('hidden');
	var left_navi_hidden = document.getElementById('left_navi_hidden');
	if(left_navi_hidden.getAttribute('class') != 'displayNone')
	{
		left_navi_hidden.setAttribute('class', 'displayNone');
		left_navi_hidden.setAttribute('className', 'displayNone');
	}
	else
	{
		left_navi_hidden.setAttribute('class', 'displayAll');
		left_navi_hidden.setAttribute('className', 'displayAll');
	}
}

function confirmation(capt, URLobj)
{
	var infobox = new InfoBox();
	infobox.setData(capt);
	infobox.addButton('peruuta', 'showScreen()');

	if (typeof(URLobj) == 'string') infobox.addButton('ok', URLobj + '; showScreen();');
	else if(URLobj) infobox.addButton('ok', 'window.location = \'' + URLobj.href + '\'');

	infobox.showInfoBox();
	return false;
}


function ajax_confirmation(capt)
{
	var infobox = new InfoBox();

}


function errorBox(capt, URLobj)
{
	var infobox = new InfoBox();
	infobox.setAlert();
	infobox.setData(capt);
	infobox.addButton('ok', 'showScreen()');
	if(URLobj) infobox.addButton('ok', 'window.location = \'' + URLobj.href + '\'');

	infobox.showInfoBox();
	return false;
}

function forwardDialog(capt, forwardTo)
{
	var infobox = new InfoBox();
	infobox.setData(capt);
	infobox.setInfo();
	//infobox.addButton('ok', 'showScreen()');
	if(forwardTo) infobox.addButton('ok', 'window.location = \'' + forwardTo + '\'');

	infobox.showInfoBox();
	return false;
}

function textBox(capt, divToSetVal)
{
	var infobox = new InfoBox();
	infobox.setData(capt);
	infobox.addButton('peruuta', 'showScreen()');
	if(divToSetVal) infobox.addButton('ok', '');

	infobox.showInfoBox();
	return false;
}

/***** FORM **/
function saveForm(theID)
{
	if(is_defined('SpawEngine'))
	{
		//alert('Spawengine found');
		SpawEngine.updateFields();
	}

	if(is_defined('tinyMCE'))
	{
		tinyMCE.triggerSave();
	}	
	
	var infobox = new InfoBox();
	infobox.setInfo();
	infobox.setData('Tietoja tallennetaan...');
	infobox.showInfoBox();

	var formData = document.getElementById(theID);

	new Ajax.Updater('popUpInfo', formData.action, {asynchronous:true, encoding:'UTF-8', parameters:Form.serialize(theID), onComplete:formSaved, onFailure:formSaveFailure, evalScripts:true});
}

function formSaveFailure()
{
	var infobox = new InfoBox();
	infobox.setAlert();
	infobox.setData('Tietojen tallennus epäonnistui. Yritä hetken kuluttua uudelleen.');
	//infobox.addButton('peruuta', '');
	infobox.addButton('ok', 'showScreen()');

	infobox.showInfoBox();
}

function formSaved()
{
	var saveSuccessful = document.getElementById('popUpInfo');

	var infobox = new InfoBox();

	if(saveSuccessful.innerHTML == 1)
	{
		infobox.setInfo();
		infobox.setData('Tietojen tallennus onnistui');
		infobox.addButton('ok', 'showScreen()');

		infobox.showInfoBox();

	}
	else
	{
		infobox.setAlert();
		infobox.setData('Tietojen tallennus epäonnistui');
		infobox.addButton('ok', 'showScreen()');

		infobox.showInfoBox();
	}
}
/***  */

function textboxdemo()
{
	var textboxdemo = document.getElementById('textboxdemo');
	var infobox = new InfoBox();

	infobox.setInfo();
	infobox.setData('Tietojen muokkaus');
	infobox.addInputText('Tekstikenttä:');
	infobox.addButton('ok', 'settextboxdemo()');

	infobox.showInfoBox();

}
function settextboxdemo()
{
	var infoBoxInputText = document.getElementById('infoBoxInputText');
	var textboxdemo = document.getElementById('textboxdemo');
	textboxdemo.innerHTML = infoBoxInputText.value;
	showScreen();
}




function setPublished(handlerScript, dbItemID, imgPublished, parameters)
{
	if (!parameters) parameters = '';
	new Ajax.Updater('popUpInfo', handlerScript+'.php?id='+dbItemID+parameters, {asynchronous:true, evalScripts:true});

	if(imgPublished.src.indexOf('x.png') == -1) //not x.png --> x.png
	{
		imgPublished.src = imgPublished.src.replace('y.png', 'x.png');
	}
	else
	{
		imgPublished.src = imgPublished.src.replace('x.png', 'y.png');
	}

}

function is_defined( variable)
{
	return (typeof(window[variable]) == "undefined")?  false: true;
}

function addToCart( theID )
{
	var formData = document.getElementById(theID);

	new Ajax.Updater('shoppingCart', formData.action, {asynchronous:true, encoding:'UTF-8', parameters:Form.serialize(theID), evalScripts:true});
	//new Ajax.Updater('shoppingCart', '/ostoskori/ajax/true/lisaa/'+pubID, {asynchronous:true, evalScripts:true});
	return false;
}


function orderDetails(toActive)
{
	//alert(toActive);
	var newState = (toActive == 'yksityisTiedot') ? 'displayTableCell' : 'displayNone';
	var formPart = document.getElementById('yksityisTiedot');

	formPart.setAttribute('class', newState);
	formPart.setAttribute('className', newState);

	var newState = (toActive == 'yritysTiedot') ? 'displayTableCell' : 'displayNone';
	var formPart = document.getElementById('yritysTiedot');

	formPart.setAttribute('class', newState);
	formPart.setAttribute('className', newState);

	var newState = (toActive == 'oppilaitosTiedot') ? 'displayTableCell' : 'displayNone';
	var formPart = document.getElementById('oppilaitosTiedot');

	formPart.setAttribute('class', newState);
	formPart.setAttribute('className', newState);
}

function requestServerTime()
{
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("get", "/admin/servertime.php", true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				displayServerTime(oXmlHttp.responseText);
			} else {
				//statusText is not always accurate
			}
		}
	};
	showServerTime();
	oXmlHttp.send(null);
}

function showServerTime()
{
	window.setTimeout('requestServerTime()', 30000);
}

function startClock()
{
	requestServerTime();
	showServerTime();
}

function displayServerTime(sText)
{
	//var divServerTime = document.getElementById("divServerTime");
	//if (is_defined(divServerTime))
	//divServerTime.innerHTML = sText;

	document.getElementById('divServerTime').innerHTML = sText;
}


function SetItemDimensions(height, width, item_id)
{
	if (height) document.getElementById(item_id).height=height;
	if (width) document.getElementById(item_id).width=width;
}


function FileUploadDeleteAction(index, attachmentID, prefix)
{
	new Ajax.Updater('attachment_'+index, prefix+'_attachment.delete.php?attachmentID='+attachmentID);

	//Clears gap between elements
	//This prevents user seeing possible error messages!!!!.
	document.getElementById('attachment_'+index).style.display='none';
}

function InsertNewFileUploadDiv(index, new_index, ID, prefix, height, loaded_height)
{
	var newdiv = document.createElement('div');
	newdiv.id = 'attachment_'+new_index;

	newdiv.style.height = height+'px';
	newdiv.style.minHeight = '0px';
	newdiv.style.valign = 'bottom';
	newdiv.style.clear = 'both';
	//newdiv.style.border = '1px solid black';

	//newdiv.appendChild(document.createTextNode(' '));

	var newa = document.createElement('a');
	newa.href='#placeholder';
	newa.setAttribute("class", "link");
	newa.className = "link";
	newa.onclick = function() {a_onclick_event_file_upload_div(index, new_index, ID, prefix, height, loaded_height)}
	newa.innerHTML='Lisää uusi tiedosto';

	newdiv.appendChild(newa);

	var scr = document.getElementById('placeholder');
	scr.parentNode.insertBefore(newdiv, scr);
}

function a_onclick_event_file_upload_div(index, new_index, ID, prefix, height, loaded_height)
{
	new Ajax.Updater('attachment_'+new_index, prefix+'_attachment_new_form.php?index='+new_index+'&ID='+ID+'&prefix='+prefix+'&height='+height+'&loaded_height='+loaded_height, {asynchronous:true, evalScripts:true})
}

function SetIframeHeight(index, height)
{
	document.getElementById('attachment_'+index).style.height=height+'px';

	var iframeElement = parent.document.getElementById('attachment_iframe_'+index);

	iframeElement.style.height = height+'px';
}

function HideElement(id)
{
	document.getElementById(id).style.display='none';
}


function toggleTinyMceEditor(id)
{
	var elm = document.getElementById(id);

	if (tinyMCE.getInstanceById(id) == null)
		tinyMCE.execCommand('mceAddControl', false, id);
	else
		tinyMCE.execCommand('mceRemoveControl', false, id);
}
