﻿window.onload = function () {
    $("ContactCtrl_txMessage").focus();
}

function changeForm() {
    var domeniuInteres = document.getElementById("ContactCtrl_ddAddressee").value;
    
    if (domeniuInteres == "1" || domeniuInteres == "2") 
        document.location.href = "/rezervare.aspx?s=" + domeniuInteres;

    if (domeniuInteres == "7")
        document.location.href = "/ContactCursuri.aspx";
}

function sendContactForm() {
    var txName = document.getElementById("ContactCtrl_txName").value;
    var txEmail = document.getElementById("ContactCtrl_txEmail").value;
    var txPhone = document.getElementById("ContactCtrl_txPhone").value;
    var txMessage = document.getElementById("ContactCtrl_txMessage").value;

    var error = "";
    try {
        if (document.getElementById("ContactCtrl_ddAddressee").selectedIndex == 0) {
            error += getJSPH("Contact_SelectatiDomeniu");
        }
    }
    catch (Err) { }

    if (!isNotEmpty(txMessage))
        error += ' - completaţi mesajul;\n';

    if (!isNotEmpty(txName))
        error += ' - completaţi numele dvs.\n';

    if (!isNotEmpty(txEmail))
        error += ' - completaţi email-ul;\n';

    if (isNotEmpty(txEmail) && !isEmail(txEmail))
        error += ' - email-ul trebuie să conţină o adresă validă de email;\n';

    if (!isNotEmpty(txPhone))
        error += ' - completaţi telefonul;\n';

    if (isNotEmpty(txPhone) && !isPhoneValid(txPhone))
        error += ' - telefonul trebuie să fie un număr valid de telefon;\n';

    if (error.length > 0) {
        error = 'Vă rugăm să verificaţi următoarele probleme:\n' + error;
        alert(error);
    }
    else {
        document.getElementById("ContactCtrl_hAction").value = "SaveContact";
        document.getElementById("frmMain").submit();
    }
}

function isNotEmpty(val) {
    return ((trim(val)).length > 0);
}

function isEmail(val) {
    if (!isNotEmpty(val)) {
        return true;
    }
    else {
        var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var m = val.match(emailRegEx);
        return ((m != null) && (m.length > 0));
    }
}

function isValidPhoneChar(c) {
    if ((c == "0") || (c == "1") || (c == "2") || (c == "3") || (c == "4")) return true;
    if ((c == "5") || (c == "6") || (c == "7") || (c == "8") || (c == "9")) return true;
    if ((c == '/') || (c == '-') || (c == '+') || c == '.') return true;
    if ((c == '(') || (c == ')') || (c == ' ')) return true;
    return false;
}

function isPhoneValid(value) {
    var i = 0;
    var s = new String();
    var c = '';
    s = value.toString()
    for (i = 0; i < s.length; i++) {
        c = s.substring(i, i + 1);
        if (!isValidPhoneChar(c)) {
            return false;
        }
    }
    return true;
}

function trim(s) {
    while (s.substring(0, 1) == ' ')
        s = s.substring(1, s.length);
    
    while (s.substring(s.length - 1, s.length) == ' ')
        s = s.substring(0, s.length - 1);
    
    return s;
}

function goToHome()
{
    document.location.href = "Default.aspx";
}
