/*
	File js relativo a:
		sorgenti.big-bug.net
		howto.big-bug.net
		big-bug.net
	funzioni per commentare, votare.
	situato in /comuni/js.js
*/
function Scurisci(id){
	var style = document.getElementById('body').style.background;
	alert(style);
}

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}
function basename(path, suffix) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ash Searle (http://hexmen.com/blog/)
    // +   improved by: Lincoln Ramsay
    // *     example 1: basename('/www/site/home.htm', '.htm');
    // *     returns 1: 'home'
 
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}
function OnLeft(){
	for(i=1;i<=5;i++){
		img = document.getElementById("stella"+i);
		img.src = "http://www.big-bug.net/images/off.gif";
	}
}
function Switch(id){
		for(i=1;i<=id;i++){
			img = document.getElementById("stella"+i);
			img.src = "http://www.big-bug.net/images/over.gif";
	}
}
function Commenta(id,tabella,nome,email,sitoweb,commento){
	var TxtNome,TxtEmail,TxtSitoweb,TxtCommento;
	TxtNome = document.getElementById(nome).value;
	TxtEmail = document.getElementById(email).value;
	TxtSitoweb = document.getElementById(sitoweb).value;
	TxtCommento = document.getElementById(commento).value;
	Button = document.getElementById('IdButton');
	Button.disabled=true;
	var ajax = new assegnaXMLHttpRequest(); 
	if(ajax){
		try {
				ajax.open("POST", "/comuni/action.php", true);  
      	       	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
     	       	ajax.send("id="+id+"&type="+tabella+"&nome="+TxtNome+"&email="+TxtEmail+"&sitoweb="+TxtSitoweb+"&commento="+TxtCommento);
     	       	ajax.onreadystatechange = function(){
     	       		if (ajax.readyState == 4) { //Richiesta Ok
   						if (ajax.status == 200) { 
   							stringa = ajax.responseText.replace(/^\s*/,"").replace(/\s*$/,"").replace(/\s+/g," ");
 								switch(stringa){
 									case "0":
 									alert("Grazie per aver commentato!");
 									Button.value="Commento Inserito.";
 									break;
 									
 									case "-1":
 									alert("Email non valida.");
 									Button.disabled=false;
 									break;
 									
 									case "-2":
 									alert("Commento non inserito, contatta il webmaster k8@big-bug.net");
 									Button.disabled=false;
 									break;
 									
 									
 									case "-3":
 									alert("Id commento inesistente, voto non valido.");
 									Button.disabled=true;
 									break;
 									
 									case "-4":
 									alert("Non puoi commentare due volte di seguito.");
 									Button.disabled=false;
 									break; 									
 									
 									case "-5":
 									alert("I dati inviati non sono validi.");
 									Button.disabled=true;
 									break;
 									
 								
 								}
 								
 							}
 							
 						}
 					}
		} catch(e){
			alert("Errore"+e);
		}
	}
}

function Vota(voto,tabella,id){

	var ajax = new assegnaXMLHttpRequest();
	if(tabella == "bg_sorgenti"){
	var xhtmlhashing = "voto_sorgente";
	}else if(tabella == "bg_howto"){
		xhtmlhashing = "voto_howto";
	}
	
	var load = document.getElementById('Hide');
	if(ajax){
		try {
				load.innerHTML = "<img src=\"http://www.big-bug.net/images/load.gif\" alt=\"Load..\">";
				ajax.open("POST", "/comuni/action.php", true);  
      	       	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
     	       	ajax.send("id="+id+"&xhtmlhashing="+xhtmlhashing+"&voto="+voto+"&type="+tabella);
     	       	ajax.onreadystatechange = function(){     	       	
     	       		if (ajax.readyState == 4) { //Richiesta Okv
   						if (ajax.status == 200) {
   						//	alert(ajax.responseText);
   							stringa = ajax.responseText.replace(/^\s*/,"").replace(/\s*$/,"").replace(/\s+/g," ");
   							
 							switch(stringa){
 								case "1":
 								alert("Il tuo voto è stato salvato!");
 								load.innerHTML = "Grazie per aver Votato!";
 								break;
 								
 								case "2":
 								alert("Puoi votare una sola volta al giorno!");
 								load.innerHTML = "Puoi votare una volta al giorno con lo stesso Ip."
 								break;
 								
 								
 								case "3":
 								alert("Voto non valido!");
 								load.innerHTML = "Il tuo voto risulta non valido."
 								break;
 								

 							}
 							
 						}
 					}
     	       		
     	       }
		} catch(e){
			alert("Errore"+e);
		}
	}

}



function assegnaXMLHttpRequest() {
	var
		XHR = null,
		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;
};
