/*
 to clear the default values in short view of formular
*/
function clearDefaults(firstName,surname,email) {
	if ($('fdata[vorname]').value == firstName) {
		$('fdata[vorname]').value = "";
	}
	if ($('fdata[nachname]').value == surname) {
		$('fdata[nachname]').value = "";
	}
	if ($('fdata[email]').value == email) {
		$('fdata[email]').value = "";
	}
}

/* 
jumps to next field in date fields 
*/
function conditionalJump(jumpTo,evt) {
	if (!evt) var evt = window.event;
	if (evt.keyCode==190) {
		$(jumpTo).focus();
		$(jumpTo).addEvent('keyup', function() { this.setProperty('value',this.value.replace(/\./g,"")); } );
	}
}

function myElementValidationHandler(passed,element) {
}

function nothing()  {
	return true;
	if ((passed===true) && ($(element).getProperty('id')=="fdata[plz]")) {

		var country = "Austria";
		var shortCountryCode =  "AT";
		if ($('fdata[land]').getValue() == '65') { country = "Germany"; shortCountryCode = "DE";}
		else { 	
			country = "";
			shortCountryCode = "";
		}
		var glocation = $(element).getValue()+", "+country;
		var geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode(shortCountryCode);
		geocoder.getLocations(
			glocation,

			/*callback function*/
			function(point) {
				if ($defined(point) && 
				    (point.Status.code == G_GEO_SUCCESS) &&
				    $defined(point.Placemark) && 
				    (point.Placemark[0].AddressDetails.Country.CountryNameCode==shortCountryCode) && 
				    (point.Placemark[0].AddressDetails.Accuracy >= 5)) {
					/*reset error messages and set city field*/
					$(element).addClass('validation-passed');
					if ($defined(point.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality)) {
						$('fdata[wohnort]').setProperty('value',point.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName);
					} else if ($defined(point.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea)) {
						$('fdata[wohnort]').setProperty('value',point.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName);
					}
					fV.resetField($('fdata[wohnort]'));
					fV.validateField($('fdata[wohnort]'));
					$(element).addClass('invalid');
					fV.resetField($(element));
					$(element).removeClass('invalid');
					$(element).addClass('validation-passed');
				} else {
					if (point.Status.code != G_GEO_SERVER_ERROR) {
						fV.resetField($(element));
						setInvalid(fV,element);
					}
				}
			}
		);
	}
}

function switchType() {
	$('fdata[typ]').setProperty('value',2);
	switchView();
}

function switchView() {
	if (typeof slider == 'undefined')  { slider = new Fx.Slide('kinder_inner', {} ); }

	if ( $('fdata[typ]').getValue()==2 ) {
		$('kinder').effect('opacity', {duration: 500}).start(1,0).chain( function() { slider.slideOut() } );
		$('acardIDContainer').removeClass("notShown");	
	} else {
		slider.slideIn().chain( function(){$('kinder').effect('opacity', {duration: 500} ).start(0,1) } );
		$('acardIDContainer').addClass("notShown");	
		$('kinder_inner').getParent().setStyle('overflow','visible');
		$('kinder_inner').getParent().setStyle('height','auto');
	}
}

function highlightThese(keys_array) {
	keys_array.each(function(key){
		$(key).removeClass("light");
	});
}

function clearMe(item,defVal) {
	$(item).removeClass("light");
	if ($(item).value == defVal) {
		$(item).value="";
		$(item).removeClass("light");
	}
}

function fillMe(item,defVal) {
	if ($(item).value != defVal) {
		if ( $(item).hasClass("light") ){
			$(item).removeClass("light");
		}
	}
	if ($(item).value == "") {
		$(item).value=defVal;
		$(item).addClass("light");
	}
}

function injectErrorMessage(message) {
//	if ( !$('pleaseFillOut') ) 
{
/*		var divEl = document.createElement('div');
		Element.addClass( divEl, 'error' );
		var divID = document.createAttribute("id");
		divID.nodeValue = "pleaseFillOut";
		divEl.setAttributeNode(divID);*/
		if (message != "") {
			jQuery('#errorDiv').append(document.createTextNode( message ));
			jQuery('#errorDiv').css("visibility","visible");
		}
//		$($('submit').parentNode).insertBefore( divEl, $('submit') );
		
	}
}

function setInvalid(fieldname) {
	jQuery(fieldname).rules("add", {
		url:true //there are no urls in the form, so i want the field to be an url which is never true, so we get an error for the field.
	});
	jQuery('#ml_custcard_orderform').validate().element(fieldname);
	jQuery(fieldname).rules("remove", "url");
}

function clearRequiredInfants(fV) {
	for (var i = 1; i <= 4; i++) {
		$('fdata[child'+i+'_year]').removeClass('infant');
	}
}

function checkForInfants() {
	var now = new Date();
	var three = new Date(1973,0,1);
	var gebDat = new Date();
	infant = false;
	for (var i = 1; i <= 4; i++) {
		$('fdata[child'+i+'_year]').addClass('infant');
		gebDat.setFullYear($('fdata[child'+i+'_year]').getValue());
		gebDat.setMonth($('fdata[child'+i+'_month]').getValue());
		gebDat.setDate($('fdata[child'+i+'_day]').getValue());
		if ( (now - gebDat) < three ) { 
			infant = true;
		}
	}

	return infant;
}

jQuery(document).ready(function() {
	jQuery('#formSubmit').click(function() {
	  jQuery('#ml_custcard_orderform').submit();
	});
});

