function validateEmail( strValue) {
    var objRegExp  = /^\w(\.?\w)*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
    return objRegExp.test(strValue);
}

function  validateNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  return objRegExp.test(strValue);
}

function validateInteger( strValue ) {
  var objRegExp  = /(^-?\d\d*$)/;
  return objRegExp.test(strValue);
}

function validateNotEmpty( strValue ) {
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   return ( strTemp.length > 0 );
}

function rightTrim( strValue ) {
   var objRegExp = /^([\w\W]*)(\b\s*)$/;
   if(objRegExp.test(strValue))
      strValue = strValue.replace(objRegExp, '$1');
   return strValue;
}

function leftTrim( strValue ) {
var objRegExp = /^(\s*)(\b[\w\W]*)$/;
   if(objRegExp.test(strValue))
      strValue = strValue.replace(objRegExp, '$2');
   return strValue;
}

function trimAll( strValue ) {
   var objRegExp = /^(\s*)$/;
   if(objRegExp.test(strValue)) {
      strValue = strValue.replace(objRegExp, '');
      if( strValue.length == 0) return strValue;
   }

   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) 
      strValue = strValue.replace(objRegExp, '$2');
   return strValue;
}

function validateDate( strValue ) {
	var arr_date = strValue.split('-');

	if (arr_date.length != 3) return show_error2("Formato de fecha incorrecto: '" + strValue + "'.\nFormato aceptado es dd-mm-yyyy.");
	if (!arr_date[0]) return show_error2("Formato incorrecto: '" + strValue + "'.\nDia ingresado incorrecto.");
	if (!RE_NUM.exec(arr_date[0])) return show_error2("D\u00EDa del mes incorrecto: '" + arr_date[0] + "'.\nIngrese valores enteros positivos.");
	if (!arr_date[1]) return show_error2("Formato de fecha incorrecto: '" + strValue + "'.\nMes ingresado incorrecto.");
	if (!RE_NUM.exec(arr_date[1])) return show_error2("Mes ingresado incorrecto: '" + arr_date[1] + "'.\nIngrese valores enteros positivos.");
	if (!arr_date[2]) return show_error2("Formato de fecha incorrecto: '" + strValue + "'.\nA\u00F1o no encontrado.");
	if (!RE_NUM.exec(arr_date[2])) return show_error2("A\u00F1o incorrecto: '" + arr_date[2] + "'.\nIngrese valores enteros positivos.");

	var dt_date = new Date();
	dt_date.setDate(1);

	if (arr_date[1] < 1 || arr_date[1] > 12) return show_error2("Mes ingresado incorrecto: '" + arr_date[1] + "'.\nRango permitido 01-12.");
	dt_date.setMonth(arr_date[1]-1);
	 
	if (arr_date[2] < 100) arr_date[2] = Number(arr_date[2]) + (arr_date[2] < NUM_CENTYEAR ? 2000 : 1900);
	dt_date.setFullYear(arr_date[2]);

	var dt_numdays = new Date(arr_date[2], arr_date[1], 0);
	dt_date.setDate(arr_date[0]);
	if (dt_date.getMonth() != (arr_date[1]-1)) return show_error2("D\u00EDa del mes incorrecto: '" + arr_date[0] + "'.\nRango permitido 01-"+dt_numdays.getDate()+".");
	return true;
}

function validateValue( strValue, strMatchPattern ) {
    var objRegExp = new RegExp( strMatchPattern);
    return objRegExp.test(strValue);
}

function validateRut(strValue){
    var objRegExp = /^\d{7,9}\-(\d|k|K)$/
    if( !objRegExp.test(strValue) ) return false;
    var tmpRut = strValue.split('-');
    return ( tmpRut[1] == getDigit(tmpRut[0]) );
}

function show_error(strField, strMsg){
    alert(strMsg);
    if( strField ) strField.focus();
    return false;
}

function show_error2(strMsg){
    alert(strMsg);
    return false;
}

function getDigit(strValue) {
	var tmprut = strValue;
	var sum = 1;
	for(var mul = 0; tmprut != 0; tmprut = Math.floor( tmprut / 10 ))
		sum = (sum + tmprut % 10 * ( 9 - mul++ % 6 ) ) % 11;
	return (sum ? sum - 1 : 'K');
}

function popwin(theUrl,winName,features,width,height) {
  var str = features + ",height=" + height + ",innerHeight=" + height;
  str += ",width=" + width + ",innerWidth=" + width;
  if (window.screen) {
    var sh = screen.availHeight - 30;
    var sw = screen.availWidth - 10;

    var txc = (sw - width) / 2;
    var tyc = (sh - height) / 2;

    str += ",left=" + txc + ",screenX=" + txc;
    str += ",top=" + tyc + ",screenY=" + tyc;
  }
  return window.open(theUrl,winName,str);
}

