
var xmlhttp = null;
var limit = 0;
var myOggetto;

/*-----------------------------
	Ajax Connection 
-----------------------------*/

function loadXMLDoc(url,loadXml){
	
	/*-----------------------------
		Code for Mozilla Safari
	-----------------------------*/
	
	if (window.XMLHttpRequest){
  		xmlhttp = new XMLHttpRequest()
  	}
	
	/*-----------------------------
		Code for IE
	-----------------------------*/
	
	else if (window.ActiveXObject){
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	if (xmlhttp != null){
		xmlhttp.onreadystatechange = eval(loadXml);
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
		return true;
 	}else{
  		return false;
  	}
}



/*----------------
	Ajax
----------------*/

function setAste(){
	
	var xmldoc = xmlhttp.responseXML;
	
   	
	/*-----------------------------
		Ready to Read XML
	-----------------------------*/
	
	if(xmlhttp.readyState == 4){
		
		var idOggetto = xmldoc.getElementsByTagName('idkey');
		var nomeOggetto = xmldoc.getElementsByTagName('nome');
		var valoreOggetto = xmldoc.getElementsByTagName('valore');
		var tipologiaAsta = xmldoc.getElementsByTagName('asta');
		var statoOggetto = xmldoc.getElementsByTagName('status');
		var inizioAsta = xmldoc.getElementsByTagName('inizio');
		var scadenzaAsta = xmldoc.getElementsByTagName('scadenza');
		var infoOggetto = xmldoc.getElementsByTagName('pinfo');
		var imageOggetto = xmldoc.getElementsByTagName('image');
		var imageFeed = xmldoc.getElementsByTagName('imageFeed');
		var feed = xmldoc.getElementsByTagName('feed');
		var utente = xmldoc.getElementsByTagName('utente');
		var prezzo = xmldoc.getElementsByTagName('prezzo');
		var iscritti = xmldoc.getElementsByTagName('iscritti');
		var newData = new Array();
		var flagTime = new Array();
		
		
		
		/*-----------------------------
			Inserisco i contenuti 
		-----------------------------*/
		contentBox = document.getElementById("text");
		prezzoText = '';
		
		for(var i = 0; i < nomeOggetto.length; i++){
			
			
			divAsta = document.createElement("div");
			divAsta.setAttribute((document.all ? 'className' : 'class'), 'asta');
			
			
			
			
			/*-----------------------------
				Creo Div per l'image 
			-----------------------------*/
			divImage = document.createElement("div");
			divImage.setAttribute((document.all ? 'className' : 'class'), 'imageAsta');
			img = document.createElement("img");
			
			
			
			/*---------------------------------
				Hack per Firefox e setImage
			---------------------------------*/
			img.setAttribute('src','/thumb/'+imageOggetto[i].firstChild.nodeValue);
			img.setAttribute('width','130');
			img.setAttribute('height','104');
			divImage.appendChild(img);
			
			
			
			/*-----------------------------
				Creo Div per testo 
			-----------------------------*/
			divText = document.createElement("div");
			divText.setAttribute((document.all ? 'className' : 'class'), 'contentAsta');
			
			divFeed = document.createElement("div");
			divFeed.setAttribute((document.all ? 'className' : 'class'), 'contentFeed'); 
			
			
			/*-----------------------------
				Aggiungo il nome e il link
			-----------------------------*/
			divTitle = document.createElement("div");
			divTitle.setAttribute((document.all ? 'className' : 'class'), 'nomeOggetto');
			title = document.createTextNode(nomeOggetto[i].firstChild.nodeValue);
		
			/* Link */
			aDettail = document.createElement("a");
			aDettail.setAttribute("href","/dettail/?id="+idOggetto[i].firstChild.nodeValue);
			aDettail.appendChild(title);			
			divTitle.appendChild(aDettail);
			
			
			
			/*-----------------------------
				Aggiungo la tipologia d'asta
			-----------------------------*/
			divTypeAsta = document.createElement("div");
			divTypeAsta.setAttribute((document.all ? 'className' : 'class'), 'typeAsta');
			asta = document.createTextNode(getAstaArray(tipologiaAsta[i].firstChild.nodeValue));
			
		
			/* Link */
			aLink = document.createElement("a");
			aLink.setAttribute("href","/comefunziona/#"+getAnchortext(getAstaArray(tipologiaAsta[i].firstChild.nodeValue)));
			aLink.appendChild(asta);
			divTypeAsta.appendChild(aLink);
			
			
			
			/*-----------------------------
				Append
			-----------------------------*/
			divText.appendChild(divTitle);
			divText.appendChild(divTypeAsta);
			divCommon = document.createElement("div");
			
			try{
				if(statoOggetto[i].firstChild.nodeValue == '1'){
					
					
					
					/*--------------------------- 
						Partecipa 
					----------------------------*/
					divCommon.setAttribute((document.all ? 'className' : 'class'), 'buttonPartecipa');
					dataArray = getDateArray(inizioAsta[i].firstChild.nodeValue);
					testo = document.createTextNode(dataArray[2]+"/"+dataArray[1]+"/"+dataArray[0]);
					divCommon.appendChild(testo);
					divText.appendChild(divCommon);
					
				}else if(statoOggetto[i].firstChild.nodeValue == '2'){
					
					
			
					/*--------------------------- 
						Dettaglio Asta 
					----------------------------*/
					divCommon.setAttribute((document.all ? 'className' : 'class'), 'buttonPartecipa');
					testo = document.createTextNode("Dettaglio Asta");
					button = document.createElement("a");
					button.setAttribute('href',"/dettail/?id="+idOggetto[i].firstChild.nodeValue);
					button.appendChild(testo);
					divCommon.appendChild(button);
					divText.appendChild(divCommon);
					try {
						
						if(!isNaN(parseInt(prezzo[i].firstChild.nodeValue))){
						
							prezzoText = ' Aggiudicato a soli ' + (parseInt(prezzo[i].firstChild.nodeValue))/100 + " &euro;";
						}else{
							prezzoText  = '<br />' + prezzo[i].firstChild.nodeValue;
						}
					}catch(e){}
					
					/*--------------------------- 
						Inserisco Feed 
					----------------------------*/
					
					try{
						feedText = document.createTextNode(feed[i].firstChild.nodeValue);
						divTitleFeed = document.createElement("div");
						divTitleFeed.setAttribute((document.all ? 'className' : 'class'), 'titoloFeed');
						titolo = document.createTextNode("Feedback");
						
						divUtente = document.createElement("div");
						divUtente.setAttribute((document.all ? 'className' : 'class'), 'utente'); 
						utenteText = document.createTextNode(utente[i].firstChild.nodeValue);
						divUtente.appendChild(utenteText);
						
						divTitleFeed.appendChild(titolo);
						divFeed.appendChild(divTitleFeed);
						divFeed.appendChild(feedText);
						divFeed.appendChild(divUtente);
					}
					catch(e){
						
					}
					
					
				}
				
			}catch(e){			
				
				
				/*-----------------------------
					Data per CountDown
				-----------------------------*/
				dataArray = getDateArray(scadenzaAsta[i].firstChild.nodeValue);
				
				if(dataArray[2] == '00')
					flagTime[i] = false;
				else
					flagTime[i] = true;
				
				year = parseFloat(dataArray[0]);
				month = parseFloat(dataArray[1]);
				day = parseFloat(dataArray[2]);
				
				
				newData[i] = new Date(year,month-1,day,22,0,0);
				
				
				/*-----------------------------
					Creo gg h m s
				-----------------------------*/	
				if(flagTime[i]) {
					divCommon.setAttribute((document.all ? 'className' : 'class'), 'data');
					testo = document.createTextNode("giorni ore min sec");
					divCommon.appendChild(testo);
					divText.appendChild(divCommon);
				}
				
				
				/*-----------------------------
					Creo Div per il tempo
				-----------------------------*/
				divData = document.createElement("div");
				divData.setAttribute("id","data"+i);
				divText.appendChild(divData);
				
				
				/*-----------------------------
					Creo il pulsante
				-----------------------------*/
				divButton = document.createElement("div");
				divButton.setAttribute((document.all ? 'className' : 'class'), 'buttonPartecipa');
				button = document.createElement("a");
				button.setAttribute('href',"/dettail/?id="+idOggetto[i].firstChild.nodeValue);
				testo = document.createTextNode("Partecipa");
				button.appendChild(testo);
				divButton.appendChild(button);
				divText.appendChild(divButton);
			}
			
			
			
			/*-----------------------------
				Creo il textAsta
			-----------------------------*/
			divTextAsta = document.createElement("div");
			divTextAsta.setAttribute((document.all ? 'className' : 'class'), 'textAsta');
			dataArray = getDateArray(scadenzaAsta[i].firstChild.nodeValue);
			
			
			
			
			/*-----------------------------------
				Creo il textAsta nel caso 2 
				e 3 indico il costo di isc
				altimenti il costo dell'asta
			-----------------------------------*/
			if(tipologiaAsta[i].firstChild.nodeValue == 2){
				
				divTextAsta.innerHTML = "Termine Asta: "+dataArray[2]+"/"+dataArray[1]+"/"+dataArray[0]+" h 22:00<br /> Valore: "+valoreOggetto[i].firstChild.nodeValue+" &euro;";
				divTextAsta.innerHTML += "<br /> Costo Iscrizione 20 &euro; per 50 offerte";
				
			
			}else if(tipologiaAsta[i].firstChild.nodeValue == 4){
				
				divTextAsta.innerHTML += "<br /> Costo Iscrizione: "+infoOggetto[i].firstChild.nodeValue+" pt Bonus";
				
			}else if(tipologiaAsta[i].firstChild.nodeValue == 6){
				
				if(dataArray[2] != '00')
					divTextAsta.innerHTML = "Termine Asta: "+dataArray[2]+"/"+dataArray[1]+"/"+dataArray[0]+" h 22:00<br /> Valore: "+valoreOggetto[i].firstChild.nodeValue+" &euro;";
				else
					divTextAsta.innerHTML = "Partenza Asta: Minimo "+iscritti[i].firstChild.nodeValue+" Iscrizioni<br />";
				
				divTextAsta.innerHTML += "Valore: "+valoreOggetto[i].firstChild.nodeValue+" &euro;"+"<br /> Costo Iscrizione "+infoOggetto[i].firstChild.nodeValue+" &euro;";
				
			}else{
				
				divTextAsta.innerHTML += " Costo P.Info: "+infoOggetto[i].firstChild.nodeValue+" &euro;";
			}
			
			divTextAsta.innerHTML += prezzoText;
			prezzoText = '';
			
			divText.appendChild(divTextAsta);
			
			/*-----------------------------
				AppendChild
			-----------------------------*/
			divAsta.appendChild(divImage);
			divAsta.appendChild(divFeed);
			divAsta.appendChild(divText);
			contentBox.appendChild(divAsta);
			
		
		}
		
		
		/*-----------------------------
			CountDown
		-----------------------------*/
		try{
			
			for( j = 0; j <= i; j++){
				
				if(flagTime[j])
					$('#data'+j).countdown({until: newData[j], layout: '<div class="num">%D%n%D</div><div class="num">%H%n%H</div><div class="num">%M%n%M</div><div class="num">%S%n%S</div>', callback: 'locRefresh()'});
			}
		
		}catch(e){
		
		}
		
	}
	
}



/*--------------
	Feedback
--------------*/

function feedBack(){ 
	
	var xmldoc = xmlhttp.responseXML; 
	
	
   	
	if(xmlhttp.readyState == 4){
		
		/*-----------------------------
			Ready to Read XML
		-----------------------------*/
		
		var nome = xmldoc.getElementsByTagName('nome');
		var testo = xmldoc.getElementsByTagName('testo'); 
		var utente = xmldoc.getElementsByTagName('utente'); 
	
	
	
		try{
			
			if(nome.length > 0){
	
				/*-----------------------------
					Inserisco i contenuti
				-----------------------------*/
		
				document.getElementById('titoloFeed').innerHTML = nome[limit].firstChild.nodeValue;
				document.getElementById('utenteFeed').innerHTML = utente[limit].firstChild.nodeValue;
				document.getElementById('testoFeed').innerHTML = testo[limit].firstChild.nodeValue;
				
			
			limit++;
			}
		
		}catch(e){
			
			/*
				Reinizializzo il limite
			*/
			
			clearTimeout();
			limit = 0;
		}
	}

}



/*-----------------------------
	Hack per Firefox
-----------------------------*/
function childElements(node) {
  var elements = new Array();
  
  for (var i=0; i < node.childNodes.length;  i++)  {
    
	if(node.childNodes[i].nodeType == 1) {
      elements.push(node.childNodes[i]);
    }
  }
  return elements;
}



/*-----------------------------
	Get
-----------------------------*/
function getDateArray(data){
	
	dataArray = data.split("-");
	return dataArray;
}

function getAnchortext(string){
	
	string = string.replace(' ','');
	return string.toLowerCase()
}

function getAstaArray(i){
	
	astaArray = new Array("Asta Classic","Asta Classic Tris","Asta Fifty Offers","Asta P.Info Variabile","Asta Bonus","Asta Bonus","Asta Special");
	return astaArray[i];
}

function loadAste(status){
	
	loadXMLDoc('/xml/data.php?status='+status,'setAste');
}

function loadFeed(){
	
	loadXMLDoc('/xml/feedback.php','feedBack')
	setTimeout("loadFeed()",5000);
}