// JavaScript Document
var errorColor = "#eb200c"
var color = "#f9f7f7"
btag = function() {
  var elements = new Array();

  for (var i=0;i<arguments.length;i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}
addLoadEvent = function(f) { 
	var old = window.onload; 
	if (typeof old != 'function') window.onload = f 
	else { window.onload = function() { old(); f() }} 
}
addOnFocus = function() {
	for(var i=0; i<camps.length; i++) {
		if(camps[i]!= "") {
			btag(camps[i]).onfocus = function() {
				this.style.borderColor = color	
			}
		}
	}
	
}

addLoadEvent(addOnFocus)

validaForm = function(nomform) {
	errors = undefined
	for(var i=0; i<valida.length; i++) {
		if(valida[i]!="") {
			var strValida = new String(valida[i])
			var objValida = strValida.split(":")
			switch (objValida[0]) {
				case "t": 
					if(btag(camps[i]).value.length<=parseFloat(objValida[1])) {
						errors = true;
						btag(camps[i]).style.borderColor = errorColor
					}
				break
				case "m": 
					if(btag(camps[i]).value.length>parseFloat(objValida[1])) {
						errors = true;
						btag(camps[i]).style.borderColor = errorColor
					}
				break
				case "email":
					if(!validateEmailv2(btag(camps[i]).value)) {
						errors = true;
						btag(camps[i]).style.borderColor = errorColor
					}
				break;
				
				case "num":
					var charpos = btag(camps[i]).value.search("[^0-9]"); 
					if(!btag(camps[i]).value.length > 0 ||  charpos >= 0) { 
						
						
						errors = true;
						btag(camps[i]).style.borderColor = errorColor
							
					}
              break;   
			  case "file":
			  	var nom = btag(camps[i]).value
			  	nom = nom.split(".")
				if(nom[nom.length-1]!=objValida[1]) {
					
					errors = true;
					btag(camps[i]).style.borderColor = errorColor
					
				} else {
					btag(camps[i]).style.borderColor = color
				}
			  break;
			  case "checked":
			  	if(btag(camps[i]).checked!=1) {
					
					errors = true;
					btag(camps[i]+'_txt').style.color = errorColor
					
				} else {
					btag(camps[i]+'_txt').style.color = "#5a5959"
				}
			  break;
			}
		}
	}
	if(errors!=true) btag(nomform).submit()
}





function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return false;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}
