$(document).ready(function(){
	//$("div#bookmark").html('<script type="text/javascript">addthis_pub = \'wantacar\';</script><a href="http://www.addthis.com/bookmark.php" onmouseover="return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s9.addthis.com/button1-bm.gif" width="125" height="16" style="border:0" alt=""/></a><script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>');
// przesuwanie zawartosic divow zawierajacych liste samochodow
	//borrowed from jQuery easing plugin
		//http://gsgd.co.uk/sandbox/jquery.easing.php
		$.easing.backout = function(x, t, b, c, d){
			var s=1.70158;
			return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
		};
			  
		$("#calcTerm option[value='60']").attr('selected', 'selected');
		$("a#calcPrice").click();
		
		$("#calcoffer").click(function () {
			$("a#financeLink").click();
			scrollTo( $('div').get(2).childNodes[20], 800 );
		});
		
		//obsluga pokazu slajdow
		$("a.pause").html('<img class="vPause" src="images/index/off_pause.gif" alt="stop" />');
		$("a.pause").click(function () {
			var $tempPlayerStat =  document.getElementById("hotOffertSlideShow").value;
			
			if($tempPlayerStat == 'play'){
				document.getElementById("hotOffertSlideShow").value='stop';
				$("a.pause").html('<img class="vPlay" src="images/index/off_play.gif" alt="stop" />');
			}
			if($tempPlayerStat == 'stop'){
				document.getElementById("hotOffertSlideShow").value='play';
				$("a.pause").html('<img class="vPause" src="images/index/off_pause.gif" alt="stop" />');
			}
			
		});
		
		
		if(document.getElementById("hotOffertCount")){
			// gorace oferty na stronie
			//var countOffert = 1;
			var lastOffert = document.getElementById("hotOffertCount").value;
			var countOffert= Math.round(lastOffert*Math.random());
			if(lastOffert>1){
				if (countOffert==0) countOffert = 1;
				if(countOffert==1){
					$("a#left").hide();
				}
				
				if(lastOffert==1){
					$("a#right").hide();
				}
				if(countOffert==lastOffert){
					$("a#right").hide();
				}
				
				$('span#offertNoumber').html(countOffert);
				document.getElementById("hotOffertNow").value=countOffert;
			}
			else{
				$("a#right").hide();
				$("a#left").hide();
				$("a.pause").hide();
			}
		}
		
		$("a#right").click(function () {
			countOffert=countOffert+1;
			if(countOffert==1){
				$("a#left").hide();
			}
		
			if(countOffert!=1){
				$("a#left").show();
			}
			
			if(countOffert==lastOffert){
				$("a#right").hide();
			}
			if(countOffert<lastOffert){
				$("a#right").show();
			}
			 $('span#offertNoumber').html(countOffert);
			 document.getElementById("hotOffertNow").value=countOffert;
			
		});
		
		$("a#left").click(function () {
			countOffert=countOffert-1;
			if(countOffert==1){
				$("a#left").hide();
			}
		
			if(countOffert!=1){
				$("a#left").show();
			}
			
			if(countOffert==lastOffert){
				$("a#right").hide();
			}
			if(countOffert<lastOffert){
				$("a#right").show();
			}
			 $('span#offertNoumber').html(countOffert);
			 document.getElementById("hotOffertNow").value=countOffert;
		});
		
		//start wyswietlanai ofert
		if(lastOffert>1){
			setInterval("showTemp();", 5000);
		}
		
		
		$('#Screen').scrollShow({
			view:'#view',
			content:'#images',
			easing:'backout',
			wrappers:'link,crop',
			navigators:'a[id]',
			//navigationMode:'sr',
			circular:false,
			start:(countOffert-1)
		});
		
		$('#ScreenTwo').scrollShow({
			view:'#viewTwo',
			content:'#imagesTwo',
			easing:'backout',
			wrappers:'link,crop',
			navigators:'a[id]',
			//navigationMode:'sr',
			circular:false,
			start:0
		});
		
		$('#ScreenThree').scrollShow({
			view:'#viewThree',
			content:'#imagesThree',
			easing:'backout',
			wrappers:'link,crop',
			navigators:'a[id]',
			//navigationMode:'sr',
			circular:false,
			start:0
		});
		
		$('#ScreenFour').scrollShow({
			view:'#viewFour',
			content:'#imagesFour',
			easing:'backout',
			wrappers:'link,crop',
			navigators:'a[id]',
			//navigationMode:'sr',
			circular:false,
			start:0
		});
		
		//akcja przy zaladwoaniu strony
		$("div#searchSpecials").hide();
		$("div#searchNew").show();
		$("div#searchUsed").hide();
		
		$("div#advancedSearchSpecials").hide();
		$("div#advancedSearchNew").hide();
		$("div#advancedSearchUsed").hide();

		// New citroen lease car offers
		var countTwo = 3;
		if(document.getElementById("leaseCountTwo")!=null){
			var lastTwo = document.getElementById("leaseCountTwo").value;
		}
		
		if(countTwo==3){
			$("a#leftTwo").hide();
			
		}
		
		if(lastTwo==1){
			$("a#rightTwo").hide();
		}
		
		if(lastTwo==2){
			$("a#rightTwo").hide();
		}
		

		if(lastTwo==3){
			$("a#rightTwo").hide();
		}
		
		$("a#rightTwo").click(function () {
			countTwo=countTwo+1;
			if(countTwo==3){
				$("a#leftTwo").hide();
			}
		
			if(countTwo!=3){
				$("a#leftTwo").show();
			}
			
			if(countTwo==lastTwo){
				$("a#rightTwo").hide();
			}
			if(countTwo<lastTwo){
				$("a#rightTwo").show();
			}
		});
		
		$("a#leftTwo").click(function () {
			countTwo=countTwo-1;
			if(countTwo==3){
				$("a#leftTwo").hide();
			}
		
			if(countTwo!=3){
				$("a#leftTwo").show();
			}
			
			if(countTwo==lastTwo){
				$("a#rightTwo").hide();
			}
			if(countTwo<lastTwo){
				$("a#rightTwo").show();
			}
		});
		
		// New citroen car offers
		var countThree = 3;
		if (document.getElementById("leaseCountThree") != null) {
			var lastThree = document.getElementById("leaseCountThree").value;
		}
		
		if(countThree==3){
			$("a#leftThree").hide();
		}
		
		if(lastThree==1){
			$("a#rightThree").hide();
		}
		
		if(lastThree==2){
			$("a#rightThree").hide();
		}
		
		if(lastThree==3){
			$("a#rightThree").hide();
		}
		$("a#rightThree").click(function () {
			countThree=countThree+1;
			if(countThree==3){
				$("a#leftThree").hide();
			}
		
			if(countThree!=3){
				$("a#leftThree").show();
			}
			
			if(countThree==lastThree){
				$("a#rightThree").hide();
			}
			if(countThree<lastThree){
				$("a#rightThree").show();
			}
		});
		
		$("a#leftThree").click(function () {
			countThree=countThree-1;
			if(countThree==3){
				$("a#leftThree").hide();
			}
		
			if(countThree!=3){
				$("a#leftThree").show();
			}
			
			if(countThree==lastThree){
				$("a#rightThree").hide();
			}
			if(countThree<lastThree){
				$("a#rightThree").show();
			}
		});
		
		// New citroen commercial van offers
		var countFour = 3;
		if (document.getElementById("leaseCountFour") != null) {
			var lastFour = document.getElementById("leaseCountFour").value;
		}
		if(countFour==3){
			$("a#leftFour").hide();
		}
		
		if(lastFour==1){
			$("a#rightFour").hide();
		}
		
		if(lastFour==2){
			$("a#rightFour").hide();
		}
		
		if(lastFour==3){
			$("a#rightFour").hide();
		}
		
		$("a#rightFour").click(function () {
			countFour=countFour+1;
			if(countFour==3){
				$("a#leftFour").hide();
			}
		
			if(countFour!=3){
				$("a#leftFour").show();
			}
			
			if(countFour==lastFour){
				$("a#rightFour").hide();
			}
			if(countFour<lastFour){
				$("a#rightFour").show();
			}
		});
		
		$("a#leftFour").click(function () {
			countFour=countFour-1;
			if(countFour==3){
				$("a#leftFour").hide();
			}
		
			if(countFour!=3){
				$("a#leftFour").show();
			}
			
			if(countFour==lastFour){
				$("a#rightFour").hide();
			}
			if(countFour<lastFour){
				$("a#rightFour").show();
			}
		});
		// menu gorne wyszukiwarki
		$("#showSpecials").click(function () {
			$("div#searchSpecials").show();
			$("div#searchNew").hide();
			$("div#searchUsed").hide();
			
			$("div#buttonSpecials").removeClass();
			$("div#buttonNew").removeClass();
			$("div#buttonUsed").removeClass();
			
			$("div#buttonSpecials").addClass("buttonActiveRight");
			$("div#buttonNew").addClass("buttonInactiveLeft");
			$("div#buttonUsed").addClass("buttonInactiveCenter");
			
			$("div#advancedSearchSpecials").hide();
			$("div#advancedSearchNew").hide();
			$("div#advancedSearchUsed").hide();
			
			loadSpecialsModels();
		});

		$("#showNew").click(function () {
			$("div#searchSpecials").hide();
			$("div#searchNew").show();
			$("div#searchUsed").hide();
			
			$("div#buttonSpecials").removeClass();
			$("div#buttonNew").removeClass();
			$("div#buttonUsed").removeClass();

			$("div#buttonSpecials").addClass("buttonInactiveRight");
			$("div#buttonNew").addClass("buttonActiveLeft");
			$("div#buttonUsed").addClass("buttonInactiveCenter");
			
			$("div#advancedSearchSpecials").hide();
			$("div#advancedSearchNew").hide();
			$("div#advancedSearchUsed").hide();
			
			loadNewModels();
		});

		$("#showUsed").click(function () {
			$("div#searchSpecials").hide();
			$("div#searchNew").hide();
			$("div#searchUsed").show();
			
			$("div#buttonSpecials").removeClass();
			$("div#buttonNew").removeClass();
			$("div#buttonUsed").removeClass();
			
			$("div#buttonSpecials").toggleClass("buttonInactiveRight");
			$("div#buttonNew").toggleClass("buttonInactiveLeft");
			$("div#buttonUsed").toggleClass("buttonActiveCenter");
			
			$("div#advancedSearchSpecials").hide();
			$("div#advancedSearchNew").hide();
			$("div#advancedSearchUsed").hide();
			loadUsedModels();
			loadUsedYear();
		});
			
		$("a#showAdvSpecials").click(function () {
			$("div#advancedSearchSpecials").toggle();
			$("div#advancedSearchNew").hide();
			$("div#advancedSearchUsed").hide();
		});
		
		$("a#showAdvNew").click(function () {
			$("div#advancedSearchSpecials").hide();
			$("div#advancedSearchNew").toggle();
			$("div#advancedSearchUsed").hide();
		});
		
		$("a#showAdvUsed").click(function () {
			$("div#advancedSearchSpecials").hide();
			$("div#advancedSearchNew").hide();
			$("div#advancedSearchUsed").toggle();
		});
		
		$("input[name=specials_t]").change(function(){
			loadSpecialsModels();
		});
		$("#specials_model").change(function(){
			loadSpecialsDerivatives();
		});
		$("input[name=new_t]").change(function(){
			loadNewModels();
		});
		$("#new_model").change(function(){
			loadNewDerivatives();			
		});
		
		$("input[name=used_t]").change(function(){
			loadUsedModels();
		});
		$("#used_model").change(function(){
			loadUsedDerivatives();
			
		});
		activateOptionsDisabled();
		
		$('.lightbox').lightBox();
		
// zakladki dla subpage 
		$("div#generalItem").show();
		$("div#optionsItem").hide();
		$("div#techdataItem").hide();
		$("div#financeItem").hide();
		$("div#whyusItem").hide();
		$("div#whatsincludedItem").hide();
		$("div#contactItem").hide();
		
		$("a#generalLink").click(function () {
			$("div#staticViewCar").show();
			$("div#generalItem").show();
			$("div#optionsItem").hide();
			$("div#techdataItem").hide();
			$("div#financeItem").hide();
			$("div#whyusItem").hide();
			$("div#whatsincludedItem").hide();
			$("div#contactItem").hide();
			
			$("#generalLink").addClass("genActive");
			$("#generalLink").removeClass("genInactive");
			
			$("#optionsLink").addClass("optInactive");
			$("#optionsLink").removeClass("optActive");
			
			$("#techdataLink").addClass("techInactive");
			$("#techdataLink").removeClass("techActive");
			
			$("#financeLink").addClass("finInactive");
			$("#financeLink").removeClass("finActive");
			
			$("#whyusLink").addClass("whyInactive");
			$("#whyusLink").removeClass("whyActive");
			
			$("#whatsincludedLink").addClass("whatInactive");
			$("#whatsincludedLink").removeClass("whatActive");
			
			$("#contactLink").addClass("contactInactive");
			$("#contactLink").removeClass("contactActive");
			
		});
		$("a#optionsLink").click(function () {
			$("div#staticViewCar").show();
			$("div#generalItem").hide();
			$("div#optionsItem").show();
			$("div#techdataItem").hide();
			$("div#financeItem").hide();
			$("div#whyusItem").hide();
			$("div#whatsincludedItem").hide();
			$("div#contactItem").hide();
			
			$("#generalLink").addClass("genInactive");
			$("#generalLink").removeClass("genActive");
			
			$("#optionsLink").addClass("optActive");
			$("#optionsLink").removeClass("optInactive");
			
			$("#techdataLink").addClass("techInactive");
			$("#techdataLink").removeClass("techActive");
			
			$("#financeLink").addClass("finInactive");
			$("#financeLink").removeClass("finActive");
			
			$("#whyusLink").addClass("whyInactive");
			$("#whyusLink").removeClass("whyActive");
			
			$("#whatsincludedLink").addClass("whatInactive");
			$("#whatsincludedLink").removeClass("whatActive");
			
			$("#contactLink").addClass("contactInactive");
			$("#contactLink").removeClass("contactActive");
			
		});
		$("a#techdataLink").click(function () {
			$("div#staticViewCar").show();
			$("div#generalItem").hide();
			$("div#optionsItem").hide();
			$("div#techdataItem").show();
			$("div#financeItem").hide();
			$("div#whyusItem").hide();
			$("div#whatsincludedItem").hide();
			$("div#contactItem").hide();
			
			$("#generalLink").addClass("genInactive");
			$("#generalLink").removeClass("genActive");
			
			$("#optionsLink").addClass("optInactive");
			$("#optionsLink").removeClass("optActive");
			
			$("#techdataLink").addClass("techActive");
			$("#techdataLink").removeClass("techInactive");
			
			$("#financeLink").addClass("finInactive");
			$("#financeLink").removeClass("finActive");
			
			$("#whyusLink").addClass("whyInactive");
			$("#whyusLink").removeClass("whyActive");
			
			$("#whatsincludedLink").addClass("whatInactive");
			$("#whatsincludedLink").removeClass("whatActive");
			
			$("#contactLink").addClass("contactInactive");
			$("#contactLink").removeClass("contactActive");
			 
		});
		$("a#financeLink").click(function () {
			$("div#staticViewCar").hide();
			$("div#generalItem").hide();
			$("div#optionsItem").hide();
			$("div#techdataItem").hide();
			$("div#financeItem").show();
			$("div#whyusItem").hide();
			$("div#whatsincludedItem").hide();
			$("div#contactItem").hide();
			
			$("#generalLink").addClass("genInactive");
			$("#generalLink").removeClass("genActive");
			
			$("#optionsLink").addClass("optInactive");
			$("#optionsLink").removeClass("optActive");
			
			$("#techdataLink").addClass("techInactive");
			$("#techdataLink").removeClass("techActive");
			
			$("#financeLink").addClass("finActive");
			$("#financeLink").removeClass("finInactive");
			
			$("#whyusLink").addClass("whyInactive");
			$("#whyusLink").removeClass("whyActive");
			
			$("#whatsincludedLink").addClass("whatInactive");
			$("#whatsincludedLink").removeClass("whatActive");
			
			$("#contactLink").addClass("contactInactive");
			$("#contactLink").removeClass("contactActive");
			
		});
		$("a#whyusLink").click(function () {
			$("div#staticViewCar").hide();
			$("div#generalItem").hide();
			$("div#optionsItem").hide();
			$("div#techdataItem").hide();
			$("div#financeItem").hide();
			$("div#whyusItem").show();
			$("div#whatsincludedItem").hide();
			$("div#contactItem").hide();
			
			$("#generalLink").addClass("genInactive");
			$("#generalLink").removeClass("genActive");
			
			$("#optionsLink").addClass("optInactive");
			$("#optionsLink").removeClass("optActive");
			
			$("#techdataLink").addClass("techInactive");
			$("#techdataLink").removeClass("techActive");
			
			$("#financeLink").addClass("finInactive");
			$("#financeLink").removeClass("finActive");
			
			$("#whyusLink").addClass("whyActive");
			$("#whyusLink").removeClass("whyInactive");
			
			$("#whatsincludedLink").addClass("whatInactive");
			$("#whatsincludedLink").removeClass("whatActive");
			
			$("#contactLink").addClass("contactInactive");
			$("#contactLink").removeClass("contactActive");
			
		});
		$("a#whatsincludedLink").click(function () {
			$("div#staticViewCar").hide();
			$("div#generalItem").hide();
			$("div#optionsItem").hide();
			$("div#techdataItem").hide();
			$("div#financeItem").hide();
			$("div#whyusItem").hide();
			$("div#whatsincludedItem").show();
			$("div#contactItem").hide();
			
			$("#generalLink").addClass("genInactive");
			$("#generalLink").removeClass("genActive");
			
			$("#optionsLink").addClass("optInactive");
			$("#optionsLink").removeClass("optActive");
			
			$("#techdataLink").addClass("techInactive");
			$("#techdataLink").removeClass("techActive");
			
			$("#financeLink").addClass("finInactive");
			$("#financeLink").removeClass("finActive");
			
			$("#whyusLink").addClass("whyInactive");
			$("#whyusLink").removeClass("whyActive");
			
			$("#whatsincludedLink").addClass("whatActive");
			$("#whatsincludedLink").removeClass("whatInactive");
			
			$("#contactLink").addClass("contactInactive");
			$("#contactLink").removeClass("contactActive");
			
		});
		$("a#contactLink").click(function () {
			$("div#staticViewCar").hide();
			$("div#generalItem").hide();
			$("div#optionsItem").hide();
			$("div#techdataItem").hide();
			$("div#financeItem").hide();
			$("div#whyusItem").hide();
			$("div#whatsincludedItem").hide();
			$("div#contactItem").show();
			
			$("#generalLink").addClass("genInactive");
			$("#generalLink").removeClass("genActive");
			
			$("#optionsLink").addClass("optInactive");
			$("#optionsLink").removeClass("optActive");
			
			$("#techdataLink").addClass("techInactive");
			$("#techdataLink").removeClass("techActive");
			
			$("#financeLink").addClass("finInactive");
			$("#financeLink").removeClass("finActive");
			
			$("#whyusLink").addClass("whyInactive");
			$("#whyusLink").removeClass("whyActive");
			
			$("#whatsincludedLink").addClass("whatInactive");
			$("#whatsincludedLink").removeClass("whatActive");
			
			$("#contactLink").addClass("contactActive");
			$("#contactLink").removeClass("contactInactive");
		});
		
		//advanced request show
		$("div#advRequest").hide();
		$("a#advancedRequest").click(function () {
			$("div#advRequest").toggle();
		});
});

