/**
 * Functions for checking entered values of a form
 * 
 * @package contact
 * @author Igor Volynets
 */

/**
 * Visible style variables
 */

NORMAL_STYLE = "input";
HIGHLIGHT_STYLE = "hl_input";

/**
 * Trims left spaces of a string
 */

function ltrim(text) {
    var ptrn = /\s*((\S+\s*)*)/;
    return text.replace(ptrn, "$1");
}

/**
 * Trims right spaces of a string
 */

function rtrim(text) {
    var ptrn = /((\s*\S+)*)\s*/;
    return text.replace(ptrn, "$1");
}

/**
 * Trims spaces from both sides of a string
 */

function trim(text) {
    return ltrim(rtrim(text));
}

/**
 * Checks entered form values
 */

function check_values(form) {
    
    var valid = true;
    
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type != "submit") {
	        if (trim(form.elements[i].value) == "") {
	            form.elements[i].className = HIGHLIGHT_STYLE;
	            valid = false;
	        }
	        else {
	            form.elements[i].className = NORMAL_STYLE;              
	        }
	    }
    }
    
    if (!valid) {
        alert("Please fill out the entire form");
        return false;
    }
    
    var email = document.getElementById("email");
    
    if (!email.value.match(/^[a-zA-Z0-9\._\-]+@([a-zA-Z0-9\._\-]+(\.[a-zA-Z0-9]+)+)*$/)) {
        email.className = HIGHLIGHT_STYLE;
        alert("Entered e-mail address has invalid format");
        return false;
    }
    else {
        email.className = NORMAL_STYLE;
    }
    
}