$.extend($.ford, { common: {} });
$.extend($.ford.common, {postalCode: {}});
$.extend($.ford.common.postalCode, { change: function(postalCode) {
	$.ajax({
		url: _F.root + "/changePostalCode.json",
		data: { postalCode: postalCode.toUpperCase() },
		async: false,
		dataType: 'json',
		success: function(data) {
			_F.prov = data.province;
			_F.postal = data.postal;
			$('.js_postal_html').html(_F.postal);
			
			_F.location = {
				province : data.province,
				postal : data.postal
			};
			window.postal = data.postal;
			window.prov = data.province;
			Ford.Vehicle.Msrp.load();
			Ford.Common.PostalCode.set();
		}
	});
}});



Ford.Common = {
	WT: function(group, subGroup, scenario, subScenario, adClick) {
		var url = window.location.href;
		var title = $('title').html();
		if(scenario != undefined) {
			url += '#' + scenario;
			title += '#' + scenario;
		} else {
			url += '#' + group;
			title += '#' + group;
		}
		
		if(group == null || group == undefined) {
			group = '';
			subGroup = '';
		} else {
			if(subGroup == null || subGroup == undefined) {
				subGroup = '';
			}
		}

		if(scenario == null || scenario == undefined) {
			scenario = '';
			subScenario = '';
		} else {
			if(subScenario == null || subScenario == undefined) {
				subScenario = '';
			}
		}
		if (adClick == null || adClick == undefined){
			adClick = '';
		}

		dcsMultiTrack(
				"DCS.dcsuri", url,
				"WT.ti", title, 
				"WT.cg_n", group, 
				"WT.cg_s", subGroup, 
				"WT.si_n", scenario, 
				"WT.si_x", subScenario,
				"WT.ac", adClick);
		
	},
	getParam: function(param) {
		var vars = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			vars[key] = value;
		});
		return vars[param];
	},		
	getDoubleClick : function(array) {
		var obj = new Object;
		obj.url = "http://fls.doubleclick.net/activityi";
		obj.src = "1906242";
		obj.num = Math.random() + "";
		obj.num *= 10000000000000;
		// debug(obj.num + " = obj.num")
		obj.type = "";
		obj.cat = "";
		if (array != undefined) {// was =!
			obj.array = new Array();
		} else {
			obj.array = array;
		}

		$
				.extend(
						obj,
						{
							run : function() {
								var str = this.url + ';src=' + this.src
										+ ';type=' + this.type + ';cat='
										+ this.cat;

								$.each(this.array, function(key, data) {
									if (data != undefined) {
										str += ';u' + key + '=' + data;
									}
								});

								str += ';ord=1;num=' + this.num + '?';
								jQuery('body')
										.append(
												'<iframe src="'
														+ str
														+ '" width="0" height="0" frameborder="0"></iframe>');
								// debug("run() called");
							}
						});
		return obj;
	},
	url : {
		http : function() {
			return 'http';
		},
		https : function() {
			if (_F.isSsl) {
				return "https";
			}
			return Ford.Common.url.http();
		},
		goTo: function(path) {
			if(path.indexOf('http') > -1) {
				window.location = path;
				return;
			}
			window.location = _F.root + path;
		}
	},
	redirect : function(url, params, ssl) {
		if (ssl == undefined) {
			ssl = false;
		}

		var domain = F.Common.url.http();
		if (ssl) {
			domain = F.Common.url.https();
		}

		domain += '://' + window.location.host + _F.root;
		domain += url;

		if (typeof params == 'string') {
			params = eval("(" + params + ")");
		}

		if (params != undefined && params instanceof Object) {
			domain += '?';
			for ( var propertyName in params) {
				domain += propertyName + '='
						+ escape(eval('params["' + propertyName + '"]'));
				domain += '&';
			}
		}
		window.location.replace(domain);
	},
	input : {
		select : function(options) {
			var isSelect = false;

			$(options.select)
					.append(
							$('<option>').attr('value', '').append(
									local('select_one')));
			$.each(options.data, function(key, obj) {
				var option = $('<option>');
				if (typeof obj == 'string') {
					$(option).attr('value', obj);
					$(option).append(obj);
					if (obj == options.selected) {
						$(option).attr('selected', 'selected');
						$(options.select).children('option:first').attr(
								"disabled", "disabled");
						isSelect = true;
					}
				} else {
					for ( var prop in obj) {
						if (prop != 'key' && prop != 'value') {
							$(option).attr('data-' + prop, obj[prop]);
						}
					}
					$(option).attr('value', obj.key);
					$(option).append(obj.value);
					if(options.selected != null) {
						var keyStr = new String(obj.key);
						if(obj[options.selector] === options.selected || keyStr.indexOf(options.selected) != -1) {
							$(option).attr('selected', 'selected');
							$(options.select).children('option:first').attr("disabled", "disabled");
							isSelect = true;
						}
					}
				}
				$(options.select).append(option);
			});
			if (options.data.length == 1 && options.auto_select == true) {
				$(options.select).children('option:not(:first)').attr(
						'selected', 'selected');
				$(options.select).attr('disabled', 'disabled');
				if (!isSelect) {
					$(options.select).trigger('change');
				}
			} else {
				$(options.select).removeAttr('disabled');
			}

		}
	},
	Flash : {
		getModelYear : function() {
			return _F.modelYear;
		},
		getMovie : function(movieName) {
			var isIE = navigator.appName.indexOf("Microsoft") != -1;
			return (isIE) ? window[movieName] : document[movieName];
		},
		loadQAVideo : function(url, videocontainer, div) {
			var so = new SWFObject(url, videocontainer, "415", "234", "9",
					"#cccccc");
			so.addParam("bgcolor", "#cccccc");
			so.addParam("wmode", "transparent");
			so.addParam("allowfullscreen", "true");
			so.addParam("allowScriptAccess", "always");
			so.write(div);
		}
	},
	PostalCode : {
		callback: function(val) { },
		ignore : function() {
			Ford.Vehicle.Msrp.load();
			Ford.Cookie.addZipIgnore();
		},
		popup : function() {
			$("#postalmodal").dialog({
				autoOpen : true,
				modal : true,
				closeText : 'hide',
				width : 442
			});
			//location.hash = 'zip_popup';
		},
		changepopup : function() {
			$(".btnOK").click(function(e) {
				e.preventDefault();
				if (!Ford.Validation.Input.isCaZip('#postalCodeInput2')) {
					$('#postalCodeInput2').css('border', '1px solid red');
					return;
				}
				Ford.Common.PostalCode.change($('#postalCodeInput2').val());
				Ford.Vehicle.Msrp.load();
				$("#changepostalmodal").dialog("close");
			});
			$(".btnIgnore").click(function(e) {
				$("#changepostalmodal").dialog("close");
			});
			$("#closeBtn a").click(function(e) {
				$("#changepostalmodal").dialog("close");
			});
			$("#changepostalmodal").dialog({
				autoOpen : true,
				modal : true,
				closeText : 'hide',
				width : 442
			});
		},
		change : function(postalCode) {
			$.ajax({
			  url: _F.root + "/changePostalCode.json",
				data: { postalCode: postalCode.toUpperCase() },
				async : false,
				dataType : 'json',
				success : function(data) {
					_F.location = {
						province : data.province,
						postal : data.postal
					};
					window.postal = data.postal;
					window.prov = data.province;

					Ford.Vehicle.Msrp.load();
					Ford.Common.PostalCode.set();

					try {
						Ford.Flash.HomePage.refresh();
					} catch (e) {
					}
					try {
						Ford.Flash.CommercialTruck.refresh();
					} catch (e) {
					}
				}
			});
		},
		set : function() {
			$.each($('.js_postal'), function(key, object) {
				try {
					$(object).html(_F.location.postal);
				} catch (e) {
				}
				try {
					if (Ford.Validation.isCalZip(_F.location.postal)) {
						$(object).val(_F.location.postal);
					}
				} catch (e) {
				}
			});
			$.each($('.js_postal_part1'), function(key, object) {
				if (Ford.Validation.isCalZip(_F.location.postal)) {
					$(object).val(_F.location.postal.substr(0, 3));
				}
			});
			$.each($('.js_postal_part2'), function(key, object) {
				if (Ford.Validation.isCalZip(_F.location.postal)) {
					$(object).val(
							_F.location.postal.substr(
									_F.location.postal.length - 3,
									_F.location.postal.length));
				}
			});
		},
		// Updates the value of any input field with the js_postal_input class with the current postal
		// code stored in _F.postal. This was added to support legacy code that doesn't use the new
		// fo/Ford.Common.js file.
		updateInputs: function() {
			var myPostal = (_F.postal) ? _F.postal : postal;
			if(myPostal) {
				$('.js_postal_input').each(function() {
					$(this).val(myPostal);
				});
			}
		}
	},
	NoAccessoriesModal : {
		delayedPopup : function() {
			// ** somewhere the jquery ui dialog is getting its wires crossed.
			// Delaying the call to this popup temporarily solves this issue,
			// brewer 2/22/11 **
			window.setTimeout(Ford.Common.NoAccessoriesModal.popup, 2000);
		},
		popup : function() {
			jQuery('div.noAccessoriesModal').dialog({
				autoOpen : true,
				modal : true,
				closeText : 'hide',
				width : 442
			});
			location.hash = 'no_accessories';

			jQuery("div.noAccessoriesModal #closeBtn a").click(function(e) {
				$("div.noAccessoriesModal").dialog("close");
			});
			jQuery("div.noAccessoriesModal .btn").click(function(e) {
				$("div.noAccessoriesModal").dialog("close");
			});
		}
	},
	
	// Added for RaB Lincoln Page
	Contact: function() {
		var firstName;
		var lastName;
		var phone;
		var email;
		var address;
		var city;
		var postal;

		this.phone = '';
		this.email = '';
		this.address = '';
		this.city = '';
	}
};

$.extend($.ford.common, {getParam: function(param) {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars[param];
}});
$.extend($.ford.common, {WT: Ford.Common.WT });
$.extend($.ford.common.WT, {metatag: {}});
$.extend($.ford.common.WT.metatag, {set: function(key, value) {
	var metatag = $('<meta>');
	metatag.attr('name', key);
	metatag.attr('content', value);
	$('head').prepend(metatag);
}});

F.C = Ford.Common;
function Contact() {
	var firstName;
	var lastName;
	var phone;
	var email;
	var address;
	var city;
	var postal;

	this.phone = '';
	this.email = '';
	this.address = '';
	this.city = '';
}

// function DoubleClick(array) {
// var url = "http://fls.doubleclick.net/activityi"
// var src = "1906242";
// var num = F.C.dbl();
// var type;
// var cat;
// }

