function gotoSuche() {
	window.location.href = 'index.html';	
}
function gotoTreffer() {
	document.forms[0].action = "ergebnisse.html";
	document.forms[0].target = "_self";
	document.forms[0].submit();
}

function gotoFinanzierung(anzeige) {
	
	window.location.href = 'finanzierung.html?DETAIL='+anzeige;		
}

function gotoGarage() {
	window.location.href = "garage.html";
}

function OpenBemerkung() {
	document.getElementById("bemerkung").style.display = "inline";
}

function get_cookie(cookieName) {
	strValue = false;
	if (strCookie = document.cookie) {
		if (arrCookie = strCookie
				.match(new RegExp(cookieName + '=([^;]*)', 'g'))) {
			strValue = RegExp.$1;
		}
	}
	return (strValue);
}

/*
function setAnzahl(title) {
	if (get_cookie('anz_'+title))
		anz = get_cookie('anz_'+title)
	else
		anz = 0;
	if (document.getElementById('anzahl'))
		document.getElementById('anzahl').innerHTML = '<strong>' + anz
				+ '</strong>';
}*/

function OpenGarage(detail) {
	$.ajax( {
		url: "/ajax/parken.php?DO=PARKEN&ANZEIGE_ID="+detail,
		type : "GET",
		success : function handleResponseModell(reqCode) {
			$('#anzahl').text(reqCode); 
			if ($('#'+detail).size() > 0)
				$('#'+detail).text('Fahrzeug ist im Warenkorb'); 
		}
	}); 
	

}

function deleteFZ(anzeige) {
	$.ajax( {
		url: "/ajax/parken.php?DO=UNPARKEN&ANZEIGE_ID="+detail,
		type : "GET",
		success : function handleResponseModell(reqCode) {
			$('#anzahl').text(reqCode); 
		}
	}); 
}

function emptyGarage(title) {
	$.ajax( {
		url: "/ajax/parken.php?DO=UNPARKEN_ALL",
		type : "GET",
		success : function handleResponseModell(reqCode) {
			$('#anzahl').text(reqCode); 
		}
	}); 
}
function vergleiche() {
	// TODO wenn keine checkbox selected
	if (checkloop()) {
		document.forms[0].submit();
	}
}
function checkloop() {
	var checked = false; 
	if ($('input[type="checkbox"]').size() < 1)
		return true; 
	$('.fahrzeuge').each(function (){
		if ($(this).attr('checked')) {
			checked = true; 
		}
	});
	
	if (checked) {
		$('#error').html('');
		return true;
	} else {
		alert(unescape("Kein Fahrzeug ausgew%E4hlt!")); 
		return false;
	}
}

function OpenGalerie(anz, sprache, user) {
	
	var PopXXL = window.open('galerie.html?showid=' + anz + '&sprache='
			+ sprache + '&boerse=opel&USER='+user, 'Fotogalerie',
			'width=710,height=700,toolbar=no');
	PopXXL.resizeTo(710, 700);
	PopXXL.moveTo(0, 0);
	PopXXL.focus();
}

function OpenVideo(anzeige, haendler) {
	var VideoXL = window.open('video.html?aid=' + anzeige + '&hid=' + haendler, 'Videoanzeige',
			'width=380,height=325,toolbar=no');
	VideoXL.moveTo(0, 0);
	VideoXL.focus();
}

function umkreis() {
	if (document.getElementById('PLZ')) {
		if (document.getElementById('PLZ').value == "") {
			document.getElementById('UMKREIS').disabled = true;
			document.getElementById('UMKREIS').style.color = 'grey';
			if (document.getElementById('BUNDESLAND')) {
				document.getElementById('BUNDESLAND').disabled = false;
				document.getElementById('BUNDESLAND').style.color = 'black';
			}
		} else {
			document.getElementById('UMKREIS').disabled = false;
			document.getElementById('UMKREIS').style.color = 'black';
			if (document.getElementById('BUNDESLAND')) {
				document.getElementById('BUNDESLAND').disabled = true;
				document.getElementById('BUNDESLAND').style.color = 'grey';
			}
		}
	}
}

