// Validación de fechas
function ValidaFecha(source, argumentos)
{
  argumentos.IsValid = false;
  obj = document.getElementById(source.controltovalidate);
  
  if (typeof(obj) != "undefined")
  {
    argumentos.IsValid = check_date(obj);
  }
}

function ValidDate(d, m, y) // m = 0..11
{ with (new Date(y, m, d)) return ((getDate()==d) && (getMonth()==m)) }

function check_date(field) {
  var Q = trim(field.value);
  if (Q.length == 0) return true;
  
  while (Q.indexOf("/") > -1) { Q = Q.replace("/","-"); }

	if ((Q.indexOf("-") == -1) && (Q.length >= 6)) //Formato ddmmyy o ddmmyyyy		
		Q = Q.substr(0,2) + "-" + Q.substr(2,2) + "-" + Q.substr(4);
       
  if (Q.search(/^\d+-\d+-\d+$/)!=0) {  // bad format?   
    field.select();
    field.focus();
    return false;
  }
  var j, T = Q.split('-')
  for (j=0; j<=2; j++) { T[j] = parseInt(T[j], 10) } // wanted ?
  if (T[2] < 71) T[2]+=2000; else if(T[2] < 100) T[2]+=1900;
  if (!ValidDate(T[0], T[1]-1, T[2])) {  // bad value
    field.select();
    field.focus();
    return false;
  } else {
    T[0] = "" + T[0];
    T[1] = "" + T[1];
      
    if (T[0].length == 1) T[0] = "0" + T[0];
    if (T[1].length == 1) T[1] = "0" + T[1];
    
    field.value=T[0] + "/" + T[1] + "/" + T[2];
  }
  
  return true;
}

function trim(inputString)
{
  if (typeof inputString != "string") 
    return inputString;
  
  var retValue = inputString;
  var ch = retValue.substring(0, 1);
  
  // Espacios al principio
  while (ch == " ")
  {
    retValue = retValue.substring(1, retValue.length);
    ch = retValue.substring(0, 1);
  }

  // Espacios al final  
  ch = retValue.substring(retValue.length-1, retValue.length);
  while (ch == " ")
  {
    retValue = retValue.substring(0, retValue.length-1);
    ch = retValue.substring(retValue.length-1, retValue.length);
  }
  
  // Espacios repetidos dentro de la cadena
  while (retValue.indexOf("  ") != -1)
  { 
    retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
  }
  
  return retValue;
}

function AbrirCalendario(sCampoDestino)
{
	var sFeatures, sUrl, obj;
	
	sFeatures = 'width=250,height=220,resizeable=no,scrollbars=no';
	sUrl = 'Controles/VentanaCalendario.aspx?ORIGEN=' + sCampoDestino;
	
	obj = document.getElementById(sCampoDestino);
  if (typeof(obj) != "undefined")
    sUrl += "&VALOR=" + obj.value;
  
	//Abre la ventana del calendar
	CalendarWindow = open(sUrl, 'Calendario', sFeatures);
	if (CalendarWindow.opener == null) 
		CalendarWindow.opener = self;
	CalendarWindow.focus();
}

function MostrarToolTip(sCampo, bEntra)
{
	if (bEntra == 1)
	{
		sCampo.style.display="block";
	}
	else
	{
		sCampo.style.display="none";
	}
}

function ConfirmarBorrado() 
{ 
	if (confirm("Estas seguro de querer eliminar este registro?") == true) 
		return true; 
	else 
		return false; 
}		

function ConfirmarBorrado(texto) 
{ 
	if (confirm(texto) == true) 
		return true; 
	else 
		return false; 
}		

function SetFocus(sCampo)
{
	document.getElementById('" + sCampo + "').focus();
}

function SetUniqueRadioButton(nameregex, current)
{
	re = new RegExp(nameregex);
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		elm = document.forms[0].elements[i]
		if (elm.type == 'radio')
		{
			if (re.test(elm.name))
			{
				elm.checked = false;
			}
		}
	}
	current.checked = true;
}

function popUp(URL,ancho,alto) 
{
	iz=(screen.width-ancho) / 2;
	de=(screen.height-alto) / 2;
	ventana = window.open(URL,'_blank',"scrollbars=yes,height= "+alto+", width="+ancho+",left = "+iz+",top="+de);
}

function ExpandirMenu(id)
{
	obj = document.getElementById("div"+id);
	img = document.getElementById("img"+id);

	if (obj.style.display == "none")
	{
		// Expandido
		obj.style.display = "block";
		img.src = "imagenes/f_menu2.gif";
	}
	else
	{
		// Contraido
		obj.style.display = "none";
		img.src = "imagenes/f_menu1.gif";
	}
}