var increment=0;
var aktiivinenIncrement;
var iideet  = new Array();
var indeksi =0;

var uusia_kuvia=0;
var uusia_tiedostoja=0;


function lisaaTiedostorivi(tyyppi,id,suunta){
  	teeAjaxKutsu('../ajax/haeKansiot.php','',handleTiedostorivi);
}//end function


function handleTiedostorivi(){
  	if (xmlhttp.readyState == 4) {
        /* If the data was retrieved successfully */
   	    if (xmlhttp.status == 200){
            var xmlDocument = xmlhttp.responseXML;
            var kansiot = xmlDocument.getElementsByTagName('kansio');

        	var url = '<table cellpadding="0" cellspacing="0"><tr><td colspan="3" style="padding-top:10px;">Lataa tiedosto</td></tr><tr><td colspan="3"><input type="file" name="uusi_'+ uusia_tiedostoja + '_sivutiedosto[0]" size="45" /></td></tr><tr><td colspan="3">Järjestysnumero</td></tr><tr><td colspan="3"><input name="uusi_'+ uusia_tiedostoja + '_sivutiedostojarjestys" type="text" value="" /></td></tr><tr><td colspan="3">Tiedoston kuvausteksti</td></tr><tr><td colspan="3"><textarea cols="45" rows="3" name="uusi_'+ uusia_tiedostoja + '_sivutiedostoteksti"></textarea></td></tr><tr><td colspan="3">Tiedoston kansio</td></tr><tr><td colspan="3"><select name="uusi_'+ uusia_tiedostoja + '_sivutiedostokansioID"><option value="0"> - </option>';

			if(kansiot.length>0){
                for(i=0; i<kansiot.length; i++){
                    url = url + '<option value="' + kansiot[i].getAttribute('id') + '">' + kansiot[i].getAttribute('nimi') + '</option>';
                }//end for
            }//end if

            url = url + '</select></td></tr></table>';
        
        	var uusi_tr = document.getElementById('tiedostoosio_table').insertRow(0);
        	var uusi_td = uusi_tr.insertCell(0);
        	
        	uusi_td.colSpan="2";
        	uusi_td.innerHTML=url;
        
            uusia_tiedostoja++;
        }//end if
    }//end if
}//end function



function avaaTiedostoosio(id,tyyppi){
  	if(tyyppi=='avaa'){
	    if(document.getElementById('sivutiedosto_' + id)){ 
	        document.getElementById('sivutiedosto_' + id).style.display='';
	    }//end if

	    if(document.getElementById('sivutiedostosulje_' + id)){
	        document.getElementById('sivutiedostosulje_' + id).style.display='';
	    }//end if
	    
	    if(document.getElementById('sivutiedostokansio_txt_' + id)){
		  	document.getElementById('sivutiedostokansio_txt_' + id).style.display='none';
		}//end if

	    if(document.getElementById('sivutiedostoteksti_txt_' + id)){
		  	document.getElementById('sivutiedostoteksti_txt_' + id).style.display='none';
		}//end if
	}else if(tyyppi=='sulje'){
	    if(document.getElementById('sivutiedosto_' + id)){ 
	        document.getElementById('sivutiedosto_' + id).style.display='none';
	    }//end if	  

	    if(document.getElementById('sivutiedostosulje_' + id)){ 
	        document.getElementById('sivutiedostosulje_' + id).style.display='none';
	    }//end if

	    if(document.getElementById('sivutiedostojarjestys_txt_arvo_' + id)){
	      	txt_olio= document.getElementById('sivutiedostojarjestys_txt_' + id);
	      	txt_arvo= document.getElementById('sivutiedostojarjestys_txt_arvo_' + id);
	      	txt		= document.getElementById('sivutiedostojarjestys_input_' + id).value;
	      	
		  	txt_olio.style.display='';
		  	asetaTeksti(txt_arvo,txt);
		}//end if

	    if(document.getElementById('sivutiedostoteksti_txt_arvo_' + id)){
	      	txt_olio= document.getElementById('sivutiedostoteksti_txt_' + id);
	      	txt_arvo= document.getElementById('sivutiedostoteksti_txt_arvo_' + id);
	      	txt		= document.getElementById('sivutiedostoteksti_input_' + id).value;
	      	
		  	txt_olio.style.display='';
		  	asetaTeksti(txt_arvo,txt);

		}//end if

	    if(document.getElementById('sivutiedostokansio_txt_arvo_' + id)){
	      	txt_olio= document.getElementById('sivutiedostokansio_txt_' + id);
	      	txt_arvo= document.getElementById('sivutiedostokansio_txt_arvo_' + id);

            var val = document.getElementById('sivutiedostokansio_input_' + id).selectedIndex;
            txt = document.getElementById('sivutiedostokansio_input_' + id).options[val].text;
            
		  	txt_olio.style.display='';
		  	asetaTeksti(txt_arvo,txt);

		}//end if
	}//end if
}//end function



