﻿months = new Array( "ian", "feb", "mar", "apr", "mai", "iun", "iul", "aug", "sep", "oct", "nov", "dec" );

function abonare() {
    $("spSignUpTitle").style.display = "inline";
    $("registrationForm").className = "dB";
    $("txFormEmail").focus();
}

function focusOnEnter(e) {
	if (window.event && window.event.keyCode == 13) {
		$("txLoginPassword").focus();
	}
	else if (e.keyCode == 13) {
		$("txLoginPassword").focus();
	}
}

function submitOnEnter(e) {
	if (window.event && window.event.keyCode == 13) {
		autentificare();
	}
	else if (e.keyCode == 13) {
		autentificare();
	}
}

function autentificare()
{
    if (checkForm())
    {
        $("hAction").value = "Autentificare";
        $("frmMain").submit();
    }
}
function checkForm()
{
    var msg = "";
    if (trim($("txLoginEmail").value) == "")
    {
        msg += "- trebuie să-ţi introduci email-ul;\n";
    }
    else
    {
        if (!isEmail($("txLoginEmail").value))
        {
            msg += "- email-ul introdus nu este valid;\n";    
        }
    }
    if (trim($("txLoginPassword").value) == "")
    {
        msg += "- trebuie să-ţi introduci parola;\n";
    }
    
    if (trim(msg) != "")
    {
        msg = "Te rugăm să corectezi următoarele probleme:\n" + msg;
        alert(msg);
        return false;
    }
    else
    {
        return true;
    }
}

function populateDate() {
    var dd;
    var option;
    
    var today = new Date();
    var currentYear = today.getFullYear();
    
    dd = $("ddDateYear");
    dd.options.length = 0;
    
    option = document.createElement("option");
    option.value = "";
    option.text = "An";
    appendChild(dd, option);
    
    for (var year = currentYear - 80; year <= currentYear - 6; year++) {
        option = document.createElement("option");
        option.text = year;
        option.value = year;
        appendChild(dd, option);
    }
    
    dd = $("ddDateMonth");
    dd.options.length = 0;
    
    option = document.createElement("option");
    option.value = "";
    option.text = "Lună";
    appendChild(dd, option);
    
    for (var month = 0; month < months.length; month++) {
        option = document.createElement("option");
        option.text = months[month];
        option.value = month;
        appendChild(dd, option);
    }
    populateDays();
}

function appendChild(dd, option) {
    try {
        dd.add(option, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
        dd.add(option); // IE only
    }
}

function populateDays() {
    var today = new Date();
    var day = parseInt($("ddDateDay").value);
    
    var month;
    if ($("ddDateMonth").value.length > 0) {
        month = parseInt($("ddDateMonth").value);
    } else {
        month = 0;
    }

    var year = parseInt($("ddDateYear").value);
    
    dd = $("ddDateDay");
    dd.options.length = 0;
    
    option = document.createElement("option");
    option.value = "";
    option.text = "Zi";
    appendChild(dd, option);
    
    var days = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if (isLeapYear(year)) {
        days[1] = 29;
    }
    
    var option;
    for (var i = 1; i <= days[month]; i++) {
        option = document.createElement("option");
        option.text = i;
        option.value = i;
        appendChild(dd, option);
    }
    if (days[month] >= day) {
        dd.value = day;
    } else if (isNaN(day)) {
        dd.value = "";
    } else {
        dd.value = 1;
    }
}

function inregistrare() {
    var errors = "";
    
    if (trim($("txFormEmail").value).length == 0 || !isEmail($("txFormEmail").value)) {
        errors += " - trebuie să completezi email-ul tău cu o adresă validă de email;\n";
    } else {
        if (IsRegisteredEmail(trim($("txFormEmail").value))) {
            errors += " - email-ul specificat este deja folosit de un alt membru;\n";
        }
    }
    
    if (trim($("txFormPassword").value).length < 6) {
        errors += " - parola trebuie să fie de minim 6 caractere;\n";
    }
    
    if (trim($("txFormPassword").value) != trim($("txFormPasswordAgain").value)) {
        errors += " - confirmarea parolei este invalidă;\n";
    }
    
    if (!$("rbSexMale").checked && !$("rbSexFemale").checked) {
        errors += " - te rugăm să selectezi o opţiune la \"Sex\";\n"; 
    }
    
    if (trim($("txFirstName").value).length == 0) {
        errors += " - trebuie să completezi prenumele;\n";
    }
    
    if (trim($("txFormName").value).length == 0) {
        errors += " - trebuie să completezi numele;\n";
    }
    
    if ($("ddDateDay").selectedIndex == 0) {
        errors += " - trebuie să alegi ziua naşterii;\n";
    }
    
    if ($("ddDateMonth").selectedIndex == 0) {
        errors += " - trebuie să alegi luna naşterii;\n";
    }

    if ($("ddDateYear").selectedIndex == 0) {
        errors += " - trebuie să alegi anul naşterii;\n";
    }

    if (trim($("txFormCity").value).length == 0) {
        errors += " - trebuie să completezi localitatea;\n";
    }
    
    if ($("ddFormCounty").selectedIndex == 0) {
        errors += " - trebuie să alegi judeţul;\n";
    }
    
    if (trim($("txFormTelephone").value).length > 0 && !isPhoneValid(trim($("txFormTelephone").value)) ) {
        errors += " - telefonul trebuie să aibă 10 caractere, doar cifre;\n";
    }
    
    if (!$("rbAcceptYes").checked) {
        errors += " - pentru a te putea înregistra, trebuie să accepţi Politica de confidenţialitate şi Termenii şi condiţiile de utilizare a site-ului;\n";
    }
    
    if (errors.length > 0) {
        alert("Te rugăm să corectezi următoarele probleme:\n" + errors);
    } else {
        $("hAction").value = "Register";
        $("frmMain").submit();
    }
}

function IsRegisteredEmail(email) {
    var res = InfoPromotii.Web.Site.Autentificare.IsRegisteredEmail(email).value;
    if (res == "1") {
        return true;
    }
    
    return false;
}
