text = "";

function prenota(obj) {
	var c = false;
	var v = "";
	var temp = obj.split("_");
	var obj_mail = temp[0];
	
	// MSG ///////////////////
	msg_div = document.getElementById("msg_d");
	msg_div.innerHTML = "";
			msg_td = document.getElementById("msg_t");
	//////////////////////////

	text = "";
	var my_date = new Date();
	
	var month = my_date.getMonth().toString();
	if (month.length == 1) { month = "0"+month; }
	var year = my_date.getFullYear();
	var hours = my_date.getHours().toString();
	if (hours.length == 1) { hours = "0"+hours; }
	var minutes = my_date.getMinutes().toString();
	if (minutes.length == 1) { minutes = "0"+minutes; }
	var seconds = "3";//my_date.getSeconds().toString();
	if (seconds.length == 1) { seconds = "0"+seconds; }
	
	var date = my_date.getDate() + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + seconds;

	text = text + "PRENOTAZIONE ("+ date +"):\n\n";
	
	var ff = document.getElementsByTagName("input");
	for (i = 0; i < ff.length; i++) {
		///////////////////////////////////
		if (ff[i].name == "nome") {
			nome = ff[i].value;
			if (nome == "") {
				msg_td.style.display = "";
				msg_div.style.display = "";
				msg_div.innerHTML = msg_div.innerHTML + "Inserisci il Nome<br>";	
				
				ff[i].focus()
		 		ff[i].select()

				c = false;
				break;
			} else {
				text = text + "Nome:\t" + nome + "\n";
				c = true;
			}
		}
		///////////////////////////////////
		if (ff[i].name == "cognome") {
			cognome = ff[i].value;
			if (cognome == "") {
				msg_td.style.display = "";
				msg_div.style.display = "";
				msg_div.innerHTML = msg_div.innerHTML + "Inserisci il Cognome<br>";	
				
				ff[i].focus()
		 		ff[i].select()

				c = false;
				break;
			} else {
				text = text + "Cognome:\t" + cognome + "\n";
				c = true;
			}
		}
		///////////////////////////////////
		if (ff[i].name == "email") {
			email = ff[i].value;
				
			validemail = isValidEmail(email);
			if (!validemail) {
				msg_td.style.display = "";
				msg_div.style.display = "";
				msg_div.innerHTML = msg_div.innerHTML + "E-mail non valida<br>";	
				
				ff[i].focus()
		 		ff[i].select()
				
				c = false;
				break;
			}
			
			if (email == "") {
				msg_td.style.display = "";
				msg_div.style.display = "";
				msg_div.innerHTML = msg_div.innerHTML + "Inserisci l'E-mail<br>";	
				
				ff[i].focus()
		 		ff[i].select()

				c = false;
				break;
			} else {
				text = text + "E-mail:\t" + email + "\n";
				c = true;
			}
		}
		///////////////////////////////////
		if (ff[i].name == "telefono") {
			telefono = ff[i].value;
			if (telefono == "") {
				msg_td.style.display = "";
				msg_div.style.display = "";
				msg_div.innerHTML = msg_div.innerHTML + "Inserisci il Telefono<br>";	
				
				ff[i].focus()
		 		ff[i].select()

				c = false;
				break;
			} else {
				text = text + "Telefono:\t" + telefono + "\n";
				c = true;
			}
			
		}
	}
	
	text = text + "\n\n";
	text = text + "Articoli (Qta)" + "\n";
	text = text + "------------------------------------------------" + "\n";

	for (i = 0; i < ff.length; i++) {
		if (ff[i].name == "nome" || ff[i].name == "cognome" || ff[i].name == "email" || ff[i].name == "telefono") {
			//
		} else {
			
			if (i%2 == 0) {
				if (ff[i].checked) {
					text = text + ff[i].value + " ";
					v = v + ff[i].value;
				}
			} else {
				if (ff[i-1].checked) {
					text = text + "(qta: " + ff[i].value + ")\n";
				}
			}
			
		}
	}
	text = text + "------------------------------------------------" + "\n";

	if (v == "") {
		msg_td.style.display = "";
		msg_div.style.display = "";
		msg_div.innerHTML = msg_div.innerHTML + "Seleziona un prodotto<br>";
		c = false;
	}
	/////////////////////////////////////////////////////////////		
	ifr = document.getElementById("prenota_iframe");
	ifr_doc = ifr.contentWindow.document;
	ff1 = ifr_doc.getElementsByTagName("textarea");
	for (ii = 0; ii < ff1.length; ii++) {
		if (ff1[ii].name == "testo") {
			ff1[ii].value = text;
		}
	}
	/////////////////////////////////////////////////////////////		
	if (c) {
		msg_td.style.display = "none";
		msg_div.style.display = "none";
		confirm_pren(date,text);
	}
	/////////////////////////////////////////////////////////////		
//
}
//#################################################################
function back_confirm() {
	ff = document.getElementsByTagName("div");
	for (i = 0; i < ff.length; i++) {
		if (ff[i].id == "prodotti") {
			ff[i].style.display = "";
		}
		if (ff[i].id == "confirm_p") {
			ff[i].style.display = "none";
			ff[i].innerHTML = "";
		}
		if (ff[i].id == "thanx") {
			ff[i].style.display = "none";
			ff[i].innerHTML = "";
		}
	}
//
}
function confirm_pren(date,text) {
	ff = document.getElementsByTagName("div");
	for (i = 0; i < ff.length; i++) {
		if (ff[i].id == "prodotti") {
			ff[i].style.display = "none";
		}
		if (ff[i].id == "confirm_p") {
			ff[i].style.display = "";
			text_c = text;
			text_c = text_c.replace(/------------------------------------------------\n/g,"<hr class=prenota>");
			text_c = text_c.replace(/\n/g,"<br>");
			
			text_c = text_c + "<br><a id='conf_pren0' style=\"display:;\" class=prenota_btn href=\"javascript:back_confirm()\">Indietro</a>&nbsp;";
			text_c = text_c + "<a id='conf_pren' style=\"display:;\" class=prenota_btn href=\"javascript:subm_mailform('"+date+"')\">Conferma Prenotazione</a>";
			text_c = text_c + "<a id='conf_pren1' style=\"display:none;\" class=prenota_btn href=\"prenota.htm\">Nuova Prenotazione</a><br><br><br>";

			
			ff[i].innerHTML = text_c;
			text_c = "";
			
			///////////////////////////////////////////////////
			ff1 = document.getElementsByTagName("div");
			for (k = 0; k < ff1.length; k++) {
				if (ff1[k].id == "testo_print") {
					ff1[k].innerHTML = text;
				}
			}
			///////////////////////////////////////////////////	
			
		}
			
	}
//
}
//#################################################################
function subm_mailform(date) {
	
	ff = document.getElementsByTagName("a");
	for (i = 0; i < ff.length; i++) {
		if (ff[i].id == "conf_pren") {
			ff[i].style.display = "none";
		}
		if (ff[i].id == "conf_pren0") {
			ff[i].style.display = "none";
		}
		if (ff[i].id == "conf_pren1") {
			ff[i].style.display = "";
		}
	}
	/////////
			
				ifr = document.getElementById("prenota_iframe");
				ifr_doc = ifr.contentWindow.document;
				mailform = ifr_doc.getElementById("prenota_form");

				////////////////////
				ff1 = ifr_doc.getElementsByTagName("input");
				for (k = 0; k < ff1.length; k++) {
					
					if (ff1[k].name == "subject") {
						ff1[k].value = "Prenotazione ("+date+")";
					}
					
					if (ff1[k].name == "email_cliente") {
						ff2 = document.getElementsByTagName("input");
						for (j = 0; j < ff2.length; j++) {
							if (ff2[j].name == "email") {
								email_cli = ff2[j].value;
							}
						}
						
						ff1[k].value = email_cli;
					}
					
				}
				////////////////////


	mailform.action = "http://demos.invisibledesign.it/equipeolympique/email_prenotazione.php";
	//mailform.action = "email_prenotazione.php";

	ff = document.getElementsByTagName("div");
	for (i = 0; i < ff.length; i++) {
		if (ff[i].id == "thanx") {
			ff[i].style.display = "";
			ff[i].innerHTML = "La prenotazione &egrave; stata inoltrata.<br>Una copia &egrave; stata spedita all'indirizzo e-mail precedentemente inserito.<br>Verrete contattati il pi&ugrave; presto possibile.<br>Grazie e Buona Giornata<br><br>Equipe Olympique Staff<br><br><a href=\"javascript:print_c();\">stampa</a><br>";
			thanx_d = ff[i];

			//setTimeout('thanx_d.style.display = "none"', 5000);
			mailform.submit();
			
		}
	}
//
}



