//******************
//*** JAVASCRIPT ***
//******************
//
//Copyright © My Ftp Space
//Tutti i diritti riservati - Vietata la riproduzione anche parziale
//
//
//Author: Diego Rispoli
//Contact: diego.rispoli@gmail.com
//Web Site: www.diegorispolit.it
//
//Attention! don't copy, not to modify, and not to cancel this code
//You may not include this as part of your own work and then sell it or pass it off as your own work
//Every abuse will be punished
//
//###################################################################################################

//§ PRIVATE CODE §

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function clock(){
  var time = new Date()
  var hour = time.getHours()
  var minute = time.getMinutes()
  var second = time.getSeconds()
  var temp = "" + ((hour < 10) ? "0" : "") + hour
  temp += ((minute < 10) ? ":0" : ":") + minute
  temp += ((second < 10) ? ":0" : ":") + second
  //temp = time.getDate() + " "+ temp
  document.getElementById('clock').innerHTML = temp
  var id = setTimeout("clock()",500)
}
function timeNow(id){
  var time = new Date()
  var hour = time.getHours()
  var minute = time.getMinutes()
  var second = time.getSeconds()
  var temp = "" + ((hour < 10) ? "0" : "") + hour
  temp += ((minute < 10) ? ":0" : ":") + minute
  temp += ((second < 10) ? ":0" : ":") + second
  //temp = time.getDate() + " "+ temp
  document.getElementById(id).innerHTML = temp
}
var timerID = 0;
var tStart  = null;
var tID = null;
function UpdateTimer() {
   if(!tStart) tStart   = new Date();
   var   tDate = new Date();
   var   tDiff = tDate.getTime() - tStart.getTime();
   tDate.setTime(tDiff);
   var h = tDate.getUTCHours()
   var m = tDate.getUTCMinutes()
   var s = tDate.getUTCSeconds()
   
   if(h<10) h = '0'+h
   if(m<10) m = '0'+m
   if(s<10) s = '0'+s
   if(tID)
   document.getElementById(tID).innerHTML = "" 
   								   + h + ":"
                                   + m + ":" 
                                   + s;
}

function timer(id) {
   tStart   = new Date();
   tID = id;
   document.getElementById(tID).innerHTML = "00:00:00";
   timerID  = setTimeout("UpdateTimer()", 1000);
}
function  getFileImage(){
	var els = getElementsByClass('file_icon',document,'img')
	if (els.length != 0){
		var res = els[0].src.split('_')[2]
		for(n=0;n<els.length;n++){
			els[n].src = getImg(els[n].alt,res)
		}
	}
	//closeUpload();
}

