// JavaScript Document

   function ControllaForm(form)
   {
   
   var controllo = true;
   var message = '';
   
   if(document.registra.email.value == ""){
   
      message += "\nEmail";
      controllo = false;
   
   }
   
     if(document.registra.cl_password.value == ""){
   
      message += "\nPassword";
      controllo = false;
   
   }


   if(document.registra.cl_nome.value == ""){
   
      message += "\nNome";
      controllo = false;
   
   }


   if(document.registra.cl_cognome.value == ""){
   
      message += "\nCognome";
      controllo = false;
   
   }


   if(document.registra.cl_indirizzo.value == ""){
   
      message += "\nIndirizzo";
      controllo = false;
   
   }


   if(document.registra.cl_cap.value == ""){
   
      message += "\nCap";
      controllo = false;
   
   }


   if(document.registra.cl_citta.value == ""){
   
      message += "\nCitt&agrave;";
      controllo = false;
   
   }


   if(document.registra.cl_prov.value == ""){
   
      message += "\nProvincia";
      controllo = false;
   
   }


   if(document.registra.cl_telefono.value == ""){
   
      message += "\nTelefono";
      controllo = false;
   
   }


/*   if(document.registra.cl_fax.value == ""){
   
      message += "\nFax";
      controllo = false;
   
   }*/
   
      if(document.registra.cl_nazione.selectedIndex == 0){
   
      message += "\nNazione";
      controllo = false;
   
   }


      if(document.registra.consenso.checked == false){
   
      message += "\nConsenso al trattamento dei dati personali";
      controllo = false;
   
   }



   
   
   var PIvaOk=partitaIVA(document.registra.cl_codice_fisc_iva.value)
   if (PIvaOk==false)
      {
      message += "\nPartita IVA";
      controllo = false;
      }
	  
	  
	  

	  
	  
	  if (controllo){
	  document.registra.submit();
	  }else{
	  alert("Le seguenti informazioni sono mancanti o non corrette\n\n"+message);
	  }
	  
	
   }







 function PopupPic(sPicURL) { 
                window.open("http://www.dmpromo.it/popup.htm?"+sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=200");
            }


			function slide(pro_id){
				window.open('http://www.dmpromo.it/fotoshow.php?pro_id='+pro_id,'slide','resizable=yes,width=500,height=500,top=20,left=20');
			}
			
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



		function OpenSubMenu(id){
				if(document.getElementById("anchor_menu_"+id)){
					var a = document.getElementById("anchor_menu_"+id);
					var parent = a.parentNode;
					parent.style.display="block";
				}
			 
		}


  function arrotonda(numero,x) {
  return Math.round(numero*Math.pow(10,x))/Math.pow(10,x);
  }


function ToogleRules(){
	
		 if(document.getElementById("BoxSellRules")){
	     var ToogleArticle = document.getElementById("BoxSellRules");
		 
		 var h2 =  ToogleArticle.getElementsByTagName("H4");
	     var div = ToogleArticle.getElementsByTagName("DIV");
		 
		 for(var i=0; i<div.length; i++){
			 div[i].id = "div_"+i;
			 div[i].style.display="none";
		 }
		 
		 for(var i=0; i<h2.length; i++){
			 var title = h2[i].innerHTML;
			 h2[i].innerHTML = "";
			 var a = document.createElement("A");
			 a.id = "a_"+i;
			 a.title = title;
			 
			 a.onclick = function(){
				 var tdiv = document.getElementById("div_"+this.id.substr(2));
				    if(tdiv.style.display=="block"){
					   tdiv.style.display="none";
					}else{
					   tdiv.style.display="block";
					}
			 }
			 
			 a.appendChild(document.createTextNode(title));
			 h2[i].appendChild(a);
		 }
		 /*
		 if(div.length>0){
		    for(var i=0; i<div.length; i++){
			    var a = div[i].getElementsByTagName("A")[0];
				a.id = i;
				a.onclick = function(){
				
				    var p = div[this.id].getElementsByTagName("P")[0];
				    if(p.style.display=="block"){
					   p.style.display="none";
					}else{
					   p.style.display="block";
					}
				
				}
			}
		 }
 */	  }
}



function abrir_janela_popup(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



    function OpenImgZoom(img, w, h){
	         var popupzoom = "";
			 var rand=Math.floor(Math.random()*11);
			 var sPicURL = img;
			 /*
			 popupzoom = window.open('http://www.dmpromo.it/zoom.php?img='+img+'&rand='+rand, 'popup_immagine', 'width='+w+',height='+h+',scrollbars=no,resize=yes,menubar=no,top=100,left=100');
			 */
			window.open( "http://www.dmpromo.it/popup.htm?"+sPicURL, "",  
     "resizable=1,HEIGHT=200,WIDTH=200"); 
	}	
	
	
function ChangeAction(action, FormName){	
         
		 var theForm = document.forms[FormName];
		 theForm.action = action;
		 
		 }
	

function VerifyQtaMin(form){
         
		 var theForm = document.forms[form.id];
         var verify = true;
         var input = getElementsByClassName("input_quantita", "input", document);
		 var action = theForm.action.substr(theForm.action.lastIndexOf("=")+1);
		 
		 if((input.length>0) && document.getElementById("qtaminima") && (action!="acquista_campione")){
		 
			 var qta = 0;
			 var qtaminima = document.getElementById("qtaminima").value;
			 for(var i=0; i<input.length; i++){
				 qta += Number(input[i].value);
			 }
			 
			 /// controllo se è minore di 0
			 if(qta<=0){
				alert("Attenzione: indicare una quantità ordinabile per almeno un articolo!!");
				verify = false;
				return false;
			 }
			 
			 
			 if((qta>0) && (qta>=qtaminima)){
			    verify = true;
				}else{
				alert("Attenzione: la quantità minima ordinabile per questo prodotto è "+qtaminima+"!");
				verify = false;
				return false;
			 }
			 

		 }
		 
		 
		 return verify;
		 
}









/// verifica e controlla le quantità per prodotto

function VerifyChartQta(theForm){
	
	    var controllo = true;
	
	     if(theForm.prodotti_quantita){
	     var prodotti_quantita = theForm.prodotti_quantita.value;
		 var prodotti = prodotti_quantita.split(",");
			 for(var i=0; i<prodotti.length; i++){
				 pro = prodotti[i].split("=");
				 pro_id = pro[0];
				 qtaminima = pro[1];
				 if(pro_id!=""){
					 var qtasel = Number(0);
					 var nomeprodotto = "";
					 var qtainput = getElementsByClassName("qta_"+pro_id, "input", document);
					 

					for(var k=0; k<qtainput.length; k++){
						qtasel += Number(qtainput[k].value);
						nomeprodotto = qtainput[k].parentNode.getElementsByTagName("SPAN")[0].innerHTML;
					}

					 
					 if((qtaminima>qtasel) && (qtainput.length>0)){
						 alert("Attenzione: la quantità complessiva ordinabile del prodotto '"+nomeprodotto.toUpperCase()+"' non può essere inferiore a "+qtaminima+"!");
						 controllo  = false;
						 return false;
					 }
				 }
			 }
		 }
		 
	     return controllo;
}



function OpenPopupUploadFile(url, nome_articolo){
		 
		 var urlpopup = url+"?nome_articolo="+nome_articolo;
		 
		 window.open(''+urlpopup+'','uploadfile','width=860,height=560 ,toolbar=no, location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');
					
}


function AttachCSS(cssfile){

		var headID = document.getElementsByTagName("head")[0];         
		var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = 'css/'+cssfile;
		cssNode.media = 'all';
		headID.appendChild(cssNode);
		
		var elm = document.getElementById("BoxPreventivo");
		elm.style.display = "block";
	
}


function VerifyDimPage(){
	     var elm = document.getElementById("PagePrint");
}


var newwindow = null;

function OpenPopupPreventivo(){
	
	     /// w = 695

		 var elm = document.getElementById("BoxPreventivo");
		 var BoxDatiAzienda = "";
		 
		 //// dati azienda
		 var referente = document.forms["DatiAzienda"].referente_azienda.value;
		 var azienda = document.forms["DatiAzienda"].nome_azienda.value;
		 var cap = document.forms["DatiAzienda"].cap_azienda.value;
		 var indirizzo = document.forms["DatiAzienda"].indirizzo_azienda.value;
		 var citta = document.forms["DatiAzienda"].citta_azienda.value;
		 var provincia = document.forms["DatiAzienda"].provincia_azienda.value;
		 var telefono = document.forms["DatiAzienda"].telefono_azienda.value;
		 var fax = document.forms["DatiAzienda"].fax_azienda.value;
		 var email = document.forms["DatiAzienda"].email_azienda.value;
		 var partitaiva = document.forms["DatiAzienda"].partitaiva_azienda.value;
		 var codicefiscale= document.forms["DatiAzienda"].codicefiscale_azienda.value;
		 
		 
		 
         var html = elm.innerHTML;	
		 var h = getSizeWindow("h");
		 var w = 720;
		 
		 
		 
		 var pw = Math.floor((screen.width-w)/2);
	     var ph = Math.floor((screen.height-h)/2);
		 
		 if (newwindow && !newwindow.closed){
			
		   newwindow.focus(); 
		   newwindow.document.clear();
		 
		} else {

			newwindow=window.open('','','width='+w+', heigth='+h+', top=20,left=20,resizable=0,scrollbars=yes, menubar=no, toolbars=yes');
		}
		
		

			BoxDatiAzienda += '<ol>';
		    BoxDatiAzienda += '<li><label>Referente:</label><strong>'+referente+'&nbsp;</strong></li>';
			BoxDatiAzienda += '<li><label>Azienda:</label><strong>'+azienda+'&nbsp;</strong></li>';
			BoxDatiAzienda += '<li><label>P.IVA:</label>'+partitaiva+'&nbsp;</li>';
			BoxDatiAzienda += '<li><label>Cod.Fiscale:</label>'+codicefiscale+'&nbsp;</li>';
			BoxDatiAzienda += '<li><label>Indirizzo:</label>'+cap+' '+indirizzo+' &nbsp;</li>';
			BoxDatiAzienda += '<li><label>Città:</label>'+citta+' '+provincia+'&nbsp;</li>';
			BoxDatiAzienda += '<li><label>Telefono:</label>'+telefono+'&nbsp;</li>';
			BoxDatiAzienda += '<li><label>Fax:</label>'+fax+'&nbsp;</li>';
			BoxDatiAzienda += '<li><label>Email:</label>'+email+'&nbsp;</li>';
			BoxDatiAzienda += '</ol>';

		
			newwindow.document.writeln('<head><title>DM Promo s.a.s. - www.dmpromo.it<\/title>');
			newwindow.document.writeln('<link href="css/preventivo-print.css" rel="stylesheet" type="text/css" \/>');
			//newwindow.document.writeln('<\/head><body onLoad="AttachCSS(\'preventivo-print.css\');">');
			newwindow.document.writeln('<\/head><body>');
			newwindow.document.writeln('<div align="center"><div id="BoxPreventivo">');
			
			newwindow.document.writeln('<div id="HeaderPreventivo">');
			
			newwindow.document.writeln('<div id="DatiDMPromo">');
			newwindow.document.writeln('<strong>DMPromo s.a.s.</strong><br>');
			newwindow.document.writeln('Via Guaiane 37/A<br>30020 Noventa di Piave (VE)<br>');
			newwindow.document.writeln('P.IVA 03631850272<br>');
			newwindow.document.writeln('Tel: 0421/65008 - Fax: 0421/309717<br>');
			newwindow.document.writeln('E-mail: info@dmpromo.it - Web: www.dmpromo.it<br>');
			newwindow.document.writeln('Numero verde: 800 91 05 35<br>');
			newwindow.document.writeln('</div>');
			
			newwindow.document.writeln('</div>');
			
			newwindow.document.writeln(html);
			
			newwindow.document.writeln('<div id="BoxEndPreventivo" class="tablePrevOrange">');
			newwindow.document.writeln('<h4>Dati del Cliente</h4><table border="0" width="100%"><tr>');
			newwindow.document.writeln('<td width="50%" valign="top">Prima di inviare copia del seguente preventivo, è necessaria la compilazione dei dati qui sotto.<br><br>'+BoxDatiAzienda+'</td>');
			newwindow.document.writeln('<td width="50%" valign="top">Rimaniamo in attesa di un Vs. gentile riscontro.<br> Potete inviare copia del seguente preventivo sia tramite Fax al n°<strong>0421 309717</strong> che tramite e-mail: <strong>info@dmpromo.it</strong>.<br><br><br><br><br><br><br><br><br><br><br>------------------------------------------------------------------------<br>Timbro e firma per accettazione</td>');
			newwindow.document.writeln('</tr></table>');
			newwindow.document.writeln('</div>');
			newwindow.document.writeln('</div></div>');
			newwindow.document.writeln('<\/body><\/html>');
			newwindow.document.close();
			newwindow.print();
		
         
		 
}



function getSizeWindow(dimension) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  if(dimension=="w"){
	  return myWidth;
  }else if(dimension=="h"){
	  return myHeight;
  }

}



function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}