$(window).load(function() {  
	$("div#flashContainer").html('<object type="application/x-shockwave-flash" data="images/index/citroenflash.swf" width="304" height="141"><param name="movie" value="images/index/citroenflash.swf" /></object>');
	
}); 

var clickToSite;
function showTemp(){
	var lastOffert = document.getElementById("hotOffertCount").value;
	var nowOffert = document.getElementById("hotOffertNow").value;
	var clickTo = document.getElementById("hotOffertTo").value;
	var playStatus = document.getElementById("hotOffertSlideShow").value;
	
	if(playStatus == "play"){
		if((nowOffert) <= 1){
			//document.getElementById("hotOffertTo").value="right";
			clickToSite = 'right';
		}
		
		if(nowOffert == lastOffert){
			//document.getElementById("hotOffertTo").value="left";
			clickToSite = 'left';
		}
		
		if(clickToSite == "left"){
			$("a#left").click();
		}
		
		if(clickToSite == "right"){
			$("a#right").click();
		}
	}
}

function activateOptionsDisabled(){
    var sels = document.getElementsByTagName('select');
    for (var i = 0; i < sels.length; i++) {
        disabledSelects(sels[i]);
        for (var j = 0; j < sels[i].options.length; j++) {
            if (sels[i].options[j].disabled) {
                sels[i].options[j].style.color = '#CCC';
            }
			else{
				sels[i].options[j].style.color = '#000';
			}
        }
    }
}

