var mandatoryFormFieldsObj={

		mandatoryFieldsCatalogue: new Array('vorname', 'name', 'strasse', 'plz', 'ort', 'weiterleiten'),
		mandatoryFieldsContact: new Array('vorname', 'name', 'email', 'weiterleiten', 'nachricht'),
		checkForm: 'checkformContact',
		
		setMandatoryFieldsCatalogue: function(){
			for (i=0; i<mandatoryFormFieldsObj.mandatoryFieldsContact.length; i++) {
				var tmp=jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsContact[i]).val();
				tmp=tmp.replace('*', '');
				jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsContact[i]).val(tmp);
			}
			
			for (i=0; i<mandatoryFormFieldsObj.mandatoryFieldsCatalogue.length; i++) {
				var tmp=jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsCatalogue[i]).val();
				tmp=tmp.replace('*', '');
				
				var name=mandatoryFormFieldsObj.mandatoryFieldsCatalogue[i];
				var compare=formObj.fieldsHash.get(name);
				
				if (jQuery('#'+name).val()!=fieldsObj.getField(name)) {
					continue;
				}
				
				if (compare) {
					compare=compare.replace('*', '');
					if (tmp==compare) {
						jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsCatalogue[i]).val(tmp+'*');
					}
				}
				else
					jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsCatalogue[i]).val(tmp+'*');
			}
		
			mandatoryFormFieldsObj.checkForm='checkformCatalogue';
		},
		
		setMandatoryFieldsContact: function() {
			for (i=0; i<mandatoryFormFieldsObj.mandatoryFieldsCatalogue.length; i++) {
				var tmp=jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsCatalogue[i]).val();
				tmp=tmp.replace('*', '');
				jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsCatalogue[i]).val(tmp);

			}
			
			for (i=0; i<mandatoryFormFieldsObj.mandatoryFieldsContact.length; i++) {
				var tmp=jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsContact[i]).val();
				tmp=tmp.replace('*', '');
				var name=mandatoryFormFieldsObj.mandatoryFieldsContact[i];
				var compare=formObj.fieldsHash.get(name);
				
				if (jQuery('#'+name).val()!=fieldsObj.getField(name)) {
					continue;
				}
				
				if (compare) {
					compare=compare.replace('*', '');
					if (tmp==compare) {
						jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsContact[i]).val(tmp+'*');
					}
				}
				else
					jQuery('#'+mandatoryFormFieldsObj.mandatoryFieldsContact[i]).val(tmp+'*');
			}
			
			
			mandatoryFormFieldsObj.checkForm='checkformContact';
		},
		
		init: function() {
			jQuery('#c1, #c2').click(function() {
				if (jQuery('#c1').attr('checked') || jQuery('#c2').attr('checked')) {
					jQuery('.form-required').removeClass('form-required');
					mandatoryFormFieldsObj.setMandatoryFieldsCatalogue();
				} else {
					jQuery('.form-required').removeClass('form-required');
					mandatoryFormFieldsObj.setMandatoryFieldsContact();
				}
				});
		}
}

jQuery(document).ready(function() {
	mandatoryFormFieldsObj.init();
	
	// init form fields according to selected page
	var l=document.location.href;
	
	if (l.match(/contact_type=stilikon/i)) {
		mandatoryFormFieldsObj.setMandatoryFieldsCatalogue();
		jQuery('#c2').attr('checked', 'true');
	}
	else if (l.match(/contact_type=katalog/i)) {
		mandatoryFormFieldsObj.setMandatoryFieldsCatalogue();
		jQuery('#c1').attr('checked', 'true');
	}
	else {
		mandatoryFormFieldsObj.setMandatoryFieldsContact();
	}
});

function checkIfEmptyOrDefault(val, probe) {
	if (val.length==0) {
		return false;
	}
	
	val=val.replace('*', '');
	
//	if (val.match(probe))
	if (val==probe) {
		val=val.replace('*', '');
		return true;
	}
	else
		return false;
}

//copyright   : (c) macrocom gmbh

