﻿function iframeResize() {
	var iframeHelper = document.getElementById('iframeHelper');
	if (!iframeHelper)
		return;
	var i = iframeHelper.src.indexOf('?');
	if (i == -1)
		i = iframeHelper.src.length;
	var height = document.body.scrollHeight;
	iframeHelper.src = iframeHelper.src.substr(0, i) + '?height=' + height + '&cacheb=' + Math.random()
}
var categoryRegex = /(.*)\/(?:Promo|Stock|VN)(.*)/i;
function changeCategory(category) {
	var form = document.forms["main"];
	var action = form.action;
	if (!action)
		action = window.location.pathname;
	action = action.replace(categoryRegex, '$1/' + category + '$2');
	if (form.action != action) {
		form.action = action;
		form.submit();
	}
}

function gotoToUrl(el_href) {
    $(".vconfig form#main")[0].action = el_href;
    $(".vconfig form#main")[0].submit();
    return false;
}

function filterForm(th) {
	//d = console.debug;
	var items = $(".vconfig .filter input:enabled,.vconfig .filter select:enabled");
	var query = {};
	var make, model;
	$.each(items, function(i,item) {
		//d(item)
		var name = item.id || item.name;
		if(item.disabled) return;
		if(name == "make") {
			make = $(item).val();
			return;
		}
		if(name == "model" && make) {
			model = $(item).val();
			return;
		}
		
		if($(item).val() != "" && item.type != "checkbox") {
			query[name] = $(item).val();
		}
	});
	
	
	var action;
	try {
		action = $(".vconfig form#main")[0].action;
	} catch (e) {
		var forms = document.forms;
		for (var i = 0; i < forms.length; i++) {
			if (forms[i].id == "main") {
				action = forms[i].action;
				break;
			}
		}
	}
	
	var extension = "";
	var lastIndexOfSlash = action.lastIndexOf("/");
	var lastIndexOfDot = action.lastIndexOf(".");
	if (lastIndexOfDot > 0 && lastIndexOfDot > lastIndexOfSlash) {
	    extension = action.substr(lastIndexOfDot, action.length - 1);
	    action = action.substr(0, lastIndexOfDot);
	}
	if(make) action += "/" + make;
	if(model) action += "/" + model;
	action += extension;
	
	var qPairs = [];
	$.each(query, function(key,val) {
		qPairs.push(key + "=" + val);
	});
	//d(qPairs)
	//d(query)
	//d(items)
	var queryString =  qPairs.join("&");
	//d(action + (queryString ? "?"+queryString : ""));
	if (document.location.href.indexOf("huet.multimarque.fr") >= 0) {
		//alert(action + "?" + queryString);
	}
	document.location.href = action + (queryString ? "?"+queryString : "");
}

function submitForm(name, value, action) {
	var form = document.forms["main"];
	if (name) {
		var input = document.createElement("INPUT");
		input.name = name;
		if (value)
			input.value = value;
		input.type = 'hidden';
		form.appendChild(input);
	}
	form.submit();
}
function twistSection(twisty) {
	if (!twisty)
		return;
	var parentDiv = twisty;
	while (parentDiv.tagName != 'DIV') {
		parentDiv = parentDiv.parentNode;
	}
	var section = parentDiv.nextSibling;
	if (section != null && (section.tagName == 'DIV' || section.tagName == 'TABLE')) {
		if (section.style.display != 'none') {
			section.style.display = 'none';
			twisty.className += ' collapsed';
			if (twisty.tagName == 'IMG')
				twisty.alt = twisty.title = 'Show ' + twisty.name;
		} else {
			section.style.display = 'block';
			twisty.className = twisty.className.replace(' collapsed', '');
			if (twisty.tagName == 'IMG')
				twisty.alt = twisty.title = 'Hide ' + twisty.name;
		}
	}
}
function submitOnEnter(input) {
	var c = window.event.keyCode;
	if (c == 13 || c == 9)
		submitForm(input.id, input.value);
}
function keyUp(input) {
	var c = window.event.keyCode;
	if ((c > 16 && c < 46 && c != 27) || c == 91 || c == 92 || c == 93 || (c > 111 && c < 186))
		return;
	submitForm(input.id, input.value);
}
var activeTab = 0;
function initTabs() {
	$(".vconfig .tabs > .content > *").hide();
	var tabs = $(".vconfig .tabs > .tabRow > div.tab");
	tabs.eq(0).addClass('first');
	tabs.eq(tabs.size() - 1).addClass('last');
	showTab(tabs.get(0));
}
function showTab(tab) {
	var tabs = $(".vconfig .tabs > .tabRow > div.tab");
	var content = $(".vconfig .tabs > .content > *");
	var prev = tabs.get(activeTab);
	if (activeTab == 0) {
		prev.className = 'tab first';
		prev.nextSibling.className = null;
	}
	else if (activeTab == tabs.size() - 1) {
		prev.className = 'tab last';
		prev.previousSibling.className = null;
	}
	else {
		prev.className = 'tab';
		prev.nextSibling.className = null;
		prev.previousSibling.className = null;
	}
	content.eq(activeTab).hide();
	activeTab = tabs.index(tab);
	if (activeTab == 0) {
		tab.className += 'On';
		tab.nextSibling.className = 'onoff';
	}
	else if (activeTab == tabs.size() - 1) {
		tab.className += 'On';
		tab.previousSibling.className = 'offon';
	}
	else {
		tab.className += ' on';
		tab.nextSibling.className = 'onoff';
		tab.previousSibling.className = 'offon';
	}
	content.eq(activeTab).show();
}
function findDetailDiv(img) {
	var tr = $(img).parents("tr")[0];
	//var tr = img.parentNode.parentNode;
	var td = $(tr).find(".equip");

	if (!td)
		return null;
	return td;
}
function showDetail(img, show) {
	var div = findDetailDiv(img);
	if (!div[0]) return;
	if (show) {
		div.slideDown("slow")
		img.src = img.src.replace('Off', 'On');
	}
	else if (!div[0].isVisible) {
		div.slideUp("slow")
		img.src = img.src.replace('On', 'Off');
	}
}
function toggleDetail(img, show) {
	var div = findDetailDiv(img);
	if (!div) return;
	if (show == undefined)
		show = div.isVisible ? false : true;
	if (show) {
		div[0].isVisible = true;
		if (!div.IS_VISIBLE)
			showDetail(img, 'true');
	}
	else {
		div[0].isVisible = false;
		showDetail(img);
	}
}
var carRegex = /(.*)\/car\/([\d\w]+)(?:\/Options|\/AltCars|)(.*)/i;

