//////////////////////////////////////////////////////////////////////////////
// Funciones en Javascript para la Calculadora
//////////////////////////////////////////////////////////////////////////////
var df = document.forma;
var i  = 0;

// Init de los valores de piso, instalcion, insumos, accesorios
//////////////////////////////////////////////////////////////////////////////

// El costo de los materiales por metro cuadrado
Piso 		= new Array ('Laminate Special','Maypar Laminate','Engineered Floor','Hard Wood');
pPiso		= new Array ( 0.79, 1.99, 3, 4);

// El costo de la instalacion por metro cuadrado
Instalacion 	= new Array ('Install Laminate Special','Install Maypar Laminate','Install Engineered Floor','Install Hard Wood');
pInstalacion		= new Array ( 0, 2, 2, 3, 3);

// El costo de instalacion y materiales por metro lineal
Junquillo	= new Array ('Quarter Round','Quarter Round y Base Board ','Base Board');
pJunquillo  = new Array (0.25, 2.75, 2.5);

Accesorios	= new Array ('Limpiador Concentrado','Laminated Cleaner','Kit de Limpieza');
pAccesporios  = new Array (40, 40, 60);


//////////////////////////////////////////////////////////////////////////////
// *** NO TOCAR CODIGO DE AQUI EN ADELANTE ***
//////////////////////////////////////////////////////////////////////////////

// Agrego los valores al formulario Javascript
//////////////////////////////////////////////////////////////////////////////

// Agrego los tipos de Piso
for (i = 0; i < Piso.length; i++){
	df.tipo_piso[i+1] = new Option( Piso[i], pPiso[i]);
}

// Agrego los tipos de Junquillo
for (i = 0; i < Junquillo.length; i++){
	df.tipo_junquillo[i+1] = new Option( Junquillo[i], pJunquillo[i]);
}

function selector(){
	var i = df.tipo_piso.selectedIndex;
  df.instalacion.checked = true;
}

function calcular(){

  var ancho = df.ancho.value;
  var largo = df.largo.value;
  var coma = 0;

	coma = ancho.indexOf(",");
  // Si el ancho es de la forma A,BC
  if ( coma > -1){ ancho = ancho.substring(0,coma) + "." + ancho.substring(coma+1); }
	ancho = parseFloat(ancho);

  if ( (ancho<1) || isNaN(ancho) ){
	alert('Error:\nPlease input a proper vlue for width.');
	return false;
  }
  // df.ancho.value = ancho;

	coma = largo.indexOf(",");
  // Si el largo es de la forma A,BC
  if ( coma > -1){ largo = largo.substring(0,coma) + "." + largo.substring(coma+1); }
	largo = parseFloat(largo);

  if ( (largo<1) || isNaN(largo) ){
	alert('Error:\nPlease input a proper value for length.');
	return false;
  }
	// df.largo.value = largo;

  var TOTAL = 0;
  var superficie = ( Math.floor(100*largo*ancho)  )/100;
  var lJunquillo = ( Math.floor(200*(ancho+largo)))/100;

  var tPiso = df.tipo_piso[df.tipo_piso.selectedIndex].text;
  var vPiso = df.tipo_piso[df.tipo_piso.selectedIndex].value;

	var vInstalacion = pInstalacion[df.tipo_piso.selectedIndex];

  var tJunquillo = df.tipo_junquillo[df.tipo_junquillo.selectedIndex].text;
  var vJunquillo = df.tipo_junquillo[df.tipo_junquillo.selectedIndex].value;

  // Checkeo si hizo alguna selección

	if (vPiso==0){
	  alert("Error:\nPlease select a floor.");
		return false;
	}

	if (vInstalacion==0){ tInstalacion = "You have not select installation"; }
	if (vJunquillo==0){ tJunquillo = "You have not select moldings"; }

  df.resultado.value  = "Estimate:\nRoom of " + superficie + " sqf, ";
  df.resultado.value += lJunquillo + " ft.\n\n";

  df.resultado.value += "Floor:\t\t$" + (vPiso*superficie);
  df.resultado.value += "\t(" + tPiso + ", " + superficie + " sqf)\n";
  TOTAL += Math.round(vPiso*superficie);

  if (df.instalacion.checked){
  	df.resultado.value += "Installation:\t$" + (vInstalacion * superficie);
	  df.resultado.value += "\t(" + tPiso + ", " + superficie + " sqf)\n";
	  TOTAL += Math.round(vInstalacion*superficie);
	}

  df.resultado.value += "Moldings:\t$" + (vJunquillo * lJunquillo);
  df.resultado.value += "\t(" + tJunquillo + ", " + lJunquillo + " linear ft)\n";
  TOTAL += Math.round(vJunquillo*lJunquillo);


  if (df.opcional1.checked){
	  TOTAL += parseInt(df.opcional1.value);
		df.resultado.value += "Limpiador Conc.\t$" + df.opcional1.value + "\n";
	}

  if (df.opcional2.checked){
	  TOTAL += parseInt(df.opcional2.value);
		df.resultado.value += "Laminated Cleaner\t$" + df.opcional2.value + "\n";
	}

  if (df.opcional3.checked){
	  TOTAL += parseInt(df.opcional3.value);
		df.resultado.value += "Kit de Limpieza\t$" + df.opcional3.value + "\n";
	}


  df.resultado.value += "------------------ -------------------------\n";
  df.resultado.value += "TOTAL:\t\t$" + TOTAL;

}

