// FUNCIONES Generales de Javascript

// Programmer = "Diego A. Palmeira"
// Creation Date = "10/09/2002"
// Last Modified = "__/__/____"

// ********************************************************************************
// Funcion para validar el email
function EmailValidation(pEmail, pAlertbox)
{
	with (pEmail)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos<2) 
		{
			if (pAlertbox!=""){alert(pAlertbox); return false;} 
		}
		else {return true;}
	}
}
// ********************************************************************************
// Funcion para validar si un campo esta vacío
function EmptyValidation(pElement, pAlertbox)
{
	with (pElement)
	{
		if (value==null || value=="")
		{
			if (pAlertbox!="") 
			{
				alert(pAlertbox);
			} 
			return false;
		}
		else 
		{
			return true;
		}
	}
}
// ********************************************************************************
// Funcion para validar si un campo no es numérico, y sean positivos 
function NoNumericoEx(pElement, pAlertbox)
{
	with (pElement)
	{
		if (isNaN(value))
		{
			if (pAlertbox!="") 
			{
				alert(pAlertbox);
			} 
			return false;
		}
		else 
		{
			if (parseInt(value,10)<=0)
			{
				if (pAlertbox!="") 
				{
					alert(pAlertbox);
				} 
				return false;
			}
			return true;
		}
	}
}
// ********************************************************************************
// Funcion para validar si un campo no es numérico con punto decimal
function NoNumericoFloat(pElement, pAlertbox)
{
	with (pElement)
	{
		if (isNaN(value))
		{
			if (pAlertbox!="") 
			{
				alert(pAlertbox);
			} 
			return false;
		}
		else 
		{
			if (parseFloat(value)<=0)
			{
				if (pAlertbox!="") 
				{
					alert(pAlertbox);
				} 
				return false;
			}
			return true;
		}
	}
}
// ********************************************************************************
// Funcion para validar si un campo no es numérico
function NoNumerico(pElement, pAlertbox)
{
	with (pElement)
	{
		if (isNaN(value)==true)
		{
			if (pAlertbox!="") 
			{
				alert(pAlertbox);
			} 
			return false;
		}
		else 
		{
			return true;
		}
	}
}
// ********************************************************************************
// Funcion para validar si un campo no es numérico con formato de moneda
function IsCurrency(pElement, pAlertbox)
{
	with (pElement){
		var strResult = value;
		strResult = strResult.replace(".","*");
		strResult = strResult.replace(",",".");
		strResult = strResult.replace("*",".");
		if (isNaN(strResult)){
			if (pAlertbox!=""){alert(pAlertbox);} 
			return false;
		}
		else{
			if (parseFloat(strResult)<=0){
				if (pAlertbox!=""){alert(pAlertbox);} 
				return false;
			}
			return true;
		}
	}
}
// ********************************************************************************
// Funcion para validar Fechas
function IsValidDate(dateStr, pAlertbox)
{
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null)
	{
		if (pAlertbox!=""){alert(pAlertbox);} 
		// alert("Date is not in a valid format.")
		return false;
	}
	day = matchArray[1]; // parse date into variables
	month = matchArray[3];
	year = matchArray[4];
	if (day < 1 || day > 31) 
	{
		if (pAlertbox!=""){alert(pAlertbox);} 
		// alert("Day must be between 1 and 31.");
		return false;
	}
	if (month < 1 || month > 12)
	{ // check month range
		if (pAlertbox!=""){alert(pAlertbox);} 
		// alert("Month must be between 1 and 12.");
		return false;
	}
	if (year < 1950) 
	{
		if (pAlertbox!=""){alert(pAlertbox);} 
		// alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31)
	{
		if (pAlertbox!=""){alert(pAlertbox);} 
		// alert("Month "+month+" doesn't have 31 days!")
		return false;
	}
	if (month == 2)
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap))
		{
			if (pAlertbox!=""){alert(pAlertbox);} 
			// alert("February " + year + " doesn't have " + day + " days!");
			return false;
	    }
	}
	return true;
}
// ********************************************************************************
// Funcion para validar un número de teléfono
function CheckPhoneNumber(TheNumber, pAlertbox)
{
	var GoodChars = "0123456789()-+ "
	var i = 0
	if (TheNumber.value=="")
	{
		// Return false if number is empty
		if (pAlertbox!=""){alert(pAlertbox);}
		return false;
	}
	for (i =0; i <= TheNumber.value.length -1; i++)
	{
		if (GoodChars.indexOf(TheNumber.value.charAt(i)) == -1) {
			// Note: Remove the comments from the following line to see this for loop in action.
			// alert(TheNumber.value.charAt(i) + " is no good.")
			if (pAlertbox!=""){alert(pAlertbox);} 
			return false;
		} 
	}
	return true;
}
// ********************************************************************************
// Funcion para validar la entrada de las bùsquedas con solo nùmeros y letras.
	function KeywordValidation(pEntered, Alertbox)
	{
		strRegExp= /^[a-zA-Z0-9]/;
		var Data=pEntered.value;
		if (!strRegExp.test(Data))
		{
			if (Alertbox) 
				{alert(Alertbox); return false;}
		}
		else {return true;}
	}
// ********************************************************************************
// Funcion que abre una ventana nueva no resizable.
	function OpenWindow(pURL,pWidth,pHeight,pResizable,pWinName){
		var winTop = (screen.height - pHeight)/2;
		var winLeft = (screen.width - pWidth)/2;
		window.open(pURL,pWinName,"toolbar=no,statusbar=no,scrollbars=yes,menubar=no,width="+pWidth+",height="+pHeight+",resizable="+pResizable+",top="+winTop+",left="+winLeft);
		return false;
	}
// ********************************************************************************

