/**
 * Version 2.0
 */

currentTab = 'modell';
nextTab = 'daten';
previousTab = false;
formInitialized = false;



/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
	if($(el)) {
		if($(el).type && $(el).type.toLowerCase() == 'radio') {
	        var radioGroup = $(el).name;
	        var el = $(el).form;
	    } else if ($(el).tagName.toLowerCase() != 'form') {
	        return false;
	    }

	    var checked = $(el).getInputs('radio', radioGroup).find(
	        function(re) {return re.checked;}
	    );
	    return (checked) ? $F(checked) : null;
	}

}


function ProbefahrtForm(){
	instance = this;
	submited = false;
	this.done=false;
	this.observeProbefahrtForm = function()
	{
		if($('tab_modell') == null) {
			return false;
		}

		$('tab_modell').observe('click', function(element){
			document.location.href = '/forms/index/probefahrt';
		});

		$('tab_daten').observe('click', function(element){
			instance.showTabContainer('daten');
		});

		$('tab_haendler').observe('click', function(element){
			instance.showTabContainer('haendler');
		});

		$('tab_kontakt').observe('click', function(element){
			if(!instance.acceptdealer()){
				instance.showTabContainer('haendler');
			}
		});
	}






	this.observeNextTabButton = function()
	{
		$$('a[class="nexttabbutton"]').each(function(e) {
			e.observe('click', function(event){
				if(nextTab){
					if(nextTab == 'kontakt'){
						instance.acceptdealer();
					}
					else{
						instance.showTabContainer(nextTab);
					}
				}
			});
		});
	}

	this.observePreviousTabButton = function()
	{
		$$('a[class="previoustabbutton"]').each(function(e) {
			e.observe('click', function(event){
				if(previousTab){
					instance.showTabContainer(previousTab);
				} else {
					document.location.href = '/forms/index/probefahrt';
				}
			});
		});
	}

	this.observePurchasetype = function(){
		//hide all styles for commercial ordering by default


		$$('input.purchasetype').each(function(inputelem){
			inputelem.observe('change', function(element){
				instance.switchPurchasetype($RF('probefahrtForm', 'purchasetype'));

			});
		});
	};


	this.switchPurchasetype = function(type){

		if($('purchasetypemandatory')) {
			style = '';
			switch(type){

				case 'commercial':
							$('purchasetypemandatory').show();
				break;

				case 'private':
				default:
					$('purchasetypemandatory').hide();
				break;
			}
		}


	/*
		style = '';
		switch(type){

			case 'commercial':
				style = '.purchasetypeCommercial';
				currentStyle = '.purchasetypePrivate';

				if($('tab_modell').innerHTML == 'NOT ACTIVE'){
					$('tab_kontakt').innerHTML = '2. Kontakt';
				}
				else{
					$('tab_kontakt').innerHTML = '3. Kontakt';
				}
			break;

			case 'private':
			default:
				style = '.purchasetypePrivate';
				currentStyle = '.purchasetypeCommercial';
				if($('tab_modell') || $('tab_kontakt')) {
					if($('tab_modell').innerHTML == 'NOT ACTIVE'){
						$('tab_kontakt').innerHTML = '3. Kontakt';
					}
					else{
						$('tab_kontakt').innerHTML = '4. Kontakt';
					}
				}

			break;
		}

		$$(style).each(function(elem){
			elem.show();
		});

		$$(currentStyle).each(function(elem){
			elem.hide();
		});*/
	};

	this.showTabContainer = function(container)
	{
		if(currentTab == 'danke'){
			instance.setErrorText('container_tab_danke', 'Ihre Daten wurden bereits gespeichert. Wir werden uns in Kürze bei Ihnen melden.');
			container = 'danke';
		}

		if($('container_tab_daten') == null) {
			return false;
		}

		//reset all
		if($('container_tab_daten')){
			$('container_tab_daten').hide();
		}

		$('container_tab_haendler').hide();
		$('container_tab_kontakt').hide();
		$('container_tab_danke').hide();


		switch(container){

			case 'daten':
				bgPos = getBackgroundPosition($('tab_daten'));
				setBackgroundPosition($('tab_daten'), bgPos[0], '-42px');
				$('container_tab_daten').show();
				instance.switchPurchasetype($RF('probefahrtForm', 'purchasetype'));
				nextTab = 'haendler';
				previousTab = false;
				this.gfx($F('konfimodelid'));
				pageTracker._trackPageview('/testdrive/' + $F('carname') + '/data');
//				if ($('phone2').value == '') {
//                    $('phone2').value = $('phone').value;
//                }
//				if(instance.carname == 'ix35'){
//					var axel = Math.random() + "";
//					var a = axel * 10000000000000;
//					new Ajax.Request('http://ad-emea.doubleclick.net/activity;src=2200424;type=ix35;cat=1002p332;ord=' + a + '?');
//				}
			break;

			case 'haendler':
				//$('phone2').value = $('phone').value;
				if(this.validatePersonData()){
					pageTracker._trackPageview('/testdrive/' + $F('carname') + '/dealer');
					$('probefahrtForm').action='/forms/json/post/name/probefahrt2010';
					$('probefahrtForm').request({parameters: {name:'probefahrt2010'}});
					$('probefahrtForm').action='/forms/json/form/name/probefahrt2010';
					bgPos = getBackgroundPosition($('tab_haendler'));
					setBackgroundPosition($('tab_haendler'), bgPos[0], '-42px');
					$('container_tab_haendler').show();
					previousTab = 'daten';
					nextTab = 'kontakt';

					var anrede=" HERR ";
					if($RF('probefahrtForm','gender')=='F' || $RF('probefahrtForm','gender')=='f' ) {
						anrede=" FRAU ";
					}

					anrede = anrede + $F('lastname').toUpperCase();
					$('probefahrthaendlerimg').src="/image.php?id=3&car="+$F('carname')+"&person="+anrede;

					if($F('dealerid')<=1000) {
						if (map==null) {
							initGoogleMap();
						}
						$('searchaddress').value=$F('street_no')+', '+$F('zipcode')+' '+$F('city');
						startSearch($F('street_no')+', '+$F('zipcode')+' '+$F('city'));

					}
					else {
						if (map==null) {

							initGoogleMap($F('searchaddress'),$F('lat'),$F('lon'),$F('dealerid'));
						}
					}
				}
				else{
					this.showTabContainer('daten');
					return false;
				}

			break;

			case 'kontakt':

				if(this.validatePersonData() && this.validateDealerData()){
					$('probefahrtForm').action='/forms/json/post/name/probefahrt2010';
					$('probefahrtForm').request({parameters: {name:'probefahrt2010'}});
					$('probefahrtForm').action='/forms/json/form/name/probefahrt2010';
					bgPos = getBackgroundPosition($('tab_kontakt'));
					setBackgroundPosition($('tab_kontakt'), bgPos[0], '-42px');
					previousTab = 'haendler';
					nextTab = 'danke';
					if ($('phone2').value == '') {
                        $('phone2').value = $('phone').value;
                    }
					$('container_tab_kontakt').show();
					pageTracker._trackPageview('/testdrive/' + $F('carname') + '/contact');
				}
				else{
					this.showTabContainer('haendler');
					return false;
				}

			break;

			case 'danke':
				if((this.validatePersonData() && this.validateDealerData() && this.submitFormData()) || currentTab == 'danke'){

//					if(instance.carname == 'ix35'  ){
//						var axel = Math.random() + "";
//						var a = axel * 10000000000000;
//						new Ajax.Request('http://ad-emea.doubleclick.net/activity;src=2200424;type=ix35;cat=1002p349;ord=' + a + '?');
//					}

					var anrede=" HERR ";
					if($RF('probefahrtForm','gender')=='F' || $RF('probefahrtForm','gender')=='f' ) {
						anrede=" FRAU ";
					}
					this.button($F('konfimodelid'));
					$$('#konfibutton a').each(function(link){
						if($RF('probefahrtForm', 'purchasetype') == 'commercial'){
							baseurl = link.readAttribute('gewerbebaseurl');
						}
						else{
							baseurl = '';
						}

						if(link.readAttribute('href') == ''){
							link.writeAttribute('href', baseurl + '/konfi/index/index/car/' + instance.modelid);
						}
						else{
							link.writeAttribute('href', baseurl + '/konfi/konfi/');
						}
					});
					if((instance.carname == 'Genesis Coupe' ) && $('konfibutton') != null) {
				        $('konfibutton').remove();
				    }
					$$('#modelbackbutton a').each(function(link){
						modellname = instance.carname.toLowerCase().replace(/ /g, '_').replace(/\-/g, '');
						if(modellname == 'santa_fe'){
							modellname = 'santafe';
						}
                        if (modellname == 'genesis_coupe'){
							modellname = 'genesiscoupe';
                        }
						if($('probefahrtEmbeded')){
							link.writeAttribute('href', 'javascript:void(0);');
							link.observe('click', function(){
								hideOverlay('probefahrtOverlay');
							});
						}
						else{
							link.writeAttribute('href', '/pages/modelle/' + modellname + '/index.html');
						}
					});


					anrede = anrede + $F('lastname').toUpperCase();
					$('probefahrtdankeimg').src="/image.php?id=8&car="+$F('carname')+"&person="+anrede;


                    // Advertiser 'Gfmo Omd Ges Fuer Media-Optimierung MBH / Hyundai Motor Deutschland GMBH',  Conversion tracking 'Gfmo Omd Ges Fuer Media-Optimierung MBH / Hyundai Motor Deutschland GMBH_Modelseite_Confirmation' - DO NOT MODIFY THIS PIXEL IN ANY WAY -->
                    $('omd_confirmation_pixel_thankyou').src = 'http://ads.bluelithium.com/pixel?id=1217441&t=2';
                    // End of conversion tag -->
                    var modellname = instance.carname.toLowerCase().replace(/ /g, '_').replace(/\-/g, '');
                    if(modellname == 'i10' || modellname== 'i30') {

                        new Ajax.Request('/forms/index/formid', {
                          onSuccess: function(transport) {
                              $('adpepper_confirmation_pixel_thankyou').src = "http://ilead.itrack.it/clients/track/10/track.asp?cid=16683&registrierung=ok&noscript=1&userid="+transport.responseText;
                          }
                        });
                    }



                    $('container_tab_danke').show();
					pageTracker._trackPageview('/testdrive/' + $F('carname') + '/thankyou');


				}
				else{
					this.showTabContainer('kontakt');
					return false;
				}
			break;
		}

		if(currentTab != container && container != 'danke'){
			bgPos = getBackgroundPosition($('tab_' + currentTab));
			setBackgroundPosition($('tab_' + currentTab), bgPos[0], '-21px');
		}

		currentTab = container;

		return false;
	}

	this.observeAcceptDealerButton = function() {

		if($('acceptdealerbutton') == null) {
			return false;
		}

		$('acceptdealerbutton').observe('click', function(event){
			instance.acceptdealer();
		});
	}

	this.acceptdealer = function(){
		var dealerid = $('mapactionacceptdealer').readAttribute('dealerid');
		if(dealerid){
			$('dealerid').value = $('mapactionacceptdealer').readAttribute('dealerid');
			$('lat').value = $('mapactionacceptdealer').readAttribute('lat');
			$('lon').value = $('mapactionacceptdealer').readAttribute('lon');
			$('searchaddress').value = $('mapactionacceptdealer').readAttribute('searchaddress');
			Form.getInputs($('probefahrtForm'), 'hidden', 'dealerid').each(function(element){
				element.value = dealerid;
				new Ajax.Updater($('dealerbox'), '/forms/json/dealer?dealerid=' + dealerid, {onComplete: function(){
						instance.showTabContainer('kontakt');
					}
				});
			});

			return true;
		}
		else{
			instance.showTabContainer('kontakt');
			return false;
		}
	}

	this.validatePersonData = function(){
		hasError = false;
		var errorfield = new Array;

		formInputs = new Hash();
		sort = new Array('gender', 'firstname', 'lastname', 'purchasetype','company_name', 'street_no', 'zipcode', 'city', 'email', 'phone','auto_hersteller', 'auto_baujahr', 'auto_zeitraum', 'drivinglicense','company_name');

		Form.getInputs($('probefahrtForm')).each(function(element){
			formInputs.set(element.name, element);
		});

		sort.each(function(sortitem){
				element = formInputs.get(sortitem);

				switch(sortitem){

					case 'firstname':
						if(element.value == ''){
							errorfield.push('Vorname');
							hasError = true;
						}
						break;
					case 'lastname':
						if(element.value == ''){
							errorfield.push('Nachname');
							hasError = true;
						}
						break;
					case 'street_no':
						if(element.value == ''){
							errorfield.push('Straße und Hausnummer');
							hasError = true;
						}
						break;

					case 'zipcode':
						if(!element.value.match(/^[0-9]{5}$/)){
							errorfield.push('Postleitzahl');
							hasError = true;
						}
						break;

					case 'city':
						if(element.value == ''){
							errorfield.push('Ort');
							hasError = true;
						}
						break;
					case 'email':
						if(element.value != ''){
/*							if(!element.value.match(/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b$/)){
								errorfield.push('email');
								hasError = true;
							}*/
						}
						break;


					case 'drivinglicense':
						if(element.type != 'hidden' && !element.checked){
						//	instance.setErrorText('container_tab_daten', 'Sie müssen eine gültige Fahrerlaubnis besitzen, um eine Probefahrt buchen zu können.');
							errorfield.push('Fahrerlaubnis');
							hasError = true;
						}
						break;

					case 'gender':
						if ($RF('probefahrtForm','gender')== null) {
							errorfield.push('Anrede');
							hasError = true;
						}
					break;
					case 'phone':

						if(element.value == ''){
							errorfield.push('Telefon');
							hasError = true;
						}
						break;


					case 'purchasetype':
						if(!$RF('probefahrtForm', 'purchasetype')){
							errorfield.push('Art der Anschaffung');
							hasError = true;
						}
						break;

					case 'company_name':
						if($RF('probefahrtForm', 'purchasetype')=='commercial'){
							if(element.value == ''){
								errorfield.push('Firmenname');
								hasError = true;
							}
						}
						break;
				}

		});

		e=errorfield.uniq();
		if(!hasError){
			instance.setErrorText('container_tab_daten', '');
		} else {
			if(e.length == 1){
				errortext = 'Bitte prüfen Sie folgendes Feld: ';
			}
			else{
				errortext = 'Bitte prüfen Sie folgende Felder: ';
			}
			for(i = 0; i < e.length; i++){
				errortext += e[i];
				if(i < e.length - 1){
					errortext += ', ';
				}
			}
			instance.setErrorText('container_tab_daten',errortext);
		}
		return !hasError;
	}

	this.validateDealerData = function(){
		hasError = false;


		Form.getInputs($('probefahrtForm'), 'hidden', 'dealerid').each(function(element){
			if(element.value != ''){
				instance.setErrorText('container_tab_haendler', '');
				hasError = false;
			}
			else{
				instance.setErrorText('container_tab_haendler', 'Bitte wählen Sie einen Händler aus.');
				hasError = true;
			}
		});

		return !hasError;
	}

	this.setErrorText = function(containerId, text){
		$$('#' + containerId + ' .formerror').each(function(errortext){
			errortext.update(text);
		});
	}

	this.submitFormData = function(){
		hasError = false;
		var errorfield = new Array;

	//^(((((00|\+)49[ \-]{0,1})|0)[2-9][0-9]{1,4}[ \-]{0,1}|(((00|\+)49\()|\(0)[2-9][0-9]{1,4}\))[0-9]{1,7}[ \-]{0,1}[0-9]{1,5})$
		//if(!$F('phone').match(/^(((((00|\+)49[ \-]{0,1})|0)[1-9][0-9]{1,4}[ \-]{0,1}|(((00|\+)49\()|\(0)[2-9][0-9]{1,4}\))[0-9]{1,7}[ \-]{0,1}[0-9]{1,5})$/)){
	//	if(!$F('phone').match(/^(((((00|\+)49[ \-\/]{0,1})|0)[1-9][0-9]{1,4}[ \-\/]{0,1}|(((00|\+)49\()|\(0)[2-9][0-9]{1,4}\))[0-9]{1,7}[ \-\/]{0,1}[0-9]{1,5})$/)){
	//		errorfield.push('Telefon');
	//		hasError = true;
	//	}

//		if($F('contact_weekday') == 'nothing'){
//			errorfield.push('Wochentag');
//			hasError = true;
//		}

		if($F('contact_morning')== null && $F('contact_afternoon')==null ){
			errorfield.push('Zeitraum');
			hasError = true;
		}


		if(!hasError && !instance.submited){
			$('probefahrtForm').request({
				onComplete: function(){hasError = false;instance.submited = true;},
				parameters: {name:'probefahrt2010'}
				});
		}
		else{
			e=errorfield.uniq();
			if(!hasError){
				instance.setErrorText('container_tab_kontakt', '');
			} else {
				if(e.length == 1){
					errortext = 'Bitte prüfen Sie folgendes Feld: ';
				}
				else{
					errortext = 'Bitte prüfen Sie folgende Felder: ';
				}
				for(i = 0; i < e.length; i++){
					errortext += e[i];
					if(i < e.length - 1){
						errortext += ', ';
					}
				}
				instance.setErrorText('container_tab_kontakt',errortext);
			}
			return false
		}

		return true;
	}

	this.observeCloseLayerButton = function(){
		if($('probefahrtEmbeded')){
			$('buttoncompareclose').observe('click', function(){
				hideOverlay('probefahrtOverlay');
			});
		}
	}

	this.gfx = function(car) {
		cars=$H({6:'sonata',7:'grandeur',8:'tucson',9:'h1_travel',10:'h1_cargo',11:'santafe',12:'ix35',13:'genesiscoupe'});

		if(cars.keys().any(function(n) {return n == car;})) {
			$$('div[id="visual"]>img').each(function(e){
				e.src='/_gfx/modelle/'+cars.get(car)+'/aussen_key.jpg';
			});
			return true;
		}

	}

	this.button = function(car) {

		cars=$H({9:'h1_travel',10:'h1_cargo',11:'santafe'});

		if(cars.keys().any(function(n) {return n == car;})) {
			$$('div[id="konfibutton"]>div>a>img').each(function(e){
				e.src='/_gfx/beratung/'+cars.get(car)+'_button_konfi.gif';
			});
			$$('div[id="modelbackbutton"]>div>a>img').each(function(e){
				e.src='/_gfx/beratung/'+cars.get(car)+'_button_modellback.gif';
			});
			return true
		}

	}

//	this.setErrorText = function(containerId, text){
//		$$('div[class="formerror"]').each(function(errortext){
//			errortext.update(text);
//		});
//	}

	//Initialisieren aller Werte

	this.observeProbefahrtForm();
	this.observeAcceptDealerButton();
	this.observeNextTabButton();
	this.observePreviousTabButton();
	this.observeCloseLayerButton();
	this.observePurchasetype();
	this.showTabContainer('modell');
	this.showTabContainer('daten');

	instance.switchPurchasetype($RF('probefahrtForm', 'purchasetype'));
}

