var thepop;
function pop(url,w,h) {
	var x = (screen.width-w)/2
	var y = (screen.height-h)/2
	var parms = 'left=' + x + ',top=' + y + ',width=' + w +',height='+h+',toolbar=0'
	
//	thepop = window.open(url,'thepop',parms)

	 if(!thepop || thepop.closed) {
		 thepop = window.open(url,'popup',parms);
	 } else {
		 setTimeout("thepop.focus();",40);
	 }

//	if (window.focus) {thepop.focus()}

return;
}

function addWindow(add_window_url) {
 
  return false;
}

/*** COOKIES ***/
function setCookie(NameOfCookie, value, expiredays, path, domain, secure)
{
  var ExpireDate = new Date();
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays*24*3600*1000));
  document.cookie = NameOfCookie +"="+ escape(value) + 
  ((expiredays == null)? "": ";expires="+ ExpireDate.toGMTString()) +
  ((path == null)? "": (";path=" + path)) +
  ((domain == null) ? "" : (";domain=" + domain)) +
  ((secure == true) ? ";secure" : "");
}

function getCookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { 
      offset += search.length
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function getCookieOld(NameOfCookie) {
   if(document.cookie.length > 0)
  {
     begin = document.cookie.indexOf(NameOfCookie+"=");
     if(begin != -1)
     {
        begin += NameOfCookie.length + 1;
        end = document.cookie.indexOf(";",begin);
        if(end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin,end));
     }
  }
  return null;
}

function delCookie(NameOfCookie) {
	if(getCookie(NameOfCookie)){
		document.cookie = NameOfCookie +"=" + ";expires= Thu, 01-Jan-70 00:00:01 GMT";
	}
}


/*** SOUND ***/
var audio    = false; //true = no audio !
var s_click  = "<embed src='sounds/click_x.wav'   volume='90' height='0' width='0' autostart='true' loop='false'>";  
var s_cash   = "<embed src='sounds/cashreg.wav'   volume='50' height='0' width='0' autostart='true' loop='false'>";  
var s_boing  = "<embed src='sounds/boing_1.wav'   volume='50' height='0' width='0' autostart='true' loop='false'>"; 
var s_squeak = "<embed src='sounds/squeaky.wav'   volume='30' height='0' width='0' autostart='true' loop='false'>";  
var s_pop    = "<embed src='sounds/pop.wav'       volume='50' height='0' width='0' autostart='true' loop='false'>";  


function bip(sound,mute) {
   var muted = mute || false; 
   if (muted) return;
   top.F1.document.getElementById('dummyspan').innerHTML = '';
   top.F1.document.getElementById('dummyspan').innerHTML = sound;
}

function zot() {
	var z = Math.floor(Math.random()*2);
	if (z == 0) {
		bip(s_click,audio);
	} else {
		bip(s_pop,audio);
	}
	
}

function toggle_audio() {
	audio = !audio;
	top.F1.document.images['audio1'].src  =  audio ? 'img_sito/sound-on.gif' : 'img_sito/sound-off.gif' ; 
}


/*** ROLLOVER ***/
function refresh() {
if (document.images) {
	  var butt1_n = new Image();
	  butt1_n.src = "img_sito/butt1_n.gif";
	  var butt2_n = new Image();
	  butt2_n.src = "img_sito/butt2_n.gif";
	  var butt3_n = new Image();
	  butt3_n.src = "img_sito/butt3_n.gif";
	  var butt4_n = new Image();
	  butt4_n.src = "img_sito/butt4_n.gif";
	  var butt5_n = new Image();
	  butt5_n.src = "img_sito/butt5_n.gif";
	  var cass1_n = new Image();
	  cass1_n.src = "img_sito/cass1_n.png";
	  var sear1_n = new Image();
	  sear1_n.src = "img_sito/sear1_n.gif";
	  
	  var butt1_a = new Image();
	  butt1_a.src = "img_sito/butt1_a.gif";
	  var butt2_a = new Image();
	  butt2_a.src = "img_sito/butt2_a.gif";
	  var butt3_a = new Image();
	  butt3_a.src = "img_sito/butt3_a.gif";
	  var butt4_a = new Image();
	  butt4_a.src = "img_sito/butt4_a.gif";
	  var butt5_a = new Image();
	  butt5_a.src = "img_sito/butt5_a.gif";
	  var cass1_a = new Image();
	  cass1_a.src = "img_sito/cass1_a.png";
	  var sear1_a = new Image();
	  sear1_a.src = "img_sito/sear1_a.gif";
}
return true;
}
var dummy = refresh();

