// JavaScript Document
var camps = ["nom","mail","text"];
var valida = ["t:3","mail","t:18"];

function btag() {
  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 = "#6F6F6F"	
			}
		}
	}
	
}

addLoadEvent(addOnFocus)

validaForm = function() {
	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 = "#CB0000"
					}
				break
				case "mail":
					if(isValidEmail(btag(camps[i]).value)==false) {
						errors = true;
						btag(camps[i]).style.borderColor = "#CB0000"
					}
				break
			}
		}
	}
	if(errors!=true) send_cmnt();
}

function isValidEmail(email, required) {
    if (required==undefined) {   
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
