var stringToSplit = '';

//Formatea con puntito de miles
function fFormatoEntero(numero){
  var salida = '';
  var l = numero.length;
	var s = 3;
	var i = 0;
	var punto = '';
	if(!isNaN(Number(numero)) && l>0){
	  numero = parseFloat(numero);
	  numero = String(numero);
	  l = numero.length;
		for(i=l;i>0;i=i-3){
		  if(i<3){s=i;};
		  salida = numero.substr(i-s, s) + punto + salida;
		  punto = '.';
		}
	}
	else{
	  salida = 0;
	}
  return salida;
}
//Desformatea el numero
function fDesFormatoEntero(numero){
  return numero.replace(/(\.)/gi, '');
}
function sinFoto(img){
	if(document.all){
		img.src = 'images/px.gif';
	}
}
function fVentFicha(id){
	window.name = 'principal';
	javascript:window.open('default.asp?id=337&idProd=' + id, '_blank', 'width=600,height=500,left=150,top=150,scrollbars=1')
}

/*function fVentFicha(id){
	window.open('default.asp?id=337&idProd=' + id, '_blank', 'width=580;height=500;left=100;top=100');
	//javascript:window.open('default.asp?id=337&idProd=' + id, '_blank', 'width=580;height=500;left=100;top=100');
}
*/

function fFormatear(id){
  var obj = document.getElementById('id' + id);
  obj.innerHTML = fFormatoEntero(obj.innerHTML);
}
function fFormatearOf(id){
  var obj = document.getElementById('p' + id);
  obj.innerHTML = fFormatoEntero(obj.innerHTML);
  
  var obj = document.getElementById('o' + id);
  obj.innerHTML = fFormatoEntero(obj.innerHTML);
}

function fFormatearProducto(id){
  var obj = document.getElementById('id' + id);
  obj.innerHTML = fFormatoEntero(obj.innerHTML);
}

function Asignar(v){
	obj = document.getElementById('tC_' + v);
	//obj = eval('document.fFormulario.tC_' + v);
	if(obj.value.indexOf('<')>-1 || obj.value.substr(0,1)=='<'){
		obj.value = '';
		return;
	}
	if(!isInteger(obj.value) && obj.value!=''){
		alert('Debe ingresar sólo números en las cantidades de productos, gracias.');
		obj.value = '';
	}
		
	var sInner = '';
	var subTotal = 0;
	var sDescuento = 0;
	var sTotalsIva = 0;		
	sInner = sInner + '<table width="100%" border="0" cellspacing="0" cellpadding="2">';
	
	arrayOfStrings = stringToSplit.split(',')
	var sP = '';
	document.fFormulario.hDetalle.value = '';
	for (var i=0; i<arrayOfStrings.length; i++) {
		obj = document.getElementById('tC_' + arrayOfStrings[i]);
		objProd = document.getElementById('tD_' + arrayOfStrings[i]);
		objPrecio = document.getElementById('tP_' + arrayOfStrings[i]);
		if(obj.value!=0 && obj.value!=''){
			sP = parseInt(objPrecio.value)*parseInt(obj.value);
			sP = new String(sP);
			sP = fFormatoEntero(sP);
			sInner = sInner + '<tr align="left" valign="top">';
			sInner = sInner + '<td width="80%"><font color="#000000" size="2" face="Arial"><b>' + obj.value + ' </b>' + objProd.value + '</font></td>';
			sInner = sInner + '<td width="20%" align="right"><font color="#000000" size="2" face="Arial">$' + sP + '</font> </td>';
			sInner = sInner + '</tr>';
			subTotal = subTotal + (parseInt(objPrecio.value)*parseInt(obj.value));
			if(document.fFormulario.hDetalle.value!=''){document.fFormulario.hDetalle.value = document.fFormulario.hDetalle.value + 'X'}
			document.fFormulario.hDetalle.value = document.fFormulario.hDetalle.value + arrayOfStrings[i] + 'Y' + obj.value
		}
	}
	sInner = sInner + '</table>';
	var objDetalle = document.getElementById('detalle');
	objDetalle.innerHTML = sInner;
	
	var objsTotal = document.getElementById('sTotal');
	objsTotal.innerHTML = subTotal;
	
	var objsPorDes = document.getElementById('PorDescuento');
	objsPorDes.innerHTML = '0%';
	
	var subT = subTotal
	
	//subTotal = parseInt(subTotal * 1.18);
	
	if(subTotal>=100000){
		sDescuento = parseInt(subTotal * 0.15);
		objsPorDes.innerHTML = '15%';
	}
	else{
		if(subTotal>=50000){
			sDescuento = parseInt(subTotal * 0.1);
			objsPorDes.innerHTML = '10%';
		}
		else{
			if(subTotal>=25000){
				sDescuento = parseInt(subTotal * 0.05);
				objsPorDes.innerHTML = '5%';
			}
		}
	}
	var objDescuento = document.getElementById('Descuento');
	objDescuento.innerHTML = sDescuento;
	
	sTotalcIva = subTotal - sDescuento;
	
	var objTotalcIva = document.getElementById('TotalcIva');
	objTotalcIva.innerHTML = sTotalcIva;
	
	objsTotal.innerHTML		= fFormatoEntero(objsTotal.innerHTML);
	objDescuento.innerHTML	= fFormatoEntero(objDescuento.innerHTML);
	objTotalcIva.innerHTML	= fFormatoEntero(objTotalcIva.innerHTML);
	
	//---------------//
	var objTotal = document.getElementById('hT');
	objTotal.value = sTotalcIva;
	
	document.fFormulario.hPD.value = objsPorDes.innerHTML.replace('%','');
	document.fFormulario.hS.value = objsTotal.innerHTML;
	document.fFormulario.hD.value = objDescuento.innerHTML;
	document.fFormulario.hF.value = objTotalcIva.innerHTML;
	
	//objsTotal.value		= '$ ' + objsTotal.value;
	//objDescuento.value	= '$ ' + objDescuento.value;
	//objTotalcIva.value	= '$ ' + objTotalcIva.value;
}

