function showAlert(message) {
   if (!(message==null || message=="")) alert(message);
}
//trim and return a string
function trim(str1) {
    start=0;
    end=0;
    if (str1=="") return str1;
    var str=new String(str1);
   	for (var i = 0; i < str.length; i++) {
   		if (str.charAt(i) != " ") { start = i; break;}
   	}
   	for (var i = str.length-1; i >=0; i--) {
   		if (str.charAt(i) != " ") { end = i+1; break;}
   	}
   	return str.substring(start,end);
}
// checks if a field is a positive integer
function isPosInteger(field,message) {
 value=trim(field.value);
 if (!isPosIntegerV(value,message)) { field.focus(); return false; }
 return true;
}

// checks if a value is a positive integer
function isPosIntegerV(value,message) {
	value=trim(value);
	inputStr = value.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			showAlert(message);
			return false
		}
	}
	return true
}
// checks if a field is empty
function isEmpty(field,message) {
 if (isEmptyV(field.value,message)) { field.focus(); return true 
 }else return false;
}

// checks if a value is empty
function isEmptyV(value,message) {
 value=trim(value);
 if (value==null || value=="") { showAlert(message); return true; }
 return false;
}

//Another function to check if the field  is a number
function IsNumber(theField){
  var checkOK = "0123456789";
  var checkStr = theField;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++){
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length){
      allValid = false;
      break;
    }
  }
  if (!allValid){
  	sMgs = "un valor number en el campo: ";
    return (false);
  }else{
    return (true);
  }
}

//verify  a date
function ValidateDate(dayField,monthField,yearField){
	day = dayField.value;
	month = monthField.value;
	year = yearField.value;
	// Parsea global de la fecha: formato (dd/mm/aaaa)
	strDate = day+month+year;

	sMsg = "it must be (mm/dd/aaaa)";
	// Valida tamano de la fecha.
	intFecLen = strDate.length;
	if(intFecLen < 6){ dayField.focus();return false;}
	
	
	// Valida que no halla / en el dia.
	sMsg = "debe ser un nro.";
	if(! IsNumber(day)){dayField.focus(); return false;}
	
	// Valida que no halla / en el mes.	
	sMsg = "must be a number.";
	if(! IsNumber(month)){ monthField.focus();return false;}
	
	// Valida que no halla / en el ano.
	sMsg = "must be a number.";
	if(! IsNumber(year)){yearField.focus(); return false;}
	
	
	// Se fija si es una fecha valida
	// Anio es valido siempre que sea positivo
	sMsg = "Wrong year";
	yearval = new Date();

	yearval = yearval.getFullYear();

	if ((year < 1) || (year > yearval)){
		yearField.focus();
 
    		return false;
 	}
	sMsg = "wrong month";
	if (month < 1){ monthField.focus();return false;}
	
	sMsg = "Wrong day";
	if (day < 1){ dayField.focus();return false;}
	
       
	sMsg = "the month is incorrect";
	if (month > 12){ monthField.focus();return false;}
    
	sMsg = "Wrong day";
    	if (day > 31){ dayField.focus();return false;}
    
	if (month ==2) {

          if ( !(year % 4) && ( year % 100 || ! (year % 400))){
 
            if (day > 29){ dayField.focus();return false;}
          } else {
            if (day > 28){ dayField.focus();return false;}
          }           
	}
    
	if (month==4) {
    	    if (day > 30){ dayField.focus();return false;}
	}           
    
	if (month==6) {
    	    if (day > 30){ dayField.focus();return false;}
	}           

	if (month==9) {
    	    if (day > 30){ dayField.focus();return false;}
	}           
		
	if (month==11) {
    	    if (day > 30){ dayField.focus();return false;}
	}           
    
	return true;
}

function stripDelimiters(val, delimiters) {
   var finalString = "";

   for (var i = 0; i < val.length; i++) {
      var c = val.charAt(i);
      if (delimiters.indexOf(c) == -1) {
         finalString += c;
      }
   }
   return finalString;
}
function validateEmail(email) {
   var validEmailDelimiters = " ";
   ea = stripDelimiters(email.value,validEmailDelimiters);
   //var validEmailCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

   //Verify that email is not null
   if ((email.value==null)||(email.value=="")) {
      return false;
   }

   //Verify that there are no consecutive periods in email address
   if (ea.indexOf('..') > -1) {
      return false;
   }

   var strEmail2 = ea.split("@");

   if ( (strEmail2.length == 2) && (strEmail2[0].length > 0) ){ 
      var strEmail3 = strEmail2[1].split(".");
      if ( (strEmail3.length > 1) && (strEmail3[0].length > 0) ){
         if ((strEmail3[strEmail3.length-1].length < 1) || (strEmail3[strEmail3.length-1].length > 4)) {
            return false;
         }
      } else {
         return false;
      }
   } else {
      return false;
   }

//   theForm.elements[email.name].value = ea;
   return true;
}

