// LISÄÄ UUSI / MUOKKAA TARJOUSPYYNTÖÄ FORMIEN TARKISTUKSET

function teksti_normaali_rajoitettu() {

	var teksti = document.getElementById("tarjous_hankintamenettely").value;
	
	if (teksti == 1) {
		document.getElementById("hankintamenettely_selitys").innerHTML = "Tarjous jätettävä viimeistään:  	";
	}
	else {
		document.getElementById("hankintamenettely_selitys").innerHTML = "Osallistumishakemuksen jättöpäivä:";
	}

}

function validoiKaikki() {

	var tila = document.getElementById("tarjous_tila").value;
	
	// true/false muuttujat
	var tf_diaari;
	var tf_nimi; 
	var tf_julkaisu = document.getElementById("tarjous_julkaisu").value;
	var tf_pvm;
	var tf_kuvaus;
	
	
	// validoidaan aina 
	tf_nimi = validoiNimi();
	tf_kuvaus = validoiTextfield("tarjous_kuvaus");
	tf_diaari = validoiDiaari();
	
	// validoidaan jos tila = luonnos
	if (tila == 0) {
		tf_julkaisu = validoiPaivays("tarjous_julkaisu");
		if (document.getElementById("tarjous_pvm").length != 0) {
			tf_pvm = validoiPaivays("tarjous_pvm");
		}
		else {
			document.getElementById("tarjous_pvm").style.background = "";
			tf_pvm = true;
		}
		
		//tf_diaari = true;
		
	}
	else if (tila == 1 || tila == 2) {
		tf_pvm = validoiPaivays("tarjous_pvm");
		//tf_diaari = validoiDiaari();
		if (tf_julkaisu.length != 0) {
			tf_julkaisu = validoiPaivays("tarjous_julkaisu");
		}
		else {
			document.getElementById("tarjous_julkaisu").style.background = "";
			tf_julkaisu = true;
		}
		
	}
	
	// Debug
	//alert("tf_nimi = " + tf_nimi + " > tf_kuvaus = " + tf_kuvaus + " > tf_julkaisu = " + tf_julkaisu + " > tf_diaari = " + tf_diaari + " > tf_pvm = " + tf_pvm);
	
	
	if (tf_nimi && tf_kuvaus && tf_julkaisu && tf_diaari && tf_pvm) {
		document.getElementById("tallenna_nappi").disabled = false;
	}
	else {
		document.getElementById("tallenna_nappi").disabled = true;
	}

}

// Validoi diaari
function validoiDiaari() { 

	var diaari = trim(document.getElementById("tarjous_diaari").value);
	var tila = document.getElementById("tarjous_tila").value;
	var menettely = document.getElementById("tarjous_kynnysarvo").value;
	
	
	if (menettely == 0) {
		document.getElementById("tarjous_diaari").style.background = "";
		return true;
	} 
	else {
	
		if (diaari.length == 0) {
			document.getElementById("tarjous_diaari").style.background = "#FF5B4F";
			return false;
		}
		else {
			document.getElementById("tarjous_diaari").style.background = "#ADCC5B";
			return true;
		}
	
	}
	
	return true;

}

// Validoi nimi
function validoiNimi() {

	var nimi = document.getElementById("tarjous_nimi").value;

	if (nimi.length != 0) {
		document.getElementById("tarjous_nimi").style.background = "#ADCC5B";
		return true;
	}
	else {
		document.getElementById("tarjous_nimi").style.background = "#FF5B4F";
		return false;
	}

}

// validoi päiväys
function validoiPaivays(paiva_id) {
	
	var tila = document.getElementById("tarjous_tila").value;
	
	var paiva_kokonainen = document.getElementById(paiva_id).value;
	
	var tarkistus;
	
	tarkistus = isDate(paiva_kokonainen);
	
	if (tarkistus == true) {
		document.getElementById(paiva_id).style.background = "#ADCC5B";
		return true;
	}
	else if (tila == 0 && paiva_kokonainen.length == 0 && paiva_id == 'tarjous_pvm') {
		document.getElementById(paiva_id).style.background = "#FFFFFF";
		return true;
	}
	else if (tila == 1 && paiva_kokonainen.length == 0 && paiva_id == 'tarjous_julkaisu') {
		document.getElementById(paiva_id).style.background = "#FFFFFF";
		return true;
	}
	else {
		document.getElementById(paiva_id).style.background = "#FF5B4F";
		return false;
	}
	
	
}

