var strTMP="";

var Ajax = false;
if (window.XMLHttpRequest) 
{
	Ajax = new XMLHttpRequest();
} 
else if (window.ActiveXObject) 
{
	Ajax = new ActiveXObject("Microsoft.XMLHTTP");
}

function CargarAjax(method, url, div){
	if(Ajax){
		Ajax.open(method, url +"&rRand=" + (new Date()) / 1);
		Ajax.onreadystatechange = function(){
			if (Ajax.readyState == 4 && Ajax.status == 200) 
			{
				document.getElementById(div).innerHTML = Ajax.responseText;
			}
		}
		Ajax.send(null);
	} 
}
///
function isMail(_email) {
     var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,4}$/i
     return emailReg.test(_email);
  }
///
function tieneDatos(Valor) { 
 for (var i=0; i<Valor.length; i++) { 
   if ((" \t\n\r").indexOf(Valor.charAt(i))==-1) return true; 
   } 
 return false; 
}
function compareFields(field1,field2)
{alert('gfghj');
  if(field1==field2)
  {
    return true;
  }
  else
  {
        return false;
  }
}
function EsTextoFecha(objTexto, bolPermitirNulo)
{
	objTexto.value = Trim(objTexto.value);
	if (bolPermitirNulo && objTexto.value.length == 0) {
		return true;
	}
	if (EsFecha(objTexto.value)) {
		return true;
	}
	else {
		return false;
	}
}

function Trim(strValue) 
{
	var objRegExp = /^(\s*)$/;
    //check for all spaces    
	if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)          
	   		return strValue;    
	}    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;   
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
	}  
	return strValue;
}

function EsFecha(strValor)
{
 	var objRegExp = /^\d{2}\/\d{2}\/\d{4}$/ 
  	//check to see if in correct format  
	if(!objRegExp.test(strValor)) {
    	return false; //doesn't match pattern, bad date  
	}
	else{
    	//var strSeparator = strValor.substring(2,3) //find date separator
    	var arrayDate = strValor.split('/'); //split date into month, day, year
    	//create a lookup for months not equal to Feb.
    	var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        	'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
		var intDay = parseInt(arrayDate[0], 10); 
    	//check if month value and day value agree
    	if(arrayLookup[arrayDate[1]] != null) {
      		if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
        		return true; //found in lookup table, good date    
		}    
    	//check for February    
		var intYear = parseInt(arrayDate[2],10);
    	var intMonth = parseInt(arrayDate[1],10);
    	if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      		return true; //Feb. had valid number of days  
    }
  	return false; //any other values, bad date
}

function EsTextoEntero(objTexto, bolPermitirNulo, intMinimo, intMaximo)
{
	objTexto.value = Trim(objTexto.value);
	if (bolPermitirNulo && objTexto.value.length == 0) {
		return true;
	}
	if (EsEntero(objTexto.value, intMinimo, intMaximo)) {
		objTexto.value = parseInt(objTexto.value,10);
		return true;
	}
	else {
		return false;
	}
}

function EsTextoNumeric(objTexto, bolPermitirNulo, intMinimo, intMaximo)
{
	objTexto.value = Trim(objTexto.value);
	objTexto.value = objTexto.value.replace(/,/g,".");
	if (bolPermitirNulo && objTexto.value.length == 0) {
		return true;
	}
	if (EsNumeric(objTexto.value, intMinimo, intMaximo)) {
		objTexto.value = parseFloat(objTexto.value);
		return true;
	}
	else {
		return false;
	}
}


function EsEntero(strValor, intMinimo, intMaximo)
{
	var objRegExp = /(^(\+|-)?\d\d*$)/;
	var intValor;
	
	if (objRegExp.test(strValor)) {
		intValor = parseInt(strValor,10);
		if (! isNaN(intMinimo)) {
			if (intValor < intMinimo)
				return false
		}
		if (! isNaN(intMaximo)) {
			if (intValor > intMaximo)
				return false;
		}
		return true;
	}
	else {
		return false;
	}
}

function EsNumeric(strValor, numMinimo, numMaximo)
{
	var objRegExp = /(^(\+|-)?\d\d*\.\d*$)|(^(\+|-)?\d\d*$)/; 
	var numValor;
	
	if (objRegExp.test(strValor)) {
		numValor = parseFloat(strValor);
		if (! isNaN(numMinimo)) {
			if (numValor < numMinimo)
				return false
		}
		if (! isNaN(numMaximo)) {
			if (numValor > numMaximo)
				return false;
		}
		return true;
	}
	else {
		return false;
	}
}



