
/* ===================================================================== */

function isemail( v ) 
{
	var reg  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
	return reg.test( v );
}

/* ===================================================================== */

function isblank(val)
{
	if(val==null) return true;
	for(var i=0;i<val.length;i++)
	{
		if
		(	
				(val.charAt(i) != ' ') 
			&& 	(val.charAt(i) != "\t") 
			&& 	(val.charAt(i) != "\n") 
			&& 	(val.charAt(i) != "\r")
		) return false;
	}
	return true;
}

/* ===================================================================== */

function iswrote( field )
{
	var v = field.value;
	if( isblank(v) )
	{
		try
		{
			field.value = "";
		}catch(e){}
		field.focus();
		return false;
	}

	return true;
}

/* ===================================================================== */

function isselected( field )
{
	if( field.selectedIndex == 0 )
	{
		field.focus();
		return false;
	}

	return true;
}

function abrir(name){
	var winleft = (screen.width - 900) / 2;
	var winUp = (screen.height - 600) / 2;
	var winProp;
	winProp = 'width=900'+',height=600'+',left='+winleft+',top='+winUp+',scrollbars=yes,resizable=yes'
	ventana = window.open(name, "prod", winProp); 
	if (ventana.opener == null) ventana.opener = self;
}

function abrir_ventana(name){
		ventana = window.open(name, "ventana", "width=5,height=5,dependent=yes,screenX=0,screenY=0,titlebar=no,directories=no,menubars=no,status=no,scrollbars=no,resizable=no"); 
		if (ventana.opener == null) ventana.opener = self;
	}

function abrir_foto(name){
	var winleft = (screen.width - 400) / 2;
	var winUp = (screen.height - 300) / 2;
	var winProp;
	winProp = 'width=400'+',height=300'+',left='+winleft+',top='+winUp+',scrollbars=no,resizable=no'
	ventana = window.open(name, "foto", winProp); 
	if (ventana.opener == null) ventana.opener = self;
}

function money_format(oText) {
	var v = oText.value.replace(/\./g, ''), i = v.length, j = i - 1, chr, $str = '';
	if (/\D/.test(v)) {
	alert('Por favor ingrese números unicamente.');
	oText.value = oText.prevValue || '';
	return false;
	}
	while (chr = v.charAt(--i))
	$str = (((i - j) % 3 == 0 && (i != j)) ? chr + '.' : chr) + $str;
	oText.value = oText.prevValue = $str;
	return true;
}

function chequeaRequeridos(frm, CamposRequeridos, Descripcion) {
	var alertMsg = "The following fields must be filled: \n";


	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < CamposRequeridos.length; i++){
		var obj = frm.elements[CamposRequeridos[i]];
		if (obj) {
			switch(obj.type) {
			case "select-one":
				if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == "" || obj.value == ""){
					alertMsg += " - " + Descripcion[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + Descripcion[i] + "\n";
				}
				break;
						
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + Descripcion[i] + "\n";
				}
				break;
				
			case "text":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + Descripcion[i] + "\n";				
					
				}
				break;
				
			case "checkbox":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + Descripcion[i] + "\n";				
					
				}
				break;	
	
			case "file":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + Descripcion[i] + "\n";				
					
				}
				break;
	
			case "password":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + Descripcion[i] + "\n";				
					
				}						
				break;
				
			default:
			}
			
			if (obj.type == undefined) {
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++)
					if (obj[j].checked) blnchecked = true;
				
				if (!blnchecked)
					alertMsg += " - " + Descripcion[i] + "\n";
			}
		}
	}

	if (alertMsg.length == l_Msg) {
		return true;
	} else {
		alert(alertMsg);
		return false;
	}
}

