// Pflichtfelder definieren:
var zuPruefen   = "";
var pruefFelder = new Array();
	pruefFelder['empty'] = "";
	pruefFelder['downloadprospekt'] = "DownloadProspekt_01,Anrede,Vorname,Name,E_mail,Einverstaendnis_der_Speicherung";
	pruefFelder['prospekt'] = "Prospekt_01,Anrede,Vorname,Name,Strasse,Hausnummer,PLZ,Ort,E_mail,Einverstaendnis_der_Speicherung";
	pruefFelder['probefahrt'] = "Probefahrt_01,Anrede,Vorname,Name,Strasse,Hausnummer,PLZ,Ort,Telefon_privat,Telefon_beruflich,E_mail,Einverstaendnis_der_Speicherung";
	pruefFelder['newsletter'] = "Anrede,Vorname,Name,E_mail,Einverstaendnis_der_Speicherung";
var pruefInhalte  = "PLZ,Telefon_privat,Telefon_beruflich,Telefon_mobil,E_mail,Geburtsdatum_tt,Geburtsdatum_mm,Geburtsdatum_jj,Kilometerstand,Baujahr";
var pruefFeldTyp  = "Plz,Telefon,Telefon,Telefon,Email,Tag,Monat,Jahr,Nummer,Jahr";

// Allgemeine Variablen:
var myHdlSrchURL  = "/index.cfm?p=prospekte-probefahrt";
var myModulePath  = "/mymodules/service.citroen.de/";
var myContactType = "";
var myChoice      = "privat";
var AktivesBild   = 'img_wkwfst';
var lastSelectId  = "";
var prospektCnt   = 1;
var probefahrtCnt = 1;
var fehlercount;
var g_PopupIFrame;

// Bereits gewählte Prospekte:
var weg_damit = new Array();
	weg_damit['Prospekt'] = new Array();
	weg_damit['Probefahrt'] = new Array();
	weg_damit['DownloadProspekt'] = new Array();



// Funktionen zu Prospekt und Probefahrt - Zeige NUR Auswahl:
function showOnlyAndDirect(mySelect) {
	if(mySelect == 'prospekte') {
		mySelect = 'prospekt';
		selectType = mySelect.capitalize();
		hideThis = $('probefahrt');
	}
	if(mySelect == 'probefahrt') {
		selectType = mySelect.capitalize();
		hideThis = $('prospekt');
	}
	hideThis.hide();
	hideThis.next('label').hide();
	$(mySelect).checked = true;
	showHideCarSelect(selectType);
}

// Funktionen zu Prospekt und Probefahrt - Zeige Auswahl:
function showHideCarSelect(mySelect) {
	var mySelectDiv = mySelect + "_array";
	var mySelectHr  = "hr_pp";
	
	myContactType = mySelect;
	changeFormCheckFields();
	if($(mySelect.toLowerCase()).checked == true) { 
		$(mySelectDiv).show();
	}
	else {
		$(mySelect + '_01').selectedIndex = 0;
		$(mySelectDiv).hide();
		$(mySelectHr).hide();
		if(!$('prospekt').checked && !$('probefahrt').checked){
			if($('typ_choice')){
				$('typ_choice').hide();
				$('hr_choice').hide();
				$('hr_pp').show();
				setzteAktivesBild('img_ddbwvi');
			}
			if($('addData')){
				$('addData').hide();
				$('hr_basics').hide();
				$('hr_pp').hide();
			}
			if($('basics') && !$('newsletter').checked){
				$('basics').hide();
				$('submitbg').show();
				$('submithide').hide();
			}
		}
	}
}