function disabledSelects(item){
    if (item.options[item.selectedIndex].disabled) {
        for (x = 0; x < item.options.length; x++) {
            if (!item.options[x].disabled) {
                item.selectedIndex = x;
                return true;
            }
        }
    }
	return true;
}

function loadSpecialsModels(){
	// ladowanie selectow
	$("select#specials_model").html("<option value=''>Loading...</option>");
	
	$("select#specials_derivative").html("<option value=''>Loading...</option>");
	$.getJSON(baseHref+"index/index/getmodels",{specials_t: $("input[name=specials_t]:checked").val()}, function(j){
	   var options = '<option value="">Select Model</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      $("select#specials_model").html(options);
	      loadSpecialsDerivatives();
	    });	
}

function loadSpecialsDerivatives(){
	if($("#specials_model").val()!=''){
		// ladowanie selectow
		$("select#specials_derivative").html("<option value=''>Loading...</option>");
		$.getJSON(baseHref+"index/index/getderivatives",{specials_model: $("#specials_model").val(), type: $("input[name=specials_t]:checked").val()}, function(j){
	      var options = '<option value="">Select Derivative</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      $("select#specials_derivative").html(options);
	    });
	}
	else{
		var options = '<option value="">Select Derivative</option>';
		 $("select#specials_derivative").html(options);
	}
}

