// Editrice Belriguardo R 2011
// Cristiano Venturini

VTX = false;

function pagina_cat(id_cat)
	{
	var url = '/cat_libri.php?idcat=' + id_cat;
	document.location.href=url;
	}
function pagina_s_cat(id_cat,s_cat)
	{
	var url = '/cat_libri.php?idcat=' + id_cat + '&s_cat=' + s_cat;
	document.location.href=url;
	}	
	
function addCarrello()
	{
	var url = '/addCarrello.php?id_libro=' + IDLIBRO;	
//	alert(url);
	document.getElementById('carrello').src=url;	
	}
	
function rimuoviCarrello(id_libro)	
	{
	var url='/carrello_libri.php?r=' + id_libro;	
	document.location.href=url;	
	}
	
function viewCarrello(msg) { alert(msg); }	

function selz(o) { o.className='menu_20'; }
function pltx(o) { o.className='menu_10'; }

function plx_s_cat(o,id_cat)
	{
	//alert('pulli');	
	VTX = true;	
	o.className='menu_20';
	
//	var o = document.getElementById(otx);	
	var wtx = document.getElementById('sc_ptx');	
	
	var x = findPosX(o) + o.clientWidth;
//	var x = findPosX(o);
	var y = findPosY(o);
	
	var ot = '<table width="200" border="0" cellpadding="2" style="background-color:#F5A954; border: solid 1px #CCCCCC;">';
	
	if (!sk[id_cat]) { VTX = false; close_win(); return;  }
	
	//if (sk[id_cat].length < 1) { alert('polente'); VTX = false; close_win(); return; }
	//var lalli = sk[id_cat].length;
	//alert(lalli);
	
	var i;
	for (i in sk[id_cat])
		{
	//	ot += '<tr>';
	//	ot += '<td onmouseover="selz(this);" onmouseout="ptlx(this);">';
		ot += sk[id_cat][i];
	//	ot += '</td></tr>\n';	
		}
	ot += '</table>\n';
	//alert(ot);
	wtx.innerHTML = ot;
	wtx.style.width = '200px';
	wtx.style.height = 'auto';
	wtx.style.top = y + 'px';
	wtx.style.left = x + 'px';
	wtx.style.visibility = 'inherit';		
	}
 
function chiudi_scat(o)
	{
	o.className='menu_10';	
	VTX = false;	
	if (!VTX) { var t=setTimeout("close_win()",2000); }	
		
	}

function close_win()
	{
	if (!VTX) { var wtx = document.getElementById('sc_ptx');	wtx.style.visibility='hidden'; wtx.style.width='0px'; }		
		
	}
 
function openVtx() { VTX = true; }
function closeVtx() { VTX = false; var t=setTimeout("close_win()",500); }
 
function iscrizione_send()
	{
	var c = new Object();
	c.nome = document.getElementById("nome").value;
	c.indirizzo = document.getElementById("indirizzo").value;
	c.citta = document.getElementById("citta").value;
	c.telefono = document.getElementById("telefono").value;
	c.email = document.getElementById("emailz").value;
	if ( (c.nome == '') || (c.nome == 'undefined') || (c.nome == null) ) { alert('Errore: manca il nome'); return false; }
	if ( (c.indirizzo == '') || (c.indirizzo == 'undefined') || (c.indirizzo == null) ) { alert('Errore: manca indirizzo'); return false; }
	if ( (c.citta == '') || (c.citta == 'undefined') || (c.citta == null) ) { alert('Errore: manca citta'); return false; }
	if ( (c.email == '') || (c.email == 'undefined') || (c.email == null) ) { alert('Errore: manca la email'); return false; }
	return true;	 
	}
 
function get_mappa()
	{
	var url ='http://maps.google.it/maps?q=ferrara+via+delle+vecchie+24&hl=it&gl=it&z=16';
	var ptx = window.open(url,'mappa',"width=900,height=600,toolbar=false,menubar=false,status=false");
		
	}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

 