function act(imgName) {
  if (document.images) {
	document.images[imgName].src = eval(imgName + "_a.src");
  }
}

function inact(imgName) {
  if (document.images)  {
	document.images[imgName].src = eval(imgName + "_n.src");
  }
}

function reset_all(base,from,to) {
	for (k=from;k<=to;k++) {
		img = base + k;
		inact(img);
	}
}

function bup() {
	reset_all('butt',1,5);
}

function bdown(imgName) {
	bup();
	act(imgName);
//	EvalSound('sound1');
 	DHTMLSound(snd1);
}


/*** XMLHTTPREQUEST ***/

var http;
var result;
var target;
var pbar;
var showbar;
var xhr_rc;
var xhr_s;

function processReqChange() {
//  alert('processReqChange()');
    if (http.readyState == 4) {
        if (http.status == 200) {
//   		alert('target='+target+'\n\n'+http.responseText);
		if (target == 'eval') {
			eval(http.responseText);
		} else {
			top.F2.document.getElementById(target).innerHTML = http.responseText;
			top.F2.document.getElementById(target).scrollTop = 0;
		}
        } else {
            alert("There was a problem retrieving the XML data:\n" + http.statusText);
        }
    }
}

function xhr(from,what,where) {
	target = where || 'pagina';
//     alert('getxmldoc'); 
//      url = 'http://127.0.0.1:1024/tuttomagia/pgm/' + what;
//	url = 'http://vdf-server/tuttomagia/pgm/' + what;
// 	url = 'http://www.tuttomagia.it/pgm/' + what;
    url = server + what;
//       alert(url);
    if (window.XMLHttpRequest) {
	    http = new XMLHttpRequest();
	    http.open("GET", url, true);
	    http.onreadystatechange = processReqChange;
	    http.send(null);
    } else if (window.ActiveXObject) {
	    http = new ActiveXObject("Microsoft.XMLHTTP");
	    if (http) {
		    http.onreadystatechange = processReqChange;
		    http.open("GET", url, true);
		    http.send();
	    }
    } else {
//  	    alert('questo browser non supporta xmlhttprequest, link a ' + url );
 	    url = build_url(parms,1);
 	    what.location.href = url;
	    return; 	
    }
}

function buffer2div() {
	alert(document.getElementById('buffer').innerHTML);
	document.getElementById('pagina').innerHTML = '<p>'+document.getElementById('buffer').innerHTML+'</p>';
	return;
}


/*** CARRELLO ***/
var carrello_pieno = false
var src_norm = 'img_sito/cass1_n.png'
var src_roll = 'img_sito/cass1_a.png'
function setcart(pieno) {
//	alert('setcart '+pieno)
	carrello_pieno = pieno
	if (pieno == false) {
		src_norm = 'img_sito/cass1_n.png'
		src_roll = 'img_sito/cass1_a.png'
	} else {
		src_norm = 'img_sito/cass2_n.png'
		src_roll = 'img_sito/cass2_a.png'
	}
	F1.document.getElementById('cass1').src = src_norm
	return
}

function add2cart(codice,descr,qta,prezzo,disp,sk) {
	if (disp == 'NO') {
		alert('Questo articolo non è disponibile');
		return;
	}	
	ordine_corrente = getCookie('TMord');
	riga = codice + '^' + descr + '^' + qta + '^' + prezzo + '|';
	ordine_corrente += riga;
	setCookie('TMord',ordine_corrente);
	prezzo_u = prezzo / 100;
	prezzo_t = qta * prezzo_u;
	prezzo_uc = prezzo_u.toFixed(2);
	prezzo_tc = prezzo_t.toFixed(2);
	var s ='Articolo aggiunto al carrello:<br><b>' +unescape(descr)+ '</b><br>qta:'+qta+'<br>prezzo:'+prezzo_uc+'<br>totale:'+ prezzo_tc;
//	bip(s_boing);
	show_alert(s,340,165,sk);
//	alert('Articolo '+codice+' aggiunto al carrello:\n' +unescape(descr)+ '\nqta:'+qta+'\nprezzo:'+prezzo_uc+'\ntotale:'+ prezzo_tc);
	return;
}

