// JScript source code
var defaultEmptyOK = false
var checkNiceness = true;
var whitespace = " \t\n\r";
var mMessage = "Error: no puede dejar este espacio vacio"
var pPrompt = "Error: ";
var pEmail = "ingrese una dirección de correo electrónico válida";

function checkField (theField, theFunction, emptyOK, s)
{   
    var msg;
    if (checkField.arguments.length < 3) emptyOK = defaultEmptyOK;
    if (checkField.arguments.length == 4) {
        msg = s;
    } else {
        if( theFunction == isAlphabetic ) msg = pAlphabetic;
        if( theFunction == isAlphanumeric ) msg = pAlphanumeric;
        if( theFunction == isAplhanumericNormal ) msg = pAplhanumericNormal;        
        if( theFunction == isInteger ) msg = pInteger;
        if( theFunction == isNumber ) msg = pNumber;
		if( theFunction == isNumberPositivo  ) msg = pNumberPositivo;
        if( theFunction == isEmail ) msg = pEmail;
        if( theFunction == isPhoneNumber ) msg = pPhoneNumber;
        if( theFunction == isName ) msg = pName;
    }
    
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;

    if ((emptyOK == false) && (isEmpty(theField.value))) 
        //return warnEmpty(theField);

    if ( checkNiceness && !isNice(theField.value))
        return warnInvalid(theField, pNice);

    if (theFunction(theField.value) == true) 
        return true;
    else
        return warnInvalid(theField,msg);

}