/*
 * Prospekt-Formular
 */
function ZubehoerProspektForm()
{
	instance = this;
	this.selectiontype = 'pdf';
	this.submit=false;

	this.getSelectedModels = function(){
		return $$('div.modellbox[selected=1]');
	}

	this.observeSubmitButton = function(){
		$('submitbutton').observe('mouseover', function(){

				model = instance.getSelectedModels().first();
				if(model){
					$('submitbutton').writeAttribute('href', '/forms/download/zubehoerprospekt/model/' + model.readAttribute('modelid')+'/body/'+model.readAttribute('bodyid'));
					$('submitbutton').writeAttribute('target', '_blank');
				}
		});

		$('submitbutton').observe('click', function(){
					model = instance.getSelectedModels().first();
					if(!model){
						instance.setErrorText('Bitte wählen Sie ein Modell aus.');
						return false;
					}
					new Ajax.Request('/downloads/trackdl.php?model=' + model.readAttribute('modelid'));
		});
	}
	this.observeSubmitButton();
}

/*
 * Prospekt-Formular
 */
function ProspektForm()
{
	instance = this;
	submited = false;
	this.selectiontype = 'pdf';
	this.submit=false;
	this.done=false;

	this.observeTypeSelection = function(){
		$('prospektForm').getInputs('radio', 'selectiontype').each(function(element){
			element.observe('click', function(event){
				instance.setErrorText('');
				instance.selectiontype = event.element().value;
				if(event.element().value == 'pdf'){
					$('submitbuttonImage').writeAttribute('src', '/_gfx/button_download.gif');
					pageTracker._trackPageview('/brochure/pdf/index');
					instance.hideAddressForm();
					if(!$('prospektEmbeded')){
						selectedElementsCount = 0;



						$$('div.modellbox').each(function(e){
							if( $(e).readAttribute('selected') == '1') {
								selectedElementsCount++;
							}
						});
						if(selectedElementsCount > 1) {
							$$('div.modellbox').each(function(e){
								e.writeAttribute('selected','0');
								$(e).removeClassName('selected');
							});
						}

					}
				}
				else{
					pageTracker._trackPageview('/brochure/post/index');
					instance.showAddressForm();
					$('submitbuttonImage').writeAttribute('src', '/_gfx/button_send.gif');
					$('submitbutton').writeAttribute('href', 'javascript:void(0);');
					$('submitbutton').writeAttribute('target', '');
					selectedElementsCount = 0;
					$$('div.modellbox').each(function(e){
						if(e.readAttribute('selected') == 1){
							selectedElementsCount++;
						}
					});
				}
			});
		});
	}


	this.observePurchasetype = function(){
		//hide all styles for commercial ordering by default
		$$('.purchasetypeCommercial').each(function(elem){
			elem.hide();
		});

		$$('input.purchasetype').each(function(inputelem){
			inputelem.observe('change', function(element){
				instance.switchPurchasetype($RF('prospektForm', 'purchasetype'));

			});
		});
	};

	this.switchPurchasetype = function(type){
		style = '';
		switch(type){

			case 'commercial':
				$('purchasetypemandatory').show();
			break;

			case 'private':
			default:
				$('purchasetypemandatory').hide();
			break;
		}
	};

	this.observeSubmitButton = function(){
		$('submitbutton').observe('mouseover', function(){
			if(instance.selectiontype == 'pdf'){
				model = instance.getSelectedModels().first();
				if(model){
					$('submitbutton').writeAttribute('href', '/forms/download/prospekt/model/' + model.readAttribute('modelid'));
					$('submitbutton').writeAttribute('target', '_blank');
				}
			}
		});

		$('submitbutton').observe('click', function(){

			if(instance.selectiontype){
				if(instance.selectiontype == 'pdf'){
					model = instance.getSelectedModels().first();
					if(!model){
						instance.setErrorText('Bitte wählen Sie ein Modell aus.');
						return false;
					}

				//	if(model.readAttribute('carname') == 'ix35' ){
				//		var axel = Math.random() + "";
				//		var a = axel * 10000000000000;
				//		new Ajax.Request('http://ad-emea.doubleclick.net/activity;src=2200424;type=ix35;cat=1002b468;ord=' + a + '?');
				//	}

					new Ajax.Request('/downloads/trackdl.php?model=' + model.readAttribute('modelid'), {onComplete: function(){
						this.done = true;
						pageTracker._trackPageview('/brochure/pdf/' + model.readAttribute('carname') + '/download');
						if($('prospektEmbeded')){
							hideOverlay('prospektOverlay');
						}
					}});
				}
				else{
					if(instance.validatePersonData()){
						models = instance.getSelectedModels();

						modelcounter = 0;
						models.each(function(model){
						//	if(model.readAttribute('carname') == 'ix35'){
						//		var axel = Math.random() + "";
						//		var a = axel * 10000000000000;
						//		new Ajax.Request('http://ad-emea.doubleclick.net/activity;src=2200424;type=ix35;cat=1002b468;ord=' + a + '?');
						//	}
							formelement = new Element('input');
							formelement.writeAttribute('type', 'hidden');
							formelement.writeAttribute('name', 'prospekt[]');
							formelement.writeAttribute('value', model.readAttribute('modelid'));
							$('prospektForm').insert(formelement);
							modelcounter++;
							pageTracker._trackPageview('/brochure/post/' + model.readAttribute('carname') + '/data');
							pageTracker._trackPageview('/brochure/post/' + model.readAttribute('carname') + '/thankyou');
						});
						if (modelcounter != 0) {
							if (this.done==true) {
								return;
							} else {
								this.done=true;
							}
							$('prospektForm').request({onComplete: function(){
								this.submit=false;
								$('prospekt_form').hide();
								var anrede="LIEBER HERR ";
								if($RF('prospektForm','gender')=='F') {
									anrede="LIEBE FRAU ";
								}


								anrede = anrede + $F('lastname').toUpperCase();
								if (modelcounter == 1) {
									model = instance.getSelectedModels().first();
									carname=model.readAttribute('carname');
									carnamesmall=model.readAttribute('carname');
									//test=carname.substring(0,1);
									//if (test.value != 'i') {
                                    test=carname.substring(0,1).toLowerCase();
                                    if (test != 'i') {
										tmp=carname;
										carname=tmp.toUpperCase();
									}


									if(carnamesmall == 'gesamtprospekt'){
										$('prospektdankeHL').src="/image.php?id=12&person="+anrede;
										$('prospektdanketext1').update('Vielen Dank für Ihr Interesse an unseren Modellen. Wir wünschen Ihnen viel Spaß mit dem bestellten Prospektmaterial.<br/><br/>');
										text2='Unsere Fahrzeuge können Sie zudem ausgiebig im Konfigurator kennenlernen. Bei den i-Modellen haben Sie hier sogar die Möglichkeit, einen ganz individuellen Prospekt Ihres gewählten Hyundai als PDF herunterzuladen. Ein Klick genügt.';
									}


									else {
										$('prospektdankeHL').src="/image.php?id=9&car="+carname+"&person="+anrede;
										$('prospektdanketext1').update('Vielen Dank für Ihr Interesse am '+carnamesmall+'. Wir wünschen Ihnen viel Spaß </br>mit dem gewünschten Prospektmaterial.<br/><br/>');

										if (previousprobefahrtmodel){
												text2='Ihren '+carnamesmall+' können Sie zudem ausgiebig im Konfigurator kennenlernen<br/>und sich dann einen ganz individuellen Modellprospekt als PDF <br/>herunterladen. Ein Klick genügt.';

										} else {
												text2='Ihren '+carnamesmall+' können Sie zudem ausgiebig im Konfigurator kennenlernen<br/>und sich dann einen ganz individuellen Modellprospekt als PDF <br/>herunterladen. Oder buchen Sie direkt eine Probefahrt im '+carname+'. <br/>Ein Klick genügt.';
										}
									}


									$$('#konfibutton a').each(function(link){
										if(carnamesmall == 'gesamtprospekt'){
											link.writeAttribute('href', '/konfi/');
										}
										else{
										link.writeAttribute('href', '/konfi/index/index/car/' + model.readAttribute('modelid'));
										}
									});


									if(carnamesmall == 'ix20'){
									    $('prospektdankeHL').src="/image.php?id=9&car="+carname+"&person="+anrede;
									    $('prospektdanketext1').update('Vielen Dank für Ihr Interesse am '+carnamesmall+'. Wir wünschen Ihnen viel Spaß </br>mit dem gewünschten Prospektmaterial.<br/><br/>');
										$('prospektdanketext2').update('');
										$('konfibutton').remove();
									}





									if(!previousprobefahrtmodel || previousprobefahrtmodel != model.readAttribute('modelid')){
										$$('#probefahrtbutton a').each(function(link){
											link.observe('click', function(){

												if($('probefahrtOverlay')){
													showProbefahrtOverlay(true);
												}
												else{
													if(carnamesmall == 'gesamtprospekt'){
														document.location.href = link.readAttribute('baseurl') + '/forms/index/probefahrt';
														return false;
													}
													else{
														document.location.href = link.readAttribute('baseurl') + '/forms/index/probefahrt/car/' + model.readAttribute('modelid');
													}
												}
											});

										});
										modellname = carnamesmall.toLowerCase().replace(/ /g, '_').replace(/\-/g, '');
										if(modellname == 'santa_fe'){
											modellname = 'santafe';
										}
										$$('div[id="probefahrtbutton"]>div>a>img').each(function(e){
											e.src='/_gfx/beratung/'+modellname+'_probefahren.gif';
										});

									}
									else{
										$('probefahrtbutton').remove();
									}

									if(carnamesmall == 'Genesis Coupe'){
									    $('prospektdankeHL').src="/image.php?id=9&car="+carname+"&person="+anrede;
									    $('prospektdanketext1').update('Vielen Dank für Ihr Interesse am '+carnamesmall+'. Wir wünschen Ihnen viel Spaß </br>mit dem gewünschten Prospektmaterial.<br/><br/>');
										$('prospektdanketext2').update('');
										$('konfibutton').remove();
										$('probefahrtbutton').remove();
									}

									$$('#modelbackbutton a').each(function(link){
										modellname = carnamesmall.toLowerCase().replace(/ /g, '_').replace(/\-/g, '');
										if(modellname == 'santa_fe'){
											modellname = 'santafe';
										}
										if($('prospektEmbeded')){
											link.writeAttribute('href', 'javascript:void(0);');
											link.observe('click', function(){
												hideOverlay('prospektOverlay');
											});
										}
										else{
											link.writeAttribute('href', '/pages/modelle/' + modellname + '/index.html');
										}
									});
									$$('div[id="konfibutton"]>div>a>img').each(function(e){
										e.src='/_gfx/beratung/'+modellname+'_button_konfi.gif';
									});
									$$('div[id="modelbackbutton"]>div>a>img').each(function(e){
										e.src='/_gfx/beratung/'+modellname+'_button_modellback.gif';
									});
									$('modelbackbutton').remove();
								} else {
									names = '';
									instance.getSelectedModels().each(function(model){
										carname=model.readAttribute('carname');
										if(names != ''){
											names += ', ';
										}
										names += carname;
									});
									$('prospektdankeHL').src="/image.php?id=6&person="+anrede;

									$('prospektdanketext1').update('Vielen Dank für Ihr Interesse an unseren Modellen '+names+'. Wir wünschen Ihnen viel Spaß </br>mit dem gewünschten Prospektmaterial.<br/><br/>');
									if (previousprobefahrtmodel){
										text2='Ihr Wunschmodell können Sie zudem ausgiebig im Konfigurator kennenlernen</br>und sich dann einen ganz individuellen Modellprospekt als PDF <br/>herunterladen. Oder buchen Sie direkt eine Probefahrt. <br/>Ein Klick genügt.';
									} else {
										text2='Ihr Wunschmodell können Sie zudem ausgiebig im Konfigurator kennenlernen</br>und sich dann einen ganz individuellen Modellprospekt als PDF <br/>herunterladen. Ein Klick genügt.';
									}
									$('prospektdanketext2').update(text2);
									$('probefahrtbutton').remove();
									$('konfibutton').remove();
									$('modelbackbutton').remove();
								}
								$('prospekt_danke').show();

								instance.submited = true;
							}});
						} else {
							instance.setErrorText('Bitte wählen Sie ein Modell aus.');
						}

					}
				}
			}
			else{
				instance.setErrorText('Bitte wählen Sie ein Modell aus.');
			}

			return false;
		});
	}

	this.getSelectedModels = function(){
		return $$('div.modellbox[selected=1]');
	}

	this.validatePersonData = function(){
		hasError = false;
		var errorfield = new Array;

		if ($RF('prospektForm','gender')== null) {
			errorfield.push('Anrede');
			hasError = true;
		}


		Form.getInputs($('prospektForm')).each(function(element){
				switch(element.name){

					case 'firstname':
						if(element.value == ''){
							errorfield.push('Vorname');
							hasError = true;
						}
						break;
					case 'lastname':
						if(element.value == ''){
							errorfield.push('Nachname');
							hasError = true;
						}
						break;
					case 'street_no':
						if(element.value == ''){
							errorfield.push('Straße und Hausnummer');
							hasError = true;
						}
						break;
					case 'zipcode':
						if(!element.value.match(/^[0-9]{5}$/)){
							errorfield.push('Postleitzahl');
							hasError = true;
						}
						break;
					case 'city':
						if(element.value == ''){
							errorfield.push('Ort');
							hasError = true;
						}
						break;
					case 'email':
						if(element.value != ''){
/*							if(!element.value.match(/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b$/)){
								errorfield.push('email');
								hasError = true;
							}*/
						}
						break;

					case 'purchasetype':
						if(!$RF('prospektForm', 'purchasetype')){
							errorfield.push('Art der Anschaffung');
							hasError = true;
						}
						break;


					case 'company_name':
						if($RF('prospektForm', 'purchasetype')=='commercial'){
							if(element.value == ''){
								errorfield.push('Firmenname');
								hasError = true;
							}
						}
						break;
					}
		});

		e=errorfield.uniq();
		if(!hasError){
			instance.setErrorText( '');
		} else {
			if(e.length == 1){
				errortext = 'Bitte prüfen Sie folgendes Feld: ';
			}
			else{
				errortext = 'Bitte prüfen Sie folgende Felder: ';
			}

			for(i = 0; i < e.length; i++){
				errortext += e[i];
				if(i < e.length - 1){
					errortext += ', ';
				}
			}
			instance.setErrorText(errortext);
		}
		return !hasError;
	}


	this.setErrorText = function(text){
		$$('.formerror').each(function(errortext){
			errortext.update(text);
		});
	}

	this.showAddressForm = function(){
		Effect.BlindDown($('addressform'));
	}

	this.hideAddressForm = function(){
		Effect.BlindUp($('addressform'));
	}

	this.observeCloseLayerButton = function(){
		if($('prospektEmbeded')){
			if($('buttoncompareclose')!=null){
				$('buttoncompareclose').observe('click', function(){
					hideOverlay('prospektOverlay');
				});
			}

		}
	}

	if($('submitbutton')){
		this.observeTypeSelection();
		this.observeSubmitButton();
	}
	this.observePurchasetype();
	this.observeCloseLayerButton();

	this.switchPurchasetype($RF('prospektForm', 'purchasetype'));
}


