var reasons=[];
reasons[G_GEO_SUCCESS]="Success";
reasons[G_GEO_MISSING_ADDRESS]="fehlende Adresse";
reasons[G_GEO_UNKNOWN_ADDRESS]="unbekannte Adresse";
reasons[G_GEO_UNAVAILABLE_ADDRESS]="Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
reasons[G_GEO_BAD_KEY]="Bad Key: The API key is either invalid or does not match the domain for which it was given";
reasons[G_GEO_TOO_MANY_QUERIES]="Too Many Queries: The daily geocoding quota for this site has been exceeded.";
reasons[G_GEO_SERVER_ERROR]="Server error: The geocoding request could not be successfully processed.";
var ballontexte = {
	'ttwech'	: "Falls die Ballonhilfen stören, können sie hier abgeschaltet werden.",
	'folder1'	: "Hier tragen Sie bitte den Namen Ihres Hotels und Ihren Namen ein. Wenn Sie hier klicken, können Sie die Sektion aus- und einblenden. Das gilt auch für die anderen „Fächer“.",
	'labeltitle': "Hier tragen Sie den Namen Ihres Hotels ein, so wie ihn die Gäste sehen sollen &ndash; also ohne Firmierung",
	'title'		: "Hier tragen Sie den Namen Ihres Hotels ein, so wie ihn die Gäste sehen sollen &ndash; also ohne Firmierung",
	'labelname'	: "Tragen Sie hier den Name des Ansprechpartners ein.",
	'labelsex'	: "Anrede/Geschlecht des Ansprechpartners",
	'labelaka'	: "akademischer Grad des Ansprechpartners",
	'inpvname'	: "Bitte tragen Sie hier den Vornamen des Ansprechpartners ein.",
	'inpnname'	: "Bitte tragen Sie hier Ihren Familiennamen ein.",
	'labeladr'	: "Anschrift Ihres Hotels in der Form: „Mühlenweg 2“",
	'street'    : "Hier sollten Sie die Adresse Ihres Hotels in der Form „Musterweg 7“ eintragen.",
	'labelplz'  : "Postleitzahl und Ort Ihres Hotels",
	'plz'	: "Tragen Sie hier bitte die ersten Ziffern Ihrer Postleitzahl ein.",

	'city'		: "Bitte tragen Sie hier den Ort ein, in dem sich das Hotel befindet.",
	'labelland'	: "Wählen Sie hier das Land. Falls das Land nicht in der Liste erscheint, wählen Sie bitte in der Liste den Punkt „mehr Länder“",
	'land'	: "Wählen Sie hier das Land. Falls das Land nicht in der Liste erscheint, wählen Sie bitte in der Liste ganzen unten den  Punkt „mehr Länder“",
	
	'more_nr' : "Hier können Sie noch weitere Eingabefelder für Handy, Fax, usw. sichtbar machen."  ,
	'selland'   : "Wählen Sie hier das Land. Falls das Land nicht in der Liste erscheint, wählen Sie bitte in der Liste den Punkt „mehr Länder“",
	'geocode'	: "Klicken Sie hier, um den Standort Ihres Hotels auf der Karte festzulegen.  Es erscheint ein kleiner Markierer, den Sie bewegen können. Vorher müssen Sie aber wenigstens Adresse und Ort eintragen. Falls die Angaben ausreichend sind, wird die PLZ automatisch ergänzt.",
	'labelregion': "Bitte wählen Sie die Urlaubsregion. Falls Sie eine Region vermissen, klicken Sie auf das +-Symbol, um eine neue Region festzulegen.",
	'more_region':"Falls Ihre Ferienregion nicht in dieser Liste erscheint, können Sie hier eine neue Region hinzufügen, indem Sie auf hier klicken, dann erscheint ein Eingabefeld",
	'labelmail' : "An diese E-Mail-Adresse werden Ihnen wichtige Informationen, die Kundenkontakte und im Anschluß an diese Anmeldung eine Bestätigungsmail gesandt, um Ihre Präsentation freizuschalten.",
	'strompost' : "An diese E-Mail-Adresse werden Ihnen wichtige Informationen, die Kundenkontakte und im Anschluß an diese Anmeldung  eine Bestätigungsmail gesandt, um Ihre Präsentation freizuschalten.",
    'testmail' 	: "Die E-Mail-Adresse muss verifiziert werden. Wenn Sie hier klicken, bekommen Sie an die angegebene Adresse eine Testmail, deren innewohnenden Link Sie bitte bestätigen.",
    'parole2x' :"Das Passwort müssen Sie leider 2&times;eingeben. Falls Sie unbeobachtet sind, können Sie das Kästchen neben „zeigen“ betätigen."
    };
    