function fClick(){
	opener.document.fFormulario.action = 'sendmail.asp';
	opener.document.fFormulario.target = '_self';
	opener.document.fFormulario.submit();
}

function fAbrir(){
	var oF = document.fFormulario;
	window.open('default.asp?id=335' + '&hS=' + oF.hS.value + '&hD=' + oF.hD.value + '&hF=' + oF.hF.value + '&hT=' + oF.hT.value + '&hPD=' + oF.hPD.value + '&hDetalle=' + String(oF.hDetalle.value) + '&c1=' + String(oF.c1.value) + '&c2=' + String(oF.c2.value) + '&c3=' + String(oF.c3.value) + '&c4=' + String(oF.c4.value) + '&c5=' + String(oF.c5.value) + '&c6=' + String(oF.c6.value) + '&c7=' + String(oF.c7.value) + '&c8=' + String(oF.c8.value), 'Confirmar', 'width=420, height=500, left=150, top=150, scrollbars=1');
}

function fSubmit(){
	if(VerifyOne()){
		window.open('', 'Confirmar', 'width=420, height=500, left=150, top=150, scrollbars=1');
		return true;
	}
	return false;
}

function verFotoGrande(sw){

	if(sw==1){
		var oT = document.getElementById('tFicha');
		oT.style.display = 'none';
	
		var oT = document.getElementById('tFotoGrande');
		oT.style.display = '';
		
		var oT = document.getElementById('tRegresar');
		oT.style.display = '';
	}
	else{
		var oT = document.getElementById('tFicha');
		oT.style.display = '';
	
		var oT = document.getElementById('tFotoGrande');
		oT.style.display = 'none';
		
		var oT = document.getElementById('tRegresar');
		oT.style.display = 'none';
	}

}

function fCambiar(img, sw){
 if(sw==0)
  img.src = img.src.replace('_off', '_on')
 else
  img.src = img.src.replace('_on', '_off');
}

function fMenu(m){
	if(m<5){
		var o = document.getElementById('mnu_0' + m);
		fCambiar(o, 0);
		o.onmouseover='';
		o.onmouseout='';
		o.style.cursor='default';
	}
	
	var o = document.getElementById('Tit');
	o.src = 'images/tit_0' + m + '.gif';
}