function fSessionEnd() 
{
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

    if (sPage.toUpperCase() == "MICUENTA.ASPX" || sPage.toUpperCase() == "FORMULARIOEDICION.ASPX")
    {
        top.window.location = "Index.aspx";
    }
}

function fGridIsSelectedItem(grd)
{
	if (grd.length == 0)
		return false;
	return true;
}

function fTextValue(e) 
{ 
    tecla = (document.all) ? e.keyCode : e.which; 
    if (tecla == 8) return true;
    patron = /[A-Za-zñÑ\s]/;
    te = String.fromCharCode(tecla); 
    return patron.test(te); 
} 


function Trim(strValor)
{
   return strValor.replace(/(^\s*)|(\s*$)/g, "");
}

function IntegerValue()
{
		//alert(event.keyCode);
		switch(event.keyCode)
		{
			case 48:
			case 49:
			case 50:
			case 51:
			case 52:
			case 53:
			case 54:
			case 55:
			case 56:
			case 57:break;			
			default:event.returnValue=false;break;
		}		
}

function DoubleValue(cadena)
{
		//alert(event.keyCode);		
		var strCadena = new String();
		
		strCadena  = cadena;
		
		switch(event.keyCode)
		{
			case 46://"."
				if (strCadena.indexOf(".") != -1)
					event.returnValue=false;				
			break;
			case 48:
			case 49:
			case 50:
			case 51:
			case 52:
			case 53:
			case 54:
			case 55:
			case 56:
			case 57:break;			
			default:event.returnValue=false;break;
		}		
}

function ToDate(strDate, strSep) {

	var pos1, pos2, day, month, year, date;	
	pos1 = strDate.indexOf(strSep,0);
	if (pos1 < 1) 
		return null;
	day	= strDate.substring(0, pos1);
	pos2 = strDate.indexOf(strSep, ++pos1);
	if (pos2 < 3) 
		return null;
	month = strDate.substring(pos1, pos2) - 1;
	if (month<0 || month>11)
		return null;
	year = strDate.substring(++pos2, strDate.length);
	if(year.length < 4)
		return null;
	if (Number(year)> 9999 || Number(year)<0) return null;
	date1 = new Date(year, month, 1);
	date2 = new Date(year, month + 1, 1)
	daysInMonth = (date2 - date1) / (1000*60*60*24);
	delete date1;
	delete date2;
	if (day<1 || day>daysInMonth)
		return null;
	var ret = new Date(year, month, day);
	if (isNaN(ret)) return null
	else return ret;
}

function DateComparing(strDate1, strDate2, strSeparator)
{
	var Date1 = new Date();
	var Date2 = new Date();	
	Date1 = ToDate(strDate1, strSeparator);
	if(Date1 == null)
		return false;
	Date2 = ToDate(strDate2, strSeparator);
	if(Date2 == null)
		return false;
	if(Date2.getTime() > Date1.getTime())
		return true;
	else
		return false;
}

/// Devuelve:
/// -1 si hay una fecha erronea.
///  0 si strDate 1 < strDate2
///  1 si son iguales
///  2 si strDate 1 > strDate2
function DateCompare(strDate1, strDate2, strSeparator)
{
	var Date1 = new Date();
	var Date2 = new Date();	
	
	Date1 = ToDate(strDate1, strSeparator);
	Date2 = ToDate(strDate2, strSeparator);
	
	if(Date1 == null || Date2 == null)
		return -1;
		
	if(Date1.getTime() < Date2.getTime())
		return 0;
	else if (Date1.getTime() > Date2.getTime())
			return 2;
		else return 1;
}

function DetectNavigator()
{
	if (navigator.appName == "Microsoft Internet Explorer" && event.keyCode == 13)
		document.getElementById("btnBuscar").click();
	if(navigator.appName == "Netscape" && event.KeyPress.wich == 13)
		document.getElementById("btnBuscar").click();
}

function validateEmail(valor) 
{
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
    return true
  else 
    return false;
}

/*
 * Funcion para seleccionar todos los checkbox de un formulario
 
 * Parametros
 * chkAll:	Nombre del checkBox que permite la opcion de seleccionar todos
 * obj:		Nombre del listado chechbox a seleccionar
	
*/

function fSelectAll(chkAll,obj)
{			
	i	= 0;
	aObj = document.getElementById(obj + "_" + i);		
	while (!(aObj == null))
	{			
		aObj.checked = chkAll.checked;
		i++;			
		aObj = document.getElementById(obj + "_" + i);
	}
}

function URLEncode() {
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" + 				// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()"; 				// RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var plaintext = document.URLForm.F1.value;
    var encoded = "";
    for (var i = 0; i < plaintext.length; i++) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+"; 			// x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert("Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted.");
                encoded += "+";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    } // for

    document.URLForm.F2.value = encoded;
    document.URLForm.F2.select();
    return false;
};

function URLDecode() {
    // Replace + with ' '
    // Replace %xx with equivalent character
    // Put [ERROR] in output if %xx is invalid.
    var HEXCHARS = "0123456789ABCDEFabcdef";
    var encoded = document.URLForm.F2.value;
    var plaintext = "";
    var i = 0;
    while (i < encoded.length) {
        var ch = encoded.charAt(i);
        if (ch == "+") {
            plaintext += " ";
            i++;
        } else if (ch == "%") {
            if (i < (encoded.length - 2)
					&& HEXCHARS.indexOf(encoded.charAt(i + 1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i + 2)) != -1) {
                plaintext += unescape(encoded.substr(i, 3));
                i += 3;
            } else {
                alert('Bad escape combination near ...' + encoded.substr(i));
                plaintext += "%[ERROR]";
                i++;
            }
        } else {
            plaintext += ch;
            i++;
        }
    } // while
    document.URLForm.F1.value = plaintext;
    document.URLForm.F1.select();
    return false;
};
