/* NAVIGOINTI */
//here you place the ids of every element you want.
var ids=new Array('etu','seu','har','tap','kuv','lin','yht','viestix');

function switchid(id){	
	hideallids();
	showdiv(id);
	setCookie('sivu',id);
}
function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
	}		  
}
function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}
function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}
/* NAVIGOINTI */
/* NAVIGOINTI - muista sivu */
function setCookie(c_name,value,expiredays)
{var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function del_cookie(name) {
document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}
/* NAVIGOINTI - muista sivu */

/* LOMAKKEEN TARKISTUS */
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateViesti(theForm.viestikentta);
  reason += validateNimi(theForm.nimikentta);
//  reason += validatePhone(theForm.puh);
  reason += validateEmail(theForm.sahkopostikentta);

      
  if (reason != "") {
    alert("Joitakin tietoja jäi täyttämättä:\n" + reason);
    return false;
  }

//  alert("Kaikki tiedot täytetty oikein.");

	setCookie('posti','sent');
	document.forms['formi'].submit();
  return false;
}

function validateViesti(fld) {
    var error = "";
    var illegalChars = /\WÖö/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Viestikenttä tyhjä.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 1000)) {
        fld.style.background = 'Yellow'; 
        error = "Viesti on joko liian lyhyt tai pitkä (maks. 1000 merkkiä).\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow';
        error = "Viestissä on sopimattomia merkkejä.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateNimi(fld) {
    var error = "";
    var illegalChars = /\WÖö/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Et kirjoittanu nimeäsi.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 30)) {
        fld.style.background = 'Yellow'; 
        error = "Nimi on liian lyhyt tai pitkä.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "Nimessä on sopimattomia merkkejä.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Et syöttänyt sähköpostiosoitettasi.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Syötä oikeanlainen sähköpostiosoite.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Sähköpostiosoitteessa on sopimattomia merkkejä.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
/* LOMAKKEEN TARKISTUS */

function tarkista(id)
{
	//alert(getCookie('sivu'));
	var historia=getCookie('sivu');
	if (historia!='')
	{
		switchid(historia);
	}
	
}