var timer1;
function buy(codice,descrizione,qta,prezzo,disp,scheda) {
//	alert(codice+' '+descrizione+' '+prezzo+' '+qta+' '+scheda+' '+disp)
	xhr_rc = '';
	xhr('pgm','add2cart.php?c='+codice+'&d='+descrizione+'&q='+qta+'&p='+prezzo+'&a='+disp+'&k=000','eval')
	timer1 = setTimeout('buy_poll()',500);
	return;
}

function buy_poll() {
	if (xhr_rc == 'ok') {
		clearTimeout(timer1)
		show_alert(xhr_s,340,165,0);
	} else {
		timer1 = setTimeout('buy_poll()',500);
	}
}

function show_alert(s,w,h,v) {
	x = (screen.width-w)/2;
	y = (screen.height-h)/2;
	var up = '';
	if (v == 1) { up = '..\/' };
	parms = 'left=' + x + ',top=' + y + ',width=' + w +',height='+h;
 	myWindow = window.open("", "tinyWindow", "titlebar=0,width="+w+",height="+h+",left="+x+",top="+ y);
 	myWindow.document.write('<html>');
	myWindow.document.write('<head>');
	myWindow.document.write('<title>OK</title>');
	myWindow.document.write('</head>');
	myWindow.document.write('<body background="'+up+'img_sito/popup-compra.jpg"  onload="window.focus()" onblur="window.focus()">');
	myWindow.document.write('<div style="width:50%;float:left;font-family:arial,sans serif;font-size:9pt">');
	myWindow.document.write(s);
	myWindow.document.write('<br clear="all"><br><form><input type="button" value="OK" style="color:white;background-color:green;font-size:0.8em;padding:4px 16px 4px 16px" onclick="window.close()"></form></center>');
	myWindow.document.write('</div>');
	myWindow.document.write('<img src="'+up+'img_sito/carrello-popup.gif" style="float:right;margin-top:15%">');
	myWindow.document.write('</body></html>');  
	myWindow.document.close();  
}


/*** SUBMIT VIA ENTER KEY ***/

function dosubmit(q) {
 		F2.location.href = "pgm/pag.php?cat=QRY&Q=" + escape(q) 
		return false
}

function dorecalc(q) {
 		xhr('pgm' , 'recalc.php?vdfc='+q , 'cartbox_L', 0)
		return false;
}


function entsub(myform) {
	var searcher = '';
	if (window.event && window.event.keyCode == 13) {
		alert('press');
		searcher = trim(F1.document.getElementById('searchf').value);
 		alert(searcher);
		if (searcher !='') {
			parent.F2.location.href = "pgm/_home.php?cat=QRY&Q=" + escape(searcher) ;
		}
	} else {
		return true;
	}
}

/*** HELPERS ***/

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}