var formObj={
	fieldsHash: new Hash(),
	useIDs: false,
	resetToDefaultValue: function(form, field) {
		// reset original input text to input fields if value is empty
		var target=Array();
		if (formObj.useIDs)
			target=$(form).getElements('input[id="'+field+'"]');
		else
			target=$(form).getElements('input[name="'+field+'"]');
		if (target && target[0]) {
			if (target[0].value=='')
				target[0].value = formObj.fieldsHash.get(field);
		}
	},
	
	getPageID: function() {
		// get page id
		var pageID=null;
		var re =/.*id=(\d+)*/;
		var tmp=document.location.href.match(re);
		if (tmp && tmp[1]) 
			pageID = tmp[1]
		else {
			re =/.*\?(\d+)*/;
			tmp=document.location.href.match(re);
			if (tmp && tmp[1]) 
				pageID = tmp[1]
		}
		return pageID;
	},
	
	init: function(form) {
		// save all text fields to internal hash
		formObj.fieldsHash=new Hash();
		formObj.useIDs=false;
		
		var fields=$(form).getElements('input[type="text"]');
		var fieldsHash=new Hash();
		
		for (var i = 0; i < fields.length; i++) {
			// not for email field, it may be empty
			if (fields[i].id != 'email') {
				formObj.fieldsHash.set(fields[i].id, fields[i].value);
				fields[i].addEvent('blur', function(){
					formObj.resetToDefaultValue(form, this.id);
				});
			}
		} // /for
		
		var pageID=formObj.getPageID();
		
		// set default values to checkboxes
		/*
		if (pageID!=null) {
			if (pageID==55) { // Katalog bestellen
				var c1=$('c1');
				var c2=$('c2');
				
				if (c1)
					c1.setProperty('checked', true);
				if (c2)
					c2.setProperty('checked', false);
			}
			if (pageID==815) { // Katalog bestellen
				var c1=$('c1');
				var c2=$('c2');
				
				if (c1)
					c1.setProperty('checked', false);
				if (c2)
					c2.setProperty('checked', true);
			}
*		
		
			if (pageID==16) { // Kontakt
				var c1=$('c1');
				var c2=$('c2');
				
				if (c1)
					c1.setProperty('checked', false);
				if (c2)
					c2.setProperty('checked', false);
			}
	
		}*/
	}
};

// only if mootools is installed (maintaining compatibility with installations w/o mootools)
if (window.addEvent) {
	window.addEvent('domready', function(){
		var page=formObj.getPageID();
		if (page == 17) {
			formObj.init('form1');
			formObj.useIDs=true;
		}
		else 
			formObj.init('form0');
	});
}	

function removeAsterisks() {
	var elems=jQuery('#form0 :input');
	if (elems) {
		for (i=0; i<elems.length; i++) {
			var tmp=(jQuery(elems[i]).val());
			tmp=tmp.replace('*', '');
			jQuery(elems[i]).val(tmp);
		}
	}
}
function checkformCatalogue(form){
    var message='';	
	var tofocus='';	
	var mail = form.email.value;
	
	if (checkIfEmptyOrDefault(form.vorname.value, 'Vorname')){
	 message+="\n* Bitte tragen Sie Ihren Vornamen ein.";
	 jQuery(form.vorname).addClass('form-required');
	 if(!tofocus)
	 	tofocus='vorname';
	}

	if (checkIfEmptyOrDefault(form.name.value, 'Name')){
	 message+="\n* Bitte tragen Sie Ihren Namen ein.";
	 jQuery(form.name).addClass('form-required');
	 if(!tofocus)
	 	tofocus='name';
	}
	if (checkIfEmptyOrDefault(form.strasse.value, 'Straße')){
		message+="\n* Bitte tragen Sie Ihre Straße ein.";
		jQuery(form.strasse).addClass('form-required');
		if(!tofocus)
			tofocus='strasse';
	}
	if (checkIfEmptyOrDefault(form.plz.value, 'PLZ')){
		message+="\n* Bitte tragen Sie Ihre Postleitzahl ein.";
		jQuery(form.plz).addClass('form-required');
		if(!tofocus)
			tofocus='plz';
	}
	if (checkIfEmptyOrDefault(form.ort.value, 'Ort')){
		message+="\n* Bitte tragen Sie Ihren Wohnort ein.";
		jQuery(form.ort).addClass('form-required');
		if(!tofocus)
			tofocus='ort';
	}

	
	if (!form.weiterleiten.checked) {
  message+="\n* Bitte akzeptieren Sie die Datenschutzerklärung.";
  jQuery(form.weiterleiten).addClass('form-required');
	 if(!tofocus)
	 	tofocus='weiterleiten';
}
				
	if(!tofocus) {
		removeAsterisks(form);
		form.submit();
	}
	else{
		message="Bitte ändern Sie Ihre Eingabe:"+message;
//		alert(message);
		eval('form.'+tofocus+'.focus()');
		return false;
	}
}

