
/*
    s : select, submit
    r  : radio button
    c : check box
    b : button
*/

function firstFocus()
{
   if (document.forms.length > 0)
   {
      var TForm = document.forms[0];
      for (i=0;i<TForm.length;i++)
      {
      			 //(TForm.elements[i].type=="hidden") ||
		         if ( (TForm.elements[i].type=="password")|| (TForm.elements[i].type=="text")||
		           (TForm.elements[i].type=="textarea")||
		           (TForm.elements[i].type.toString().charAt(0)=="s") || (TForm.elements[i].type.toString().charAt(0)=="r") || 
		           (TForm.elements[i].type.toString().charAt(0)=="c") || (TForm.elements[i].type.toString().charAt(0)=="b"))
			         {
                                if(document.forms[0].elements[i].type!='hidden')
    					            document.forms[0].elements[i].focus();
								if(document.forms[0].elements[i].type=="text" || document.forms[0].elements[i].type=="password" || document.forms[0].elements[i].type=="textarea")
									document.forms[0].elements[i].select();
					            break;                      
			         }
      }
   }
}

/*
function firstFocus()
{
   if (document.forms.length > 0)
   {
      var TForm = document.forms[0];
      for (i=0;i<TForm.length;i++)
      {
	  	//alert("Length: "+TForm.length);	
		//alert("0: "+TForm.elements[0].type);
		//alert("1: "+TForm.elements[1].type);
		//alert("2: "+TForm.elements[2].type);
		//alert("3: "+TForm.elements[3].type);

         if ((TForm.elements[i].type=="password")|| (TForm.elements[i].type=="text")||
           (TForm.elements[i].type=="textarea")||
           (TForm.elements[i].type.toString().charAt(0)=="s"))
         {		 	
            document.forms[0].elements[i].focus();
			if(document.forms[0].elements[i].type=="text" || document.forms[0].elements[i].type=="password" || document.forms[0].elements[i].type=="textarea")
				document.forms[0].elements[i].select();
            break;
         }
      }
   }
}	*/
 

/****************************************************************/
function makeSQLSafe (formField)
{
	formField.value = sqlSafe (formField.value);
}

 
 /****************************************************************/
/* PURPOSE:  Since we are using the single tick mark as the
	string delimiter to construct our SQL queries, a string with
	a tick mark in it will cause a SQL error.  Therefore we replace
	all "'" with "''", which eliminates the possibility of a SQL error.
*/

function sqlSafe (s)
{
	var new_s = trim(s);
 	if (!(new_s.length == 0 || new_s == null)){
		new_s = replaceAll (new_s, "'", "^");
		new_s = replaceAll (new_s, "^", "''");
		new_s = replaceAll (new_s, "\"", "^");
		new_s = replaceAll (new_s, "^", "''");
	}
	return new_s;
}


function Regresar() {
	window.history.back()
}
/*
from: http://www.boutell.com/newfaq/creating/windowcenter.html
sample: <a href="page.html" target="popup" onClick="wopen('page.html', 'popup', 300, 200); return false;">Click here to open the page in a new window.</a>
*/
function wopen(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
  return win;
}

function wopenwithscroll(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=yes, resizable=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
  return win;
}

function printReport() {
    if (window.print) {
        setTimeout('window.print();',200);
    }
    else if (agt.indexOf("mac") != -1) {
        alert("Presione 'Cmd + p' para Imprimir el Reporte.");
    }
    else {
        alert("Presione 'Ctrl + p' para Imprimir el Reporte.")
    }
}

function hidePrint() {
    if (document.all) { 
        document.all.layerPrint.innerHTML = ""; 
    }
}

function showPrint(strInstrucciones) {
    if (document.all) { 
        document.all.layerPrint.innerHTML = '<a href="javascript:void(printReport());"><img alt="Seleccione para Imprimir" src="imagenes/BotonImprimir.gif" border="0"></a>'; 
    }
    if (document.layers) { 
        document.layers.ilayerPrint.visibility = "show"; 
    }
    location.href = strInstrucciones;
}

function AsignaValorCampoForma(frmcampo, valor){
	eval(frmcampo+'.value=\''+valor+'\'');
}