function ChkLogin()
{
	f = document.frm_login;
	f.goes.disabled=true;
	if(!tieneDatos(f.p_usuario.value))
	{
		alert("Debe ingresar su nombre de Usuario.");
		f.p_usuario.focus();
		f.goes.disabled = false;
		return false;
	}
	
	if(!tieneDatos(f.p_clave.value))
	{
		alert("Debe ingresar su Clave.");
		f.p_clave.focus();
		f.goes.disabled = false;
		return false;
	}

	
	f.submit();
}
function Chkcambiar()
{
	f = document.frm_clave;
	f.goes.disabled=true;
	if(!tieneDatos(f.p_usuario.value))
	{
		alert("Debe ingresar su nombre de Usuario.");
		f.p_usuario.focus();
		f.goes.disabled = false;
		return false;
	}
		
	f.submit();
}
function Chkclave2()
{
	f = document.frm_clave2;
	f.goes.disabled=true;
	if(!tieneDatos(f.p_Contrasena.value))
	{
		alert("Debe ingresar una Contraseña.");
		f.p_Contrasena.focus();
		f.goes.disabled = false;
		return false;
	}
	
	if(f.p_Contrasena.value != f.p_Contrasena2.value)
  	{
    	alert("Las contraseñas no coinciden.");
		f.p_Contrasena.select();
		f.p_Contrasena.focus();
		f.goes.disabled = false;
		return false;
  	}	
	f.submit();
}

function Chkusuario()
{
	f = document.frm;
	f.goes.disabled=true;
	if(!tieneDatos(f.p_username.value))
	{
		alert("Debe ingresar un nombre de Usuario.");
		f.p_username.focus();
		f.goes.disabled = false;
		return false;
	}	
	if(!tieneDatos(f.p_email.value))
	{
		alert("Debe ingresar un Email.");
		f.p_email.focus();
		f.goes.disabled = false;
		return false;
	}
	if(!isMail(f.p_email.value))
	{
		alert("Debe ingresar un Email Valido.");
		f.p_email.focus();
		f.goes.disabled = false;
		return false;
	}
	if(f.p_action.value == 'add')
	{
	if(!tieneDatos(f.p_password.value))
	{
		alert("Debe ingresar una Contraseña.");
		f.p_password.focus();
		f.goes.disabled = false;
		return false;
	}
	
	if(f.p_password.value != f.p_password2.value)
  	{
    	alert("Las contraseñas no coinciden.");
		f.p_password.select();
		f.p_password.focus();
		f.goes.disabled = false;
		return false;
  	}
	}
	else 
	{
		if(f.p_password.value != '' || f.p_password2.value != '')
		{
								
								if(!tieneDatos(f.p_password.value))
								{
								alert("Debe ingresar una Contraseña.");
								f.p_password.focus();
								f.goes.disabled = false;
								return false;
								}
	
								if(f.p_password.value != f.p_password2.value)
  								{
    							alert("Las contraseñas no coinciden.");
								f.p_password.select();
								f.p_password.focus();
								f.goes.disabled = false;
								return false;
  								}
									
		}
	}
	f.submit();
}

function Chkla()
{
	f = document.frm;
	f.goes.disabled=true;
	if(!tieneDatos(f.p_titulo.value))
	{
		alert("Debe ingresar un Titulo.");
		f.p_titulo.focus();
		f.goes.disabled = false;
		return false;
	}	
	if(!tieneDatos(f.p_contenido.value))
	{
		alert("Debe ingresar Contenido.");
		f.p_contenido.focus();
		f.goes.disabled = false;
		return false;
	}	
	f.submit();
}
function ChkContacto()
{
	f = document.frm_contacto;
	f.goes.disabled=true;
	if(!tieneDatos(f.p_nombre.value))
	{
		alert("Debe ingresar su nombre.");
		f.p_nombre.focus();
		f.goes.disabled = false;
		return false;
	}
	
	if(!isMail(f.p_email.value))
	{				
		alert("Ingrese un e-mail válido.");
		f.p_email.focus();
		f.goes.disabled = false;
		return false;
	}
	
	if(!tieneDatos(f.p_consulta.value))
	{
		alert("Debe ingresar la consulta o comentario.");
		f.p_consulta.focus();
		f.goes.disabled = false;
		return false;
	}
	f.submit();
}