function checkformContact(form){
	  var message='';	
		var tofocus='';	
		var mail = form.email.value;
		
		if (checkIfEmptyOrDefault(form.vorname.value, 'Vorname')){
		jQuery(form.vorname).addClass('form-required');
		 message+="\n* Bitte tragen Sie Ihren Vornamen ein.";
		 if(!tofocus)
		 	tofocus='vorname';
		}

		if (checkIfEmptyOrDefault(form.name.value,'Name')){
			jQuery(form.name).addClass('form-required');
		 message+="\n* Bitte tragen Sie Ihren Namen ein.";
		 if(!tofocus)
		 	tofocus='name';
		}
		
		if(mail){
			checkmail=validateEmail(mail);
			if(checkmail!=true){
				jQuery(form.email).addClass('form-required');
				message+=checkmail;
				if(!tofocus)
			 		tofocus='email';
			}
		}
		
		if (form.nachricht.value.match(/Nachricht an uns \.\.\./i) || form.nachricht.value.length==0){
			jQuery(form.nachricht).addClass('form-required');
		 message+="\n* Bitte tragen Sie eine Nachricht ein.";
		 if(!tofocus)
		 	tofocus='name';
		}
		
		
		if (!form.weiterleiten.checked) {
    message+="\n* Bitte akzeptieren Sie die Datenschutzerklärung.";
    jQuery(form.weiterleiten).addClass('form-required');
		 if(!tofocus)
		 	tofocus='weiterleiten';
  }
					
		if(!tofocus) {
			removeAsterisks(form);
			form.submit();
		}
		else{
			message="Bitte ändern Sie Ihre Eingabe:"+message;
//			alert(message);
			eval('form.'+tofocus+'.focus()');
			return false;
		}
}

function checkform(form) {
	jQuery('.form-required').removeClass('form-required');
	if (mandatoryFormFieldsObj.checkForm=='checkformContact') {
		checkCaptcha();
		checkformContact(form);
	}
	else {
		checkCaptcha();
		checkformCatalogue(form);
	}
}


function checkformen(form){
    var message='';	
		var tofocus='';	
		var mail = form.email.value;
		
		if (form.vorname.value == '' || form.vorname.value=='first name'){
		 message+="\n* Please enter your first name.";
		 if(!tofocus)
		 	tofocus='vorname';
		}

		if (form.name.value == '' || form.name.value=='surname'){
		 message+="\n* Please enter your surname.";
		 if(!tofocus)
		 	tofocus='name';
		}
		
		if (!form.weiterleiten.checked) {
      message+="\n* Please accept the data protection regulation.";
		 if(!tofocus)
		 	tofocus='weiterleiten';
    }
	/*
    if(mail){
			checkmail=validateEmail(mail);
			if(checkmail!=true){
				message+=checkmail;
				if(!tofocus)
			 		tofocus='email';
			}
		}
		*/
					
		if(!tofocus)
			form.submit();
		else{
			message="Please check your input:"+message;
//			alert(message);
			eval('form.'+tofocus+'.focus()');
			return false;
		}
}



