$(document).ready(function(){
    $("#listSelect").change(function () {
        var str = "";
        $("select option:selected").each(function () {
      	   if($(this).attr('id')=='resetSelect'){
      		   return false;
      	   }else if($(this).attr('id')=='addToMZ'){
  	    	   if($("input:checked").length == 0){
  					  $("#errorMZ").attr("style","display: block");
  					  $("#errorCompare").attr("style","display: none");
  	              }else{
  	            	  MZ($("#getCarsInActPage").text(),'addList',0);
  	              }
  		   }else if($(this).attr('id')=='removeFromMZ'){
  			   if($("input:checked").length == 0){
  					  $("#errorMZ").attr("style","display: block");
  					  $("#errorCompare").attr("style","display: none");
  	              }else{
  	            	  MZ($("#getCarsInActPage").text(),'removeList',0);
  	              }
  		   }else if($(this).attr('id')=='doCompare'){
  		    if($("input:checked").length == 0 || $("input:checked").length > 3){
  				  $("#errorCompare").attr("style","display: block");
  				  $("#errorMZ").attr("style","display: none");
                }else{
  				   vtpcompare($("#getCarsInActPage").text(),'addList',10,$("#getCurrentComparisons").text(),$("#getMaxComparisons").text());
  				   var href = $("#buttonCompare").attr("href") + document.p.ixList.value;
  				   var alt = $("#buttonCompare").attr("title");
  				   href = alt + document.p.ixList.value;
  				   $("#buttonCompare").attr("href",href);
  				   $("#buttonCompare").trigger('click');
               }
  		   }
  	       $("#resetSelect").attr("selected","selected");
         }
        );
      });
});

function STD(i,v)
{ 
  document.p.ixList.value =i;
  document.p.v.value = v;
  document.p.act.value = "offer";
  document.p.submit();
}

function Sei(i)
{ 
  document.p.act.value = "list";
  document.p.page.value = i;
  document.p.submit();
}

function ResultSortL(sortBy,sortOrder,action)
{
//  document.p.act.value = action;
  document.p.sortBy.value 	 = sortBy;
  document.p.sortOrder.value = sortOrder;
  document.p.submit();
}

function MZ(i,action,v)
{ // Merkzettel: Fzg. hinzufuegen / loeschen
  document.p.notepad.value = action;
  document.p.v.value = v;
  var cars="";
  if(action=="addList"||action=="removeList"){
    if(document.p.car)
    {
  	 if(i>1){
	  	for(var j=(i-1);j>=0;j--){
			if(document.p.car[j].checked)
				cars+="$"+ document.p.car[j].value;
	  	}
  	 }else{
		if(document.p.car.checked)
		 cars+="$"+ document.p.car.value;
  	 }
  	}
  	//if (cars=="")  //auch wenn kein Fzg. gewaehlt wurde, kommt eine Fehlermeldung
  	//  return; 
  	document.p.ixList.value =cars;
  }else{
	  document.p.ixList.value =i;
  }

  document.p.act.value = "notepad";
//  document.p.ixList.value = flagListe();
  document.p.action += "?show=";
  document.p.action += getViewStat();  
  document.p.submit();
    
}

function vtpcompare(i,action,v,current,max)
{
  document.p.compare.value = action;
  var cars="";
  current=0;
  if(action=="addList"||action=="removeList"){
    if(document.p.car)
    {
  	 if(i>1){
	  	  for(var j=0;j<i;j++){
			if(document.p.car[j].checked){
				cars+="c"+ document.p.car[j].value;
				current++;
			}
	  	}
  	 }else{
		if(document.p.car.checked)
		 cars+="c"+ document.p.car.value;
  	 }
  	}
  	//if (cars=="")   //auch wenn kein Fzg. gewaehlt wurde, kommt eine Fehlermeldung
  	//  return; 
  	document.p.ixList.value =cars;
  }else{
	  document.p.ixList.value =i;
  }
}

function compare(i,action,v,current,max)
{
  document.p.compare.value = action;
  var cars="";
  current=0;
  if(action=="addList"||action=="removeList"){
    if(document.p.car)
    {
  	 if(i>1){
	  	  for(var j=0;j<i;j++){
			if(document.p.car[j].checked){
				cars+="$"+ document.p.car[j].value;
				current++;
			}
	  	}
  	 }else{
		if(document.p.car.checked)
		 cars+="$"+ document.p.car.value;
  	 }
  	}
  	//if (cars=="")   //auch wenn kein Fzg. gewaehlt wurde, kommt eine Fehlermeldung
  	//  return; 
  	document.p.ixList.value =cars;
  }else{
	  document.p.ixList.value =i;
  }

//  document.p.ixList.value = flagListe();
  document.p.act.value = "compare";
  document.p.v.value = v;  
  document.p.submit();
      	
}

function showCarDetail(i)
{
  //Fahrzeug: anzeigen
  document.p.act.value = "offer";
  document.p.cid.value = i;
  document.p.submit();
}

function showCarLead(i)
{
  //Anfrage zum Fahrzeug aus Vergleich
  document.p.act.value = "lead";
  document.p.cid.value = i;
  document.p.v.value = 11;
  document.p.submit();
}

function showCarEnquiry(i)
{
  //Anfrage zum BRG Fahrzeug aus Vergleich
  document.p.act.value = "enquiry";
  document.p.cid.value = i;
  document.p.v.value = 0;
  document.p.submit();
}

function showCompBack()
{
  //zurueck zur letzten Aktion
  document.p.act.value = "compare";
  document.p.compare.value = "back";
  document.p.submit();
}

function showListBack()
{
  //zurueck zur letzten Aktion
  document.p.act.value = "list";
  document.p.selfAction.value = "back";
  document.p.submit();
}

function lines(i)
{ 
  document.p.ixList.value = flagListe();
  document.p.act.value = "list";
  document.p.lines.value = i; //angezeigte fzg. pro seite
  document.p.submit();
}

function flagListe()
{
  if(!document.p.car)
    return 0;
  var stat = 0;
  for (i=document.p.car.length-1;i>=0;i--)
  { stat <<= 1;
	if(document.p.car[i].checked)
	  stat |= 1;
  }
  return stat;
}

function checkItem(url)
{
  document.images.dummybild.src = url;
}

function mi(cell){cell.bgColor = '#E1E1E1';}
function mo(cell,color){cell.bgColor=(color)?color:'#FFFFFF';}
