// JavaScript Document

function Calculator(titulo,trabajo,material,espesor)

{

	this.titulo		= titulo;

	this.trabajo	= trabajo;

	this.material	= material;

	this.espesor	= espesor;

	this.items		= new Array();



	this.addItem = function (porcentaje,textoProducto,linkProducto,tiempo,ph,obserVACCUMnes)

	{

		u = new Array();



		//fix a javascript bug

		if (porcentaje==0.07 || porcentaje==0.007)

		{

			porcentaje = (porcentaje==0.07)?7:70; //Math.round(Number(porcentaje)*100);

		} else {

			if (porcentaje != "")

			{

				porcentaje = Number(porcentaje)*100;

			} else {

				porcentaje = false;// (textoProducto)	

			}

		}

		

		u['porcentaje'] =  porcentaje;

		u['textoProducto'] = textoProducto;

		u['linkProducto'] = linkProducto;

		u['tiempo'] = tiempo;

		u['ph'] = ph;

		u['obserVACCUMnes'] = obserVACCUMnes;

		this.items.push(u);

	}

	

	this.getHeader = function()

	{

		return "<h1>"+this.titulo+"</h1>";

	}



	this.getMainForm = function()

	{

		var html="<table class='form' cellspacing='0' cellpadding='0'><tr>";

		html +="<td><h2><b>项目:</b> "+this.trabajo+"<br/><b>基础材料:</b> "+this.material+"</h2></td>";

		html +="<td style='width: 160px; border-right:1px solid #a0a0a0; border-left:1px solid #a0a0a0; padding-left: 10px;'><h3>厚度: "+this.espesor+"<br/>种粮 (Kg.): ";

		html +="<input type='text' id='txtpeso' value='' size='6' style='margin-left:3px;margin-rigth:3px;' onKeyUp='calc.calc()'/></td>";

		html +="<td style='padding-left: 10px;width: 215px;'><input type='button' id='calcular' value='计算' onclick='calc.calc()' style='margin-right:3px;'/>";

		html +="<input type='button' id='imprimir' value='印刷' onclick='window.print()' style='margin-right:3px;'/>";

		html +="<input type='button' id='imprimir' value='恢复' onclick='calc.reset();'/>";

		html +="</h2></td></tr></table>";

		return html;	

	}



	this.calc = function ()

	{

		var peso = Number(document.getElementById('txtpeso').value);

		var valor;

		if (peso>=0)

		{

			document.getElementById('txtpeso').style.borderColor="#FFFFFF";

			for (var i=0; i<this.items.length ; i++)

			{

				if (this.items[i]['porcentaje']=="" || this.items[i]['porcentaje']===false)

				{

				} else {

					valor = Number(peso) * Number(this.items[i]['porcentaje']) / 100;

					t = document.getElementById('item_'+i);

					t.value = Math.round(valor * 1000) / 1000;	

				}

			}

		} else {

			document.getElementById('txtpeso').style.borderColor="#FF0000";

			document.getElementById('txtpeso').select();

		}

	}



	this.reset = function ()

	{

		for (var i=0; i<this.items.length ; i++)

		{

			if (this.items[i]['porcentaje']=="" || this.items[i]['porcentaje']===false)

			{

			} else {

				document.getElementById('item_'+i).value = "";

			}

		}

		peso = document.getElementById('txtpeso');

		peso.value = "";

		peso.focus();



	}



	this.buildTable = function ()

	{

		var html = "";

		html +="<table id='calc_items' border='0' cellpadding='3' cellspacing='1' />"

		html +=this.makeTitles();

		for (var i=0; i<this.items.length ; i++)

		{

			if ( this.items[i]['textoProducto']!="--")

			{

				html+= this.makeRow(i);

			} else {

				html+= this.makeRowSep();

			}

			

		}

		html +="</table>";

		html +=this.makeFooter();

		return html;

	}



	this.makeFooter = function ()

	{

		var html ="";

		html +="<p class='calc_footer_msg'>这些配方的好效果依靠我们的产品的使用经验。<br/>";

		html +="如果不是按照我们的配方应用而发生变化就得自己负责效果。 </p>";

		html +="<p class='calc_footer_close'><input type='button' value='關閉' onClick='window.close()' /></p>";

		return html;

	}

	this.makeTitles = function ()

	{

		var html ="<tr class='calc_title'>";

		html+="<td class='calc_title' style='width: 120px'>KGS/GRS</td>";

		html+="<td class='calc_title' style='width: 60px'>%</td>";

		html+="<td class='calc_title'>产品</td>";

		html+="<td class='calc_title' style='width: 50px'>时间</td>";

		html+="<td class='calc_title' style='width: 40px'>Ph</td>";

		html+="<td class='calc_title'>备注</td>";

		html+="</tr>";

		return html;

	}

	this.makeRowSep = function()

	{

		return "<tr class='sep'><td colspan=6 class='sep'>&nbsp;</td></tr>";

	}



	this.makeRow = function (i)

	{

		var html="";

		

		html +="<tr>";

		html +=this.makeTextbox(i);		

		html +=this.makePorcentaje(i);			

		html +=this.makeProducto(i);			

		html +=this.makeTiempo(i);			

		html +=this.makePh(i);			

		html +=this.makeObserVACCUMnes(i);			

		html +="</tr>";



		return html;

	}



	this.makeTextbox = function (i)

	{

		var html ="<td class='center'>";

		if (this.items[i]['porcentaje']=="" || this.items[i]['porcentaje']===false)

		{

			html +="&nbsp;";

		} else {

			html +="<input type='text' size='6' id='item_"+i+"' class='texbox_calculated'>";

		}

		html +="</td>";

		return html;

	}



	this.makePorcentaje = function (i)

	{

		var html ="<td class='right'>";

		if (this.items[i]['porcentaje']=="" && this.items[i]['porcentaje']===false)

		{

			html +="&nbsp;";

		} else {

			if (this.items[i]['porcentaje'].toFixed(2))

			{

				html +=this.items[i]['porcentaje'].toFixed(2)+"%";

			}else{

				html +=this.items[i]['porcentaje']+"%";

			}

			

		}

		html +="</td>";

		return html;

	}



	this.makeProducto = function (i)

	{

		var html ="<td>";

		if (this.items[i]['linkProducto'])

		{

			html +="<a href='"+this.items[i]['linkProducto']+"' title='Documents: "+this.items[i]['textoProducto']+"'>"+this.items[i]['textoProducto']+"</a>";

		} else {

			html +=this.items[i]['textoProducto'];

		}

		html +="</td>";

		return html;

	}



	this.makeTiempo = function (i)

	{

		var html ="<td class='center'>";

		if (this.items[i]['tiempo'])

		{

			html +=this.items[i]['tiempo'];

		} else {

			html +="&nbsp;";

		}

		html +="</td>";

		return html;

	}



	this.makePh = function (i)

	{

		var html ="<td class='center'>";

		if (this.items[i]['ph'])

		{

			html +=this.items[i]['ph'];

		} else {

			html +="&nbsp;";

		}

		html +="</td>";

		return html;



		return html;

	}

	this.makeObserVACCUMnes = function (i)

	{

		var html ="<td>";

		if (this.items[i]['obserVACCUMnes'])

		{

			html +=this.items[i]['obserVACCUMnes'];

		} else {

			html +="&nbsp;";

		}

		html +="</td>";

		return html;

	}

}
