﻿
var Lamco = (function() {
    return {
        FileUpload: {
            InitializeValidators: function() {
                $('.FileUploadValidator').each(function() {
                    $(this).data('RadUploadClientId', $(this).prev().attr('id'));
                });
            },
            Validate: function(source, arguments) {
                arguments.IsValid = getRadUpload($(source).data('RadUploadClientId')).validateExtensions();
            }
        }
        ,
        AgentInGoodStanding: {
            Validate: function(source, arguments) {
                arguments.IsValid = $('[id$=cbxRealEstateGoodStanding]').attr("checked");
            }
        }
		,
        AgentLitigation: {
            Validate: function(source, arguments) {
                arguments.IsValid = $('[id$=cbxAgentLitigation]').attr("checked");
            }
        }
		,
        GuidelineAgreement: {
            Validate: function(source, arguments) {
                arguments.IsValid = $('[id$=cbxGuidelines]').attr("checked");
            }
        }
		,
        GuidelinePopupAgreement: {
            Validate: function(source, arguments) {
                var allCheckboxesChecked = true;

                $('[id$=pnlRulesGuidelines] input:checkbox').each(function() {
                    allCheckboxesChecked &= $(this).attr("checked");
                });

                arguments.IsValid = allCheckboxesChecked;
            }
        }
		,
        UserSpecialty: {
            Validate: function(source, arguments) {
                var anyChecked = false;
                var specialtyText = '';

                $('[id$=cblSpecialty] :checkbox').each(function() {
                    anyChecked |= this.checked;
                });

                specialtyText = $('[id$=txtOtherSpecialties]').val();

                specialtyText = specialtyText.replace(/^\s+|\s+$/g, '');

                anyChecked |= (specialtyText != '');

                arguments.IsValid = anyChecked;
            }
        }
        ,
        SecondaryRealEstateLicense: {
            Validate: function(source, arguments) {
                // Ensure that the user didn't partially fill out the secondary real estae
                // license section(It's not required but if the number is entered without
                // an expiration date, this is an issue)
                var secondaryLicense = $('[id$=txtSecondaryRealEstateLicenseNumber]').val().replace(/^\s+|\s+$/g, '');
					
				var secondaryLicenseExpiration = $('[id$=rdDtPckrSecondaryRealEstateLicenseExpiration]').val().replace(/^\s+|\s+$/g, '');
					
                arguments.IsValid = ((secondaryLicense == '' && secondaryLicenseExpiration == '') || (secondaryLicense != '' && secondaryLicenseExpiration != '')) ? true : false;
            }
        }
		,
        UserRegions: {
            Validate: function(source, arguments) {
                var atLeastOneRegion = false;
                var serviceRegionsUnique = true;
                var serviceRegionList = [];
                var i = 0;

                $('[id*=txtRegion] :input').each(function() {

                    var serviceRegionText = $(this).val().replace(/^\s+|\s+$/g, '');
                    if (serviceRegionText != '') {
                        serviceRegionList[i] = serviceRegionText;
                        i++;
                        atLeastOneRegion |= true;
                    }
                });

                function SortNumber(a, b) {
                    return a - b
                }

                /* Check regions for uniqueness
                */
                if (atLeastOneRegion) {
                    serviceRegionList.sort(SortNumber);
                    for (index = 0; index < serviceRegionList.length; index++) {
                        if ((index + 1) < serviceRegionList.length) {
                            if (serviceRegionList[index] == serviceRegionList[index + 1]) {
                                serviceRegionsUnique = false;
                                break;
                            }
                        }
                    }
                }
                arguments.IsValid = atLeastOneRegion && serviceRegionsUnique;
            }
        },
        GarageType: {
            ShowTextBox: function(show) {
                if (show) {
                    $('[ID$=txtGarageCapacity_txtInner]').attr("disabled", false);
                    $('[ID$=lblGarageCapacity]').attr("disabled", false);
                    $('[ID$=txtGarageCapacity_txtInner]').parent().attr("disabled", false);
                    $('[ID$=hfGarageCapacityEnabled]').val(true);
                    ValidatorEnable($('[ID$=txtGarageCapacity_ctl02]')[0], true);

                }
                else {
                    // clear any previous values since it's being disabled
                    $('[ID$=txtGarageCapacity_txtInner]').val('');
                    $('[ID$=txtGarageCapacity_txtInner]').attr("disabled", true);
                    $('[ID$=lblGarageCapacity]').attr("disabled", true);
                    $('[ID$=txtGarageCapacity_txtInner]').parent().attr("disabled", true);
                    ValidatorEnable($('[ID$=txtGarageCapacity_ctl02]')[0], false);
                    $('[ID$=hfGarageCapacityEnabled]').val(false);
                }
            }
        }
        ,
        CityStateZip: {
            Validate: function(source, arguments) {
                // city, state  and zip are now combined
                // into one field so validation needs to be rewritten
                // For now it will always be success.

                var cityStateZipText = $('[ID$=txtCityStateZip]').val().trim();

                var cityCommaStateRegex = /^([A-Za-z\s]*),([A-Za-z\s]*)$/.test(cityStateZipText);
                var cityCommaStateZipRegex = /^([A-Za-z\s]*),([A-Za-z\s]*)(\d{5})\s*$/.test(cityStateZipText);
                var zipRegex = /^\s*(\d{5})\s*$/.test(cityStateZipText);
                var cityOrStateAndZipRegex = /^([A-Za-z\s]*)(\d{5})\s*$/.test(cityStateZipText);
                var cityOrStateRegex = /^([A-Za-z\s]*)$/.test(cityStateZipText);
				var emptyFieldOrDefaultFlag = false;
				
				// Is the field empty or it contains the default text?
				if(cityStateZipText == '' || cityStateZipText == 'City , State Zip')
				{
					emptyFieldOrDefaultFlag = true;
				}
        

                arguments.IsValid = (cityCommaStateRegex || cityCommaStateZipRegex || zipRegex || cityOrStateAndZipRegex || cityOrStateRegex ) && !emptyFieldOrDefaultFlag;
            }
        }
    };
})();

$(function() {
	Lamco.FileUpload.InitializeValidators();

	$('.Sortable').sortable({
		stop: function(event, ui) {
			$($(ui)[0].item)
				.parents('.Sortable')
				.find('input[id$=hfOrder]')
				.each(function(index) {
					$(this).val(index + 1);
				});
		},
		axis: 'y',
		handle: '.Anchor',
		opacity: 0.5
	});
});

