var xmlhttp

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
} catch (e) {
try {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
} catch (E) {
xmlhttp=false
 }
 }
@else
xmlhttp=false
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false
	}
}

var valittu_tiedosto = false;
var valittu_hakemisto = false;
var valittu_hakemisto_selko = false;
var valittu_hakemisto_kuva = false;
var valittu_hakemisto_plussa = false;
var valittu_hakemisto_linkki = false;
var valittu_tyokalu = false;
var valinta_poistettavissa = false;
var avaus_id;
var draggables = new Array();

function teeHakemisto(dir) {

	query = "action=tee_hakemisto&ID=" + encodeURI(dir);
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);

	document.getElementById('hakemistolista').innerHTML = xmlhttp.responseText;
	
	query = "action=tee_hakemisto&ID=" + encodeURI(dir) + "&droppables=1";
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);

	eval(xmlhttp.responseText);
}

function valitseTiedosto(tiedosto, file, kommentti_id, toiminto) {

	if (valittu_tiedosto) {
	
		valittu_tiedosto.className = "tiedosto";
		valittu_tiedosto = false;
	}
	
	valittu_tiedosto = document.getElementById(tiedosto);
	
	if (valittu_tiedosto) {
	
		valittu_tiedosto.className = "tiedosto_valittu";
		valinta_poistettavissa = false;
			
		window.setTimeout("asetaValintaPoistettavissa()", 200);
	}	
		
	if (file) {
	
		suljeTyokalu();
		
		if (!kommentti_id) kommentti_id = '';
		if (!toiminto) toiminto = '';
		
		query = "action=hae_tiedoston_tiedot&ID=" + encodeURI(file) + "&KOMMENTTI=" + encodeURI(kommentti_id) + "&TOIMINTO=" + encodeURI(toiminto);
		url = PHP_SELF;
		xmlhttp.open("POST",url,false);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(query);

		document.getElementById('alalomake').innerHTML = xmlhttp.responseText;
	} 
}

function asetaValintaPoistettavissa() {
	valinta_poistettavissa = true;
}

function haeHakemistonTiedot(dir) {

	suljeTyokalu();
	
	query = "action=hae_hakemiston_tiedot&ID=" + encodeURI(dir);
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);

	document.getElementById('alalomake').innerHTML = xmlhttp.responseText;
}

function siirraTiedosto(tiedosto, dir, dir_koodattu, elem) {
	
	valitseHakemisto(dir, dir_koodattu, true);
	
	valittu_hakemisto_linkki.style.cursor = "wait";
	
	query = "action=siirra_tiedosto&TIEDOSTONIMI=" + encodeURI(tiedosto) + "&ID=" + encodeURI(dir);
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);
	
	if (xmlhttp.responseText.length > 0) alert(xmlhttp.responseText);
	
	valittu_hakemisto_linkki.style.cursor = "default";
	
	window.location = PHP_SELF+"?ID="+encodeURI(dir);
}

function valitseHakemisto(dir, dir_koodattu, ala_hae_tiedostoja) {

	//if (dir == valittu_hakemisto_selko && dir) return false;

	valittu_hakemisto_selko = dir;
	
	if (valittu_tyokalu) {
		avaaTyokalu(valittu_tyokalu, valittu_tyokalu_kohde);
	}
	
	if (valittu_hakemisto_linkki) valittu_hakemisto_linkki.className = 'hakemiston_nimi';
	valittu_hakemisto_linkki = document.getElementById("linkki_"+dir_koodattu);
	if (valittu_hakemisto_linkki) valittu_hakemisto_linkki.className = 'hakemiston_nimi_valittu';

	if (valittu_hakemisto_kuva) valittu_hakemisto_kuva.src = 'kuvat/dokumenttienhallinta/folder.gif';
	valittu_hakemisto_kuva = document.getElementById("folder_kuva_"+dir_koodattu);
	valittu_hakemisto_kuva.src = 'kuvat/dokumenttienhallinta/folder_avattu.gif';
	
	if (!ala_hae_tiedostoja) haeTiedostot(dir, dir_koodattu);
	
	haeHakemistonTiedot(dir);
}

function haeTiedostot(dir, dir_koodattu, jarjesta, jarjestys) {

	valittu_hakemisto_linkki.style.cursor = "wait";
	
	if (jarjesta) document.getElementById('tiedostot_otsikkorivi').style.cursor = "wait";

	query = "action=hae_hakemistopolku&ID=" + encodeURI(dir);
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);

	document.getElementById('valittu_hakemisto').innerHTML = xmlhttp.responseText;

	query = "action=hae_tiedostot&ID=" + encodeURI(dir);
	if (jarjesta) query += "&JARJESTA=" + encodeURI(jarjesta);
	if (jarjestys) query += "&JARJESTYS=" + encodeURI(jarjestys);
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);

	document.getElementById('tiedostot').innerHTML = xmlhttp.responseText;

	query = "action=hae_tiedostot&ID=" + encodeURI(dir) + "&draggables=1";
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);

	eval(xmlhttp.responseText);

	valittu_hakemisto_linkki.style.cursor = "default";
	
	if (jarjesta) document.getElementById('tiedostot_otsikkorivi').style.cursor = "default";
	
	valinta_poistettavissa = false;
		
	window.setTimeout("asetaValintaPoistettavissa()", 200);
	
	return;
}

