var FBSM=window.FBSM;
if(typeof FBSM=='undefined'){FBSM=new Object();FBSM.C=new Object();FBSM.PD=new Object();FBSM.U=new Object();FBSM.SC=new Object();FBSM.W=new Object();FBSM.WC=new Object();FBSM.F=new Object();FBSM.TFG=new Object();FBSM.R=new Object();}

FBSM.SC.lastNote = '';
FBSM.SC.lastBrand = "";
window.onscroll = function(){
	$("#IEFRAMEFIX:visible").show();
	$("#IEFRAMEFIX:hidden").hide();
};
/* BRAND LIST PAGE - FIRST PAGE */
FBSM.SC.toggleLists=function(){
	if($("#ListList").attr("class")=="hidden"){
		$("ListDropDown").attr("class","hidden");
		$("#ListList").removeClass();
		$("#showcase_switch_label").html("Condense View");
	} else {
		$("ListDropDown").removeClass();
		$("#ListList").attr("class","hidden");
		$("#showcase_switch_label").html("Expand View");
	}
}
FBSM.SC.typeFilter=function(type){
	FBSM.SC.toggleLoad(1);
	$.ajax({
		type: "POST",
		url: "controls/Showcase/Subpage/Main_TypeFilter.aspx",
		data: "Type=" + type,
		dataType: 'application/x-www-form-urlencoded',
		success: function(response){
			$("#ShowcaseWrapper").html(response);
			FBSM.SC.toggleLoad(0);
		}
	});
}
/* SINGLE PRODUCT PAGE - THIRD PAGE */
FBSM.SC.showpay=function(){
	if ((document.calc.loan.value == null || document.calc.loan.value.length == 0) || (document.calc.months.value == null || document.calc.months.value.length == 0) || (document.calc.rate.value == null || document.calc.rate.value.length == 0)){
		var details = "";
		if(document.calc.loan.value == null || document.calc.loan.value.length == 0) details = "Loan amount";
		if(document.calc.months.value == null || document.calc.months.value.length == 0) details += ((details!="")?", " : "") + "Loan length";
		if(document.calc.rate.value == null || document.calc.rate.value.length == 0) details += ((details!="")?", " : "") + "Interest rate";
		alert(details + " field" + ((details.indexOf(",")>-1) ? "s have" : " has") + " not been completely filled out. Please verify that all fields are complete.");
	} else {
		var princ = (document.calc.loan.value>0) ? document.calc.loan.value : 1;
		var term  = (document.calc.months.value>0) ? document.calc.months.value : 1;
		var intr  = (document.calc.rate.value>0) ? document.calc.rate.value / 1200 : 0;
		document.calc.pay.value = (intr>0) ? Math.round(princ * intr / (1 - (Math.pow(1/(1 + intr), term)))) : Math.round(princ/term);
		document.calc.annualpay.value = (intr>0) ? (Math.round(princ * intr / (1 - (Math.pow(1/(1 + intr), term)))) *12) : Math.round(princ/(term/12));
	}
}
FBSM.SC.show_info=function(showme){
	$(".ShowCaseContentChunk").hide();
	$("#infoList").children().removeClass();
	$("#"+showme).show();
	$("#"+showme+"_li").attr("class","activeTab");
	$(".equipmentListIcon_img").each(function(){if($(this).width() >= 120){$(this).width(120);}});
}
FBSM.SC.toggleViewThumbs=function(showAll){
	$("#ShowcaseHeroThumbs").attr("class",((showAll) ? 'contactSheet' : 'scrolling'));
	$("#viewAll").toggleIf(!showAll);
	$("#hideAll").toggleIf(showAll);
}
FBSM.SC.show_full=function(image, imgsource){
	$('#LargeHeroLink').attr("href",image);
	$('#LargeHero').attr("src",image);
	$('#photoSource').html(imgsource);
}
FBSM.SC.show_fullImage=function(imagea, imagesrc, imgsource){
	$('#LargeHeroLink').attr("href",imagea);
	$('#LargeHero').attr("src",imagesrc);
	$('#photoSource').html((imgsource!="")?imgsource:"&nbsp;");
}
FBSM.SC.viewAll=function(toggle){
	$('#ShowcaseProductsDD').toggleIf(!toggle);
	$('#ShowcaseProductsList').toggleIf(toggle);
	$('#viewToggle').html("<a href=\"#\" onclick=\"FBSM.SC.viewAll(" + ((toggle) ? "0" : "1") + ");return false;\">" + ((toggle) ? "Condense View" : "View All") + "</a>");
}
FBSM.SC.showNote=function(name){
	if(name==FBSM.SC.lastNote){FBSM.SC.hideNote(name); return;}
	if(FBSM.SC.lastNote!=''){FBSM.SC.hideNote(FBSM.SC.lastNote);}
	FBSM.SC.lastNote = name;
	$("#"+name+'_body').attr("class","notevisible");
	$("#IEFRAMEFIX").show();
	FBSM.SC.checkScroll($("#"+name+'_body')[0]);
	if($("#rb_txtFirstName").length>0){
		var rb_offsetWidth = $("#rb_txtFirstName").width();//+"px !important";
		$("#rb_cmbCountry").css("width",rb_offsetWidth);
		$("#rb_cmbRegion").css("width",rb_offsetWidth);
	}
	if($("#rq_txtFirstName").length>0){
		var rq_offsetWidth = $("#rq_txtFirstName").width();//+"px !important";
		$("#rq_cmbCountries").css("width",rq_offsetWidth);
		$("#rq_cmbRegions").css("width",rq_offsetWidth);
	}
	switch(name){
		case "raqs": FBSM.C.loadCustomJavascriptParameters("show_request_quote_start","o"); break;
		case "rab": FBSM.C.loadCustomJavascriptParameters("request_brochure_start","o"); break;
		case "financings": FBSM.C.loadCustomJavascriptParameters("show_calc_start","o"); break;
	}
}
FBSM.SC.hideNote=function(name){
	$("#"+name+'_body').attr("class","notehidden");
	$("#IEFRAMEFIX").hide();
	FBSM.SC.lastNote = '';
}
FBSM.SC.checkScroll=function(body){
	var scrollToTop = 0;
	var IEFRAMEFIX = $("#IEFRAMEFIX")[0];
	var wrapperBody = $("#ShowcaseWrapper")[0];
	if( typeof( window.innerWidth ) == 'number' ){
		scrollToTop = window.pageYOffset;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ){
		scrollToTop = document.body.scrollTop;
		if(scrollToTop==0) scrollToTop = document.documentElement.scrollTop;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ){
		scrollToTop = document.body.scrollTop;
		if(scrollToTop==0) scrollToTop = document.documentElement.scrollTop;
	}
	var offsetTop = wrapperBody.offsetTop;
	var topval = (scrollToTop - offsetTop >= 0) ? scrollToTop - offsetTop: 0;
	var leftval = Math.round((wrapperBody.clientWidth/2)-(body.clientWidth/2));
	body.style.top = (topval+30) + "px";
	body.style.left = (leftval>30) ? leftval + "px" : "30px";
	IEFRAMEFIX.style.top = (topval+30) + "px";
	IEFRAMEFIX.style.left = (leftval>30) ? leftval + "px" : "30px";
	IEFRAMEFIX.style.height = body.offsetHeight;
}
FBSM.SC.comparativeFeatureValueChanged=function(productOwnerId, value, caller, elementToUpdate, hasOptionsFlag){
	if(value!="-1"){
		var lastDigitOfElement = elementToUpdate.substring(elementToUpdate.length-1,elementToUpdate.length);
		var url="",data="";
		switch(caller) {
			case "showcase_compare_year":
				url = "controls/Showcase/Subpage/Compare_GetProductOwners.aspx";
				data = "id=" + productOwnerId + "&year=" + value;
				break;
			case "showcase_compare_po":
				var year = $("#showcase_compare_year_" + lastDigitOfElement);
				if((year.length>0 || hasOptionsFlag==1)) {
					url = "controls/Showcase/Subpage/Compare_GetStyles.aspx";
					data = "id=" + productOwnerId + "&year=" + ((year.length>0) ? year.val() : "") + "&poId=" + value;
				}
				break;
			case "showcase_compare_style":
				var year = $("#showcase_compare_year_" + lastDigitOfElement);
				var po = $("#showcase_compare_po_" + lastDigitOfElement);
				if((year.length>0 || hasOptionsFlag==1) && po.length>0) {
					url = "controls/Showcase/Subpage/Compare_GetModels.aspx";
					data = "id=" + productOwnerId + "&year=" + ((year.length>0) ? year.val() : "") + "&poId=" + po.val() + "&styleId=" + value;
				}
				break;
			case "showcase_compare_model":
				url = "controls/Showcase/Subpage/Compare_GetOptions.aspx";
				data = "id=" + value;
				break;
		}
		if(url!=""&&data!="") {
			FBSM.SC.toggleLoad(1);
			$.ajax({
				type: "POST",
				url: url,
				data: data,
				dataType: 'application/x-www-form-urlencoded',
				success: function(response){
					var element = $("#"+elementToUpdate);
					FBSM.SC.innerHTMLSelectFix(element[0],response);
					element.css("width",($("#showcase_compare_year_1").length>0) ? $("#showcase_compare_year_1").width() : $("#showcase_compare_po_1").width());
					$('#' + elementToUpdate).selectbox();
					FBSM.SC.toggleLoad(0);
				}
			});
		}
	}
}
FBSM.SC.showfeatures=function(id, type){
	if((type=="option") && $("#showcase_compare_option_0").val()=="-1") $("#showcase_compare_option_0")[0].selectedIndex = 1;
	var id0 = ($("#showcase_compare_option_0").length>0)?$("#showcase_compare_option_0").val():"-1";
	var id1 = $("#showcase_compare_" + type + "_1").val();
	var id2 = $("#showcase_compare_" + type + "_2").val();
	var strIds = ((id0!="-1")?id0:id)+((id1!="-1")?","+id1:"")+((id2!="-1")?","+id2:"");
	if(strIds!=id){
		$.ajax({
			type: "POST",
			url: "controls/Showcase/Subpage/Compare_GetFeatures.aspx",
			data: "products=" + strIds + "&areOptions=" + (type=="option"),
			dataType: 'application/x-www-form-urlencoded',
			success: function(response){
				$("#showcase_compare_features").html(response);
				FBSM.SC.toggleLoad(0);
			}
		});
	}
	$("#showcase_compare_print_link").removeAttr("onclick").unbind().click(function(){
		var popup = window.open('showcaseprintprodcompare.htm?products='+strIds+'&areOptions='+(type=="option"),'_blank');
		popup.focus();
		return false;
	});
}
FBSM.SC.getRegions_showcase=function(country, caller){
	FBSM.SC.toggleLoad(1);
	$.ajax({
		type: "POST",
		url: "controls/Showcase/Subpage/SinglePage_GetRegions.aspx",
		data: "country_name=" + country,
		dataType: 'application/x-www-form-urlencoded',
		success: function(response){
			var element = $("#"+caller);
			FBSM.SC.innerHTMLSelectFix(element[0],response);
			element.css("width",(caller.indexOf("rb_")>-1) ? $("#rb_txtFirstName").width() : $("#rq_txtFirstName").width());
			FBSM.SC.toggleLoad(0);
		}
	});
}
FBSM.SC.preValidateForm=function(caller, year, brand, name, type, validateCityState, productType){
	var hasErrors=false,EmptyElements="",requiredElements="",requiredElmNames="",email="",confirmEmail="",urlString="";
	if(caller.id.indexOf("rq")>-1){
		urlString = "controls/Showcase/Subpage/SinglePage_SendQuoteRequest.aspx";
		if($("rq_checkWithTrade:checked").length>0){
			requiredElements=new Array("#rq_txtFirstName", "#rq_txtLastName", "#rq_txtEmail", "#rq_txtConfirmEmail", "#rq_txtDayPhone", "#rq_txtTradeManufacturer", "#rq_txtTradeModel", "#rq_txtTradeUse", "#rq_txtTradeYear");
			requiredElmNames=new Array("First Name", "Last Name", "Email", "Confirmation Email", "Phone", "Trade Manufacturer", "Trade Model", "Years Owned", "Trade Year");
		} else if($("#rq_txtVehicleYear").length>0){
			requiredElements=new Array("#rq_txtFirstName", "#rq_txtLastName", "#rq_txtEmail", "#rq_txtConfirmEmail", "#rq_txtDayPhone", "#rq_txtVehicleYear", "#rq_txtVehicleMake", "#rq_txtVehicleModel", "#rq_txtVehicleStyle");
			requiredElmNames=new Array("First Name", "Last Name", "Email", "Confirmation Email", "Phone", "Vehicle Year", "Vehicle Manufacturer", "Vehicle Model", "Vehicle Style");
		} else {
			requiredElements=new Array("#rq_txtFirstName", "#rq_txtLastName", "#rq_txtEmail", "#rq_txtConfirmEmail", "#rq_txtDayPhone");
			requiredElmNames=new Array("First Name", "Last Name", "Email", "Confirmation Email", "Phone");
		}
		if(validateCityState==1){
			requiredElements.push("#rq_txtCity", "#rq_cmbRegions");
			requiredElmNames.push("City","Region");
		}
		email = $("#rq_txtEmail").val();
		confirmEmail = $("#rq_txtConfirmEmail").val();
	} else {
		urlString = "controls/Showcase/Subpage/SinglePage_SendBrochureRequest.aspx";
		requiredElements=new Array("#rb_txtFirstName", "#rb_txtLastName", "#rb_txtEmailAddress", "#rb_txtEmailAddressConfirmation", "#rb_txtAddress1", "#rb_txtCity", "#rb_txtPostalCode");
		requiredElmNames=new Array("First Name", "Last Name", "Email", "Confirmation Email", "Address", "City", "Postal Code");
		email = $("#rb_txtEmailAddress").val();
		confirmEmail = $("#rb_txtEmailAddressConfirmation").val();
	}
	var element = null;
	for(i=0;i<requiredElements.length;i++){
		element = $(requiredElements[i]);
		if(element.val()==""){
			hasErrors = true;
			EmptyElements += ((EmptyElements!="") ? ", " : "") + requiredElmNames[i];
		}
	}
	if(!hasErrors){
		if(brand==undefined || brand=="" || name==undefined || name==""){
			alert("Brand, and/or Product Name are empty.  Please reload the page.");
		} else {
			if(email!="" && confirmEmail!="" && email.toLowerCase()==confirmEmail.toLowerCase()){
				var prefix = (caller.id.indexOf("rq")>-1)?"rq":"rb";
				var queryString = prefix+"_type="+escape(type)+"&"+prefix+"_year="+escape(year)+"&"+prefix+"_brand="+encodeURIComponent(brand)+"&"+prefix+"_name="+encodeURIComponent(name)+"&"+prefix+"_productType="+((productType!=undefined&&productType!="")?productType:"new")+"&";
				$(caller).parents("form").find("input:not(:submit,:button),select,textarea").each(function(i) {
					queryString+=this.id+"="+((this.type!="checkbox")?escape(this.value):this.checked)+"&";
				});
				$.ajax({
					type: "POST",
					url: urlString,
					data: queryString,
					dataType: 'application/x-www-form-urlencoded',
					success: function(response){
						if(response.substring(0,2)=="1:"){
							alert(response.substring(2));
						} else {
							FBSM.SC.hideNote((caller.id.indexOf("rq_")>-1)?"raqs":"rab");
							$("#response_body_contents").html(response);
							FBSM.SC.showNote("response");
							$("#response_body_contents").find(':text,:password,textarea').attr('value','').end().find(':checkbox,:radio').attr('checked',false).end().find('select').attr('selectedIndex',-1);
							FBSM.C.loadCustomJavascriptParameters("form_submit","o");
						}
						FBSM.SC.toggleLoad(0);
					}
				});
			} else {
				alert("Email addresses do not match, please verify that they are correct and submit the form again.");
			}
		}
	} else {
		alert(EmptyElements + ((EmptyElements.indexOf(",")>-1)? " are empty. These fields are" : " is empty. This field is") + " required, please double check your form and submit it again.");
	}
}

