//---------------------- Check Data and return error ----------------------------------------------
function errorsFound(errorMsg){
	if (errorMsg != '') {
		alert ("Por favor, compruebe los siguientes errores:\n\n" + errorMsg);
		return true;
	} else return false;
}

function isValidData( allowNull, fieldName, fieldObj, checkType){
	var fieldValue= trimField(fieldObj);
	var dataType = checkType.toLowerCase();
	var errorMsg = '';
	if (fieldValue == '') {
		if (!allowNull) errorMsg = "- '" + fieldName + "' no puede estar vacio.\n";
	} else if (dataType=="int") {
		if (!isInt(fieldValue)) errorMsg = "- '" + fieldName + "' tiene que ser un número válido sin punto(.) ó coma(,).\n";
	} else if (dataType=="float" || dataType=="currency"){
	  if (!isFloat(fieldValue)) errorMsg = "- '" + fieldName + "' tiene que ser un número válido.\n";
	} else if (dataType=="email"){
		if (!isEmail(fieldValue)) errorMsg = "- '" + fieldName + "' tiene que ser un email válido.\n";		
	} else if (dataType=="digits"){
		if (!isDigit(fieldValue)) errorMsg = "- '" + fieldName + "' contiene digitos incorrectos.\n";
	}
	if (errorMsg.length>0) {
		//fieldObj.focus();
		fieldObj.select();
	}	
	return errorMsg;	
}

// ----------------------- Clean Data -------------------------------------
function  trimField(fieldObj){
	var cleaned =	trim(fieldObj.value);
	fieldObj.value = cleaned;
	return cleaned;
}

function trim(strParam){
  target = strParam;
  while( (target.length > 0) && (target.indexOf(' ') == 0) ){
	  target = target.substr(1, target.length - 1);
	}
  while( (target.length > 0) && (target.lastIndexOf(' ') == target.length - 1) ){
	  target = target.substr(0, target.length - 1);
	}
	return(target)
}

function maskQuote(strToClean){
  Masked= strToClean;
  QuotePos= strToClean.indexOf("'");
  if (QuotePos>=0){
    LeftStr= strToClean.substring(0,QuotePos);
    RightStr= strToClean.substring(QuotePos + 1,strToClean.length);
    Masked= LeftStr + "''" + MaskQuote(RightStr)  // recursividad
   }
  return Masked
}
//-------------------- Validate Data Types --------------------------------------------
function validDate(aYear, aMonth, aDay){
  var actualDate = new Date(parseInt(aYear), parseInt(aMonth) - 1, parseInt(aDay));
  return( parseInt(aDay) == parseInt(actualDate.getDate()) );
}

function isEmail(email){
   p=email.indexOf('@');
   return  !(p<1 || p==(email.length-1))
}

function isInt(number){
  num = parseInt(number);
  return (number == '' + num)
}

function isDigit(number){
  var ValidDigits= new String("01234567890");
  for (i=0;i<number.length;i++){
    if (ValidDigits.indexOf(number.charAt(i))==-1) return false
  }
  return true
}

function trimZeros(strNum){
	var lastNum= strNum.charAt(strNum.length-1);
	if (lastNum=='.'){
		return strNum.substr(0,strNum.length-1);
	} else if (lastNum!='0') {
		return strNum
	} else {
		var newNum= strNum.substr(0,strNum.length-1);
		return trimZeros(newNum);
	}	
}

function isFloat(number){
	var frmNum = number;
	if  (frmNum.charAt(0) == '.') frmNum = '0' + frmNum // start with . example .50
	var lastPoint = frmNum.lastIndexOf('.');
	if ( lastPoint>=0) { 
		var otherPoint = frmNum.lastIndexOf('.',lastPoint-1);
		if (otherPoint>=0) return false;	// another point, error!
		frmNum= trimZeros(frmNum);  // trim al trailing zeros
	}	
	//-------------------------------------------------------
	var strNum = '' + parseFloat(number);
	var i
	for (i=strNum.length;i<frmNum.length;i++) strNum += '0' // case the user type 0000
	return (frmNum == strNum)
}

function LimitAttach(form, file, extArray)
{
   if (!file) 
      return false;
   while (file.indexOf("\\") != -1)
       file = file.slice(file.indexOf("\\") + 1);
   var ext = file.slice(file.indexOf(".")).toLowerCase();
   for (var i = 0; i < extArray.length; i++) 
   {
       if (extArray[i] == ext) 
          return true; 
   }
   return false;
}