function avaaSulje(dir_koodattu, dir, hae_tiedostot, avaa_myos, ala_sulje) {

	valittu_hakemisto = document.getElementById(dir_koodattu);
	
	valittu_hakemisto_plussa = document.getElementById("plussa_"+dir_koodattu);

	if (valittu_hakemisto) {
	
		if (valittu_hakemisto.style.display == 'none' || ala_sulje) {
	
			valittu_hakemisto.style.display = 'block';
			
			query = "action=hae_on_alahakemistoja&ID=" + encodeURI(dir);
			url = PHP_SELF;
			xmlhttp.open("POST",url,false);
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.send(query);
			
			if (hae_tiedostot) {
				
				query = "action=hae_on_alahakemistoja&ID=" + encodeURI(dir);
				url = PHP_SELF;
				xmlhttp.open("POST",url,false);
				xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				xmlhttp.send(query);
				
				if (xmlhttp.responseText == "1") valittu_hakemisto_plussa.src = 'kuvat/dokumenttienhallinta/miinus.gif'; 
			
			} else if (xmlhttp.responseText == "1") valittu_hakemisto_plussa.src = 'kuvat/dokumenttienhallinta/miinus.gif'; 
			
		} else {
	
			valittu_hakemisto.style.display = 'none';
			valittu_hakemisto_plussa.src = 'kuvat/dokumenttienhallinta/plus.gif'; 
		}
	}
	
	if (avaa_myos) {
	
		valittu_hakemisto_kuva = document.getElementById("folder_kuva_"+avaa_myos);
		valittu_hakemisto_linkki = document.getElementById("linkki_"+avaa_myos);
		
		yla_hakemisto = document.getElementById(avaa_myos);
		yla_hakemisto_plussa = document.getElementById("plussa_"+avaa_myos);
		yla_hakemisto_linkki = document.getElementById("linkki_"+avaa_myos);
		
		yla_hakemisto.style.display = 'block';
		yla_hakemisto_plussa.src = 'kuvat/dokumenttienhallinta/miinus.gif'; 
	}
	
	if (hae_tiedostot) {

		valitseHakemisto(dir, dir_koodattu);
	}
}

function avaaTyokalu(tyokalu, kohde) {

	valittu_tyokalu = tyokalu;
	valittu_tyokalu_kohde = kohde;

	query = "action=tyokalu&tyokalu=" + encodeURI(tyokalu) + "&ID=" + encodeURI(valittu_hakemisto_selko);
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);

	document.getElementById('alalomake').innerHTML = '';

	document.getElementById('alalomake').innerHTML = xmlhttp.responseText;
}

function suljeTyokalu(mode) {

	valittu_tyokalu = false;
	if (mode == 1) document.getElementById('hakutulos').innerHTML = '';
		else document.getElementById('alalomake').innerHTML = '';
}

function avaaTiedosto(tiedosto, muokattavaksi) {
	
	openWindow(PHP_SELF+"?action=avaa_tiedosto&ID="+encodeURI(tiedosto)+"&muokattavaksi="+muokattavaksi);
}

function haeTiedostoOnOlemassa(dir, tiedostopolku) {

	query = "action=tarkista_tiedosto&HAKEMISTO=" + encodeURI(dir) + "&TIEDOSTO=" + encodeURI(tiedostopolku);
	url = PHP_SELF;
	xmlhttp.open("POST",url,false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(query);

	if (parseInt(xmlhttp.responseText) == "1") {
		
		alert("File already exists!");
		return true;
		
	} else return false;
}

function tarkistaNimi(nimi, mode) {

	return true;

	if (mode == 1) {
	
		osat = nimi.split("\\");
		
		i = osat.length - 1;
		
		nimi = osat[i];
	}
	
	re = new RegExp('^[a-zA-Z0-9åäöÅÄÖ ,\._\(\)\-\/]+$');

	if (re.test(nimi)) return true;
		else {
			alert("Name can only contain letters (a-ö, A-Ö), numbers (0-9) and punctuation marks (.,_- )");
			return false;
		}
}

function openWindow(url, w, h, mode) {
 	
	d=document;
	l=(d.layers)?1:0;
	if (l) {
		x = window.innerWidth;
		y = window.innerHeight;
	} else {
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	if (mode == 1) {
		x = x/2 - w/2;
		y = y/2 - h/2;
	}
	else {
		x = x/2 - mode;
		y = 80;
	}

	var mypopup = window.open(url, 'pbatch', 'toolbar=no,resizable=1,scrollbars=1,left='+ x 
	+',top='+y+',screenX='+ x +',screenY='+y);
	mypopup.opener = self;
}
