/* ********************************************************************************* */
/* *******  Librería de JavaScripts General.js  		      ******* */
/*							*/
/*	AbrirPagina					*/
/*	AbrirAyudaWeb					*/
/*	AbrirImpresion					*/
/*	AbrirNuevaVentana					*/
/*	refresh						*/
/*	SeguridadCampo					*/
/*	ImprimirPagina					*/
/*	ValidarNumero					*/
/*	ValidarDigitos					*/
/*	DatoFaltante					*/
/*	VerificarFecha					*/
/*	trim						*/
/*	Redondear					*/
/* ******************************************************************************** */
function AbrirPagina( formulario, bd, bdreg, frame ) {
	/* Abre página requerida por usuario */
	var argv = AbrirPagina.arguments ;
	var argc = AbrirPagina.arguments.length ;
	var frame = (argc > 3) ? argv[3] : null ;
	UsuarioCookie = GetCookie('Usuario',0) ;		//Usuario
	NumeroCookie = GetCookie('Sesion',1) ;			// Numero
	UsuarioNombreCookie = GetCookie('Usuario',1) ;		//UsuarioNombre
	UsuarioEmailCookie = GetCookie('Usuario',2) ;		//Usuario Email
	elemento = 'http://'+ location.host +'/'+ bdreg ;
	formulario = 'http://'+ location.host +'/'+ bd + formulario ;
	parametros = '&USR=' + UsuarioCookie + '&RDN=' + NumeroCookie + '&NAME=' + UsuarioNombreCookie + 
	'&EMAIL=' + UsuarioEmailCookie + '&PAG=' + formulario ;
	if ( UsuarioCookie != null && NumeroCookie != null && UsuarioNombreCookie != null ) {
		/* ****** si encontro el cookie verifica que el usuario sea valido. */
		elemento = elemento + '/UsuarioVerificar?OpenAgent' ;
	} else {
		/* ****** si no encontro el cookie solicita identificación. */
		elemento = elemento + '/FrmLogin?OpenForm' ;
	}
	if ( frame != null ) {
		window.open(elemento + parametros, frame) ;
	} else {
		location.href = elemento + parametros ;
	}
}
function AbrirAyudaWeb( modulo, bdayuda ) {
	/* Abre página de documentos de Ayuda del Directorio */
	ancho = 450 ;
	izquierda = screen.width - ancho - 10 ;
	parametros = 'width=' + ancho +', left=' + izquierda +', top= 0, toolbar=0, menubar=0, scrollbars=yes, resizable=yes,directories=0, height=500' ;
	ventanaayuda = window.open( 'http://'+ location.host + '/' + bdayuda + '/FrmMarcosAyuda?OpenForm&MODULO=' + modulo ,'Ayuda', parametros ) ;
	ventanaayuda.focus() ;
}
function AbrirImpresion( pagina, ventana ) {
	/* Abre unaa ventana para impresion de reportes - OP 20/09/2003 */
	var argv = AbrirImpresion.arguments ;
	var argc = AbrirImpresion.arguments.length ;
	var ventana = (argc > 1) ? argv[2] : "VentanaImpresion";
	var naleatorio = Math.random() ;
	parametros = "height=screen.height, width=screen.width, top= 0, location=0, menubar=0, directories=0, status=0, toolbar=0,  scrollbars=yes, resizable=yes, copyhistory=0" ;
	ventanaauxiliar = window.open( pagina + "&" + naleatorio.toString(), ventana, parametros ) ;
	ventanaauxiliar.focus() ;
}
function AbrirNuevaVentana( pagina, ventana ) {
	/* Abre una nueva ventana */
	var argv = AbrirNuevaVentana.arguments ;
	var argc = AbrirNuevaVentana.arguments.length ;
	var ventana = (argc > 1) ? argv[2] : 'Auxiliar' ;
	if (pagina.substr(0, 7) != 'http://') {
	/* si contiene http tiene la dirección completa sino hace referencia al servidor del portal */
		pagina = 'http://'+ location.host + '/' + pagina ;
	}
	ancho = 450 ;
	izquierda = screen.width - ancho - 10 ;
	parametros = 'height=500, width=' + ancho +', left=' + izquierda +', top= 0, location=0, menubar=0, directories=0, status=0, toolbar=yes,  scrollbars=yes, resizable=yes, copyhistory=0' ;
	ventanaauxiliar = window.open( pagina, ventana, parametros ) ;
	ventanaauxiliar.focus() ;
}
function bNavegador() {
  if( navigator.appName )
  {
    if( navigator.appName == "Microsoft Internet Explorer")  return 1;
    if( navigator.appName == "Netscape")  return 2;
  }
  return 0;
}
var contador = 1;
function AbreVentana(ventana){
  var w=640, h=480;
  var windowName = new String( contador );
  windowName = "v" + windowName;
  var x = bNavegador();	
  if (window.screen && window.screen.availHeight) {
    h = window.screen.availHeight - 123; // 63
    if( x==2 ) h = h - 11;
    w = window.screen.availWidth - 24;
  }

	if (ventana==1)
	  window.open("http://www.sider.com.pe/sidernet/sid_login2.html", windowName, 
			"status=no,resizable=yes,toolbar=no,scrollbars=yes,top=0,left=0,width=" + w + ",height=" + h, 1 );
	
	if (ventana==2)
	  window.open("http://www.sider.com.pe", windowName, 
			"status=yes,resizable=yes,scrollbars=yes,top=0,left=0,width=" + w + ",height=" + h, 1);
	
	if (ventana==3)
	  window.open("https://www.sider.com.pe", windowName, 
			"status=yes,resizable=yes,scrollbars=yes,top=0,left=0,width=" + w + ",height=" + h, 1);
	
	if (ventana==4)
	  window.open("https://www.sider.com.pe",windowName,
	"scrollbars=no,resizable=yes,toolbar=no,width=730,height=540,left=0,top=0");
	
	if (ventana==5)
	window.open("http://www.sider.com.pe",windowName,"top=0,left=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=760,screenX=25,screenY=30,height=540");
	
	contador = contador + 1;
}