function ordinaLibro()
	{
	var c = new Object();
	c.nome = document.getElementById("nome").value;
	c.cognome = document.getElementById("cognome").value;
	c.indirizzo = document.getElementById("indirizzo").value;
	c.citta = document.getElementById("citta").value;
	c.cap = document.getElementById("cap").value;
	c.telefono = document.getElementById("telefono").value;
	c.email = document.getElementById("email").value;
	c.email2 = document.getElementById("email2").value;	
	c.pagamento = getCheckedValue(document.getElementById('pagamento'));
	if (c.pagamento == "") { c.pagamento = getCheckedValue(document.getElementById('pagamento2')); }
	
	//alert(c.pagamento);
	//return;

	if ( (c.nome == '') || (c.nome == 'undefined') || (c.nome == null) ) { alert('Errore: manca il nome'); return; }
	if ( (c.cognome == '') || (c.cognome == 'undefined') || (c.cognome == null) ) { alert('Errore: manca il cognome'); return; }
	if ( (c.indirizzo == '') || (c.indirizzo == 'undefined') || (c.indirizzo == null) ) { alert('Errore: manca indirizzo'); return; }
	if ( (c.citta == '') || (c.citta == 'undefined') || (c.citta == null) ) { alert('Errore: manca citta'); return; }
	if ( (c.cap == '') || (c.cap == 'undefined') || (c.cap == null) ) { alert('Errore: manca il CAP'); return; }
	if ( (c.email == '') || (c.email == 'undefined') || (c.email == null) ) { alert('Errore: manca la email'); return; }
	if ( (c.email2 == '') || (c.email2 == 'undefined') || (c.email2 == null) ) { alert('Errore: manca la conferma email'); return; }
	if ( c.email != c.email2 ) { alert('Errore: le email non coincidono'); return; }
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,5})$/;
	if(reg.test(c.email) == false) { alert('Indirizzo email non valido'); return; }

	
	
	//var url = '/ordina_libro.php';
	var url = '/conferma_ordine_carrello.php';
	var argz = '';
//	argz += 'idlibro=' + IDLIBRO;
	argz += 'nome=' + escape(encodeURI(c.nome));
	argz += '&cognome=' + escape(encodeURI(c.cognome));
	argz += '&indirizzo=' + escape(encodeURI(c.indirizzo));
	argz += '&citta=' + escape(encodeURI(c.citta));
	argz += '&cap=' + escape(encodeURI(c.cap));
	argz += '&telefono=' + escape(encodeURI(c.telefono));
	argz += '&email=' + escape(encodeURI(c.email));
	argz += '&pagamento=' + escape(encodeURI(c.pagamento));
	alert(argz);
	//return;
//	argz += '&username_insert=' + escape(encodeURI(document.getElementById("username_insert").value));
//	argz += '&copertina=' + escape(encodeURI(hpx.foto.img));
//	argz += '&foto=' + escape(encodeURI(hpx.foto.img));
//	argz += '&importanza=' + escape(encodeURI(importanza));

	//alert(argz);
	var Xreq = XRequestz();
	Xreq.onreadystatechange = function()
			{
			if (Xreq.readyState == 4) { if (Xreq.status == 200) {
			//xmlz = Xreq.responseText;
			var rtx = Xreq.responseText;
			if (rtx.substr(0,1) == "1") 
				{ 
				alert('Ordine eseguito. Verificare la email ' + c.email + ' per confermare l\'ordine'); 
				document.location.href='/catalogo.php'; 
				}
			else { alert('Errore: ' + rtx); }
			} }
			}
    Xreq.open('POST', url, true);
  	Xreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   	Xreq.setRequestHeader("Content-length", argz.length);
   	Xreq.setRequestHeader("Connection", "close");
   	Xreq.send(argz);
	}



 