function getImg(ext,res){
	var path='images/'
	switch(ext){
		// text files
		case ('txt'): case ('css'): case ('log'): case ('ini'):
			return path+'file_txt_'+res
		break
		// scripts
		case ('asp'): case ('php'): case ('pl'): case ('plx'): case ('cgi'): case ('aspx'): case ('asa'): case ('js'): case ('xml'):
			return path+'file_asp_'+res
		case ('java'): case ('jsp'):
			return path+'file_java_'+res
		break
		// html
		case ('htm'): case ('html'): case ('shtml'):
			return path+'file_html_'+res
		break
		// office documents
		case ('doc'): case ('rtf'):
			return path+'file_doc_'+res
		case ('xls'):case('csv'):
			return path+'file_xls_'+res
		case ('ppt'):
			return path+'file_ppt_'+res
		case ('mdb'):
			return path+'file_mdb_'+res
		break
		// binaries
		case ('exe'): case ('msi'):
			return path+'file_exe_'+res
		case ('dll'):
			return path+'file_dll_'+res
		break
		// disk images
		case ('iso'): case ('img'): case ('bin'):
			return path+'file_iso_'+res
		break
		// executables
		case ('bat'): case ('class'):
			return path+'file_bat_'+res
		break
		// archives
		case ('zip'): 
			return path+'file_zip_'+res
		case ('rar'): 
			return path+'file_rar_'+res
		case ('tar'):
			return path+'file_tar_'+res
		case ('ace'): case ('jar'):
			return path+'file_jar_'+res
		break
		// images
		case ('gif'):
			return path+'file_gif_'+res
		case ('tif'): case ('jpg'): case ('jpeg'): case ('bmp'):
			return path+'file_jpg_'+res
		case ('png'): case ('psd'):
			return path+'file_png_'+res
		break
		// pdf
		case ('pdf'):
			return path+'file_pdf_'+res
		break
		// flash
		case ('swf'):
			return path+'file_swf_'+res
		case ('fla'):
			return path+'file_fla_'+res
		break
		// video
		case ('avi'): case ('mpeg'): case ('mpg'): case ('rm'): case ('ram'):
			return path+'file_avi_'+res
		case ('mov'):
			return path+'file_mov_'+res
		break
		// audio
		case ('mp3'): case ('mp4'): case ('wav'): case ('ogg'): case ('aac'): case ('au'): case ('aiff'):
			return path+'file_mp3_'+res
		break
		// windows media
		case ('wmv'): case ('wma'): case ('asf'): case ('wmf'):
			return path+'file_wmv_'+res
		break
		// blank
		case ('blank'): case (''):
			return path+'file_blank_'+res
		break
		//help
		case ('hlp'): case ('chm'):
			return path+'file_hlp_'+res
		// others
		default:
			return path+'file_foo_'+res
	}
}
function taskHeight(){
	var h = document.getElementById('browser').clientHeight
    document.getElementById('taskPane').style.height = h+"px";
}
var newWin
function openUpload(){
    newWin = window.open('','Uploading','width=330,height=250,resizable=no');
    var str = ""
    str+="<html><head><title>Loading dates</title><style type=\"text/css\">body{font-family:verdana;font-size:90%;background-color:#F0F1F5;}</style><\/head>"
    str+="<body><div id='uploading' style='display:block'><img src='../images/loading.gif' alt='Loading' \/> Loading in progress...<br \/>Waiting moment.<\/div>"
    str+="<\/body><\/html>"
    newWin.document.write(str);
	//newWin.location.reload(true)
}
function closeUpload(){
	if(readCookie("uploaded")=="true"){
		eraseCookie("uploaded")
		newWin = window.open('','Uploading');
		newWin.close();
	}
}
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// define variable to point to progress window
var UploadProgress;

function UploadForm_Submit(type,id)
{
	//alert(type +" "+id)
	var Session = new String();
	Session = Math.floor(Math.random() * 0xFFFFFF).toString(16);
	if(id==null||id=='')id=Session
	// build URL for upload status
	if (type=='tabs'){
		var ProgressUrl = "includes/progress_status.htm?uploader="+type+"&session=" + id;
	}else{
		var ProgressUrl = "includes/progress_status.htm?uploader="+type+"&session=" + id;
	}
	// define features for pop-up window
	var Features = new String("");
	var width = 350
	var height = 350
	Features += "toolbar=no, ";
	//Features += "menubar=no, ";
	Features += "status=yes, ";
	Features += "resizable=no, ";
	Features += "top="+(screen.height - height) /2+", ";
	Features += "left="+(screen.width - width)/2+", ";
	Features += "width="+width+", ";
	Features += "height="+height;

	// open the pop-up window
	UploadProgress = window.open(ProgressUrl, "upload", Features);
	UploadProgress.focus()
	//UploadProgress.opener.window()
	// append identification of session
	var a = document.getElementById('UploadForm').action
	if(a.indexOf("Session") <=0){
		document.getElementById('UploadForm').action += "&session=" + id;
	}
	return true;

}
function querystring(name){
	var qs = unescape(location.search);
	if(qs){
		qs = qs.substr(1,qs.length);
		var q = new Array();
		var q = qs.split("&");
		var querystring = new Array();
		for(var n=0;n < q.length;n++){
			  var querystring = q[n].split("=");
			  if(querystring[0].toLowerCase() == name.toLowerCase()){
				return querystring[1];
				break;
			  }
		 }
	 }
	 return '';
 }
