﻿var gwo = {
    //Get the values of hidden fields to determine whether the GWO test is on or off
    setOptions: function () {
        
    },
	goToProperty: function(propertyID){
		propertyURL = $('#'+propertyID).attr('href');
		window.location = propertyURL;
	},
    //Convert string values to boolean values
    stringToBoolean: function (value) {
        switch (value.toLowerCase()) {
            case "true": case "yes": case "1": return true;
            case "false": case "no": case "0": case null: return false;
            default: return Boolean(value);
        }
    },
    changeGWOVariables: function () {
		//Set the market id in the "Market Area" drop down to the correct market (GWO changes it to whatever is
		//placed in a variation, so we need to change it back to the correct market for the user)
		var marketID = $('#market_id').attr('value');
		$('#ucRegister_ddlMarkets').val(marketID);
		if($('#geo_gwo_modal_variation_on').length > 0){
			if($(".gwo-number-of-properties").length > 0){
				numberOfProperties = $('#number_of_properties').attr('value');
				$('<span class="gwo-number-of-properties">' + numberOfProperties + '</span>').replaceAll('.gwo-number-of-properties');
			}
			if($(".gwo-current-geo").length > 0){
				geoName = $('#geo_name').attr('value');
				$('<span class="gwo-current-geo">' + geoName + '</span>').replaceAll('.gwo-current-geo');
			}
		}
		// The following variables are not used within a variation for experiment 12.1.1
        /*city = $('#city_name').attr('value');
        property = $('#property_name').attr('value');
        zipcode = $('#zip_code').attr('value');
        managementcompany = $('#management_company').attr('value');
        yearbuilt = $('#year_built').attr('value');
        yearremodeled = $('#year_remodeled').attr('value');
        streetaddress = $('#street_address').attr('value');
        propertyimagesrc = $('#property_image_src').attr('value');
        propertyimagealt = $('#property_image_alt').attr('value');
        canonicalurl = $('#canonical_url').attr('value');
        latitude = $('#latitude_geo').attr('value');
        longitude = $('#longitude_geo').attr('value');
        $('<span class="gwo-city-name">' + city + '</span>').replaceAll('.gwo-city-name');
        $('<a class="property-name" href="' + canonicalurl + '">' + property + '</a>').replaceAll('.property-name');
        $('<span class="gwo-zipcode">' + zipcode + '</span>').replaceAll('.gwo-zipcode');
        $('<span class="gwo-management-company">' + managementcompany + '</span>').replaceAll('.gwo-management-company');
        $('<span class="gwo-year-built">' + yearbuilt + '</span>').replaceAll('.gwo-year-built');
        $('<span class="gwo-year-remodeled">' + yearremodeled + '</span>').replaceAll('.gwo-year-remodeled');
        $('<span class="gwo-street-address">' + streetaddress + '</span>').replaceAll('.gwo-street-address');
        $('<img class="gwo-property-image" itemprop="image" width="300" height="205" alt="' + propertyimagealt + '" src="' + propertyimagesrc + '" />').replaceAll('.gwo-property-image');
        $('<abbr class="geo" itemprop="geo" itemtype="http://schema.org/GeoCoordinates" itemscope><meta content="' + latitude + '" itemprop="latitude"><meta content="' + longitude + '" itemprop="longitude"></abbr>').replaceAll('.geo');
		*/
    }
}
$(document).ready(function () {
	if ($('#gwo_modal_variation_on').length > 0) {
		if(gwo.stringToBoolean($('#gwo_modal_variation_on').attr('value'))){
			$(".list-item-spacer").click(function(e){
				regModal.showModal("For details about this property,", "create your free account", "/Apartments/images/buttons/VIEWDETAILS_Green_Shinny.png", "View Details");
			});
			$("#full-image").click(function(e){
				regModal.showModal("To view all property photos,", "create your free account", "/Apartments/images/buttons/VIEWPHOTOS_Green_Shinny_164x32.png", "View Photos");
			});
		}
	}
	if($('#geo_gwo_modal_variation_on').length > 0){
		if(gwo.stringToBoolean($('#geo_gwo_modal_variation_on').attr('value'))){
			regModal.options.buttonID = 'ucRegister_cmdRegister';
			regModal.options.width = '30em';
			regModal.options.left = '25%';
			regModal.options.top = '20%';
			regModal.defaults.buttonsrc = $('#'+regModal.options.buttonID).attr('src');
			regModal.defaults.buttonAltText = $('#'+regModal.options.buttonID).attr('alt');
			
			//The modalID is required.
			regModal.options.modalID = 'reg-module';
			regModal.options.subheaderID = 'registration-form-subheader';
			$('#area-statistics-grid tr').click(function(){
				regModal.showModal('', '', "/Apartments/images/buttons/VIEWTHELATESTLISTINGS_Green_Shinny.png", "View the latest listings");
			});
			$('.modal-trigger').click(function(){
				regModal.showModal("", "", "/Apartments/images/buttons/VIEWTHELATESTLISTINGS_Green_Shinny.png", "View the latest listings");
			});
		}
	}

    //GWO pushes the validation script to the bottom of the body and the validation script breaks,
    //so we decided to use an alternative validation when testing.
    if ($('#gwo_variation_on').length > 0) {
		gwo.changeGWOVariables();
		gwo.setOptions();
		formid = $("form").attr("id");
		ucID = "ucRegister";
        $('#'+formid).validate({
            errorClass: "error",
            errorElement: "span",
            errorPlacement: function (error, element) {
                element.before(error);
            }
        });
		if($('#'+ucID+'_txtFirstName').length > 0){
			$('#'+ucID+'_txtFirstName').rules("add", {
				required: true,
				messages: {
					required: "Required"
				}
			});
		}
        $('#'+ucID+'_txtLastName').rules("add", {
            required: true,
            messages: {
                required: "Required"
            }
        });
			$('#'+ucID+'_txtEmail').rules("add", {
				required: true,
				email: true,
				messages: {
					required: "Required",
					email: "Example: username@domain.com"
				}
			});
			$.validator.addMethod('phone', function (value, element) {
				var result = /^[\(]?\d{3}[\)]?\w?-?\.?\d{3}\w?-?\.?\d{4}([xX]\d{1,5})?/.exec(value)
				return result && value == result[0];
			}, "Example: (###)###-####X#####");
        $.validator.addMethod('select', function (value, element) {
            return this.optional(element) || $(element).val() != 0;
        }, "Required");
        $.validator.addMethod('market', function (value, element) {
            return this.optional(element) || $(element).val() != 1;
        }, "Required");
        $('#'+ucID+'_txtPhone').rules("add", {
            required: true,
            phone: true,
            messages: {
                required: "Required"
            }
        });

       $('#'+ucID+'_ddlApartmentSize').rules("add", {
            select: true,
            messages: {
                select: "Required"
            }
        });
        $('#'+ucID+'_ddlAnticipatedMoveDates').rules("add", {
            select: true,
            messages: {
                select: "Required"
            }
        });
        $('#'+ucID+'_ddlMarkets').rules("add", {
            market: true,
            messages: {
                market: "Required"
            }
        });
		
    }
	
});