// Funktionen zu Prospekt und Probefahrt - "weitere Modelle"-Link anzeigen:
function c_link_check(type,num) {
	var meinSelect = type + '_0' + num;
	var auswahl    =  $(meinSelect)[$(meinSelect).selectedIndex].value;	
	switch (type){
		case "DownloadProspekt" :
			myOptionVal= auswahl;
			myLink = 'adddownloadprospekt';
			myCnt=5;
		break;
		
		case "Prospekt" :
			myOptionVal= auswahl;
			myLink = 'addprospekt';
			myCnt=5;
		break;
		
		case "Probefahrt" :
			myOptionVal= $(meinSelect)[$(meinSelect).selectedIndex].text;
			myLink = 'addprobefahrt';
			myCnt=3;
		break;
	}
	if(num < myCnt) {
		if(auswahl == '') $(myLink).hide();
		else {
			weg_damit[type][num-1] = myOptionVal;
			$(myLink).show();
			if($('typ_choice')){
				if(!$('typ_choice').visible()){
					$('typ_choice').show();
					$('hr_pp').show();
					if($('newsletter').checked){
						$('addData').show();
						$('hr_basics').show();
						$('hr_choice').show();
					}
				}
				setzteAktivesBild('img_ihre_daten');
			}
		}
	}
}
// Funktionen zu Prospekt und Probefahrt - weiteres Dropdown anzeigen:
function addField(type){
	var myDiv           = type + "_selects";
	var mySelect        = type + "_01";
	var myCnt           = 0;
	var newFieldContent = "";
	
	thisCnt = prospektCnt;
	myCnt   = prospektCnt +1;
	myName  = type + "_0" + myCnt;
			
	switch (type){
		case "DownloadProspekt" :
			$('adddownloadprospekt').hide();		
			if(myCnt==6) return false;
			prospektCnt += 1;	
			label = "Prospekt";
		break;
		
		case "Prospekt" :
			$('addprospekt').hide();		
			if(myCnt==6) return false;
			prospektCnt += 1;	
			label = "Prospekt";
		break;
		
		case "Probefahrt" :
			thisCnt = probefahrtCnt;
			myCnt = probefahrtCnt +1;
			myName = "Probefahrt_0" + myCnt;
			$('addprobefahrt').hide();
			if(myCnt==4) return false;
			probefahrtCnt += 1;	
			label = "Probefahrt";
		break;
	}
	selectId     = type + "_0";
	lastSelectId = selectId + thisCnt;
	$(lastSelectId).setStyle({border:'none',color:'#260313'});
	
	newFieldContent = '<br style="clear:left;"/><label for="' + selectId +  myCnt + '" class="label_links">' + label + ' ' + myCnt + '</label>';
	newFieldContent += '<select name="' + myName + '" id="' + selectId +  myCnt + '" class="' + type + '" onchange="c_link_check(\'' + type +'\',' + myCnt +');">';
	
	myOptions = $(mySelect).options;
	$A(myOptions).each(function(option) {	
		switch (type){
			case "DownloadProspekt" :
				myOptionVal= option.value;
			break;
			
			case "Prospekt" :
				myOptionVal= option.value;
			break;
			
			case "Probefahrt" :
				myOptionVal= option.text;
			break;
		}
		if(weg_damit[type].indexOf(myOptionVal) == -1){
			newFieldContent += '<option value="' + option.value + '">' + option.text + '</option>';
		}
	});
	newFieldContent += '</select>';
	$(myDiv).insert(newFieldContent);
}

// Newsletter-Auswahl: Pflichtfelder anpassen und anzeigen
function zeigeNewsletterRel() {
	if($('newsletter').checked){
		if(!$('prospekt').checked && !$('probefahrt').checked) {
			myContactType = "newsletter";
		}
		if(!$('addData').visible()){
			showBasics('privat');
			$('hr_basics').hide();
		}
		if($('Firmenname'))showBasics('privat');
		$('submitbg').hide();
		$('submithide').show();
		setzteAktivesBild('img_ddbwvi');
	}	
	else {
		if(!$('prospekt').checked && !$('probefahrt').checked) { 
			$('basics').hide();	
			$('hr_choice').hide();
			$('submitbg').show();
			$('submithide').hide();
			$('hr_pp').hide();
		}
	}
}


//zu prüfende Pflichtfelder ändern:
function changeFormCheckFields(){
	var altZuPruefen = $A(zuPruefen.split(','));
	var type = "empty";
	if($('newsletter').checked)type="newsletter";
	if($('prospekt').checked)type="prospekt";
	if($('probefahrt').checked)type="probefahrt";
	
	
	altZuPruefen.each(function(myitem) {
		if(myitem.length&& $(myitem.toLowerCase())){
			meinObj  = $(myitem.toLowerCase());				
			if(typeof(meinObj.down('em'))!='undefined'){
				meinObj.down('em').hide();
			}
		}
	});
	
	var neuZuPruefen = $A(pruefFelder[type].split(','));
	neuZuPruefen.each(function(myitem) {
		if(myitem.length && $(myitem.toLowerCase())){
			meinObj  = $(myitem.toLowerCase());			
			if(typeof(meinObj.down('em'))!='undefined'){
				meinObj.down('em').show();
			}
		}
	});
	zuPruefen = pruefFelder[type];
}

//Pflichtfelder anzeigen:
function showBasics(type) {
	loadFormSnippet(type,'basics',myContactType); 	
	if(myContactType != "newsletter"){
		$('addData').show(); 
	}
	$('submitbg').hide();	
	$('hr_choice').show(); 
	$('submithide').show(); 
	setzteAktivesBild('img_ddbwvi');	
}

