/**
 * @author francesco
 */
var pagina_redir = "";

function goToPage(p, f, idt, a)
{
	if (confirm("Vuoi davvero cancellare il topic?"))
		window.location = "/" + p + "?f=" + f + "&idt=" + idt + "&a=" + a;
}

function submitFilter(id)
{
	window.location = "/index.php?p=catalogo_auto&cat=" + parseInt(id);
}

function cambia_foto(foto, titolo)
{
	var new_foto = new Image();
	new_foto.src = foto;
	var w = new_foto.width;
	var h = new_foto.height;
	var DIM_MAX = 275;
	
	if (w > DIM_MAX || h > DIM_MAX)
	{
		if (w > h)
		{
			h = h * DIM_MAX / w;
			w = DIM_MAX;
		}
		else
		{
			w = w * DIM_MAX / h;
			h = DIM_MAX;
		}
	}
	
	document.getElementById("foto_principale").src = foto;
	document.getElementById("foto_principale").width = w;
	document.getElementById("foto_principale").height = h;
	document.getElementById("foto_principale").alt = titolo;
	document.getElementById("foto_principale").title = titolo;
	document.getElementById("foto_principale").onclick = function()
	{
		openPhoto(foto);
	}
}

function openPhoto(foto, titolo, larghezza, altezza )
{
	var new_foto = new Image();
	new_foto.src = foto;
	var w = new_foto.width;
	var h = new_foto.height;
	var DIM_MAX = 490;
	
	if (w > DIM_MAX || h > DIM_MAX)
	{
		if (w > h)
		{
			h = h * DIM_MAX / w;
			w = DIM_MAX;
		}
		else
		{
			w = w * DIM_MAX / h;
			h = DIM_MAX;
		}
	}
	
	var dimX = screen.width; //larghezza pagina
	var dimY = screen.height; //lunghezza pagina
	
	if (document.all) // IF IE
	{
		dimX = document.body.parentNode.scrollLeft + (dimX / 3);
		dimY = document.body.parentNode.scrollTop + 100;
	}
	else
	{
		dimX = window.scrollX + (dimX / 3); //larghezza pagina
		dimY = window.scrollY + 100; //lunghezza pagina
	}
	
	var wall = document.createElement("div");
	wall.style.position = "absolute";
	wall.style.top = dimY + "px";
	wall.style.left = dimX + "px";
	wall.style.width = "500px";
	wall.style.height = "400px";
	wall.style.backgroundColor = "#FFFFEE";
	wall.style.border = "1px solid black";
	
	var box_video = document.createElement("div");
	box_video.style.position = "relative";
	box_video.style.top = "5px";
	box_video.style.left = "5px";
	box_video.style.width = "495px";
	box_video.style.height = "370px";
	box_video.innerHTML = titolo + '<br />'+ '<br />';
	box_video.innerHTML += '<img src="' + foto + '" width="' + w + '" height="' + h + '" />';

	var link = document.createElement("a");
	link.style.position = "relative";
	link.style.top = "10px";
	link.style.left = "230px";
	link.innerHTML = "Chiudi";
	link.href = "javascript:;";
	link.onclick = function() 
	{ 
		document.body.removeChild(wall); 
	}
	
	wall.appendChild(box_video);
	wall.appendChild(link);
	document.body.appendChild(wall);
}

function checkForm(nome_form)
{
    var oForm = document.getElementById(nome_form);
    var field;
    
    for (var i = 0; i < oForm.elements.length; i++)
    {
        field = oForm.elements[i];

        switch (field.type)
        {
        	case "text" :
        	case "textarea" :
        	case "select-one" :
        	case "file" :

	            if (field.value.length <= 0)
	            {
		            alert("Il campo non puo' essere vuoto");
		            field.value = "";
					field.style.backgroundColor = "#f0f0f0";
					field.style.borderColor = "#ff0000";
					field.focus();
					return;
	            }
	            else if ((field.id == "coperti" || field.name == "coperti" || 
	            		field.id == "telefono" || field.name == "telefono" || 
	            		field.id == "cellulare" || field.name == "cellulare") &&
	            		isNaN(field.value))
	            {
	            	alert("Il campo puo' contenere solo numeri");
	                field.style.backgroundColor = "#f0f0f0";
					field.style.borderColor = "#ff0000";
	                field.value = "";
	                field.focus();
	                return;
	            }
	            else if ((field.id == "email" || field.name == "email") && (field.value.indexOf("@") <= 0 || field.value.indexOf("@") == field.value.length - 1))
	            {
	            	alert("E' necessario inserire una e-mail valida");
	                field.style.backgroundColor = "#f0f0f0";
					field.style.borderColor = "#ff0000";
	                field.value = "";
	                field.focus();
	                return;
	            }
	            else if ((field.id == 'giorno' || field.id == 'data' || field.name == 'giorno' || field.name == 'data'))
	            {
	            	var temp = field.value.split("/");
	            	if (temp[0].length != 2 || temp[1].length != 2 || temp[2].length != 4)
	            	{
	            		alert("La data deve essere in formato GG/MM/AAAA");
		                field.style.backgroundColor = "#f0f0f0";
						field.style.borderColor = "#ff0000";
		                field.value = "";
		                field.focus();
		                return;
	            	}
	            	else
	            	{
	            		field.style.backgroundColor = "";
		            	field.style.borderColor = "";
	            	}
	            }
	            else
	            {
	            	field.style.backgroundColor = "";
	            	field.style.borderColor = "";
	            }
	            
	            break;

            case "checkbox" :

				if (field.checked == false)
				{
					alert("Il campo deve essere selezionato");
					field.focus();
					return;
				}

				break;

            case "radio" :

				field = document.getElementsByName(field.name);

				var checked = false;
				for (var j in field)
					if (field[j].checked)
					{
						checked = true;
						break;
					}

				if (!checked)
				{
					alert("Deve essere selezionata almeno un'opzione");
					field[0].focus();
					return;
				}
            	
	            break;
        }
    }

    document.getElementById(nome_form).submit();
}

