/*
	==========================================================================
	Module: ValidateForms.js
	==========================================================================
	==========================================================================
	The Software, including this file, is subject to the End User License
	Agreement.
	Author: Aaron E. Quiroz De la Luz 
	Modify by:
	Copyright (c) 2002, Syndein S.A. de C.V., All Rights Reserved.
	==========================================================================
*/

var NN4 = document.layers? true : false;
var IE4 = document.all? true : false;

var nSelectSize = "0";
var nCtrlAdd = 0;

function Printer()
{
 window.print();
}

//params 
// ValueSelect = Valor de control
// ValueComp   = Valor a comparar
// szLayer     = Nombre del layer
// niLeft	   = Posicion
// niTop	   = Posicion
function validateOption(ValueSelect,ValueComp,oTex,szLayer,niLeft1024,niTop1024,niLeft800,niTop800){

var x,y;

	if (ValueSelect==ValueComp){
		oTex.value="";
		x=ValidateResolutionLeft(niLeft1024,niLeft800);
		y=ValidateResolutionTop(niTop1024,niTop800);
		display(szLayer,x,y);
		}
	else{
		hide(szLayer);
		oTex.value=ValueSelect;
	}
}

function returnForm(){

	if (IE4) {
		var f = document.forms[0];
	}else if (NN$) {
		var f = document.forms[0];
	}
	return f;
}

function getFormFromFrame(nForm,nFrame){
	if (IE4) {
		var frm=frames.parent.frames[nFrame].document.forms[nForm];
	}else if (NN$) {
		 var frm=frames.parent.frames[nFrame].document.forms[nForm];
	}
    return frm;
}

function getFrameFromFrame(nFrame,nForm){
	if (IE4) {
		var frm=frames.parent.frames.parent.frames[nFrame].document.forms[nForm];
	}else if (NN$) {
		 var frm=frames.parent.frames.parent.frames[nFrame].document.forms[nForm];
	}
    return frm;
}


function validateSelect(oSelect,vSComp,oTex,sDesCmp){
	if (oSelect.value==vSComp){
		if (oTex.value==""){
			alert(" Favor de Capturar el Campo : " + sDesCmp );
			return false;
		}
	}
	return true;
}

// Show object in x,y Position
function display(szLayer,niLeft,niTop) {
  if (IE4) {
    document.all[szLayer].style.pixelLeft = niLeft;
    document.all[szLayer].style.pixelTop = niTop;
    document.all[szLayer].style.visibility = "visible";
  }
  else if(NN4) {
    document.layers[szLayer].left = left;
    document.layers[szLayer].top = top;
    document.layers[szLayer].visibility = "show";
  }
}

//Hide Object
function hide(szLayer) {
  if (IE4) {
    document.all[szLayer].style.visibility = "hidden";
  }
  else if(NN4) {
    document.layers[szLayer].visibility = "hidden";
  }
}

function ValidateResolutionLeft(v1024,v800){
	if (screen.width==1024)
	{
		return v1024;
	}
	if (screen.width==800)
	{
		return v800;
	}
}
function ValidateResolutionTop(v1024,v800){

	if (screen.width==1024)
	{
		return v1024;
	}
	if (screen.width==800)
	{
		return v800;
	}
}
function resize(oCtrl, ValueComp, nSize) {
	if (oCtrl.value ==ValueComp) {
		nSelectSize = oCtrl.style.width;
		oCtrl.style.width = nSize;
	}
	else
		if (nSelectSize != "0")
			oCtrl.style.width = nSelectSize;
}

function checkToFalse(obj){
 obj.checked=false;
}
function checkToTrue(obj){
 obj.checked=true;
}

function getEncuestaPregunta(vVal,v1,v2){
 var sTemp=vVal;
 var    arr=sTemp.split(",");
	 v1=arr[0];
	 v2=arr[1];
//	 alert ("" + v1+ " "+ v2);
}
function ChangeChildOp(nVal,obj){
		frm= returnForm();
		for ( var index = 0; index < frm.elements.length; index++) 
		{
			if(frm.elements[index].type == "checkbox")
			{
				sCampo=frm.elements[index].name;
				sarr=sCampo.split("_");
				if(sarr[1] == nVal)
				 {
					frm.elements[index].checked=obj.checked;
				 }
			}
		}
return true;
}

