var nn6=document.getElementById&&!document.all;
document.onclick = naviga;
var imgload = new Image();
imgload.src = 'images/loading.gif';


function naviga(e){
	if (e == null) { e = window.event; htype='move';} 
	var target = e.target != null ? e.target : e.srcElement;
	var fobj = target;
	var topelement = nn6 ? "HTML" : "BODY";
	
	while (fobj.tagName != topelement && fobj.id != "categories" && fobj.parentNode)
		fobj = nn6 ? fobj.parentNode : fobj.parentElement;
	if(target.tagName == 'LI' && fobj.id == "categories"){
		var curLeft = parseInt(target.style.marginLeft);
		if(target.nextSibling){
			target.nextSibling.style.display == 'none' ? disp = 'block' : disp = 'none';
		}
		while(target.nextSibling){
			target = target.nextSibling;
			
			if(disp == 'none'){
				if(parseInt(target.style.marginLeft) > curLeft)
					target.style.display = disp;
			}else{
				if(parseInt(target.style.marginLeft) == curLeft+10)
					target.style.display = disp;
			}
			
			
			if(parseInt(target.style.marginLeft) == curLeft){
				return false;
			}
		}
	}
}

var nImg = 0;
function fotoGal(w){
	var img = document.getElementById('imgGal');
	var images = new Array();
	images = img.name.split(';');
	if(w=='next'){
		if(nImg < images.length - 1)
			nImg++;
		else
			nImg = 0;
	}else{
		if(nImg > 0)
			nImg--;
		else
			nImg = images.length - 1;
	}
	img.src = '/'+images[nImg];
	
	//integrazione con il lightbox
	if(document.getElementById('p5-imgContent')){
		var el = document.getElementById('imgGal');
		if(el.src.indexOf('nothumb.gif') == -1){
			editPrev(el.src.replace('thumbs/',''),el.title);
			document.getElementById('p5-contatore').innerHTML = nImg + 1 + ' of ' + totImages;
		}
	}
}

function addQty(qta,id){
	window.location = 'yourcart.php?edit&item='+id+'&qta='+qta;
}

function submitCart(){
		window.location = 'shipping.php';
}

function isMail(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(address) == false)
		return false;
	else
		return true;
}
function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function highlight(el){
	el.style.border = '1px solid #F00';
}
function verifyData(n){
	var form = document.formInfo;
	var error = '';
	
	if(n)
		message = 'All fields are required';
	else
		message = 'All fields in "Registering info" are required';
		
	
	if(isMail(form.email.value) == false) error = 'Incorrect E-mail address';
	
	if(trim(form.name.value) == ''){
		error = message;
		highlight(form.name);
	}
	if(trim(form.surname.value) == ''){
		error = message;
		highlight(form.surname);
	}
	if(trim(form.email.value) == ''){
		error = message;
		highlight(form.email);
	}
	if(trim(form.address.value) == ''){
		error = message;
		highlight(form.address);
	}
	if(trim(form.cap.value) == ''){
		error = message;
		highlight(form.cap);
	}
	if(trim(form.city.value) == ''){
		error = message;
		highlight(form.city);
	}
	if(trim(form.prov.value) == ''){
		error = message;
		highlight(form.prov);
	}
	
	if(form.idstato)
		if(trim(form.idstato.value) == ''){
			error = message;
			highlight(form.idstato);
		}
	
	//controllo l'indirizzo alternativo
	if(form.idstato2){
		if(trim(form.idstato2.value) != '' || trim(form.name2.value) != '' || trim(form.surname2.value) != '' || trim(form.address2.value) != '' || trim(form.cap2.value) != '' || trim(form.city2.value) !='' || trim(form.prov2.value)!=''){
		
			message = 'All fields in "Ship to" are required';
			
			if(trim(form.idstato2.value) == ''){
				error = message;
				highlight(form.idstato2);
			}
			if(trim(form.name2.value) == ''){
				error = message;
				highlight(form.name2);
			}
			if(trim(form.surname2.value) == ''){
				error = message;
				highlight(form.surname2);
			}
			if(trim(form.address2.value) == ''){
				error = message;
				highlight(form.address2);
			}
			if(trim(form.cap2.value) == ''){
				error = message;
				highlight(form.cap2);
			}
			if(trim(form.city2.value) == ''){
				error = message;
				highlight(form.city2);
			}
			if(trim(form.prov2.value) == ''){
				error = message;
				highlight(form.prov2);
			}
		}
	}
	
	
	if(error != ''){
		alert(error);
		return false;
	}else{
		return true;
	}
}

function subm(){
	if(document.forms['paypal_form'])
		document.forms['paypal_form'].submit();
}

function sear(){
	var escaped = escape(document.getElementById('search').value);
	window.location='/search.php?search='+escaped+'&id='+document.getElementById('idfornitore').value;
}

