// JavaScript Document


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("H2");
	     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";
					}
				
				}
			}
		 }
 */	  }
}


/// 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(){
		  
		  			window.open('http://www.dmpromo.it/upload.php','uploadfile','width=800,height=550 ,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="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(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 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;
}
