/*
'======================================================================
' Descripcion: Funciones javascrip varias para el manejo de formularios
'
' Parametros: ninguno
'
' Autor: Gustavo Navarro
'
' Fecha de Creación: 27/08/2002
'
'======================================================================
*/

//==================================================
function validaCamposTextObligatorios(objForm,vCampos,vCaptions)
//==================================================
{
	var iCant;
	var i;
	var tAux;
	iCant=vCampos.length;
	for(i=0;i<iCant;i++)
		{
		//alert(vCampos[i]);
		if(!objForm[vCampos[i]].disabled)
		{
		tAux=objForm[vCampos[i]].value;
		if(soloEspacios(tAux))
			{
			alert("El campo " + vCaptions[i] + " es obligatorio.");
			objForm[vCampos[i]].focus();
			return false;
			}
		}
		}
	return true;		
}
//==================================================
function validaCamposNumericos(objForm,vCampos,vCaptions)
//==================================================
{
	var iCant;
	var i;
	var tAux;
	iCant=vCampos.length;
	for(i=0;i<iCant;i++)
		{
		if(!objForm[vCampos[i]].disabled)
		{
		tAux=objForm[vCampos[i]].value;
		if(!SoloNumeros(tAux))
			{
			alert("El campo " + vCaptions[i] + " debe ser numérico.");
			objForm[vCampos[i]].focus();
			return false;
			}
		}
		}
	return true;		
}

//==================================================
function validaCamposCombo(objForm,vCampos,vCaptions)
//==================================================
{
	var iCant;
	var i;
	var tAux;
	iCant=vCampos.length;
	for(i=0;i<iCant;i++)
		{
		if(!objForm[vCampos[i]].disabled)
		{
		if(objForm[vCampos[i]].selectedIndex==0)
			{
			alert("Para el campo " + vCaptions[i] + " debe elegir una opción.");
			objForm[vCampos[i]].focus();
			return false;
			}
		}
		}
	return true;		
}

//==================================================
function validaCamposBit(objForm,vCampos,vCaptions)
//==================================================
{
	var iCant;
	var i;
	var tAux;
	iCant=vCampos.length;
	for(i=0;i<iCant;i++)
		{
		if(!objForm[vCampos[i]][0].disabled)
		{
		
		if(!(objForm[vCampos[i]][0].checked ||objForm[vCampos[i]][1].checked ))
			{
			alert("El campo " + vCaptions[i] + " es obligatorio.");
			objForm[vCampos[i]][0].focus();
			return false;
			}
		}
		}
	return true;		
}

//================================================== 
function soloEspacios(sTexto) 
//==================================================
{
 var i;
 var c;
 for ( i=0; i< sTexto.length; i++) 
  { 
   c = sTexto.substring(i,i + 1);
   if((escape(c)!='%0D') || (escape(c)!='%0A')) 
     if (c !=" ")
        return false;
  } 
return true
}

//================================================== 
function DivOnOff(tImg,tDiv)
//==================================================
{
var objDiv;
var objImg;

var ImgOpen = new Image();
var ImgClose = new Image();
ImgOpen.src='/Cielos_web/Imagenes/SoloMenos.gif';
ImgClose.src='/Cielos_web/Imagenes/SoloMas.gif';

objDiv=document.getElementById(tDiv);
objImg=document.images[tImg];
if (objDiv.style.display == 'none')
		{
		objDiv.style.display = '';
		objImg.src=ImgOpen.src;
		}
else
		{
		objDiv.style.display = 'none';
		objImg.src=ImgClose.src;
		}

}

//'========================================================
function AddCombo(ComboOrigen,ComboDestino)
//'========================================================
{
var i;
var j;
var CantAsignado;
var CantNoAsignado;
var Element;
var PageName; 
var AntValue;

CantNoAsignado=ComboOrigen.length;
CantAsignado=ComboDestino.length;

for(i=CantNoAsignado-1; i>=0; i--)
  {
    AntValue=ComboOrigen.options[0].selected;
	ComboOrigen.options[0].selected=(! AntValue);
	ComboOrigen.options[0].selected=AntValue;
	
	if(ComboOrigen.options[i].selected)
		{
		Element=document.createElement("OPTION");
		PageName=ComboOrigen.options[i].value;
		PageName="+" + PageName.substring(1,PageName.length);
		Element.value=PageName;
		Element.text=ComboOrigen.options[i].text;
		ComboOrigen.remove(i);
		j=0;
		while(j<CantAsignado && Element.text.toLowerCase()>ComboDestino.options[j].text.toLowerCase())
			j++;
		ComboDestino.add(Element,j);
		ComboDestino.options[j].selected=true
		}
  }
}