function getBackgroundPosition(element)
{
	

	pos = new Array;
	if(element!=null) {
		if (Prototype.Browser.IE){
			pos[0] = element.getStyle('backgroundPositionX');
			pos[1] = element.getStyle('backgroundPositionY');
		} else{		
			pos = element.getStyle('backgroundPosition').split(' ');
		}
	}
	return pos;
}

function setBackgroundPosition(element, posX, posY)
{
	if(element!=null) {
		if(Prototype.Browser.IE){
			element.setStyle({backgroundPositionY: posX});
			element.setStyle({backgroundPositionY: posY});
		}
		else{
			element.setStyle({backgroundPosition: posX + ' ' + posY});
		}	
	}

	
}

function showOverlay(overlayid) {
	if ($(overlayid) == null) {
		return true;
	}
	$(overlayid).hide();

	var elem = $('overlay');
	if (elem == null){
		return true;
	}
	elem.toggle();

	if (!$(overlayid).visible()) {
		try {
			var viewportHeight = document.viewport.getHeight();
			var viewportWidth = document.viewport.getWidth();
			var scroll = document.viewport.getScrollOffsets();

			var layerHeight = $(overlayid).getHeight();
			var layerWidth = $(overlayid).getWidth();

			if(layerHeight < 400 && viewportHeight > 400){
				layerHeight = 400;
			}

			//var viewportOffset = $(overlayid).viewportOffset().top;
			var viewportOffset = 0;

			$('overlay').style.height = viewportHeight + scroll.top + "px";

			topPosition = ((viewportHeight - layerHeight) / 2) + scroll.top;
			if(topPosition < 10){
				topPosition = 10;
			}

			leftPosition = ((viewportWidth - layerWidth) / 2) + scroll.left;

			if(navigator.appName == "Microsoft Internet Explorer" && (navigator.appVersion.indexOf('MSIE 6') != -1)){
				leftPosCorrection = ($('overlay').viewportOffset().left * -1);
				if(leftPosCorrection > 0){
					leftPosCorrection = 0 - leftPosCorrection;
				}

				leftPosition = -10;

				overlayWidth = document.viewport.getWidth();
				if(overlayWidth < $('outer').getWidth()){
					overlayWidth = $('outer').getWidth();
				}

				overlayHeight = ($('outer').getHeight() + $('header').getHeight());

				if(overlayHeight < viewportHeight){
					overlayHeight = viewportHeight;
				}

				$('overlay').style.position = 'absolute';
			//	$('overlay').style.left = leftPosCorrection + 'px';
				$('overlay').style.width = overlayWidth;
				$('overlay').style.height = overlayHeight + 'px';

				Event.observe(window, 'scroll', function() {
					scroll = document.viewport.getScrollOffsets();
					$('overlay').style.top = scroll.top + "px";
				});

			}

			$(overlayid).style.top = topPosition + "px";
		//	$(overlayid).style.left = leftPosition + "px";
			$('overlay').hide();
			Effect.Appear($('overlay'), {duration: 0.5 ,from:0.0, to:0.75});
			Effect.Appear($(overlayid), {duration: 0.5 ,from:0.0, to:1.0});
		}
		catch(err) {
			//alert("ERROR: "+err);
		}
	}

	$('overlay').observe('click', function(event){
		hideOverlay(overlayid);
		//Event.stopObserving($('overlay'));
	});

	$$('.detail_out').each(function(element){
		$(element).observe('click', function(event){
			hideOverlay(overlayid);
			//Event.stopObserving($(element));
		});
	});

	return true;
}