/* used cars*/

function loadUsedModels(){
	// ladowanie selectow
	$("select#used_model").html("<option value=''>Loading...</option>");
	$("select#used_derivative").html("<option value=''>Loading...</option>");
	$.getJSON(baseHref+"index/index/getmodelsused",{used_t: $("input[name=used_t]:checked").val()}, function(j){
	      var options = '<option value="">Select Model</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      $("select#used_model").html(options);
	      loadUsedDerivatives();
	    });	
}

function loadUsedDerivatives(){
	if($("#used_model").val()!=''){
		// ladowanie selectow
		$("select#used_derivative").html("<option value=''>Loading...</option>");
		$.getJSON(baseHref+"index/index/getderivativesused",{used_model: $("#used_model").val(), type: $("input[name=used_t]:checked").val()}, function(j){
	      var options = '<option value="">Select Derivative</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      $("select#used_derivative").html(options);
	    });
	}
	else{
		var options = '<option value="">Select Derivative</option>';
		 $("select#used_derivative").html(options);
	}
}

function loadUsedYear(){
	if($("#used_year").val()!=''){
		// ladowanie selectow
		$("select#used_year").html("<option value=''>Loading...</option>");
		$.getJSON(baseHref+"index/index/getyearused",{used_t: $("input[name=used_t]:checked").val()}, function(j){
	      var options = '<option value="">Select Year</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      $("select#used_year").html(options);
	    });
	}
	else{
		var options = '<option value="">Select Year</option>';
		 $("select#used_year").html(options);
	}
}

