var valid = true;
var MSG_NOT_CHOOSEN = "Not Chosen";

function trim_spaces() {
    var temp_string = this;
    while(temp_string.substring(0, 1) == " ")
        temp_string = temp_string.substring(1);
    while(temp_string.substring(temp_string.length - 1) == " ")
        temp_string = temp_string.substring(0, temp_string.length - 2);
    return temp_string;
}
String.prototype.trim=trim_spaces;

function isEmail2(who) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*$/i;
    return(email.test(who));
}

function disValid(el, title) {

    if (el.type=="hidden") {
        var icon = document.getElementById("IMG"+el.id);
        icon.style.backgroundColor="#FFA090";
        icon.style.padding="2px 2px 2px 2px";
    }
    el.style.backgroundColor="#FFA090";
    el.title += title;
    valid = false;
}

function colorValid(val) {
    var re = new RegExp("#[abcdef\\d]{6}", "i");
    return val.match(re) == val;      
}

function validate(Sender) {
    valid = true;
	for (var i = 0; i < Sender.elements.length; i++) {
        var el = Sender.elements[i];
        if(el.disabled)
            continue;
        var type = el.type;
        var alt = el.getAttribute("alt");
        var value = el.value;
        var pwd;
        var re1 = new RegExp("[^\\d\\-]");
        var re2 = new RegExp("-?\\d*");

        if (type != "checkbox" && type != "radio" && type != "submit" && type != "image" ) {
            el.style.backgroundColor="";
            el.title = "";
			if (alt != null && value != null && alt.indexOf("mustChoose") > -1 && value.length > 0) {
				var noEmpty = null;
				if (alt.indexOf("ifNoEmpty") > -1) {
					var noEmptyName = alt.substring(alt.indexOf("ifNoEmpty(") + 10, alt.indexOf(")", alt.indexOf("ifNoEmpty(") + 10));
					noEmpty = Sender.elements[noEmptyName];
				}
				if (value == "0" && (!noEmpty || noEmpty.value.length > 0))
					disValid(el, alt.substring(alt.indexOf("mustChoose(") + 11, alt.indexOf(")", alt.indexOf("mustChoose(") + 11)))
			}
			if (alt != null && value != null && alt.indexOf("a_integer") > -1 && value.length > 0) {
				if (value.search(re1) > -1 || value.match(re2) != value)
					disValid(el, "Должно быть целое число. ");
			}
            if (alt != null && value != null && alt.indexOf("day") > -1 && value.length > 0) {
                if (value.search(re1) > -1 || value.match(re2) != value || value < 1 || value > 31)
                    disValid(el, "Должно быть целое число из диапазона 1-31. ");
            }
            if (alt != null && value != null && alt.indexOf("month") > -1 && value.length > 0) {
                if (value.search(re1) > -1 || value.match(re2) != value || value < 1 || value > 12)
                    disValid(el, "Должно быть целое число из диапазона 1-12. ");
            }
            if (alt != null && value != null && alt.indexOf("year") > -1 && value.length > 0) {
                if (value.search(re1) > -1 || value.match(re2) != value || value < 1900 || value > 2010)
                    disValid(el, "Должно быть целое число из диапазона 1900-2010. ");
            }
			if (alt != null && value != null && alt.indexOf("email") > -1 && value.length > 0) {
				if (!isEmail2(value))
					disValid(el, "E-mail не корректен. ");
			}
            var minLength = null;
            var maxLength = null;
            if (alt != null) {
                var re = /[^,\d]+\d+/g;
                var rd = /\d+/;
                var bound;
                do {
                    bound = re.exec(alt);
                    if (bound!=null) {
                        var pre = bound[0];
                        if (pre.indexOf('>')>-1 || pre.indexOf('&gt;')>-1){
                            var dig = rd.exec(pre);
                            minLength = dig[0];
                        }
                        if (pre.indexOf('<')>-1 || pre.indexOf('&lt;')>-1){
                            var dig = rd.exec(pre);
                            maxLength= dig[0];
                        }

                    }
                } while (bound!=null);

            }
            if (maxLength == null) maxLength = el.getAttribute("maxlength");
            if ((maxLength != null && maxLength < value.trim().length) || (minLength != null && minLength >= value.trim().length))
                disValid(el, "Поле имеет длину " + value.length + " символов, максимальная длина " + maxLength + ((minLength!=null) ? (" и минимальная длина " + ++minLength + ". " ): ""));
            if (alt != null && alt.indexOf("pwd1") > -1) {
                pwd = value;
            }
            if (alt != null && alt.indexOf("pwd2") > -1) {
                if (pwd != value)
                    disValid(el, "Введенные вами пароли не совпадают");
            }

        }
	}
    if (!valid) {
        alert("Пожалуйста исправьте выделенные цветом поля.");
        return false;
    } else {
        return true;
    }
}