function makeDaysOfMonth(){
	  var i = 0;
	  this[i++] = 0; // dummy
	  this[i++] = 31;
	  this[i++] = 29;
	  this[i++] = 31;
	  this[i++] = 30;
	  this[i++] = 31;
	  this[i++] = 30;
	  this[i++] = 31;
	  this[i++] = 31;
	  this[i++] = 30;
	  this[i++] = 31;
	  this[i++] = 30;
	  this[i  ] = 31;
	  this.length = i;
}
	
function calcAge(DateValue, Format){
	  var checkstr = "0123456789";
	  var DateTemp = ""; 
	  var t, mon, day, year, DD, MM, YY, age;
	  var MTB = new makeDaysOfMonth();
	  var dd, mm, yy;
	  	  
	  /* Delete all chars except 0..9 */	
	  for (i = 0; i < DateValue.length; i++) {
		  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
			 DateTemp = DateTemp + DateValue.substr(i,1);
		  }
	  }	   
	  DateValue = DateTemp;
	  yy = DateValue.substr(4,4);
  	  
	  if (Format == "DD/MM/YYYY") {
		   mm = DateValue.substr(2,2);
		   dd = DateValue.substr(0,2);
	  }
	  else{
			// MM/DD/YYYY 
		   mm = DateValue.substr(0,2);
		   dd = DateValue.substr(2,2);	
	  }	  
	  
	  YY   = parseInt(yy,10);	// year of birth (4 digits)
	  MM   = parseInt(mm,10);	// month of birth (1-12)
	  DD   = parseInt(dd,10);	// date of birth (1-31)	 
	  
	  if (MTB[MM] < DD || DD < 1) return -1;
	  t    = new Date();	// get current date
	  year = t.getFullYear();	// get year of current
	  mon  = t.getMonth() + 1;	// get month of current
	  day  = t.getDate();	// get date of current  
	  
	  if (MM == 2 && DD == 29){	// check leap year
		if (!(((YY % 4 == 0) && (YY % 100 != 0)) || (YY % 400 == 0))){
		  //alert("The year " +YY+ " ends at 28th of "+MM+" month\nPlease check the date.");
		  return -1;
		}
	  }
	  age = year - YY;
	  if ((MM > mon) || (MM == mon && day < DD)) age --;	  
	  
	  return age;
	  
}
	
function calcAge2(dd, mm, yy){
	  var t, mon, day, year, DD, MM, YY, age;
	  var MTB = new makeDaysOfMonth();
	  YY   = parseInt(yy,10);	// year of birth (4 digits)
	  MM   = parseInt(mm,10);	// month of birth (1-12)
	  DD   = parseInt(dd,10);	// date of birth (1-31)
	  	  
	  if (MTB[MM] < DD || DD < 1) return -1;
	  t    = new Date();	// get current date
	  year = t.getFullYear();	// get year of current
	  mon  = t.getMonth() + 1;	// get month of current
	  day  = t.getDate();	// get date of current
	  	  
	  if (MM == 2 && DD == 29){	// check leap year
		if (!(((YY % 4 == 0) && (YY % 100 != 0)) || (YY % 400 == 0))){
		  //alert("The year " +YY+ " ends at 28th of "+MM+" month\nPlease check the date.");
		  return -1;
		}
	  }
	  age = year - YY;
	  if ((MM > mon) || (MM == mon && day < DD)) age --;
	  return age;
}	

function AsignaValorCampoFormaCalculaEdad(frmcampo, valor, frmcampoedad){
	var age;
	
	eval(frmcampo+'.value=\''+valor+'\'');		
	age = calcAge(valor,'DD/MM/YYYY');	
	eval(frmcampoedad+'.value=\''+age+'\'');
}

function AsignaValorCampoFormaCalculaEdad_v2(frmcampo, frmcampoedad){
	var age;
	var valor;
	
	valor = eval(frmcampo+'.value');		
	age = calcAge(valor,'DD/MM/YYYY');	
	eval(frmcampoedad+'.value=\''+age+'\'');
}

function updateOpener() {
    opener.location.href = opener.location.href;
}