//#################################################################
//#################################################################
function print_c() {
	var txtprint = "<HTML><HEAD><TITLE>Equipe Olympique - Prenotazione</TITLE><SCRIPT SRC=\"o/js.js\"></SCRIPT></HEAD><BODY>";
	ff = document.getElementsByTagName("div");
	for (i = 0; i < ff.length; i++) {
		if (ff[i].id == "testo_print") {
			txtprint = txtprint + ff[i].innerHTML;
			txtprint1 = txtprint.replace(/------------------------------------------------\n/g,"<hr>");
			txtprint1 = txtprint1.replace(/------------------------------------------------/g,"<hr>");
			txtprint1 = txtprint1.replace(/\n/g,"<br>");
		}
	}
	txtprint = txtprint1 + "<br><br><div id=print_btn class=print_btn style=\"display:;\"><button onclick=\"printmenu(this);\">&nbsp;&nbsp;STAMPA&nbsp;&nbsp;</button>&nbsp;<button onclick=\"window.close();\">&nbsp;&nbsp;CHIUDI&nbsp;&nbsp;</button></div>";

	txtprint = txtprint + "</BODY></HTML>";
	
	mywindow = window.open("","printwin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=500,height=600,screenX=500,screenY=100");
	mywindow.document.write(txtprint);
}