function isDate(s) {   

	// make sure it is in the expected format
	if (s.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}$/g) != 0)
		return false;

	// remove other separators that are not valid with the Date class 			
	s = s.replace(/[\-|\.|_]/g, "/");
	
	var arrDateParts = s.split("/");
	
	s = arrDateParts[1] + "/" + arrDateParts[0] + "/" + arrDateParts[2];
	
	// convert it into a date instance
	var dt = new Date(Date.parse(s));	    
		
	// check the components of the date
	// since Date instance automatically rolls over each component
	
	arrDateParts = s.split("/");
	
	return (
		dt.getMonth() == arrDateParts[0]-1 &&
		dt.getDate() == arrDateParts[1] &&
		dt.getFullYear() == arrDateParts[2]
	);

}

// Validoi kuvaus/selitys
function validoiTextfield(kentta_id) {

	var nimi = document.getElementById(kentta_id).value;

	if (nimi.length != 0) {
		document.getElementById(kentta_id).style.background = "#ADCC5B";
		return true;
	}
	else {
		document.getElementById(kentta_id).style.background = "#FF5B4F";
		return false;
	}

}

// PÄÄTÖSTIETOJEN TARKISTUKSET
function validoiPaatostiedot() {

	var tf_pvm				= validoiPVM('paatos_pvm');
	var tf_numero			= validoiStringi('paatos_nro');
	var tf_voittaja			= validoiStringi('paatos_voittaja');
	var tf_arvo				= isNumeric2('paatos_arvo');
	var tf_sop_kerta		= validoiCheckbox('paatos_sop_kerta');
	var tf_sop_alku			= validoiPVM('paatos_sop_alku');
	var tf_sop_loppu		= validoiPVM('paatos_sop_loppu');
	var tf_optio			= isNumeric3('paatos_optio'); // poistetaan + merkit (eli toisin sanoen ne ovat sallittujen listalla)
	var tf_laji				= validoiLaji();
	//var tf_tekija			= validoiStringi('paatos_tekija');
	var tf_tekija;
	
	// kertahankinta vs hankinta jolla on ajanjakso
	var kertahankinta = document.getElementById('paatos_sop_kerta').value;
	var aikajakso = document.getElementById('paatos_sop_alku').value;
	
	// normihankinta vs moniosainen hankinta
	var moniosainen = document.getElementById('paatostyyppi').value;
	if (moniosainen == 2) { // jos moniosainen niin tarkistetaan myös kohde
		var tf_voittaja_kohde 	= validoiStringi('paatos_voittaja_kohde');
	}
	else { // Muutoin merkataan tämä vain aina true
		var tf_voittaja_kohde = true;
	}

	
	/* // VANHA KOODI
	if (kertahankinta.length > 0) {
		document.getElementById('paatos_sop_alku').value = "";
		document.getElementById('paatos_sop_loppu').value = "";
		document.getElementById('paatos_optio').value = "0";
		document.getElementById('paatos_sop_alku').style.background = "#C0C0C0";;
		document.getElementById('paatos_sop_loppu').style.background = "#C0C0C0";
		document.getElementById('paatos_optio').style.background = "#C0C0C0";
		document.getElementById('paatos_sop_alku').disabled = true;
		document.getElementById('paatos_sop_loppu').disabled = true;
		document.getElementById('paatos_optio').disabled = true;
		tf_sop_alku = true;
		tf_sop_loppu = true;
		tf_optio = true;
	}
	if (aikajakso.length > 0) {
		document.getElementById('paatos_sop_kerta').value = "";
		document.getElementById('paatos_sop_kerta').style.background = "#C0C0C0";
		document.getElementById('paatos_sop_kerta').disabled = true;
		tf_sop_kerta = true;
	}
	if (kertahankinta.length == 0) {
		document.getElementById('paatos_sop_alku').disabled = false;
		document.getElementById('paatos_sop_loppu').disabled = false;
		document.getElementById('paatos_optio').disabled = false;
	}
	if (aikajakso.length == 0) {
		document.getElementById('paatos_sop_kerta').disabled = false;
	}
	*/
	// UUSI KOODI
	if (tf_sop_kerta) {
		document.getElementById('paatos_sop_alku').value = "";
		document.getElementById('paatos_sop_loppu').value = "";
		document.getElementById('paatos_optio').value = "0";
		document.getElementById('paatos_sop_alku').style.background = "#C0C0C0";;
		document.getElementById('paatos_sop_loppu').style.background = "#C0C0C0";
		document.getElementById('paatos_optio').style.background = "#C0C0C0";
		document.getElementById('paatos_sop_alku').disabled = true;
		document.getElementById('paatos_sop_loppu').disabled = true;
		document.getElementById('paatos_optio').disabled = true;
		tf_sop_alku = true;
		tf_sop_loppu = true;
		tf_optio = true;
	}
	if (aikajakso.length > 0) {
		document.getElementById('paatos_sop_kerta').disabled = true;
		tf_sop_kerta = true;
	}
	if (!tf_sop_kerta) {
		document.getElementById('paatos_sop_alku').disabled = false;
		document.getElementById('paatos_sop_loppu').disabled = false;
		document.getElementById('paatos_optio').disabled = false;
	}
	if (aikajakso.length == 0) {
		document.getElementById('paatos_sop_kerta').disabled = false;
	}
	
	
	
	// lopullinen tarkistus (kaikki pitää olla true jotta voidaan tallentaa)
	
	// tf_tekija nykyään aina true, se haetaan osittain pudotusvalikosta 
	tf_tekija = true;
	
	if (tf_pvm && tf_numero && tf_voittaja && tf_voittaja_kohde && tf_arvo && tf_sop_loppu && tf_sop_alku && tf_sop_loppu && tf_optio && tf_tekija && tf_laji) {
		document.getElementById('tallenna_nappi').disabled = false;
	}
	else {
		document.getElementById('tallenna_nappi').disabled = true;
	}

}

