var comment_form_send = function(){

    log_id = $("comment_form_log_id").value;
    name = $("comment_form_name").value;
    email = $("comment_form_email").value;
    comment = $("comment_form_comment").value;

    var error = false;
    if(fieldRequired(name) != true){
        error = true;
        fieldError($("comment_form_name"));
    }
    else{
        fieldOk($("comment_form_name"));
    }
    if(fieldEmail(email) != true){
        error = true;
        fieldError($("comment_form_email"));
    }
    else{
        fieldOk($("comment_form_email"));
    }
    if(fieldRequired(comment) != true){
        error = true;
        fieldError($("comment_form_comment"));
    }
    else{
        fieldOk($("comment_form_comment"));
    }

    if(!error){
        $("comment_form_name").value="";
        $("comment_form_email").value="";
        $("comment_form_comment").value="";
        Effect.SlideUp('log_comment_add', { duration: 1.0 });
        new Ajax.Request(IV_url +"medialog/comment", {
            parameters: {
                "log_id" : log_id,
                "name" : name,
                "email" : email,
                "comment" : comment
            },
            onSuccess: function(transport) {
                commentInsertion(transport.responseText);
            }
        });
    }
};

function fieldError(p_obj){
    p_obj.style.borderColor = "#C74B3E";
    p_obj.style.backgroundColor = "#F6DADA";
    p_obj.style.backgroundImage = "url("+IV_url+"common/images/error.gif)";
    p_obj.style.backgroundRepeat = "no-repeat";
    p_obj.style.backgroundPosition = "right";
    fieldObserve(p_obj);
}
function fieldOk(p_obj){
    p_obj.style.borderColor = "#28AE24";
    p_obj.style.backgroundColor = "#D1F1D0";
    p_obj.style.backgroundImage = "url("+IV_url+"common/images/ok.gif)";
    p_obj.style.backgroundRepeat = "no-repeat";
    p_obj.style.backgroundPosition = "right";
    fieldObserve(p_obj);
}

function fieldObserve(p_obj){
    Event.observe(p_obj, 'change', function(){fieldReset(p_obj)});
    Event.observe(p_obj, 'focus', function(){fieldReset(p_obj)});
    Event.observe(p_obj, 'blur', function(){fieldReset(p_obj)});
}

var fieldReset = function(p_obj){
        p_obj.style.borderColor = "#999";
        p_obj.style.backgroundColor = "#FFF";
        p_obj.style.backgroundImage = "";
};

// Campo requerido
function fieldRequired(p_string){
	p_string = stringTrim(p_string);
	if(!stringEmpty(p_string)){
		return true;
	}
	return "isEmpty";
}

function fieldEmail(p_string){
	p_string = stringTrim(p_string);
	if(!stringEmpty(p_string)){
		if(stringMail(p_string)){
			return true;
		}
		else{
			return "noEmail";
		}
	}
	return "isEmpty";
}

/* ********* Funciones que validan cadenas ********* */
// Email valido
function stringMail(emailStr){
	if (emailStr.length == 0) {
		return true;
	}
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat) == null) {
		return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				return false;
			}
		}
		return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray == null) {
		return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if ((domArr[domArr.length-1].length < 2) || (domArr[domArr.length-1].length > 3)) {
		return false;
	}
	if (len < 2) {
		return false;
	}
	return true;
}
// Cadena vacia
function stringEmpty(p_string){
	for(t=0; t < stringTrim(p_string).length; t++){
		if(p_string.charAt(t) != " "){
			return false;
		}
	}
	return true;
}

/* ********* Funciones accesorias de validacion ********* */
// Corta espacios al inicio y final
function stringTrim(string){
	if(string.length > 0){
		string = string.replace(/^s+/, '');
		for (var t = string.length; t > 0; t--) {
			if (/S/.test(string.charAt(t))) {
				string = string.substring(0, t);
				break;
			}
		}
	}
	return string;
}