function CalculaEdad(frmcampo,frmcampoedad){
	var age;	
	age = calcAge(eval(frmcampo+'.value'),'DD/MM/YYYY');	
	eval(frmcampoedad+'.value=\''+age+'\'');
}

//Pone la primera letra de cada palabra en mayuscula
function changeCase(frmObj) {
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;
	tmpStr = frmObj.value.toLowerCase();
	strLen = tmpStr.length;

	if (strLen > 0)  {
		for (index = 0; index < strLen; index++)  {
			if (index == 0)  {
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,strLen);
				tmpStr = tmpChar + postString;
			}
			else{
				tmpChar = tmpStr.substring(index, index+1);
				if (tmpChar == " " && index < (strLen-1)){
					tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
					preString = tmpStr.substring(0, index+1);
					postString = tmpStr.substring(index+2,strLen);
					tmpStr = preString + tmpChar + postString;
			    }
		    }
	   }
	}
	frmObj.value = tmpStr;
}

function UpperCase(frmObj) {
	frmObj.value = frmObj.value.toUpperCase()
}


/****************************************************************/
function makeSinAcentos (formField)
{
	alert(formField.value);
	formField.value = quitaAcentos (formField.value);
}

 
 /****************************************************************/
/* PURPOSE:  Since we are using the single tick mark as the
	string delimiter to construct our SQL queries, a string with
	a tick mark in it will cause a SQL error.  Therefore we replace
	all "'" with "''", which eliminates the possibility of a SQL error.
*/

function quitaAcentos (s)
{
   var new_s = trim(s);
	
   /*
   var sinAcento=new Array('a','e','i','o','u','A','E','I','O','U','?');
   var conAcento=new Array('/?','?','?','?','?','?','?','?','?','?','?');

   var longitud;
   longitud=new_s.length;
   for (i=0; i<11; i++)
     { posicion=new_s.indexOf(conAcento[i]);
       while (posicion!=-1)
        { new_s=new_s.substring(0,posicion)+sinAcento[i]+new_s.substring(posicion+1,longitud);
          posicion=new_s.indexOf(conAcento[i]);}      
     }
    */
	
 	if (!(new_s.length == 0 || new_s == null)){
		new_s = replaceAll (new_s, "?", "a");
		new_s = replaceAll (new_s, "?", "e");
		new_s = replaceAll (new_s, "?", "i");
		new_s = replaceAll (new_s, "?", "o");
		new_s = replaceAll (new_s, "?", "u");
		new_s = replaceAll (new_s, "?", "n");
		new_s = replaceAll (new_s, "?", "A");
		new_s = replaceAll (new_s, "?", "E");
		new_s = replaceAll (new_s, "?", "I");
		new_s = replaceAll (new_s, "?", "O");
		new_s = replaceAll (new_s, "?", "U");
		new_s = replaceAll (new_s, "?", "N");		
	}
	
	return new_s;
}

function replaceAll (s, fromStr, toStr)
{
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}

function trim(strTexto) {

	var i;
	i=0
	while (i<strTexto.length) {
	   if (strTexto.charAt(i)==" ") {
	      strTexto=strTexto.substring(i+1,strTexto.length)
	      i=-1
	   }
	   else {
	      break
	   }
	   i++
	}
	i=strTexto.length-1
	while (i>=0) {
	   if (strTexto.charAt(i)==" ") {
	      strTexto=strTexto.substring(0,strTexto.length-1)
	      i=strTexto.length
	   }
	   else {
	      break
	   }
	   i--
	}
	return strTexto
}

// Limita el tipo de archivo que se puede subir
// Uso: <input type=button name="Submit" value="Submit" onclick="LimitAttach(this.form, this.form.uploadfile.value)">
extFotos = new Array(".gif", ".jpg");
extDocumentos = new Array(".doc", ".pdf", ".html", ".htm", ".ppt");
function LimitAttach(form, file, fieldname, extArray) {
	allowSubmit = false;
	if (!file) return;
	while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.indexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	if (allowSubmit){ 
		//form.submit();
		//return allowSubmit;
	}
	else{
		//document.forms[0].reset();
		eval('document.forms[0].'+fieldname+'.focus()');
	}
	return allowSubmit;
}
//  