function validoiPaatostiedotMoniosainenVaihe2() {

	
	var tf_voittaja			= validoiStringi('paatos_voittaja');
	var tf_voittaja_kohde 	= validoiStringi('paatos_voittaja_kohde');
	var tf_laji				= validoiLaji();
	
	// lopullinen tarkistus (kaikki pitää olla true jotta voidaan tallentaa)
	
	if (tf_voittaja && tf_voittaja_kohde && tf_laji) {
		document.getElementById('tallenna_nappi').disabled = false;
	}
	else {
		document.getElementById('tallenna_nappi').disabled = true;
	}

}

// Keskeytyksen validointi

function validoiKeskeytys() {

	var tf_paatospaiva = validoiPVM('paatos_pvm');
	var tf_paatosnro = validoiStringi('paatos_nro');
	var tf_peruste = validoiStringi('peruste');
	var tf_lajit = validoiLaji('laji_id[]');
	var tf_tekija = validoiStringi('paatoksen_tekija');
	

	if (tf_paatospaiva && tf_paatosnro && tf_peruste && tf_lajit && tf_tekija) {
		document.getElementById('tallenna_nappi').disabled = false;
	}
	else {
		document.getElementById('tallenna_nappi').disabled = true;
	}
}

// lisätietojen validointi
function validoiLisatieto() {
	
	var tf_tarjouspyynto = isNumeric2('tarjouspyynto_id');
	var tf_tyyppi = isNumeric2('tyyppi');
	
	if (tf_tarjouspyynto &&  tf_tyyppi) {
		document.getElementById('tallenna_nappi').disabled = false;
	}
	else {
		document.getElementById('tallenna_nappi').disabled = true;
	}
}

// validoi päiväys
function validoiPVM(paiva_id) {
	
	var paiva_kokonainen = document.getElementById(paiva_id).value;
	
	var tarkistus;
	
	tarkistus = isDate(paiva_kokonainen);
	
	if (tarkistus == true) {
		document.getElementById(paiva_id).style.background = "#ADCC5B";
		return true;
	}
	else {
		document.getElementById(paiva_id).style.background = "#FF5B4F";
		return false;
	}
	
	
}

function isNumeric2(stringi) {

	var tarkistus = IsNumeric(document.getElementById(stringi).value);
	
	if (tarkistus) {
	
		document.getElementById(stringi).style.background = "#ADCC5B";
		return true;
		
	}
	else {
	
		document.getElementById(stringi).style.background = "#FF5B4F";
		return false;
		
	}

}

function isNumeric3(stringi) {

	var temp = document.getElementById(stringi).value;
	
	temp = temp.replace('+', '');

	var tarkistus = IsNumeric(temp);
	
	if (tarkistus) {
	
		document.getElementById(stringi).style.background = "#ADCC5B";
		return true;
		
	}
	else {
	
		document.getElementById(stringi).style.background = "#FF5B4F";
		return false;
		
	}

}

function validoiCheckbox(stringi) {

	if (document.getElementById(stringi).checked) {
		return true;
	}
	else {
		return false;
	}

}

function validoiStringi (stringi) {

	var stringi2 = document.getElementById(stringi).value;

	if (stringi2.length == 0) {
		document.getElementById(stringi).style.background = "#FF5B4F";
		return false;
	}
	else {
		document.getElementById(stringi).style.background = "#ADCC5B";
		return true;
	}

}

function validoiLaji(stringi) {
	
	var lajit = document.getElementsByName('laji_id[]');
	var temp2 = false;
	
	//alert(lajit[1].checked);
	
	for (i=0; i<lajit.length; i++) {
	
		if (lajit[i].checked) {
			temp2 = true;
		}
	
	}
	
	if (!temp2) {
		document.getElementById('lajiliitokset').style.border = "2px dotted #FF5B4F";
	}
	else {
		document.getElementById('lajiliitokset').style.border = "2px dotted #ADCC5B";
	}
	
	return temp2;
	
	
}	

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function trim(string) {

	return string.replace(/^\s+|\s+$/g, '') ;

}