var map;
var geo;
var pe;
// leo micosse 04174 689264
var tt = true;
function testInp(id) {
	if ($F(id).length<3) {
		Element.addClassName($(id),'error');
		return false;
	} 
	Element.removeClassName($(id),'error');
	return true;
}
var rules=
	{
	'#ttcb' : function(e) {
		e.setAttribute('autocomplete','off');
		e.onclick = function() {
			if (!e.checked) {
				$$('.ballon').each(function(el){Element.removeClassName(el,'ballon');});
				tt = false;
			} else {
				$$('.ballon').each(function(el){Element.addClassName(el,'ballon');});
				tt = true;
			
			}
			UnTip();
			Behaviour.apply(rules);
		}
	},
	'#submit' : function(f) {
		f.onclick = function() {
			var ok = true;
			var filter=/^([\w-_]+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		    ok &= testInp('title');
			ok &= testInp('vname');
			ok &= testInp('nname');
			ok &= testInp('tel');
			ok &= testInp('parole1');
			ok &= testInp('parole2');
			ok &= testInp('street');
			ok &= testInp('plz');
			ok &= testInp('city');
			if (testInp('street') && testInp('city') && !testInp('gps')) {
				Effect.Pulsate('geocode');
				ok = false;
			}
			if (!filter.test($F('strompost'))) {
				ok &= false;
				$('strompost').addClassName('error');			
			} else if ($F('mailvalide') != 'true') {
				Effect.Pulsate('testmail');
			}
			
		}
	},
	'.wm' : function(e) {
		e.setAttribute("rel",e.getAttribute('value'),0);
	},
	'.aco' : function(e) {
		e.setAttribute("autocomplete",'off',1);
	},
    '#pwzeiger' : function(e) {
    	e.onclick = function() {
    		togglePW('parole1');
    		togglePW('parole2');
    		Behaviour.apply(rules);
    	}
    },
	'#testmail' : function(e) {
		e.disabled = null;
		e.onclick = function() {
			var filter=/^([\w-_]+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
			if (!filter.test($F('strompost'))) {
				Element.addClassName($('strompost'),'error');
				return false;
			} else {
				Element.removeClassName($('strompost'),'error');
				e.disabled ='disabled';
				$('strompost').style.backgroundImage='url(./geduld.gif)';
				new Ajax.Request('./sendtestmail.php',{
					parameters:'_='+$F('strompost'),
					onSuccess:function() {
						$('strompost').style.backgroundImage=null;

						pe = new PeriodicalExecuter(function(pe) {
							 new Ajax.Request('./sendtestmail.php',{
							 			parameters:'ismailok='+$F('strompost'),
							 			onSuccess:function(a) {
							 				}});
							 				
							 				},5);}});}
			return false;	
		}, 
		e.onkeyup = function() {	
				e.disabled = null;
				return false;	
		}
	},
	'#geocode':function(f)
		{
		f.onclick=function()
			{
			var d=true;
			if($F('street').length<3)
				{
				Element.addClassName($('street'),'error');
				d=false
			}
			else Element.removeClassName($('street'),'error');
		
		
			if($F('city').length<3)
				{
				Element.addClassName($('city'),'error');
				d=false
			}
			else Element.removeClassName($('city'),'error');
			if(d)
				{
				var e = $F('street')+' ' + $F('plz')+' '+$F('city')+', '+$F('land');
				if(!map)
					{
				}
				geo.getLocations(e,function(a)
					{
					if(a.Status.code==G_GEO_SUCCESS)
						{
						Element.show('mapwrap');
						new Draggable('mapwrap');
						map=new GMap($('map'));
						var items = a.Placemark[0].address.split(',');
						var p=a.Placemark[0].Point.coordinates;
						var city  = items[1].split(' ');
						$('street').value=items[0];
						$('plz').value=city[1];
						$('plz').removeClassName('error');
						$('city').value=city[2];
						$('gps').value=p[1]+','+p[0];

						map.setCenter(new GLatLng(52.0,10.0),6);
						map.addControl(new GSmallMapControl());
						map.clearOverlays();
					
						var b=new GMarker(new GLatLng(p[1],p[0]),
							{
							title:$F('title'),draggable:true
						}
						);
						map.addOverlay(b);
						GEvent.addListener(b,"dragend",function()
							{
							$('gps').value=b.getPoint().lat()+','+b.getPoint().lng()
						}
						);
						var p=a.Placemark[0].Point.coordinates;
						map.setCenter(new GLatLng(p[1],p[0]),13)
						new Effect.MoveBy($('mapwrap'), 360, 0, {duration:2, transition:Effect.Transitions.sinoidal});
						$('adresse').innerHTML = a.Placemark[0].address;
					}
					else
						{
						var c="Code "+a.Status.code;
						if(reasons[a.Status.code])
							{
							c=reasons[a.Status.code]
						}
						alert('Kann "'+e+'" nicht auflösen.')
					}
				}
				)
			}
		}
	}
	,'#map':function(a)
		{
		a.ondblclick=function()
			{
			Element.hide(a.parentNode)
		}
	},
	'#gps' : function(a) {
		a.setAttribute('autocomplete','off');
	}
	,'#savegps':function(a)
		{
		a.onclick=function()
			{
						new Effect.MoveBy($('mapwrap'), -360, 0, {duration:2, transition:Effect.Transitions.sinoidal});

			Effect.Fade($('mapwrap'))
		}
	}
	,'#more_nr' : function(a) {
		a.onclick = function() {
			Effect.Fade(a);
			$$('.nr').each(function(e) {
				e.toggle();
			}
			)
		};
	}
	,'.nr' : function(a) {
		a.style.display='none';
	},
	'#more_region' : function(a) {
		a.onclick = function() {
			Effect.Appear($('neuregion').parentNode);
			$('region').disabled='disabled';
			$('regionlabel').innerHTML = 'neue Region';
		}
	},
	'#neuregion' : function(a) {
		a.autocomplete='off';
		a.onfocus = function() {
			$('neuregion').value='';
		}
	},
	'#saveneuregion' : function(a) {
		a.onclick = function() {
			if (!$F('neuregion').length) {
				$('region').disabled=null;
				Effect.Fade($('neuregion').parentNode);		
				return null;
			}	
			var p = "isoland=" + encodeURI($F('land')) +"&region="+encodeURI($F('neuregion')); 
			new Ajax.Request('./lib/saveneuregion.ajax.php',{parameters:p,onSuccess:function(r) {
					var region = $('region');
					region.disabled=null;
					Effect.Fade($('neuregion').parentNode);		
					region.options[region.length] = new Option($F('neuregion'),r.responseText,true,true);
			}});
	    return false;
	    }
	},
	'#land' : function(e) {
		e.setAttribute('autocomplete','off');
		e.onchange = function() {
			if ($F('land') == '-') {
				new Ajax.Request('./lib/getlaender.ajax.php',{onSuccess:function(r) {
					fillSelect($('land'),r.responseText);
					//Regionen neu einlesen:
					new Ajax.Request('./lib/getregionenbyland.ajax.php', {
						parameters:'isoland=' + $F('land'),
						onSuccess:function(a) {
							fillSelect($('region'),a.responseText); 
					}});
			 	}
			});
			} else {
					new Ajax.Request('./lib/getregionenbyland.ajax.php', {
						parameters:'isoland=' + $F('land'),
						onSuccess:function(a) {
							fillSelect($('region'),a.responseText); 
					}});
		    }		
		}
	},
	'legend' : function(e) {
		e.style.cursor='pointer';
		var p = e.nextSibling.firstChild;
		e.onclick = function() {Effect.toggle(p,'blind');};
		
	},

	'#parole1' : function(e) {
		e.onkeyup = function () {
			var guete = testPassword($F('parole1'));
							$('parole2').disabled = null;

			$('guete').innerHTML  = 'Sicherheit: ' + guete.verdict;
			if (guete.score>3) {
				Effect.Appear($('parole2'));
			} else  Effect.Fade($('parole2'));
			
		}
	
	},
	'#parole2' : function(e) {
		e.onfocus = function() {
			$('parole2').removeClassName('wm');
			e.setAttribute('type',$('parole1').getAttribute('type'));
			e.style.color='black';
			$('guete').style.color='red';
			$('guete').innerHTML = 'Passworte stimmen nicht überein!';
			if ($F('parole2') == e.getAttribute('rel')) e.value=''; 
		},
		e.onkeyup = function() {
			if ($F('parole1') == $F('parole2')) {
				$('guete').innerHTML = 'Passwörter stimmen überein.';
			$('guete').style.color='green';

				$('parole2').disabled = 'disabled';
			}
		}
	},
	'.ballon' : function(b) {
		var txt = ballontexte[b.id];
	    b.style.cursor = (b.tagName != 'INPUT')  
	    	? 'help'
	    	:  'standard';
	   
	 	b.onmouseover = function() {
	  		if (tt) Tip(txt, BALLOON,true,ABOVE,true,OFFSETX,-17,TEXTALIGN,'left',PADDING, 8,FONTSIZE,'8pt');
		},
		b.onmouseout = function() {
			if (tt) UnTip();
		},
		b.onfocus = function() {
			if (tt) UnTip();
		}

	}
};
Event.observe(window,'load',function()
	{
	new Ajax.Request('./lib/getregionenbyland.ajax.php', {
		parameters:'isoland=' + $F('land'),
		onSuccess:function(a) {
			fillSelect($('region'),a.responseText); 
	}});
	geo=new GClientGeocoder();
	var elems = document.getElementsByTagName('*');
	for (var i=0;i<elems.length;i++) {
		var elem = elems[i].id;
		if (elems[i].id && ballontexte[elems[i].id]!=undefined) Element.addClassName(elems[i],'ballon');
	}
	Behaviour.register(rules);
	Behaviour.apply(rules);
}
);

function fillSelect(elem,json) {
	var options = eval("(" + json + ")"); 
    while (elem.length) elem.options[0] = null;
	if (json.length<3) return;
	for (var i in  options) {
		elem.options[elem.length] = new Option(options[i],i);
	}
}




function testPassword(passwd)
{
		var intScore   = 0;
		var strVerdict = "schwach";
		
		// PASSWORD LENGTH
		if (passwd.length<4)                         // length 4 or less
		{
			intScore = (intScore+3)
		}
		else if (passwd.length>5 && passwd.length<7) // length between 5 and 7
		{
			intScore = (intScore+6)
		}
		else if (passwd.length>6 && passwd.length<16)// length between 8 and 15
		{
			intScore = (intScore+12)
		}
		else if (passwd.length>15)                    // length 16 or more
		{
			intScore = (intScore+18)
		}
		
		
		// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
		if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
		{
			intScore = (intScore+1)
		}
		
		if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
		{
			intScore = (intScore+5)
		}
		
		// NUMBERS
		if (passwd.match(/\d+/))                                 // [verified] at least one number
		{
			intScore = (intScore+5)
		}
		
		if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
		{
			intScore = (intScore+5)
		}
		
		
		// SPECIAL CHAR
		if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
		{
			intScore = (intScore+5)
		}
		
									 // [verified] at least two special characters
		if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
		{
			intScore = (intScore+5)
		}
	
		
		// COMBOS
		if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
		{
			intScore = (intScore+2)
		}

		if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers
		{
			intScore = (intScore+2)
		}
 
									// [verified] letters, numbers, and special characters
		if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
		{
			intScore = (intScore+2)
		}
	
	
		if(intScore < 16)
		{
		   strVerdict = "sehr schwach"
		}
		else if (intScore > 15 && intScore < 25)
		{
		   strVerdict = "schwach"
		}
		else if (intScore > 24 && intScore < 35)
		{
		   strVerdict = "mittel"
		}
		else if (intScore > 34 && intScore < 45)
		{
		   strVerdict = "stark"
		}
		else
		{
		   strVerdict = "sehrstark"
		}
	
		return {score:intScore,verdict:	strVerdict};
}



function togglePW(id){
	var foo = $(id);
	var type = (foo.getAttribute('type')=='text') ? 'password' :'text';
	try {
		var val = foo.value;
		foo.type  = type;
		foo.value = val;
	}
	catch(e) {
		var bar = document.createElement('input');
		bar.type   = type;
		bar.value  = foo.value;
		bar.name   =  foo.getAttribute('name');
		bar.className =  foo.getAttribute('class');
		bar.style.display = foo.getStyle('display');	
		bar.id   =  id;
		
		$(id).parentNode.replaceChild(bar,foo);
	}
} 

