// onError handling for auto images
function invalidImg(thisImg) {
	var uc_errimg_lg = 'http://images.domainer.com/www.domainer.com/GENERAL/uc_noimg_320x240.gif';
	var uc_errimg_sm = 'http://images.domainer.com/www.domainer.com/GENERAL/uc_noimg_61x45.gif';
	// Is list or detail?
	if ($("div").is(".uc_list_view")) {
		$(thisImg).attr("src",uc_errimg_sm);
		$(thisImg).attr("height","45");
		$(thisImg).attr("width","61");
	} else if ($("div").is(".uc_gallery")) {
		$(thisImg).attr("src",uc_errimg_lg);
		// Determines detail page-type
		if ($(thisImg).parents().is(".uc_gly_1img") || $(thisImg).parents().is(".selected"))
			$(thisImg).parent().attr("href",uc_errimg_lg);
	}
	$(thisImg).addClass("noimg");
	$(thisImg).attr("onerror","");
	$(thisImg).parent().attr("title","This image is not available.");
};
// Expand/collapse facetBar lists
function uc_fb_toggle(toggle){
	var list_parent = $(toggle).parents(".uc_fb_opt");
	var list_partial = $(list_parent).find(".uc_fb_opt_part");
	var list_full = $(list_parent).find(".uc_fb_opt_full");
	
	if ($(list_partial).is(":visible")) {
		$(list_partial).hide();
		$(list_full).show();
	} else {
		$(list_full).hide();
		$(list_partial).show();
	}
};
// Expand/collapse vehicle details
function uc_toggleDetails(ele) {
	$(ele).parent().find('ul').toggle(250);
	$(ele).toggleClass('expand');
	$(ele).toggleClass('collapse');
};
function toProperCase(str) {
  return str.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
}
// AJAX ZIP location lookup
var uc_zipLocation = "";
var uc_cachedZip = 0;
function uc_ajaxValidateZip(ele) {
	if (ele.value != uc_cachedZip && ele.value.length == 5) {
		var zipAjaxUrl = "http://" + location.host + "/ajax/zip/lookup?zip=" + ele.value;
		$.get(zipAjaxUrl, function(data) {
			var dataArray = data.split(";");
			var dataCity = $.trim(toProperCase(dataArray[0]));
			var dataState = $.trim(toProperCase(dataArray[1]));
			uc_zipLocation = dataCity + ", " + dataState;
			if (uc_zipLocation == "Unknown, Unknown")
				uc_zipLocation = "Unknown ZIP code";
			$("#cityState").val(uc_zipLocation);
		});
	}
}
$(document).ready(function() {
	// Add asterisks (*) to required field labels
	$('label.required').append('&nbsp;<strong>*</strong>&nbsp;');
	// Strip all but digits from phone number.
	var digitsOnlyField = ["#frm_zip","#frm_phone1","#frm_phone4","#home_phone1","#work_phone1","#work_phone_ext","#workPhoneNumber","#workExt","#homePhoneNumber","#zip_search"];
	for (i=0; i < digitsOnlyField.length; i++) {
		$(digitsOnlyField[i]).keyup(function() {
			this.value = this.value.replace(/\D/g,"");
		})
	}
	// Add error class to erroneous fields
	if (typeof(window['errorsArray']) != "undefined") {
		$.each(errorsArray, function() {
			$("#" + this).addClass("error");
		});
	}
	// Enable tooltip extension on <a.tooltip />
	$('a.tooltip').tooltip({
		showBody: "::",
		showURL: false
	});
});
