﻿function PageLoad(countryID) {
    var drpCountry = document.getElementById('drpCountry');
    var defaultCountryID = countryID;
    for (var i = 0; i < drpCountry.options.length; i++) {
        if (drpCountry.options[i].value == defaultCountryID) {
            drpCountry.options[i].selected = true;
            clearDropDown();
            fillStates(drpCountry);
            break;
        }

    }
}
function fillStates(comp) {
    var txtStateCompID = "";
    var drpStateCompID = "";
    var drpCountryCompID = "";

    drpCountryCompID = comp.id;
    drpStateCompID = "drpState";
    txtStateCompID = "txtState";

    var isStates = false;
    clearDropDown(document.getElementById(drpStateCompID));
    var countries = document.getElementById("hdnCountryStates").value.split('###');
    for (var i = 0; i < countries.length; i++) {
        var countryDetails = countries[i].split('@#');
        if (document.getElementById(drpCountryCompID).value == countryDetails[0]) {
            isStates = true;
            document.getElementById(txtStateCompID).value = "";
            document.getElementById(drpStateCompID).style.display = '';
            document.getElementById(txtStateCompID).style.display = 'none';
            document.getElementById(drpStateCompID).length = 0;
            var opt = document.createElement('option');
            opt.value = "0";
            opt.text = "---Please Select---";
            document.getElementById(drpStateCompID).options.add(opt);
            var states = countryDetails[1].split('|');
            for (var j = 0; j < states.length; j++) {
                if (Trim(states[j]) != "") {
                    var stateDetails = states[j].split('_');
                    var stateOpt = document.createElement('option');
                    stateOpt.value = stateDetails[0];
                    stateOpt.text = stateDetails[1];
                    if (document.getElementById('hdnSelectedState').value == stateDetails[0]+"|"+stateDetails[1]) {
                        stateOpt.selected = true;
                    }
                    document.getElementById(drpStateCompID).options.add(stateOpt);
                }
            }
            break;
        }

    }
    if (!isStates) {
        //   clearDropDown(document.getElementById(drpStateCompID));
        document.getElementById(drpStateCompID).style.display = 'none';
        document.getElementById(txtStateCompID).style.display = '';
        document.getElementById('hdnSelectedState').value = "";
    }



}
function clearDropDown(dropDown) {
    if (dropDown) {
        var count = dropDown.length;
        if (count > 1) {
            for (var i = count; i >= 0; i--) {
                dropDown.remove(i);
            }
        }
    }
}
function ValidatePage() {
    var pageValid = true;
    pageValid = ValidateControl(document.getElementById('txtEmailAddress'), document.getElementById('lblErrorEmailAddress'), "Please enter email address", "text") && pageValid;
    pageValid = ValidateControl(document.getElementById('txtPassword'), document.getElementById('lblPasswordError'), "Please enter password", "text") && pageValid;
    pageValid = ValidateControl(document.getElementById('txtConfirmPassword'), document.getElementById('lblConfirmPasswordError'), "Please enter password", "text") && pageValid;
    pageValid = ValidateControl(document.getElementById('txtNickName'), document.getElementById('lblErrorNickName'), "Please enter a nick name", "text") && pageValid;
   // pageValid = ValidateControl(document.getElementById('drpPrimaryRole'), document.getElementById('lblErrorRole'), "Please select a role", "dropdown") && pageValid;
    pageValid = ValidateControl(document.getElementById('txtWordVerification'), document.getElementById('spnErrorWordVerification'), "Please enter the code displayed below", "text") && pageValid;
    updateRTE('RTEAboutCompany', 4000);
    if (pageValid) {
        pageValid = ValidPassword(document.getElementById('txtPassword').value.length);
        
    }
    if (pageValid) {
        pageValid = VerifyPasswords();
    }
    if (pageValid) {
        var emailValid = emailCheck(document.getElementById('txtEmailAddress').value)
        if (!emailValid) {
            ShowErrorMsg(document.getElementById('lblErrorEmailAddress'), "Please enter a valid email address");
        }
        else {
            HideErrorMsg(document.getElementById('lblErrorEmailAddress'));
        }
        pageValid = emailValid && pageValid;
    }
    var licenceAgreement = ValidateLicenceAgreement();
    return pageValid && licenceAgreement;


}
function ValidateLicenceAgreement() {

    if (document.getElementById('chkAgreement').checked) {
        document.getElementById('spnErrorLicenceAgreement').style.display = "none";
        document.getElementById('spnErrorLicenceAgreement').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('spnErrorLicenceAgreement').style.display = "";
        document.getElementById('spnErrorLicenceAgreement').innerHTML = "Please agree to the Terms of Use";
    }
    return false;
}
function VerifyPasswords() {
    var pwd = document.getElementById('txtPassword').value;
    var reenterPwd = document.getElementById('txtConfirmPassword').value;
    if (pwd == reenterPwd) {
        return true;
    }
    ShowErrorMsg(document.getElementById('lblConfirmPasswordError'), "The two Passwords do not seem to match. Please re-enter the Password.");
    return false;
    
}
function ValidPassword(pwdLength) {
    if (pwdLength < 6) {
        ShowErrorMsg(document.getElementById('lblPasswordError'), "Password should have atleast 6 characters.");
        return false;
    }
    if (pwdLength > 20) {
        ShowErrorMsg(document.getElementById('lblPasswordError'), "Password should not be more than 20 characters.");
        return false;
    }
    return true;
}
function EnableControl(comp) {
    if (comp.checked) {
        document.getElementById('btnSignUp').disabled = false;
    }
    else {
        document.getElementById('btnSignUp').disabled = true;
    }
}