﻿var blnagreedToTOS = true; //boolean to store whether terms of services have been agreed
var blnEmailsMatch = true; //boolean to store whether emails typed match
var blnAllowSendMail = true;
//To read and print Terms of Contract
var IsEps = getQSValue("IsEPS");
if (IsEps == "false")
    IsEps = false;
if (IsEps == "") {
    IsEps = false;
}
function agreedToTOS() {
    if (document.getElementById("chkAgreeTC").checked) {
        document.getElementById("spnTOS").style.display = "none";
        blnagreedToTOS = true;
    }
    else {
        document.getElementById("spnTOS").style.display = "block";
        blnagreedToTOS = false;
    }
};

function agreedToAllowSendMail() {
    if (document.getElementById("chkSendMail").checked) {
        document.getElementById("spnSendMail").style.display = "none";
        blnAllowSendMail = true;
    }
    else {
        document.getElementById("spnSendMail").style.display = "block";        
        blnAllowSendMail = false;
    }
};

function validateEmails() {
    if (document.getElementById("txtReTypeEmail").value != "" && (document.getElementById("txtEmail").value != document.getElementById("txtReTypeEmail").value)) {
        document.getElementById("spnEmailMismatch").style.display = "block";
        blnEmailsMatch = false;
    }
    else {
        document.getElementById("spnEmailMismatch").style.display = "none";
        blnEmailsMatch = true;
    }
};

function onSubmitRegistrationStep1Details() {
    
        var lmPleaseWait = bb.document.getElementById('lmPleaseWait');
        lmPleaseWait.show();
        var objContact = { ContactId: 0, FirstName: document.getElementById("txtFirstName").value, LastName: document.getElementById("txtLastName").value, IsAllowSendMail: document.getElementById("chkSendMail").checked };
        var objUser = { UserName: document.getElementById("txtEmail").value };
        var strLangCode = getQSValue("Lang");
        var url = window.location.search.substring(1)
        var Isbusiness = true; 
        if(IsEps==true)
            Isbusiness = getQSValue("Isbusiness");
        else
            Isbusiness = true;                
            
        var IntRID = getQSValue("RID");
        var strHCompInitial = getQSValue("HCompInitial");
        EC09WebApp.EC09WebService.EC09WebService.RegisterCustomerStep1(objContact, objUser, url, strLangCode, strHCompInitial, IntRID, onSuccessRegisterCustomerStep1, onRegistrationFail);
    
};

function onSuccessRegisterCustomerStep1(step1response) {
    var divDetails = document.getElementById("divDetails");
    var username = document.getElementById("txtEmail").value;
    strHCompInitial = getQSValue("HCompInitial");
    strLanguageKey = getQSValue("Lang");
    if (step1response == "true") {
        if (strHCompInitial == "MMPHAND")
            bb.command.load('SuccessfulRegistration.aspx?', 'GET', null, null, divDetails, "replace");
        else if (IsEps == "true")
            bb.command.load('EPS_SuccessfulRegistration.aspx?HCompInitial=' + strHCompInitial + '&Lang=' + strLanguageKey, 'GET', null, null, divDetails, "replace");
        else
            bb.command.load('EC_SuccessfulRegistration.aspx?', 'GET', null, null, divDetails, "replace");
    }
    else if (step1response == "UserExist") {
        if (bb.browser.gecko)
            document.getElementById("spnRegistrationError").innerHTML = step1msgUserExist;
        else
            document.getElementById("spnRegistrationError").innerText = step1msgUserExist;

     }
    else
     {
        if (bb.browser.gecko)
            document.getElementById("spnRegistrationError").innerHTML = step1response;
        else
            document.getElementById("spnRegistrationError").innerText = step1response;

    }
    bb.document.getElementById('lmPleaseWait').hide();    
}

function onSucessregthx(arg) {
    document.getElementById('lnkCustomerLogin1').style.display = 'none';
    bb.document.getElementById('lmPleaseWait').hide();
};
function onRegistrationFail(response) {

    
    showTopMsg(response._message, 7000, '1', true);
    
    if (bb.browser.gecko)
        document.getElementById("spnRegistrationError").innerHTML = response._message;        
    else
        document.getElementById("spnRegistrationError").innerText = response._message;
    
    bb.document.getElementById('lmPleaseWait').hide();
};

function onRegistrationCancel() {
try
{
    if (strHCompInitial == "MMPHAND")
        location.href = "../MMPLOGIN.aspx";
    else if (strHCompInitial == "PCBSHOPHAND")
        location.href = "../ThePCBShopLOGIN.aspx";
    else {
        
        if (IsEps == "true")
            location.href = "../EPSLOGIN.aspx";
        else 
            location.href = "../ECLOGIN.aspx";
    }
 }
 catch (exception) {
     showTopMsg(exception, 7000, '1', true);
 }

};

function trimEmailSpaces() {
    document.getElementById('txtEmail').value = document.getElementById('txtEmail').value.trim();
    document.getElementById('txtReTypeEmail').value = document.getElementById('txtReTypeEmail').value.trim();
};
function OnStepOneregistrationSubmit() {
try 
{
    clearErrorMsg('spnRegistrationError');
    trimEmailSpaces();
    agreedToTOS();
    agreedToAllowSendMail();
    validateEmails();
    if (bb.evaluateSmart("id('frmRegistration')").validate() && blnagreedToTOS && blnEmailsMatch && blnAllowSendMail) {
        onSubmitRegistrationStep1Details();
    }
 }
 catch (exception) {
     showTopMsg(exception, 7000, '1', true);
 }

};
function OnStepOneregistrationCancel() {
try 
{
    clearErrorMsg('spnRegistrationError');
    onRegistrationCancel();
 }
 catch (exception) {
     showTopMsg(exception, 7000, '1', true);
 }
};