function checkFormLazio(nome_form) 
{
    var oForm = document.getElementById(nome_form);
    var field;
    for (var i = 0; i < oForm.elements.length; i++)
    {
        field = oForm.elements[i];
        /*alert (oForm.elements[i].type);*/
        
        switch (field.type)
        {
        /*case "select-one" :*/
    	/*case "file" :*/
        	case "textarea" :
        		if (field.value.length <= 0)
        		{
        			
		            alert("Il campo non puo' essere vuoto");
		            field.value = "";
					field.style.backgroundColor = "#f0f0f0";
					field.style.borderColor = "#ff0000";
					field.focus();
					return;
        		} else
	            {
	            	field.style.backgroundColor = "";
	            	field.style.borderColor = "";
	            }
	            
	            break;
        	case "text" :    
        		if (field.value.length <= 0)
	            {
        			if((field.id=="titolo_file" && document.send_form.new_file.value!= 0)||(field.id=="nickname"))
        			{
			            alert("Il campo non puo' essere vuoto");
			            field.value = "";
						field.style.backgroundColor = "#f0f0f0";
						field.style.borderColor = "#ff0000";
						field.focus();
					return;
        			}
	            }
	            else
	            {
	            	field.style.backgroundColor = "";
	            	field.style.borderColor = "";
	            }
	            
	            break;

            case "checkbox" :

				if (field.checked == false)
				{
					alert("Il campo deve essere selezionato");
					field.focus();
					return;
				}

				break;

            case "radio" :

				field = document.getElementsByName(field.name);

				var checked = false;
				for (var j in field)
					if (field[j].checked)
					{
						checked = true;
						break;
					}

				if (!checked)
				{
					alert("Deve essere selezionata almeno un'opzione");
					field[0].focus();
					return;
				}
            	
	            break;
        }
    }

    document.getElementById(nome_form).submit();
}

function swapHeader(pic)
{
	document.getElementById("header_foto").src = pic;
}

function Logout(page, red, div)
{
	this.t_page = page;
	this.target = div;
	this.redtarget = red;

	this.exec = function()
	{
		try
		{
			pagina_redir = this.t_page;
			document.getElementById(this.target).innerHTML = "Uscendo ...";
			makeRequest("include_user/php/" + this.redtarget, this.redir, "POST", "");
		} catch (e) { document.getElementById(this.target).innerHTML = e.message; }
	}

	this.redir = function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200) window.location.replace(pagina_redir);
			else
			{
				var contentHTML = "<b>Errore durante il trasferimento dati.</b>";
				contentHTML += "<br />Stato : " + xmlhttp.status;
				document.getElementById(user_target).innerHTML = contentHTML;
			}
		}
	}
}

function inviaRichiesta(value)
{
	makeRequest("include_html/mini_sito.php", redir2, "POST", "id=" + value);
}

var redir2 = function()
{
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 200)
		{
			getById("php_response").innerHTML = xmlhttp.responseText;
		}
		else
		{
			var contentHTML = "<b>Errore durante il trasferimento dati.</b>";
			contentHTML += "<br />Stato : " + xmlhttp.status;
			document.getElementById(user_target).innerHTML = contentHTML;
		}
	}
}

function handlePhp()
{
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 200) document.getElementById(user_target).innerHTML = xmlhttp.responseText;
		else
		{
			var contentHTML = "<b>Errore durante il trasferimento dati.</b>";
			contentHTML += "<br />Stato : " + xmlhttp.status;
			document.getElementById(user_target).innerHTML = contentHTML;
		}
	}
}

try { LLI.callBack("functions1_lib"); }
catch (e) {}