
window.onerror = function(msg, err_url, line) {alert('Errore sconosciuto! Stai calmo e riprova!');}

function getXMLHttpRequest() {
var XHR = null;
var  browserUtente = navigator.userAgent.toUpperCase();
 if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
  XHR = new XMLHttpRequest();
 else if(
  window.ActiveXObject &&
  browserUtente.indexOf("MSIE 4") < 0) {
  if(browserUtente.indexOf("MSIE 5") < 0)
   XHR = new ActiveXObject("Msxml2.XMLHTTP");
  else
   XHR = new ActiveXObject("Microsoft.XMLHTTP");
 }
 return XHR;
}


function getLink(indirizzo, stringaDati, divID)
{
	var XHrequest = getXMLHttpRequest(),
	noAJAX = true;
	
	if (XHrequest) {
				noAJAX = false;
				XHrequest.open("GET",indirizzo+'?'+stringaDati,true);
				XHrequest.onreadystatechange = function ()
				{
					CheckStatusOut(XHrequest,divID);
			  }
			  XHrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			  XHrequest.setRequestHeader("connection", "close");
			  XHrequest.send('');
		}
	return noAJAX;	
}	  



function postLink(indirizzo, stringaDati,divID,sid)
{
	var XHrequest = getXMLHttpRequest(),
	noAJAX = true;
	
	if (XHrequest) {
				noAJAX = false;
				XHrequest.open("POST",indirizzo,true);
				XHrequest.onreadystatechange = function ()
				{
					CheckStatusOut(XHrequest,divID,sid);
			  }
			  XHrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			  XHrequest.setRequestHeader("connection", "close");
			  XHrequest.send(stringaDati);
		}
	return noAJAX;	
}	  



function postForm(formName,indirizzo,sid) {
	var form = document.forms[formName];
	var stringaDati = "";
	for (var i=0; i<form.elements.length; i++) {
		
		if((form.elements[i].type=='checkbox' || form.elements[i].type=='radio') && !form.elements[i].checked){
			continue;
		} 

		stringaDati += "&" + encodeURIComponent(form.elements[i].name) + "=" + encodeURIComponent(form.elements[i].value);
	}
	return postLink(indirizzo, stringaDati,'output_msg',sid);
}



function WaitWith (divID)
{
	if (divID == 'output_msg')
	   document.getElementById(divID).innerHTML =  "\<img src\=\"\/img\/indicator\.gif\" alt\=\"indicator\.gif\" title\=\"indicator\.gif\" \/\>";
}





function CheckStatusOut(XHrequest,divID,sid)
{
	if(XHrequest.readyState == 1)
	{ WaitWith(divID); }
	
   //verificaTempoTrascorso = function(){};
  
	if(XHrequest.readyState == 4)
	{
		if (XHrequest.status == 200)
		  {
		    var output = XHrequest.responseText;
		    if (!output) location.replace("http://www.yougulp.it/signup/userreg.php");
		    else if (output.substr(0,2) == 'pe') location.replace("http://www.yougulp.it/account/");
		    else if (output.substr(0,2) == 'ed') location.replace("http://www.yougulp.it/account/files");
		    else if (output.substr(0,2) == 'up') 
		   	 {
		   	   cells = document.getElementsByTagName("nobr");
			 	   for(i=1; i < cells.length; i++) cells.item(i).style.color = '#000000';
				   // *********************************** PARTENZA UPLOAD *************************************************************///
		  	   beginUpload(sid);
		      }
		    else
		      { 
		  	   document.getElementById(divID).innerHTML = setErrorRed(output);
		  	  }
	    }
	  else document.getElementById(divID).innerHTML = "Errore di risposta dal server numero: "+XHrequest.status;
	}
	
}


function setErrorRed(output){
	var tables = 0;
	tables = output.split(',');	
	var cells;
	cells = document.getElementsByTagName("nobr");
	var lung =8;
  for(i=1; i < lung; i++){
    if (tables[i] == 0)
    {cells.item(i-1).style.color = '#000000';}
	  else
	  {cells.item(i-1).style.color = '#ff0000';}
	}
	return tables[0]; //}
}












