// DEFINE HTTP OBJECT

function getHTTPObject() {
  var xmlhttp;

  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
	}
  	return xmlhttp;
}

// handle requests for login and logout
var httplog = getHTTPObject();

// handle requests for content area
var httpcnt = getHTTPObject();

// handle requests for sidebar area
var httpsbr = getHTTPObject();

//_____________________________________________________________________<INITIALIZE>_________________________________________________________________

function initialize() {
	logstatus();
	request_info('bun-venit');
	document.getElementById("content").style.height = (window.innerHeight-106)+'px';
	document.getElementById("actiuni").style.top = (window.innerHeight-70)+'px';
	document.getElementById("actiuni").style.width = (window.innerWidth-512)+'px';
}

function debug() {
	var obj = document.getElementById("debug");

	obj.style.position = 'absolute';
	obj.style.left = window.pageXOffset+10+'px';
	obj.style.top = window.pageYOffset+10+'px';
	obj.style.display="block";
	obj.style.zIndex = "2";
}

//_____________________________________________________________________</INITIALIZE>________________________________________________________________

//_____________________________________________________________________<LOGIN SYSTEM>_______________________________________________________________

function register(fName, page) {
	processing();

	var url = "/content/?"+page+"&token="+Math.random();

	theForm = document.getElementById(fName);
	postVars = '';

	for (i=0; i < theForm.elements.length; i++) {
		ele = theForm.elements[i];
		postVars += ele.name + '=' + ele.value + '&'
	}

	httplog.open('POST', url);
    httplog.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httplog.send(postVars);
	httplog.onreadystatechange = function() {

		if (httplog.readyState == 4) {
			if (httplog.status==200) {
				if (httplog.responseText=='err1') {
					register_status('denied.gif', 'Eroare: Doar spatii!');
				} else if (httplog.responseText=='err2') {
					register_status('denied.gif', '<span style="font-size: 15px;">Prea putine caractere in username (min. 5)!</span>');
				} else if (httplog.responseText=='err3') {
					register_status('denied.gif', '<span style="font-size: 15px;">Prea putine caractere in parola (min. 6)!</span>');
				} else if (httplog.responseText=='err4') {
					register_status('denied.gif', '<span style="font-size: 15px;">Cod activare invalid!</span>');
				} else if (httplog.responseText=='err5') {
					register_status('denied.gif', '<span style="font-size: 15px;">Nume utilizator indisponibil!</span>');
				} else {
					register_status('ok.png', 'Cont creat cu succes!');
					setTimeout("req_files()", 1000);
				}
			}
		}

	}
}

function display_login() {

	document.getElementById("interval").style.visibility='hidden';
	document.getElementById("filtru").style.visibility='hidden';
	document.getElementById("actiuni").style.visibility='hidden';
	document.getElementById("publicitate").style.display='block';

	document.getElementById('content').innerHTML = '<div align="center"><form id="fLogin" method="post" enctype="multipart/form-data" name="fPremium" target="_self"><table border="0" cellspacing="0" cellpadding="5" class="input">     <tr><td align="right" class="denumire_input">Utilizator<\/td><td><input id="pse" name="pse" type="text" value="" \/><\/td><\/tr><tr><td align="right" class="denumire_input">Parola<\/td><td><input name="pss" type="password" value="" \/><\/td><\/tr><tr><td align="right" class="denumire_input">Cod validare<\/td><td><input name="captcha" type="text" value="" \/><\/td><\/tr><tr><td align="right" class="denumire_input"><\/td><td><img id="captcha_img" src="plugins\/captcha\/acl_captcha.php" alt="cod validare" onMouseOver="this.style.cursor = \'pointer\'" onClick="javascript:this.src += \'?\'" title="cod validare" \/><\/td><\/tr><tr><td align="left"><input type="button" value="Cont nou" onclick="request_info(\'bun-venit\')" \/><\/td><td align="right"><input type="button" value="Login" onclick="do_login(\'fLogin\'); blur();" \/><\/td><\/tr><\/table><\/td><\/tr>\n<\/table></form></div>';
	document.getElementById('captcha_img').src = 'plugins\/captcha\/acl_captcha.php?token='+Math.random();
}

function logstatus(status) {
	var url = "/content/?utilizator&token="+Math.random();
	obj = document.getElementById('logstatus');

	httpsbr.open("GET", url , true);
	httpsbr.onreadystatechange = function() {
		if (httpsbr.readyState == 4) {
			if (httpsbr.status==200) { 
				var utilizator = httpsbr.responseText;
				if (utilizator!='not_logged_in') {
					obj.innerHTML = '<a onclick="do_logout()">'+utilizator+' | logout</a>';
					request_info('fisiere');
				} else {
					obj.innerHTML = '<a onclick="display_login()">login</a>';
				}
			}
		} 
	}
	httpsbr.send(null);
}