//'========================================================
function RemoveCombo(ComboOrigen,ComboDestino)
//'========================================================
{
var i;
var j;
var CantAsignado;
var CantNoAsignado;
var Element;
var PageName; 
var AntValue;

CantNoAsignado=ComboDestino.length;
CantAsignado=ComboOrigen.length;
for(i=CantAsignado-1; i>=0; i--)
  {
    AntValue=ComboOrigen.options[0].selected;
	ComboOrigen.options[0].selected=(! AntValue);
	ComboOrigen.options[0].selected=AntValue;
	if(ComboOrigen.options[i].selected)
		{
		Element=document.createElement("OPTION");
		PageName=ComboOrigen.options[i].value;
		PageName="-" + PageName.substring(1,PageName.length);
		Element.value=PageName;
		Element.text=ComboOrigen.options[i].text;
		ComboOrigen.remove(i);
		j=0;
		while(j<CantNoAsignado && Element.text.toLowerCase()>ComboDestino.options[j].text.toLowerCase())
			j++;
		ComboDestino.add(Element,j);
		ComboDestino.options[j].selected=true;
		}
  }
}
//'========================================================
function ComboOrdenSubir(Combo)
//'========================================================
{
var i;
var j;
var CantAsignados=Combo.length;
var Elemento;
var FamiliaID;
var AntValor;
if (CantAsignados!=0)
	{
	Combo.options[0].selected=false;
	for (i=1;i<CantAsignados;i++)
		{
		if(Combo.options[i].selected)
			{
			Elemento=document.createElement("OPTION");
			Elemento=Combo.options[i-1];
			Combo.remove(i-1);
			Combo.add(Elemento,i);
			}
		}
	}
}
//'========================================================
function ComboOrdenBajar(Combo)
//'========================================================
{
var i;
var j;
var CantAsignados=Combo.length;
var Elemento;
var FamiliaID;
var AntValor;
if (CantAsignados!=0)
	{
	Combo.options[CantAsignados-1].selected=false;
	for (i=CantAsignados-2;i>=0;i--)
		{
		if(Combo.options[i].selected)
			{
			Elemento=document.createElement("OPTION");
			Elemento=Combo.options[i+1];
			Combo.remove(i+1);
			Combo.add(Elemento,i);
			}
		}
	}
}

//'--------------------------------------------------------------------------------
function ValidaInputFecha(tPosfijoCampo)
//'--------------------------------------------------------------------------------
{
	var objDia;
	var objMes;
	var objAnio;
	var tFecha;
	
	objDia=document.getElementById("Dia" + tPosfijoCampo);
	objMes=document.getElementById("Mes" + tPosfijoCampo);
	objAnio=document.getElementById("Anio" + tPosfijoCampo);
	tFecha=(objMes.selectedIndex+1).toString() + "/";
	tFecha=tFecha + objDia.value + "/" +objAnio.value;
	if(ValidaFecha(tFecha))
		{
			if(sTrim(objAnio.value).length==4)
				return true;
			else 
				alert("El año debe tener cuatro dígitos");	
		}
	else
		alert("La fecha es incorrecta");		

	objDia.focus();
	return false;
}	

//'--------------------------------------------------------------------------------
function sTrim(tTxt)
//'--------------------------------------------------------------------------------
{
 var tAuxL="";
 var tAuxR="";
 var i;
 var c;
 var iCant;
 iCant=tTxt.length;
 i=0;
 while(i<iCant)
	{
	if(tTxt.substr(i,1)!=" ")
		break;
	i++
	}
while(i<iCant)
	{
	tAuxL=tAuxL+tTxt.substr(i,1)
	i++
	}		
 iCant=tAuxL.length;
 i=iCant-1;
 while(i>=0)
	{
	if(tAuxL.substr(i,1)!=" ")
		break;
	i--
	}
while(i>=0)
	{
	tAuxR=tAuxL.substr(i,1)+  tAuxR;
	i--
	}		
return tAuxR;
 
}

//================================================== 
function ValidaDNI(oDNI)
//================================================== 
{

	if(SoloNumeros(oDNI.value))
		return true;
	oDNI.focus();
	alert("El Campo Documento debe ser numérico");
	return false;
}

//================================================== 
function SoloNumeros(vNum)
//================================================== 
{
	var sAux;
	var iCant;
	var i;
	var c;
	
	sAux=sTrim(vNum);
	iCant=sAux.length;
	for(i=0;i<iCant;i++)
		{			
			c=sAux.substring(i,i+1);
			if(c<"0" || c>"9")
				return false;
		}
	return true;
}

