var myRequest = new Array();
var x = 0;
function showImage(urlImage){
	startLoading(true);
	document.getElementById('immagineProdotto').src = urlImage;
	setTimeout("stopLoading()", 500);
}

function newsletter(){
	var email = document.getElementById('newsEmail').value;
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email)) {
		x++;
		var kgoCaptcha = document.getElementById('kgoCaptcha').value;
		var r = Math.random();
		myRequest[x] = CreateXmlHttpReq(function() {myVerificaHandler(x)});
		myRequest[x].open("GET","kgoLoader/checkVerifica.php?r=" + escape(r) + "&kgoCaptcha=" + escape(kgoCaptcha));
		myRequest[x].send(null);
	}else{
		startLoading(false);
		document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">Inserire un indirizzo email corretto.<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
		fissaCentro('myLayer', 40, 10, 120, false);
		return false;
	}
}

//ancora in bozza.. da completare quando trovo tempo----------------------------------
function loadObjX(div, m, d, h, y){
	//startLoading(false);
	//document.getElementById(div).innerHTML = '<table width="100%" bgcolor="#D0D0D7"><tr><td height="400" align="center" valign="middle"><img src="' + 'http://demo.kgo.it/' + 'kgoLoader/overcome.gif"></td></tr></table>';
	//startLoading(false);
	setTimeout("loadObjX2('"+div+"', '"+m+"', '"+d+"', '"+h+"', '"+y+"')", 250);
}

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;
}

function loadObjX2(div, m, d, h, y){
	x++;
	var r = Math.random();
	var myUrl = 'kgoLoader/kgoLoader.php?';
	myUrl += 'r=' + escape(r);
	myUrl += '&d=' + escape(d);
	myUrl += '&m=' + escape(m);
	myUrl += '&h=' + escape(h);
	myUrl += '&y=' + escape(y);
	myRequest[x] = CreateXmlHttpReq(function() {StandardMyHandlerKgo(div, x)});
	myRequest[x].open("GET", myUrl);
	myRequest[x].send(null);
}

function StandardMyHandlerKgo(div, x) {
	if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
		e = document.getElementById(div);
		e.innerHTML = myRequest[x].responseText;
		//setTimeout("stopLoading()", 150);
		setTimeout("Nascondi('cover')", 150);
	}
}


function loadCart(domicilio){
	x++;
	startLoading(false);
	var r = Math.random();
	var theUrl = 'carrelloAjaxRiepilogo.php?r=' + escape(r);
	theUrl += '&domicilio=' + escape(domicilio);
	myRequest[x] = CreateXmlHttpReq(function() {stampaInnerHTML('contenutoCarrello', true, x , '', '')});
	myRequest[x].open("GET", theUrl);
	myRequest[x].send(null);
}

function emailHandler(x) {
	if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
		mySo = myRequest[x].responseText;
		if(mySo == '0010'){
			document.aggiornamento.submit();
		}else{
			document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">L\'indirizzo email indicato è già presente.<p><input type="button" name="Annulla" id="Annulla" value="Annulla" onclick="stopLoading();" /></p></div>';
			fissaCentro('myLayer', 40, 10, 120, false);
			return false;
		}
	}
}

function blockMe(field){
	stopLoading();
	document.getElementById(field).focus();
}

function controlloDati(){
	x++;
	var kgoCaptcha = document.getElementById('kgoCaptcha').value;
	var r = Math.random();
	myRequest[x] = CreateXmlHttpReq(function() {myVerificaHandler(x)});
	myRequest[x].open("GET","kgoLoader/checkVerifica.php?r=" + escape(r) + "&kgoCaptcha=" + escape(kgoCaptcha));
	myRequest[x].send(null);
}

function myVerificaHandler(x) {
	if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
		mySo = myRequest[x].responseText;
		if(mySo==1){
			document.theForm.submit();
		}else{
			alert('Ricopiare il codice Captcha nel campo apposito.');
			document.getElementById('kgoCaptcha').focus();
			document.getElementById('kgoCaptcha').value = '';
			cambiaCodice();
		}
	}
}

function cambiaCodice(){
	startLoading(true);
	setTimeout("cambiaCodice2()", 500);
}

function cambiaCodice2(){
	var r = Math.random();
	document.getElementById('captchaDiv').innerHTML = '<img src="kgoLoader/captcha/immagine.php?r=' + escape(r) + '"><br>se il codice &egrave; illeggibile, <span class="captchaLinkSmall" onClick="cambiaCodice();">[clicca qui]</span>';
	stopLoading();
}



function hiddenHandler(x){
	if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
		var myResponse = myRequest[x].responseText;
		if(myResponse != 'ok'){
			alert(myResponse);
		}
	}
}

function startLoading(showLoading){
	var myTarget = document.getElementById('myFog');
	var screenLen = document.body.clientWidth;
	var screenHei= document.body.clientHeight;
	//myTarget.style.width = screenLen + 'px';
	//myTarget.style.height = screenHei + 'px';
	myTarget.style.width = '100%';
	myTarget.style.height = '100%';
	myTarget.style.top = 0;
	myTarget.style.left = 0;
	myTarget.style.visibility = 'visible';
	myTarget.style.display = 'block';
	if(showLoading){ fissaCentro('myLoadGif', 32, 32, 1, true); }
}
function fissaCentro(div, larghezza, altezza, dimensioneFont, isPixel){
	myDiv = document.getElementById(div);
	if(isPixel){
		myDiv.style.height = altezza + 'px';
		myDiv.style.width = larghezza + 'px';
		myDiv.style.fontSize = dimensioneFont + 'px';
		myDiv.style.top = ((document.body.clientHeight/2) + (altezza/2)) + 'px';
		myDiv.style.left = ((document.body.clientWidth/2) - (larghezza/2)) + 'px';
	}else{
		myDiv.style.width = larghezza + '%';
		myDiv.style.height = altezza + '%';
		myDiv.style.fontSize = dimensioneFont + '%';
		myDiv.style.top = (50 - (altezza/2)) + '%';
		myDiv.style.left = (50 - (larghezza/2)) + '%';
	}
	myDiv.style.visibility = 'visible';
	myDiv.style.display = 'block';
}
function MostraNascondi(quale){
	var r = document.getElementById(quale);
	if(r.style.visibility == 'hidden'){
		r.style.visibility = 'visible';
		r.style.display = 'block';
	}else{
		r.style.visibility = 'hidden';
		r.style.display = 'none';
	}
}
function Nascondi(quale){
	var r = document.getElementById(quale);
	r.style.visibility = 'hidden';
	r.style.display = 'none';
}
function Mostra(quale){
	var r = document.getElementById(quale);
	r.style.visibility = 'visible';
	r.style.display = 'block';
}
function CreateXmlHttpReq(handler) {
	var xmlhttp = null;
	try {
		xmlhttp = new XMLHttpRequest();
	}catch(e){
		try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	xmlhttp.onreadystatechange = handler;
	return xmlhttp;
}
function StandardMyHandler(div) {
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		e = document.getElementById(div);
		e.innerHTML = myRequest.responseText;
		stopLoading();
	}
}
function stampaInnerHTML(div, sL, x, funzione, parametro) {
	if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
		var myResponse = myRequest[x].responseText;
		document.getElementById(div).innerHTML = myResponse;
		if(sL){
			Nascondi('myFog');
			Nascondi('myLayer');
		}
		if(funzione){
			if(parametro){
				funzione(parametro);
			}else{
				funzione();
			}
		}
	}
}
function stopLoading(){
	Nascondi('myFog');
	Nascondi('myLayer');
	Nascondi('myLoadGif')
}