function do_login(fName) {
	processing();

	var url = "/log-system/?login&token="+Math.random();

	theForm = document.getElementById(fName);
	postVars = '';

	for (i=0; i < theForm.elements.length; i++) {
		ele = theForm.elements[i];
		postVars += ele.name + '=' + ele.value + '&'
	}

	httplog.open('POST', url);
    httplog.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httplog.send(postVars);
	httplog.onreadystatechange = function() {
		logstatus();
		//server_status(httplog, 'fisiere');
		window.location="/";
	}
}

function do_logout() {
	var url = "/log-system/?logout&token="+Math.random();

	httplog.open("GET", url , true);
	httplog.onreadystatechange = function() {
		logstatus();
		server_status(httplog, 'fisiere');
	}
	httplog.send(null);
}

function login_error(error) {

document.getElementById("popup").innerHTML = '<table border="0"><tr><td width="320" align="left" valign="top"><table border="0"><tr><td valign="middle"><img src="images/denied.gif" /></td><td valign="middle" style="font-size: 16px; font-weight: bold">'+error+'</td></tr></table></td><td align="right" valign="top"><img src="images/close.jpg" onclick="hide_status()" /></td></tr></table>';
document.getElementById("popup_obj").style.display='block';

}


//_____________________________________________________________________</LOGIN SYSTEM>______________________________________________________________

//_____________________________________________________________________<STATUS>_____________________________________________________________________

function processing() {
	document.getElementById("popup").innerHTML = '<img src="images/sloading.gif" alt="loading..." />';
	document.getElementById("popup_obj").style.display='block';
}

function show_status(status) {

document.getElementById("popup").innerHTML = '<table border="0"><tr><td width="320" align="left" valign="middle" style="font-size: 18px;">'+status+'</td><td align="right" valign="top"><img src="images/close.jpg" onclick="hide_status()" /></td></tr></table>';
document.getElementById("popup_obj").style.display='block';

}

function register_status(img, status) {

document.getElementById("popup").innerHTML = '<table border="0"><tr><td width="320" align="left" valign="middle" style="font-size: 18px;"><table border="0"><tr><td valign="middle"><img src="images/'+img+'" /></td><td valign="middle">'+status+'</td></tr></table></td><td align="right" valign="top"><img src="images/close.jpg" onclick="hide_status()" /></td></tr></table>';
document.getElementById("popup_obj").style.display='block';

}

function server_status(httpobj, page) {
        if (httpobj.readyState == 4) {
              if(httpobj.status==200) {
              var results=httpobj.responseText;

				  if (results!="err1"&&results!="err2") {
					hide_status();
					if (page!=null) {request_info(page);}
				  } else {
					if (results=="err1") {var error = "User/Pass incorecte!";} else if (results=="err2") {var error = "Cod validare incorect!";}
					login_error(error);
				  }
			  }
        }
}

function hide_status() {
	document.getElementById("popup").style.width = '300px';
	document.getElementById("popup_obj").style.display='none';
	document.getElementById("popup").style.background = "#FFF";
}

//_____________________________________________________________________</STATUS>____________________________________________________________________

//_____________________________________________________________________<REQUEST INFO>_______________________________________________________________

function request_info(page) {

	var url = "/content/?"+page+"&token="+Math.random();

	/*
	var headID = document.getElementsByTagName("head")[0];
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = 'style/'+page+'.css';
	cssNode.media = 'screen';
	headID.appendChild(cssNode);
	*/

	httpcnt.open("GET", url , true);

	httpcnt.onreadystatechange = function () {
		if (httpcnt.readyState < 4) {
		document.getElementById('content').innerHTML = '<div align="center"><img src="images/loading.gif" alt="loading..." /></div>'; }
		else {
			if (httpcnt.readyState == 4) {
				  if(httpcnt.status==200) {
				  var results=httpcnt.responseText;
				  if (results!="not_logged_in") {

					  document.getElementById('content').innerHTML = results;
					  hide_status();

					  if (page == 'fisiere') {
						  document.getElementById("publicitate").style.display='none';
						  document.getElementById("interval").style.visibility='visible';
						  document.getElementById("filtru").style.visibility='visible';
						  document.getElementById("actiuni").style.visibility='visible';
						  request_filtru();
					  } else {
						  document.getElementById("interval").style.visibility='hidden';
						  document.getElementById("filtru").style.visibility='hidden';
						  document.getElementById("actiuni").style.visibility='hidden';
						  document.getElementById("publicitate").style.display='block';
					  }

					  } else { display_login(); }
				  }
			}
		}
	};

	httpcnt.send(null);
}