/* ---------------------------------------------------------------------------------------------------------------- */
/* Fin de Redireccionando URL */
/* ************************************************************************************ */
function refresh() {
	/*  Refresca el documento */
	_doClick('$Refresh',this,'_self') ;
}
function SeguridadCampo(campo) {
	/* Para impedir la edición del campo. */
	campo.blur() ;
}
function ImprimirPagina(){   
	/* Permite imprimir páginas Web en navegadores MSN 4.0 y superiores. */
	if (window.print) {
		window.parent.contenido.focus();
		window.print() ;
	    } else {
    		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>' ;
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser) ;
	    	WebBrowser1.ExecWB(6, 2) ;
	}
}
function ValidarNumero( campo ) {
	/* Valida cantidades numéricas con signos de decimales y miles - OP 03/10/2003 */
	var valid = ".,0123456789"
	var temp;
	
	for (var i=0;  i < campo.value.length; i++) {
		temp = "" + campo.value.substring(i, i+1);
		
		if (valid.indexOf(temp) == "-1")  {
			alert("El NUMERO ingresado no es válido.");
			campo.focus();
			campo.select();
			return false;
		}
	}
	return true;
}
function ValidarDigitos( texto ) {
	/* Valida solo números sin signos de decimales o miles - OP 03/10/2003 */
	var valid = "0123456789"
	var temp;
	
	for (var i=0;  i < texto.length; i++) {
		temp = "" + texto.substring(i, i+1);
		if (valid.indexOf(temp) == "-1")  { return 0 } ; 
	}	
	return 1 ;
}

