	

function MostrarCalendario(strControl,iYear,iMonth,iDay,bDateISO, strDateISO , sIdioma ){
	var intPosX;
	var intPosY;
	var retornoCalendario;

	var oTarget = document.getElementById(strControl);
	if 	(oTarget == null){return};	

		
	if (strDateISO == null){strDateISO=""}
	if (iYear == null){iYear=""}
	if (iMonth == null){iMonth=""}
	if (iDay == null){iDay=""}
	if (bDateISO == null){bDateISO=false}	
	if (sIdioma == null){sIdioma='es'}
	
	// Modificado DTorres 10/06/2004
	
	var intX = 363;
	var intY = 178;
	
	if (oTarget.getAttribute("format") != ""){
		var strMascara = oTarget.getAttribute("format");
	}
	
	if (oTarget.value != "")
			{
			
		if (oTarget.getAttribute("bDateISO"))
			{
				if 	(strDateISO == ""){strDateISO = oTarget.value}		
			}
		else
			{
			if (oTarget.getAttribute("format") != "")
				{
				// Modificado DTorres 10/06/2004
				//var strMascara = oTarget.getAttribute("format");
				
				switch (strMascara)
					{
					case "dd/mm/yyyy":
						iYear  = oTarget.value.substring(6,10);
						iMonth = oTarget.value.substring(3,5);	
						iDay   = oTarget.value.substring(0,2);
					break;
					case "mm/dd/yyyy":
						iYear  = oTarget.value.substring(6,10);
						iMonth = oTarget.value.substring(0,2);	
						iDay   = oTarget.value.substring(3,5);
					break;
					case "yyyy/mm/dd":
						iYear  = oTarget.value.substring(0,4);
						iMonth = oTarget.value.substring(5,7);	
						iDay   = oTarget.value.substring(8,10);
					break;
					}
				}		
					
			}	
		}


	intPosX = window.screenLeft + window.event.clientX - window.event.offsetX + window.event.srcElement.width;
	intPosY = window.screenTop + window.event.clientY -window.event.offsetY+ window.event.srcElement.height;
	if ((intPosX + intX) > screen.width)
	{
		intPosX = window.screenLeft + (window.event.clientX - window.event.offsetX) - intX ;
	}
	if ((intPosY + intY) > screen.height)
	{
		intPosY = window.screenTop + (window.event.clientY - window.event.offsetY) - intY ;
	}



	concatenado =  "/ms/_softpoint/_inc/Calendario.asp?iYear=" + iYear + "&iMonth=" + iMonth + "&iDay=" + iDay + "&bDateISO=" + bDateISO + "&strDateISO=" + strDateISO;
	concatenado = concatenado + "&strMascara=" + strMascara + "&idioma=" + sIdioma; 
	
	retornoCalendario = window.showModalDialog(concatenado ,"blank" ,"edge:sunken; status=no; dialogLeft=" + intPosX + "; dialogTop=" + intPosY + "; dialogHeight=" + intY + "px; dialogWidth="+ intX + "px; scroll:no; unadorned:on; resizable:no; help:no; dialoghide:yes; ");
	if (retornoCalendario != ""){
		oTarget.value = retornoCalendario;
		oTarget.setAttribute("bDateISO",bDateISO);	
	}
		
}
	