function printmenu(obj) {
	btns = document.getElementById("print_btn");
	btns.style.display = "none";
	window.print();
	btns.style.display = "";
}



//#################################################################
function f(o) {
  o.value = o.value.replace(/([^0-9])/g,"");
}
//#################################################################
function isValidEmail(email) {
	if (! allValidChars(email)) {  // check to make sure all characters are valid
		return false;
	}
	
	if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
		return false;
	} else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
		return false;
	} else if (email.indexOf("@") == email.length) {  // @ must not be the last character
		return false;
	} else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		return false;
	} else if (email.indexOf(".") == email.length) {  // . must not be the last character
		return false;
	}
	
	return true;
}

function allValidChars(email) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < email.length; i++) {
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
			continue;
		parsed = false;
		break;
	}
	return parsed;
}



//#################################################################
function changeqta(o,oq,iq,q,prezzo) {
	p = "prezzo_"+iq;
	var prezzo_div = document.getElementById(p);
	
	var ff = document.getElementsByTagName("input");
	for (i = 0; i < ff.length; i++) {
		if (ff[i].name == oq) {
			if (!o.checked) {
				ff[i].value = "";
			} else
			if (ff[i].value == "" && o.checked) {
				ff[i].value = "1";
				
				prezzo_div.innerHTML = "";
				
				var sub_tot = prezzo;
				prezzo_div.innerHTML = sub_tot + " &euro;";
				
			} else
			if (ff[i].value != "" && ff[i].value > q*1) {
				ff[i].value = q;
			}
		}
	}
	
	maketotal();
//
}
function changeqta1(o,i,prezzo,qta) {
	q = o.value;
	if (o.value != "" && o.value > qta*1) {
		o.value = qta;
		q = qta;
	}
	
	//////	
	p = "prezzo_"+i;
	prezzo_div = document.getElementById(p);
	prezzo_div.innerHTML = "";
	
	var sub_tot = q * prezzo;
	if (sub_tot == 0) {
		prezzo_div.innerHTML = "";
	} else {
		prezzo_div.innerHTML = sub_tot + " &euro;";
	}
	////////
	a = i - 2;
	a_checkbox = "articolo_"+a;
	var ff = document.getElementsByTagName("input");
	for (i = 0; i < ff.length; i++) {
		if (ff[i].name == a_checkbox) {
			if (sub_tot == 0) {
				ff[i].checked = false;
			} else {
				ff[i].checked = true;
			}
		}
	}
	maketotal();
//
}