function ValidateChildOp(nVal){
		frm= returnForm();
		for ( var index = 0; index < frm.elements.length; index++) 
		{
			if(frm.elements[index].type == "checkbox")
			{
				sCampo=frm.elements[index].name;
				sarr=sCampo.split("_");
				if(sarr[1] == nVal)
				 {
					if (frm.elements[index].checked==true)
						return true;
				 }
			}
		}
return false;
}
function ChangeParentOp(nVal,obj){
		frm= returnForm();
		for ( var index = 0; index < frm.elements.length; index++) 
		{
			if(frm.elements[index].type == "checkbox")
			{				
				if(frm.elements[index].value == nVal)
				 {
				    if (frm.elements[index].checked==true){
						//alert (""+frm.elements[index].value);
					    if (ValidateChildOp(frm.elements[index].value)==false)
							{frm.elements[index].checked=false;}
				    }
					else{
						frm.elements[index].checked=true;
					}
				 }
			}
		}
return true;
}

// Show object in 
function ShowDiv(szLayer) {
  if (IE4) {
    document.all[szLayer].style.visibility = "visible";
  }
  else if(NN4) {
    document.layers[szLayer].visibility = "show";
  }
}

//Hide Object
function HideDiv(szLayer) {
  if (IE4) {
    document.all[szLayer].style.visibility = "hidden";
  }
  else if(NN4) {
    document.layers[szLayer].visibility = "hidden";
  }
}

function setPointer() {
    if (document.all)
        for (var i=0;i < document.all.length; i++)
             document.all(i).style.cursor = 'wait';
}

function resetPointer() {
    if (document.all)
        for (var i=0;i < document.all.length; i++)
             document.all(i).style.cursor = 'default';
}

/*validación numérica RRV*/
function valNumerica(sValor) {
	var re='[^0-9]';
	sTemp=sValor.toUpperCase();
	nRes=sTemp.search(re);
	if (nRes==-1) {
		return true;
	}
	else {
		alert("Este campo debe contener solo caracteres NUMÉRICOS");
		return false;
	}
}

function getFileName(val)
{
	splitted=new Array();
	file_with_path=val;

	splitted=file_with_path.split("\\");
	splitted.reverse();
//	alert("file name="+splitted[0]);
	sFileName=splitted[0];
	return sFileName;
}

function validFileName(val)
{
	var objRegExp  =  /(^[a-z_0-9 .A-Z]+$)/; 
		if(objRegExp.test(val))
		{
			return true;
		}
		else
		{
			return false;
		}
}

function ValidCheck(obj){
	if (obj.checked==false)
	{
		obj.value="N";
	}else
	{
		obj.value="Y";
	}
}

function iccValidCheckNumber(obj)
{
	if (obj.checked==false)
	{
		obj.value="0";
	}else
	{
		obj.value="1";
	}
}

//RRV
function iccReplaceAll(textoRecibido, searchThis, replaceWith) 
{
	if(searchThis=="")
	{
		alert("Favor de introducir un caracter para su reemplazo");
		return false;
	}
	if(replaceWith=="")
	{
		replaceWith=" ";
	}
	
	strTemp = "" + textoRecibido;
	strRet="";
	while (strTemp.search(searchThis) > -1)
		{
			nPosFin=strTemp.search(searchThis);
			strRet+=strTemp.substr(0,nPosFin);
			nPosFin +=1;
			strRet += replaceWith;
			strTemp= strTemp.substr(nPosFin);
		}
strRet+= strTemp;
//document.frm.string.value=strRet;
return strRet;
}

function iccValidCantidad(obj){
	var nCant=obj.value;
	if (isNaN(nCant*1.0)){
		alert ('La cantidad requiere valores numericos');
		obj.focus();
		return false;
	}
	if (nCant<0){
		alert ('La cantidad requiere ser mayor de cero');
		return false;
	}
	return true;
}

