// JàvaScript Document

//mostra elemento
function show(nome){
if ((document.getElementById) && (document.getElementsByTagName)) {
		
		if(document.getElementById(nome).getElementsByTagName('ul')[0].style.display == 'none')
		{
			document.getElementById(nome).getElementsByTagName('ul')[0].style.display = 'block';
			document.getElementById(nome).getElementsByTagName('a')[0].className = 'open';
		} else {
			document.getElementById(nome).getElementsByTagName('ul')[0].style.display = 'none';
			document.getElementById(nome).getElementsByTagName('a')[0].className = '';
		}
}
}

function mostra(id){
if (document.getElementById){
if(document.getElementById(id).style.display == 'none'){
document.getElementById(id).style.display = 'block';
} else {
document.getElementById(id).style.display = 'none';
}
}
}


//paginazione immmagini
function showPic(where,desc,which) { 
	if (document.getElementById) { 
		document.getElementById(where).src = which.href;
		document.getElementById('didascalia').innerHTML = desc;
	} 
}

//paginazione immmagini
function showImg(where,which) { 
	if (document.getElementById) { 
		document.getElementById(where).src = which.href;
	} 
}



//invio form
function send(formid) {
	document.getElementById(formid).submit();
}


//funzione di stampa
function stampa() {
	if (window.print) {
		window.print();
	} else {
		alert('Funzione non supportata dal browser. Seleziona Stampa dal menu File.');
	}
}


var xmlHttp // xmlHttp variable

function GetXmlHttpObject(){ // This function we will use to call our xmlhttpobject.
var objXMLHttp=null // Sets objXMLHttp to null as default.
if (window.XMLHttpRequest){ // If we are using Netscape or any other browser than IE lets use xmlhttp.
objXMLHttp=new XMLHttpRequest() // Creates a xmlhttp request.
}else if (window.ActiveXObject){ // ElseIf we are using IE lets use Active X.
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") // Creates a new Active X Object.
} // End ElseIf.
return objXMLHttp // Returns the xhttp object.
} // Close Function


function validate_add_to_cart(size) {
		
		sitem = document.add_to_cart_frm.item_id.value;
		sqta = document.add_to_cart_frm.qta.value;
		
		if(size == 1)
		{
			ssize = document.add_to_cart_frm.size.options[document.add_to_cart_frm.size.selectedIndex].value;
			message = ' for the selected size';
		}
		else
		{
			ssize = 0;
			message = '';
		}
		
		if (document.add_to_cart_frm.qta.value == '')
		{
			document.getElementById("alert-box").innerHTML = '<p>You have not inserted a quantity.</p>';
			return false;
		}
		
		else if (isNaN(document.add_to_cart_frm.qta.value) || (String(document.add_to_cart_frm.qta.value).indexOf(".") != (-1)))
		{
			document.getElementById("alert-box").innerHTML = '<p>Use only whole numbers for quantity.</p>';
			return false;
		}
		
		else if (document.add_to_cart_frm.qta.value == 0)
		{
			document.getElementById("alert-box").innerHTML = '<p>You have inserted zero. Insert a number for quantity.</p>';
			return false;
		}
		
		else if ((size == 1) && (document.add_to_cart_frm.size.selectedIndex == 0))
		{
			document.getElementById("alert-box").innerHTML = '<p>You have not selected your size.</p>';
			return false;
		}
		
		else
		{
			xmlHttp=GetXmlHttpObject()
			if (xmlHttp==null){ 
			alert ("Browser does not support HTTP Request")
			return
			}

			var url="http://www.oxox.it/check_qta.php?sel_item="+sitem+"&sel_qta="+sqta+"&sel_size="+ssize 
			xmlHttp.open("GET",url,true)
			xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				if(xmlHttp.responseText != 'ok')
				{
					document.getElementById("alert-box").innerHTML = '<p>The quantity you have selected for this item is more than we have in stock.<br />You can order maximum '+xmlHttp.responseText+' piece(s)'+message+'.</p>';
				}
				else
				{
					document.add_to_cart_frm.submit();
				}
			} // End If.
			};
			xmlHttp.send(null);
		}
}


function upd_postage_ajax(suff,cart) {
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){ 
	alert ("Browser does not support HTTP Request")
	return
	}
	
	var page="http://www.oxox.it/update_postage.php?suff="+suff+"&cart="+cart
	xmlHttp.open("GET",page,true)
	xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
			document.getElementById("pst").innerHTML = xmlHttp.responseText;
			} // End If.
	};
	xmlHttp.send(null);
}


function upd_postage(suff,cart) {
	page="http://www.oxox.it/update_postage.php?suff="+suff+"&cart="+cart
	location.href = page;
}