function maketotal() {
	totale_span = document.getElementById("totale_s");
	totale_span.innerHTML = "";
	
	tot = 0;
	var ff = document.getElementsByTagName("div");
	for (i = 0; i < magazzino.length; i++) {
		var n = ff[i].id.split("_");
		
		if (n[0] == "prezzo") {
			var p = ff[i].innerHTML.split(" ");
			alert(p[0] + "___" + p[1])
			//alert(p[0]*1)
			tot = tot + p[0]*1;
			
		}
	}
	alert("TOT: "+tot)
	totale_span.innerHTML = tot;
//
}
//#################################################################
function cancel_p() {
	location = location.href;
	location.reload(true);
}
//#################################################################
//#################################################################
//#################################################################
function prenota0() {
//
if (magazzino.length == 0) {
	document.getElementById("prenota").innerHTML = "<center><br>Presto online........<br><br></center>";
} else {
	var container = document.getElementById("prenota");


	var table = "<table cellspacing=0 cellpadding=0 class=prenota>";
	var table1 = "<table cellspacing=0 cellpadding=0 class=prenota1>";
	var end_table = "</table>";
	var tr = "<tr>";
	var end_tr = "</tr>";
	var td = "<td class=td0>";
	var end_td = "</td>";
	var content = "";
	//#############################################
	content = "<div id=prodotti style=\"display:;\">";
	
	
	content = content + "<form id=prenota_0 method=POST enctype=\"text/plain\">";
	content = content + table;
	content = content + tr + "<td class=prenota_h>" + "&nbsp;" + end_td;
	content = content + "<td class=prenota_h>" + "<b>" + "Prodotti" + "</b>" + end_td;
	content = content + "<td class=prenota_h>" + "<b>" + "Disponibilit&agrave;" + "</b>" + end_td + "\n";
	content = content + "<td class=prenota_h>" + "<b>" + "Quantit&agrave;" + "</b>" + end_td + "\n";
	content = content + "<td class=prenota_h>" + "<b>" + "Prezzo (&euro;)" + "</b>" + end_td + "\n";
	content = content + "<td class=prenota_h>" + "<b>" + "Subtotale (&euro;)" + "</b>" + end_td + end_tr + "\n";
	j = 0;
	/////////////////////////////////////////////////
	for (i = 0; i < magazzino.length; i++) {
		
		if (i%3 == 0) {
			
			iq = i + 2;
			descrizione = magazzino[i];
			quantita_disp = magazzino[iq];
			prezzo = magazzino[iq];
			
			j = j + 1;
			if (j%2 == 0) {
				csstr = "c2b"
			} else {
				csstr = "c1b"
			}
			content = content + "<tr class="+csstr+">"  + "<td class=td1>" + 
			"<input onclick=\"changeqta(this,'qta_"+iq+"','"+iq+"','"+quantita_disp+"','"+prezzo+"')\" type=checkbox name=articolo_"+i+" value=\""+descrizione+"\">" + 
			end_td + 
			td + 
			descrizione + 
			end_td;

		} else if (i%3 == 1) {

			quantita_disp = magazzino[i];
			
			content = content + td + 
			quantita_disp + 
			end_td; 
			
		} else {
			
			q = i - 1;
			quantita_disp = magazzino[q];
			prezzo = magazzino[i];
			
			content = content + td + 
			"<input onkeydown=\"f(this)\" onkeyup=\"f(this);changeqta1(this,'"+i+"','"+prezzo+"','"+quantita_disp+"')\" onblur=\"f(this)\" onclick=\"f(this)\" type=text name=qta_"+i+" size=5 value=\"\">" + 
			end_td + 
			td + 
			prezzo + " &euro;" + 
			end_td +
			td + 
			"<b><div id=prezzo_"+i+"></div></b>" + 
			end_td +
			end_tr + "\n";
			
		}
			
	//
	}

	//MSG///////////////////////////////////////////////
	content = content + tr + "<td id=msg_t class=msg colspan=4 style=\"display:none;\"><div id=msg_d class=msg style=\"display:none;\"></div></td>" + end_tr;
	content = content + end_table;

	//TOTALE/////////////////////////////////////////////
	content = content + table1;
	content = content + tr + "<td class=td_tot>Totale: <span id=totale_s></span> &euro;" + end_td + end_tr;
	content = content + end_table;
	///////////////////////////////////////////////


	content = content + table;
	/////////////////////////////////////////////////
	content = content + tr + "<td colspan=2 class=td3><b>Inserisci i tuoi dati per ultimare la prenotazione</b>.<br>Verrai contattato il pi&ugrave; presto possibile e potrai venire a ritirare i prodotti che hai scelto presso il nostro punto vendita." + end_td + end_tr;

	content = content + tr + "<td class=td1>Nome*</td>" + td + "<input type=text name=nome size=30 value=\"\">" + end_td + end_tr;
	content = content + tr + "<td class=td1>Cognome*</td>" + td + "<input type=text name=cognome size=30 value=\"\">" + end_td + end_tr;
	content = content + tr + "<td class=td1><nobr>E-mail*</td>" + td + "<input type=text name=email size=30 value=\"\">" + end_td + end_tr;
	content = content + tr + "<td class=td1>Telefono*</td>" + td + "<input onkeydown=\"f(this)\" onkeyup=\"f(this)\" onblur=\"f(this)\" onclick=\"f(this)\" type=text name=telefono size=30 value=\"\">" + end_td + end_tr;
	/////////////////////////////////////////////////

	content = content + tr + "<td class=nota colspan=2>I campi contrassegnati con l'asterisco (*) sono obbligatori</td>" + end_tr;
	content = content + tr + "<td class=td_btn colspan=2>" + "<a class=prenota_btn href=\"javascript:cancel_p();\">Annulla</a>" + "&nbsp;<a class=prenota_btn href=\"javascript:prenota('prenota_0')\">Prenota</a>" + end_td + end_tr;
	
	content = content + end_table;
	content = content + "</form>";
	//#############################################
	
	
	content = content + "</div>";
	
	content = content + "<div id=testo_print style=\"display:none;\"></div>";
	//#############################################
	content = content + "<div id=confirm_p style=\"display:none;\"></div>";
	//#############################################
	content = content + "<div id=thanx style=\"display:none;\"></div>";
	//#############################################


	//#############################################
	container.innerHTML = content;
	}
//
}