function DatoFaltante( mensaje, campo, ficha ) { 
/* Valida que un campo no este vacio, si es así envía un mensaje - OP 03/10/2003 */
if (campo.type == "hidden") { mensaje = mensaje + " en la ficha " + "\n" + ficha + " (parte superior de este formulario)" } 
alert("Debe ingresar " + mensaje + "\n" + "y todos los campos marcados con >.") ;
if (campo.type != "hidden") { campo.focus() }
}
function VerificarFecha(campo) {
/* Valida que una fecha este bien escrita - OP 03/10/2003 */
	tmpFecha = campo.value ;
	fechaCorrecta = false ;
	if ( (tmpFecha.length == 8) || (tmpFecha.length == 10) ) {
		/* se rearma para estandarizar el caracter de separación */
		if (tmpFecha.length == 8 ) {
			tmpDay = tmpFecha.substring( 0, 2 ) ;
			tmpMonth = tmpFecha.substring( 2, 4 ) ;
			tmpYear = tmpFecha.substring( 4, 8 ) ;
			tmpFecha.value = tmpDay + "/" + tmpMonth + "/" + tmpYear ;
		}
		tmpDay = tmpFecha.substring( 0, 2 ) ;
		tmpMonth = tmpFecha.substring( 3, 5 ) ;
		tmpYear = tmpFecha.substring( 6, 10 ) ;
		fechaCorrecta = true ;
		if ( ValidarDigitos( tmpDay ) == 1 ) { 
			if (tmpDay.substring(0,1)=="0") {tmpDay=tmpDay.substring(1,2);} 
			numero = parseInt(tmpDay) ;
			if ( numero < 1 || numero > 31 ) { fechaCorrecta = false }  ;
		} else { fechaCorrecta = false }
		if ( fechaCorrecta == true && ValidarDigitos( tmpMonth ) == 1 ) {
			if (tmpMonth.substring(0,1)=="0") {tmpMonth=tmpMonth.substring(1,2);} 
			numero = parseInt(tmpMonth) ;
			if ( numero < 1 || numero > 12 ) { fechaCorrecta = false }
		} else { fechaCorrecta = false }
		if ( fechaCorrecta == true &&  ValidarDigitos( tmpYear ) == 1 ) {
			numero = parseInt(tmpYear) ;
			if ( numero < 1 ) { fechaCorrecta = false }
		} else { fechaCorrecta = false }
	} 
	if ( fechaCorrecta == true ) 
	{ 
		if (tmpDay.length == 1) {tmpDay = "0" + tmpDay;}
		if (tmpMonth.length == 1) {tmpMonth = "0" + tmpMonth;}
		return tmpDay + "/" + tmpMonth + "/" + tmpYear 
	} 
	else 
	{
		alert( "La FECHA ingresada no es correcta." ) ;
		campo.focus() ;
		campo.select();
		return 0 ;
	}
	
}
function trim( cadena ) {
/* Elimina los espacios en blanco sobrantes en toda la cadena - OP 06/10/2003 */
	fixedTrim = "" ;
	lastCh = " " ;
	for (x = 0; x < cadena.length; x++) {
		ch = cadena.charAt( x ) ;
		if ( ch != " " || lastCh !=  " " ) { fixedTrim += ch }
		lastCh = ch ;
	}
	if ( fixedTrim.charAt( fixedTrim.length - 1 ) == " ") {
		fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1)
	}
	return fixedTrim
}