/**/
function loadNewModels(){

	// ladowanie selectow
	$("select#new_model").html("<option value=''>Loading...</option>");
	$("select#new_derivative").html("<option value=''>Loading...</option>");
	$.getJSON(baseHref+"index/index/getnewmodels",{new_t: $("input[name=new_t]:checked").val()}, function(j){
	      var options = '<option value="">Select Model</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      $("select#new_model").html(options);
	      loadNewDerivatives();
	    });	
}

function loadNewDerivatives(){
	if($("#new_model").val()!=''){
		
		// ladowanie selectow
		$("select#new_derivative").html("<option value=''>Loading...</option>");
		$.getJSON(baseHref+"index/index/getnewderivatives",{new_model: $("#new_model").val(), type: $("input[name=new_t]:checked").val()}, function(j){
	      var options = '<option value="">Select Derivative</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	      }
	      $("select#new_derivative").html(options);
	    });
	}
	else{
		var options = '<option value="">Select Derivative</option>';
		 $("select#new_derivative").html(options);
	}
}

function setLeasePrice(price, instalments, id){
	// sets top price, price, price + vat, initial payment, initial payment + vat
	$("#offer_id").val(id);
	$("input#priceRecalculate").val(price);
	var priceGetRecalc = price;
	var priceGetCheck = $("input#checkPrice").val();
	var priceGetRad = $("input#radPrice").val();
	var instal = parseFloat(instalments)+parseFloat(3);
	var priceSum = parseFloat(priceGetRecalc) + parseFloat(priceGetCheck/(instal)) + parseFloat(priceGetRad/(instal));
	$("#topPrice").html(formatNumber(parseFloat(priceSum)))
	$("#initial_payment").html(formatNumber(parseFloat(priceSum)*3));
	$("#initial_payment_vat").html(formatNumber(parseFloat(priceSum)*3*1.15));
	$("#instalmentsCount").html(instalments);
	$("#bottomPrice").html(formatNumber(priceSum));
	$("#bottomPrice_vat").html(formatNumber(priceSum*1.15));
}

