function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function  validateNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //check for numeric characters
  return objRegExp.test(strValue);
}

function validateInteger( strValue ) {
  var objRegExp  = /(^-?\d\d*$)/;

  //check for integer characters
  return objRegExp.test(strValue);
}

function validateAlpha( strValue ) {
  var objRegExp  = /[\W_]/;

  //check for alphanumeric characters
  return objRegExp.test(strValue);
}

function validateString( strValue ) {
  var objRegExp  = /^[a-zA-z áéíóúÁÉÍÓÚñÑ']+$/;

  //check for string characters
  return objRegExp.test(strValue);
}

function validateRFC( strValue ) {
  var objRegExp  = /^[A-Z]{4}[0-9]{6}[a-zA-z0-9]{3}$/;

  //check for a valid RFC
  return objRegExp.test(strValue);
}

function validateRFC2( strValue ) {
  var objRegExp  = /^[A-Z]{3}[0-9]{6}[a-zA-z0-9]{3}$/;

  //check for a valid RFC
  return objRegExp.test(strValue);
}

function string2Upper( objString ) {
	objString.value = objString.value.toUpperCase();
	//return objString.value.toUpperCase();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function isValidCreditCard(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Master Card") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Discover") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "American Express") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

function objectConverter(a)
{
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}

function validaUpload(nombre_archivo, tipo_archivo, lenguaje){
	
	var message = '';
	
	var type1 = ["jpg", "jpeg", "gif", "png"];
	var type2 = ["jpg", "jpeg", "gif", "png", "doc", "xls", "ppt", "pdf", "docx", "xlsx", "pptx"];
	var type3 = ["jpg", "jpeg"];
	var type4 = ["jpg", "jpeg", "gif", "swf"];
	var type5 = ["ico"];
	var type6 = ["csv"];
	
	if(tipo_archivo==1){ //Tipo: Imagen
		file_type = type1;
	}
	else if(tipo_archivo==2){ //Tipo: Archivo Valido
		file_type = type2;
	}
	else if(tipo_archivo==3){ //Tipo: solo jpg
		file_type = type3;
	}
	else if(tipo_archivo==4){ //Tipo: jpg, gif y swf
		file_type = type4;
	}
	else if(tipo_archivo==5){ //Tipo: ico
		file_type = type5;
	}
	else if(tipo_archivo==6){ //Tipo: csv
		file_type = type6;
	}
	else{ //Tipo: Default
		file_type = type2;
	}
	//alert(nombre_archivo);
	if(nombre_archivo.indexOf(".")>0 && nombre_archivo.substring(nombre_archivo.indexOf(".")+1).length>0){
		
		if(nombre_archivo.substring(nombre_archivo.indexOf(".")+1).indexOf(".")<0){
			if(nombre_archivo.substring(nombre_archivo.indexOf(".")+1).toLowerCase() in objectConverter(file_type)){
				var objRegExp  = /^[a-zA-Z0-9_]+[a-zA-Z0-9_,\s]+[a-zA-Z0-9_]+$/;			
				file = nombre_archivo.substring(0,nombre_archivo.indexOf("."));
				
				if(file.indexOf(".")<0 && file.search(objRegExp)!=-1){
					message = '';
				}
				else{ //Nombre incorrecto
					if (lenguaje == "esp")
						message = "El archivo '" + nombre_archivo + "' contiene caracteres no válidos.\n   El nombre del archivo no debe contener acentos, ñ ni otros caracteres especiales";
					else if (lenguaje == "eng")
						message = "The file '" + nombre_archivo + "' contains invalid characters.\n   The file name must not have accents, ñ, $, @, &, or any other special characters";
					else
						message = "The file '" + nombre_archivo + "' contains invalid characters.\n   The file name must not have accents, ñ, $, @, &, or any other special characters";
				}
			}
			else{ //Extension no permitida
				extension = nombre_archivo.substring(nombre_archivo.indexOf(".")+1);
					if (lenguaje == "esp")
						message = "El archivo '" + nombre_archivo + "' tiene una extensión no válida: '" + extension.toLowerCase() + "'";
					else if (lenguaje == "eng")
						message = "The file '" + nombre_archivo + "' has an invalid extension: '" + extension.toLowerCase() + "'";
					else
						message = "The file '" + nombre_archivo + "' has an invalid extension: '" + extension.toLowerCase() + "'";
				
				if(tipo_archivo!=2){
					valid_extensions = file_type.join("', '");
					if (lenguaje == "esp")
						message += "\n  El archivo sólo puede tener alguna de las siguientes extensiones: '" + valid_extensions + "'";
					else if (lenguaje == "eng")
						message += "\n  The file can only have one of the following extensions: '" + valid_extensions + "'";
					else
						message += "\n  The file can only have one of the following extensions: '" + valid_extensions + "'";
				}
				
			}
		}
		else{ //Tiene mas de un punto
					if (lenguaje == "esp")
						message = "El archivo '" + nombre_archivo + "' contiene más de un punto.\n  El archivo sólo puede contener un punto.";
					else if (lenguaje == "eng")
						message = "The file '" + nombre_archivo + "' contains more than one period.\n  The file name can only contain one period.";
					else
						message = "The file '" + nombre_archivo + "' contains more than one period.\n  The file name can only contain one period.";
		}
	}
	else{ //No tiene extension
		if (lenguaje == "esp")
			message = "El archivo '" + nombre_archivo + "' no tiene extensión.";
		else if (lenguaje == "eng")
			message = "The file '" + nombre_archivo + "' has no extension.";
		else
			message = "The file '" + nombre_archivo + "' has no extension.";

		if(tipo_archivo!=2){
			valid_extensions = file_type.join("', '");
			if (lenguaje == "esp")
				message += "\n  El archivo debe tener alguna de las siguientes extensiones: '" + valid_extensions + "'";
			else if (lenguaje == "eng")
				message += "\n  The file can only have one of the following extensions: '" + valid_extensions + "'";
			else
				message += "\n  The file can only have one of the following extensions: '" + valid_extensions + "'";
		}
	}
	
	return message;
}