//Zusätzliche Info-Felder von Snippet laden:
function loadFormSnippet(myType,snippet,check){
	if(myType.length){
		myChoice = myType;
		myAjaxUrl = myModulePath + 'snippets/' + snippet + '_' + myType + '.html';
	}
	else {
		myAjaxUrl = myModulePath + 'snippets/' + snippet + '.html';
	}
	
	myButton = $('button_' + myType);
	$(snippet).show();
	if($('hr_' + snippet))$('hr_' + snippet).show();
	
	if($(myButton)){
		$(myButton).setStyle({backgroundPosition:'top'});
		if($(myButton).next('a')){
			$(myButton).next('a').setStyle({backgroundPosition:'bottom'});
		}
		else if($(myButton).previous('a')){
			$(myButton).previous('a').setStyle({backgroundPosition:'bottom'});
		}
	}
	new Ajax.Request(myAjaxUrl,
		{		  
		method:'get',
		onSuccess: function(transport){
		var response = transport.responseText || "";
		if(response.length)$(snippet + 'Content').update(response);
		if(check.length)changeFormCheckFields();
		}
	});
}

// Weitere Info-Felder zuklappen
function zuklappen(id, img_id) {
	$(id).innerHTML = '';
	setzteAktivesBild('img_ddbwvi');
	$(img_id).width = '353';
	$(img_id).height = '24';
	$(img_id).src = myModulePath + 'templates/bilder/' + img_id + '.png';
}
//Aktives Bild im aktuellen Fieldset setzen:
function setzteAktivesBild(img_id) {
	oldimg = $(AktivesBild).src;
	newimg = oldimg.replace(/_aktiv/,'_normal');
	$(AktivesBild).src = newimg;
	if ($(img_id).name != 'specialButton') {
		newoldimg = $(img_id).src;
		newnewimg = newoldimg.replace(/_normal/, '_aktiv');
		$(img_id).src = newnewimg;
	} else {
		newnewimg = myModulePath + 'templates/bilder/' + img_id + '_aktiv.png';
		switch (img_id) {
			case 'duerfen_wir_sie_besser_kennenlernen':
				$(img_id).width = '283';
				$(img_id).height = '28';
				break;
			case 'ich_fahre_zur_zeit':
				$(img_id).width = '164';
				$(img_id).height = '28';
				break;
			case 'haendlerauswahl':
				$(img_id).width = '144';
				$(img_id).height = '28';
				break;
		}
		$(img_id).src = newnewimg;
	}
	AktivesBild = img_id;
}

//Formular-Überprüfung - allgemeine Überprüfung:
function form_check(form_name) {
	var testEmail   = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var testTelefon = /^([0+]?[0-9]+)([ +-\.\/ ]*[0-9])+$/;
	var testPlz     = /^[0-9]{5}/;
	var testTag     = /^([1-2][0-9])|(30|31)/;
	var testMonat   = /^((0[1-9])|(1[0-2]))/;
	var testJahr    = /^[19|20][0-9]{2}/;
	var testNummer  = /^[0-9]+/;
	var radioGroups = "";
	var fehler      = "";
	var fehlercount = 0;
	var pruefe      = zuPruefen.split(",");
	var pruefDas    = pruefInhalte.split(",");
	var pruefSo     = pruefFeldTyp.split(",");
	var felder      = $(form_name).getElements();
	felder.each(function(item) {
		if($(item)){
			meinTyp   = $(item).type;
			meinTitle = $(item).title;
			if(meinTyp && meinTitle.length){
				/*
				Felder deren Inhalt auf korrekte Form überprüft werden:
				Felder kommen aus globaler Variable       --> pruefInhalte
				Prüf-Formate kommen aus globaler Variable --> pruefFeldTyp
				*/
				if(pruefDas.indexOf($(item).id)!=-1) {
					filterSo = eval("test" + pruefSo[pruefDas.indexOf($(item).id)]);
					if($F(item).length&&!filterSo.test($F(item))){
						if(meinTitle.match(/(\w+) eine (\w+)/))fehler += '<li>'+ meinTitle.replace(/(\w+) eine (\w+)/,"$1 eine korrekte $2") +'</li>';
						if(meinTitle.match(/(\w+) ein (\w+)/))fehler += '<li>'+ meinTitle.replace(/(\w+) ein (\w+)/,"$1 ein korrektes $2") +'</li>';
						if(meinTitle.match(/(\w+) einen (\w+)/))fehler += '<li>'+ meinTitle.replace(/(\w+) einen (\w+)/,"$1 einen korrekten $2") +'</li>';
					}
				}
				/*
				generelle Pflichtfelder:
				Felder kommen aus globaler Variable --> zuPruefen
				*/
				if(pruefe.indexOf($(item).id)!=-1){
					if($F(item) == ''){
						fehler += '<li>'+ meinTitle +'</li>';			
						fehlercount += 1; 
					}
					if(meinTyp == 'checkbox') {
						if($(item).checked == '') {
							fehler += '<li>'+ meinTitle +'</li>';		
							fehlercount += 1; 
						}
					}
					if(meinTyp == 'radio') {
						var thisRadioChecked = false;
						$A(meinName).each(function(radio) {
							if(radio.checked){
								thisRadioChecked = true;		
								fehlercount += 1; 
							}
						});
						if(!thisRadioChecked && radioGroups.indexOf(meinName)==-1){
							fehler += '<li>'+ meinTitle +'</li>';
							radioGroups += meinName 		
							fehlercount += 1; 
						}
					}
				}				
			}
		}
	});
	if (fehler != '') {
		var fehlertext = '<div id="msgwrap"><div id="msgtop" style="text-align:right;padding:2px 24px 0 0;"><a href="#" onclick="hide_msg_box()" title="schlie&szlig;en">X</a></div><div id="msgcont"><h4>Folgende Felder wurden nicht (korrekt) ausgef&uuml;llt:</h4>';
		fehlertext += '<ul>' + fehler + '</ul></div><div id="msgbottom"></div></div>';
		$('msg_box').innerHTML = fehlertext;
		var msgtoph = $('msgtop').getStyle('height').replace(/px/,'');
		var msgtopw = $('msgbottom').getStyle('height').replace(/px/,'');
		var hoehe = Number(msgtoph) + Number(msgtopw) + (fehlercount*18);
		show_msg_box(hoehe);
		return false;
  }
}