function controlarFechaFormato(){
var fechaF;
var sFormato;
	
	 
	 if (event.srcElement.value != "" )// ' si está vacía no controla.
	{
		try	
		{
			sFormato = "";
			sFormato = event.srcElement.format;		
			
		} catch (e) {
			return false;
		}
		
	
		fechaF = FechaFormateada(event.srcElement.value, sFormato);
		if (fechaF=='' ) {
			event.srcElement.focus();
			event.srcElement.select ;
			return false;			
			}
		else
			event.srcElement.value = fechaF; 
		
	}
}

	function FechaFormateada(fecha,sFormato){
	//********************************************************************
	// Descripción:	Recibe una fecha, la valida y la retorna con el formato recibido
	// Fecha Creación:	04/06/2003 
	// Autor:			AVargas
	//********************************************************************
	var arrFecha;
	var dia;
	var mes;
	var ano;
	var K_MIN_ANIO = 1000;
	var K_MAX_ANIO = 9999;
	var sReturn = "";
	var sfecha = ""
		if (sFormato == "" )
			
			alert ("falta especificar formato de fecha");
		
		// Devuelve un String con la fecha formateada
		//on error resume next
		sfecha = fecha.tostring;
		
		//sfecha = sfecha.replace('-','/');
		//sfecha = sfecha.replace('.','/');
		//sfecha = sfecha.replace('\','/');
		
		arrFecha = fecha.split("/");
		
		if (sFormato == "dd/mm/yyyy" ) {
			dia = arrFecha[0];		
			mes = arrFecha[1];
			ano = arrFecha[2];
		} else if (sFormato == "mm/dd/yyyy") {
			mes = arrFecha[0];
			dia = arrFecha[1];		
			ano = arrFecha[2];		
		} else if (sFormato = "yyyyy/mm/yyyy") {
			ano = arrFecha[0];		
			mes = arrFecha[1];
			dia = arrFecha[2];		
		} else
			alert("formato no soportado:" + sFormato);
		
				
		
		if (dia > 31 ) {
			sReturn = '';
			// mstrPantalla (0 : Alert, 1 : Confirm) 
			// mstrIcono (0: Error, 1: Informacion, 2: Advertencia)
			// mstrDatos (valor a reemplazar o texto libre)
			// mstrTipoDato (Reemplazar : ?) 
			var strQuery = "?mstrMensaje=Mensaje_028" + "&mstrPantalla=0" + "&mstrIcono=2";
			var blnResp = showModalDialog("/" + CONST_NOMBRESITIO + "/_iu/MensajeUsuario/MensajeUsuario.asp" + strQuery, null, "dialogWidth:400px; dialogHeight:160px; help:no; status:no; reSizable:yes");	
			return sReturn
		}
		
		if (mes > 12) {
					
			sReturn = '';
			// mstrPantalla (0 : Alert, 1 : Confirm) 
			// mstrIcono (0: Error, 1: Informacion, 2: Advertencia)
			// mstrDatos (valor a reemplazar o texto libre)
			// mstrTipoDato (Reemplazar : ?) 
			var strQuery = "?mstrMensaje=Mensaje_029" + "&mstrPantalla=0" + "&mstrIcono=2";
			var blnResp = showModalDialog("/" + CONST_NOMBRESITIO + "/_iu/MensajeUsuario/MensajeUsuario.asp" + strQuery, null, "dialogWidth:400px; dialogHeight:160px; help:no; status:no; reSizable:yes");	
			return sReturn
		}
		
		if (ano < 999) {
			
			sReturn = '';
			// mstrPantalla (0 : Alert, 1 : Confirm) 
			// mstrIcono (0: Error, 1: Informacion, 2: Advertencia)
			// mstrDatos (valor a reemplazar o texto libre)
			// mstrTipoDato (Reemplazar : ?) 
			var strQuery = "?mstrMensaje=Mensaje_030" + "&mstrPantalla=0" + "&mstrIcono=2";
			var blnResp = showModalDialog("/" + CONST_NOMBRESITIO + "/_iu/MensajeUsuario/MensajeUsuario.asp" + strQuery, null, "dialogWidth:400px; dialogHeight:160px; help:no; status:no; reSizable:yes");	
			return sReturn
		}
		
		if (!isDate(ano,mes,dia) ) {
			sReturn = "";
			// mstrPantalla (0 : Alert, 1 : Confirm) 
			// mstrIcono (0: Error, 1: Informacion, 2: Advertencia)
			// mstrDatos (valor a reemplazar o texto libre)
			// mstrTipoDato (Reemplazar : ?) 
			var strQuery = "?mstrMensaje=Mensaje_031" + "&mstrPantalla=0" + "&mstrIcono=2";
			var blnResp = showModalDialog("/" + CONST_NOMBRESITIO + "/_iu/MensajeUsuario/MensajeUsuario.asp" + strQuery, null, "dialogWidth:400px; dialogHeight:160px; help:no; status:no; reSizable:yes");	
			return sReturn
		}
		
		dia = Right('0' + dia,2); 		
		mes = Right('0' + mes,2);		
		
		
		sReturn = sFormato;
		
		sReturn = sReturn.replace("dd",dia);
		sReturn = sReturn.replace("mm",mes);
		sReturn = sReturn.replace("yyyy",ano);
		return sReturn
/*		
		} catch (e) {
			sReturn = "";
			// mstrPantalla (0 : Alert, 1 : Confirm) 
			// mstrIcono (0: Error, 1: Informacion, 2: Advertencia)
			// mstrDatos (valor a reemplazar o texto libre)
			// mstrTipoDato (Reemplazar : ?) 
			var strQuery = "?mstrMensaje=Mensaje_032" + "&mstrPantalla=0" + "&mstrIcono=2";
			var blnResp = showModalDialog("/" + CONST_NOMBRESITIO + "/_iu/MensajeUsuario/MensajeUsuario.asp" + strQuery, null, "dialogWidth:400px; dialogHeight:160px; help:no; status:no; reSizable:yes");	
			return sReturn;
		}
*/		
		
}

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}