//================================================== 
function ValidaFecha(date)
//================================================== 
	{
  	var err=0;
	var psj=0;
	var fecha,b,d,year;
	

	fecha=date.split("/");
	b = fecha[0];// month
	d = fecha[1];//date
	year = fecha[2];// year
  
	//basic error checking
	if (b<1 || b>12) err = 1;
	if (d<1 || d>31) err = 1;
	if(year.length!=2 && year.length!=4) err=1; 
	if (year<0 || year>3000) err = 1;
	
	//advanced error checking

	// months with 30 days
	if (b==4 || b==6 || b==9 || b==11){
		if (d==31) err=1
	}

	// february, leap year
	if (b==2){
		// feb
		var g=parseInt(year/4)
		if (isNaN(g))  err=1;
		if (d>29) err=1;
		if (d==29 && ((year/4)!=parseInt(year/4))) err=1;
	     }

	if (err==1)
		return false;
	else
		return true;
}

 
 //========================================================= 
function emailCheck (emailStr) 
//========================================================= 
{ 
/* The following pattern is used to check if the entered e-mail address fits the user@domain 
format. It also is used to separate the username from the domain. */

var emailPat=/^(.+)@(.+)$/ 

/* The following string represents the pattern for 
matching all special characters. We don't want to allow special characters in 
the address. These characters include ( ) < > @ , ; : \ " . [ ] */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]" 

/* The following string represents the range of characters allowed in a username or domainname. It 
really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]" 

/* The following pattern represents the range of characters 
allowed as the first character in a valid username or domain. I just made it the 
same as above, but if you want to add a different constraint, you would change 
it here. */
 var firstChars=validChars 
 
 
/* The following pattern applies if the "user" is a quoted string (in which case, there are no rules about which 
characters are allowed and which aren't; anything goes). E.g. "jiminy 
cricket"@disney.com is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
 
/* The following pattern applies for domains that are IP addresses, rather than 
symbolic names. E.g. joe@[123.124.233.4] is a legal e-mail address. NOTE: The 
square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/ 

/* The following string represents at atom (basically a series of non-special characters.) */
var atom="(" + firstChars + validChars + "*" + ")" 

/* The following string represents one word in the typical username. For example, in 
john.doe@somewhere.com, john and doe are words. Basically, a word is either an 
atom or quoted string. */
 var word="(" + atom + "|" + quotedUser + ")" 
// The following pattern describes the structure of the user 
var userPat=new RegExp("^" + word + "(\\." + word + ")*$") 

/* The following pattern describes the structure 
of a normal symbolic domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$") 

/* Finally, let's start trying to figure out if the supplied address is valid. */
/* Begin with the course pattern to simply break up user@domain into different pieces that are 
easy to analyze. */
var matchArray=emailStr.match(emailPat)
if(matchArray==null) 
 { /* Too many/few @'s or something; basically, this address 
   doesn't even fit the general mould of a valid e-mail address. */

  return false
} 

 var user=matchArray[1] 
 var domain=matchArray[2] 
 // See if "user" is valid 
 if(user.match(userPat)==null) 
  { // user is not valid 
   //alert("El nombre de usuario no parece valido.") 
   return false
  } 
  /* if the e-mail address is at an IP address 
(as opposed to a symbolic host name) make sure the IP address is valid. */
 var IPArray=domain.match(ipDomainPat) 
 if (IPArray!=null) 
 { // this is an IP address 
  for (var i=1;i<=4;i++) 
   { 
    if (IPArray[i]>255) 
     { 
      //alert("The IP address is invalid!") 
       return false 
     }
   } 
return true 
} 

// Domain is symbolic name 
var domainArray=domain.match(domainPat)
if (domainArray==null) 
 { //alert("This domain is invalid")
   return false 
 } 
 /* domain name seems valid, but now make sure that it ends in a three-letter word (like com, edu, gov) or a 
two-letter word, representing country (uk, nl). If there's a country code at the end of the address, the full domain must include a hostname and category (e.g. 
host.co.uk or host.pub.nl). If it ends in a .com or something, make sure there's 
a hostname.*/ 

/* Now we need to break up the domain to get a count of how many 
atoms it consists of. */ 
var atomPat=new RegExp(atom,"g") 
var domArr=domain.match(atomPat) 
var len=domArr.length
 if(domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3)
  { // the address must end in a two letter or three letter word. 
 // alert("the end part of the domain address must have three letters  , or two if it's a country.") 
  return false 
  } 
  
  /* If it ends in a country code, we want to make sure 
there are at least 2 atoms preceding it (representing host and category (i.e. 
com, gov, etc.)) */
 if (domArr[domArr.length-1].length==2  && len<3) //|| len<3
 { 
  return false; 
  } 
  
 /* If it just ends in 
.com, .gov, etc., make sure there's a host name. This case can never actually 
happen because earlier checks take care of this implicitly, but we'll do it 
anyway. */
 if (domArr[domArr.length-1].length==3 && len<2)// || len<2 
 {
  var errStr="This address has not have a server" 
 // alert(errStr) 
  return false 
  } 
  // If we've gotten this far, everything's valid! 
  return true;
  
  }