function ordinaLibroSingolo()
	{
	var c = new Object();
	c.nome = document.getElementById("nome").value;
	c.cognome = document.getElementById("cognome").value;
	c.indirizzo = document.getElementById("indirizzo").value;
	c.citta = document.getElementById("citta").value;
	c.cap = document.getElementById("cap").value;
	c.telefono = document.getElementById("telefono").value;
	c.email = document.getElementById("email").value;
	c.email2 = document.getElementById("email2").value;	
	c.pagamento = getCheckedValue(document.getElementById('pagamento'));
	if (c.pagamento == "") { c.pagamento = getCheckedValue(document.getElementById('pagamento2')); }
	
	//alert(c.pagamento);
	//return;

	if ( (c.nome == '') || (c.nome == 'undefined') || (c.nome == null) ) { alert('Errore: manca il nome'); return; }
	if ( (c.cognome == '') || (c.cognome == 'undefined') || (c.cognome == null) ) { alert('Errore: manca il cognome'); return; }
	if ( (c.indirizzo == '') || (c.indirizzo == 'undefined') || (c.indirizzo == null) ) { alert('Errore: manca indirizzo'); return; }
	if ( (c.citta == '') || (c.citta == 'undefined') || (c.citta == null) ) { alert('Errore: manca citta'); return; }
	if ( (c.cap == '') || (c.cap == 'undefined') || (c.cap == null) ) { alert('Errore: manca il CAP'); return; }
	if ( (c.email == '') || (c.email == 'undefined') || (c.email == null) ) { alert('Errore: manca la email'); return; }
	if ( (c.email2 == '') || (c.email2 == 'undefined') || (c.email2 == null) ) { alert('Errore: manca la conferma email'); return; }
	if ( c.email != c.email2 ) { alert('Errore: le email non coincidono'); return; }
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,5})$/;
	if(reg.test(c.email) == false) { alert('Indirizzo email non valido'); return; }

	
	
	//var url = '/ordina_libro.php';
	var url = '/ordina_libro.php';
	var argz = '';
	argz += 'idlibro=' + IDLIBRO;
	argz += '&nome=' + escape(encodeURI(c.nome));
	argz += '&cognome=' + escape(encodeURI(c.cognome));
	argz += '&indirizzo=' + escape(encodeURI(c.indirizzo));
	argz += '&citta=' + escape(encodeURI(c.citta));
	argz += '&cap=' + escape(encodeURI(c.cap));
	argz += '&telefono=' + escape(encodeURI(c.telefono));
	argz += '&email=' + escape(encodeURI(c.email));
	argz += '&pagamento=' + escape(encodeURI(c.pagamento));
//	argz += '&username_insert=' + escape(encodeURI(document.getElementById("username_insert").value));
//	argz += '&copertina=' + escape(encodeURI(hpx.foto.img));
//	argz += '&foto=' + escape(encodeURI(hpx.foto.img));
//	argz += '&importanza=' + escape(encodeURI(importanza));

	//alert(argz);
	var Xreq = XRequestz();
	Xreq.onreadystatechange = function()
			{
			if (Xreq.readyState == 4) { if (Xreq.status == 200) {
			//xmlz = Xreq.responseText;
			var rtx = Xreq.responseText;
			if (rtx.substr(0,1) == "1") 
				{ 
				alert('Ordine eseguito. Verificare la email ' + c.email + ' per confermare l\'ordine'); 
				document.location.href='/catalogo.php'; 
				}
			else { alert('Errore: ' + rtx); }
			} }
			}
    Xreq.open('POST', url, true);
  	Xreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   	Xreq.setRequestHeader("Content-length", argz.length);
   	Xreq.setRequestHeader("Connection", "close");
   	Xreq.send(argz);
	}
	
function immagine_libro(imgx,w,h)
	{
	//alert(imgx + ' ' + w + ' ' + h);	
	var fotoz = document.getElementById('libro');
	//var plt_w = document.body.clientWidth;
	//var plt_h = document.body.clientHeight;
	var x = (document.body.clientWidth - w) / 2; 
	var y = (document.body.clientHeight - h) / 2; 
	
	var o = document.getElementById('img_ptx');
	if (o)
		{
		x = findPosX(o);
		y = findPosY(o);	
			
		}
	
	if (x < 5) { x = 10; }
	if (y < 5) { y = 10; }
	//wlx = window.innerWidth;
	//wly = window.offsetHeight;
	//var x = (wlx / 2) - (w / 2);
 	//var y = (wly / 2) - (h / 2); 	
	fotoz.innerHTML = '<img src="' + imgx + '" height="' + h + '" width="' + w + '" border="1">';
	fotoz.style.width = w + 'px';
	fotoz.style.height = h + 'px';	
	
	fotoz.style.top = y + 'px';
	fotoz.style.left = x + 'px';	
	fotoz.style.visibility = 'inherit';
	}

function plx_close(fotoz)
	{
//	var fotoz = document.getElementById('libro');	
	fotoz.style.width = '0px';

	fotoz.style.height = '0px';	

	fotoz.style.top = '0px';
	fotoz.style.left = '0px';	
	fotoz.style.visibility = 'hidden';	
	}	
	

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
	

	