/* Product list page, get tabs - pulled out to eliminate load times */
FBSM.SC.showTabs=function(brand, type, year, hash){
	//CALL METHOD TO ASPX FILE TO LOAD NEW CONTENT
	hash = ((hash!="")?hash:type);
	FBSM.SC.toggleLoad(1);
	$.ajax({
		type: "POST",
		url: "controls/Showcase/Subpage/ProductList_GetTab.aspx",
		data: "Brand=" + brand + "&Type=" + type + "&Year=" + year,
		dataType: 'application/x-www-form-urlencoded',
		success: function(response){
			var element = $("#showcaseProductListFieldset").html(response);
			if(hash!=""){
				if(hash.toString().indexOf("#")>-1){
					location.hash = hash.toString();
					FBSM.SC.recordYearChange(year);
				} else {
					if(hash!=-1) $("quicksearch_type options[value='"+hash.toString()+"']").attr('selected', 'selected');
				}
			}
			FBSM.SC.toggleLoad(0);
		}
	});
	//Update promotion widget on page
	$.getJSON("/Services/WidgetServices.asmx/GetPromoJSON?brand=" + brand + "&type=" + type + "&year=" + year, function(json){
		FBSM.W.globalPromoData = json;
		FBSM.W.initializeOEMPromotions();
	});
}
FBSM.SC.updateAction = function(){
	var formElm = $("#showcase_quicksearch_form");
	var brandElm = $("#quicksearch_brand");
	var typeElm = $("#quicksearch_type");
	var yearElm = $("#quicksearch_year");
	formElm.attr("action","showcaseproductslist.htm?" + "Brand=" + FBSM.SC.correctEncoding(brandElm.val()) + ((typeElm.val()!="ALL") ? "&Type=" + FBSM.SC.correctEncoding(typeElm.val()) : "&Type=" + FBSM.SC.correctEncoding(typeElm[0].options[1].value)) + ((yearElm.val()!="ALL")? "&Year=" + yearElm.val() : ""));
	if(brandElm.val()!="ALL"){
		formElm.submit();
	} else {
		formElm.attr("action","showcase.htm");
		formElm.submit();
	}
}
FBSM.SC.correctEncoding = function(value) {
	return value.replace("&", "%26");
}
FBSM.SC.updateCaller = function(caller) {
	caller = $(caller);
	var callerIsBrand = (caller.attr("id")=="quicksearch_brand");
	if(callerIsBrand) FBSM.SC.lastBrand = caller.val() != "ALL";
	if(callerIsBrand || caller.val()!="ALL") {
		FBSM.SC.toggleLoad(1);
		$.ajax({
			type: "POST",
			url: (callerIsBrand) ? "controls/Showcase/Subpage/QuickSearch_GetTypes.aspx" : "controls/Showcase/Subpage/QuickSearch_GetYears.aspx",
			data: (callerIsBrand) ? "Brand=" + FBSM.SC.correctEncoding(caller.val()) : "Brand=" + FBSM.SC.correctEncoding($("#quicksearch_brand").val()) + "&Type=" + FBSM.SC.correctEncoding(caller.val()),
			dataType: 'application/x-www-form-urlencoded',
			success: function(response){
				if(callerIsBrand) {
					$("#quicksearch_type").parent().html(response);
					$("#quicksearch_year").parent().html("<select id=\"quicksearch_year\"><option value=\"ALL\">- Year -</option><option value=\"ALL\">- All -</option></select>");
				} else {
					$("#quicksearch_year").parent().html(response);
					if(response=="<select id=\"quicksearch_year\"><option value=\"ALL\">- Year -</option><option value=\"ALL\">- All -</option></select>") {
						$("#quicksearch_year").closest("td").hide();
					} else {
						$("#quicksearch_year").closest("td").show();
					}
				}
				FBSM.SC.toggleLoad(0);
			}
		});
	} else {
		$("#quicksearch_year").parent().html("<select id=\"quicksearch_year\"><option value=\"ALL\">- Year -</option><option value=\"ALL\">- All -</option></select>");
	}
}
FBSM.SC.loadCheckBody = function() {
	var scrollToTop = 0;
	var wrapperBody = $("#ShowcaseWrapper")[0];
	if( typeof( window.innerWidth ) == 'number' ) {
		scrollToTop = window.pageYOffset;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		scrollToTop = document.body.scrollTop;
		if(scrollToTop==0) scrollToTop = document.documentElement.scrollTop;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		scrollToTop = document.body.scrollTop;
		if(scrollToTop==0) scrollToTop = document.documentElement.scrollTop;
	}
	var offsetTop = wrapperBody.offsetTop;
	var topval = (scrollToTop-offsetTop>=0)?scrollToTop-offsetTop:0;
	var leftval = Math.round((wrapperBody.clientWidth/2)-20);
	$("#load").css("top",topval + 200).css("left",(leftval>50)?leftval:50);
}
FBSM.SC.toggleLoad=function(status){
	$("#load").toggleIf(status);
	if(status)FBSM.SC.loadCheckBody();
}
FBSM.SC.recordYearChange = function(year) {
	if(typeof(year)=="string") {
		FBSM.C.loadCustomJavascriptParametersFromSource('s.pageName="showcase product list";s.eVar21={@showcaseProductOwner};s.eVar22={@showcaseType};s.eVar23="' + year.addSlashes() + '";s.prop14=s.eVar21;s.prop15=s.eVar22;s.prop16=s.eVar23;');
	}
}
