/**
 * @author Marcel Fiedrich
 */

var formChecker = function() {
	
	var scForm = $('scForm');
	if (scForm != null) {
		var submitButton = $('fieldSubmit');
		var inputFields = $$('input');
	}
	
	
	/*
	 * This function was for a special campaign at the loewe website
	 */
	var zipCodeSearch = new function() {
		var zipCodeContainer = $('zipCodeContainer');
		var artSLDealerInput = $('artSLDealer');
		if (zipCodeContainer != null) {
			submitButton.setStyle('display', 'none');
			var artSLZipCode = $('artSLZipCode');
			artSLZipCode.setProperty('name', Math.random());
			var dealerLabel = null;
			var dealerSelect = new Element('select', {
				'name':'dealerSelect',
				'id':'dealerSelect'
			});
			var dealerFirstOption = null;
			var dealerOption = null;
			var dealerSelectValue = null;
			var theDealer = new Element('p', {
				'class':'theDealer'
			});
			
			function zipCodeRequest(event) {
					zipCodeContainer.empty();
					var zipCodeValue = artSLZipCode.value.charAt(0);
					dealerLabel = new Element('label', {
						'html':'W&auml;hlen Sie Ihren H&auml;ndler:',
						'for':'dealerSelect'
					});
					dealerFirstOption = new Element('option', {
						'html':'W&auml;hlen Sie Ihren H&auml;ndler',
						'value':''
					});
					var jsonRequest = new Request.JSON({
						'url': '/fileadmin/js/zipCodeList.js',
						onComplete: function(jsonObjs){
							var zipCodeArr = jsonObjs.artSLZipCode;
							dealerSelect.empty();
							dealerFirstOption.inject(dealerSelect);
							zipCodeArr.each(function(element, index){
								if(zipCodeValue == element.zipcode) {
									var dealerArr = element.dealers;
									dealerArr.each(function(dealer, index){
										dealerOption = new Element('option', {
											'html':dealer.zip + '&nbsp;' + dealer.city + ' - ' + dealer.name,
											'value':dealer.name + ' - ' + dealer.zip + ' - ' + dealer.city
										});
										dealerOption.inject(dealerSelect);
									});
									dealerLabel.inject(zipCodeContainer);
									dealerSelect.inject(zipCodeContainer);
									dealerSelect.addEvent('change', function(){
										theDealer.empty();
										submitButton.setStyle('display', 'none');
										dealerArr.each(function(dealer, index){
											dealerSelectValue = dealer.name + ' - ' + dealer.zip + ' - ' + dealer.city;
											if(dealerSelect.value == dealerSelectValue) {
												theDealer.set('html', '<strong>' + dealer.name + '</strong><br />' + dealer.street + '<br />' + dealer.zip + '&nbsp;' + dealer.city);
												theDealer.inject(zipCodeContainer);
												submitButton.setStyle('display', 'block');
												scForm.action="/index.php?id=294"
											}
										});
										
									});
								}
							});
							
							/*submitButton.addEvent('click', function(event) {
								event = new Event(event);
								Cookie.write(
									'artSLDealer', 
									dealerSelect.value
								);
								alert(Cookie.read('artSLDealer'));
								
							});*/
						}
					}).send();
			}
			
			artSLZipCode.addEvent('keyup', function(event) {
				if(artSLZipCode.value != null && artSLZipCode.value.length < 6) {
					scForm.action=""
					submitButton.setStyle('display', 'none');
					zipCodeRequest(event);
				} else {
					event.stop();
				}
			});
		}
		if(artSLDealerInput != null) {
			if($('artSLDealer').value == '') {
				alert('Sie haben keinen Händler ausgewählt. Bitte füllen Sie das Händler-Formular noch einmal aus!');
				window.location.href="http://www.loewe.de";
			}
		}
	};
	
	//put the hidden labelvalues into the inputfields
	var fieldFill = function(){
		inputFields.each(function(field, index){
			var fieldIdent = field.getProperty('id');
			var fieldType = field.getProperty('type');
			if (fieldType == 'text') {
				var fieldLabel = field.getPrevious('label');
				if (fieldLabel.getProperty('for') == fieldIdent) {
				
					fieldLabel = fieldLabel.firstChild.data;
					field.setProperty('value', fieldLabel);
					var fieldFocus = function(){
						if (field.getProperty('value') == fieldLabel) {
							field.setProperty('value', '');
						}
					};
					var fieldBlur = function(){
						if (field.getProperty('value') == '') {
							field.setProperty('value', fieldLabel);
						}
					};
					
					field.addEvent('focus', function(){
						fieldFocus();
					});
					field.addEvent('blur', function(){
						fieldBlur();
					});
					
				}
				
			}
			
			
		});
	};
	// If the form exists - run the function (see above)
	if (scForm != null) {
		fieldFill();
	}
	
	// Check the required (class) fields
	var checkForm = function(e) {
		
		inputFields.each(function(field, index) {
			var fieldIdent = field.getProperty('id');
			var requiredField = field.hasClass('required');
			
			if (requiredField != false) {
				//check the required fields
				var fieldLabel = field.getPrevious('label');
				
				if (fieldLabel.getProperty('for') == fieldIdent) {
					fieldLabel = fieldLabel.firstChild.data;
					
					switch (field.value) {
						case fieldLabel:
							field.setStyle('color', '#ff0000');
							e.stop();
							break;
						case '':
							field.setStyle('color', '#ff0000');
							e.stop();
							break;
						default:
							if (field.getProperty('name') == 'email') {
								if (field.value.indexOf('@') == -1) {
									field.setStyle('color', '#ff0000');
									e.stop();
								} else {
									field.setStyle('color', '#010101');
								}
							} else {
								field.setStyle('color', '#010101');
							}
							break;
					}
				}
			} else {
				// delete the fieldlabels from the inputfields
				var fieldLabel = field.getPrevious('label');
				if (fieldLabel != null) {
					if (fieldLabel.getProperty('for') == fieldIdent) {
						fieldLabel = fieldLabel.firstChild.data;
						if (field.value == fieldLabel) {
							field.value = '';
						}
					}
				}
			}
		});
		
	};
	// If the form exists - add some events
	if (scForm != null) {
		submitButton.addEvent('mouseenter', function(){
			this.setStyles({
					'background-image': 'url(fileadmin/img/subNavActArrow.gif)',
					'color':'#010101'
				});
		}).addEvent('mouseleave', function(){
			this.setStyles({
					'background-image': 'url(fileadmin/img/subNavArrow.gif)',
					'color':'#a0a0a0',
					'cursor':'pointer'
				});
		}).addEvent('click', function(e){
			e = new Event(e);
			// run the checkform function (see above) - if checkForm is false (e.stop())
			checkForm(e);
		});
	}
};