var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);
var tempX = 0;
var tempY = 0;
var url_sito = "http://www.armadillonet.it";

function PreparaDati(nome_form){
  var stringa = "";
  for (i=0; i<document.forms.length;i++) {
	  if (document.forms[i].name==nome_form) var frm = document.forms[i];
  }
  
  if (frm==null) var frm = document.forms[0];
  var numeroElementi = frm.elements.length;
  
  for(var i = 0; i < numeroElementi; i++){
	//if(frm.elements[i].type=="checkbox" && frm.elements[i].checked==true) frm.elements[i].value="1";
	//controllo se il campo non è fleggato lo metto a vuoto
	if(frm.elements[i].type=="checkbox" && frm.elements[i].checked==false){ frm.elements[i].value="";	}
    if(i < numeroElementi-1){
      stringa += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value)+"&";
    }else{
      stringa += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value);
    }  
  }
  return stringa;
}


function ajaxRequest(method,page,element,form,func){
 var xmlhttp;
 var el=document.getElementById(element);
 
  xmlhttp= new XMLHttp();
  xmlhttp.open(method,page,true);
  
  if ( method == 'POST' ) {
	  
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.setRequestHeader('Cache-Control', 'no-cache'); // if XML code rather than a static XML file (IE)
  uristring = PreparaDati(form);
 }else{
	 uristring="";
 }
 
  xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4){
			if (el!=null){
				if(el.tagName!="INPUT"){
					el.innerHTML =xmlhttp.responseText;
				}else{
					el.value =xmlhttp.responseText;
				}
			}else{
				var mess=new Boxy("<p>"+xmlhttp.responseText+"</p>",{title: 'Message',modal: true,draggable:false});
			}
			if(func) func.call();
			
		}else {}
	}
  
 
  xmlhttp.send(uristring);
  
}

 function XMLHttp() {
 var request = false ;
 if ( window.XMLHttpRequest ) {
  request = new XMLHttpRequest() ;
 } else if ( window.ActiveXObject ) {
  try {
   request = new ActiveXObject ( 'Msxml2.XMLHTTP' ) ;
  } catch ( e ) {
   try {
    request = new ActiveXObject ( 'Microsoft.XMLHTTP' ) ;
   } catch ( e ) {}
  }
 }
 if ( !request ) alert( 'Could not create an XMLHttpRequest istance.' );
 return request ;
}

function confirmDeleteFile(file,page,testo) {
	id_file=file.split("|");
	a="Confermi la candellazione del file";
	if(testo!='' && testo && testo!='undefined'){ 
		a=testo;
	}else{
		a="Confermi la candellazione del file";
	}
	
	doConfirm = confirm(a + ' "'+id_file[1]+'"');	
	if (doConfirm == true) { 
		window.location = url_sito+page+'&action=delete&file='+id_file[0];
	} else {
		return;
	}
}

function aggiungi_carrello(url,codice,box,func){
	ajaxRequest('GET',url+'carrello.aj.php?add='+codice,box,'',func);
}

function elimina_carrello(url,codice,tipo){
	if(confirm('Eliminare questo articolo da carrello?')) ajaxRequest('GET',url+'carrello.aj.php?'+tipo+'&delete='+codice,'box_carrello','');
}

function aggiorna_carrello(url,codice,tipo){
	qta=document.getElementById("qta_"+codice).value;
	ajaxRequest('GET',url+'carrello.aj.php?'+tipo+'&update='+codice+'&qta='+qta,'box_carrello','');
}

function aggiorna_quantita(url,box,func){
	ajaxRequest('GET',url+'carrello.aj.php?update_qta=1',box,'',func);
}

function aggiorna_box_carrello(url,box,func){
	ajaxRequest('GET',url+'carrello.aj.php?print_cart=1',box,'',func);
}

function fade_extend(eid,stato){
	var action="";
	var element=document.getElementById(eid);
	if(element.style.display=="none" || stato==1){
		action="inline";
	}else{
		action="none";
	}
	element.style.display=action;
}

/*function stampa_lista_prodotti(prodotti,contenitore){
	var sep='<sep>';
	var codici_prodotti=prodotti.join(sep);
	ajaxRequest('GET',url_sito+'prodotti_tiny.aj.php?codici='+codici_prodotti+'&sep='+sep,contenitore,'','');
}*/

function opencluetip(messaggio,linko){
	var maxY=document.body.clientHeight-50;
	
	var cont=document.getElementById('cluetip');
	var linkover=document.getElementById(linko);
	var position = {x:0,y:0};
	while (linkover){
		position.x += linkover.offsetLeft;
		position.y += linkover.offsetTop;
		linkover = linkover.offsetParent;
	}
	cont.innerHTML=messaggio.replace("\"","&quot;");
	cont.style.display="inline";

	var tempX=position.x+100;
	var tempY=position.y;
	
	var pageY;
	if(typeof(window.pageYOffset)=='number') {
		pageY=window.pageYOffset;
	}else {
		pageY=document.documentElement.scrollTop;
	}
	
	//alert("MAXY: "+maxY+", YOFFESET: "+pageY+", OFFSETHEIGHT: "+cont.offsetHeight);
	
	if(cont.offsetHeight+tempY > maxY+pageY){ tempY=maxY+pageY-cont.offsetHeight; }
	
	
	cont.style.left=tempX+"px";
	cont.style.top=tempY+"px";
}

function closecluetip(){
	var cont=document.getElementById('cluetip');
	cont.innerHTML='';
	cont.style.display="none";
}

function boxy_frame(url,title_frame){
	new Boxy("<iframe width=\"340\" height=\"80\" src=\""+url+"\"></iframe>",{title: title_frame,modal: true,draggable:false, afterHide: function() { window.location.reload(); }});
}