function recalculateOptPrice(item, price, optprice, name, id){
	if(item.checked == true){
		var priceAll= document.getElementById("optPrice").value;
		var priceRad= document.getElementById("radOptPrice").value;
		var sumPrice = parseFloat(priceAll)+parseFloat(optprice);
		$("input#optPrice").val(sumPrice);
		$("#bigPriceTop").html(formatNumber(parseFloat(sumPrice)+parseFloat(priceRad)));
		$('<li id="'+ id +'">' + name + '</li>').appendTo('ul#addYourOptions');
		//pobieranie opcji dla proceed to order
		var selOptions = document.getElementById("offer_options").value;
		$("input#offer_options").val(selOptions+"#"+name);
		
	}
	else{
		var priceAll= document.getElementById("optPrice").value;
		var sumPrice = parseFloat(priceAll)-parseFloat(optprice);
		var priceRad= document.getElementById("radOptPrice").value;
		$("input#optPrice").val(sumPrice);
		$("#bigPriceTop").html(formatNumber(parseFloat(sumPrice)+parseFloat(priceRad)));
		$("li#"+id).remove();
		
		var selOptions = document.getElementById("offer_options").value;
		selOptions = selOptions.replace("#"+name,""); 
		$("input#offer_options").val(selOptions);
	}
}

function addGap(item){//Dodaje Opcje GAP insurance do listy.
	if(item.checked == true){
		$('<li id="gapId">' + 'Gap insurance' + '</li>').appendTo('ul#addYourOptions');
		var selOptions = document.getElementById("offer_options").value;
		$("input#offer_options").val(selOptions+"#GAP Insurance");
	}
	else{
		$("li#"+'gapId').remove();
		var selOptions = document.getElementById("offer_options").value;
		selOptions = selOptions.replace("#GAP Insurance",""); 
		$("input#offer_options").val(selOptions);
		
	}
}

function recalculateOptPriceRad(item, price, optprice,name,val){
	
	if(item.checked == true){
		$("input#radOptPrice").val(optprice);
		var priceAll= document.getElementById("optPrice").value;
		var sumPrice = parseFloat(priceAll)+parseFloat(optprice);
		
		$("#bigPriceTop").html(formatNumber(sumPrice));
		if(val=='replace'){
			$("li#0").remove();
			$('<li id="0">' + name + '</li>').appendTo('ul#addYourOptions');
			$("input#offer_color").val(name);
		}
		if(val=='clear'){
			$("li#0").remove();
			$("input#offer_color").val('0');
		}
	}
}

function recalculateOptPriceRadLease(item, price, optprice,name,val){
	
	if(item.checked == true){
		$("input#radPrice").val(optprice);
		var priceRecalculate= document.getElementById("priceRecalculate").value;
		var priceCheck = document.getElementById("checkPrice").value;
		var instalments = $("#instalmentsCount").html();
		var allPrice = parseFloat(priceCheck)+parseFloat(optprice);
		var instal = parseFloat(instalments)+parseFloat(3);
		var sumPrice = parseFloat(priceRecalculate)+parseFloat(allPrice/instal);
		$("#bottomPrice").html(formatNumber(sumPrice));
		$("#topPrice").html(formatNumber(parseFloat(sumPrice)))
		$("#initial_payment").html(formatNumber(parseFloat(sumPrice)*3));
		$("#initial_payment_vat").html(formatNumber(parseFloat(sumPrice)*3*1.15));
		$("#bottomPrice_vat").html(formatNumber(parseFloat(sumPrice)*1.15));
		if(val=='replace'){
			$("li#0").remove();
			$('<li id="0">' + name + '</li>').appendTo('ul#addYourOptions');
			$("input#offer_color").val(name);
		}
		if(val=='clear'){
			$("li#0").remove();
			$("input#offer_color").val('0');
		}
	}
}

