﻿//script type="text/javascript">
function gonder(){
	this.loadingImg='loading.gif';							// dosya yüklenirken, yükleme işleminin halen devam ettiğini ifade edecek animasyon
	this.varName = "sends";
	(this.url)? this.url=url : this.url = "upload.jsp";		// gönderilen dosyaları sunucu tarafndan karşılayacak dosyanın yolu.
	//this.tmpContainer=document.documentElement;							// kod ile oluşturulan iframe ve diğer geçici nesneleri barındıracak elementi belirtir.
	this.tmpContainer = document.getElementsByTagName("body")[0];
	this.formMail = document.formMail;
	this.uplUyari =  this.getId("uplUyari");
};

gonder.prototype.createFormUp = function(){
	
	// iframe
	var sesID=Math.floor(Math.random()*9999999);	// bu yükleme işlemi için benzersiz bir numara üretiliyor
	var ifrm=document.createElement('iframe');		// üzerinden dosyanın gönderileceği frame oluşturluyor.
	this.tmpContainer.appendChild(ifrm);
	this.setAttributes(ifrm,[['width',0],['height',0],['style','display:none;']]);
	
	if(ifrm.contentWindow) ifrm.cnt=ifrm.contentWindow; else ifrm.cnt=ifrm.contentDocument; 	// contnetWindow ie için, contentDocument firefox, opera ve dipeğleri için
	ifrm.cnt.name='hifrm'+sesID;
	ifrm.id='hifrm'+sesID;
	
	//form
	var formUpl = document.createElement('form');
	this.setAttributes(formUpl,[['id','f'+ifrm.id],['target',ifrm.cnt.name],['action',this.url],['method','post'],['encoding','multipart/form-data'],['enctype','multipart/form-data'],['style','display:none;']]);
	
	//fileBrowser
	var newBrowseBox=this.file.cloneNode(true);
	this.setAttributes(this.file,[['id',null],['class',null]]);
	this.file.parentNode.insertBefore(newBrowseBox,this.file);
	
	//hidden
	var hidBox =  document.createElement("input"); //this.getName("number")
	this.setAttributes(hidBox,[['type','text'],['name','deneme'],['value',this.getName("number").value]]);
	
	/*
	var hidBox = this.getId('number').cloneNode(true);
	this.setAttributes(this.getId('number'),[['id',null],['class',null]]);
	this.file.parentNode.insertBefore(hidBox,this.file);
	this.appendChilds(formUpl,this.getId('number'),this.file);
	*/
	
	this.appendChilds(formUpl,hidBox,this.file);
	this.tmpContainer.appendChild(formUpl);

	formUpl.submit();
	this.upControl(ifrm.cnt.name);
};

gonder.prototype.exportFile = function(faylneym){
	var ayrac;
	var n;
	if(faylneym.indexOf("Windows")!=-1){
		ayrac="/";
	}
	else{
		ayrac="\\";
	}
	var n = faylneym.substring(faylneym.lastIndexOf(ayrac),(faylneym.length));
	if (n.indexOf("\\")==0 || n.indexOf("/")==0){
		n = n.substring(1);
	}
	return (n);
	
}

gonder.prototype.upControl = function(ifrmID){
	var vn=this.varName;
	var ifrm=this.getId(ifrmID);
	if(!ifrm) return false;
	this.gonButton = this.getName("gonder");
	this.gonButton.value="Dosya Yükleniyor...";
	try
	{
		if(ifrm.contentWindow)
			var doc=ifrm.contentWindow.document;
		else
			var doc=ifrm.cnt;

		var resp = parseInt(doc.getElementById('sonuc').value);
		//this.getId('dosya').value = this.exportFile(this.file.value);
		this.getId('dosya').value = this.exportFile(doc.getElementById('fsName').value);
//		this.file.value=this.exportFile(doc.getElementById('fsName').value);

		if(resp==0){
			/*
			resimGuncelle("img.jsp","resim");
			this.getName("number").value="";
			*/
			this.getId("uplUyari").innerHTML="Gönderiliyor...";
			document.formMail.submit();
		}
		else{
			var msg;
			switch (resp){
				case 2:
					msg = "Lütfen Boş Dosya Göndermeyiniz.";
					break;
				case 4:
					msg = "İzin Verilmeyen Dosya Türü. Resim, PowerPoint ve Word Dosyalarının Yüklenmesine İzin Verilir.";
					break;
				case 6:
					msg = "Yükleme Sınırını Aştınız.";
					break;
				case 7:
					msg = "Resimdeki Güvenlik Kodunu Aşağıdaki Alana Doğru Girip Tekrar Deneyiniz.";
					break;
				case 8:
					msg = "Resimdeki Güvenlik Kodunu Aşağıdaki Alana Girip Tekrar Deneyiniz.";
					break;
				case 9:
					msg = "Dosya Boyutu Sınırı Aşılmış. En fazla 1 MB (1024 kb) Dosya Göndermeyi Deneyiniz.";
					break;
				case 10:
					msg = "Formu Yanlış Bir Şekilde Kullanıyorsunuz. Lütfen Doğru Bir Kullanım Deneyiniz.";
					break;
				default:
					msg = "Bilinmeyen Hata Oluştu, Lütfen Internet Explorer ile Daha Sonra Tekrar Deneyiniz.";
					break;
			}
			this.getId("uplUyari").innerHTML=msg;
			this.getName("number").value="";
			this.getId("dosya").value="";
			resimGuncelle("img.jsp","resim");
			this.gonButton.value="Tekrar Gönder";
		}
		// iframe ile yapılacak bir şey kalamdı, siliniyor.
		this.removeTempElements(ifrmID);
	}
	catch(e)
	{
		//henüz dosya yüklenmemiş, dolayısıyla yanıt gelmemişse, bu metodu biraz zaman sonra tekrar çalıştır.
		setTimeout(vn+'.upControl(\''+ifrmID+'\')',1800);
		return false;
	}
}

gonder.prototype.removeTempElements=function(ifrmID){
	this.getId(ifrmID).parentNode.removeChild(this.getId(ifrmID));
	this.getId("f"+ifrmID).parentNode.removeChild(this.getId("f"+ifrmID));
}

gonder.prototype.getId = function(){
	this.element = document.getElementById(arguments[0]);
	return this.element;
};

gonder.prototype.getName = function(){
	this.element = document.getElementsByName(arguments[0])[0];
	return this.element;
};

gonder.prototype.setAttributes=function(obj,attrs)
{
	for(var i=0;i<attrs.length;i++) obj.setAttribute(attrs[i][0],attrs[i][1]); 
	return true;
}

gonder.prototype.appendChilds=function(obj)
{
	for(var i=1;i<arguments.length;i++) obj.appendChild(arguments[i]);
	return true;
}

gonder.prototype.start = function(){
	this.file = this.getName("file");	// upload edilecek dosyayi iceren obje
	if(this.file.value.length>0){
		this.createFormUp();
		this.uplUyari.innerHTML = "<img src='"+this.loadingImg+"' align='absmiddle' /> Dosya yükleniyor lütfen bekleyiniz..."
	}
	else{
		this.formMail.submit();
	}
};

var sends = new gonder();
//script>