function show_details(anzeige) {	
	if ($('#CADC').val() != '' && $('#CADC').size() > 0 )
		window.location.href = 'details.html?DETAIL='+anzeige+'&CADC='+$('#CADC').val()+'#anker';
	else
		window.location.href = 'details.html?DETAIL='+anzeige;	 
	
}

function openKontakt(anzeige, sprache) {
	fenster = window.open('kontakt.php?DETAIL='+anzeige+"&SPRACHE="+sprache, 'kontakt', 'height=700,width=900');
	fenster.focus();
	fenster.moveTo(70, 70);
}

function suchauftrag(what) {
	if (what == 0) {
		document.forms['suchmaske'].action = 'suchauftrag.php';
		document.forms['suchmaske'].target = 'suchauftrag';
		fenster = window.open('about:blank', 'suchauftrag',
				'height=800,width=900');
		fenster.focus();
		fenster.moveTo(70, 70);
		document.forms['suchmaske'].onsubmit = fenster;
		document.forms['suchmaske'].submit();
	} else {
		fenster = window.open('about:blank', 'suchauftrag',
				'height=800,width=900');
		fenster.focus();
		fenster.moveTo(70, 70);
		document.getElementById('ACTION').value = what;
		document.getElementById('SUCHAUFTRAG').value = 1;
		document.forms['suchauf'].submit();

	}
}

function goback(parameter) {
	opener.document.getElementById("ACTION").value = '';
	opener.document.getElementById("SUCHAUFTRAG").value = 0;
	window.close();
}

function setColor() {
	uncheckAll();
	var elements = document.getElementsByTagName('a');
	for ( var i = 0; i < elements.length; i++)
		elements[i].style.color = '#333';

}

function uncheckAll() {
	for ( var i = 0; i < opener.document.forms['suchmaske'].elements.length; i++) {
		opener.document.forms['suchmaske'].elements[i].checked = false;
	}
}

function del(code) {
	document.getElementById('CODE').value = code;
	suchauftrag('delete');
}

function allow(email, code) {
	document.getElementById('EMAIL').value = email;
	document.getElementById('CODE').value = code;
	document.getElementById('ACTION').value = 'allow';
	document.getElementById('SUCHAUFTRAG').value = 1;
	suchauftrag(0);

}

function logout() {
	document.forms[0].target='_self';
	document.forms[0].action='login.php'; 
	document.forms[0].submit();

}

function berechne() {
	document.forms['smartbuy'].submit();

}

function reserviereFZ(anzeige) {
	document.getElementById('RESERVE').value = anzeige;
	document.forms[0].target = "_self";
	document.forms[0].action = '';
	document.forms[0].submit();

}

function reserviere() {
	if (checkloop()) {
		document.getElementById('RESERVE').value = '1';
		document.forms[0].target = "_self";
		document.forms[0].action = '';
		document.forms[0].submit();
	} 

}

function buyonline(detail) {
	document.getElementById('DETAIL').value = detail;
	document.forms['garage'].action = 'kaufen.php';
	document.forms['garage'].target = 'kaufen';
	fenster = window.open('about:blank', 'kaufen',
			'height=1000,width=870,scrollbars=yes');
	fenster.focus();
	fenster.moveTo(70, 70);
	document.forms['garage'].onsubmit = fenster;
	document.forms['garage'].submit();

}

function buy(debug) {
	
    	document.forms[0].action = 'kaufen.php';
    	
    
	
	if (checkloop()) {
		
		document.forms[0].target = 'kaufen';
		fenster = window.open('about:blank', 'kaufen',
				'height=1000,width=870,scrollbars=1');
		fenster.focus();
		fenster.moveTo(70, 70);
		document.forms[0].onsubmit = fenster;
		document.forms[0].submit();
	} else {
		return false; 
		
	}

}


function deleteFZ() {
	if (checkloop()) {
		document.getElementById('DELETE').value = '1';
		document.forms[0].target = "_self";
		document.forms[0].action = '';
		document.forms[0].submit();
	}

}		 


