
function Validator(form) {
    this.form = form;
    this.valid = true;
    this.text = "";
}

Validator.prototype.form;
Validator.prototype.valid;
Validator.prototype.text;

Validator.prototype.getForm = function() {
    return this.form;
}

Validator.prototype.reset = function() {
	this.valid = true;
	this.text = "";
}

Validator.prototype.empty = function(q) {
	for (i = 0; i < q.length; i++) {
	   if (q.charAt(i) != " ") {
	   	return false
	   }
	}
	return true
}

Validator.prototype.required = function(control,msg) {
	if (this.empty(control.value) == true) {
		this.valid = false;
		this.text = this.text+"<li>"+msg+"</li>";
		return false;
	}
	else
		return true;
}

Validator.prototype.checked = function(control,msg) {
	if (control.checked == false) {
		this.valid = false;
		this.text = this.text+"<li>"+msg+"</li>";
		return false;
	}
	else
		return true;
}

Validator.prototype.date = function(control,msg) {
   var expr = /^(\d{4})[/-](0[1-9]|1[012])[/-]([012][1-9]|3[01])$/;
   if (expr.test(control.value) == false) {
      this.valid = false;
      this.text = this.text+"<li>"+msg+"</li>";
      return false;
   }
   else
      return true;
}

Validator.prototype.datetime = function(control,msg) {
   var expr = /^(\d{4})[/-](0[1-9]|1[012])[/-]([012][1-9]|3[01])\s*(\s(0\d|1\d|2[0-3]):([0-5]\d)(:([0-5]\d))?)?$/;
   if (expr.test(control.value) == false) {
      this.valid = false;
      this.text = this.text+"<li>"+msg+"</li>";
      return false;
   }
   else
      return true;
}

Validator.prototype.email = function(control,msg) {
	if ((control.value.indexOf("@") == -1) || (control.value.indexOf(".") == -1)) {
		this.valid = false;
		this.text = this.text+"<li>"+msg+"</li>";
		return false;
	}
	else
		return true;
}

Validator.prototype.equal = function(ctrl1,ctrl2,msg) {
	if (ctrl1.value != ctrl2.value) {
		this.valid = false;
		this.text = this.text+"<li>"+msg+"</li>";
		return false;
	}
	else
		return true;
}

Validator.prototype.validate = function() {
	var fs = document.getElementById("error");
	if (fs != null)
		fs.parentNode.removeChild(fs);
	if (this.valid == false) {
		fs = document.createElement('fieldset');
		fs.setAttribute('id','error');
		fs.innerHTML = "<legend>Errors</legend><ul>"+this.text+"</ul>";
		this.form.insertBefore(fs,this.form.elements[0]);
		location.href = "#error";
	}
	var valid = this.valid;
	this.reset();
	return valid;
}

function swap(id1, id2)
{
   document.getElementById(id1).style.display = "none";
   document.getElementById(id2).style.display = "block";
}
function checkAll(field)
{
	for (i = 0; i < field.length; i++)
		field[i].checked = true;
	return false;
}

function ShowHide(id, boton_mostrar, boton_ocultar) {
    if(document.getElementById(id).className != "oculta"){
		// mostrar
		document.getElementById(boton_ocultar).className = "oculta";
		document.getElementById(boton_mostrar).className = "muestra";
        document.getElementById(id).className = "oculta";
    } else {
		document.getElementById(boton_ocultar).className = "muestra";
		document.getElementById(boton_mostrar).className = "oculta";
        document.getElementById(id).className = "muestra";
    }
}




function confirmDel()
{
var agree=confirm("Are you Sure?");
if (agree)
return true ;
else
return false ;
}

function confirmDeltext(text)
{
var agree=confirm(text);
if (agree)
return true ;
else
return false ;
}


function blinkIt() {
 if (!document.all) return;
 else {
   for(i=0;i<document.all.tags('blink').length;i++){
      s=document.all.tags('blink')[i];
      s.style.color=(s.style.color=='#117033')?'#777777':'#117033';
   }
 }
}
function stopIt(obj)
{
      clearTimeout(blinkIt);
      obj.style.color = '#ffffff';
}


function only_numbers(e)
	{
	var key;
	var keychar;
	el = Event.element(e);
	
	if (window.event) {
		key = window.event.keyCode;
	} else if (e) {
		key = e.which;
	} else {
		return true;
	}
	
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==Event.KEY_BACKSPACE) || (key==Event.KEY_TAB) || (key==Event.KEY_RETURN) || (key==Event.KEY_ESC)) {
		return true;
	} else if ((keychar == '.' && !($F(el.id).indexOf('.') != -1)) || (("0123456789").indexOf(keychar) > -1)) {
		return true;
	} else {
		try { Event.stop(e); } catch(ce) { }
		try { e.returnValue = false; } catch(ce) { }
	
	return false;
	}
	//Esta es la formula para los input
	//onkeypress="return only_numbers(event)"
}