function recalculateLeaseOptPrice(item, price, optprice, name, id){ // zliczanie opcji, cen dla checkboxow
	
	if(item.checked == true){
		var priceCar = document.getElementById("priceRecalculate").value;
		var priceRad = document.getElementById("radPrice").value;
		var priceCheck = document.getElementById("checkPrice").value;
		var sumPrice = parseFloat(priceCheck) + parseFloat(optprice);
		var instalments = $("#instalmentsCount").html();
		var instal = parseFloat(instalments)+parseFloat(3);
		$("input#checkPrice").val(sumPrice);
		var sumAll = parseFloat(sumPrice/instal)+ parseFloat(priceRad/instal)+ parseFloat(priceCar);
		$("#initial_payment").html(formatNumber(parseFloat(sumAll)*3));
		$("#initial_payment_vat").html(formatNumber(parseFloat(sumAll)*3*1.15));
		$("#bottomPrice_vat").html(formatNumber(parseFloat(sumAll)*1.15));
		
		$("#topPrice").html(formatNumber(sumAll));
		$("#bottomPrice").html(formatNumber(sumAll));
		$('<li id="'+ id +'">' + name + '</li>').appendTo('ul#addYourOptions');
		
		//pobieranie opcji dla proceed to order
		
		var selOptions = document.getElementById("offer_options").value;
		$("input#offer_options").val(selOptions+"#"+name);
	}
	else{
		
		var priceCar = document.getElementById("priceRecalculate").value;
		var priceRad = document.getElementById("radPrice").value;
		var priceCheck = document.getElementById("checkPrice").value;
		var sumPrice = parseFloat(priceCheck) - parseFloat(optprice);
		var instalments = $("#instalmentsCount").html();
		var instal = parseFloat(instalments)+parseFloat(3);
		$("input#checkPrice").val(sumPrice);
		var sumAll = parseFloat(sumPrice/instal)+ parseFloat(priceRad/instal)+ parseFloat(priceCar);
		$("#initial_payment").html(formatNumber(parseFloat(sumAll)*3));
		$("#initial_payment_vat").html(formatNumber(parseFloat(sumAll)*3*1.15));
		$("#bottomPrice_vat").html(formatNumber(parseFloat(sumAll)*1.15));
		
		$("#topPrice").html(formatNumber(sumAll));
		$("#bottomPrice").html(formatNumber(sumAll));
		$("li#"+id).remove();
		
		var selOptions = document.getElementById("offer_options").value;
		selOptions = selOptions.replace("#"+name,""); 
		$("input#offer_options").val(selOptions);
		
	}
}

function recalculateLeaseOptPriceSmall(){
	var priceCar= document.getElementById("optPrice").value;
	var priceOpt= document.getElementById("onlyCheckPrice").value;
	var instalments = $("#instalmentsCount").html();
	$("#bigPriceOpt").html(formatNumber(parseFloat(priceCar)+parseFloat(priceOpt/instalments)));
}

function recalculateNewPrice(item, price, metallic){
	if(item.checked == true){
		setNewPrice(parseFloat(metallic)*1.15+parseFloat(price) );
	}
	else{
		setNewPrice(price);
	}
}

function setNewPrice(price){
	$("#bigPriceBottom").html(formatNumber(price));
	$("#bigPriceTop").html(formatNumber(price));
	
}

function depositSetDefault(price){
	var depositTempPrice = formatNumberNoComa(price * (25/100));
	$("#depositInput").val(depositTempPrice);
	var balancetofinance = parseFloat(price) - parseFloat($("#depositInput").val());
	var multipler = parseFloat($("#calcTerm").val()) / 12 * 5.9 / 100;
	$("#calcResult").html(formatNumber(( parseFloat(balancetofinance) + parseFloat(balancetofinance) * parseFloat(multipler)) / parseFloat($("#calcTerm").val())));
}

function goToLabel(){
	window.location.href = $(this).attr('href');
}

function recalculateHirePurchase(price){
	var conDepositInput = formatNumberNoComa($("#depositInput").val());
	$("#depositInput").val(conDepositInput); 
	if(isNaN(parseInt( $("#depositInput").val()))){
		alert("You have to fill a deposit");
		return;
	}
	var balancetofinance = parseFloat(price) - parseFloat($("#depositInput").val());
	var multipler = parseFloat($("#calcTerm").val()) / 12 * 5.9 / 100;
	$("#calcResult").html(formatNumber(( parseFloat(balancetofinance) + parseFloat(balancetofinance) * parseFloat(multipler)) / parseFloat($("#calcTerm").val())));
	var apr = parseFloat(balancetofinance)* (9.9/100)*(parseFloat($("#calcTerm").val()/12));
	
	$("#totalResult").html(formatNumber(parseFloat(balancetofinance) + parseFloat(balancetofinance) * (5.9/100)+ apr));
}

function SpecialsLoadSimilar(link){
	$("body").css('cursor','wait');
    link = link.replace("#INSTALMENTS#", $("#instalments").val());
    link = link.replace("#MILEAGE#", $("#mileage").val());
    link = link.replace("#GAP#", $("#gap").attr("checked")?1:0);
    link = link.replace("#METALLIC#", $("#metallic").attr("checked")?1:0);
	
    $.getJSON(link, function(result){
    	$("body").css('cursor','auto');
        if (result) {			
        	setLeasePrice(result['price'], result['instalments'], result['offer_id']);
			for(j=0; j<document.getElementById("instalments").options.length;j++){
				document.getElementById("instalments").options[j].disabled=true;
				for(i=0; i<result['aAllAvailableInstalments'].length;i++){
					if(result['aAllAvailableInstalments'][i]==$(document.getElementById("instalments").options[j]).html()){	
						document.getElementById("instalments").options[j].disabled=false;
					}
				}
			}
			
			for(j=0; j<document.getElementById("mileage").options.length;j++){
				document.getElementById("mileage").options[j].disabled=true;
				for(i=0; i<result['aAllAvailableMileages'].length;i++){
					if(result['aAllAvailableMileages'][i]==$(document.getElementById("mileage").options[j]).html()){
						document.getElementById("mileage").options[j].disabled=false;
					}
				}
			}
			activateOptionsDisabled();
        }
    });
}