function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function YY_checkform() { //v4.71
//copyright (c)1998,2002 Yaromat.com
  var a=YY_checkform.arguments,oo=true,v='',s='',err=false,r,o,at,o1,t,i,j,ma,rx,cd,cm,cy,dte,at;
  for (i=1; i<a.length;i=i+4){
    if (a[i+1].charAt(0)=='#'){r=true; a[i+1]=a[i+1].substring(1);}else{r=false}
    o=MM_findObj(a[i].replace(/\[\d+\]/ig,""));
    o1=MM_findObj(a[i+1].replace(/\[\d+\]/ig,""));
    v=o.value;t=a[i+2];
    if (o.type=='text'||o.type=='password'||o.type=='hidden'){
      if (r&&v.length==0){err=true}
      if (v.length>0)
      if (t==1){ //fromto
        ma=a[i+1].split('_');if(isNaN(v)||v<ma[0]/1||v > ma[1]/1){err=true}
      } else if (t==2){
        rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");if(!rx.test(v))err=true;
      } else if (t==3){ // date
        ma=a[i+1].split("#");at=v.match(ma[0]);
        if(at){
          cd=(at[ma[1]])?at[ma[1]]:1;cm=at[ma[2]]-1;cy=at[ma[3]];
          dte=new Date(cy,cm,cd);
          if(dte.getFullYear()!=cy||dte.getDate()!=cd||dte.getMonth()!=cm){err=true};
        }else{err=true}
      } else if (t==4){ // time
        ma=a[i+1].split("#");at=v.match(ma[0]);if(!at){err=true}
      } else if (t==5){ // check this 2
            if(o1.length)o1=o1[a[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!o1.checked){err=true}
      } else if (t==6){ // the same
            if(v!=MM_findObj(a[i+1]).value){err=true}
      }
    } else
    if (!o.type&&o.length>0&&o[0].type=='radio'){
          at = a[i].match(/(.*)\[(\d+)\].*/i);
          o2=(o.length>1)?o[at[2]]:o;
      if (t==1&&o2&&o2.checked&&o1&&o1.value.length/1==0){err=true}
      if (t==2){
        oo=false;
        for(j=0;j<o.length;j++){oo=oo||o[j].checked}
        if(!oo){s+='* '+a[i+3]+'\n'}
      }
    } else if (o.type=='checkbox'){
      if((t==1&&o.checked==false)||(t==2&&o.checked&&o1&&o1.value.length/1==0)){err=true}
    } else if (o.type=='select-one'||o.type=='select-multiple'){
      if(t==1&&o.selectedIndex/1==0){err=true}
    }else if (o.type=='textarea'){
      if(v.length<a[i+1]){err=true}
    }
    if (err){s+='* '+a[i+3]+'\n'; err=false}
  }
  if (s!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+s)}
  document.MM_returnValue = (s=='');
}





function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands
    //
    // version: 906.1806
    // discuss at: http://phpjs.org/functions/number_format
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +     input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +     improved by: davook
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     input by: Jay Klehr
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *     example 10: number_format('1.20', 2);
    // *     returns 10: '1.20'
    // *     example 11: number_format('1.20', 4);
    // *     returns 11: '1.2000'
    // *     example 12: number_format('1.2000', 3);
    // *     returns 12: '1.200'
    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

