// <script language="JavaScript1.2" SRC="js/popup.js"></script>
// onclick="NewWindow('<??>','name','550','350','no');return false;"

var win= null;
function NewWindow(mypage,myname,w,h,scroll,max){
  var winl = ((screen.width-w)/2)-12;
  var wint = (screen.height-h)/2;
  if (max) { maxi = "yes"; } else { maxi = "no"; }
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable='+maxi;
  win=window.open(mypage,myname,settings);
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function EsNumerico( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	var isValid = true;
	var decCount = 0;		// number of decimal points in the string

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for negative numbers (first char == '-')
	// and a single decimal point (any one char in string == '.').   
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
				(numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       
//if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
   } // END for   
   
   	return isValid;
}  // end IsNum


function AddItem (destino){
	NombreTabla = 'tabla_'+destino;
	var n = document.getElementById(NombreTabla).rows.length;
	ContenidoTabla = document.getElementById(NombreTabla);
	fila = document.createElement('tr');
	nombre_id = "Fila_"+destino+"_"+n;
	fila.id = nombre_id
	fila.align = "center";

	celdaA = document.createElement('td'); 
	celdaA.id = "Celda_"+destino+"_"+n+"_A";
	celdaA.width = "20";
	celdaA.innerHTML = '<A HREF="javascript:RemoveItem(\''+nombre_id+'\',\''+NombreTabla+'\');"><IMG SRC="img/ico_del.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="Eliminar Item"></A>';
	fila.appendChild(celdaA); 

	celdaB = document.createElement('td'); 
	celdaB.id = "Celda_"+destino+"_"+n+"_B";
	celdaB.align = "left";
	celdaB.width = "350";
	celdaB.innerHTML = '<INPUT TYPE="text" NAME="descripcion_item[]" id="TXTItem_'+destino+'_'+n+'" style="width:340px;" value="'+document.getElementById('temp_B').value+'" maxlength="240" onDblClick="ItemPlantilla(\'TXTItem_'+destino+'_'+n+'\')">';
	fila.appendChild(celdaB); 

	celdaC = document.createElement('td'); 
	celdaC.id = "Celda_"+destino+"_"+n+"_C";
	celdaC.width = "60";
	celdaC.innerHTML = '<SELECT NAME="moneda[]" id="moneda_'+destino+'_'+n+'"><OPTION VALUE="U$S">U$S</OPTION><OPTION VALUE="$">$</OPTION></SELECT>';
	fila.appendChild(celdaC); 

	celdaD = document.createElement('td'); 
	celdaD.id = "Celda_"+destino+"_"+n+"_D"; 
	celdaD.width = "80";
	celdaD.innerHTML = '<INPUT TYPE="text" NAME="costo_unitario[]" id="costo_unitario_'+destino+'_'+n+'" onChange="calcular(this);" value="'+document.getElementById('temp_D').value+'" OnBlur="calcular(this);" OnChange="calcular(this);" style="width:60px;text-align:right;">';
	fila.appendChild(celdaD); 

	celdaE = document.createElement('td'); 
	celdaE.id = "Celda_"+destino+"_"+n+"_E"; 
	celdaE.innerHTML = "x";
	celdaE.width = "10";
	fila.appendChild(celdaE); 

	celdaF = document.createElement('td'); 
	celdaF.id = "Celda_"+destino+"_"+n+"_F";
	var recalcular_celda = "Celda_"+destino+"_"+n+"_F";
	celdaF.innerHTML = '<INPUT TYPE="text" NAME="cantidad[]" id="cantidad_'+destino+'_'+n+'" style="width:30px;text-align:center;" value="'+document.getElementById('temp_F').value+'" maxlength="2" onChange="calcular(this);" OnBlur="calcular(this);">';
	celdaF.width = "40";
	fila.appendChild(celdaF); 

	celdaG = document.createElement('td'); 
	celdaG.id = "Celda_"+destino+"_"+n+"_G"; 
	celdaG.innerHTML = '<INPUT TYPE="text" NAME="costo_total[]"  value="'+document.getElementById('temp_G').value+'" id="costo_total_'+destino+'_'+n+'" style="width:60px;text-align:right;">';
	celdaG.width = "80";
	fila.appendChild(celdaG); 


	ContenidoTabla.appendChild(fila); 
	//alert(recalcular_celda)
	//calcular(recalcular_celda);

}

function RemoveItem (id,destino){
	letra = destino.substr(6);
	document.getElementById(destino).deleteRow(document.getElementById(id).rowIndex);
	Subtotales(letra);
}

function ItemPlantilla(id){
	NewWindow('listadoitems.php?id='+id,'name','250','350','no');
}

function Subtotales(letra){
	A = 0;
	J = 0;
	var valor = document.forms[0].elements.length-1;
	for(var i=1; i < valor;i++){
		var objeto = document.forms[0].elements[i]
		var nombre_id = objeto.id;
		var principio = nombre_id.substr(0, 13)
		var principio2 = nombre_id.substr(0, 16) //comision_total_
		if (  principio == "costo_total_"+letra ) {
			B = eval (document.getElementById(nombre_id).value)
			C = A + B
			A = C;
			document.getElementById("subtotal_"+letra).value = A;
		}
		if ( principio2 == "comision_total_"+letra  ) {
			L = eval (document.getElementById(nombre_id).value)
			M = J + L
			J = M;
			document.getElementById("subtotal_comision").value = J;
		}
	}
	Z = eval(document.getElementById("subtotal_A").value);
	R = eval(document.getElementById("subtotal_comision").value);
	X = eval(document.getElementById("subtotal_B").value);
	Y = eval(Z + X + R);
	if ( Y >= 0 ){
		document.getElementById("cotizacion_total").value = Y;
	}

}

function calcular(obj){
	if (obj.name == "cantidad[]" ){
		// cantidad_B_0
		letra = obj.id.substr(9,1);
		linea = obj.id.substr(11);
	}

	if (obj.name == "costo_unitario[]" ){
		// costo_unitario_B_0
		letra = obj.id.substr(15,1);
		linea = obj.id.substr(17);
	}
	subtotal = "subtotal_"+letra;
	resultado = "costo_total_"+letra+"_"+linea
	multiplicar = "cantidad_"+letra+"_"+linea
	unitario = "costo_unitario_"+letra+"_"+linea
	if (EsNumerico(obj.value)) {
		if (document.getElementById(multiplicar).value > 0 && document.getElementById(unitario).value > 0 ) {
			document.getElementById(resultado).value = eval(document.getElementById(unitario).value*document.getElementById(multiplicar).value);
			Subtotales(letra);
		}
	} else {
		document.getElementById(resultado).value = "";
	}
	Subtotales(letra)
}

function CalculaPorcentaje(ibj){
	linea = ibj.substr(13) //('porcentaje_B_4');"
	var porc = document.getElementById(ibj).value;
	var subt = document.getElementById("subtotal_A").value;
	var kk = porc * subt;
	valor = kk/100;
	
	if ( valor > 0 ){
		valor = valor * -1;
	}
	
	document.getElementById("comision_total_B_"+linea).value = valor;
	Subtotales('B');
}

function ItemsDefault(){
	RemoveItem('Fila_A_0','tabla_A');
	document.getElementById("temp_B").value = "Tarifa Adulto";
	document.getElementById("temp_F").value = document.getElementById("adulto").value;
	AddItem("A");
	ResetVars ();
	document.getElementById("temp_B").value = "Tarifa Child 2 a 3 años";
	document.getElementById("temp_F").value = document.getElementById("child").value;
	AddItem("A");
	ResetVars ();
	Subtotales('A');

	RemoveItem('Fila_B_0','tabla_B');
	document.getElementById("temp_B").value = "Inscripción Adulto";
	document.getElementById("temp_D").value = document.getElementById("COSTO_INS_ADT").value;
	document.getElementById("temp_F").value = document.getElementById("adulto").value;
	document.getElementById("temp_G").value = document.getElementById("COSTO_INS_ADT").value * document.getElementById("adulto").value;
	AddItem("B");
	ResetVars ();
	document.getElementById("temp_B").value = "Inscripción Menores";
	document.getElementById("temp_D").value = document.getElementById("COSTO_INS_CHD").value;
	document.getElementById("temp_F").value = document.getElementById("child").value;
	document.getElementById("temp_G").value = document.getElementById("COSTO_INS_CHD").value * document.getElementById("child").value;
	AddItem("B");
	ResetVars ();
	document.getElementById("temp_B").value = "Tasas de Embarque";
	document.getElementById("temp_F").value = eval(document.getElementById("adulto").value) + eval(document.getElementById("child").value) + eval(document.getElementById("bebe").value);
	AddItem("B");
	ResetVars ();
	document.getElementById("temp_B").value = "Impuestos";
	document.getElementById("temp_F").value = eval(document.getElementById("adulto").value) + eval(document.getElementById("child").value) + eval(document.getElementById("bebe").value);
	AddItem("B");
	ResetVars ();	
	Subtotales('B');
}
function ResetVars (){
	document.getElementById("temp_B").value = "";
	document.getElementById("temp_D").value = "";
	document.getElementById("temp_F").value = "";
	document.getElementById("temp_G").value = "";
}

function nro_codigo(){
	document.getElementById("comprobante").value = document.getElementById("codigo").value;
}