function formatNumber (anynum) {
	decimal = 2;
    divider =100;
    workNum=Math.abs((Math.round(anynum*divider)/divider));
    workStr=""+workNum
    if (workStr.indexOf(".")==-1){workStr+="."}
	dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
	pStr=workStr.substr(workStr.indexOf("."))
	while (pStr.length-1< decimal){pStr+="0"}
	if(pStr =='.') pStr ='';
	//--- Adds a comma in the thousands place.   
	if (dNum>=1000) {
	dLen=dStr.length
	dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
	} 
	retval = dStr + pStr
	//-- Put numbers in parentheses if negative.
	if (anynum<0) {retval="("+retval+")";}
	return retval;
}

function formatNumberNoComa (anynum) {
	decimal = 2;
    divider =100;
    workNum=Math.abs((Math.round(anynum*divider)/divider));
    workStr=""+workNum
    if (workStr.indexOf(".")==-1){workStr+="."}
	dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
	pStr=workStr.substr(workStr.indexOf("."))
	while (pStr.length-1< decimal){pStr+="0"}
	if(pStr =='.') pStr ='';
	retval = dStr + pStr
	//-- Put numbers in parentheses if negative.
	if (anynum<0) {retval="("+retval+")";}
	return retval;
}

function loadCustomerType(item){
	$("#applyform").html('<div class="loading">Loading...</div>');
	$("#applyform").load(changeCustomerLink.replace("#TYPE#", $(item).val()));
}

function previousAddressChange(item){
	if( parseInt($(item).val()) > 2 ){
		$("#previousAddressDiv").hide();
	}
	else{
		$("#previousAddressDiv").show();
	}
}

function requestAquoteGetDerivatives(make,deriv){
if(make!=''){
		// ladowanie selectow
		$("select#requestAquoteDerivative").html("<option value=''>Loading...</option>");
		$.getJSON(baseHref+"index/index/getrequestderivatives",{new_model: make}, function(j){
	      var options = '';
	      for (var i = 0; i < j.length; i++) {
	    	 if(j[i].optionValue==deriv){
	    		 options += '<option selected="selected" value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	    	 }
	    	 if(j[i].optionValue!=deriv){
	    		 options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	    	 }
	    	}
	      $("select#requestAquoteDerivative").html(options);
	    });
	}
	else{
		var options = '<option value="">Select Derivative</option>';
		 $("select#requestAquoteDerivative").html(options);
	}
}

//Funkcja dla request a quote dla general

function requestAquoteGetDerivativesReq(make,deriv){
	if(make!=''){
			// ladowanie selectow
			$("select#requestAquoteDerivativeReq").html("<option value=''>Loading...</option>");
			$.getJSON(baseHref+"index/index/getrequestderivatives",{new_model: make}, function(j){
		      var options = '';
		      for (var i = 0; i < j.length; i++) {
		    	 if(j[i].optionValue==deriv){
		    		 options += '<option selected="selected" value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		    	 }
		    	 if(j[i].optionValue!=deriv){
		    		 options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		    	 }
		    	}
		      $("select#requestAquoteDerivativeReq").html(options);
		    });
		}
		else{
			var options = '<option value="">Select Derivative</option>';
			 $("select#requestAquoteDerivativeReq").html(options);
		}
	}

function requestAquoteGetDerivativesUsed(make,deriv){
	if(make!=''){
		// ladowanie selectow
		$("select#requestAquoteDerivative").html("<option value=''>Loading...</option>");
		$.getJSON(baseHref+"index/index/getderivativesused",{used_model: make, type: $("input[name=used_t]:checked").val()}, function(j){
	      var options = '';
	      for (var i = 0; i < j.length; i++) {
	    	  if(j[i].optionValue==deriv){
		    		 options += '<option selected="selected" value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		    	 }
	    	  if(j[i].optionValue!=deriv){  
	    		  options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	    	  }
	    	  }
	      $("select#requestAquoteDerivative").html(options);
	    });
	}
	else{
		var options = '<option value="">Select Derivative</option>';
		 $("select#requestAquoteDerivative").html(options);
	}
}

//Request a quote dla general dla usedcars
function requestAquoteGetDerivativesReqUsed(make,deriv){
	if(make!=''){
			// ladowanie selectow
			$("select#requestAquoteDerivativeReq").html("<option value=''>Loading...</option>");
			$.getJSON(baseHref+"index/index/getderivativesused",{used_model: make, type: $("input[name=used_t]:checked").val()}, function(j){
		      var options = '';
		      for (var i = 0; i < j.length; i++) {
		    	 if(j[i].optionValue==deriv){
		    		 options += '<option selected="selected" value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		    	 }
		    	 if(j[i].optionValue!=deriv){
		    		 options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		    	 }
		    	}
		      $("select#requestAquoteDerivativeReq").html(options);
		    });
		}
		else{
			var options = '<option value="">Select Derivative</option>';
			 $("select#requestAquoteDerivativeReq").html(options);
		}
	}

//pobieranie derivatives dla usedcars
function setselect(name){
	name = name.replace(/ /gi, "-");
	//$("form#ModelForm").attr("action","search/n/"+name); 
	$("form#ModelForm").attr("action",name);
	
}