//Formular-Überprüfung - Händlerauswahl: Händlerdaten splitten und in entsprechende Felder schreiben:
function chkHdlNr(field){
	var arrInfo       = field.innerHTML.split("<br>");
	if(Prototype.Browser.IE)arrInfo = field.innerHTML.split("<BR>");
	var hdlNr         = field.id//parseInt(arrInfo[0].stripTags());
	var hdlName       = arrInfo[0].stripTags();
	var hdlAdresse    = arrInfo[1];
	$('PLZBereich').value   = hdlAdresse.split(",")[1].substr(1,5);
	$('HaendlerName').value = hdlName;
	$('HaendlerNr').value   = hdlNr;
}

//Formular-Überprüfung - Funktionen zur Infobox - IE-Check:
function IsIE6() {
	return ( Prototype.Browser.IE && (typeof window.XMLHttpRequest == "undefined"));
}

//Formular-Überprüfung - Funktionen zur Infobox - Infobox verstecken:
function hide_msg_box() {
	$('msg_box').hide();
	HidePopupDiv('msg_box');
	$('c_formular').stopObserving('click',hide_msg_box);
}

//Formular-Überprüfung - Funktionen zur Infobox - Infobox anzeigen:
function show_msg_box(height) {
	if($('addData') && $('addData').visible())$('msg_box').setStyle({bottom:'-50px'});
	else $('msg_box').setStyle({bottom:'150px'});	
	$('msg_box').show();
	ShowPopupDiv('msg_box', height);
	$('c_formular').observe('click', hide_msg_box);
}

//Formular-Überprüfung - Funktionen zur Infobox - Infobox verstecken - IE6-Hack:
function HidePopupDiv(divID) {
	var divPopup;
	divPopup=$(divID);
	divPopup.style.visibility = "hidden";
	if (IsIE6()) {
		document.body.removeChild(g_PopupIFrame);
		g_PopupIFrame = null;
	}
}

//Formular-Überprüfung - Funktionen zur Infobox - Infobox anzeigen - IE6-Hack:
function ShowPopupDiv(divID, hoehe) {
	var divPopup=$(divID);
	if (!IsIE6()) {
			divPopup.style.visibility ="visible";
			return;
	}
	divPopup.style.zIndex=divPopup.style.zIndex+1;
	var iFrame = new Element('iframe',{'src':'','frameborder':'0'});
	$(iFrame).setStyle({
		position:'absolute',
		filter:'alpha(opacity=0)',
		left: (divPopup.offsetLeft + 8) + 'px',
		top: $(divID).offsetTop + 2 + 'px',
		width: '390px',
		height: hoehe + 'px',
		border: 'none',
		outline: 'none'
	});
	document.body.appendChild(iFrame);
	g_PopupIFrame = iFrame;
	divPopup.style.visibility ="visible";
}
