
var xmlhttp
var TransationActive = false
var baseURL = "http://dnax.netsons.org/"

/*var XHRequest = {
	loadXMLDoc :  function(url, callback)
{
// codice per Mozilla, etc.
if (window.XMLHttpRequest)
  {
  this= new XMLHttpRequest
  this.onreadystatechange= callback
  this.open("GET",url,true)
  this.send(null)
  }
// codice per  IExplore
else if (window.ActiveXObject)
  {
  this= ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange=function(callback)
    xmlhttp.open("GET",url,true)
    xmlhttp.send()
    }
  }
}
}*/

function delete_row(id)
{
	item_row = document.getElementById(id);
	Fat.fade_element(item_row.id,null,700,'#f9dc23');
	var func = function() { item_row.style.display = "none"; recolorList();}
	setTimeout(func, 705);
}

var tempId
var errTitle
var errDesc


function ajax_responseParse(xmldata)
{
	if(xmldata.getElementsByTagName('error').length)
	{
		errTitle = xmldata.getElementsByTagName('title')[0].firstChild.nodeValue
		errDesc = xmldata.getElementsByTagName('description')[0].firstChild.nodeValue
		
		Error_elem = document.getElementById("error-box");
		if(Error_elem.childNodes.length>0) Error_elem.removeChild(Error_elem.firstChild);
		Error_elem.style.opacity = 0
		Error_elem.setAttribute("class","msg_box_error");
		Error_elem.innerHTML = "<div><p><strong>" + errTitle + "</strong></p><p>" + errDesc + "</p></div>" 
		AlphaFx.fade_element("error-box",25, 500, 0, 10)
		return false
	}
	else
	{
		var d=document.getElementById("error-box");
		if(d.childNodes.length>0) d.removeChild(d.firstChild);
		return true
	}
}

function ajax_delete(what, id)
{
	
	if(xmlhttp && xmlhttp.readyState)
	{
	  
	  if (xmlhttp.readyState==4)
	  {	
		// if "OK"
		if (xmlhttp.status==200)
		{
			if(ajax_responseParse(xmlhttp.responseXML)){
				delete_row(tempId)
			}
			xmlhttp = false
			return false
		}
		else
		{
			alert("Impossibile ricevere i dati " + xmlhttp.status)
			return true
		}
	  }
	  //alert("Request state: " + xmlhttp.readyState)
	  return false
	}
	else 
	{		
		tempId = id
		url = baseURL + "admin_ajax-server.php?action=delete-" + what + "&id=" + id + "&sid=" + Math.random()
		xmlhttp = new XMLHttpRequest()
		xmlhttp.onreadystatechange=ajax_delete
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
		//alert("richiesta inviata")
		return false;
	}
	
}

function ajax_preview()
{	
	if(xmlhttp && xmlhttp.readyState)
	{	  
	  if (xmlhttp.readyState==4)
	  {	
		if (xmlhttp.status==200)
		{
			previewObj = document.getElementById("preview");
			previewObj.innerHTML = xmlhttp.responseText
			window.location = "#preview"
			xmlhttp = false
			return false
		}
		else
		{
			alert("Impossibile ricevere i dati " + xmlhttp.status)
			return true
		}
	  }
	  return false
	}
	else 
	{		
		url = baseURL + "admin_ajax-server.php?action=preview&sid=" + Math.random()
		xmlhttp = new XMLHttpRequest()
		xmlhttp.onreadystatechange=ajax_preview
		xmlhttp.open("POST",url,true);
		post_data = "title=" + encodeURIComponent(document.msg_insert.title.value) + "&cat=" + encodeURIComponent(document.msg_insert.cat.value) + "&text=" + encodeURIComponent(document.msg_insert.text.value);		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
		xmlhttp.setRequestHeader("Content-length", post_data.length);  
        	xmlhttp.setRequestHeader("Connection", "close");  
		xmlhttp.send(post_data);		return false;
	}
}

function ajax_login()
{
	
	if(xmlhttp && xmlhttp.readyState)
	{
	  
	  if (xmlhttp.readyState==4)
	  {	
		// if "OK"
		if (xmlhttp.status==200)
		{
			if(ajax_responseParse(xmlhttp.responseXML)){
				login_fx(xmlhttp.responseXML)
			}
			else enable_form()
			xmlhttp = false
			return false
		}
		else
		{
			alert("Impossibile ricevere i dati " + xmlhttp.status)
			return true
		}
	  }
	  return false
	}
	else 
	{		
		url = baseURL + "admin_ajax-server.php?action=login&sid=" + Math.random()
		xmlhttp = new XMLHttpRequest()
		xmlhttp.onreadystatechange=ajax_login
		xmlhttp.open("POST",url,true)
		post_data = "login_user=" + encodeURIComponent(document.login_form.login_user.value) + "&login_passw=" + encodeURIComponent(document.login_form.login_passw.value)
		if(document.login_form.refer.value != '') post_data = post_data + "&refer=" + encodeURIComponent(document.login_form.refer.value)
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
		xmlhttp.setRequestHeader("Content-length", post_data.length);  
        xmlhttp.setRequestHeader("Connection", "close");  
		xmlhttp.send(post_data)
		//alert("richiesta inviata")
		return false;
	}
	
}

function login_fx(xmldata)
{
	login_obj = document.getElementById("login");
	AlphaFx.fade_element("login",25, 250, 10, 0)
	var func = function() { login_obj.style.display = "none" }
	setTimeout(func, 250);
		
	var func = function() { Error_elem = document.getElementById("error-box");
	if(Error_elem.childNodes.length>0) Error_elem.removeChild(Error_elem.firstChild);
	Error_elem.style.opacity = 0.00
	Error_elem.setAttribute("class","msg_box_success");
	Error_elem.innerHTML = "<div><p><strong>Loggato con successo</strong></p><p>Benvenuto " + document.login_form.login_user.value + "!</p></div>" 
	AlphaFx.fade_element("error-box",25, 500, 0, 10) }
		
	setTimeout(func, 500);
	if(xmldata.getElementsByTagName('refer').length) redir_url = xmldata.getElementsByTagName('refer')[0].firstChild.nodeValue
	else redir_url = baseURL
	
	var func = function() { document.location.href = redir_url }
	
	setTimeout(func, 3000);
	
}

function delete_item(what, id, message, link)
{
	
	if( confirm(message) ){
		link.href += '&is_js_confirmed=1';
		return ajax_delete( what, id );
	}
	else return false;

}

var alt = "alt"
var altOffset = 0

function recolorList() {
		theList = document.getElementById("the-list");
		//if ( !alt )
		//	return;
		var alt = "alt";
		var offset = 0;
		var listItems = theList.getElementsByTagName("tr");
		var n = 1
		for(i=0; i < listItems.length; i++){
			Elem = listItems[i];
			if(Elem.style.display != "none")
			{
			  if ( n % 2 ){
				Elem.className = "";
			  }
			  else
				Elem.className = alt;
			  n++;	
			}
		}
}