function request_filtru() {
	var url = "/content/?filtru&token="+Math.random();
	httpsbr.open("GET", url , true);
	httpsbr.onreadystatechange = handle_filtru;
	httpsbr.send(null);
}


//_____________________________________________________________________</REQUEST INFO>______________________________________________________________

//_____________________________________________________________________<SEND INFO>__________________________________________________________________

function send_info(fName, page) {
	processing();

	var url = "/content/?"+page+"&token="+Math.random();

	theForm = document.getElementById(fName);
	postVars = '';

	for (i=0; i < theForm.elements.length; i++) {
		ele = theForm.elements[i];
		postVars += ele.name + '=' + ele.value + '&'
	}

	httpcnt.open('POST', url);
    httpcnt.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpcnt.send(postVars);
	httpcnt.onreadystatechange = function() {

		if (httpcnt.readyState < 4) {

		} else {
			if (page=='profil') {
				if (httpcnt.readyState == 4) {
					if (httpcnt.status==200) { show_status(httpcnt.responseText); setTimeout("req_profil()", 1000); }
				}
			}  else { server_status(httpcnt, page); }
		}
	}
}

function edit_labels(fName, label) {
	//processing();

	var url = "/content/?asign-label&token="+Math.random();

	theForm = document.getElementById(fName);

	postVars = '';

	for (i=0; i < theForm.elements.length; i++) {
		j = i+1;
		ele = theForm.elements[i];
		if (ele.type=='checkbox') {
			if (ele.checked) {postVars += j + '=' + ele.value + '&';}
		}
	}

	postVars += 'label=' + label;

	//alert(postVars);

	httpcnt.open('POST', url);
    httpcnt.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpcnt.send(postVars);
	httpcnt.onreadystatechange = function() {
		server_status(httpcnt, 'fisiere');
	}
}

function add_friend(label) {
	//processing();

	var url = "/content/?add-friend&token="+Math.random();

	postVars = 'adresa=' + label;

	//alert(postVars);

	httpcnt.open('POST', url);
    httpcnt.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpcnt.send(postVars);
	httpcnt.onreadystatechange = function() {
		server_status(httpcnt, 'fisiere');
	}
}

function mail_files(fName, adresa) {
	//processing();

	var url = "/content/?mail-files&token="+Math.random();

	theForm = document.getElementById(fName);

	postVars = '';

	for (i=0; i < theForm.elements.length; i++) {
		j = i+1;
		ele = theForm.elements[i];
		if (ele.type=='checkbox') {
			if (ele.checked) {postVars += j + '=' + ele.value + '&';}
		}
	}

	postVars += 'adresa=' + adresa;

	//alert(postVars);

	httpcnt.open('POST', url);
    httpcnt.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpcnt.send(postVars);
	httpcnt.onreadystatechange = function() {
		server_status(httpcnt, 'fisiere');
	}
}

function edit_comms(fName, comm) {
	//processing();

	var url = "/content/?comment&token="+Math.random();

	theForm = document.getElementById(fName);

	postVars = '';

	for (i=0; i < theForm.elements.length; i++) {
		j = i+1;
		ele = theForm.elements[i];
		if (ele.type=='checkbox') {
			if (ele.checked) {postVars += j + '=' + ele.value + '&';}
		}
	}

	postVars += 'comm=' + comm;

	//alert(postVars);

	httpcnt.open('POST', url);
    httpcnt.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpcnt.send(postVars);
	httpcnt.onreadystatechange = function() {
		server_status(httpcnt, 'fisiere');
	}
}

function delete_files(fName) {
	//processing();

	theForm = document.getElementById(fName);

	if (theForm != null) {

		var url = "/content/?delete&token="+Math.random();
		var postVars = '';

		for (i=0; i < theForm.elements.length; i++) {
			j = i+1;
			ele = theForm.elements[i];
			if (ele.type=='checkbox') {
				if (ele.checked) {postVars += j + '=' + ele.value + '&';}
			}
		}

		//alert(postVars);
		if (postVars!='') {

		httpcnt.open('POST', url);
		httpcnt.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		httpcnt.send(postVars);
		httpcnt.onreadystatechange = function() {
				if (httpcnt.readyState == 4) {
					if (httpcnt.status==200) { show_status(httpcnt.responseText); setTimeout("req_files()", 1000); }
				}
		}
		} else {show_status('Niciun fisier selectat!');}

	} else {show_status('Niciun fisier disponibil!');}
}

