﻿function validateRequired(theElement) {
    if (theElement.value == "" || theElement.value == "-1") {
        return false;
    }
    return true;
}

function validateCompare(theElement, theElement2) {
    if (theElement.value != theElement2.value) {
        return false;
    }
    return true;
}

function validateEmail(theElement) {
    var emailPat = /^(([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}){0,1}$/;

    var match = (theElement.value).match(emailPat);
    if (match == null) {
        return false;
    }
    return true;
}

function validatePassword(theElement) {
    var passwordPattern = /^[0-9A-Za-z]{4,15}$/;

    var match = (theElement.value).match(passwordPattern);
    if (match == null) {
        return false;
    }
    return true;
}

function validateName(theElement) {
    var namePattern = /^[0-9A-Za-z ]{1,50}$/;

    var match = (theElement.value).match(namePattern);
    if (match == null) {
        return false;
    }
    return true;
}

function validateChecked(theElement) {
    if (!theElement.checked) {
        return false;
    }
    return true;
}

function validateLength(theElement, theLowerLimit, theUpperLimit) {
    var theLength = theElement.value.length;
    if (theLength >= theLowerLimit && theLength <= theUpperLimit) {
        return true;
    }
    return false;
}

function validateNumber(theElement) {
    if (isNaN(theElement.value)) {
        return false;
    }
    return true;
}

function validateInt(theElement) {
    theValue = parseInt(theElement.valueOf);

    if ((theValue % 1) == 0) {
        return true;
    }
    return false;
}

function submitForm(formId) {
    var theForm = document.getElementById(formId);

    var valid = true;

    switch (formId) {
        case "RegistrationForm":
            valid = validateRegistration();
            break;
        case "LoginForm":
            valid = validateLogin();
            break;
    }
    if (!valid) {
        return false;
    }

    theForm.submit();
    return false;
}

function validateRegistration() {
    var theEmail = document.getElementById("Email");
    var thePassword = document.getElementById("Password");
    var thePassword2 = document.getElementById("Password2");
    var theFirstName = document.getElementById("FirstName");
    var theLastName = document.getElementById("LastName");
    var theCountry = document.getElementById("Country");
    
    var theLocalCities = document.getElementById("LocalCities");
    var theInternationalCities = document.getElementById("InternationalCities");
    
    var theMobile = document.getElementById("Mobile");
    var theCaptcha = document.getElementById("Captcha");
    var theTerms = document.getElementById("Terms");

    if (!validateRequired(theEmail)) {
        alert("Email is required");
        theEmail.focus();
        return false;
    }

    if (!validateEmail(theEmail)) {
        alert("Email address is invalid");
        theEmail.focus();
        return false;
    }

    if (!validatePassword(thePassword)) {
        alert("Password must be between 4 and 15 characters long \nand must only be composed of letters and digits")
        thePassword.focus();
        return false;
    }

    if (!validateRequired(thePassword2)) {
        alert("Repeat the password");
        thePassword2.focus();
        return false;
    }

    if (!validateCompare(thePassword, thePassword2)) {
        alert("Passwords do not match");
        thePassword2.focus();
        return false;
    }

    if (!validateName(theFirstName)) {
        alert("First name is required, cannot exceed 50 characters \nand must only be composed of letters");
        theFirstName.focus();
        return false;
    }

    if (!validateName(theLastName)) {
        alert("Last name is required, cannot exceed 50 characters \nand must only be composed of letters");
        theLastName.focus();
        return false;
    }

    if (theMobile.value != "") {
        if (!validateNumber(theMobile) || !validateLength(theMobile, 10, 50)) {
            alert("Mobile number must be between 10 and 50 digits long");
            theMobile.focus();
            return false;
        }
    }

    if (!validateRequired(theCountry)) {
        alert("Select your current country");
        theCountry.focus();
        return false;
    }

    if (theCountry.value == "1" && !validateRequired(theLocalCities)) {
        alert("Select your region/city");
        theLocalCities.focus();
        return false;
    }
    
    if (theCountry.value == "2" && !validateRequired(theInternationalCities)) {
        alert("Select your region/city");
        theInternationalCities.focus();
        return false;
    }

    if (!validateRequired(theCaptcha)) {
        alert("Type in the code you see in the image");
        theCaptcha.focus();
        return false;
    }

    if (!validateChecked(theTerms)) {
        alert("You must agree to the terms & conditions");
        return false;
    }

    return true;
}

function validateLogin() {
    var theUsername = document.getElementById("Username");
    var thePassword = document.getElementById("Password");

    if (!validateRequired(theUsername)) {
        alert("Username is required");
        theUsername.focus();
        return false;
    }

    if (!validateRequired(thePassword)) {
        alert("Password is required");
        thePassword.focus();
        return false;
    }

    return true;
}

var new_window;
function LegalPop(url) {
    new_window = window.open(url, 'Legal', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=500,height=500');
    new_window.focus();
}

function clearOptions(optionList) {
    if (optionList) {
        while (optionList.firstChild) {
            optionList.removeChild(optionList.firstChild);
        }
        addToOptionList(optionList, "", "Select")
    }
}

function addToOptionList(optionList, optionValue, optionText) {
    optionList[optionList.length] = new Option(optionText, optionValue);
}

function countryChanged(selectedCountry) {
    var spanLocal = document.getElementById("Local");
    var spanInternational = document.getElementById("International");

    var ddlLocal = document.getElementById("LocalCities");
    var ddlInternational = document.getElementById("InternationalCities");

    if (selectedCountry == "1") {
        ddlLocal.disabled = false;
        ddlInternational.disabled = true;
        
        spanLocal.style.display = "";
        spanInternational.style.display = "none";
    } else {
        ddlInternational.disabled = false;
        ddlLocal.disabled = true;
        
        spanInternational.style.display = "";
        spanLocal.style.display = "none";
    }
    return false;
}

function clickClear(field, defaultText) {
    if (field.value == defaultText) {
        field.value = "";
    }
}
function clickReset(field, defaultText) {
    if (field.value == "") {
        field.value = defaultText;
    }
}
