var pathImg = "images/prodotti/";
var pathImgBN = "images/prodotti/bn/";
var imgselezione = "ok.gif";
var div_over_dark = "darkdiv";
function checkImg(id,col,prev,prz,tot){ 
	var valore = document.getElementById(id).value;
	valore = (valore == 1) ? 0 : 1;
	document.getElementById(id).value = valore;
	
	prz = getValFormat(prz,"1");
	//tot = getValFormat(tot,"1");
	
	if (valore) { 
		document.getElementById(col).innerHTML = '<img src="images/' + imgselezione + '" border="0" width="24" />';
		//document.getElementById(col).style.background="#66FF00";
	}
	else {
		document.getElementById(col).innerHTML = '';
		//document.getElementById(col).style.background=prev;
	}
}
function cambiaimg(idc,idimg,img,stato){
	var valore = document.getElementById(idc).value;
	if (valore==1) { 
		document.getElementById(idimg).src=pathImg + img; 
	}
	else {
		if (stato=="1"){
			document.getElementById(idimg).src=pathImg + img;
		}
		else{ 
			document.getElementById(idimg).src=pathImgBN + img; 
		}
	}
}
function aggiungiimg(idc,idimg,img,noimg){ 
	var valore = document.getElementById(idc).value;
	if (valore==1) {
		document.getElementById(idimg).src=pathImg + img;
		mostra_singolo(idimg);
		//alert('check');
	}
	else {
		if (noimg==''){
			document.getElementById(idimg).src='';
			nascondi(idimg);
			//alert('no check no img');
		}
		else{
			document.getElementById(idimg).src=pathImg + noimg;
			mostra_singolo(idimg);
			//alert('no check img');
		}
	}
}
function getValFormat(val,op){
	var tmp = val;
	if (op=="1"){
		tmp = tmp.replace('.','');
		tmp = tmp.replace(',','.');
	}
	else{
		if (tmp!=''){ tmp = tmp.replace('.',','); }
		else{ tmp = ''; }
	}
	return tmp;
}

/* Spostamento attivazioni */
function getX(obj){
   return( obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft+getX(obj.offsetParent) );
}

function getY(obj){
   return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+getY(obj.offsetParent) );
}

function getStringaFinale(obj,separatore){
	return obj.substring(obj.lastIndexOf(separatore)+1);
}
function getStringaSenzaFinale(obj,separatore){
	return obj.substring(0,obj.lastIndexOf(separatore)+1);
}

function spostaDivAtt(idref,idmove,dimW,dimH,idchk){
	if (getStringaFinale(document.getElementById(idchk).src,"/") != imgselezione){
		spostaMostraOggetto(idref,idmove,dimW,dimH);
	}
	else{
		nascondi(idmove);
	}
}
/* FINE Spostamento attivazioni */

function spostaMostraOggetto(idref,idmove,dimW,dimH){
	dimW = 0;
	var xPos = parseInt(getX(document.getElementById(idref)));
	var yPos = parseInt(getY(document.getElementById(idref)));
	/*var altScreen = parseInt(document.getElementById('tblBase').offsetHeight);
	var larghScreen = parseInt(document.getElementById('tblBase').offsetWidth);*/
	var altScreen = parseInt(GetHeight());
	var larghScreen = parseInt(GetWidth());
	var leftScreen = 0;
	var topScreen = 0;
	var larghDiv = 0;
	var altDiv = 0;
	var leftDiv = 0;
	var topDiv = 0;
	
	//sposto il div fuori schermo
	document.getElementById(idmove).style.left = "-9999px";
	//lo rendo visibile
	mostra_singolo(idmove);
	//nascondiDivDaStringa(document.getElementById('objStrDiv').value,'|');
	
	//adesso posso avere larghezza e altezza correnti
	larghDiv = parseInt(document.getElementById(idmove).offsetWidth);
	altDiv = parseInt(document.getElementById(idmove).offsetHeight);

	//se la posizione corrente + la dimensione del div vanno fuori schermo
	if ((parseInt(xPos) + parseInt(dimW) + parseInt(larghDiv))>larghScreen){
		//dalla posizione corrente toglie la dimensione del div e una costante (opzionale)
		leftDiv = parseInt(xPos) - parseInt(dimW) - parseInt(larghDiv) + "px";	
		//se il div va troppo a destra
		if (parseInt(leftDiv)<leftScreen){
			//imposta il top a 0
			leftDiv = leftScreen + "px";
		}
	}
	else{
		//altrimenti va bene cosė
		leftDiv = parseInt(xPos) + parseInt(dimW) + "px";
	}
	
	//come sopra
	if ((parseInt(yPos) + parseInt(dimH) + parseInt(altDiv))>altScreen){
		topDiv = parseInt(yPos) - parseInt(dimH) - parseInt(altDiv) + "px";
		//se il div va troppo in alto
		if (parseInt(topDiv)<topScreen){
			//imposta il top a 0
			topDiv = topScreen + "px";
		}
	}
	else{
		topDiv = parseInt(yPos) + parseInt(dimH) + "px";
	}
	
	document.getElementById(idmove).style.left = leftDiv;
	document.getElementById(idmove).style.top = topDiv;
}

function nascondiDivDaStringa(strDiv,separatore){
	if (strDiv=="") return false;
	var col_array=strDiv.split(separatore);
	var part_num=0;
	while (part_num < col_array.length-1)
	{
	  //alert('div_' + col_array[part_num]);
	  nascondi('div_' + col_array[part_num]);
	  part_num+=1;
	}
}

/* CODICI X ALTEZZA/LARGHEZZA (FIX IE8) */
/* clientHeight restituisce la larghezza VISIBILE della finestra (idem per clientWidth) */
function GetHeight(){
	var y = 0;
	if (self.innerHeight){
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight){
		y = document.documentElement.clientHeight;
	}
	else if (document.body){
		y = document.body.clientHeight;
	}
	return y;
}

function GetWidth(){
	var x = 0;
	if (self.innerWidth){
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth){
		x = document.documentElement.clientWidth;
	}
	else if (document.body){
		x = document.body.clientWidth;
	}
	return x;
}

function getDivSelezione(pag,query,divdest,nomediv){
	carica(divdest,pag,query);
	spostaDivAtt('div_sel_' + nomediv,'div_att','15','33','chk_' + nomediv);
	mostra_singolo(div_over_dark);
}

function controllaQuantita(idtext,op,idlink){
	if (op=="-"){
		if (document.getElementById(idtext).value != 1)
			cambiaQuantita(idtext,op,idlink);
    }
	else{
		cambiaQuantita(idtext,op,idlink);	
	}
}

function cambiaQuantita(idtext,op,idlink){
	if (op=="+"){
		if (document.getElementById(idtext).value < 999){
			document.getElementById(idtext).value=parseInt(document.getElementById(idtext).value)+1;
		}
    }
    else{
    	if (document.getElementById(idtext).value > 0)
    		document.getElementById(idtext).value=parseInt(document.getElementById(idtext).value)-1;
    }
	document.getElementById(idtext).style.background="yellow";
	if (idlink!=""){
		mostra_singolo(idlink);
	}
}
function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

 return true;
}

/*var altScreen = 800;
var larghScreen = 600;

if(document.all){
altScreen = window.screen.height;
larghScreen = window.screen.width;

availW = document.body.clientWidth;
availH = document.body.clientHeight;
}else{
availW = innerWidth;
availH = innerHeight;
}*/
/* FINE CODICI X ALTEZZA E LARGHEZZA (FIX IE8) */