function Redondear( numero , decimales) {
/* Redondea un número a la cantidad de decimales indicados - OP 06/10/2003 */
	var tmpNumero = parseFloat(numero) * decimales ;
	tmpNumero = Math.round( tmpNumero ) / decimales ;
	return tmpNumero ;
}
/* Function cuando hace click con el boton derecho del mouse*/
function click(e) {
	var message="SiderPeru S.A. - Derechos Reservados 2004";
	if (document.all) {
		if (event.button==2||event.button==3) {
			alert(message);
			return false;
		}
	}

	if (document.layers) {
		if (e.which == 3) {
			alert(message);
			return false;
		}
	}	
}
/* Funcion que convierte a Mayusculas el valor de un cuadro de Texto */
function aMayusculas(campo){
     campo.value=campo.value.toUpperCase();
}
/* funcion que sirve para Imprimir Marcos */
function imprime_marcos() {
		window.parent.inferiord.doPrint();
}
/* Funcion que se Activa cuando sales de SiderNet - Vendedores */
function salir() { 		
		f = document.form1;
		f.action = "Controler";		
		f.sider.value = "salir";
		f.submit();
		//alert('¡Gracias por Ingresar a SiderNet'); 
}
/* Funcion que se Activa cuando sales de SiderNet para los Clientes*/
function salirClientes() { 		
		f = document.form1;
		f.action = "ControlerClientes";		
		f.sider.value = "salir";
		f.submit();
		//alert('¡Gracias por Ingresar a SiderNet'); 
}
/* Funcion que se Activa cuando sales de SiderNet para los Clientes Publicos*/
function salirClientesPublicos() { 		
		f = document.form1;
		f.action = "ControlEncuestaServlet";		
		f.accionClienteEncuesta.value = "salir";
		f.submit();
		//alert('¡Gracias por Ingresar a SiderNet'); 
}
/* Funcion que abre una nueva Ventana */
function NewWindow(u,n,w,h,f,p,x,y){
	var win=(ws=window.screen?1:0,m=Math,C='center',R='random',M='custom',sw=screen.availWidth,sh=screen.availHeight,T=(p==C&&ws)?(sh-h)/2:(p==R&&ws)?(m.floor(m.random()*(sh-h))):(p==M)?y:100,L=(p==C&&ws)?(sw-w)/2:(p==R&&ws)?(m.floor(m.random()*(sw-w))):(p==M)?x:100,s='width='+w+',height='+h+',top='+T+',left='+L,s+=(!f||f=='')?'':','+f,window.open(u,n,s));
	if(win.focus){
		win.focus();
	}
}
/* Función que permite Contar el Codigo del Cliente*/
function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) {// if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	}
}
/* Desactivamos el Botón Derecho del Mouse*/
function disableRightClick(e)
{
  var message = "Por seguridad se ha desactivado el click derecho";
 
  if(!document.rightClickDisabled) // initialize
  {
    if(document.layers)
    {
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown = disableRightClick;
    }
    else document.oncontextmenu = disableRightClick;
    return document.rightClickDisabled = true;
  }
  if(document.layers || (document.getElementById && !document.all))
  {
    if (e.which==2||e.which==3)
    {
      //alert(message);
      return false;
    }
  }
  else
  {
    //alert(message);
    return false;
  }
}

/* Imprimir Certificado de Calidad*/
function ImprimirPaginaCalidad(){   
	/* Permite imprimir páginas Web en navegadores MSN 4.0 y superiores. */
	if (window.print) {		
		window.parent.certificado.focus();
		window.parent.certificado.print() ;
		f = document.form1;
		f.action = "/sidernet/cventas?sider=impresionCertificadoCalidad";	
		f.sider.value = "impresionCertificadoCalidad";
		f.submit();			
	    } else {
    		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>' ;
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser) ;
	    	WebBrowser1.ExecWB(6, 2) ;
	}
}

/* Abrir la Ventana de Certificados de Calidad*/
var cuenta = 1;
function AbreVentanaCalidad(ventana, certi, modelo, factura, fecha, condicion, numero){
	var w=640, h=480;
  	var windowName = new String( cuenta );
  	windowName = "v" + windowName;
  	var x = bNavegador();	
  	if (window.screen && window.screen.availHeight) {
		h = window.screen.availHeight - 123; 
		if( x==2 ) h = h - 11;
		w = window.screen.availWidth - 24;
  	}			
	cuenta = cuenta + 1;
	/* Envio de Datos al Servlet para que sea procesado */
	f = document.form1;
	f.action = "/sidernet/cventas?sider=getDetalleFacturaCalidad&factura=" + certi;		
	f.sider.value = "getDetalleFacturaCalidad";
	f.certificado.value = certi;
	f.modelo.value = modelo;
	f.fecha.value = fecha;
	f.factura.value = factura;
	f.condicion.value = condicion;
	f.numero.value = numero;
	f.submit();
	/*window.history.go(0);	*/
}