function ppaddress(){
		var button = document.getElementById('buttonConfirm');
	if(document.formPay.pmethod[0].checked == true){
		button.style.backgroundImage = 'url(images/btn_xpressCheckout.gif)';
		button.style.width= '145px';
		button.style.height= '45px';
		button.innerHTML = '';
	}else{
		button.style.backgroundImage = '';
		button.style.width= '';
		button.style.height= '';
		button.innerHTML = 'Confirm Order';
	}
}
function prevGal(){
	var el = document.getElementById('imgGal');
	if(el.src.indexOf('nothumb.gif') == -1)
		startPrev(el.src.replace('thumbs/',''),el.title);
}
function showhide(id){
	el = document.getElementById(id);
	if(el.style.display == '' || el.style.display=='none')
		el.style.display = 'block';
	else
		el.style.display = 'none';		
}
function searchVal(label,val){
	document.getElementById('searchField').innerHTML = 'Search by manufacturer '+label;
	document.getElementById('idfornitore').value = val;
	showhide('searchList');	
}

var scrollTime = false;
var restart = false;

function hscrollzInitialize(){
	if(document.getElementById('hscrollContent')){
		var cont = 0;
		var width = 0;
		var els = document.getElementById('hscrollContent').getElementsByTagName('DIV');
		if(els.length > 0){
			for(var i=0;i<els.length;i++)
				if(els[i].className == 'item')
					cont++;
					
			width = (160 * cont) + 20;	
			document.getElementById('hscrollContent').style.width = width + 'px';
			if(cont > 3)
				hscrollz();
			else
				fadeIn('hscrollContent',0);
		}
	}
}

function hscrollz(){
	if(document.getElementById('hscrollContent')){
		clearTimeout(restart);
		clearTimeout(scrollTime);
		var content = document.getElementById('hscrollContent');
		var hscroll = document.getElementById('hscroll');
		
		var curitem = content.firstChild;
		while(curitem.nodeType == 3)
			curitem = curitem.nextSibling;
			
		itemWidth = curitem.offsetWidth;
		
		if(content.style.visibility != 'visible'){
			fadeIn('hscrollContent',0);
			return false;
		}
		var end = content.offsetWidth;
		var currentScroll = hscroll.scrollLeft;
		var contentWidth = hscroll.offsetWidth;
	
		if(parseInt(currentScroll) % itemWidth == 0 && parseInt(currentScroll) > 0){
			content.removeChild(curitem);
			content.appendChild(curitem);
			hscroll.scrollLeft = 0;
			currentScroll = 0;
		}
		
		if(currentScroll + contentWidth <end){
			hscroll.scrollLeft = currentScroll + 1;
			scrollTime = setTimeout('hscrollz()',45);
		}else{
			scrollTime = setTimeout("fadeOut('hscrollContent',100)",2000);
		}
	}
}
function hscrollzpause(n){
	if(n == 1){
		clearTimeout(scrollTime);
		clearTimeout(restart);
		scrollTime = 'pause';
	}else{
		if(scrollTime == 'pause'){
			clearTimeout(scrollTime);
			restart = setTimeout("hscrollz()",500);
		}
	}
}

function fadeIn(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		obj.style.visibility = 'visible';
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
		}else{
			scrollTime = setTimeout('hscrollz()',2000);
		}
	}
}

function fadeOut(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity >= 0) {
			setOpacity(obj, opacity);
			opacity -= 10;
			window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
		}else{
			document.getElementById('hscrollContent').style.visibility = 'hidden';
			document.getElementById('hscroll').scrollLeft = 0;
			hscrollz();
		}
	}
}
function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	obj.style.filter = "alpha(opacity:"+opacity+")";// IE/Win
	obj.style.KHTMLOpacity = opacity/100;// Safari<1.2, Konqueror
	obj.style.MozOpacity = opacity/100;// Older Mozilla and Firefox
	obj.style.opacity = opacity/100;// Safari 1.2, newer Firefox and Mozilla, CSS3
}

function nospam(host,name){
	window.location = 'mailto:'+name+'@'+host;	
}
function racc(n){
	//var checked = document.getElementById('racc').checked;
	
	if(n==1)
		window.location = "orderdetail.php?racc";
	else
		window.location = "orderdetail.php";
}
function get(url,id,func) {
	loading();
	  if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	  }
	  if (x) {
		x.onreadystatechange = function() {
		  if (x.readyState == 4 && x.status == 200) {
			var text = x.responseText;
			if(id){
				el = document.getElementById(id);
				el.innerHTML = text;
			}
			
			if(func)
				eval(func);
			
			loading();
			
		  }
		}
		x.open("GET", url, true);
		x.send(null);
	  }
}
function loading(){
	
}