//#############################################
//#############################################
//#############################################
function cont(f) {

	var cont_c = true;
	
	var ff = document.getElementsByTagName("input");
	for (i = 0; i < ff.length; i++) {
		
		if (ff[i].name == "nome") {
			nome0 = ff[i].value;
		}
		if (ff[i].name == "cognome") {
			cognome0 = ff[i].value;
		}
		if (ff[i].name == "telefono") {
			telefono0 = ff[i].value;
		}
		if (ff[i].name == "email") {
			email0 = ff[i].value;
			if (ff[i].value != "") {
				var validemail = isValidEmail(ff[i].value);
				if (!validemail) {
					ff[i].value = "E-MAIL non valida";
					cont_c = false;
					break;
				}
			} else {
					ff[i].value = "";
					ff[i].value = "inserire E-MAIL";
					cont_c = false;
					break;
			}

		}
	}
	var ff = document.getElementsByTagName("textarea");
	for (i = 0; i < ff.length; i++) {
		if (ff[i].name == "messaggio") {
			messaggio0 = ff[i].value;
			if (ff[i].value == "") {
				ff[i].value = "Scrivere un MESSAGGIO";
				cont_c = false;
				break;
			}
		}
	}
	
	if (cont_c) {
	
		ifr = document.getElementById("contatti_iframe");
		ifr_doc = ifr.contentWindow.document;
		form = ifr_doc.getElementById("contatti");

		////////////////////
		ff1 = ifr_doc.getElementsByTagName("textarea");
		for (ii = 0; ii < ff1.length; ii++) {
			if (ff1[ii].name == "messaggio") {
				ff1[ii].value = messaggio0;
			}
		}

		ff1 = ifr_doc.getElementsByTagName("input");
		for (ii = 0; ii < ff1.length; ii++) {
			if (ff1[ii].name == "nome") {
				ff1[ii].value = nome0;
			}
			if (ff1[ii].name == "cognome") {
				ff1[ii].value = cognome0;
			}
			if (ff1[ii].name == "email") {
				ff1[ii].value = email0;
			}
			if (ff1[ii].name == "telefono") {
				ff1[ii].value = telefono0;
			}
		}
		/////////////////////
		

		ff1 = document.getElementsByTagName("div");
		for (ii = 0; ii < ff1.length; ii++) {
			if (ff1[ii].id == "thanx_contatti") {
				ff1[ii].style.display = "";
			}
		}
		ff1 = document.getElementsByTagName("div");
		for (ii = 0; ii < ff1.length; ii++) {
			if (ff1[ii].id == "btns1") {
				ff1[ii].style.display = "none";
			}
		}
		ff1 = document.getElementsByTagName("div");
		for (ii = 0; ii < ff1.length; ii++) {
			if (ff1[ii].id == "btns2") {
				ff1[ii].style.display = "";
			}
		}
		form.action = "http://demos.invisibledesign.it/equipeolympique/email.php";
		//form.action = "email.php";
		
		form.submit();
		
		form.outerHTML = "";
	}
//
}





//#############################################
//#############################################
//#############################################
//#############################################
//#############################################


