﻿function Cancel() {
    if (GetValueFromURL("id") != null)
        window.close();
    else
        window.location.href = '/index.aspx';
}

function showLayer(layer) {
    if (document.all && document.all[layer])
        document.all[layer].style.display = 'block';
    else if (document.getElementById(layer))
        document.getElementById(layer).style.display = 'block';
}

function hideLayer(layer) {
    if (document.all && document.all[layer])
        document.all[layer].style.display = 'none';
    else if (document.getElementById(layer))
        document.getElementById(layer).style.display = 'none';
}

//---------------------------------------------------------------------------------------------------------
// Secondary functions: called during form validation process
//---------------------------------------------------------------------------------------------------------

function InitRegistration() {
    ShowState();
}

function GetUserResolution() {
    ecran_l = screen.width;
    ecran_h = screen.height;
    couleurs = (1 << screen.colorDepth);
    resolution = ecran_l + "x" + ecran_h + "x" + couleurs;
    getObjFormField("RegistrationForm", "USREG_00_Resolution").value = resolution;
}

function isPhoneNumber(aStr) {
    var index;
    var reg = "0123456789()+/\\-_. "
    for (index = 0; index < aStr.length; index++)
        if (reg.indexOf(aStr.charAt(index)) == -1)
        return false;
    return true;
}

function checkphone(champ, message) {
    if (!isPhoneNumber(champ.value)) {
        alert(message);
        champ.select();
        champ.focus();
    }
}

function ShowState() {
    if (USREG_00_Country.options[USREG_00_Country.selectedIndex].value == "1" || USREG_00_Country.options[USREG_00_Country.selectedIndex].value == "2" || USREG_00_Country.options[USREG_00_Country.selectedIndex].value == "158") {
        showLayer("state");
    }
    else {
        hideLayer("state");
        USREG_00_State.selectedIndex = 0;
    }
}

var emailArr = new Array(
    "@126.",
    "@163.",
    "@aliceadsl.",
    "@alicemail.",
    "@aol.",
    "@bellsouth.",
    "@comcast.",
    "@earthlink.",
    "@gmail.",
    "@hanmail.",
    "@hotmail.",
    "@msn.",
    "@orange.",
    "@rediffmail.",
    "@sbcglobal.",
    "@tiscali.",
    "@t-online.",
    "@verizon.",
    "@vsnl.",
    "@wanadoo.",
    "@yahoo.",
    "@libero.",
    "@free."
    );

function Rd(url) {
    if (url != "") {
        window.location.href = url;
    }
}