$(document).ready(function(){
	// Carousel initialisieren
	if ($('#paketteaser').size() > 0 ){
		$('#paketteaser').jcarousel({
			visible: 4
		});
	}
	
	
		$("#checkall").click(function()	{
				var checked_status = this.checked;
				$("input[type=checkbox]").each(function(){
					this.checked = checked_status;
				});
			});		
		
		$("#GOTOTREFFER").click(function() {
			/*alert("/ajax/getTreffer.php?"+$('#suchmaske').serialize());
			return false; 
			*/
			$.ajax( {
				url: "/ajax/getTreffer.php?"+$('#suchmaske').serialize(),
				type : "GET",
				success : function handleResponseModell(reqCode) {
					if ($('#CADC').val() != '' && $('#CADC').length > 0)
						window.location.href = 'ergebnisse.html?OFFSET=0&CADC='+$('#CADC').val();		
						else
						window.location.href = 'ergebnisse.html?OFFSET=0';
					}
			});
		});
		
		$("#RESET").click(function() {
			$.ajax( {
				url: "/ajax/getTreffer.php?ACTION=RESET",
				type : "GET",
				success : function handleResponseModell(reqCode) {
						window.location.href = 'index.html';	
					}
			});
		});
		
		
		$('#SPRACHE, #ORDER').change(function(){
			$.ajax( {
				url: "/ajax/getTreffer.php?"+$('form').serialize()+"&SPRACHE="+$('#SPRACHE').val(),
				type : "GET",
				success : function handleResponseModell(reqCode) {
						window.location.reload();	
					}
			});
			
		});	
		
		
		
		$('#PAK').click(function(){
			if ($('form[name="garage"]').length > 0 ){
				if (checkloop()) {
				document.forms['garage'].action = 'addvehicle.php' ; 
				document.forms['garage'].target = 'paket';
				fenster = window.open('about:blank', 'paket',
				'height=1000,width=1000,scrollbars=1');
				fenster.focus();
				fenster.moveTo(0, 0);
				document.forms['garage'].onsubmit = fenster;
				document.forms['garage'].submit();
				} 
			} else {
				fenster = window.open('addvehicle.php', 'paket',
				'height=1000,width=1000,scrollbars=1');
				fenster.focus();
			}
			
		});

		$("#NEWPAK").click(function() {
			$.ajax( {
				url: "/ajax/paketiere.php?ACTION=NEWPAK",
				type : "GET",
				success : function handleResponseModell(reqCode) {
						$('#paketeadd').append(reqCode); 		
					}
			});
		});
		
		$("#ZUORD").click(function() {
			if ($('input[name="dealer"]').val() == '')
				var dealer = 'empty';
			else
				var dealer = $('input[name="dealer"]').val(); 
			$.ajax( {
				url: "/ajax/paketiere.php?ACTION=ZUORD&DEALER="+dealer+"&PAKETNAME="+$('input[name="package"]').val(),
				type : "GET",
				success : function handleResponseModell(reqCode) {
					$('#HEMAIL').val('');
					$('#PUBLISH').attr("disabled", false); 
					if (reqCode == 'notfound'){
						$('.error').html('Fehler: H&auml;ndler '+dealer+' wurde nicht gefunden');
						$('#PUBLISH').attr("disabled", true); 
					} else if (reqCode != 0) {
						$('#HEMAIL').val(reqCode);
						$('.error').html('Hinweis: H&auml;ndler '+dealer+' wurde Paket zugeordnet'); 
					} else {
						$('.error').html(''); 
						$('#PUBLISH').attr("disabled", true);
					}
				  		
				}
			});
		});
		
		
		$(".DELPAK").click(function() {
			Check = confirm(unescape("Wollen Sie dieses Paket l%F6schen?"));
			if (Check == false) 
				return false; 
			$.ajax( {
				url: "/ajax/paketiere.php?ACTION=DELPAK&PAKETNAME="+$(this).attr('id'), //id weil val() in #*+$§+* -IE7 bei Buttons nicht geht
				type : "GET",
				async:false,  //FF 
				success : function handleResponseModell(reqCode) {
						window.location.reload(); 
					}
			});
			 		
		});
		
		$("#ADDFZ").click(function() {
			
			if ($('input[name="paket"]:checked').size() > 0 ){
				$('#loader').show();
				$.ajax( {
					url: "/ajax/paketiere.php?ACTION=ADDFZMULTIPLE&FAHRZEUGE="+$('#fzids').val()+"&PAKETNAME="+$('input[name="paket"]:checked').val(),
					type : "GET",
					//async:false,  //FF 
					success : function handleResponseModell(reqCode) {
					}
				});
				window.close();
				window.opener.location.reload(true);
			} else {
				alert(unescape("Kein Paket ausgew%E4hlt!")); 
			}
		});
		
		$("#DELFZ").click(function() {	
			Check = confirm(unescape("Wollen Sie diese Fahrzeuge aus dem Paket l%F6schen?"));
			if (Check == false) 
				return false; 
				$('.fahrzeuge').each(function (){  
					if ($(this).attr('checked') ){	
						var fahrzeug = $(this).val(); 
						$.ajax( {
							url: "/ajax/paketiere.php?ACTION=DELFZ&FAHRZEUG="+fahrzeug+"&PAKETNAME="+$('input[name="package"]').val(),
							type : "GET",
							async:false,  //FF 
							success : function handleResponseModell(reqCode) {
									
							}
						});
					};
				}); 
			
			window.location.reload(); 
		});
		
		
		$("#PUBLISH").click(function() {			
			if ($('.fahrzeuge').size() > 0 ){
				if ($('input[name="dealer"]').val() == '') {
					$('.error').html('Fehler: Dealercode darf nicht leer sein'); 
					return false; 
				}
				$.ajax( {
					url: "/ajax/paketiere.php?ACTION=PUBLISH&DEALER="+$('input[name="dealer"]').val()+"&PAKETNAME="+$('input[name="package"]').val()+"&HEMAIL="+$('input[name="HEMAIL"]').val(),
					type : "GET",
					success : function handleResponseModell(reqCode) {
							$('.error').html(reqCode);  
							$('#DEPUBLISH').attr("disabled", false); 
							$('#PUBLISH').attr("disabled", true); 
							$('#DELFZ').attr("disabled", true); 
						}
				});
			
			} else {
				alert('Fehler: Paket darf nicht leer sein'); 
				
			}
		
		});
		
		
		
		$("#DEPUBLISH").click(function() {
			$.ajax( {
				url: "/ajax/paketiere.php?ACTION=DEPUBLISH&DEALER="+$('input[name="dealer"]').val()+"&PAKETNAME="+$('input[name="package"]').val(),
				type : "GET",
				async:false,  //FF 
				success : function handleResponseModell(reqCode) {
						
					}
			});
			window.location.reload(); 
		});
		
		
		$("#DENYFZ").click(function() {	
			Check = confirm(unescape("Wollen Sie dieses Fahrzeuge ablehnen?"));
			if (Check == false) 
				return false; 
				$('.fahrzeuge').each(function (){  
					if ($(this).attr('checked') ){	 
						fahrzeug = $(this).val(); 
						$.ajax( {
							url: "/ajax/paketiere.php?ACTION=DENYFZ&FAHRZEUG="+$(this).val()+"&PAKETNAME="+$('input[name="package"]').val(),
							type : "GET",
							async:false,  //FF 
							success : function handleResponseModell(reqCode) {
								 
							}
						});
					};
				}); 
				window.location.reload(); 
		});
		
		
		$("#DENYPAK").click(function() {
			Check = confirm(unescape("Wollen Sie dieses Paket ablehnen?"));
			if (Check == false) 
				return false; 
			$.ajax( {				
				url: "/ajax/paketiere.php?ACTION=DENYPAK&PAKETNAME="+$('input[name="package"]').val(),
				type : "GET",
				success : function handleResponseModell(reqCode) {
						$('.error').html(reqCode);  	
					}
			});
		});
		
		
		$("#ACCPAK").click(function() {
			//wenn abgelehnte FZ, dann Mail an Verkäufer über abgelehnte FZ
			//wenn nicht, starte Kaufvorgang und Mail an Verkäufer: Paket akzeptiert
			var denied = false; 
			
			$('.fahrzeuge').each(function (){
				// wenn ein Fahrzeug abgelehnt
				if ($(this).attr('disabled')) {
					denied = true; 
				}			
			}); 
			
			if (denied) {
				 
				$.ajax( {				
					url: "/ajax/paketiere.php?ACTION=ACCPAK&PAKETNAME="+$('input[name="package"]').val(),
					type : "GET",
					success : function handleResponseModell(reqCode) {
							$('.error').html(reqCode);  	
						}
				});				
			}else {
				$("input[type=checkbox]").each(function(){
					this.checked = true;
				});
				buy();
			}
			//TODO
			// wenn kauf erfolgreich, Inputs disablen beim Händler und Status auf Accepted
		}); 		
});