//ajax stuff
function ajaxRequest(params){
	var url = params.url
	if(params.start !=null){
		params.start()
	}
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
		//xmlhttp.overrideMimeType("text/xml");
	}else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function(){ 
		//alert(xmlhttp.readyState)
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
			if(params.finish !=null){
				params.finish(xmlhttp)
			}
				
			}else if (xmlhttp.status == 500){
				//alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
			}else{
				//alert("There has been an error: \n" + xmlhttp.statusText + ' - ' + xmlhttp.status)
			}
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.setRequestHeader('content-type', 'text/xml'); 		
	xmlhttp.send(null);	
}
//ajax stuff
function window_onunload()
{
	// if progress doesn't exist, return
	if(UploadProgress == null){return;}
	
	// if progress doesn't expose closed method
	if(UploadProgress.closed){return}
	
	// close progress window
	UploadProgress.closeMe();
}
function addEvent( obj, type, fn ) { 
  if ( obj.attachEvent ) { 
    obj['e'+type+fn] = fn; 
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
    obj.attachEvent( 'on'+type, obj[type+fn] ); 
  } else 
    obj.addEventListener( type, fn, false ); 
} 
function removeEvent( obj, type, fn ) { 
  if ( obj.detachEvent ) { 
    obj.detachEvent( 'on'+type, obj[type+fn] ); 
    obj[type+fn] = null; 
  } else 
    obj.removeEventListener( type, fn, false ); 
}
var linkCount = 0
function loader(){
	getFileImage()
	addEvent(document,'keyup',test)
	//makeExtLink(1)
	linkCount = getElementsByClass("quickkey",document.getElementById("files"),"a").length
}
var key = "xyz";
var keypressedCount = 0
var item = "folder"
var last =""
function makeExtLink(mode,classToChange,type){
	if(classToChange==null)classToChange="newwin"
	var el = getElementsByClass(classToChange,document,"a");
	for(var n=0;n < el.length;n++){
		if(mode==1){
			var newa=document.createElement("a")
			newa.setAttribute("href", el[n].href)
			newa.setAttribute("target", "_blank")
			newa.setAttribute("style", "text-decoration:none;vertical-align: baseline;position:relative;bottom: 0.4em;font-size:0.8em;")
			if(type==1){
				newa.innerHTML="&nbsp;N"
			}else{
				newa.innerHTML="&nbsp;<span style='bottom: 0.3em;'><img src='../images/nw.gif' /></span>"
			}
			el[n].parentNode.insertBefore(newa,el[n].nextSibling);
		}else{
			el[n].target="_blank"
		}
	}
}	

function test(e,inputObj){
	//alert(keypressedCount)
	if(e.target=="[object HTMLHtmlElement]"|| e.srcElement.type==null){
		if(key!=String.fromCharCode(e.keyCode).toLowerCase()){
			keypressedCount=0
		}
		if(keypressedCount==linkCount){
			keypressedCount=0
		}
		key = String.fromCharCode(e.keyCode).toLowerCase()
		for(keypressedCount;keypressedCount<linkCount;keypressedCount++){
			if(document.getElementById(key + keypressedCount)!=null){
				document.getElementById("files").scrollTop = getElementTop(key + keypressedCount)-30
				//document.getElementById(key + keypressedCount).style.backgroundColor='black';
				document.getElementById(key + keypressedCount).innerHTML='&nbsp;&deg;';
				if(last!=""){
					document.getElementById(last).style.backgroundColor='';
					document.getElementById(last).innerHTML='&nbsp;';
				}
				last=key + keypressedCount;
				keypressedCount++;
				break;
			}
		}
	}
}

function getElementTop(Elem) {
	if(document.getElementById) {	
		var elem = document.getElementById(Elem);
	} else if (document.all) {
		var elem = document.all[Elem];
	}
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;

}
// assign event handler to unload event
window.onload = loader
window.onunload = window_onunload;