function iccDiscount(desc, precio){
	var sPercent = "";
	var arrPercent ;
	var nPercent=0;
	if (desc>0)
	{
		sPercent = "" + ((desc/100)*precio );
		arrPercent = sPercent.split(".");
		nPercent=arrPercent[0] +"." + arrPercent[1].substr(0,2);
	}else
	 nPercent=0;
//	alert ("" + nPercent );
	return nPercent;
}

function iccAgregarProductosSIM(){
	var frm= returnForm();


	var sCod		="";
	var sPzscaja	="";
	var sPrecio		="";
	var sPrecioUnitarioConDescuento		="";
	var sL_PrecioUnitarioConDescuento		="";
	var sDesc		="";
	var sPesoUnit	="";
	var sUPeso		="";
	var sVolUnit	="";
	var sUVol		="";
	var sGenetica	="";
	var sUOM		="";
	var sCantidad	="";
	var sImporte	="";
	var sCubicaje	="";
	var sDescuento	="";
	var nDescPor	=0;
	var sDescuentoPrecioUnitario="";
	var sImpCDesc    ="";
	/*Campos Adicionales*/
	var sLinea="";
	var sLineaCodigo="";

	var sPrecioTransferencia="";
	var sFactorSegundas="";
	var sGvv="";
	var sPiezasExplosionadas="";




	var sComma="";
	var nItemsSelected=0;

		/*Agregar los productos que tienen cantidad mayor a cero*/
		for ( var index = 0; index < frm.elements.length; index++) 
		{
			if(frm.elements[index].type == "text")
			{	
//				alert('nombre:'+frm.elements[index].name.substr(0,13));
				if(frm.elements[index].name.substr(0,13) == "ICC_CANTIDAD_")
				{
//					alert('id:'+frm.elements[index].name.substr(13));
					id=frm.elements[index].name.substr(13);
					nCantidad=frm.elements[index].value;
					nCantidad=nCantidad*1.0;
					if (isNaN(nCantidad)){
						alert('La cantidad debe ser un numerico mayor de cero ');
						frm.elements[index].focus();
						return false;
					}else
					{
						
						if( nCantidad >0 )
						{
							valItem =eval('frm.ICC_ITEM_'+ id + '.value');
//							alert('info:'+ valItem);

							arrVal=valItem.split("°");
							i=0;
							sCod		+= (sComma + arrVal[i++]);
//							alert("Cod:"+sCod);
							sPzscaja	+= (sComma + arrVal[i++]);
//							alert("sPzscaja:"+sPzscaja);
							nPrecioU	=arrVal[i++]*1.0;
							sPrecio		+= (sComma + nPrecioU);
//							alert("sPrecio:"+sPrecio);
							sPrecioUnitarioConDescuento		= arrVal[i++];
							sL_PrecioUnitarioConDescuento += (sComma + sPrecioUnitarioConDescuento );
//							alert("sPrecioUnitarioConDescuento:"+sPrecioUnitarioConDescuento);
							sDesc		+= (sComma + arrVal[i++]);
//							alert("sDesc:"+sDesc);
							nPesoU		= arrVal[i++];
							sPesoUnit	+= (sComma + nPesoU);
//							alert("sPesoUnit:"+sPesoUnit);
							sUPeso		+= (sComma + arrVal[i++]);
//							alert("sUPeso:"+sUPeso);
							nVolU		= arrVal[i++];
							sVolUnit	+= (sComma + nVolU);
//							alert("sVolUnit:"+sVolUnit);
							sUVol		+= (sComma + arrVal[i++]);
//							alert("sUVol:"+sUVol);
							sGenetica	+= (sComma + arrVal[i++]);
//							alert("sGenetica:"+sGenetica);
							sUOM		+= (sComma + arrVal[i++]);
//							alert("sUOM:"+sUOM);
							nDescPor=arrVal[i++]*1.0;
							if (isNaN(nDescPor))
							{
								nDescPor=0;
							}
							sDescuento+= (sComma + nDescPor);
//							alert("sDescuento:"+sDescuento);
							sCantidad	+= (sComma + ("" + nCantidad));
//							alert("sCantidad:"+sCantidad);
							sImporte	+= (sComma + (nPrecioU * nCantidad));
//							alert("sImporte:"+sImporte);
							if (nNegocio==562){
							//aeq 2 	nU= nPesoU;
								nU= nPesoU/1000;
							}else{
								nU=nVolU;
							}
							nCub= (nU*1.0)*(nCantidad);
							sCubicaje	+= (sComma + nCub);
							if (nDescPor>0)
							{
							// Valdiar como hacer la operacion
								//nDescArt=(nDescPor/100)*(nPrecioU * nCantidad)
								nDescArt=iccDiscount(nDescPor,nPrecioU) ; // Descuento al precio unitario
							    nPrecioUnitarioConDescuento=sPrecioUnitarioConDescuento* 1.00;
								nImpCDesc=(nPrecioUnitarioConDescuento*nCantidad)   ;
							}else{
								nDescArt=0;
								nImpCDesc=(nPrecioU * nCantidad);
							}
							sDescuentoPrecioUnitario+= (sComma + nDescArt);
							sImpCDesc+= (sComma + nImpCDesc);
							//alert("sImpCDesc:"+sImpCDesc + " nImpCDesc:"+nImpCDesc);
//							alert("sCubicaje:"+sCubicaje);
							/*Campos Adicionales*/
							sLinea+= (sComma + arrVal[i++]);
							sLineaCodigo+= (sComma + arrVal[i++]);
							//alert("sLinea:"+sLinea);
							//alert("sLineaCodigo:"+sLineaCodigo);
							sPrecioTransferencia+= (sComma + arrVal[i++]);
							sFactorSegundas+= (sComma + arrVal[i++]);
							sGvv+= (sComma + arrVal[i++]);
							sPiezasExplosionadas+= (sComma + arrVal[i++]);


							sComma="|";
							nItemsSelected++;
							
						}
					}
				}
			}
		}

//alert('Termino');
		if(nItemsSelected<1){
			alert('No hay productos que la cantidad sea mayor que cero para agregarlo al Pedido.');
			return false;
		}

		frm.T_ICC_CODIGO.value			=sCod;
		frm.T_ICC_CUBICAJE.value		=sCubicaje;
		frm.T_ICC_PIEZASCAJA.value		=sPzscaja;
		frm.T_ICC_PRECIOITEM.value		=sPrecio;
		frm.T_ICC_PRECIOUNITARIOCONDESCUENTO.value			=sL_PrecioUnitarioConDescuento;
		frm.T_ICC_DESCRIPCION.value		=sDesc;
		frm.T_ICC_PESO_UNITARIO.value	=sPesoUnit;
		frm.T_ICC_UNIDAD_PESO.value		=sUPeso;
		frm.T_ICC_VOL_UNITARIO.value	=sVolUnit;
		frm.T_ICC_UNIDAD_VOL.value		=sUVol;
		frm.T_ICC_GENETICA.value		=sGenetica;
		frm.T_ICC_UNIDADMEDIDA.value	=sUOM; 
		frm.T_ICC_CANTIDAD.value		=sCantidad;
//		alert('Cantidad:'+sCantidad);
		frm.T_ICC_CANTIDADTOTAL.value	=sImporte;
		frm.T_ICC_DESCUENTO.value		=sDescuento;
//		alert('Descuento:'+sDescuento);
		frm.T_ICC_DESCUENTO_IMPORTE.value=sDescuentoPrecioUnitario;//Es el descuento al precio unitario
		frm.T_ICC_IMPORTE_CON_DESCUENTO.value=sImpCDesc;
		/*Campos  Adicionales*/
		frm.T_ICC_LINEA.value=sLinea;
		frm.T_ICC_LINEA_CODE.value=sLineaCodigo;

		frm.T_ICC_PRECIO_TRANSFERENCIA.value=sPrecioTransferencia;
		frm.T_ICC_FACTOR_SEGUNDAS.value		=sFactorSegundas;
		frm.T_ICC_GVV.value					=sGvv;
		frm.T_ICC_PIEZAS_EXPLOSIONADAS.value=sPiezasExplosionadas;


//alert ('Asignados ...');
	ValidForm();
return false;
}