function GetofferForm(){
	currentTab = 'daten';
	instance = this;
	submited = false;
	this.observeOfferForm = function()
	{


		$('tab_daten').observe('click', function(element){
			instance.showTabContainer('daten');
		});

		$('tab_haendler').observe('click', function(element){
			instance.showTabContainer('haendler');
		});

		$('tab_kontakt').observe('click', function(element){
			if(!instance.acceptdealer()){
				instance.showTabContainer('haendler');
			}
		});
	}

	this.observeNextTabButton = function()
	{
		$$('a[class="nexttabbutton"]').each(function(e) {
			e.observe('click', function(event){
				if(nextTab){
					if(nextTab == 'kontakt'  ){
						instance.acceptdealer();

					} else{
						instance.showTabContainer(nextTab);
					}
				}
			});
		});
	}

	this.observePreviousTabButton = function()
	{
		$$('a[class="previoustabbutton"]').each(function(e) {
			e.observe('click', function(event){
				if(previousTab){
					instance.showTabContainer(previousTab);
				} else {
					document.location.href = '/forms/index/getoffer';
				}
			});
		});
	}

	this.observePurchasetype = function(){


		$$('input.purchasetype').each(function(inputelem){
			inputelem.observe('change', function(element){
				instance.switchPurchasetype($RF('getofferForm', 'purchasetype'));

			});
		});
	};


	this.switchPurchasetype = function(type){

		style = '';
		switch(type){

			case 'commercial':
						$('purchasetypemandatory').show();
			break;

			case 'private':
			default:
				$('purchasetypemandatory').hide();
			break;
		}
	};

	this.showTabContainer = function(container)
	{

		if(currentTab == 'danke'){
			instance.setErrorText('container_tab_danke', 'Ihre Daten wurden bereits gespeichert. Wir werden uns in Kürze bei Ihnen melden.');
			container = 'danke';
		}

		if($('container_tab_daten') == null) {
			return false;
		}

		//reset all
		if($('container_tab_daten')){
			$('container_tab_daten').hide();
		}

		$('container_tab_haendler').hide();
		$('container_tab_kontakt').hide();
		$('container_tab_danke').hide();

		instance.switchPurchasetype($RF('getofferForm', 'purchasetype'));
		switch(container){

			case 'daten':
				bgPos = getBackgroundPosition($('tab_daten'));
				setBackgroundPosition($('tab_daten'), bgPos[0], '-42px');
				$('container_tab_daten').show();
				nextTab = 'haendler';
				previousTab = false;
				this.gfx($F('konfimodelid'));
			break;

			case 'haendler':
				if(this.validatePersonData()){
					bgPos = getBackgroundPosition($('tab_haendler'));
					setBackgroundPosition($('tab_haendler'), bgPos[0], '-42px');
					$('getofferForm').action='/forms/json/post/name/getoffer';
					$('getofferForm').request({parameters: {name:'getoffer'}});
					$('getofferForm').action='/forms/json/form/name/getoffer';
					previousTab = 'daten';
					nextTab = 'kontakt';
					$('container_tab_haendler').show();
					if($F('dealerid')<=1000) {
						if (map==null) {
							initGoogleMap();
						}
						$('searchaddress').value=$F('street_no')+', '+$F('zipcode')+' '+$F('city');
						startSearch($F('street_no')+', '+$F('zipcode')+' '+$F('city'));

					}
					else {
						if (map==null) {

							initGoogleMap($F('searchaddress'),$F('lat'),$F('lon'),$F('dealerid'));
						}
					}

				}else{
					this.showTabContainer('daten');
					return false;
				}

			break;

			case 'kontakt':

				if(this.validatePersonData() && this.validateDealerData()){
					bgPos = getBackgroundPosition($('tab_kontakt'));
					setBackgroundPosition($('tab_kontakt'), bgPos[0], '-42px');
					$('getofferForm').action='/forms/json/post/name/getoffer';
					$('getofferForm').request({parameters: {name:'getoffer'}});
					$('getofferForm').action='/forms/json/form/name/getoffer';
					previousTab = 'haendler';
					nextTab = 'danke';
					new Ajax.Updater($('personbox'), '/forms/json/person', {onComplete: function(){
							$('container_tab_kontakt').show();
						}
					});


				}
				else{
					this.showTabContainer('haendler');
					return false;
				}
			//	$('phone').value = $('phone-1').value;
			break;

			case 'danke':

				if((this.validatePersonData() && this.validateDealerData() && this.submitFormData()) || currentTab == 'danke'){
					bgPos = getBackgroundPosition($('tab_kontakt'));
					setBackgroundPosition($('tab_kontakt'), bgPos[0], '-42px');
					var dealerid = $('mapactionacceptdealer').readAttribute('dealerid');
					new Ajax.Updater($('finalbox'), '/forms/json/offerfinal?dealerid=' + dealerid, {onComplete: function(){
							$('container_tab_danke').show();
						}
					});
					new Ajax.Request('/konfi/pdf/index/longterm/1', {
					  method: 'post',
					  onSuccess: function(transport) {
					  }
				   });


				}
				else{
					this.showTabContainer('haendler');
					return false;
				}
					break;
			}

			if(currentTab != container && container != 'danke' && currentTab!='modell' ){

				bgPos = getBackgroundPosition($('tab_' + currentTab));
				setBackgroundPosition($('tab_' + currentTab), bgPos[0], '-21px');
			}

			currentTab = container;

			return false;
	}

	this.observeAcceptDealerButton = function() {

		if($('acceptdealerbutton') == null) {
			return false;
		}

		$('acceptdealerbutton').observe('click', function(event){
			instance.acceptdealer();
		});
	}

	this.acceptdealer = function(){
		var dealerid = $('mapactionacceptdealer').readAttribute('dealerid');
		if(dealerid){
			$('dealerid').value = $('mapactionacceptdealer').readAttribute('dealerid');
			$('lat').value = $('mapactionacceptdealer').readAttribute('lat');
			$('lon').value = $('mapactionacceptdealer').readAttribute('lon');
			$('searchaddress').value = $('mapactionacceptdealer').readAttribute('searchaddress');
			Form.getInputs($('getofferForm'), 'hidden', 'dealerid').each(function(element){
				element.value = dealerid;
				new Ajax.Updater($('dealerbox'), '/forms/json/dealergetoffer?dealerid=' + dealerid, {onComplete: function(){
						instance.showTabContainer('kontakt');
					}
				});
			});

			return true;
		}
		else{
			instance.showTabContainer('kontakt');
			return false;
		}
	}





	this.validatePersonData = function(){
		hasError = false;
		var errorfield = new Array;

		formInputs = new Hash();
		sort = new Array('gender', 'firstname', 'lastname', 'purchasetype', 'company_name','street_no', 'zipcode', 'city', 'email', 'auto_hersteller', 'auto_baujahr', 'auto_zeitraum', 'drivinglicense');

		Form.getInputs($('getofferForm')).each(function(element){
			formInputs.set(element.name, element);
		});

		sort.each(function(sortitem){
				element = formInputs.get(sortitem);
				switch(sortitem){

					case 'firstname':
						if(element.value == ''){
							errorfield.push('Vorname');
							hasError = true;
						}
						break;
					case 'lastname':
						if(element.value == ''){
							errorfield.push('Nachname');
							hasError = true;
						}
						break;
					case 'street_no':
						if(element.value == ''){
							errorfield.push('Straße und Hausnummer');
							hasError = true;
						}
						break;

					case 'zipcode':
						if(!element.value.match(/^[0-9]{5}$/)){
							errorfield.push('Postleitzahl');
							hasError = true;
						}
						break;

					case 'city':
						if(element.value == ''){
							errorfield.push('Ort');
							hasError = true;
						}
						break;
					case 'email':
						if(element.value == ''){
						//	if(!element.value.match(/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b$/)){
								errorfield.push('E-Mail');
								hasError = true;
						//	}
						}
						break;

					case 'drivinglicense':
						if(element.type != 'hidden' && !element.checked){
						//	instance.setErrorText('container_tab_daten', 'Sie müssen eine gültige Fahrerlaubnis besitzen, um eine Offer buchen zu können.');
							errorfield.push('Fahrerlaubnis');
							hasError = true;
						}
						break;

					case 'gender':
						if ($RF('getofferForm','gender')== null) {
							errorfield.push('Anrede');
							hasError = true;
						}
					break;
					case 'phone':
						if(element.value != ''){
							errorfield.push('Telefon');
							hasError = true;
						}
						break;
					case 'purchasetype':
						if(!$RF('getofferForm', 'purchasetype')){
							errorfield.push('Art der Anschaffung');
							hasError = true;
						}
						break;
					case 'company_name':

						if($RF('getofferForm', 'purchasetype')=='commercial'){
							if(element.value == ''){
								errorfield.push('Firmenname');
								hasError = true;
							}
						}
						break;

				}

		});

		e=errorfield.uniq();
		if(!hasError){
			instance.setErrorText('container_tab_daten', '');
		} else {
			if(e.length == 1){
				errortext = 'Bitte prüfen Sie folgendes Feld: ';
			}
			else{
				errortext = 'Bitte prüfen Sie folgende Felder: ';
			}
			for(i = 0; i < e.length; i++){
				errortext += e[i];
				if(i < e.length - 1){
					errortext += ', ';
				}
			}
			instance.setErrorText('container_tab_daten',errortext);
		}
		return !hasError;
	}

	this.validateDealerData = function(){


		hasError = false;


		Form.getInputs($('getofferForm'), 'hidden', 'dealerid').each(function(element){
			if(element.value != ''){
				instance.setErrorText('container_tab_haendler', '');
				hasError = false;
			}
			else{
				instance.setErrorText('container_tab_haendler', 'Bitte wählen Sie einen Händler aus.');
				hasError = true;
			}
		});

		return !hasError;
	}

	this.setErrorText = function(containerId, text){
		$$('#' + containerId + ' .formerror').each(function(errortext){
			errortext.update(text);
		});
	}

	this.submitFormData = function(){
		hasError = false;
		var errorfield = new Array;

	//^(((((00|\+)49[ \-]{0,1})|0)[2-9][0-9]{1,4}[ \-]{0,1}|(((00|\+)49\()|\(0)[2-9][0-9]{1,4}\))[0-9]{1,7}[ \-]{0,1}[0-9]{1,5})$
		//if(!$F('phone').match(/^(((((00|\+)49[ \-]{0,1})|0)[1-9][0-9]{1,4}[ \-]{0,1}|(((00|\+)49\()|\(0)[2-9][0-9]{1,4}\))[0-9]{1,7}[ \-]{0,1}[0-9]{1,5})$/)){
/*		if(!$F('phone').match(/^(((((00|\+)49[ \-\/]{0,1})|0)[1-9][0-9]{1,4}[ \-\/]{0,1}|(((00|\+)49\()|\(0)[2-9][0-9]{1,4}\))[0-9]{1,7}[ \-\/]{0,1}[0-9]{1,5})$/)){
			errorfield.push('Telefon');
			hasError = true;
		}

		if($F('contact_weekday') == 'nothing'){
			errorfield.push('Wochentag');
			hasError = true;
		}

		if($F('contact_morning')== null && $F('contact_afternoon')==null ){
			errorfield.push('Zeitraum');
			hasError = true;
		}
*/

		if(!hasError && !instance.submited){
			$('getofferForm').request({
				onComplete: function(){hasError = false;instance.submited = true;},
				parameters: {name:'getoffer'}
				});
		}
		else{
			e=errorfield.uniq();
			if(!hasError){
				instance.setErrorText('container_tab_kontakt', '');
			} else {
				if(e.length == 1){
					errortext = 'Bitte prüfen Sie folgendes Feld: ';
				}
				else{
					errortext = 'Bitte prüfen Sie folgende Felder: ';
				}
				for(i = 0; i < e.length; i++){
					errortext += e[i];
					if(i < e.length - 1){
						errortext += ', ';
					}
				}
				//instance.setErrorText('container_tab_kontakt',errortext);
			}
			return false
		}

		return true;
	}

	this.observeCloseLayerButton = function(){
		if($('OfferEmbeded')){
			$('buttoncompareclose').observe('click', function(){
				hideOverlay('OfferOverlay');
			});
		}
	}

	this.gfx = function(car) {
		cars=$H({6:'sonata',7:'grandeur',8:'tucson',9:'h1_travel',10:'h1_cargo',11:'santafe',12:'ix35'});

		if(cars.keys().any(function(n) {return n == car;})) {
			$$('div[id="visual"]>img').each(function(e){
				e.src='/_gfx/modelle/'+cars.get(car)+'/aussen_key.jpg';
			});
			return true;
		}

	}

	this.button = function(car) {

		cars=$H({9:'h1_travel',10:'h1_cargo',11:'santafe'});

		if(cars.keys().any(function(n) {return n == car;})) {
			$$('div[id="konfibutton"]>div>a>img').each(function(e){
				e.src='/_gfx/beratung/'+cars.get(car)+'_button_konfi.gif';
			});
			$$('div[id="modelbackbutton"]>div>a>img').each(function(e){
				e.src='/_gfx/beratung/'+cars.get(car)+'_button_modellback.gif';
			});
			return true
		}

	}

//	this.setErrorText = function(containerId, text){
//		$$('div[class="formerror"]').each(function(errortext){
//			errortext.update(text);
//		});
//	}

	//Initialisieren aller Werte

	this.observeOfferForm();
	this.observeAcceptDealerButton();
	this.observeNextTabButton();
	this.observePreviousTabButton();
	this.observeCloseLayerButton();
	this.observePurchasetype();
	this.showTabContainer('modell');
	this.showTabContainer('daten');

	instance.switchPurchasetype($RF('getofferForm', 'purchasetype'));
}



function hideOverlay(overlayid)
{
	Effect.Fade('overlay', {duration: 0.5 ,from:0.75, to:0.0});
	Effect.Fade(overlayid,{duration: 0.5 ,from:1.0, to:0.0});

	if($(overlayid).readAttribute('reloadtab')){
		mainTabHdl.showTab(false, $(overlayid).readAttribute('reloadtab'));
	}
}

