TrafficDB = {
    currentFilialNumber: 0,
    commentUploadPhoto: null,
    uploadBeginFlag: 0,
    uploadOperationFlag: null,
    uploadDeleteFileName: null,
    openFilialForm: false, 
    regTextField: /^[a-zа-я_0-9-\s\.\(\)\[\]\/,#№"']*$/i,
    regPhoneCode: /^[0-9\(\)]*$/i,
    regPhone: /^[0-9\-\+]*$/i,
    regUrl: /^(www\.)?[а-яА-Яa-zA-Z0-9-\.]+\.(com|ru|org|net|mil|edu|ca|co.uk|com.au|gov|biz|su|de|ру)$/i,
    editionType: 'gts',
    
    editionType: 'gts',
    
    openTab: function(tabName) {
        var objArgs = new Object();
        objArgs['newStep'] = tabName;
        objArgs['currentStep'] = $('currentStep').getValue();
        if($('currentStep').getValue() == "contact"){
            var DataTab = this.getDataContactTab();
        }else if($('currentStep').getValue() == "company"){
           var DataTab = this.getDataCompanyTab();
        }else if($('currentStep').getValue() == "photo"){
           var DataTab = this.getDataPhotoTab();
        }else if($('currentStep').getValue() == "filial"){
           var DataTab = this.getDataFilialTab();
        }else if($('currentStep').getValue() == "services"){
           var DataTab = this.getDataServicesTab();
        }else if($('currentStep').getValue() == "sender"){
           var DataTab = this.getDataSenderTab();
        }
        if(DataTab != null){
            objArgs['dataTab'] = DataTab;
            if(this.uploadOperationFlag != null){
                objArgs['operation'] = this.uploadOperationFlag;
                this.uploadOperationFlag = null;
            }
            if(this.uploadDeleteFileName != null){
                objArgs['fileName'] = this.uploadDeleteFileName;
                this.uploadDeleteFileName = null;
            }
            //this.windowOverlay('on');
            //makeCall('TrafficDB/showQuestionnaire', objArgs);
            $('args').value = Object.toJSON(objArgs);
            document.qustionnaireForm.submit();
        }
    },
    sendAllDataAnketa: function(){
        var objArgs = new Object();
        objArgs['saveAllData'] = true;
        $('args').value = Object.toJSON(objArgs);
        //this.windowOverlay('on');
        //makeCall('TrafficDB/showQuestionnaire', objArgs);
        document.qustionnaireForm.submit();
    },
    getDataContactTab: function(){
        var error = false;
        var objArgsContact = new Object();
        this.clearError('juristicFirmName');
        this.clearError('city');
        //this.clearError('region');
        this.clearError('adress');
        this.clearError('phoneMain');
        this.clearError('phoneCodeMain');
        this.clearError('phoneAdditionalFirst');
        this.clearError('phoneCodeAdditionalFirst');
        this.clearError('phoneAdditionalSecond');
        this.clearError('phoneCodeAdditionalSecond');
        this.clearError('fax');
        this.clearError('faxCode');
        this.clearError('emailFirst');
        this.clearValidateError('phoneMain', 'phoneMainNotValid');
        this.clearValidateError('postalCode', 'postalCodeNotValid');
        this.clearValidateError('emailFirst', 'emailFirstNotValid');
        this.clearValidateError('emailSecond', 'emailSecondNotValid');
        this.clearValidateError('siteFirst', 'siteFirstNotValid');
        this.clearValidateError('siteSecond', 'siteSecondNotValid');

        if(Filter.trim($('juristicFirmName').getValue()) == ""){
           error = true;
           this.showError('juristicFirmName');
        }
        if(Filter.trim($('city').getValue()) == ""){
           error = true;
           this.showError('city');
        }
        if(Filter.trim($('adress').getValue()) == ""){
           error = true;
           this.showError('adress');
        }
        if(Filter.trim($('phoneMain').getValue()) == ""){
           error = true;
           this.showError('phoneMain');
        }else if(Filter.trim($F('phoneMain')) != "" &&
                 !this.regPhone.test($F('phoneMain'))){
            error = true;
            this.showValidateError('phoneMain', 'phoneMainNotValid');
        }else if(Filter.trim($F('phoneCodeMain')) != "" &&
                 !this.regPhoneCode.test($F('phoneCodeMain'))){
            error = true;
            this.showError('phoneCodeMain');
        }
        
        if(Filter.trim($F('phoneAdditionalFirst')) != "" &&
           !this.regPhone.test($F('phoneAdditionalFirst'))){
           error = true;
           this.showError('phoneAdditionalFirst');
        }else if(Filter.trim($F('phoneAdditionalFirst')) != "" &&
                 Filter.trim($F('phoneCodeAdditionalFirst')) != "" &&
                 !this.regPhoneCode.test($F('phoneCodeAdditionalFirst'))){
           error = true;
           this.showError('phoneCodeAdditionalFirst');
        }
        if(Filter.trim($F('phoneAdditionalSecond')) != "" &&
           !this.regPhone.test($F('phoneAdditionalSecond'))){
           error = true;
           this.showError('phoneAdditionalSecond');
        }else if(Filter.trim($F('phoneAdditionalSecond')) != "" &&
                 Filter.trim($F('phoneCodeAdditionalSecond')) != "" &&
                 !this.regPhoneCode.test($F('phoneCodeAdditionalSecond'))){
           error = true;
           this.showError('phoneCodeAdditionalSecond');
        }
        if(Filter.trim($F('fax')) != "" &&
           !this.regPhone.test($F('fax'))){
           error = true;
           this.showError('fax');
        }else if(Filter.trim($F('fax')) != "" &&
                 Filter.trim($F('faxCode')) != "" &&
                 !this.regPhoneCode.test($F('faxCode'))){
           error = true;
           this.showError('faxCode');
        }
        if(Filter.trim($('emailFirst').getValue()) == ""){
           error = true;
           this.showError('emailFirst');
        }else if(!Validate.email($F('emailFirst'))){
           error = true;
           this.showValidateError('emailFirst', 'emailFirstNotValid');
        }
        if(Filter.trim($('postalCode').getValue()) != "" &&
           !Validate.digits($('postalCode').getValue()))
        {
            error = true;
            this.showValidateError('postalCode', 'postalCodeNotValid');
        }
        if(Filter.trim($F('emailSecond')) != "" &&
           !Validate.email($F('emailSecond')))
        {
            error = true;
            this.showValidateError('emailSecond', 'emailSecondNotValid');
        }
        if(Filter.trim($F('siteFirst')) != "" &&
           !this.regUrl.test($F('siteFirst'))){
            error = true;
            this.showValidateError('siteFirst', 'siteFirstNotValid');
        }
        if(Filter.trim($F('siteSecond')) != "" &&
           !this.regUrl.test($F('siteSecond'))){
            error = true;
            this.showValidateError('siteSecond', 'siteSecondNotValid');
        }
        /*if($F('region') == 0){
            this.showError('region');
            error = true;
        }*/

        if(error){
           return null;
        }else{
           objArgsContact['brandFirmName'] = $('brandFirmName').getValue();
           objArgsContact['juristicFirmName'] = $('juristicFirmName').getValue();
           objArgsContact['postalCode'] = $('postalCode').getValue();
           objArgsContact['city'] = $('city').getValue();
           objArgsContact['region'] = $F('region');
           objArgsContact['adress'] = $('adress').getValue();
           objArgsContact['showAdress'] = $('showAdress').getValue();
           objArgsContact['phoneCodeMain'] = $('phoneCodeMain').getValue();
           objArgsContact['phoneMain'] = $('phoneMain').getValue();
           objArgsContact['phoneCodeMain'] = $('phoneCodeMain').getValue();
           objArgsContact['phoneMain'] = $('phoneMain').getValue();
           objArgsContact['phoneCodeAdditionalFirst'] = $('phoneCodeAdditionalFirst').getValue();
           objArgsContact['phoneAdditionalFirst'] = $('phoneAdditionalFirst').getValue();
           objArgsContact['phoneCodeAdditionalSecond'] = $('phoneCodeAdditionalSecond').getValue();
           objArgsContact['phoneAdditionalSecond'] = $('phoneAdditionalSecond').getValue();
           objArgsContact['faxCode'] = $('faxCode').getValue();
           objArgsContact['fax'] = $('fax').getValue();
           objArgsContact['siteFirst'] = $('siteFirst').getValue();
           objArgsContact['siteSecond'] = $('siteSecond').getValue();
           objArgsContact['emailFirst'] = $('emailFirst').getValue();
           objArgsContact['emailOfficeFirst'] = $('emailOfficeFirst').getValue();
           objArgsContact['showEmailFirst'] = $('showEmailFirst').getValue();
           objArgsContact['emailSecond'] = $('emailSecond').getValue();
           objArgsContact['emailOfficeSecond'] = $('emailOfficeSecond').getValue();
           objArgsContact['showEmailSecond'] = $('showEmailSecond').getValue();
           return objArgsContact;
        }
    },
    getDataCompanyTab: function(){
        var error = false;
        this.clearError('anonsFirm');
        this.clearError('aboutFirm');
        this.clearValidateError('anonsFirm', 'anonsFirmNotValid');
        this.clearValidateError('aboutFirm', 'aboutFirmNotValid');

        if(!this.verificationDataDownTime()){
            error = true;
        }
        if(Filter.trim($('anonsFirm').getValue()) == ""){
            this.showError('anonsFirm');
            error = true;
        }else if($F('anonsFirm').length > 130){
            this.showValidateError('anonsFirm', 'anonsFirmNotValid');
            error = true;
        }
        if(Filter.trim($('aboutFirm').getValue()) == ""){
            this.showError('aboutFirm');
            error = true;
        }else if($F('aboutFirm').length > 1000){
            this.showValidateError('aboutFirm', 'aboutFirmNotValid');
            error = true;
        }
        if(error){
           return null;
        }else{
           var objArgsCompany = new Object();
           objArgsCompany['downTime'] = this.getDataDownTime();
           objArgsCompany['anonsFirm'] = $F('anonsFirm');
           objArgsCompany['aboutFirm'] = $F('aboutFirm');
           return objArgsCompany;
        }
    },
    getDataServicesTab: function(){
        var error = false;
        this.clearError('services');
        this.clearError('paymentOther');
        this.clearValidateError('services', 'servicesNotValid');
        this.clearValidateError('brand', 'brandNotValid');
        //this.clearValidateError('hotinformation', 'hotinformationNotValid');
        this.clearUploadError('uploadPriceBigSize');
        this.clearUploadError('uploadPricePartial');
        this.clearUploadError('uploadPriceNotUploaded');
        this.clearUploadError('uploadPriceWrongMime');
        this.clearUploadError('uploadPriceNoFile');
        $('meansPayError').hide();
        var meansPayData = Checkbox.getValue('meansPay');
        if(!meansPayData){
            $('meansPayError').show();
            error = true;
        }
        if(Filter.trim($('services').getValue()) == ""){
            this.showError('services');
            error = true;
        }else if($F('services').length > 700){
            this.showValidateError('services', 'servicesNotValid');
            error = true;
        }
        if($F('checkboxPaymentOther') == 7 && Filter.trim($F('paymentOther')) == ""){
            this.showError('paymentOther');
            error = true;
        }
        if(Filter.trim($F('brand')) != "" && $F('brand').length > 200){
            this.showValidateError('brand', 'brandNotValid');
            error = true;
        }
        /*if(Filter.trim($F('hotinformation')) != "" && $F('hotinformation').length > 200){
            this.showValidateError('hotinformation', 'hotinformationNotValid');
            error = true;
        }*/
        if(error){
           return null;
        }else{
            var objArgsServices = new Object();
            objArgsServices['meansPay'] = meansPayData;
            objArgsServices['brand'] = $F('brand');
            //objArgsServices['hotinformation'] = $F('hotinformation');
            objArgsServices['services'] = $F('services');
            if($F('checkboxPaymentOther') == 7){
                objArgsServices['paymentOther'] = $F('paymentOther');
            }
            return objArgsServices;
        }
    },
    getDataSenderTab: function(){
        var error = false;
        this.clearError('senderName');
        this.clearError('senderPost');
        this.clearError('senderEmail');
        this.clearError('senderPhone');
        this.clearError('senderPhoneCode');
        this.clearValidateError('senderPhone', 'senderPhoneNotValid');

        if(Filter.trim($('senderName').getValue()) == ""){
           error = true;
           this.showError('senderName');
        }
        if(Filter.trim($('senderPost').getValue()) == ""){
           error = true;
           this.showError('senderPost');
        }
        if(Filter.trim($('senderEmail').getValue()) == ""){
           error = true;
           this.showError('senderEmail');
        }else if(!Validate.email($F('senderEmail'))){
           error = true;
           this.showValidateError('senderEmail', 'senderEmailNotValid');
        }
        if(Filter.trim($('senderPhone').getValue()) == ""){
           error = true;
           this.showError('senderPhone');
        }else if(Filter.trim($F('senderPhone')) != "" &&
                 !this.regPhone.test($F('senderPhone'))){
            error = true;
            this.showValidateError('senderPhone', 'senderPhoneNotValid');
        }else if(Filter.trim($F('senderPhoneCode')) != "" &&
           !this.regPhoneCode.test($F('senderPhoneCode'))){
           error = true;
           this.showError('senderPhoneCode');
        }

        if(error){
           return null;
        }else{
            var objArgsSender = new Object();
            objArgsSender['senderName'] = $F('senderName');
            objArgsSender['senderPost'] = $F('senderPost');
            objArgsSender['senderEmail'] = $F('senderEmail');
            objArgsSender['senderPhone'] = $F('senderPhone');
            objArgsSender['senderPhoneCode'] = $F('senderPhoneCode');
            return objArgsSender;
        }
    },
    getDataPhotoTab: function(){
        var objArgsPhotoLogo = new Object();
        objArgsPhotoLogo['commentAnons'] = $F('commentAnons');
        if($F('notReadyLogo') == 'on'){
            objArgsPhotoLogo['logoOrder'] = 1;
        }else if($F('readyLogo') == 'on'){
            objArgsPhotoLogo['logoOrder'] = 0;
        }
        objArgsPhotoLogo['photoComment'] = $F('photoComment');
        return objArgsPhotoLogo;
    },
    getDataFilialTab: function(){
        var objArgsCompany = new Object();
        objArgsCompany['overlook'] = true;
        return objArgsCompany;
    },
    selectedDownTime: function(idElement){
        if(idElement == 'roundTheClock'){
            $('workdayForm').style.display = "none";
            $('sheduleForm').style.display = "none";
        }else if(idElement == 'workday'){
            $('sheduleForm').style.display = "none";
            $('workdayForm').style.display = "block";
        }else if(idElement == 'schedule'){
            $('sheduleForm').style.display = "block";
            $('workdayForm').style.display = "none";
        }
    },
    selectedDownTimeFilial: function(idElement, filialId){
        var elementId = idElement + filialId;
        if(elementId == 'roundTheClock' + filialId){
            $('workdayForm' + filialId).style.display = "none";
            $('sheduleForm' + filialId).style.display = "none";
        }else if(elementId == 'workday' + filialId){
            $('sheduleForm' + filialId).style.display = "none";
            $('workdayForm' + filialId).style.display = "block";
        }else if(elementId == 'schedule' + filialId){
            $('sheduleForm' + filialId).style.display = "block";
            $('workdayForm' + filialId).style.display = "none";
        }
    },
    changeDisableStatus: function(idElement){
        if($F(idElement) == "on"){
            $(idElement + "HourAt").disabled = false;
            $(idElement + "HourTo").disabled = false;
            $(idElement + "HourDinnerAt").disabled = false;
            $(idElement + "HourDinnerTo").disabled = false;
            $(idElement + "MinAt").disabled = false;
            $(idElement + "MinTo").disabled = false;
            $(idElement + "MinDinnerAt").disabled = false;
            $(idElement + "MinDinnerTo").disabled = false;
        }else{
            $(idElement + "HourAt").disabled = true;
            $(idElement + "HourTo").disabled = true;
            $(idElement + "HourDinnerAt").disabled = true;
            $(idElement + "HourDinnerTo").disabled = true;
            $(idElement + "MinAt").disabled = true;
            $(idElement + "MinTo").disabled = true;
            $(idElement + "MinDinnerAt").disabled = true;
            $(idElement + "MinDinnerTo").disabled = true;
        }
    },
    inizializeCompanyTab: function(){
        if($F('schedule') == 'on'){
            this.selectedDownTime('schedule');
        }else if($F('workday') == 'on'){
            this.selectedDownTime('workday');
        }
        this.changeDisableStatus('scheduleMonday');
        this.changeDisableStatus('scheduleTuesday');
        this.changeDisableStatus('scheduleWednesday');
        this.changeDisableStatus('scheduleThursday');
        this.changeDisableStatus('scheduleFriday');
        this.changeDisableStatus('scheduleSaturday');
        this.changeDisableStatus('scheduleSunday');
    },
    inizializeFilialTab: function(filialId){
        if($F('schedule' + filialId) == 'on'){
            this.selectedDownTimeFilial('schedule', filialId);
        }else if($F('workday' + filialId) == 'on'){
            this.selectedDownTimeFilial('workday', filialId);
        }
        this.changeDisableStatus('scheduleMonday' + filialId);
        this.changeDisableStatus('scheduleTuesday' + filialId);
        this.changeDisableStatus('scheduleWednesday' + filialId);
        this.changeDisableStatus('scheduleThursday' + filialId);
        this.changeDisableStatus('scheduleFriday' + filialId);
        this.changeDisableStatus('scheduleSaturday' + filialId);
        this.changeDisableStatus('scheduleSunday' + filialId);
    },
    verificationDataDownTime: function(filialId){
         var error = false;
         if(filialId == null){
             var scheduleMonday    = 'scheduleMonday';
             var scheduleTuesday   = 'scheduleTuesday';
             var scheduleWednesday = 'scheduleWednesday';
             var scheduleThursday  = 'scheduleThursday';
             var scheduleFriday    = 'scheduleFriday';
             var scheduleSaturday  = 'scheduleSaturday';
             var scheduleSunday    = 'scheduleSunday';
             var workday           = 'workday';
             var schedule          = 'schedule';
             var downTimeError     = 'downTimeError';
         }else{
             var scheduleMonday    = 'scheduleMonday' + filialId;
             var scheduleTuesday   = 'scheduleTuesday' + filialId;
             var scheduleWednesday = 'scheduleWednesday' + filialId;
             var scheduleThursday  = 'scheduleThursday' + filialId;
             var scheduleFriday    = 'scheduleFriday' + filialId;
             var scheduleSaturday  = 'scheduleSaturday' + filialId;
             var scheduleSunday    = 'scheduleSunday' + filialId;
             var workday           = 'workday' + filialId;
             var schedule          = 'schedule' + filialId;
             var downTimeError     = 'downTimeError' + filialId;
         }
         this.clearTimeError(scheduleMonday);
         this.clearTimeError(scheduleTuesday);
         this.clearTimeError(scheduleWednesday);
         this.clearTimeError(scheduleThursday);
         this.clearTimeError(scheduleFriday);
         this.clearTimeError(scheduleSaturday);
         this.clearTimeError(scheduleSunday);
         this.clearTimeError(workday);
         $(downTimeError).hide();
         if($F(workday) == 'on'){
             if(!this.validateWorkTime(workday)){
                 error = true;
             }
         }else if($F(schedule) == 'on'){
             if($F(scheduleMonday) == 'on' && !this.validateWorkTime(scheduleMonday)){
                 error = true;
             }
             if($F(scheduleTuesday) == 'on' && !this.validateWorkTime(scheduleTuesday)){
                 error = true;
             }
             if($F(scheduleWednesday) == 'on' && !this.validateWorkTime(scheduleWednesday)){
                 error = true;
             }
             if($F(scheduleThursday) == 'on' && !this.validateWorkTime(scheduleThursday)){
                 error = true;
             }
             if($F(scheduleFriday) == 'on' && !this.validateWorkTime(scheduleFriday)){
                 error = true;
             }
             if($F(scheduleSaturday) == 'on' && !this.validateWorkTime(scheduleSaturday)){
                 error = true;
             }
             if($F(scheduleSunday) == 'on' && !this.validateWorkTime(scheduleSunday)){
                 error = true;
             }
             if($F(scheduleMonday) != 'on' && $F(scheduleTuesday) != 'on' &&
                $F(scheduleWednesday) != 'on' && $F(scheduleThursday) != 'on' &&
                $F(scheduleFriday) != 'on' && $F(scheduleSaturday) != 'on' &&
                $F(scheduleSunday) != 'on'){
                $(downTimeError).show();
                error = true;
             }
         }
         if(error){
             return false;
         }else{
             return true;
         }
    },
    validateWorkTime: function(id){
        var errorTime = false;
        if($F(id+'HourAt') == -1 || $F(id+'HourTo') == -1 || 
           $F(id+'MinAt') == -1 || $F(id+'MinTo') == -1){
            this.showTimeError(id);
            errorTime = true;
        }
        if(errorTime){
            return false;
        }else{
            return true;
        }
    },
    clearErrorWorkTime: function(id){
        this.clearTimeError(id+'At');
        this.clearTimeError(id+'To');
        this.clearTimeError(id+'DinnerAt');
        this.clearTimeError(id+'DinnerTo');
    },
    getDataDownTime: function(filialId){
        if(filialId == null){
            var scheduleMonday    = 'scheduleMonday';
            var scheduleTuesday   = 'scheduleTuesday';
            var scheduleWednesday = 'scheduleWednesday';
            var scheduleThursday  = 'scheduleThursday';
            var scheduleFriday    = 'scheduleFriday';
            var scheduleSaturday  = 'scheduleSaturday';
            var scheduleSunday    = 'scheduleSunday';
            var workday           = 'workday';
            var schedule          = 'schedule';
            var roundTheClock     = 'roundTheClock';
        }else{
            var scheduleMonday    = 'scheduleMonday' + filialId;
            var scheduleTuesday   = 'scheduleTuesday' + filialId;
            var scheduleWednesday = 'scheduleWednesday' + filialId;
            var scheduleThursday  = 'scheduleThursday' + filialId;
            var scheduleFriday    = 'scheduleFriday' + filialId;
            var scheduleSaturday  = 'scheduleSaturday' + filialId;
            var scheduleSunday    = 'scheduleSunday' + filialId;
            var workday           = 'workday' + filialId;
            var schedule          = 'schedule' + filialId;
            var roundTheClock     = 'roundTheClock' + filialId;
        }
        var objDownTime  = new Object();
        if($F(roundTheClock) == 'on'){
           objDownTime['dow'] = 'roundTheClock';
        }else if($F(workday) == 'on'){
           /*objDownTime['dow'] = 'workday';
           objDownTime['start_time'] = $F(workday + 'At');
           objDownTime['end_time'] = $F(workday + 'To');
           objDownTime['start_dinner_time'] = $F(workday + 'DinnerAt');
           objDownTime['end_dinner_time'] = $F(workday + 'DinnerTo');*/
           objDownTime = this.getDownTimeOneDay(workday);
           objDownTime['dow'] = 'workday';
        }else if($F(schedule) == 'on'){
           objDownTime['dow'] = 'schedule';
           if($F(scheduleMonday) == "on"){
               objDownTime['mo'] = this.getDownTimeOneDay(scheduleMonday);
           }
           if($F(scheduleTuesday) == "on"){
               objDownTime['tu'] = this.getDownTimeOneDay(scheduleTuesday);
           }
           if($F(scheduleWednesday) == "on"){
               objDownTime['we'] = this.getDownTimeOneDay(scheduleWednesday);
           }
           if($F(scheduleThursday) == "on"){
               objDownTime['th'] = this.getDownTimeOneDay(scheduleThursday);
           }
           if($F(scheduleFriday) == "on"){
               objDownTime['ft'] = this.getDownTimeOneDay(scheduleFriday);
           }
           if($F(scheduleSaturday) == "on"){
               objDownTime['sa'] = this.getDownTimeOneDay(scheduleSaturday);
           }
           if($F(scheduleSunday) == "on"){
               objDownTime['su'] = this.getDownTimeOneDay(scheduleSunday);
           }
        }
        return objDownTime;
    },
    getDownTimeOneDay: function(elementId){
        var objSchedule = new Object();
        var hourAt = $F(elementId + 'HourAt');
        var minAt  = $F(elementId + 'MinAt');
        var hourTo = $F(elementId + 'HourTo');
        var minTo  = $F(elementId + 'MinTo');
        var hourDinnerAt = $F(elementId + 'HourDinnerAt');
        var minDinnerAt  = $F(elementId + 'MinDinnerAt');
        var hourDinnerTo = $F(elementId + 'HourDinnerTo');
        var minDinnerTo  = $F(elementId + 'MinDinnerTo');
        if(hourAt.length == 1){
             var hourAt = '0' + hourAt;
        }
        if(hourTo.length == 1){
             var hourTo = '0' + hourTo;
        }
        if(minAt.length == 1){
             var minAt = '0' + minAt;
        }
        if(minTo.length == 1){
             var minTo = '0' + minTo;
        }
        if(hourDinnerAt.length == 1){
             var hourDinnerAt = '0' + hourDinnerAt;
        }
        if(hourDinnerTo.length == 1){
             var hourDinnerTo = '0' + hourDinnerTo;
        }
        if(minDinnerAt.length == 1){
             var minDinnerAt = '0' + minDinnerAt;
        }
        if(minDinnerTo.length == 1){
             var minDinnerTo = '0' + minDinnerTo;
        }
        objSchedule['start_time'] = hourAt + ':' + minAt;
        objSchedule['end_time'] = hourTo + ':' + minTo;
        if($F(elementId + 'HourDinnerAt') != -1){
            objSchedule['start_dinner_time'] = hourDinnerAt + ':' + minDinnerAt;
        }else{
            objSchedule['start_dinner_time'] = "";
        }
        if($F(elementId + 'HourDinnerTo') != -1){
            objSchedule['end_dinner_time'] = hourDinnerTo + ':' + minDinnerTo;
        }else{
            objSchedule['end_dinner_time'] = "";
        }
        return objSchedule;
    },
    showError: function(id){
            if(this.editionType == 'gts'){
                $(id).addClassName('highlight');
                $(id + 'Error').removeClassName('hidden');
            }else{ 
                if($(id).hasClassName('long_field')){
                    $(id).removeClassName('long_field');
                    $(id).addClassName('long_field_rb');
                }else if($(id).hasClassName('phone')){
                    $(id).removeClassName('phone');
                    $(id).addClassName('phone_rb');
                }else if($(id).hasClassName('kod')){
                    $(id).removeClassName('kod');
                    $(id).addClassName('kod_rb');
                }
            }
            $(id + 'Error').show();
    },
    showValidateError: function(fieldId, errorId){
        if(this.editionType == 'gts'){
            $(fieldId).addClassName('highlight');
            $(errorId).removeClassName('hidden');
        }else{
            if($(fieldId).hasClassName('long_field')){
                $(fieldId).removeClassName('long_field');
                $(fieldId).addClassName('long_field_rb');
            }else if($(fieldId).hasClassName('site')){
                $(fieldId).removeClassName('site');
                $(fieldId).addClassName('site_rb');
            }else if($(fieldId).hasClassName('phone')){
                $(fieldId).removeClassName('phone');
                $(fieldId).addClassName('phone_rb');
            }
        }
        $(errorId).show();
    },
    clearValidateError: function(fieldId, errorId){
        $(errorId).hide();
        if(this.editionType == 'gts'){
            $(fieldId).removeClassName('highlight');
            $(errorId).addClassName('hidden');
        }else{
            if($(fieldId).hasClassName('long_field_rb')){
                $(fieldId).removeClassName('long_field_rb');
                $(fieldId).addClassName('long_field');
            }else if($(fieldId).hasClassName('site_rb')){
                $(fieldId).removeClassName('site_rb');
                $(fieldId).addClassName('site');
            }else if($(fieldId).hasClassName('phone_rb')){
                $(fieldId).removeClassName('phone_rb');
                $(fieldId).addClassName('phone');
            }
        }
    },
    clearError: function(id){
        $(id + 'Error').hide();
        if(this.editionType == 'gts'){
            $(id).removeClassName('highlight');
            $(id + 'Error').addClassName('hidden');
        }else{
            if($(id).hasClassName('long_field_rb')){
                $(id).removeClassName('long_field_rb');
                $(id).addClassName('long_field');
            }else if($(id).hasClassName('phone_rb')){
                $(id).removeClassName('phone_rb');
                $(id).addClassName('phone');
            }else if($(id).hasClassName('kod_rb')){
                $(id).removeClassName('kod_rb');
                $(id).addClassName('kod');
            }
        }
    },
    showTimeError: function(id){
        $(id + 'Error').show();
        /*if($(id).hasClassName('kod')){
            $(id).removeClassName('kod');
            $(id).addClassName('kod_rb');
        }*/
    },
    clearTimeError: function(id){
        $(id + 'Error').hide();
        /*if($(id).hasClassName('kod_rb')){
            $(id).removeClassName('kod_rb');
            $(id).addClassName('kod');
        }*/
    },
    showUploadError: function(id){
        if(this.editionType == 'gts'){
            $(id).removeClassName('hidden');
        }else{
            $(id).show();
        }
    },
    clearUploadError: function(id){
        if(this.editionType == 'gts'){
            $(id).addClassName('hidden');
        }else{
            $(id).hide();
        }
    },
    addFilial: function(){
        if(!this.openFilialForm){
            this.openFilialForm = true;
            this.currentFilialNumber += 1;
            $('newFilial').show();
            this.inizializeCompanyTab();
            this.changeDisplayButtonAddFilial();
        }else{
            alert('Форма добавления/редактирования филиала уже открыта!');
        }
    },
    saveDataFilial: function(action, filialId){
        if(action == 'editfilial'){
            var filialName     = 'filialName' + filialId;
            //var filialRegion   = 'filialRegion' + filialId;
            var filialCity     = 'filialCity' + filialId;
            var filialAddress  = 'filialAddress' + filialId;
            var phoneMain      = 'phoneMain' + filialId;
            var phoneCodeMain  = 'phoneCodeMain' + filialId;
            var faxCode        = 'faxCode' + filialId;
            var fax            = 'fax' + filialId;
            var filialPostCode = 'filialPostCode' + filialId;
            var phoneAdditionalOne     = 'phoneAdditionalOne' + filialId;
            var phoneCodeAdditionalOne = 'phoneCodeAdditionalOne' + filialId;
            var phoneAdditionalTwo     = 'phoneAdditionalTwo' + filialId;
            var phoneCodeAdditionalTwo = 'phoneCodeAdditionalTwo' + filialId;
            var filialShowAddress      = 'filialShowAddress' + filialId;
        }else{
            var filialName     = 'filialName';
            //var filialRegion   = 'filialRegion';
            var filialCity     = 'filialCity';
            var filialAddress  = 'filialAddress';
            var phoneMain      = 'phoneMain';
            var phoneCodeMain  = 'phoneCodeMain';
            var faxCode        = 'faxCode';
            var fax            = 'fax';
            var filialPostCode = 'filialPostCode';
            var phoneAdditionalOne     = 'phoneAdditionalOne';
            var phoneCodeAdditionalOne = 'phoneCodeAdditionalOne';
            var phoneAdditionalTwo     = 'phoneAdditionalTwo';
            var phoneCodeAdditionalTwo = 'phoneCodeAdditionalTwo';
            var filialShowAddress      = 'filialShowAddress';
        }
        
        var error = false;
        this.clearError(filialName);
        //this.clearError(filialRegion);
        this.clearError(filialCity);
        this.clearError(filialAddress);
        this.clearError(phoneMain);
        this.clearError(phoneCodeMain);
        this.clearError(faxCode);
        this.clearError(fax);
        this.clearError(phoneAdditionalOne);
        this.clearError(phoneCodeAdditionalOne);
        this.clearError(phoneAdditionalTwo);
        this.clearError(phoneCodeAdditionalTwo);
        this.clearValidateError(phoneMain, phoneMain + 'NotValid');
        this.clearValidateError(filialPostCode, filialPostCode + 'NotValid');

        if(Filter.trim($(filialName).getValue()) == ""){
           error = true;
           this.showError(filialName);
        }
        if(Filter.trim($(filialCity).getValue()) == ""){
           error = true;
           this.showError(filialCity);
        }
        if(Filter.trim($(filialAddress).getValue()) == ""){
           error = true;
           this.showError(filialAddress);
        }
        if(Filter.trim($(phoneMain).getValue()) == ""){
           error = true;
           this.showError(phoneMain);
        }else if(Filter.trim($F(phoneMain)) != "" &&
                 !this.regPhone.test($F(phoneMain))){
            error = true;
            this.showValidateError(phoneMain, phoneMain + 'NotValid');
        }else if(Filter.trim($F(phoneCodeMain)) != "" &&
                 !this.regPhoneCode.test($F(phoneCodeMain))){
            error = true;
            this.showError(phoneCodeMain);
        }
        
        if(Filter.trim($F(phoneAdditionalOne)) != "" &&
           !this.regPhone.test($F(phoneAdditionalOne))){
           error = true;
           this.showError(phoneAdditionalOne);
        }else if(Filter.trim($F(phoneAdditionalOne)) != "" &&
                 Filter.trim($F(phoneCodeAdditionalOne)) != "" &&
                 !this.regPhoneCode.test($F(phoneCodeAdditionalOne))){
           error = true;
           this.showError(phoneCodeAdditionalOne);
        }
        if(Filter.trim($F(phoneAdditionalTwo)) != "" &&
           !this.regPhone.test($F(phoneAdditionalTwo))){
           error = true;
           this.showError(phoneAdditionalTwo);
        }else if(Filter.trim($F(phoneAdditionalTwo)) != "" &&
                 Filter.trim($F(phoneCodeAdditionalTwo)) != "" &&
                 !this.regPhoneCode.test($F(phoneCodeAdditionalTwo))){
           error = true;
           this.showError(phoneCodeAdditionalTwo);
        }
        if(Filter.trim($F(fax)) != "" &&
           !this.regPhone.test($F(fax))){
           error = true;
           this.showError(fax);
        }else if(Filter.trim($F(fax)) != "" &&
                 Filter.trim($F(faxCode)) != "" &&
                 !this.regPhoneCode.test($F(faxCode))){
           error = true;
           this.showError(faxCode);
        }
        if(Filter.trim($(filialPostCode).getValue()) != "" &&
           !Validate.digits($(filialPostCode).getValue()))
        {
            error = true;
            this.showValidateError(filialPostCode, filialPostCode + 'NotValid');
        }
        /*if($F(filialRegion) == 0){
           error = true;
           this.showError(filialRegion);
        }*/
        if(action == 'editfilial'){
            if(!this.verificationDataDownTime(filialId)){
                error = true;
            }
        }else{
            if(!this.verificationDataDownTime()){
                error = true;
            }
        }
        if(error){
           return null;
        }else{
           var objArgs = new Object();
           objArgs['filialName'] = $(filialName).getValue();
           objArgs['filialCity'] = $(filialCity).getValue();
           objArgs['filialRegion'] = $F('region');
           objArgs['filialAddress'] = $(filialAddress).getValue();
           objArgs['filialPostCode'] = $(filialPostCode).getValue();
           objArgs['phoneMain'] = $(phoneMain).getValue();
           objArgs['phoneCodeMain'] = $(phoneCodeMain).getValue();
           objArgs['phoneAdditionalOne'] = $(phoneAdditionalOne).getValue();
           objArgs['phoneCodeAdditionalOne'] = $(phoneCodeAdditionalOne).getValue();
           objArgs['phoneAdditionalTwo'] = $(phoneAdditionalTwo).getValue();
           objArgs['phoneCodeAdditionalTwo'] = $(phoneCodeAdditionalTwo).getValue();
           objArgs['fax'] = $(fax).getValue();
           objArgs['faxCode'] = $(faxCode).getValue();
           objArgs['filialShowAddress'] = $(filialShowAddress).getValue();
           if(action == 'editfilial'){
               objArgs['filialDiv'] = $('filialId').getValue();
               objArgs['downTime'] = this.getDataDownTime(filialId);
           }else{
               objArgs['downTime'] = this.getDataDownTime();
               objArgs['filialDiv'] = 'filial' + this.currentFilialNumber;
           }
           objArgs['newStep'] = 'filial';
           objArgs['currentStep'] = 'filial';
           objArgs['operation'] = 'savefilial';
           //alert(Object.toJSON(objArgs));
           $('args').value = Object.toJSON(objArgs);
           document.qustionnaireForm.submit();
        }
    },
    changeDisplayButtonAddFilial: function(){
        if($('buttonAddFilial').visible()){
            $('buttonAddFilial').hide();
        }else{
            $('buttonAddFilial').show();
        }
    },
    uploadInit: function(addType)
    {
        if(this.uploadBeginFlag == 1){
            this.uploadBeginFlag = 0;

            if(addType == 'photo'){
                this.clearUploadError('uploadPhotoBigSize');
                this.clearUploadError('uploadPhotoNotUploaded');
                this.clearUploadError('uploadPhotoWrongMime');
                this.clearUploadError('uploadPhotoPartial');

                var errorUploading = Cookie.getCookie('uploadPhoto');
                Cookie.deleteCookie('uploadPhoto','/','');
                if(errorUploading){
                    switch (errorUploading) {
                        case 'BigSize' :
                            this.showUploadError('uploadPhotoBigSize');
                        break;
                        case 'Partial' :
                            this.showUploadError('uploadPhotoPartial');
                        break;
                        case 'NoFile' :
                            this.showUploadError('uploadPhotoNotUploaded');
                        break;
                        case 'WrongMime' :
                            this.showUploadError('uploadPhotoWrongMime');
                        break;
                    }
                }else{
                    fileName = Cookie.getCookie('uploadPhotoFileName');
                    fileLocalName = Cookie.getCookie('uploadPhotoLocalName');
                    var objArgs = new Object();
                    objArgs["returnDiv"] = "photoGalleryUpload";
                    objArgs["operation"] = "savephoto";
                    objArgs["fileLocalName"] = fileLocalName;
                    objArgs["fileName"] = fileName;
                    objArgs["photoComment"] = this.commentUploadPhoto;
                    this.commentUploadPhoto = null;
                    $('args').value = Object.toJSON(objArgs);
                    document.qustionnaireForm.submit();
                    //this.windowOverlay('on');
                    //makeCall('TrafficDB/showQuestionnaire', objArgs);
                }
            }else if(addType == 'logo'){
                this.clearUploadError('uploadLogoBigSize');
                this.clearUploadError('uploadLogoPartial');
                this.clearUploadError('uploadLogoNotUploaded');
                this.clearUploadError('uploadLogoWrongMime');

                var errorUploading = Cookie.getCookie('uploadLogo');
                Cookie.deleteCookie('uploadLogo','/','');
                if(errorUploading){
                    switch (errorUploading) {
                        case 'BigSize' :
                            this.showUploadError('uploadLogoBigSize');
                        break;
                        case 'Partial' :
                            this.showUploadError('uploadLogoPartial');
                        break;
                        case 'NoFile' :
                            this.showUploadError('uploadLogoNotUploaded');
                        break;
                        case 'WrongMime' :
                            this.showUploadError('uploadLogoWrongMime');
                        break;
                    }
                }else{
                    fileName = Cookie.getCookie('uploadLogoFileName');
                    fileLocalName = Cookie.getCookie('uploadLogoLocalName');
                    var objArgs = new Object();
                    objArgs["returnDiv"] = "dataUploadLogo";
                    objArgs["operation"] = "savelogo";
                    objArgs["fileLocalName"] = fileLocalName;
                    objArgs["fileName"] = fileName;
                    //this.windowOverlay('on');
                    //makeCall('TrafficDB/showQuestionnaire', objArgs);
                    $('args').value = Object.toJSON(objArgs);
                    document.qustionnaireForm.submit();
                }
            }else if(addType == 'price'){
                this.clearUploadError('uploadPriceBigSize');
                this.clearUploadError('uploadPricePartial');
                this.clearUploadError('uploadPriceNotUploaded');
                this.clearUploadError('uploadPriceWrongMime');

                var errorUploading = Cookie.getCookie('uploadPrice');
                Cookie.deleteCookie('uploadPrice','/','');
                if(errorUploading){
                    switch (errorUploading) {
                        case 'BigSize' :
                            this.showUploadError('uploadPriceBigSize');
                        break;
                        case 'Partial' :
                            this.showUploadError('uploadPricePartial');
                        break;
                        case 'NoFile' :
                            this.showUploadError('uploadPriceNotUploaded');
                        break;
                        case 'WrongMime' :
                            this.showUploadError('uploadPriceWrongMime');
                        break;
                    }
                }else{
                    fileName = Cookie.getCookie('uploadPriceFileName');
                    fileLocalName = Cookie.getCookie('uploadPriceLocalName');
                    var objArgs = new Object();
                    objArgs["returnDiv"] = "dataUploadPrice";
                    objArgs["operation"] = "saveprice";
                    objArgs["fileLocalName"] = fileLocalName;
                    objArgs["fileName"] = fileName;
                    //this.windowOverlay('on');
                    //makeCall('TrafficDB/showQuestionnaire', objArgs);
                    $('args').value = Object.toJSON(objArgs);
                    document.qustionnaireForm.submit();
                }
            }
        }
    },
    uploadPhoto: function()
    {
        if(Filter.trim($F('uploadPhoto')) == ""){
            this.showUploadError('uploadPhotoNoFile');
        }else{
            this.uploadOperationFlag = 'savephoto';
            this.openTab('photo');
        }
    },
    uploadLogo: function()
    {
        if(Filter.trim($F('uploadLogo')) == ""){
            this.showUploadError('uploadLogoNoFile');
        }else{
            this.uploadOperationFlag = 'savelogo';
            this.openTab('photo');
        }
    },
    uploadPrice: function()
    {
        if($F('uploadPrice').strip() == ""){
            this.showUploadError('uploadPriceNoFile');
        }else{
            this.uploadOperationFlag = 'saveprice';
            this.openTab('services');
        }
    },
    deleteUploadPhoto: function(fileName)
    {
        this.uploadDeleteFileName = fileName;
        this.uploadOperationFlag = 'deletephoto';
        this.openTab('photo');
    },
    deleteUploadLogo: function()
    {
        this.uploadOperationFlag = 'deletelogo';
        this.openTab('photo');
    },
    deleteUploadPrice: function()
    {
        this.uploadOperationFlag = 'deleteprice';
        this.openTab('services');
    },
    showStatusUploadLogo: function(){
        if(this.editionType == 'gts'){
             $('statusUploadLogoNumber').removeClassName('hidden');
             $('statusUploadLogoData').removeClassName('hidden');
        }else{
             $('statusUploadLogoData').show();
        }     
        $('uploadLogo').setValue("");
        $('uploadLogo').disable();
        $('uploadLogoSave').disable();
    },
    hiddenUploadLogo: function(){
        if(this.editionType == 'gts'){
             $('statusUploadLogoNumber').addClassName('hidden');
             $('statusUploadLogoData').addClassName('hidden');
        }else{
            $('statusUploadLogoData').hide();
        }
        $('uploadLogo').setValue("");
        $('uploadLogo').enable();
        $('uploadLogoSave').enable();
    },
    showStatusUploadPrice: function(){
        if(this.editionType == 'gts'){
             $('statusUploadPriceNumder').removeClassName('hidden');
             $('statusUploadPriceData').removeClassName('hidden');
        }else{
             $('statusUploadPriceData').show();
        }
        $('uploadPrice').setValue("");
        $('uploadPrice').disable();
        $('uploadPriceSave').disable();
    },
    hiddenUploadPrice: function(){
        if(this.editionType == 'gts'){
             $('statusUploadPriceNumder').removeClassName('hidden');
             $('statusUploadPriceData').removeClassName('hidden');
        }else{
            $('statusUploadPriceData').hide();
        }
        $('uploadPrice').setValue("");
        $('uploadPrice').enable();
        $('uploadPriceSave').enable();
    },
    setCountFilial: function(count){
        this.currentFilialNumber = parseInt(count);
    },
    deleteFilial: function(filialId){
        if(confirm("Вы действительно хотите удалить филиал?")){
            objArgs = new Object();
            objArgs['newStep'] = 'filial';
            objArgs['currentStep'] = 'filial';
            objArgs['operation'] = 'deletefilial';
            objArgs["filialDiv"] = filialId;
            $('args').value = Object.toJSON(objArgs);
            document.qustionnaireForm.submit();
        }
    },
    editFilial: function(filialId){
        if(!this.openFilialForm){
            this.openFilialForm = true;
            this.inizializeFilialTab(filialId);
            this.changeDisplayButtonAddFilial();
            $('minimize_' + filialId).hide();
            $('maximize_' + filialId).show();
        }else{
            alert('Форма добавления/редактирования филиала уже открыта!');
        }
    },
    deleteFormFilial: function(){
        //$('filial' + this.currentFilialNumber).remove();
        this.openFilialForm = false;
        $('newFilial').hide();
        this.changeDisplayButtonAddFilial();
    },
    errorTabData: function(tabName){
        if(tabName == "contact"){
            this.getDataContactTab();
        }else if(tabName == "company"){
            this.getDataCompanyTab();
        }else if(tabName == "services"){
           this.getDataServicesTab();
        }else if(tabName == "sender"){
           this.getDataSenderTab();
        }
    },
    changeDisplayOtherPayment: function(){
        this.clearError('paymentOther');
        if($F('checkboxPaymentOther') == 7){
            if(this.editionType == 'gts'){
                $('fieldPaymentOther').removeClassName('hidden');
            }else{
                $('fieldPaymentOther').show();
            }
        }else{
            if(this.editionType == 'gts'){
                $('fieldPaymentOther').addClassName('hidden');
            }else{
                $('fieldPaymentOther').hide();
            }
        }
    },
    windowOverlay: function(status)
    {
        var window_overlay = $('window_overlay')
        if(null != window_overlay) {
            if('on' === status) {
                new Effect.Appear('window_overlay', {duration: 0.5, from: 0.0 , to: 0.2});
                window_overlay.style.height = Element.getDimensions(document.body).height+'px';
                window_overlay.style.width = Element.getDimensions(document.body).width+'px'
                //Event.observe('window_overlay', 'click', function() { Xms.windowOverlay('off') } );
                return true;
            } else if('off' === status) {
                //Event.stopObserving('window_overlay', 'click', function() { Xms.windowOverlay('off') } );
                new Effect.Fade('window_overlay', {duration: 0.5});
                return true;
            } else {
                alert('Unknow status');
                return false;
            }
        } else {
            alert('No overlay div found. Check your code please.');
            return false;
        }
    },
    initializeQuestionnaireFree: function()
    {
        if ($('cat_cont').hasClassName('m_anc_istr')) {
            $('cat_cont').removeClassName('m_anc_istr');
            $('cat_cont').addClassName('m_anc_besp');
        }
    },
    freeQuestionnaireGoToStep: function(step)
    {
        var error = false;
        objArgs = new Object();
        switch ($F('currentStep')) {
            case 'aboutCompany' :
                if (this.freeQuestionnaireValidateAboutCompany()) {
                    objArgs = this.freeQuestionnaireGetTabAboutCompany(objArgs);
                } else {
                    error = true;
                }
            break;
            case 'contacts' :
                if (this.freeQuestionnaireValidateContacts()) {
                    objArgs = this.freeQuestionnaireGetTabContacts(objArgs);
                } else {
                    error = true;
                }
            break;
            case 'sender' :
                if (this.freeQuestionnaireValidateSender()) {
                    objArgs = this.freeQuestionnaireGetTabSender(objArgs);
                } else {
                    error = true;
                }
            break;
       }

       if (!error) {
           this.windowOverlay('on');
           objArgs['step'] = step;
           objArgs['currentStep'] = $F('currentStep');
           makeCall('TrafficDB/showQuestionnaireOrganization', objArgs);
       }
    },
    freeQuestionnaireValidateAboutCompany: function()
    {
        var isValid = true;
        /* Clear errors */
        if ($('juristicPerson').hasClassName('long_field_rb')) {
            $('juristicPerson').removeClassName('long_field_rb');
            $('juristicPerson').addClassName('long_field');
        }
        $('juristicPersonIsEmptyError').hide();

        if ($('headName').hasClassName('long_field_rb')) {
            $('headName').removeClassName('long_field_rb');
            $('headName').addClassName('long_field');
        }
        $('headNameIsEmptyError').hide();

        $('aboutComanyIsEmptyError').hide();
        $('aboutComanyMaxlengthError').hide();

        $('firmServiceIsEmptyError').hide();
        $('firmServiceMaxlengthError').hide();

        $('companyBrandsMaxlengthError').hide();

        /* Validate */
        if ($F('juristicPerson').strip().length == 0) {
            isValid = false;
            $('juristicPersonIsEmptyError').show();
            $('juristicPerson').removeClassName('long_field');
            $('juristicPerson').addClassName('long_field_rb');
        }

        if ($F('headName').strip().length == 0) {
            isValid = false;
            $('headNameIsEmptyError').show();
            $('headName').removeClassName('long_field');
            $('headName').addClassName('long_field_rb');
        }

        if ($F('aboutComany').strip().length == 0) {
            isValid = false;
            $('aboutComanyIsEmptyError').show();
        } else if ($F('aboutComany').strip().length > 1000) {
            isValid = false;
            $('aboutComanyMaxlengthError').show();
        }

        if ($F('firmService').strip().length == 0) {
            isValid = false;
            $('firmServiceIsEmptyError').show();
        } else if ($F('firmService').strip().length > 700) {
            isValid = false;
            $('firmServiceMaxlengthError').show();
        }

        if ($F('companyBrands').strip().length > 200) {
            isValid = false;
            $('companyBrandsMaxlengthError').show();
        }

        return isValid;
    },
    freeQuestionnaireGetTabAboutCompany: function(objArgs)
    {
        objArgs['companyName'] = Filter.encodeURIComponent($F('companyName').strip());
        objArgs['juristicPerson'] = Filter.encodeURIComponent($F('juristicPerson').strip());
        objArgs['headName'] = Filter.encodeURIComponent($F('headName').strip());
        objArgs['headPost'] = Filter.encodeURIComponent($F('headPost').strip());
        objArgs['aboutComany'] = Filter.encodeURIComponent($F('aboutComany').strip());
        objArgs['firmService'] = Filter.encodeURIComponent($F('firmService').strip());
        objArgs['companyBrands'] = Filter.encodeURIComponent($F('companyBrands').strip());
        return objArgs;
    },
    freeQuestionnaireValidateContacts: function()
    {
        var isValid = true;

        /* Clear errors */
         $('phoneisEmptyError').hide();
         $('faxisEmptyError').hide();
         $('siteURLError').hide();
       //  $('siteUrl').removeClassName('long_field_rb');
         $('emailIsEmptyError').hide();
         $('emailIsNotValidError').hide();
        // $('email').removeClassName('long_field_rb');
         $('indexPostReality').removeClassName('border_red');
         //$('regionReality').removeClassName('border_red');
         this.clearError('regionReality');
         $('cityReality').removeClassName('long_field_rb');
         $('addressReality').removeClassName('long_field_rb');

         $('indexPostPostal').removeClassName('border_red');
         //$('regionPostal').removeClassName('border_red');
         this.clearError('regionPostal');
         $('cityPostal').removeClassName('long_field_rb');
         $('addressPostal').removeClassName('long_field_rb');

         $('indexPostJuristic').removeClassName('border_red');
         //$('regionJuristic').removeClassName('border_red');
         this.clearError('regionJuristic');
         $('cityJuristic').removeClassName('long_field_rb');
         $('addressJuristic').removeClassName('long_field_rb');

        /* Validate */
        if(!this.freeQuestionnairePhoneValidate('phoneCode1','phone1', true)) {
            isValid = false;
            $('phoneisEmptyError').show();
        }
        if(!this.freeQuestionnairePhoneValidate('phoneCode2','phone2')) {
            isValid = false;
            $('phoneisEmptyError').show();
        }
        if(!this.freeQuestionnairePhoneValidate('phoneCode3','phone3')) {
            isValid = false;
            $('phoneisEmptyError').show();
        }
        if(!this.freeQuestionnairePhoneValidate('phoneCode4','phone4')) {
            isValid = false;
            $('phoneisEmptyError').show();
        }
        if(!this.freeQuestionnairePhoneValidate('phoneCode5','phone5')) {
            isValid = false;
            $('phoneisEmptyError').show();
        }

        if(!this.freeQuestionnairePhoneValidate('faxCode','fax')) {
            isValid = false;
            $('faxisEmptyError').show();
        }

        if ($F('siteUrl').strip().length > 0 && !Validate.url('http://' + $F('siteUrl'))) {
             isValid = false;
           //  $('siteUrl').addClassName('long_field_rb');
             $('siteURLError').show();
        }

        if ($F('email').strip().length == 0) {
            isValid = false;
            $('emailIsEmptyError').show();
            //$('email').addClassName('long_field2_rb');
        }
        if ($F('email').strip().length > 0 && !Validate.email($F('email').strip())) {
            isValid = false;
            //$('email').addClassName('long_field_rb');
            $('emailIsNotValidError').show();
        }
        /* address reality */
        if (!Validate.digits($F('indexPostReality').strip())) {
            isValid = false;
            $('indexPostReality').addClassName('border_red');
        }

        if ($F('regionReality') == 0) {
            //$('regionReality').addClassName('border_red');
            this.showError('regionReality');
            isValid = false;
        }

        if ($F('cityReality').strip().length == 0) {
            isValid = false;
            $('cityReality').addClassName('long_field_rb');
        }

        if ($F('addressReality').strip().length == 0) {
            isValid = false;
            $('addressReality').addClassName('long_field_rb');
        }
        /* address postal */
        if ($F('indexPostPostal').strip().length == 0 || !Validate.digits($F('indexPostPostal').strip())) {
            isValid = false;
            $('indexPostPostal').addClassName('border_red');
        }

        if ($F('regionPostal') == 0) {
            //$('regionPostal').addClassName('border_red');
            this.showError('regionPostal');
            isValid = false;
        }

        if ($F('cityPostal').strip().length == 0) {
            isValid = false;
            $('cityPostal').addClassName('long_field_rb');
        }

        if ($F('addressPostal').strip().length == 0) {
            isValid = false;
            $('addressPostal').addClassName('long_field_rb');
        }

        /* address juristic */
        if ($F('indexPostJuristic').strip().length == 0 || !Validate.digits($F('indexPostJuristic').strip())) {
            isValid = false;
            $('indexPostJuristic').addClassName('border_red');
        }

        if ($F('regionJuristic') == 0) {
            //$('regionJuristic').addClassName('border_red');
            this.showError('regionJuristic');
            isValid = false;
        }

        if ($F('cityJuristic').strip().length == 0) {
            isValid = false;
            $('cityJuristic').addClassName('long_field_rb');
        }

        if ($F('addressJuristic').strip().length == 0) {
            isValid = false;
            $('addressJuristic').addClassName('long_field_rb');
        }
        return isValid;
    },
    freeQuestionnaireGetTabContacts: function(objArgs)
    {
        objArgs['phoneCode1'] = Filter.encodeURIComponent($F('phoneCode1').strip());
        objArgs['phoneCode2'] = Filter.encodeURIComponent($F('phoneCode2').strip());
        objArgs['phoneCode3'] = Filter.encodeURIComponent($F('phoneCode3').strip());
        objArgs['phoneCode4'] = Filter.encodeURIComponent($F('phoneCode4').strip());
        objArgs['phoneCode5'] = Filter.encodeURIComponent($F('phoneCode5').strip());
        objArgs['faxCode']    = Filter.encodeURIComponent($F('faxCode').strip());

        objArgs['phone1'] = Filter.encodeURIComponent($F('phone1').strip());
        objArgs['phone2'] = Filter.encodeURIComponent($F('phone2').strip());
        objArgs['phone3'] = Filter.encodeURIComponent($F('phone3').strip());
        objArgs['phone4'] = Filter.encodeURIComponent($F('phone4').strip());
        objArgs['phone5'] = Filter.encodeURIComponent($F('phone5').strip());
        objArgs['fax']    = Filter.encodeURIComponent($F('fax').strip());

        objArgs['siteUrl'] = Filter.encodeURIComponent($F('siteUrl').strip());
        objArgs['email']   = Filter.encodeURIComponent($F('email').strip());

        objArgs['indexPostReality']  = Filter.encodeURIComponent($F('indexPostReality').strip());
        objArgs['regionReality']     = $F('regionReality');
        objArgs['cityReality']       = Filter.encodeURIComponent($F('cityReality').strip());
        objArgs['addressReality']    = Filter.encodeURIComponent($F('addressReality').strip());

        objArgs['indexPostPostal']   = Filter.encodeURIComponent($F('indexPostPostal').strip());
        objArgs['regionPostal']      = $F('regionPostal');
        objArgs['cityPostal']        = Filter.encodeURIComponent($F('cityPostal').strip());
        objArgs['addressPostal']     = Filter.encodeURIComponent($F('addressPostal').strip());

        objArgs['indexPostJuristic'] = Filter.encodeURIComponent($F('indexPostJuristic').strip());
        objArgs['regionJuristic']    = $F('regionJuristic');
        objArgs['cityJuristic']      = Filter.encodeURIComponent($F('cityJuristic').strip());
        objArgs['addressJuristic']   = Filter.encodeURIComponent($F('addressJuristic').strip());

        return objArgs;
    },
    freeQuestionnaireValidateSender: function()
    {
        var isValid = true;

        /* clear errors */
        $('senderName').removeClassName('long_field_rb');
        $('senderPost').removeClassName('long_field_rb');
        $('senderEmail').removeClassName('long_field_rb');
        /* validate */
        if ($F('senderName').strip().length == 0) {
           isValid = false;
           $('senderName').addClassName('long_field_rb');
        }
        if ($F('senderPost').strip().length == 0) {
           isValid = false;
           $('senderPost').addClassName('long_field_rb');
        }
        if ($F('senderEmail').strip().length == 0) {
            isValid = false;
            $('senderEmail').addClassName('long_field_rb');
        }
        if ($F('senderEmail').strip().length > 0 && !Validate.email($F('senderEmail').strip())) {
            isValid = false;
            $('senderEmail').addClassName('long_field_rb');
        }

         if(!this.freeQuestionnairePhoneValidate('senderPhoneCode','senderPhone', true)) {
            isValid = false;
        }
        return isValid;
    },
    freeQuestionnairePhoneValidate: function(codeField, phoneField, isRequire) {
        var isValid = true;
        $(phoneField).removeClassName('long_field_rb');
        $(codeField).removeClassName('long_field_rb');

        if (isRequire && $F(phoneField).strip().length == 0) {
            $(phoneField).addClassName('long_field_rb');
            isValid = false;
        }
        if ($F(phoneField).strip().length > 0 ) {
            if (!this.regTextField.test($F(phoneField).strip())) {
                $(phoneField).addClassName('long_field_rb');
                isValid = false;
            }
        }

        if (!Validate.digits($F(codeField).strip())) {
            $(codeField).addClassName('long_field_rb');
            isValid = false;
        }

        return isValid;
    },
    freeQuestionnaireGetTabSender: function(objArgs) {
        objArgs['senderName']      = Filter.encodeURIComponent($F('senderName').strip());
        objArgs['senderPost']      = Filter.encodeURIComponent($F('senderPost').strip());
        objArgs['senderEmail']     = Filter.encodeURIComponent($F('senderEmail').strip());
        objArgs['senderPhoneCode'] = Filter.encodeURIComponent($F('senderPhoneCode').strip());
        objArgs['senderPhone']     = Filter.encodeURIComponent($F('senderPhone').strip());
        return objArgs;
    },
    freeQuestionnaireShowFilialForm: function() {
        $('filialForm').show();
    },
    freeQuestionnaireSaveFilial: function(id){
        var objArgs = new Object();
        if (!id) {
            id = '';
        }

        if (this.freeQuestionnaireValidateFilialForm(id)) {
            objArgs = this.freeQuestionnaireGetFilialFormData(objArgs, id);

            this.windowOverlay('on');
            objArgs['step'] = 'filials';
            objArgs['currentStep'] = 'saveFilial';
            if (id) {
                objArgs['filialId'] = id;
            }
            makeCall('TrafficDB/showQuestionnaireOrganization', objArgs);
        }
    },
    freeQuestionnaireValidateFilialForm: function(id) {
        var isValid = true;
         if (!id) {
            id = '';
         }
        /* clear errors */
        $(id + '_filialName').removeClassName('long_field_rb');
        $(id + '_filialNameIsEmptyError').hide();
        $(id + '_filialIndexPostal').removeClassName('border_red');
        //$(id + '_filialRegionIsEmptyError').hide();
        $(id + '_filialRegion').removeClassName('border_red');
        $(id + '_filialCity').removeClassName('long_field_rb');
        $(id + '_filialAddress').removeClassName('long_field_rb');
        $(id + '_filialPhoneIsEmptyError').hide();
        /* validate */
        if($F(id + '_filialName').strip().length == 0) {
            isValid = false;
            $(id + '_filialName').addClassName('long_field_rb');
            $(id + '_filialNameIsEmptyError').show();
        }
        if (!Validate.digits($F(id + '_filialIndexPostal').strip())) {
            isValid = false;
            $(id + '_filialIndexPostal').addClassName('border_red');
        }
        if ($F(id + '_filialRegion') == 0) {
            isValid = false;
            //$(id + '_filialRegionIsEmptyError').show();
            $(id + '_filialRegion').addClassName('border_red');
        }
        if ($F(id + '_filialCity').strip().length == 0) {
            isValid = false;
            $(id + '_filialCity').addClassName('long_field_rb');
        }
        if ($F(id + '_filialAddress').strip().length == 0) {
            isValid = false;
            $(id + '_filialAddress').addClassName('long_field_rb');
        }
        if(!this.freeQuestionnairePhoneValidate(id + '_filialPhoneCode1',id + '_filialPhone1', true)) {
            isValid = false;
            $(id + '_filialPhoneIsEmptyError').show();
        }
        if(!this.freeQuestionnairePhoneValidate(id + '_filialPhoneCode2',id + '_filialPhone2')) {
            isValid = false;
            $(id + '_filialPhoneIsEmptyError').show();
        }
        if(!this.freeQuestionnairePhoneValidate(id + '_filialPhoneCode3',id + '_filialPhone3')) {
            isValid = false;
            $(id + '_filialPhoneIsEmptyError').show();
        }
        if(!this.freeQuestionnairePhoneValidate(id + '_filialFaxCode',id + '_filialFax')) {
            isValid = false;
        }

        return isValid;
    },
    freeQuestionnaireGetFilialFormData: function(objArgs, id) {
        if (!id) {
            id = '';
         }
        objArgs['filialName']        = Filter.encodeURIComponent($F(id + '_filialName').strip());
        objArgs['filialIndexPostal'] = Filter.encodeURIComponent($F(id + '_filialIndexPostal').strip());
        objArgs['filialRegion']      = $F(id + '_filialRegion');
        objArgs['filialCity']        = Filter.encodeURIComponent($F(id + '_filialCity').strip());
        objArgs['filialAddress']     = Filter.encodeURIComponent($F(id + '_filialAddress').strip());

        objArgs['filialPhoneCode1'] = Filter.encodeURIComponent($F(id + '_filialPhoneCode1').strip());
        objArgs['filialPhoneCode2'] = Filter.encodeURIComponent($F(id + '_filialPhoneCode2').strip());
        objArgs['filialPhoneCode3'] = Filter.encodeURIComponent($F(id + '_filialPhoneCode3').strip());
        objArgs['filialFaxCode']    = Filter.encodeURIComponent($F(id + '_filialFaxCode').strip());

        objArgs['filialPhone1'] = Filter.encodeURIComponent($F(id + '_filialPhone1').strip());
        objArgs['filialPhone2'] = Filter.encodeURIComponent($F(id + '_filialPhone2').strip());
        objArgs['filialPhone3'] = Filter.encodeURIComponent($F(id + '_filialPhone3').strip());
        objArgs['filialFax']    = Filter.encodeURIComponent($F(id + '_filialFax').strip());

        return objArgs;
    },
    freeQuestionnaireShowFilialDetails: function(filialId) {
        var objArgs = new Object();
        this.windowOverlay('on');
        objArgs['filialId'] = filialId;
        objArgs['step'] = 'filials';
        objArgs['currentStep'] = 'showFilialDetails';
        makeCall('TrafficDB/showQuestionnaireOrganization', objArgs);
    },
    freeQuestionnaireCloseFilialForm: function(filialId) {
        var objArgs = new Object();
        this.windowOverlay('on');
        objArgs['filialId'] = filialId;
        objArgs['step'] = 'filials';
        objArgs['currentStep'] = 'closeFilialForm';
        makeCall('TrafficDB/showQuestionnaireOrganization', objArgs);
    },
    freeQuestionnaireRemoveFilial: function(filialId) {
        if(confirm('Вы действительно хотите удалить филиал?')) {
            var objArgs = new Object();
            this.windowOverlay('on');
            objArgs['filialId'] = filialId;
            objArgs['step'] = 'filials';
            objArgs['currentStep'] = 'removeFilial';
            makeCall('TrafficDB/showQuestionnaireOrganization', objArgs);
        }
    },
    freeQuestionnaireSend: function() {
        var objArgs = new Object();
        if (this.freeQuestionnaireValidateSender()) {
            objArgs = this.freeQuestionnaireGetTabSender(objArgs);
             objArgs['currentStep'] = 'sendData';
             makeCall('TrafficDB/showQuestionnaireOrganization', objArgs);
        }
    },
    getAnonsHelp: function() {
        parametrs = new Object();
        parametrs['width']  = '601';
        parametrs['height'] = '616';
        parametrs['left']   = (screen.width - 601)/2;
        parametrs['top']    = (screen.height - 616)/2;
        Windows.open('/skins/gts/popup_questionnaire_tips.htm', 'Help', parametrs);
    },
    getCountRemainSymbol: function(elementId, maxLength){
        var valueField = $F(elementId);
        var currentLength = valueField.length;
        var remainLength = maxLength - currentLength;
        if(remainLength < 0){
            var text = valueField.substr(0, maxLength);
            $(elementId).setValue(text);
            alert('Текст не должен превышать ' + maxLength + ' символов. Введенный текст обрезан до ' + maxLength +' символов.');
        }else{
            $(elementId + 'SymbolCount').update(remainLength);
        }        
    }
}