function poistaSivutiedosto(id,nimi){
    if(document.getElementById('sivutiedosto_' + id)){
        if(confirm("Haluatko varmasti poistaa tiedoston '" + nimi +"'? Tiedosto poistetaan lopullisesti vasta Tallenna-nappia painettaessa.")){
            var tr = document.getElementById('tiedostoosio_' + id);
            tr.parentNode.removeChild(tr);
        }//end if
    }//end if
}//end function



function poistaGalleriakuva(id,nimi){
    if(document.getElementById('kuva_' + id)){
        if(confirm("Haluatko varmasti poistaa galleriasta kuvan '" + nimi +"'? Kuva poistetaan lopullisesti vasta Tallenna-nappia painettaessa.")){
            var tr  = document.getElementById('kuva_' + id);
            var tr2 = document.getElementById('kuvaosio_' + id);
            tr.parentNode.removeChild(tr);
            tr2.parentNode.removeChild(tr2);
        }//end if
    }//end if
}//end function


function avaaKuvaosio(id,tyyppi){
  	if(tyyppi=='avaa'){
	    if(document.getElementById('kuva_' + id)){ 
	        document.getElementById('kuva_' + id).style.display='';
	    }//end if

	    if(document.getElementById('sulje_' + id)){
	        document.getElementById('sulje_' + id).style.display='';
	    }//end if
	    
	    if(document.getElementById('otsikko_txt_' + id)){
		  	document.getElementById('otsikko_txt_' + id).style.display='none';
		}//end if

	    if(document.getElementById('kuvateksti_txt_' + id)){
		  	document.getElementById('kuvateksti_txt_' + id).style.display='none';
		}//end if
	}else if(tyyppi=='sulje'){
	    if(document.getElementById('kuva_' + id)){ 
	        document.getElementById('kuva_' + id).style.display='none';
	    }//end if	  

	    if(document.getElementById('sulje_' + id)){ 
	        document.getElementById('sulje_' + id).style.display='none';
	    }//end if

	    if(document.getElementById('otsikko_txt_' + id)){
	      	txt_olio= document.getElementById('otsikko_txt_' + id);
	      	txt		= document.getElementById('otsikko_input_' + id).value;
	      	
		  	txt_olio.style.display='';
		  	asetaTeksti(txt_olio,txt);
		}//end if

	    if(document.getElementById('kuvateksti_txt_' + id)){
	      	txt_olio= document.getElementById('kuvateksti_txt_' + id);
	      	txt		= document.getElementById('kuvateksti_input_' + id).value;
	      	
		  	txt_olio.style.display='';
		  	asetaTeksti(txt_olio,txt);

		}//end if
	}//end if
}//end function


function luoLomake(){
  	
    var url='<table class="kr_leveys" style="border-collapse: collapse;"><tbody><tr><td>Lataa kuva</td></tr><tr><td><input name="uusi_' + uusia_kuvia + '_tiedosto[0]" size="35" type="file"></td></tr><tr><td class="tilaaYlos">Kuvateksti</td></tr><tr><td><textarea rows="4" cols="47" name="uusi_' + uusia_kuvia + '_kuvateksti" ></textarea></td></tr><tr><td>Järjestysnumero</td></tr><tr><td><input type="text" name="uusi_' +  uusia_kuvia +'_jarjestys" class="otsikko" /></td></tr></tbody></table>';

	var uusi_tr = document.getElementById('kuvaosio_table').insertRow(0);
	var uusi_td = uusi_tr.insertCell(0);
	
	uusi_td.colSpan="2";
	uusi_td.innerHTML=url;
	
    uusia_kuvia++;
}//end function


