function checkInputForm(id) {
	if (checkInput(id)) 
	{
		document.getElementById(id).submit();
	}
}

function checkInputFormPhone(id) {
	if (checkInput(id)) 
	{
		document.getElementById('personform').submit();
	}
}

function checkNewsInput(id,action) {
	if (checkInput(id)) 
	{
		if (action == 'preview')
		{
			document.getElementById('newsform').action = 'uutinen_esikatselu.php';
			document.getElementById('useraction').value = 'preview';
			document.getElementById('newsform').submit();
		}
		else if (action == 'preview_mod')
		{
			document.getElementById('newsform').action = 'uutinen_esikatselu.php';
			document.getElementById('useraction').value = 'preview_mod';
			document.getElementById('newsform').submit();
		}
		else if (action == 'publish')
		{
			document.getElementById('newsform').submit();
		}
	}
}

function checkNewUserInput(id) {
	if (checkInput(id)) 
	{
		var pw1 = document.getElementById('salasana').value;
		var pw2 = document.getElementById('salasana2').value;
		if (pw1 != pw2)
		{
			showerrormsg('Salasana ja sen varmistus eivät ole samat.');
		}
		else
		{
			document.getElementById('newuserform').submit();
		}
	}
}

function checkModUserInput(id) {
	if (checkInput(id)) 
	{
		var pw1 = document.getElementById('salasana').value;
		var pw2 = document.getElementById('salasana2').value;
		if (pw1 != pw2)
		{
			showerrormsg('Salasana ja sen varmistus eivät ole samat.');
		}
		else
		{
			document.getElementById('moduserform').submit();
		}
	}
}

// Kirppiksen ilmoituksen tarkistus
function checkMarketInput() {
	if (checkInput('fieldilmo') && checkInput('fieldilmoittaja') ) 
	{
		document.getElementById('adform').submit();
	}
}

function checkInput(id) {
  var node = document.getElementById(id);
  if (node.hasChildNodes)
  {
		var children = node.childNodes;
		var error = 0;
		for (var i = 0; i < children.length; i++) 
		{
	  	if(children[i].className && children[i].className == 'chk')
	  	{
	    	if(children[i].value == '')
				{
		  		children[i].style.borderColor = '#ffd324';
					children[i].style.background = '#fff6bf';
		  		var error = 1;
				}
	  	}
		}
		if (error == 1)
		{
			showerrormsg('Täytä vähintään kaikki keltaiset kentät.');
			return false;
		}
		else
		{
			return true;
		}
  }
}

function showerrormsg(msg) {
	var elm = document.getElementById('errormessage');
	elm.innerHTML = msg;
	elm.style.display = 'block';
}

function removeError(id) {
  document.getElementById(id).style.borderColor = '#999';
	document.getElementById(id).style.background = '#fff';
}

/* 
	Edit news text
	--------------
*/
function addTags(tag) {
	if (tag == 'a') 
	{
		var url = prompt('Kirjoita linkin osoite (myös http://), esim. http://www.oulunsalo.fi', 'http://');
		if (url == null) return false;
	}
	s = trimString(document.selection.createRange().text);
  if (s != '') 
	{
		if (tag == 'a') document.selection.createRange().text = "<"+tag+" href='"+url+"' title='"+s+"'>"+s+"</"+tag+"> ";
		else document.selection.createRange().text = "<"+tag+">"+s+"</"+tag+"> ";
	}
}
function trimString(s) { 
  s = s.replace( /^\s+/g, "" ); // strip leading
  return s.replace( /\s+$/g, "" ); // strip trailing
}