function set_filtru(name, value) {
	//processing();

	var url = "/content/?set-filtru&token="+Math.random();

	var postVars = name+'='+value;
	//alert(postVars);

	httpsbr.open('POST', url);
    httpsbr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    httpsbr.send(postVars);
	httpsbr.onreadystatechange = function() {
		server_status(httpsbr, 'fisiere');
	}
}

//_____________________________________________________________________</SEND INFO>_________________________________________________________________

//_____________________________________________________________________<HANDLE INFO>________________________________________________________________

function handle_filtru() {
	if (httpsbr.readyState < 4) {
		document.getElementById('filtru').innerHTML = '<table style="width: 300px; height: 300px;"><tr><td align="center" valign="middle"><img src="images/sloading.gif" alt="loading..." /></td></tr></table>';
	}
	else {
        if (httpsbr.readyState == 4) {
              if(httpsbr.status==200) {
              var results=httpsbr.responseText;
              document.getElementById('filtru').innerHTML = results;
              debug();
			  }
        }
	}
}

//_____________________________________________________________________</HANDLE INFO>_______________________________________________________________

//_____________________________________________________________________<INPUT>______________________________________________________________________

function label_input() {
	processing();

	theForm = document.getElementById('fFiles');

	if (theForm != null) {
        selItems = 0;

		for (i=0; i < theForm.elements.length; i++) {
			j = i+1;
			ele = theForm.elements[i];
			if (ele.type=='checkbox') {
				if (ele.checked) {selItems++;}
			}
		}

		if (selItems>0) {
			document.getElementById("popup").innerHTML = '<table border="0"><tr><td width="320">Eticheta:<br /><form><input id="label" name="label" type="text" value="" />&nbsp;<input type="button" value="OK" onclick="edit_labels(\'fFiles\', this.form.label.value)" /></form></td><td align="right" valign="top"><img src="images/close.jpg" onclick="hide_status()" /></td></tr></table>';
			document.getElementById("popup_obj").style.display='block';
			document.getElementById("label").focus();
		} else {
			show_status('Niciun fisier selectat!');
		}
	} else {
		show_status('Niciun fisier disponibil!');
	}
}

function mail_input(adrese) {
	processing();
	
	adresa=adrese.split(";");
	
	optiuni = '';
	
	for ( var i in adresa )
	{
		if (adresa[i]!='') {optiuni += '<option value="'+adresa[i]+'">'+adresa[i]+'</option>';} 
	}

	theForm = document.getElementById('fFiles');

	if (theForm != null) {
        selItems = 0;

		for (i=0; i < theForm.elements.length; i++) {
			j = i+1;
			ele = theForm.elements[i];
			if (ele.type=='checkbox') {
				if (ele.checked) {selItems++;}
			}
		}

		if (selItems>0) {
			document.getElementById("popup").innerHTML = '<form><table border="0"><tr><td width="320">E-mail destinatar:<br /><select id="label" style="font-size: 11px;"><option value=""></option>'+optiuni+'</select>&nbsp;<input type="button" value="OK" onclick="if (this.form.label.value!=\'\') {mail_files(\'fFiles\', this.form.label.value)} else {alert(\'E-mail invalid!\');}" /></td><td align="right" valign="top"><img src="images/close.jpg" onclick="hide_status()" /></td></tr><tr><td><input id="newfriend" name="newfriend" type="text" value="" />&nbsp;<input type="button" value="Adauga adresa" onclick="if (this.form.newfriend.value!=\'\') {add_friend(this.form.newfriend.value)} else {alert(\'E-mail invalid!\');}" /></td></tr><tr><td><span>* fisierele vor fi primite de pe adresa "fisiere@web-stick.ro". Adaugati aceasta adresa in lista de contacte.</span></td></tr></table></form>';
			document.getElementById("popup_obj").style.display='block';
			document.getElementById("label").focus();
		} else {
			show_status('Niciun fisier selectat!');
		}
	} else {
		show_status('Niciun fisier disponibil!');
	}
}