//---------------------------------------------------------------------------------------------------------
// Form Validation
//---------------------------------------------------------------------------------------------------------
// Validation du formulaire
var isemailcheck = false;
function Validation() {
    //getObjForm("RegistrationForm").submit();
    //return true;
  
    // First Name
    if (isNull(USREG_00_FirstName.value)) {
        alert("First Name is required.");
        USREG_00_FirstName.focus();
        return false;
    }
    // Last Name
    if (isNull(USREG_00_LastName.value)) {
        alert("Last Name is required.");
        USREG_00_LastName.focus();
        return false;
    }
    // Email
    if (USREG_00_Email)
    {
        if (isNull(USREG_00_Email.value)) {
            alert("E-Mail is required.");
            USREG_00_Email.focus();
            return false;
        }
        if (!checkmail(USREG_00_Email.value)) {
            alert("Email address does not seem to be valid.");
            USREG_00_Email.focus();
            return false;
        }
        // Confirmation Email
        if (isNull(USREG_00_ConfirmEmail.value)) {
            alert("Please confirm your e-mail address.");
            USREG_00_ConfirmEmail.focus();
            return false;
        }
        else if (USREG_00_Email.value.toLowerCase() != USREG_00_ConfirmEmail.value.toLowerCase()) {
            alert("You have specified 2 different email addresses. Please make sure you enter similar and correct addresses.")
            BackUpMistypedEmails(USREG_00_Email.value, USREG_00_ConfirmEmail.value)
            //USREG_00_ConfirmEmail.select();
            USREG_00_Email.focus();
            USREG_00_ConfirmEmail.focus();
            return false;
        }
    }
    // Company Name
    if (isNull(USREG_00_Company.value)) {
        alert("Company is required.");
        USREG_00_Company.focus();
        return false;
    }
    // City
    if (USREG_00_City)
    {
        if (isNull(USREG_00_City.value)) {
            alert("City is required.");
            USREG_00_City.focus();
            return false;
        }
    }
    // Country
    if (USREG_00_Country.selectedIndex == 0) {
        alert("Country is required.");
        USREG_00_Country.focus();
        return false;
    }
    // State
    /*
    if (USREG_00_Country.options[USREG_00_Country.selectedIndex].value == "1" || USREG_00_Country.options[USREG_00_Country.selectedIndex].value == "2" || USREG_00_Country.options[USREG_00_Country.selectedIndex].value == "158") {
        if (USREG_00_State.options[USREG_00_State.selectedIndex].value == "0" || USREG_00_State.options[USREG_00_State.selectedIndex].value == "") {
            alert("State/Province is required for US, Canada and Australia users.");
            USREG_00_State.focus();
            return false;
        }
    }
    */
    // Phone number
    if (USREG_00_Phone)
    {
        if (isNull(USREG_00_Phone.value)) {
            alert("Phone is required.");
            USREG_00_Phone.focus();
            return false;
        }
        else if (isNull(USREG_00_Inter_Phone.value)) {
            alert("International code for phone is required.");
            USREG_00_Inter_Phone.focus();
            return false;
        }
    }
    // Job Department
    if (USREG_00_JobFunction.selectedIndex == 0) {
        alert("Job Department is required.");
        USREG_00_JobFunction.focus();
        return false;
    }
    // Activity
    if (USREG_00_Activity.selectedIndex == 0) {
        alert("Activity is required.");
        USREG_00_Activity.focus();
        return false;
    }
    if (ValidateCombo())
        return false;
    // User ID
    if (isNull(USREG_00_Login.value)) {
        alert("Please enter your User ID.");
        USREG_00_Login.focus();
        return false;
    }
    rexp = /^[a-zA-Z0-9]+$/
    if (!rexp.test(USREG_00_Login.value)) {
        alert("Your User ID must contain only alpha-numeric characters. Please enter a new User ID.");
        USREG_00_Login.select();
        return false;
    }
    if ((USREG_00_Login.value.length < 6) || (USREG_00_Login.value.length > 12)) {
        alert("Your User ID must contain between 6 and 12 alpha-numeric characters. Please enter a new User ID.");
        USREG_00_Login.select();
        return false;
    }
    // Password
    if ((isNull(USREG_00_Password1.value))) {
        alert("Please enter your password.");
        USREG_00_Password1.select();
        return false;
    }
    else if (!rexp.test(USREG_00_Password1.value) || USREG_00_Password1.value.length < 6 || USREG_00_Password1.value.length > 12) {
        alert("Your password must contain between 6 and 12 alpha-numeric characters. Please enter a new password.");
        USREG_00_Password1.select();
        return false;
    }
    else if ((isNull(USREG_00_Password2.value))) {
        alert("Please confirm your password.");
        USREG_00_Password2.select();
        return false;
    }
    else if (USREG_00_Password1.value != USREG_00_Password2.value) {
        alert("Please confirm your password.")
        USREG_00_Password2.select();
        return false;
    }
    if (getObjFormField("RegistrationForm", "ConfirmTermsConditions")) {
        if (!getObjFormField("RegistrationForm", "ConfirmTermsConditions").checked) {
            alert("You have to agree SpecialChem's Terms and Conditions to continue.");
            getObjFormField("RegistrationForm", "ConfirmTermsConditions").focus();
            return false;
        }
    }

    var popupheight = "200"; 							// define the popup's height;
    var popupwidth = "500"; 							// define the popup's width;
    var popuptop = (screen.availHeight - popupheight) / 2; // define the popup's vertical position on screen;
    var popupleft = (screen.availWidth - popupwidth) / 2; 	// define the popup's horizontal position on screen;

    var emailperso = false;
    for (var i = 0; i < emailArr.length; i++) {
        if (USREG_00_Email.value.indexOf(emailArr[i]) != -1) {
            emailperso = true;
            break;
        }
    }
    if (emailperso && !isemailcheck) {
        var popup = window.open("/common/shared/registration/emailconfirmation.aspx", "popup", "width=" + popupwidth + ",height=" + popupheight + ",toolbar=no,scrollbars=no,statusbar=no,resizable=no,top=" + popuptop + ",left=" + popupleft + "");
        popup.focus();
        return false;
    }
    GetUserResolution();
    return true;
}


//---------------------------------------------------------------------------------------------------------
// email Confirmation Methods
//---------------------------------------------------------------------------------------------------------

function ConfirmEmail() {
    window.opener.isemailcheck = true;
    window.opener.btn_Click();
    window.close();
}

function ModifyEmail() {
    window.opener.USREG_00_Email.select();
    window.close();
}

function Init() {
    document.getElementById("email").innerHTML = window.opener.USREG_00_Email.value;
}


//---------------------------------------------------------------------------------------------------------
// Mistyped email
//---------------------------------------------------------------------------------------------------------
function BackUpMistypedEmails(email, emailconfirmation) {
    try {
        $.ajax({ url: '/common/shared/registration/mistyped-emails.aspx?email=' + email + '&emailconf=' + emailconfirmation });
    }
    catch (err) { }
}