function longitud_login_password(frm) {
	
	var errorMsg = "";
	var errorMsgLong = "";

	var pass;
	pass = frm.password.value; 
	
	var confirmacion;
	confirmacion = frm.confirmacion.value;
	
	var login;
	login = frm.login.value;

	if (login.length < 6) {
		
		errorMsg += "\n Nombre de usuario \t\t- El Nombre de usuario debe tener al menos 6 caracteres";
		frm.login.value =  "";
	
	} else {
		
		if (isAlphanumeric(login) == false){
			
			errorMsg += "\n Nombre de usuario \t\t- El Nombre de usuario solo puede tener numeros y letras";
			frm.login.value =  "";
		}
	}
	
	if (pass.length < 6 ){
			
		errorMsg += "\n Contraseña \t\t- La Contraseña debe tener al menos 6 caracteres";
 
 		} else {
			
			if (isAlphanumeric(pass) == false){
			
			errorMsg += "\n Contraseña \t\t- La Contraseña solo puede tener numeros y letras";
			
			} else {
			
				if (confirmacion != pass) {
	
				errorMsg += "\n Confirmación \t\t- No coinciden la Contraseña y la Confirmación";
				frm.password.value = "";
				frm.confirmacion.value =  "";
				
				}
			}
			
		
	 }	
		
	if ((errorMsg != "") || (errorMsgLong != "")){
		msg = "_______________________________________________________________\n\n";
		msg += "El formulario no ha sido enviado debido a que se presentaron problemas.\n";
		msg += "Por favor corrija los problemas y vueva a enviar el formulario.\n";
		msg += "_______________________________________________________________\n\n";
		msg += "Los siguientes campos necesitan ser corregidos: \n";
		
		errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
		return false;
	}

	return true;
	
}

function validaEmail(frm) {
	
	var errorMsg = "";
	var errorMsgLong = "";
	
	
	//Verifica el email
			
	if (frm.email.value != '') {
	var checkEmail = "@.";
	var checkStr = frm.email.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkEmail.length;  j++)
	{
		if (ch == checkEmail.charAt(j) && ch == "@")
		EmailAt = true;
		if (ch == checkEmail.charAt(j) && ch == ".")
		EmailPeriod = true;
		if (EmailAt && EmailPeriod)
		break;
		if (j == checkEmail.length)
		break;
	}
	// if both the @ and . were in the string
		if (EmailAt && EmailPeriod)
		{
			EmailValid = true
			break;
		}
	}
	if (!EmailValid) {
	  errorMsg += "\n Email \t\t- You must enter a valid Email address";
	}
	}
	
	if ((errorMsg != "") || (errorMsgLong != "")){
		msg = "";
		
		
		errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
		return false;
	}

	return true;
}


function validaEmailpara(frm) {
	
	var errorMsg = "";
	var errorMsgLong = "";
	
	
	//Verifica el email
			
	if (frm.email_para.value != '') {
	var checkEmail = "@.";
	var checkStr = frm.email_para.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkEmail.length;  j++)
	{
		if (ch == checkEmail.charAt(j) && ch == "@")
		EmailAt = true;
		if (ch == checkEmail.charAt(j) && ch == ".")
		EmailPeriod = true;
		if (EmailAt && EmailPeriod)
		break;
		if (j == checkEmail.length)
		break;
	}
	// if both the @ and . were in the string
		if (EmailAt && EmailPeriod)
		{
			EmailValid = true
			break;
		}
	}
	if (!EmailValid) {
	  errorMsg += "\n Email \t\t- You must enter a valid Email address";
	}
	}
	
	if ((errorMsg != "") || (errorMsgLong != "")){
		msg = "";
		
		
		errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
		return false;
	}

	return true;
}


function Validar (frm, fecha, deshabilitar) 
{
 
 if (chequeaRequeridos(frm, cmpReq, desc))
  {
    if (deshabilitar == null) frm.Submit.disabled = true;
    return true;
  }
  else
    return false;
}   

function ValidarEmail (frm, fecha, deshabilitar) 
{
	
  if (chequeaRequeridos(frm, cmpReq, desc) && validaEmail(frm))
  {
    if (deshabilitar == null) frm.Submit.disabled = true;
    return true;
  }
  else
    return false;
}   

function ValidarEmailpara (frm, fecha, deshabilitar) 
{
	
  if (chequeaRequeridos(frm, cmpReq, desc) && validaEmailpara(frm))
  {
    if (deshabilitar == null) frm.Submit.disabled = true;
    return true;
  }
  else
    return false;
}   