var whitespace = " \t\n\r";

function getNChars(n,ch){
	var i,newStr;

	newStr = "";
	for (i = 0; i < n; i++){
		newStr = newStr + ch;
	}
	return newStr;
}

function removeChar(s,ch){
	var i,c,newStr;

	newStr = "";
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (c != ch){
			newStr = newStr + c
		}
	}
	return newStr;
}

function isWhitespace(s){
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1)
			return false;
	}
	return true;
}

function isEmail(s){
	if (isEmpty(s))
		if (isEmail.arguments.length == 1) return true;
		else return (isEmail.arguments[1] == true);
	if (isWhitespace(s)) return false;

	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")){
		i++
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;

	while ((i < sLength) && (s.charAt(i) != ".")){
		i++
	}

	if ((i >= sLength - 1) || (s.charAt(i) != "."))	return false;
	else return true;
}

function LTrim(str){
	while(str.length > 0 && str.charAt(0) == " "){
		str = str.substr(1,str.length)
	}
	return str
}

function RTrim(str){
	while(str.length > 0 && str.charAt(str.length-1) == " "){
		str = str.substr(0,str.length-1);
	}
	return str
}

function Trim(theStr){
	theStr = RTrim(theStr);
	theStr = LTrim(theStr);
	return theStr
}

function isEmpty(s){
	return ((s == null) || (s.length == 0));
}

function isDigit (c){
	return ((c >= "0") && (c <= "9"));
}

function isInteger (s){
	var i;

	if (isEmpty(s)) {return false;}

	for (i = 0; i < s.length; i++){
		if (!isDigit(s.charAt(i))){return false;}
	}
	return true;
}

function isDecimal (s){
	var i,nums;

	if (isEmpty(s)) {return false;}
	nums = s.split(".");

	if (nums.length == 1){
		return isInteger(nums[0]);
	}else if(nums.length == 2){
		return isInteger(nums[0]) && isInteger(nums[1]);
	}
	return false;
}

function isFieldEmpty(theForm,fieldVal,fieldName,alertName){
	if (isEmpty(fieldVal)){
		alert("Debes ingresar "+alertName);
		theForm.elements[fieldName].focus();
		return true;
	}
	return false;
}

function checkEmail(emailStr){
	var result = false

	if (!isEmpty(emailStr)){
		result = isEmail(emailStr);
	}
	return result;
}

function isLeapYear(year){

	if (year % 4 == 0){
		if (year % 100 == 0 && year % 400 != 0){
			return false;
		}
		return true;
	}
	return false;
}

function checkDate(day,month,year){
	var febDays = 28;

	if (isNaN(day))
		return false;
	if (isNaN(month))
		return false;
	if (isNaN(year))
		return false;
	if (month < 1 || month > 12)
		return false;
	if (year < 1800)
		return false;
	if (isLeapYear(year)) {febDays = 29;}
	if (month == 2 && day > febDays){return false;}
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30){return false;}
	if (day > 31) {return false;}

	return true;
}

function esmenor(fecha1,fecha2)
{
	a11 = parseFloat(fecha1.substr(4, 4));

	d11 = parseFloat(fecha1.substr(0, 2));

	m11 = parseFloat(fecha1.substr(2, 2));

	a21 = parseFloat(fecha2.substr(4, 4));

	d21 = parseFloat(fecha2.substr(0, 2));

	m21 = parseFloat(fecha2.substr(2, 2));

	if (a11 < a21)
		return true;
	else
		if (a11 == a21)
			if (m11 < m21)
				return true;
			else
				if (m11 == m21)
					if (d11 < d21)
						return true;
					else
						return false;
				else
					return false;
		else
			return false;
}