function submitform(f){ 
	if (F2.document.getElementById('ibase')) {
		var min = F2.document.getElementById('ibase').value
	} else {
		var min = 0;
		alert('Errore: Carrello vuoto')
		return;
	}
	if (min < 4000) {
		alert("Attenzione: l'ordine minimo è di 40,00 Euro.\nIl tuo ordine è di "+ format2(min) + " Euro." );
		return  
	}
 	var formdata = '', elem = '', empty = 0
 	for(var s=0; s<f.elements.length; s++){
		elem = f.elements[s]
		error = 0
		if (elem.type == 'radio') {
			if (elem.checked == true) {
				formdata += '&' + elem.name + "=" + escape(elem.value)
			}
		} else {
			elem.value = trim(elem.value)
			if (elem.name == 'nome' && elem.value == '') {
				error = 1
				break
			}
			if (elem.name == 'cognome' && elem.value == '') {
				error = 1
				break
			}
			if (elem.name == 'via' && elem.value == '') {
				error = 1
				break
			}
			if (elem.name == 'n' && elem.value == '') {
				error = 1
				break
			}
			if (elem.name == 'cap' && elem.value == '') {
				error = 1
				break 
			}
			if (elem.name == 'cap' && elem.value.length != 5) {
				error = 2
				break
			}
			if (elem.name == 'cap' && checknum(elem) == false) {
				error = 3
				break
			}
			if (elem.name == 'citta' && elem.value == '') {
				error = 1
				break
			}
			if (elem.name == 'prov' && elem.value == '') {
				error = 1
				break
			}
			if (elem.name == 'prov' && elem.value.length != 2) {
				error = 2
				break
			}
			if (elem.name == 'tel' && elem.value == '') {
				error = 1
				break
			}
			if (elem.name == 'tel' && checktel(elem) == false) {
				error = 3
				break
			}
			if (elem.name == 'fax' && checktel(elem) == false) {
				error = 3
				break
			}
			if (elem.name == 'email' && elem.value == '') {
				error = 1 
				break 
			}
			if (elem.name == 'email' && checkemail(elem) == false) {
				error = 5
				break
			}
			if (elem.name == 'ok196' && elem.checked == false) {
				error = 6
				break
			}
			formdata += '&' + elem.name + "=" + escape(striphtml(elem.value))
			if (navigator.appVersion.indexOf("MSIE")!=-1) elem.style.backgroundColor = 'lightyellow';
		}
 	} 
	if (error == 0) {
		formdata += '&bp=0'
		parent.F2.location.href = 'ordine.php?' + formdata.substring(1)  
	} else {
		if (error == 1) {alert('dato obbligatorio:' + elem.name)}
	        if (error == 2) {alert('valore dato errato: '+elem.name )}
		if (error == 3) {alert('introdurre un numero: '+elem.name )}
		if (error == 4) {alert('introdurre il numero telefonico\nsenza caratteri di separazione: '+elem.name )}
		if (error == 5) {alert('indirizzo email errato: '+elem.value )}
		if (error == 6) {alert('per proseguire è necessario dare il consenso\nal trattamento dei dati personali')}
		elem.focus();
		if (navigator.appVersion.indexOf("MSIE")!=-1) elem.style.backgroundColor = 'yellow';
	}
}


function striphtml(oldstring) {
   var newstring = "";
   var intag = false;
   for(var i = 0; i < oldstring.length; i++) {
        if(oldstring.charAt(i) == '<') intag = true;
        if(oldstring.charAt(i) == '>') {
              intag = false;
              i++;
        }
        if(!intag) newstring += oldstring.charAt(i);
   }
   return newstring;
}

function printme() {
 	parent.F2.focus();
// 	top.F1.document.getElementById('testata').style.display = "none";
// 	top.F2.document.getElementById('orderbox').style.display = "none";
 	top.F2.document.getElementById('bpass_form').style.display = "none";
 	alert('print')
//	window.print();
	parent.F2.print();
// 	top.F2.document.getElementById('orderbox').style.display = "";
// 	top.F1.document.getElementById('testata').style.display = "";
	top.F2.document.getElementById('bpass_form').style.display = "";
}

function format2 (num) {
	string = ""+num;
	string = string.substring(0,string.length-2)+","+string.substring(string.length-2,string.length);
	return string;
}

function checknum(field) {
	pattern = /^[0-9]*$/;
	return pattern.test(field.value)
}

function checktel(field) {
	pattern = /^[0-9 ]*$/;
	return pattern.test(field.value)
}

function checkemail(field) {
if (field.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	return true;
else
	return false;
}


function clickbtn(code,what) {
	var IE6 = false
	/*@cc_on
	@if (@_jscript_version < 5.7) 
		var IE6 = true
	@end
	@*/
	formname = 'f_'+code
	if (what == 'buy') {
		elemname = 'submit_'+code
		if (!IE6) new_src = '../img_sito/carrello_click.jpg'
	} else {
		elemname = 'v_'+code
		if (!IE6) new_src = '../img_sito/ico-video_click.gif'
	}
	var b = top.frames['F2'].document.getElementById(elemname)
	if (!IE6) b.src = new_src
	return
}