var defaultEmptyOK = false

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}



// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}



// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}



// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer >= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}




function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}


function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}


// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
// 

function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}




function Right(s, n){
// Devuelve los n últimos caracteres de la cadena
	var t=s.length;
	if(n>t)
		n=t;		
	return s.substring(t-n, t);
}

function VerificarMascaraFecha() {
	
	if(document.selection != null) {
		if(document.selection.createRange().text != '')
			document.selection.clear();
	}
	var keyCode = event.keyCode;
	var autoFill = true; // llenar automaticamente los caracteres especiales  event.srcElement.autofill;
	//if(autoFill == null)
	//	autoFill = event.target.attributes[event.target.attributes.length - 2].value;
	var format = event.srcElement.format;
	
	var mask = format.replace("dd","99").replace("mm","99").replace("yyyy","9999");

	if(mask == null)
		mask = event.target.attributes[event.target.attributes.length - 1].value;
	var val = event.srcElement.value;
	var retValue = false;
	
	if(mask.length == 0 || keyCode == 13 || keyCode == 8 || keyCode == 0) {
		retValue = true;
	} else {
	
		var maskChar = mask.substr(val.length, 1);
		var nextMaskChar = '';
		if((val.length + 1) <= mask.length)
			nextMaskChar = mask.substr(val.length + 1, 1);
	
		if ((maskChar.charCodeAt(0) == 57) && (keyCode >= 48) && (keyCode <= 57)) // digito
			retValue = true;
		else if((maskChar.toLowerCase().charCodeAt(0) == 99) && (((keyCode >= 65) && (keyCode <=90)) || ((keyCode >= 97) && (keyCode <= 122)))) //&& (((keyCode >= 65) && (keyCode <= 90)) || ((keyCode >= 99) && (keyCode <= 122))))
			retValue = true;
		else if(maskChar == String.fromCharCode(keyCode)) // caracter especial
			retValue = true;
		else
			retValue = false;
			
		if(retValue && autoFill == true) {
			var i=1;
			var addedKey = false;
			while(nextMaskChar != '') {
				if((val.length + i) <= mask.length)
					nextMaskChar = mask.substr(val.length + i, 1);
				if(nextMaskChar.toLowerCase() == '9' || nextMaskChar.toLowerCase() == 'c')
					nextMaskChar = '';
				var charCode = nextMaskChar.toLowerCase().charCodeAt(0);
				if((charCode != 57) && ((charCode < 48) || (charCode > 57)) && (charCode != 99)) {
					if(!addedKey) {
						event.srcElement.value = event.srcElement.value + String.fromCharCode(keyCode) + nextMaskChar;
						addedKey = true;
					} else
						event.srcElement.value = event.srcElement.value + nextMaskChar;
					retValue = false;
				}
				i++;
			}
		}
	}
	
	return retValue;
}

function VerificarMascaraPaste() {
	var pasteData = window.clipboardData.getData("Text");
	var format = event.srcElement.format;
	var re;
	if (format =='dd/mm/yyyy')
		re = /\d{2}\/\d{2}\/\d{4}/;
	if (format =='mm/dd/yyyy')
		re = /\d{2}\/\d{2}\/\d{4}/;
	if (format =='yyyy/mm/dd')
		re = /\d{4}\/\d{2}\/\d{2}/;
				
	if(re.test(pasteData) || pasteData.length == 0) {
		event.srcElement.value = pasteData;
		return false;
	} else
		return false;
}


