var whitespace = " \t\n\r";

function isNumero(str){
  var flag=true;
  var i=0;
  var nums=new Array(1,1,1,1,1,1,1,1,1,1);
  while (i<str.length && flag){
    flag= (nums[str.charAt(i++)]!=null);
  }
  return flag;
}     

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 false;
		else
			//return (isEmail.arguments[1] == true);
	
	if (isWhitespace(s))
		return false;
	
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")){
		if (i != 0 && i != sLength-1 && s.charAt(i) == ' '){
			return false;
		}
		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 checkFecha(day,month,year){
	var febDays = 28;
	
	;
	if (day < 1 || day > 31) {return false;}
	if (month < 1 || month > 12) {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;}
	
	return true;
}

function reloadTopFrame(){
	if (typeof(parent.menuFrame) != "undefined" && !parent.menuFrame.isLoggedOn){
		parent.menuFrame.location.href="/3deseos/frames/top.jsp?dummy="+(new Date()).getTime();
	}
}

function alertNotLogged(){
	alert("Tu session como usuario a expirado. Por favor vuelve a logearte.");
	
	if (typeof(parent.menuFrame) != "undefined" && parent.menuFrame.isLoggedOn){
		parent.menuFrame.location.href="/3deseos/frames/top.jsp?dummy="+(new Date()).getTime();
	}
}

function checkNotLogged(){
	if (typeof(parent.menuFrame) != "undefined" && parent.menuFrame.isLoggedOn){
		alert("Tu sesion como usuario a expirado. Por favor vuelve a logearte.");
		parent.menuFrame.location.href="/3deseos/frames/top.jsp?dummy="+(new Date()).getTime();
	}
}

function selectall(theForm,total,whatAction){
	var i, tmpBox, estado, nuevoestado;
	var choosenBoxes=0;
		
	for(i = 0; i < total; i++){
		tmpBox = eval("theForm.checkbox_"+i);
		if (typeof(tmpBox) != "undefined"){
			if (whatAction =="selectAll"){
				tmpBox.checked = theForm.todos.checked;
			}
			if (tmpBox.checked){
				choosenBoxes++;
			}
		}
	}
	
	if (whatAction =="deleteMessage" && choosenBoxes < 1){
		alert("Debes elegir un o mas mensajes!")
	} else if (whatAction =="deleteMessage"){
		theForm.action.value=whatAction;
		theForm.submit();
	}else{
		return true;
	}
	return false;
}