/* quote page */
printBody = function(staticCss) {
	var cssHtml = "";
	if (typeof staticCss == "object") {
		for (var i in staticCss) {
			cssHtml += '<link href="' + staticCss[i] + '" rel="stylesheet" type="text/css" />';

		}
	} else {
		cssHtml += '<link href="' + staticCss + '" rel="stylesheet" type="text/css" />';
	}
	var body = document.getElementById("printBody");
	var bodyHtml = body ? body.innerHTML : null;
	var WindowObject = window.open('', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');
	WindowObject.document.write('<html><head>' + cssHtml + '<script>function showDetail() {} function toggleDetail() {} function twistSection() {}</script></head><body onload="this.focus(); this.print();" class="vconfig print">'
      + bodyHtml + "</body></html>"); // + $("#printBody").html() + "</div></body></html>");
	WindowObject.document.close();
	WindowObject.focus();
}
printQuote = function() {
var wnd = window.open('', 'PrintWindow', 'width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes');
wnd.document.write('<html><head><link href="/vconfig/static/master.css" rel="stylesheet" type="text/css" /><script>function showDetail() {} function toggleDetail() {} function twistSection() {}</script></head><body onload="this.focus(); this.print();" class="vconfig print">');
wnd.document.write($('#printBody').html());

wnd.document.write('<div class="Disclaimer">\
    Tarifs indiqués TTC (TVA 19,6 % incluse) et susceptibles de modification sans préavis, mais garantis une fois le véhicule commandé.<br/>\
    Les points importants des conditions générales de vente :<br/>\
    • Le prix de vente du véhicule est garanti au jour de la commande jusqu’à la livraison.<br/>\
    • Les frais de formalités administratives et de mise à disposition du véhicule effectuées par le garage « Multimarque » (préparation du véhicule, immatriculation provisoire, etc.) sont inclus dans le prix de vente du véhicule.<br/>\
    • Les frais de carte grise définitive sont facturés en sus et dépendent du nombre de chevaux fiscaux, du niveau d’émission de CO2 et du département de lieu de domicile du client.<br/>\
    • Les bonus - malus liés au niveau d’émission de CO2 du véhicule figurant sur le bon commande sont présentés sous réserve d’application par les administrations.<br/>\
    • Les constructeurs se réservent le droit, à tout moment et sans préavis, de modifier les spécifications décrites dans le bon de commande. Ces informations sont données à titre indicatif.<br/>\
    • Le délai de livraison figurant sur le bon de commande est indicatif.<br/>\
    • Un acompte de 10 % est demandé à la signature du bon de commande.<br/>\
    • Dès disponibilité du véhicule, la facture pour le règlement du solde est établie par votre garagiste « Multimarque ».<br/>\
    Nos conditions générales de vente complète : <a href="www.multimarque.fr/cgv">www.multimarque.fr/cgv</a><br/><br/><br/>\
  </div>\
  <table border="0" cellspacing="0" cellpadding="0" width="100%"><tr>\
      <td width="33%"><img src="/i/french/logos/logo_pvm_fr_180_01a.png"/></td>\
      <td width="33%" align="center" style="font-size:13px">http://www.multimarque.fr</td>\
      <td align="right"><img src="/fr/i/ills/ill_telazur_180_01a.png"/></td>\
  </tr></table>\
  </body></html>');
wnd.document.close();
wnd.focus();
}