function isEmail (s)
{
    if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isNice(s)
{
        var i = 1;
        var sLength = s.length;
        var b = 1;
        while(i<sLength) {
                if( (s.charAt(i) == "\"") || (s.charAt(i) == "'" ) ) b = 0;
                i++;
        }
        return b;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en whitespace,
        // entonces retornar falso
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function warnEmpty (theField)
{   theField.focus()
    alert(mMessage)
    statBar(mMessage)
    return false
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    statBar(pPrompt + s)
    return false
}

function statBar (s)
{   window.status = s
}



function bNavegador() {
  if( navigator.appName )
  {
    if( navigator.appName == "Microsoft Internet Explorer")  return 1;
    if( navigator.appName == "Netscape")  return 2;
  }
  return 0;
}

var urlBancaInternet = 'https://www.mibanco.com.pe/webib/servlet/com.dlya.bantotal.hhbkm275?S,com.dlya.bantotal.hebanking';
	
function AbreBancavirtual() {
  var w=640, h=480;
  var windowName 
  windowName = "vbi1"
  var x = bNavegador();	
  if (window.screen && window.screen.availHeight) {
    h = window.screen.availHeight - 63; // 63
    if( x==2 ) h = h - 11;
    w = window.screen.availWidth - 4;
    if(h>590) h = 590;
    if(w>800) w = 800;	

  }
  window.open(urlBancaInternet, windowName, "status=yes,resizable=no,toolbar=no,scrollbars=yes,top=0,left=0,width=" + w + ",height=" + h, 1 );

}

function trim(cadena){ //Retorna cadena sin espacion emblanco a los extremos	
	while (cadena.substr(0,1)==" ")
		cadena=cadena.substr(1);
	while (cadena.substr(cadena.length-1,1)==" ")
		cadena=cadena.substr(0,cadena.length-1);
	while (cadena.indexOf("  ")!=-1)
		cadena=cadena.replace("  "," "); 
	return(cadena); 
}
		
function invFecha2(nTipFormat,dFecIni){   
	var dFecIni = dFecIni.replace(/-/g,"/");        // reemplaza el - por /    
		
	//primera division fecha   
	var nPosUno  = ponCero(dFecIni.substr(0,dFecIni.indexOf("/")));   
	//2º divicion fecha   
	var nPosDos  = ponCero(dFecIni.substr(parseInt(dFecIni.indexOf("/")) + 1,parseInt(dFecIni.lastIndexOf("/")) - parseInt(dFecIni.indexOf("/")) - 1));   
	//3º divicion fecha   
	var nPosTres = ponCero(dFecIni.substr(parseInt(dFecIni.lastIndexOf("/")) + 1));   

	switch(nTipFormat){   
		case 1 :    //  YYYY/MM/DD   
			dReturnFecha = nPosTres + "/" + nPosDos + "/" + nPosUno;   
			break;   	
		case 2 :    //  YYYY/DD/MM   
			dReturnFecha = nPosTres + "/" + nPosUno + "/" +nPosDos;   
			break;   	
		case 3 :    //  DD/MM/YYYY 
			dReturnFecha = nPosUno + "/" + nPosDos + "/" +nPosTres;  
			break;   		
		case 4 :    //  DD/YYYY/MM   
			dReturnFecha = nPosUno + "/" + nPosTres + "/" +nPosDos;   
			break;   
	}   
		
	return dReturnFecha;    // retorna la fecha        
}  
		
function invFecha(nTipFormat,dFecIni){   
	var dFecIni = dFecIni.replace(/-/g,"/");        // reemplaza el - por /    
		
	//primera division fecha   
	var nPosUno  = ponCero(dFecIni.substr(0,dFecIni.indexOf("/")));   
	//2º divicion fecha   
	var nPosDos  = ponCero(dFecIni.substr(parseInt(dFecIni.indexOf("/")) + 1,parseInt(dFecIni.lastIndexOf("/")) - parseInt(dFecIni.indexOf("/")) - 1));   
	//3º divicion fecha   
	var nPosTres = ponCero(dFecIni.substr(parseInt(dFecIni.lastIndexOf("/")) + 1));   
	
	switch(nTipFormat){   
		case 1 :    //  YYYY/MM/DD   
			dReturnFecha = nPosTres + "" + nPosDos + "" + nPosUno;   
			break;   
	
		case 2 :    //  YYYY/DD/MM   
			dReturnFecha = nPosTres + "" + nPosUno + "" +nPosDos;   
			break;   
	
		case 3 :    //  DD/MM/YYYY   
			dReturnFecha = nPosUno + "" + nPosDos + "" +nPosTres;   
			break;   
		
		case 4 :    //  DD/YYYY/MM   
			dReturnFecha = nPosUno + "" + nPosTres + "" +nPosDos;   
			break;   
	}   
		
	return dReturnFecha;    // retorna la fecha        
} 

//Agrega un cero delante del strPon cuando tenga solo un caracter   
function ponCero(strPon){   
	if(parseInt(strPon.length) < 2)   
		strPon = "0" + strPon;   
	return strPon;   
}  
		
function fecha_actual() {   
	var fecha_actual = new Date();
	var dia = fecha_actual.getDate();
	var mes = fecha_actual.getMonth() + 1;  
	var anio = fecha_actual.getFullYear();
	
	if (mes < 10)   
		mes = '0' + mes;   
	if (dia < 10)   
		dia = '0' + dia;
	
	return dia + "/" + mes + "/" + anio;
} 

function formatFecha(oElement){
	var fecha = oElement.value;
	var fechaarr = fecha.split("/");
	
	dia = fechaarr[0];
	mes = fechaarr[1];
	anio = fechaarr[2];
	
	if (mes.length == 1)   
		mes = '0' + mes;   
	if (dia.length == 1)   
		dia = '0' + dia;
		
	return dia + "/" + mes + "/" + anio;
}

function validar_Fecha(fecha){   
	if (fecha != undefined && fecha.value != "" ){ 
		if (fecha.value.split("/").length - 1 != 2){
			fecha.focus();
			alert("Fecha introducida errónea"); 
			return false; 			
		}
		if (!/^\d{2}\/\d{2}\/\d{4}$/.test(fecha.value)){ 
			fecha.value = formatFecha(fecha);
		} 
		
		var dia = parseInt(fecha.value.substring(0,2),10); 
		var mes = parseInt(fecha.value.substring(3,5),10); 
		var anio = parseInt(fecha.value.substring(6),10); 
		switch(mes){ 
			case 1: 
			case 3: 
			case 5: 
			case 7: 
			case 8: 
			case 10: 
			case 12: 
				numDias=31; 
				break; 
			case 4: case 6: case 9: case 11: 
				numDias=30; 
				break; 
			case 2: 
				if (comprobarSiBisisesto(anio)){ numDias=29 }else{ numDias=28}; 
				break; 
			default: 
				fecha.focus();
				alert("Fecha introducida errónea"); 
				return false; 
		} 
		if (dia>numDias || dia==0){ 
			fecha.focus();
			alert("Fecha introducida errónea"); 
			return false; 
		} 
		return true; 
	} 
} 	

function comprobarSiBisisesto(anio){ 
	if (( anio % 100 != 0) && ((anio % 4 == 0) || (anio % 400 == 0)))
		return true; 	
	else
		return false; 
} 

var formatoFecha= 1;
function compareFechas(fecha_inicio, fecha_fin, mensaje){
	fecha_inicio = invFecha(formatoFecha, fecha_inicio);   
	fecha_fin = invFecha(formatoFecha, fecha_fin);   
				
	if(fecha_inicio > fecha_fin){
		alert(mensaje);
		return false;
	}
	return true
}

function getInternetExplorerVersion(){
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer'){
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function objFormulario(frm,nombre) { //Permite obtener un objeto (campo) de un formulario basado en el nombre del objeto
	for (i=0; i<frm.length; i++){				
		if (frm.elements[i].name.indexOf(nombre) !=-1){	
			return frm.elements[i];
		}
	}
	return null;
}

function objFormulario2(ById, ByTagName, nombre){
	var table = document.getElementById(ById);   
	var cells = table.getElementsByTagName(ByTagName);   
	for (var i = 0; i < cells.length; i++) {  
		if (cells[i].id.indexOf(nombre)!=-1){		
			return cells[i].id;
		}
	} 
}

function retornaValorRadioBotton(rbgroup){
	for (i=0;i<rbgroup.length;i++){ 
		if (rbgroup[i].checked){ 
			return rbgroup[i].value; 					
		} 
	} 
	return '';
}

function retornaValorRadioBotton2(rblname){
	var f = document.getElementsByTagName("input");
	for (var i = 0 ; i < f.length ; ++i){ 
		if (f[i].type == "radio"){ 
			var radiogroup = f[i].name; 
			if (radiogroup==rblname){
				var obj = document.getElementsByName(radiogroup);
				for(var j = 0 ; j < obj.length; ++j){
					if (obj[j].checked)
						return true;
				}
			}
		}
	} 
	return false;
} 

function verifica(obj, mensaje) { //Valida si el campo presenta caracteres en blanco
	if (trim(obj.value)=="") {
		if (trim(mensaje)!="") {
			alert(mensaje);
			obj.focus();
			obj.select();
		}
		return false;
	}
	else {
		return true;
	}
}

function solonumeroEntero(){
	var tecla=window.event.keyCode;
	if(tecla<48 || tecla>58){
		window.event.keyCode=0;
	}
}

function solonumeroDecimal(){
	var tecla=window.event.keyCode;
	if (tecla < 46 || tecla > 58){				
		window.event.keyCode=0;
	}	
}

function validaSeleccion(obj,valorSeleccionado) { //Retorna si esta seleccionado el valor indicado
	//obj = document.formulario.campoSelection
	if (obj) {
		if (obj.options[obj.selectedIndex].value==valorSeleccionado)
			return true;
		else
			return false;
	}
}

function msgErrorCamp(obj, EtiqCamp) {
	if (trim(EtiqCamp)!='') 
		alert(EtiqCamp);
	obj.focus();
	if (obj.type=='text') obj.select();
}

function checkControlChk(obj,items) {  
	var chk = document.getElementById(obj);
	var frm = document.forms[0];
	var chkChild = null;		
	var streturn = '';
	for (i=0;i<items;i++){
		chkChild = document.getElementById(obj + "_" + i.toString());
		if (chkChild.checked)
			streturn= chkChild.value;
	}
	return streturn;
} 

function getNewProperty(obj,propiedad){
	var valor=obj.getAttribute? obj.getAttribute(propiedad).toString() : ""
	return valor;
}

function existProperty(obj,propiedad){
	if (obj.getAttribute(propiedad))
		return true;
	else
		return false;
}

Array.prototype.remove = function(from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	this.length = from < 0 ? this.length + from : from;
	return this.push.apply(this, rest);
};

function IsNumeric(input) {
   return (input - 0) == input && input.length > 0;
}

function roundOff(value, precision)
{		
    value = "" + value //convert value to string
    precision = parseInt(precision);

    var whole = "" + Math.round(value * Math.pow(10, precision));

    var decPoint = whole.length - precision;

    if(decPoint != 0){
            result = whole.substring(0, decPoint);
            result += ".";
            result += whole.substring(decPoint, whole.length);
    }else{
            result = whole;
    }
    
    if (Math.abs(parseFloat(value))>0 && Math.abs(parseFloat(value))<1) {
   		if (value.indexOf('-')=='-1') {
   			if (result.indexOf('.')=='-1') 
   				result='0.'+result;
   			else 
   				result='0'+result;
			
   		}
	   	else {
	   		result='-0.'+result.replace('-.','');
	   	}
   	}
    
    if (result=='.0' || result=='.00') result='0.00'
    
    return result;
}
