if(typeof UPLOAD=="undefined")
{
	var UPLOAD  = {
	Form:"test",
	SavePath: "tmp",
	CreateCheckbox: true,
	MaxUploadFiles: 3,
	MaxUploadFileSize: 2000,
	ContainerUpload: false,
	UpLoadFilePHPSrc: "/?page=ofertaReservationUploadFile&type=text/xml&test=1",
	CreatePathUrl: false
	};
}

UPLOAD.Presentation = {
	SetCheckbox:function(name)
	{
		var element = document.createElement("div");
		element.setAttribute("id", "div_"+name);
		var el = document.createElement("input");
		el.setAttribute("type", "checkbox");
		var teraz = new Date();
		var i = Math.abs(Math.sin(teraz.getTime()));
		//el.setAttribute("name", "file_"+(UPLOAD.Form.getElementsByTagName("div").length));
		el.setAttribute("name", "file_" + i);
		el.setAttribute("value", name);
		el.setAttribute("id", name);
		el.setAttribute("checked", "checked");
		element.appendChild(el);
		var el = document.createElement("label");
		el.setAttribute("for",name);
		el.innerHTML = name;
		element.appendChild(el);
		var el = document.createElement("input");
		el.setAttribute("type", "button");
		el.setAttribute("value", "usun");
		el.onclick = function (e) {
			if(UPLOAD.ContainerUpload) UPLOAD.ContainerUpload.removeChild(document.getElementById(element.getAttribute("id")));
			else UPLOAD.Form.removeChild(document.getElementById(element.getAttribute("id")));
		}
		element.appendChild(el);
		if(UPLOAD.ContainerUpload) {
			UPLOAD.ContainerUpload.appendChild(element);
		}
		else {
			UPLOAD.Form.appendChild(element);
		}
		if (document.getElementById(name))document.getElementById(name).checked=true;
	},
	SetCreatePathUrl:function(xml){
		if(!xml.getAttribute("new_name")){
			alert("Niepoprawny format pliku. Dozwolone tylko pliki w formacie .pdf");
		}
		else{
			UPLOAD.ContainerUpload.innerHTML = '<b><span style="color:#F00">Dodano cennik</span><br /><a target="_blank" href="http://img.wakacje.pl/zdj/wypoczynek/pricelists/'+xml.getAttribute("new_name")+'">Podgląd cennika</a></b> <b><a style=\'cursor:pointer\' onclick=\"if(confirm(\'Czy na pewno chcesz usunąć plik z cennikiem?\')) { var request = YAHOO.util.Connect.asyncRequest(\'POST\', \'/ed/delfile.php\', callback, \'id_obiekty='+xml.getAttribute("id_obiekty")+'\'); }\">Usuń cennik</a></b>';	
		}
	},
	Show:function(data)
	{
		xml=data.documentElement;
		if(UPLOAD.CreateCheckbox) this.SetCheckbox(xml.getAttribute("new_name"));
		if(UPLOAD.CreatePathUrl){
			this.SetCreatePathUrl(xml);
		}
	}
}

UPLOAD.Call = {

	upload:function(o)
	{
		UPLOAD.Presentation.Show(o.responseXML);
	}
}

UPLOAD.UploadFile = {
	/**
		Sprawdza czy w ciągu nie występują polskie znaki poczynając od pozycji pos
	**/
	IsPLSign:function(string,pos){
		if(string.indexOf("ą",pos)>=0) return true;
		if(string.indexOf("Ą",pos)>=0) return true;
		if(string.indexOf("ż",pos)>=0) return true;
		if(string.indexOf("Ż",pos)>=0) return true;
		if(string.indexOf("ź",pos)>=0) return true;
		if(string.indexOf("Ź",pos)>=0) return true;
		if(string.indexOf("ń",pos)>=0) return true;
		if(string.indexOf("Ń",pos)>=0) return true;
		if(string.indexOf("ś",pos)>=0) return true;
		if(string.indexOf("Ś",pos)>=0) return true;
		if(string.indexOf("ł",pos)>=0) return true;
		if(string.indexOf("Ł",pos)>=0) return true;
		if(string.indexOf("ę",pos)>=0) return true;
		if(string.indexOf("Ę",pos)>=0) return true;
		if(string.indexOf("ó",pos)>=0) return true;
		if(string.indexOf("Ó",pos)>=0) return true;
		return false;
	},

	/**
		Funkcja sprawdza ostatnie wystąpienie backslasha w ciągu
		Grzegorz Prawdzik 2008-02-08
	**/

	findLastSlash:function (string){
		var slash = "\\";
		var pos=0;
		var pos_tmp;
		while (pos>=0){
			pos_tmp = pos;
			pos = string.indexOf(slash,pos);

				if (pos>=0) {
					pos+=1;
					pos_tmp=pos;
				}

		}
		return pos_tmp;
	},

	UploadFile:function()
	{
		if(UPLOAD.ContainerUpload)
		{
			if((UPLOAD.ContainerUpload.getElementsByTagName("div").length)>=(UPLOAD.MaxUploadFiles)) return false;
		}
		else
		{
			if((UPLOAD.Form.getElementsByTagName("div").length)>=(UPLOAD.MaxUploadFiles)) return false;
		}
		var inputs = UPLOAD.Form.getElementsByTagName("input");
		for(var i=0; i<inputs.length; i++){
			if(inputs[i].type=="file"){
				var pos = UPLOAD.UploadFile.findLastSlash(inputs[i].value);
				//sprawdzamy polskie znaki tylko w nazwie pliku
				if (UPLOAD.UploadFile.IsPLSign(inputs[i].value,pos)){
					alert("Nie można dodać tego pliku ponieważ jego nazwa zawiera niedozwolone znaki");
					return false;
				}
			}
		}
		UPLOAD.Call.SavePath = this.SavePath;
		var data = "SavePath="+UPLOAD.SavePath;
		YAHOO.util.Connect.setForm(UPLOAD.Form,true);
		YAHOO.util.Connect.asyncRequest("POST", UPLOAD.UpLoadFilePHPSrc, UPLOAD.Call);

	}
}