function senden(seite){
	$('#OFFSET').val(seite);
	$.ajax( {
		url: "/ajax/getTreffer.php?"+$('#trefferliste').serialize()+"&SPRACHE="+$('#SPRACHE').val(),
		type : "GET",
		success : function handleResponseModell(reqCode) {
			if ($('#CADC').val() != '')
				window.location.href = 'ergebnisse.html?OFFSET='+seite+'&CADC='+$('#CADC').val();		
			else
				window.location.href = 'ergebnisse.html?OFFSET='+seite;	
			}
	});
}


function GetWM24Modelle(sprache, selected, cadc){
	 
	$('#ANZEIGE_MODELL_ID option[value="0"]').attr('selected', true);						
	var marke	= document.getElementById("ANZEIGE_MARKE_ID").options[document.getElementById("ANZEIGE_MARKE_ID").selectedIndex].value;	
	var data = "marke="+marke+"&sprache="+sprache+"&selected="+selected+"&cadc="+cadc;

	$('#loader').attr("src", "./images/ajax-loader.gif");
	$("#ANZEIGE_MODELL_ID").attr('disabled', 'disabled');
	$.ajax ({
		url: "/ajax/get_modelle.php",
		type: "GET",
		data: data,
		success: function handleResponseModell(reqCode){
			var modelle	= reqCode.split(",");
			var selected = 0;
			for(var i=document.getElementById("ANZEIGE_MODELL_ID").options.length-1; i>=0; i--){
				document.getElementById("ANZEIGE_MODELL_ID").options[i] = null;
			}
		
			for(var i=0; i<modelle.length; i++){
				var parts	= modelle[i].split(":");
				var sel = false;
				if  (parts[2] == 1) {
					sel = true;
					selected = parts[0];
				}
				document.getElementById("ANZEIGE_MODELL_ID").options[i] = new Option($("<div>"+parts[1]+"</div>").html(), parts[0], sel, sel);
				if (sel==true) {
					document.getElementById("ANZEIGE_MODELL_ID").options[i].selected = true;
				}
			}
			
		$('#loader').attr("src", "./images/blank.gif");
		$("#ANZEIGE_MODELL_ID").removeAttr('disabled');
		}		
	});
	
	
}


	
function genPDF(id){
	document.forms[0].action = 'includes/preisaufstellung_opel_ma.php';
	document.forms[0].target = '_blank';
	//fenster = window.open('about:blank', 'ma','height=1,width=1');
	//document.forms[0].onsubmit = fenster;
	document.forms[0].submit();
	//fenster.close();
	
	
	
}

/*
function getTrefferanzahl() {
	$.ajax( {
		url : window.location.protocol+"//"+window.location.hostname+":"+window.location.port+"/ajax/getTrefferanzahl.php?"+$('form').serialize() ,
		type : "GET",
		success : function handleResponseModell(reqCode) {
			$('#anz_treffer').text(reqCode); 
		}
	}); 
	
	
	
}*/



