﻿var oRegisterUser =
{
    mWindowLoad :
        function()
        {
            oRegisterUser.mAddEventListeners();
        
            var oError = document.getElementById(window.oControls.lblErrorId);
            var sError = oError.innerHTML;
            
            if((sError != null) && (sError != ""))
            {
                sError.replace(/'\''/, "\'");
                setTimeout('alert(\''+sError+'\');', 1);
                oError.innerHTML = "";
            }
            
            if(window.sMode == 'registration')
            {
                oRegisterUser.mInitRegistration();
            }
        },
    
    mInitRegistration :
        function()
        {
            oRegisterUser.mSetCountryCodeLabel();
        
            oRegisterUser.mSetInvoiceDataVisible();
            oRegisterUser.mSetShippingDataVisible();
            
            oRegisterUser.mSetDefaultValues();
        },
    
    mSetDefaultValues :
        function()
        {
            var txtInvoiceZip = document.getElementById(window.oControls.txtInvoiceZipId);
            var txtInvoiceStreet = document.getElementById(window.oControls.txtInvoiceStreetId);
            var txtInvoiceStreetType = document.getElementById(window.oControls.txtInvoiceStreetTypeId);
            var txtInvoiceStreetNumber = document.getElementById(window.oControls.txtInvoiceStreetNumberId);
            
            var txtShippingZip = document.getElementById(window.oControls.txtShippingZipId);
            var txtShippingStreet = document.getElementById(window.oControls.txtShippingStreetId);
            var txtShippingStreetType = document.getElementById(window.oControls.txtShippingStreetTypeId);
            var txtShippingStreetNumber = document.getElementById(window.oControls.txtShippingStreetNumberId);
            
            if(txtInvoiceZip.value == "")
                txtInvoiceZip.value = window.oDefaultValue.zip;
            if(txtInvoiceStreet.value == "")
                txtInvoiceStreet.value = window.oDefaultValue.street;
            //if(txtInvoiceStreetType.value == "")
            //    txtInvoiceStreetType.value = window.oDefaultValue.streetType;
            if(txtInvoiceStreetNumber.value == "")
                txtInvoiceStreetNumber.value = window.oDefaultValue.streetNumber;
            
            if(txtShippingZip.value == "")
                txtShippingZip.value = window.oDefaultValue.zip;
            if(txtShippingStreet.value == "")
                txtShippingStreet.value = window.oDefaultValue.street;
            //if(txtShippingStreetType.value == "")
            //    txtShippingStreetType.value = window.oDefaultValue.streetType;
            if(txtShippingStreetNumber.value == "")
                txtShippingStreetNumber.value = window.oDefaultValue.streetNumber;
        },

    mAddEventListeners :
        function()
        {
            if(window.sMode == 'registration')
            {
                oRegisterUser.mAddCountryCodeEventListener();
                oRegisterUser.mAddInvoiceClickEventListeners();
                oRegisterUser.mAddShippingClickEventListeners();
            }
        },
    
    mAddCountryCodeEventListener :
        function()
        {
            var ddlCountries = document.getElementById(window.oControls.ddlCountriesId);
            
            ddlCountries.onclick = function() { oRegisterUser.mSetCountryCodeLabel() };
            //ddlCountries.setAttribute('onclick', 'oRegisterUser.mSetCountryCodeLabel();');
        },
    
    mAddInvoiceClickEventListeners :
        function()
        {
            var rdbInvoiceTrue = document.getElementById(window.oControls.rdbInvoiceTrueId);
            var rdbInvoiceFalse = document.getElementById(window.oControls.rdbInvoiceFalseId);
            
            var sClickScript = "if(this.value=='{value}'){this.value='';}";
            var rValueRegexp = /\{value\}/;
            
            var txtZip = document.getElementById(window.oControls.txtInvoiceZipId);
            var txtStreet = document.getElementById(window.oControls.txtInvoiceStreetId);
            //var txtStreetType = document.getElementById(window.oControls.txtInvoiceStreetTypeId);
            var txtStreetNumber = document.getElementById(window.oControls.txtInvoiceStreetNumberId);
            
            rdbInvoiceTrue.onclick = function() { oRegisterUser.mSetInvoiceDataVisible(); };
            rdbInvoiceFalse.onclick = function() { oRegisterUser.mSetInvoiceDataVisible(); };
            //rdbInvoiceTrue.setAttribute('onclick', 'oRegisterUser.mSetInvoiceDataVisible();');
            //rdbInvoiceFalse.setAttribute('onclick', 'oRegisterUser.mSetInvoiceDataVisible();');
            
            //txtZip.setAttribute('onclick', sClickScript.replace(rValueRegexp, window.oDefaultValue.zip));
            //txtStreet.setAttribute('onclick', sClickScript.replace(rValueRegexp, window.oDefaultValue.street));
            //txtStreetType.setAttribute('onclick', sClickScript.replace(rValueRegexp, window.oDefaultValue.streetType));
            //txtStreetNumber.setAttribute('onclick', sClickScript.replace(rValueRegexp, window.oDefaultValue.streetNumber));
            txtZip.onclick = function() { if(this.value == window.oDefaultValue.zip) { this.value = ''; } };
            txtStreet.onclick = function() { if(this.value == window.oDefaultValue.street) { this.value = ''; } };
            //txtStreetType.onclick = function() { if(this.value == window.oDefaultValue.streetType) { this.value = ''; } };
            txtStreetNumber.onclick = function() { if(this.value == window.oDefaultValue.streetNumber) { this.value = ''; } };
        },
        
    mAddShippingClickEventListeners :
        function()
        {
            var rdbShippingTrue = document.getElementById(window.oControls.rdbShippingTrueId);
            var rdbShippingFalse = document.getElementById(window.oControls.rdbShippingFalseId);
            
            //rdbShippingTrue.setAttribute('onclick', 'oRegisterUser.mSetShippingDataVisible();');
            //rdbShippingFalse.setAttribute('onclick', 'oRegisterUser.mSetShippingDataVisible();');
            rdbShippingTrue.onclick = function() { oRegisterUser.mSetShippingDataVisible(); };
            rdbShippingFalse.onclick = function() { oRegisterUser.mSetShippingDataVisible(); };
            
            var sClickScript = "if(this.value=='{value}'){this.value='';}";
            var rValueRegexp = /\{value\}/;
            
            var txtZip = document.getElementById(window.oControls.txtShippingZipId);
            var txtStreet = document.getElementById(window.oControls.txtShippingStreetId);
            //var txtStreetType = document.getElementById(window.oControls.txtShippingStreetTypeId);
            var txtStreetNumber = document.getElementById(window.oControls.txtShippingStreetNumberId);
            
            //txtZip.setAttribute('onclick', sClickScript.replace(rValueRegexp, window.oDefaultValue.zip));
            //txtStreet.setAttribute('onclick', sClickScript.replace(rValueRegexp, window.oDefaultValue.street));
            //txtStreetType.setAttribute('onclick', sClickScript.replace(rValueRegexp, window.oDefaultValue.streetType));
            //txtStreetNumber.setAttribute('onclick', sClickScript.replace(rValueRegexp, window.oDefaultValue.streetNumber));
            txtZip.onclick = function() { if(this.value == window.oDefaultValue.zip) { this.value = ''; } };
            txtStreet.onclick = function() { if(this.value == window.oDefaultValue.street) { this.value = ''; } };
            //txtStreetType.onclick = function() { if(this.value == window.oDefaultValue.streetType) { this.value = ''; } };
            txtStreetNumber.onclick = function() { if(this.value == window.oDefaultValue.streetNumber) { this.value = ''; } };
            
        },
    
    mSetCountryCodeLabel :
        function()
        {
            var ddlCountries = document.getElementById(window.oControls.ddlCountriesId);
            var lblCountryCode = document.getElementById(window.oControls.lblCountryCodeId);
            var sCountryCode = ddlCountries.options[ddlCountries.selectedIndex].value;
            sCountryCode = "+"+sCountryCode;
            
            lblCountryCode.innerHTML = sCountryCode;
        },
    
    mSetInvoiceDataVisible :
        function()
        {
            var rdbInvoiceTrue = document.getElementById(window.oControls.rdbInvoiceTrueId);
            
            var oInvoiceDiv = document.getElementById('invoiceData');
            var oInvoiceRemark = document.getElementById('invoiceRemarks');
            
            var bHide = !rdbInvoiceTrue.checked;
            
            oRegisterUser.mHideControl(oInvoiceDiv, bHide);
            oRegisterUser.mHideControl(oInvoiceRemark, bHide);            
            
            var rdbShippingTrue = document.getElementById(window.oControls.rdbShippingTrueId);

            if(!bHide && rdbShippingTrue.checked){
              document.getElementById('copyInvoiceDataSection').style.display = 'block';
            }
            else{
              document.getElementById('copyInvoiceDataSection').style.display = 'none';
            }
        },
    
    mCopyInvoiceDataToShipping : 
        function()
        {
            var txtInvoiceName = document.getElementById(window.oControls.txtInvoiceNameId);
            var txtInvoiceCity = document.getElementById(window.oControls.txtInvoiceCityId);
            var txtInvoiceZIP = document.getElementById(window.oControls.txtInvoiceZipId);
            var txtInvoiceStreet = document.getElementById(window.oControls.txtInvoiceStreetId);
            var txtInvoiceStreetType = document.getElementById(window.oControls.txtInvoiceStreetTypeId);
            var txtInvoiceStreetNumber = document.getElementById(window.oControls.txtInvoiceStreetNumberId);
            
            document.getElementById(window.oControls.txtShippingNameId).value = txtInvoiceName.value;
            document.getElementById(window.oControls.txtShippingCityId).value = txtInvoiceCity.value;
            document.getElementById(window.oControls.txtShippingZipId).value = txtInvoiceZIP.value;
            document.getElementById(window.oControls.txtShippingStreetId).value = txtInvoiceStreet.value;
            document.getElementById(window.oControls.txtShippingStreetTypeId).selectedIndex = txtInvoiceStreetType.selectedIndex;
            document.getElementById(window.oControls.txtShippingStreetNumberId).value = txtInvoiceStreetNumber.value;
        },
    
    mSetShippingDataVisible :
        function()
        {
            var rdbShippingTrue = document.getElementById(window.oControls.rdbShippingTrueId);
            
            var oShippingLegend = document.getElementById('shippingLegend');
            var oshippingData = document.getElementById('shippingData');
            var oshippingRemarks = document.getElementById('shippingRemarks');
            
            var bHide = !rdbShippingTrue.checked;
            
            oRegisterUser.mHideControl(oShippingLegend, bHide);
            oRegisterUser.mHideControl(oshippingData, bHide);
            oRegisterUser.mHideControl(oshippingRemarks, bHide);
            
            var rdbInvoiceTrue = document.getElementById(window.oControls.rdbInvoiceTrueId);
            
            if(!bHide && rdbInvoiceTrue.checked){
              document.getElementById('copyInvoiceDataSection').style.display = 'block';
            }
            else{
              document.getElementById('copyInvoiceDataSection').style.display = 'none';
            }
        },
    
    mHideControl :
        function(oControl, bHide)
        {
            var sStyle = '';
            
            if(bHide)
                sStyle = 'display: none;';
            
            oControl.setAttribute('style', sStyle);
            oControl.style.cssText = sStyle;
        },
    
    mValidateInput :
        function()
        {
            var txtPersonName = document.getElementById(window.oControls.txtPersonNameId);
            var txtPhoneNumber = document.getElementById(window.oControls.txtPhoneNumberId);
            var txtEmail = document.getElementById(window.oControls.txtEmailId);
            var txtPassword = document.getElementById(window.oControls.txtPasswordId);
            var txtPasswordRepeat = document.getElementById(window.oControls.txtPasswordRepeatId);
            
            var spanInvoiceTrue = document.getElementById('spanInvoiceTrue');
            var spanInvoiceFalse = document.getElementById('spanInvoiceFalse');
            var rdbInvoiceTrue = document.getElementById(window.oControls.rdbInvoiceTrueId);
            var rdbInvoiceFalse = document.getElementById(window.oControls.rdbInvoiceFalseId);
            var txtInvoiceName = document.getElementById(window.oControls.txtInvoiceNameId);
            var txtInvoiceCity = document.getElementById(window.oControls.txtInvoiceCityId);
            var txtInvoiceZIP = document.getElementById(window.oControls.txtInvoiceZipId);
            var txtInvoiceStreet = document.getElementById(window.oControls.txtInvoiceStreetId);
            //var txtInvoiceStreetType = document.getElementById(window.oControls.txtInvoiceStreetTypeId);
            var txtInvoiceStreetNumber = document.getElementById(window.oControls.txtInvoiceStreetNumberId);
            
            var spanShippingTrue = document.getElementById('spanShippingTrue');
            var spanShippingFalse = document.getElementById('spanShippingFalse');
            var rdbShippingTrue = document.getElementById(window.oControls.rdbShippingTrueId);
            var rdbShippingFalse = document.getElementById(window.oControls.rdbShippingFalseId);
            var txtShippingName = document.getElementById(window.oControls.txtShippingNameId);
            var txtShippingCity = document.getElementById(window.oControls.txtShippingCityId);
            var txtShippingZIP = document.getElementById(window.oControls.txtShippingZipId);
            var txtShippingStreet = document.getElementById(window.oControls.txtShippingStreetId);
            //var txtShippingStreetType = document.getElementById(window.oControls.txtShippingStreetTypeId);
            var txtShippingStreetNumber = document.getElementById(window.oControls.txtShippingStreetNumberId);
            
            //var txtLicensePlateNumber = document.getElementById(window.oControls.txtLicensePlateNumberId);
            
            if(txtInvoiceZIP.value == window.oDefaultValue.zip)
                txtInvoiceZIP.value = "";
            if(txtInvoiceStreet.value == window.oDefaultValue.street)
                txtInvoiceStreet.value = "";
            //if(txtInvoiceStreetType.value == window.oDefaultValue.streetType)
            //    txtInvoiceStreetType.value = "";
            if(txtInvoiceStreetNumber.value == window.oDefaultValue.streetNumber)
                txtInvoiceStreetNumber.value = "";
            
            if(txtShippingZIP.value == window.oDefaultValue.zip)
                txtShippingZIP.value = "";
            if(txtShippingStreet.value == window.oDefaultValue.street)
                txtShippingStreet.value = "";
            //if(txtShippingStreetType.value == window.oDefaultValue.streetType)
            //    txtShippingStreetType.value = "";
            if(txtShippingStreetNumber.value == window.oDefaultValue.streetNumber)
                txtShippingStreetNumber.value = "";
            
            var bValid = true;
            
            bValid = bValid & oRegisterUser.mValidateCommonData(txtPersonName, txtPhoneNumber, txtEmail,
                                txtPassword, txtPasswordRepeat);
            bValid = bValid & oRegisterUser.mValidateAddressData(rdbInvoiceTrue, rdbInvoiceFalse, spanInvoiceTrue, spanInvoiceFalse,
                                txtInvoiceName, txtInvoiceCity, txtInvoiceZIP, txtInvoiceStreet, /* txtInvoiceStreetType,*/
                                txtInvoiceStreetNumber);
            bValid = bValid & oRegisterUser.mValidateAddressData(rdbShippingTrue, rdbShippingFalse, spanShippingTrue, spanShippingFalse,
                                txtShippingName, txtShippingCity, txtShippingZIP, txtShippingStreet, /* txtShippingStreetType,*/
                                txtShippingStreetNumber);
            //bValid = bValid & oRegisterUser.mValidateLicensePlateNumber(txtLicensePlateNumber);
            
            if(!bValid)
            {
                oRegisterUser.mSetDefaultValues();
                alert(window.oErrors.inputError);
            }
            
            return bValid;
        },
    
    mValidateCommonData :
        function(txtPersonName, txtPhoneNumber, txtEmail, txtPassword, txtPasswordRepeat)
        {
            var bValid = true;
            
            var personNameRegexp = /^.+$/;
            var phoneNumberRegexp = /^[0-9 -()]+$/;
            var emailRegexp = /^[a-zA-Z0-9._]+@[a-zA-Z0-9]+.[a-zA-Z0-9.]+$/;
            var passwordRegexp = /^[a-zA-Z0-9]{4,}$/;
            
            bValid = bValid & oRegisterUser.mValidateWithRegexp(txtPersonName, personNameRegexp);
            bValid = bValid & oRegisterUser.mValidateWithRegexp(txtPhoneNumber, phoneNumberRegexp);
            bValid = bValid & oRegisterUser.mValidateWithRegexp(txtEmail, emailRegexp);
            
            if((txtPassword.value == null) || (txtPassword.value.match(passwordRegexp) == null) ||
                (txtPassword.value != txtPasswordRepeat.value))
            {
                txtPassword.setAttribute('style', 'background-color: #FFD0CC');
                txtPassword.style.cssText = 'background-color: #FFD0CC';
                txtPasswordRepeat.setAttribute('style', 'background-color: #FFD0CC');
                txtPasswordRepeat.style.cssText = 'background-color: #FFD0CC';
                bValid = false;
            }
            else
            {
                txtPassword.setAttribute('style', '');
                txtPassword.style.cssText = '';
                txtPasswordRepeat.setAttribute('style', '');
                txtPasswordRepeat.style.cssText = '';
            }
            
            return bValid;
        },
    
    mValidateAddressData :
        function(rdbTrue, rdbFalse, spanTrue, spanFalse, txtName, txtCity, txtZip, txtStreet, /*txtStreetType,*/ txtStreetNumber)
        {
            var bValid = true;
        
            if((!rdbTrue.checked) && (!rdbFalse.checked))
            {
                spanTrue.setAttribute('style', 'color: red;');
                spanTrue.style.cssText = "color: red;";
                spanFalse.setAttribute('style', 'color: red;');
                spanFalse.style.cssText = "color: red;";
                return false;
            }
            else
            {
                spanTrue.setAttribute('style', '');
                spanTrue.style.cssText = "";
                spanFalse.setAttribute('style', '');
                spanFalse.style.cssText = "";
            }
            
            if(!rdbTrue.checked)
                return true;
            
            var nameRegexp = /^.+$/;
            var cityRegexp = /^.+$/;
            var zipRegexp = /^.+$/;
            var streetRegexp = /^.+$/;
            //var streetTypeRegexp = /^.+$/;
            var streetNumberRegexp = /^.+$/;
            
            bValid = bValid & oRegisterUser.mValidateWithRegexp(txtName, nameRegexp);
            bValid = bValid & oRegisterUser.mValidateWithRegexp(txtCity, cityRegexp);
            bValid = bValid & oRegisterUser.mValidateWithRegexp(txtZip, zipRegexp);
            bValid = bValid & oRegisterUser.mValidateWithRegexp(txtStreet, streetRegexp);
            //bValid = bValid & oRegisterUser.mValidateWithRegexp(txtStreetType, streetTypeRegexp);
            bValid = bValid & oRegisterUser.mValidateWithRegexp(txtStreetNumber, streetNumberRegexp);
            
            return bValid;
        },
    
    mValidateLicensePlateNumber :
        function(txtLicensePlateNumber)
        {
            var licensePlateRegexp = /^.{0}$|^[A-Za-z]{3}[ -]?[0-9]{3}$/;
            
            return oRegisterUser.mValidateWithRegexp(txtLicensePlateNumber, licensePlateRegexp);
        },
    
    mValidateWithRegexp :
        function(txtControl, regValidator)
        {
            if(txtControl.value.match(regValidator) == null)
            {
                txtControl.setAttribute('style', 'background-color: #FFD0CC');
                txtControl.style.cssText = 'background-color: #FFD0CC';
                return false;
            }
            else
            {
                txtControl.setAttribute('style', '');
                txtControl.style.cssText = '';
                return true;
            }
        }
};

oVox.mOnload(oRegisterUser.mWindowLoad);