function poistaRivi(id){
    var otus = document.getElementById(id);

    otus.parentNode.removeChild(otus);
}//end function


function lisaaTagi(td,tyyppi,laatikko){
    if(linkki=='url'){teksti='/Linkki';}else{teksti='Linkki';}

    var otus=document.getElementById(laatikko);
    otus.value=otus.value + "[" + linkki + "]";
    otus.focus();

    asetaTeksti(document.getElementById(td),teksti);

    if(linkki=='url'){linkki='/url';}else{linkki='url';}
}//end function

function alustaPvmLista(kuukausi,otus){
    var paiva_lkm;
    var pv_arvo;

    switch(parseInt(kuukausi)){
        case 2:
            paiva_lkm=28;
        break;

        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            paiva_lkm=31;
        break;

        case 4: case 6: case 9: case 11:
            paiva_lkm=30;
        break;
    }//end switch

    elementti=document.getElementById(otus);

    pv_arvo=elementti.value;
    elementti.options.length=0;

    for(i=0; i<paiva_lkm; i++){
        elementti.options[parseInt(i)]=new Option(parseInt(i+1),parseInt(i+1));
    }//end for

    if(parseInt(pv_arvo)<=parseInt(paiva_lkm)){
        elementti.value=parseInt(pv_arvo)
    }else{
        elementti.value=parseInt(paiva_lkm)
    }//end if
}//end function


function poistaEntiteetti(sivu,id,teksti){
    if(confirm("Haluatko varmasti poistaa " + teksti + "?")){
        location.href = sivu + "?poisto=1&id=" + escape(id);
    }//end function
}//end function


function tyhjennaOtus(otusnimi){
    var otus = document.getElementById(otusnimi);
    asetaTeksti(otus,"");
}//end function

function luoInputFile(otusnimi){
    var otus = document.getElementById(otusnimi);

    td      = document.createElement("td");
    input   = document.createElement("input");

    otus.appendChild(td);
    td.appendChild(input);

    input.setAttribute('type','file');
    input.setAttribute('name','tiedosto[0]');
    input.setAttribute('size',70);
}//end function

function asetaTeksti(otus,arvo){
    if(IEselain){
        otus.innerText=arvo;
    }else{
        otus.textContent=arvo;
    }//end if
}//end function

var filenro=1;

function lisaaInputFileRivi(listanimi,etuliite,nimi){
 	/*******************************************************
	* Luodaan Elementit
	*******************************************************/
	var oppilaslista = document.getElementById(listanimi);
	var tr			 = document.createElement('tr');
	var td			 = document.createElement('td');
	var input		 = document.createElement('input');

	/*******************************************************
	* Liitetään TR->TABLEEN ja TD->TR:N
	********************************************************/
	oppilaslista.appendChild(tr);
	tr.appendChild(td);
	tr.setAttribute('id','tiedostorivi' + filenro);
	
	td.colSpan="2";

	/*******************************************************
	* Asetetaan SELECTIN atribuutit
	********************************************************/

	//id
	input.setAttribute('id',(etuliite) + (filenro));
	//name
	input.setAttribute('name',nimi + '['+ (filenro) + ']');
	//onChange
	input.setAttribute('onChange','javascript:lisaaInputFileRivi("' + listanimi + '","' + etuliite + '","' + nimi + '")');
	//css-luokka

    input.setAttribute('size',35);
    input.setAttribute('type','file');

	/*******************************************************
	* Liitetään INPUT->TD:n
	********************************************************/
	td.appendChild(input);

    filenro++;
}//end function


	function poistaElementti(elementtiNimi,uusi,otsikko,lisaa){
	    elementti=document.getElementById(elementtiNimi);
	    isa=elementti.parentNode;
	    isa.removeChild(elementti);
	
		if(lisaa){
		    isa.innerHTML=isa.innerHTML + '<tr><td>'+otsikko+'</td></tr><tr><td><input type="file" name="' + uusi + '[0]" size="35" /></td></tr>';
		}//end if
	}//end function	

