//if (typeof (trackingVariables) == "undefined") {
//    var trackingVariables = new Array();

//    var trackingDate = new Date();
//    var trackingDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
//    var trackingDay = trackingDays[trackingDate.getDay()];
//    var trackingDayType = trackingDate.getDay() == 0 || trackingDate.getDay() == 6 || trackingDate.getDay() == 7 ? "Weekend" : "Weekday";
//}

//trackingVariables["tracking-00054"] = { pageName: "GYTIRES | RETAIL LOCATOR | RETAIL SEARCH | BUTTON | INDEX", event: "", prop1: "RETAIL LOCATOR", prop2: "RETAIL SEARCH", prop3: "RETAIL LOCATOR | RETAIL SEARCH", prop4: "BUTTON", prop5: "RETAIL SEARCH | BUTTON", prop6: "RETAIL LOCATOR | RETAIL SEARCH  | BUTTON", prop7: "INDEX", prop8: "BUTTON | INDEX", eVar9: "", prop9: "RETAIL SEARCH | BUTTON | INDEX", eVar10: "", prop10: "RETAIL LOCATOR | RETAIL SEARCH | BUTTON | INDEX", eVar11: "", prop11: "", eVar12: "", prop12: "", eVar13: "", prop13: "", prop14: "", prop15: "", prop16: "", prop17: "", prop18: "", prop19: "", prop20: "", eVar21: "<<ZIP CODE>>", prop21: "", prop22: "", prop23: "", prop24: "", prop25: "", prop26: "", prop27: "", prop28: trackingDate.toTimeString(), prop29: trackingDay, prop30: trackingDayType, prop32: "GYTIRES" };


var RetailerLocatorModule = Class.extend({

	form: null,
	cityTextField: null,
	stateTextField: null,
	zipTextField: null,
	cityTextBufferField: null,
	stateTextBufferField: null,
	zipTextBufferField: null,
	goBtn: null,

	emptyTextColor: '#666666',
	filledTextColor: '#000000',

	// Initializer.
	//
	init: function () {

		this.form = $('#rlm_retailerLocatorForm')[0];
		this.cityTextField = $('#rlm_cityField')[0];
		this.stateTextField = $('#rlm_stateField')[0];
		this.zipTextField = $('#rlm_zipField')[0];
		this.cityBufferField = $('#rlm_cityBufferField')[0];
		this.stateBufferField = $('#rlm_stateBufferField')[0];
		this.zipBufferField = $('#rlm_zipBufferField')[0];
		this.goBtn = $('#rlm_retailerLocatorGoBtn')[0];

		this.form.reset();

		var thisRLMObj = this;

		$(this.goBtn).click(function (e) {
			thisRLMObj.onSubmit();
		});

		var cookieValue = this.getCookieValue();
		if (cookieValue != null) {
			var cookieValues = cookieValue.split("|");
			this.cityTextField.value = cookieValues[0];
			if (this.cityTextField.value == "City") { this.initTextField(this.cityTextField); }

			this.stateTextField.value = cookieValues[1];
			this.zipTextField.value = cookieValues[2];
			if (this.zipTextField.value == "ZIP Code") { this.initTextField(this.zipTextField); }
		} else {
			this.initTextField(this.cityTextField);
			this.initTextField(this.zipTextField);
		}
	}, // End init().

	initTextField: function (textField) {

		textField.initLabel = textField.value;
		textField.style.color = this.emptyTextColor;

		var thisRLMObj = this;

		$(textField).focus(function (e) {
			thisRLMObj.onTextFieldFocus(e.target);
		}
	    );

		$(textField).blur(function (e) {
			thisRLMObj.onTextFieldBlur(e.target);
		}
	);

	}, // End initTextField().

	onTextFieldFocus: function (textField) {

		if (textField.value == textField.initLabel) {
			textField.value = '';
			textField.style.color = this.filledTextColor;
		} // End if.

	}, // End onTextFieldFocus().

	onTextFieldBlur: function (textField) {

		if (textField.value == '') {
			textField.value = textField.initLabel;
			textField.style.color = this.emptyTextColor;
		} // End if.

	}, // End onTextFieldBlur().

	onSubmit: function () {

		this.onSubmitTextField(this.cityTextField, this.cityBufferField);
		this.onSubmitTextField(this.stateTextField, this.stateBufferField);
		this.onSubmitTextField(this.zipTextField, this.zipBufferField);

		if (this.zipBufferField.value == '') {

			if ((this.cityBufferField.value) == '' || (this.stateBufferField.value == '')) {

				alert('City and State OR ZIP/Postal Code are required fields.');
				return;

			} // End if.

		} // End if.

		this.setCookieValue(this.cityTextField.value, this.stateTextField.value, this.zipTextField.value);

		//        $.extend(s_gdyr, trackingVariables["tracking-00054"]);
		//        s_gdyr.eVar21 = this.zipTextField.value;
		//        s_gdyr.tl();
		//        return false;

		this.callFloodlightTag("http://fls.doubleclick.net/activityi;src=2810977;type=lar;cat=gytir061;ord=");
		setTimeout(this.form.submit(), 500);
	}, // End onSubmit().

	onSubmitTextField: function (textField, bufferField) {

		if (textField.value == textField.initLabel) {
			bufferField.value = '';
		} else {
			bufferField.value = textField.value;
		} // End if.

	}, // End onSubmitTextField().
	setCookieValue: function (city, state, zip) {
		var date = new Date();
		date.setTime(date.getTime() + (90 * 24 * 60 * 60 * 1000));
		expires = '; expires=' + date.toUTCString();
		var value = [city, state, zip].join('|');
		document.cookie = ["retailerLocator", "=", encodeURIComponent(value), expires, "; path=/"].join('');
	},
	getCookieValue: function () {
		var name = "retailerLocator";
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	},
	callFloodlightTag: function (sourceTag) {
		var tag_url = sourceTag + Math.floor(Math.random() * 999999) + "?";
		if (document.getElementById("DCLK_FLDiv")) { var flDiv = document.getElementById("DCLK_FLDiv"); }
		else {
			var flDiv = document.body.appendChild(document.createElement("div"));
			flDiv.id = "DCLK_FLDiv";
			flDiv.style.display = "none";
		}
		var DCLK_FLIframe = document.createElement("iframe");
		DCLK_FLIframe.id = "DCLK_FLIframe_" + Math.floor(Math.random() * 999999);
		DCLK_FLIframe.src = tag_url;
		flDiv.appendChild(DCLK_FLIframe);
	}
});