function comm_input(obj) {
	//processing();

	theForm = document.getElementById('fFiles');
	tObj = document.getElementById(obj);
	tValue = tObj.value;
	selItems = 0;

	for (i=0; i < theForm.elements.length; i++) {
		j = i+1;
		ele = theForm.elements[i];
		if (ele.type=='checkbox') {
			if (ele.checked) {selItems++;}
		}
	}

	if (selItems>0) {
		document.getElementById("popup").innerHTML = '<table border="0"><tr><td width="320">Comentariu:<form><table border="0"><tr><td><textarea id="comm" name="comm" style="font-size: 12px; width: 220px; height: 200px;">'+tValue+'</textarea></td><td><input type="button" value="OK" onclick="edit_comms(\'fFiles\', this.form.comm.value)" /></td></tr></table></form></td><td align="right" valign="top"><img src="images/close.jpg" onclick="hide_status()" /></td></tr></table>';
		document.getElementById("popup_obj").style.display='block';
		document.getElementById("comm").focus();
	} else {
		show_status('Niciun fisier selectat!');
	}

}

//_____________________________________________________________________</INPUT>_____________________________________________________________________

//_____________________________________________________________________<LOCAL ACTIONS>______________________________________________________________

function search(obj) {
	theObj = document.getElementById(obj);
	set_filtru('search', theObj.value)
}

function select(obj) {
	theForm = document.getElementById('fFiles');

	for (i=0; i < theForm.elements.length; i++) {
		j = i+1;
		ele = theForm.elements[i];
		if (ele.type=='checkbox') {
			if (ele.name != obj) {
			if (ele.checked) {ele.checked=0;}
			}
		}
	}

	theObj = document.getElementById(obj);
	if (theObj.checked) {theObj.checked=0;} else {theObj.checked=1;}
}

function select_all() {
	theForm = document.getElementById('fFiles');

	for (i=0; i < theForm.elements.length; i++) {
		ele = theForm.elements[i];
		if (ele.type=='checkbox') {
			ele.checked=1;
		}
	}
}

function deselect_all() {
	theForm = document.getElementById('fFiles');

	for (i=0; i < theForm.elements.length; i++) {
		ele = theForm.elements[i];
		if (ele.type=='checkbox') {
			ele.checked=0;
		}
	}
}

function preview_img(src) {
	document.getElementById("popup").style.background = "#FFF url('images/sloading.gif') no-repeat fixed center";
	document.getElementById("popup").innerHTML = '<div style="width: 100px; overflow: hidden"><img src="'+src+'" onclick="hide_status()" onload="resize_preview_img(this.width, \''+src+'\')" style="visibility:hidden" /></div>';
	document.getElementById("popup").style.width = '900px';
	document.getElementById("popup").style.zIndex = '1';
	document.getElementById("popup_obj").style.display='block';
}

function preview_pdf(src) {
	document.getElementById("popup").style.background = "#FFF url('images/sloading.gif') no-repeat fixed center";
	document.getElementById("popup").innerHTML = '<table><tr><td><embed src="'+src+'#toolbar=0&navpanes=0" width="900" height="550"></td><td valign="top"><div onclick="hide_status()" align="center" style="padding: 5px; font-size: 14px; background-color: #FFF; color: #FFF;"><img src="images/close.jpg" /></div></td></tr></table>';
	document.getElementById("popup").style.width = '900px';
	document.getElementById("popup").style.zIndex = '1';
	document.getElementById("popup_obj").style.display='block';
}

function preview_txt(src) {
	document.getElementById("popup").innerHTML = '<div onclick="hide_status()" align="right" style="padding-bottom: 5px; padding-right: 20px; font-size: 14px;"><img src="images/close.jpg" /></div><iframe src="http://www.web-stick.ro/content/?text&file='+src+'" width="900" height="550" frameborder="0"></iframe>';
	document.getElementById("popup").style.width = '900px';
	document.getElementById("popup").style.zIndex = '1';
	document.getElementById("popup_obj").style.display='block';
}

function resize_preview_img(size, src) {
	if (size>900) {document.getElementById("popup").innerHTML = '<img width="900" src="'+src+'" onclick="hide_status()" />';}
	else {document.getElementById("popup").innerHTML = '<img src="'+src+'" onclick="hide_status()" />';}
}

function req_profil() {
	request_info('profil');
}

function req_hp() {
	request_info('bun-venit');
}

function req_files() {
	request_info('fisiere');
	hide_status();
}

function req_upload() {
	document.getElementById('content').innerHTML = '<iframe width="600" height="600" scrolling="no" frameborder="0" src="plugins/uploader/upload/index.php"></iframe>';
	document.getElementById("interval").style.visibility='hidden';
	document.getElementById("filtru").style.visibility='hidden';
	document.getElementById("actiuni").style.visibility='hidden';
	document.getElementById("publicitate").style.display='block';
}

//_____________________________________________________________________</LOCAL ACTIONS>_____________________________________________________________