function checkform3(form){
    var message='';	
		var tofocus='';	
		var mail = form.email.value;
		
		if (form.vorname.value == '' || form.vorname.value=='Vorname'){
		 message+="\n* Bitte tragen Sie Ihren Vornamen ein.";
		 if(!tofocus)
		 	tofocus='vorname';
		}

		if (form.name.value == '' || form.name.value=='Name'){
		 message+="\n* Bitte tragen Sie Ihren Namen ein.";
		 if(!tofocus)
		 	tofocus='name';
		}
		
		if (form.strasse.value == '' || form.strasse.value=='Straße'){
		 message+="\n* Bitte tragen Sie Ihre Straße ein.";
		 if(!tofocus)
		 	tofocus='strasse';
		}
		
		if (form.ort.value == '' || form.ort.value=='Ort'){
		 message+="\n* Bitte tragen Sie Ihren Wohnort ein.";
		 if(!tofocus)
		 	tofocus='ort';
		}

		if (form.name.value == '' || form.name.value=='E-mail'){
		 message+="\n* Bitte tragen Sie Ihre E-Mail ein.";
		 if(!tofocus)
		 	tofocus='email';
		}
                  
                  if (form.captcha.value.length<5) {
		      message+="\n* Bitte tragen Sie den Sicherheitscode ein.";
		 if(!tofocus)
		 	tofocus='captcha';
	         }

		
		if (!form.gewinnspiel.checked) {
      message+="\n* Bitte akzeptieren Sie die Teilnahmebedingungen.";
		 if(!tofocus)
		 	tofocus='weiterleiten';
    }
	
    if(mail){
			checkmail=validateEmail(mail);
			if(checkmail!=true){
				message+=checkmail;
				if(!tofocus)
			 		tofocus='email';
			}
		}
		
					
		if(!tofocus)
			form.submit();
		else{
			message="Bitte überprüfen Sie Ihre Eingabe:"+message;
//			alert(message);
			eval('form.'+tofocus+'.focus()');
			return false;
		}
}



function checkRegister(form){
    var mail = form.email.value;
		var message='',tofocus='';
					
		checkmail=validateEmail(mail);
		if(checkmail!=true){
			message+=checkmail;
			if(!tofocus)
		 		tofocus='email';
		}
				
		
		if(!tofocus)
			form.submit();
		else{
			message="Bitte ändern Sie Ihre Eingabe:"+message;
//			alert(message);
			eval('form.'+tofocus+'.focus()');
			return false;
		}
} 

function validateEmail(address){
		var message='';
		if (address == ''){
			message+="\n* Bitte tragen Sie Ihre E-Mail-Adresse ein.";
			return message;
		}
		if (!address.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)){
     	message+="\n* Ihre E-Mail-Adresse enthält Fehler, bitte überprüfen Sie die Schreibweise.";
	  	return message;
		}
		return true;
}

function checkCaptcha() {
	
	if (jQuery('#captcha').length>0 && (jQuery('#captcha').val()=='undefined' || jQuery('#captcha').val()=='') ) {
		jQuery('#captcha').addClass('form-required');
	}
	else {
		jQuery('#captcha').removeClass('form-required');
	}
}

// watch out - this is a mix of mootools and jquery :-|
function lightboxHtml(html){
        if($('lightbox').style.display!='block'){
            $('lightbox').style.opacity=0;
            $('lightbox').style.display='block';
        }
        if($('lightbox').style.opacity==1)
            myfx=new Fx.Style('lightbox','opacity').start(1,0);

        else {
            jQuery('#lightbox').html(html);
            myfx=new Fx.Style('lightbox','opacity').start(0,1);
        }
    }


// additional functions for merchant preselection by postcode 	
function getMerchantPreselection() {
	var inputValuePostcode=jQuery('#plz').val();
	
	jQuery('#wrapper-input-postcode').html('');
	jQuery.get("http://www.wk-wohnen.de/index.php?id=970", { postcode: inputValuePostcode},
	 function(data){
	
	if (data.error) {
	   jQuery('#wrapper-input-postcode').html('');
	}
	else {
	   //jQuery('#wrapper-input-postcode').html(data.html);
		jQuery('#lightbox').addClass('form');
		lightboxHtml(data.html);
		jQuery('#lightbox').focus();
		jQuery('#mid').val(jQuery(jQuery('#lightbox-content-merchant-list input.checkbox')[0]).val());
	}
	  }, 'json');
}

jQuery(document).ready( function() {
	jQuery('input#c1').click( function() {
		if (jQuery(this).attr('checked')) {
			getMerchantPreselection();
		}
		else {
			jQuery('#wrapper-input-postcode').html('');
		}
	});
	
	// form field
	jQuery('input#plz').keyup( function() {
		if (jQuery('input#c1').attr('checked')) {
			if (jQuery('input#plz').val().length>4) {
				getMerchantPreselection();
			}
		}
	});
	
	// bind action for future select fields received via ajax
	jQuery('#lightbox-content-merchant-list input.checkbox').live('click', function() {
		// close lightbox
		lightboxHtml(''); 
		
		// inject selected param
		jQuery('#mid').val(jQuery(this).val());
	});
	
	
});

