var $sTimeoutObj = null;

/**
 * JS functions
 */
$(document).ready(function(){
	
	var $i = 0;
	
	$("#ansprechpartnercountry").change(function () {
		$sCountry = $(this).val();
		if ($sCountry == "Deutschland") {
			$("#plzselection").show();
			$("#categoryselection").show();
		} else {
			$("#plzselection").hide();
			$("#categoryselection").hide();
		}
	});
	
	$(".jsHSearch").click(function() {
		$sTargetDivShow = $(this).attr('jsboxshow');
		$sTargetDivHide = $(this).attr('jsboxhide');
		
		$("#"+$sTargetDivHide).hide();
		$("#"+$sTargetDivShow).show();
		
		$("#countryDefault").attr("selected", "selected");
		$("#defaultRadius").attr("selected", "selected");
		$(".nonDefaultRadius").removeAttr("selected");
		$("#plzSearch").val("");		
	});
	
	if ($(".imageContainer") && $("#jsImBoList")) {
		$(".imageContainer img").each(function (){
			$sSrc = $(this).attr("src");
			
			$oImage = new Image();
			$oImage.src = $sSrc;
			
			$sCode = '<div class="image"><div class="rahmen" style="width: 107px; height: 80px;"><div class="inlay1"><div class="inlay2"><a href="' + $sSrc + '" class="lightbox" rel="lightbox[1]" target="_self"><img src="' + $sSrc + '" border="0" alt="" title="" /></a></div></div></div></div>';			
								
			$("#jsImBoList").append($sCode);
		});
	}
	
	var $iImCnt = 0;
	var $iImAkt = 0;
	$("#jsImBoList .image").each(function () {
		$(this).addClass("imboli_" + $iImCnt);
		$iImCnt = $iImCnt + 1;
		if ($iImCnt > 3) {
			$(this).hide();
		}
	});
	$iImCnt = 0;
	
	$("#jsImBoList img").each(function() {
		$sThisSrc = $(this).attr("src");
		$sThisDetail = $(this).attr("detail");
		$sThisZoom = $(this).attr("zoom");
		if (!$(this).attr("detail"))
			$sThisDetail = $sThisSrc;
		if (!$(this).attr("zoom"))
			$sThisZoom = $sThisSrc;
		
		$oImage = new Image();
		$oImage.src = $sThisSrc;		
		$oImage.width = parseInt($oImage.width);
		$oImage.height = parseInt($oImage.height);
		if ($oImage.width > $oImage.height) {
			if ($oImage.width > 100)
				$(this).attr('width', '100');
			else
				$(this).removeAttr('width');
			$(this).removeAttr('height');
		}
		else {
			if ($oImage.height > 70)
				$(this).attr('height', '70');
			else
				$(this).removeAttr('height');
			$(this).removeAttr('width');
		}		
		if ($iImCnt == 0)
		{
			$oImage = new Image();
			$oImage.src = $sThisDetail;		
			$oImage.width = parseInt($oImage.width);
			$oImage.height = parseInt($oImage.height);
			if ($oImage.width > $oImage.height) {
				if ($oImage.width > 230)
					$("#jsImBoDetail").html('<img src="' + $sThisDetail + '" width="230px" />');
				else
					$("#jsImBoDetail").html('<img src="' + $sThisDetail + '" />');
			} else {
				if ($oImage.height > 230)
					$("#jsImBoDetail").html('<img src="' + $sThisDetail + '" height="230px" />');
				else
					$("#jsImBoDetail").html('<img src="' + $sThisDetail + '" />');
			}
			$("#jsImBoLupe").attr('href', $sThisZoom);
		}			
		$iImCnt = $iImCnt + 1;		
		$(this).hover(
			function () {
				$sThisSrc = $(this).attr("src");
				$sThisDetail = $(this).attr("detail");
				$sThisZoom = $(this).attr("zoom");
				if (!$(this).attr("detail"))
					$sThisDetail = $sThisSrc;
				if (!$(this).attr("zoom"))
					$sThisZoom = $sThisSrc;
				$oImage = new Image();
				$oImage.src = $sThisDetail;		
				$oImage.width = parseInt($oImage.width);
				$oImage.height = parseInt($oImage.height);
				
				
				if ($oImage.width > $oImage.height) {
					if ($oImage.width > 230)
						$("#jsImBoDetail").html('<img src="' + $sThisDetail + '" width="230px" />');
					else
						$("#jsImBoDetail").html('<img src="' + $sThisDetail + '" />');
				} else {
					if ($oImage.height > 230)
						$("#jsImBoDetail").html('<img src="' + $sThisDetail + '" height="230px" />');
					else
						$("#jsImBoDetail").html('<img src="' + $sThisDetail + '" />');
				}
				$("#jsImBoLupe").attr('href', $sThisZoom);
			},
			function () {}
		);	
	});
	
	if ($("#imboUp")) {
		$("#imboUp").hide();
		if ($iImCnt < 4) {			
			$("#imboDown").hide();
		}
		
		$("#imboUp").click(function () {
			$iOld = $iImAkt + 3;
			$iImAkt = $iImAkt - 1;
			$(".imboli_"+$iImAkt).show();
			$(".imboli_"+$iOld).hide();
			$("#imboDown").show();
			if ($iImAkt == 0)
				$("#imboUp").hide();
		});
		
		$("#imboDown").click(function () {
			$iNew = $iImAkt + 3;
			$(".imboli_"+$iImAkt).hide();
			$(".imboli_"+$iNew).show();
			$iImAkt = $iImAkt + 1;
			$("#imboUp").show();
			if ($iImAkt == $iImCnt-3)
				$("#imboDown").hide();
		});
	}
	
	
	
	
	
	
	
	
	
	
	
	$iAktSicherheit = 0;
	$iCntSicherheit = 0;
	$(".jsCtrlSicherheitUp").addClass("dontshow");
	$(".tsContainer").each(function () {
		$(this).addClass('tsid_'+$iCntSicherheit);
		if ($iCntSicherheit == 0 || $iCntSicherheit == 1)
			$(this).show();
		else
			$(this).hide();			
		$iCntSicherheit = $iCntSicherheit + 1;		
	});
	if ($iCntSicherheit <= 2) {
		$(".jsCtrlSicherheitDown").addClass("dontshow");
	}
	
	
	
	/**
	 * Thema Sicherheit "hoch"
	 */
	$(".jsCtrlSicherheitUp").click(function () {
		if ($iCntSicherheit > 2 && $iAktSicherheit >= 1) {
			$iOldAktSicherheit1 = $iAktSicherheit;
			$iOldAktSicherheit2 = $iAktSicherheit + 1;
			$iAktSicherheit = $iAktSicherheit - 1;
			
			if ($iAktSicherheit == 0)
				$(".jsCtrlSicherheitUp").addClass("dontshow");
			$(".jsCtrlSicherheitDown").removeClass("dontshow");
			$(".tsid_"+$iAktSicherheit).show();
			$(".tsid_"+$iOldAktSicherheit2).hide();	
		}							
	});
	
	/**
	 * Thema Sicherheit "runter"
	 */
	$(".jsCtrlSicherheitDown").click(function () {		
		if ($iCntSicherheit > 2 && $iAktSicherheit < $iCntSicherheit-2) {
			$iOldAktSicherheit1 = $iAktSicherheit;
			$iOldAktSicherheit2 = $iAktSicherheit + 1;
			$iAktSicherheit = $iAktSicherheit + 1;
			$iAktSicherheit2 = $iAktSicherheit + 1;
			
			$iAktSicherheit2 = $iAktSicherheit + 1;
			if ($iAktSicherheit == $iCntSicherheit-2)
				$(".jsCtrlSicherheitDown").addClass("dontshow");
			$(".jsCtrlSicherheitUp").removeClass("dontshow");
			$(".tsid_"+$iAktSicherheit2).show();
			$(".tsid_"+$iOldAktSicherheit1).hide();	
		}
	});
		
	/**
	 * News Box breite setzen
	 */
	var $iCntNewsBoxen = 0;
	$("#jsNewsScrollBox .cesNewsBox").each(function () {
		$iCntNewsBoxen = $iCntNewsBoxen + 1;
	});
	$("#jsNewsScrollBox").css('width', $iCntNewsBoxen*414);
		
	/**
	 * News Box nach rechts scrollen
	 */	
	$(".jsCtrlNewsRight").hover(
		function () {	
			clearInterval($sTimeoutObj);		
			$sTimeoutObj = window.setInterval(function() {
				$sMaxWidth = $("#jsNewsScrollBox").css('width');
				$sMaxWidth = $sMaxWidth.replace("px", "");
				$sMaxWidth = parseInt($sMaxWidth);
				$sMaxWidth = $sMaxWidth - 683;
				$sMaxWidth = $sMaxWidth * -1;
				
				$sLeft = $("#jsNewsScrollBox").css('left');
				$sLeft = $sLeft.replace("px", "");
				$sLeft = parseInt($sLeft);		
				if ($sLeft > $sMaxWidth)
				{
					$sLeft = $sLeft - 4;
					$sLeft = $sLeft + 'px';
					$("#jsNewsScrollBox").css('left', $sLeft);
				}
				else
				{
					$("#jsNewsScrollBox").css('left', $sMaxWidth+'px');
					clearInterval($sTimeoutObj);
				}
			}, 1);
		},
		function () {		
			clearInterval($sTimeoutObj);
		}	
	);
	
	$(".jsCtrlNewsLeft").hover(
		function () {			
			clearInterval($sTimeoutObj);
			$sTimeoutObj = window.setInterval(function() {				
				$sLeft = $("#jsNewsScrollBox").css('left');
				$sLeft = $sLeft.replace("px", "");
				$sLeft = parseInt($sLeft);
				if ($sLeft < 0)
				{
					$sLeft = $sLeft + 4;
					$sLeft = $sLeft + 'px';
					$("#jsNewsScrollBox").css('left', $sLeft);
				}
				else
				{
					$("#jsNewsScrollBox").css('left', '0px');
					clearInterval($sTimeoutObj);
				}
			}, 1);
		},
		function () {		
			clearInterval($sTimeoutObj);
		}	
	);
	
	
	/**
	 * OrderSlip Functions
	 */
	window.getClick = function() {
		return true;
	}

	window.SuchenUndErsetzen = function(QuellText, SuchText, ErsatzText) { 
		if ((QuellText == null) || (SuchText == null))           { return null; }
		if ((QuellText.length == 0) || (SuchText.length == 0))   { return QuellText; }
				
		if ((ErsatzText == null) || (ErsatzText.length == 0))    { ErsatzText = ""; }
		
		var LaengeSuchText = SuchText.length;
		var LaengeErsatzText = ErsatzText.length;
		var Pos = QuellText.indexOf(SuchText, 0);
		
		while (Pos >= 0)
		{
		    QuellText = QuellText.substring(0, Pos) + ErsatzText + QuellText.substring(Pos + LaengeSuchText);
		    Pos = QuellText.indexOf(SuchText, Pos + LaengeErsatzText);
		}
		return QuellText;
	} 

	window.trim = function(s) {
	  while (s.substring(s.length-1,s.length) == ' ') {
	    s = s.substring(0,s.length-1);
	  }
	  return s;
	}
	
	var mypopup = false;
	window.openpopup = function(w, h, site) {
		popen = 1;
		var x = screen.availWidth/2-w/2;
		var y = screen.availHeight/2-h/2;
		if (mypopup == 0 || mypopup.closed==true)
		{
			mypopup = window.open(site,'thepopup','width='+w+',height='+h+',left='+x+',top='+y+',screenX='+x+',screenY='+y);
			mypopup.focus();
		}        
		else
		{
			mypopup.focus();
			mypopup.location.replace(site);		
		}
	}

	var ischecking = 0;
	window.check_input = function()
	{
		if (ischecking == 0)
		{
			ischecking = 1;
			var out_string = document.mag_form.magnet.value;
			var tag = "";
			var monat = "";
			var jahr = "";
			var laufnummer = "";
			var field_a = "";
			var field_b = "";
			var field_c = "";
			var field_d = "";
			var last_tmp;
			var pos;
			var tmp;
			var tempo;
	
			document.getElementById('error').innerHTML = "<B></B>";         
			anzahlbustaben = out_string.length;	 			 
					  
			if (out_string.length > anzahlbustaben)
			{
				out_string = "";
				document.mag_form.magnet.value = "";
				document.getElementById('error').innerHTML = "<B>Fehler beim einlesen, bitte versuchen Sie es erneut!</B>";	 		  
				document.mag_form.magnet.focus();
				anzahlbustaben = 0;
				ischecking = 0;
			}  
			else
			{	
				anzahlbustaben = out_string.length;	
				
				out_string = out_string.replace(/ /g, "");
				out_string = out_string.replace(/ß/g, "-");			
				
				if (out_string.indexOf('CES') > 0)
				{
					if (out_string.indexOf('%') !== -1) 
					{
						out_string = out_string.substr(out_string.indexOf('%')+1); 
					} 
	
					if (out_string.indexOf('?') !== -1) 
					{
						out_string = out_string.substr(0, out_string.indexOf('?'));
					}	 				
			 	
					tempo = out_string.substr(0, out_string.indexOf('CES'));
					out_string = tempo;  
					out_string = trim(out_string); 
					tmp_outstr = out_string;
					
					index = tmp_outstr.match(/[0-9]{7,15}/);
					index = index + '';
					
					if (index.length > 8)
					{
						iTill = index.length - 8;
						index = index.substr(iTill, 8);
					}
		
					index = tmp_outstr.indexOf(index);	 				
					if (index < 10)
					{
						tmp_outstr = tmp_outstr.substr(10);
						index = tmp_outstr.match(/[0-9]{7,15}/);
						index = index + '';
						if (index.length > 8)
						{
							iTill = index.length - 8;
							index = index.substr(iTill, 8);
						}
						index = tmp_outstr.indexOf(index);
						index = index + 10;
					}
					last_tmp = index+8;
	
					//last_tmp = out_string.length-1;
					//tmp = last_tmp-3;
					tmp = last_tmp-4;
					jahr = out_string.substr(tmp, 4);
					tmp = tmp-2;
					monat = out_string.substr(tmp, 2);
					tmp = tmp-2;
					tag = out_string.substr(tmp, 2);
	
					/////////////////////////////////////////////////	   
					tempo = out_string.substr(0, tmp);
					out_string = tempo;	
					/////////////////////////////////////////////////	   			  
	
					// Laufnummer				
					pos = out_string.length - 2;			   
					laufnummer = out_string.substr(pos, 2);
					out_string = out_string.substr(0, pos);				
					
					// Feld A
					out_string = trim(out_string);
					index = out_string.match(/[a-zA-Z]{0,5}([0-9]P)?/);
					field_a = index + '';	 					 			
					
					iPosKomma = field_a.indexOf(',');
					if (iPosKomma > 0)
						field_a = field_a.substr(0, iPosKomma);
					
					iPosPlus = field_a.indexOf('P');
					if (iPosPlus < 3)
					{
						index = out_string.match(/[a-zA-Z]{0,5}/);
						field_a = index + '';
					}
					out_string = out_string.substr(field_a.length);
				
					if (iPosPlus >= 3)
					{				
						field_a = field_a + 'LUS';
					} 					 			
		
					// Feld B
					out_string = trim(out_string);
					index = out_string.match(/[0-9]{4,7}/);
					field_b = index + '';
					out_string = out_string.substr(field_b.length);
				
					
					//Feld C & Feld D
					out_string = trim(out_string);
					field_c = out_string.substr(0, 3);
					field_d = out_string.substr(3);
								 			
					if (laufnummer != "" && field_a != "" && field_b != "" && field_c != "")
					{ 
						dotimer = 0;	
	
						document.getElementById('magnetkarte').value=document.mag_form.magnet.value;
						document.getElementById('laufnummer').value=laufnummer;
						document.getElementById('datum').value=jahr+monat+tag;
						document.getElementById('feld_a').value=field_a;
						document.getElementById('feld_b').value=field_b;
						document.getElementById('feld_c').value=field_c;
						document.getElementById('feld_d').value=field_d;				 				 
						
						document.getElementById('error').innerHTML = "<B>Die Magnetkarte ist g&uuml;ltig!</B>";	  	
						document.getElementById('lampe').src='fileadmin/ces/images/orderslip/lampe_gruen.gif';
						document.mag_form.magnet.style.visibility = 'hidden';
						document.getElementById('card_auth_button').style.display = 'inline';
						document.formu.submit();
						ischecking = 0;
						return 1;
					}
					else
					{
						document.getElementById('error').innerHTML = "<B>Die Magnetkarte ist ung&uuml;ltig!</B>";	  	
						document.getElementById('lampe').src='fileadmin/ces/images/orderslip/lampe_rot.gif';
						ischecking = 0;
						return 0;
					}
				}
				else
				{	  
					document.getElementById('error').innerHTML = "<B>Karte wird eingelesen, bitte warten!</B>";	
					document.getElementById('lampe').src='fileadmin/ces/images/orderslip/lampe_rot.gif';
					ischecking = 0;
					return 0;
				}
			}	  
		}	
	}		
	
	window.scaleImageBoxImages = function() {
		$("#jsImBoList img").each(function() {
			$oImage = new Image();
			$oImage.src = $(this).attr("src");		
			$oImage.width = parseInt($oImage.width);
			$oImage.height = parseInt($oImage.height);
			if ($oImage.width > $oImage.height) {
				if ($oImage.width > 100)
					$(this).attr('width', '100');
				else
					$(this).removeAttr('width');
				$(this).removeAttr('height');
			}
			else {
				if ($oImage.height > 70)
					$(this).attr('height', '70');
				else
					$(this).removeAttr('height');
				$(this).removeAttr('width');
			}
		});
	}
	setTimeout("scaleImageBoxImages()", 500);
	
	$('.lightbox').lightBox();
});
