// Проверка валидности E-mail
function isEMailAddr(elem) {
	var str = elem.value;
    str = str.toLowerCase( );
    if (str.indexOf("@") > 1) {
        var addr = str.substring(0, str.indexOf("@"));
        var domain = str.substring(str.indexOf("@") + 1, str.length);
        // at least one top level domain required
        if (domain.indexOf(".") == -1) {
            alert("Проверьте e-mail");
            return false;
        }
        // parse address portion first, character by character
        for (var i = 0; i < addr.length; i++) {
            oneChar = addr.charAt(i).charCodeAt(0);
            // dot or hyphen not allowed in first position; dot in last
            if ((i == 0 && (oneChar == 45 || oneChar == 46))  || 
                (i == addr.length-1 && oneChar == 46)) {
                alert("Проверьте email");
                return false;
            }
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert("Проверьте email");
                return false;
            }
        }
        for (i = 0; i < domain.length; i++) {
            oneChar = domain.charAt(i).charCodeAt(0);
            if ((i == 0 && (oneChar == 45 || oneChar == 46)) || ((i == domain.length - 1  || i == domain.length - 2) && oneChar == 46)) {
                alert("Проверьте e-mail");
                return false;
            }
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert("Проверьте e-mail");
                return false;
            }
        }
        return true;
    }
    alert("Проверьте Email!");
    return false;
}

function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var re = /\b\d{1,2}[.]\d{1,2}[.]\d{4}\b/;
    if (re.test(entry)) {
        var delimChar = ".";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        day = parseInt(entry.substring(0, delim1), 10);
        mo = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        var testDate = new Date(yr, mo-1, day);
        alert(testDate)
        if (testDate.getDate( ) == day) {
            if (testDate.getMonth( ) + 1 == mo) {
                if (testDate.getFullYear( ) == yr) {
                    return true;
                } else {
                    alert("Проверьте дату!");
                }
            } else {
                alert("Проверьте дату!");
            }
        } else {
            alert("Проверьте дату!");
        }
    } else {
        alert("Проверьте дату! Требуемый формат: дд.мм.гггг");
    }
    return false;
}

function checkform_change_password() {
	if (document.all.passwd1.value=='' || document.all.passwd2.value=='') {
		window.alert("Введите пароль!");
 		return false;
	}
	if (document.all.passwd1.value!=document.all.passwd2.value) {
		window.alert("Пароли не совпадают!");
 		return false;
	}
	return true;
}

function checkorder() {
	a = 0;
	var n2 = 'item'+a;
	while (document.getElementById(n2)) {
		if (parseFloat(document.getElementById(n2).value)!=document.getElementById(n2).value) {
			alert('Проверьте поля кол-во, значения могут быть только числами!');
			return false;
		};
		a = a+1;
		n2 = 'item'+a;
	}
	if (document.all.fio) if (document.all.fio.value=='') {alert('Введите ФИО!'); return false;}
	if (document.all.address) if (document.all.address.value=='') {alert('Введите адрес доставки!'); return false;}
	if (document.all.tel) if (document.all.tel.value=='') {alert('Введите телефон!'); return false;}
	if (document.all.password1) if (document.all.password1.value!=document.all.password2.value) {alert('Пароли не совпадают!'); return false;}
	if (document.all.email) if (document.all.email.value!='') if (!isEMailAddr(document.all.email)) return false;

	//if (confirm('Вы проверили заказ и он верный?')) return true; else return false;
	document.all.do_not_check_cart.value = 'true';
	return true;
}
