if(!de) var de={};
if(!de.praktikawelten) de.praktikawelten={};
if(!de.praktikawelten.Events) de.praktikawelten.Events={};

de.praktikawelten.Events.Program = {
	"DATA_UPDATED"	: "data_updated",
	"DATA_CHANGED"	: "data_changed"
};

de.praktikawelten.Program = new Class({
	
	// Implementierungen
	Implements: [Events,Options],
	
	// DefaultOptions
	options: {
	},
	
	initialize: function(options) {
		this.programType = 0;
		this.programcountry = 0;
		this.country = 0;
		this.program = 0;
		this.programLength = 0;
		this.languageCourseLength = 0;
		this.ajaxrequests = 0;
		this.gPrice = 0;
		this.pPrice = 0;
		this.lPrice = 0;
		this.ePrice = 0;
		this.cPrice = 0;
		this.TravelPriceAry = [];
		this.reset = true;
		this.ExtraObjectList = [];
		this.ExtraPriceList = [];
		this.UsedExtra = [];
		this.LanguageSkill = [];
		this.Coupon = '';
	},
	
	Config: function( programtype, country, program, programlength, languagecourselength ) {
		this.reset = false;
		if( programtype > 0 ) this.setProgramType( programtype, false );
		if( country > 0 ) this.setCountry( country, false );
		if( programlength > 0 ) this.setProgramLength( programlength );
		if( program > 0 ) this.setProgram( program );
		if( languagecourselength > 0 ) this.setLanguagecourseLength( languagecourselength );
	},
	
	setLanguagecourseLength: function( length ) {
		if( length == undefined ) return false;
		this.languageCourseLength = length;
		
		this.doAjax({
			"a_action"				: "LoadPrice",
			"ProgramLengthId"		: this.programlength,
			"LanguageCourseId"		: this.languageCourseLength
		});
	},
	/*
	setLanguageCourseLength: function(length) {
		if( length == undefined ) return false;
		this.languagecourselength = length;
	},
	*/
	
	setProgramLength: function(length) {
		if( length == undefined ) return false;
		this.programlength = length;
		this.doAjax({
			"a_action"				: "LoadLanguageCourseList",
			"PriceId"				: this.programlength
		});
	},
	
	setProgramType: function(type, clearcache) {
		if( this.reset ) {
			$('languagecourse_container').innerHTML = "";
			$('calendar-inputField').value = "";
			$('country_select').length = 0;
			$('program_select').length = 0;
			$('programlength').length = 0;
			$('languagecourselength').length = 0;
			$('calendar-trigger').style.display = 'none';
		}
		
		if( clearcache ) {
			
			this.country = 0;
			$('country_select').length = 0;
			$('country_select').selectedIndex = 0;
			
			this.program = 0;
			$('program_select').length = 0;
			$('program_select').selectedIndex = 0;
			
		}
		
		if( type == 2 ) {
			$('program_title').innerHTML = "Route:*";
			$('languagecourselength_container').style.display = 'none';
			$('length').style.display = 'none';
			$('languagecourse').style.display = 'none';
		} else {
			$('program_title').innerHTML = "Programm:*";
			$('languagecourselength_container').style.display = 'block';
			$('length').style.display = 'block';
			//$('languagecourse').style.display = 'block';
		}
		
		if( type == undefined || isNaN( type ) || type == 0 ) return false;
		$('price').innerHTML = 0;
		$('ProgramPrice').value = 0;
		this.programType = type;
		this.doAjax({
			"a_action"				: "LoadCountryList",
			"ProgramTypId"			: type
		});
	},
	
	setCountry: function(country, clearcache ) {
		if( this.reset ) {
			$('languagecourse_container').innerHTML = "";
			$('calendar-inputField').value = "";
			$('program_select').length = 0;
			$('programlength').length = 0;
			$('languagecourselength').length = 0;
			$('calendar-trigger').style.display = 'none';
		}
		
		if( clearcache ) {
			
			this.program = 0;
			$('program_select').length = 0;
			$('program_select').selectedIndex = 0;
			
		}
		
		if( country == undefined || isNaN( country ) || country == 0 ) return false;
		$('price').innerHTML = 0;
		$('ProgramPrice').value = 0;
		this.country = country;
		this.doAjax({
			"a_action"				: "LoadProgramList",
			"CountryId"				: this.country,
			"ProgramTyp"			: this.programType
		});
	},
	
	setStarttime: function( calendar ) {
	//setStarttime: function( calendar ) {
		
		if( calendar != undefined ) {
			calendar.hide();
		}
		
		if( this.programType != 2 ) return false;
		//var d = $('calendar-inputField').value.replace( /\./g, "" );
		
		var tmp = $('calendar-inputField').value.split( "." );
		var d = tmp[2]+tmp[1]+tmp[0];
		this.setpPrice( this.TravelPriceAry[d] );
	},
	
	setProgram: function(program) {
		if( this.reset ) {
			$('languagecourse_container').innerHTML = "";
			$('calendar-inputField').value = "";
			$('programlength').length = 0;
			$('languagecourselength').length = 0;
			$('calendar-trigger').style.display = 'none';
		}
		if( program == undefined || isNaN( program ) || program == 0 )  return false;
		this.program = program;
		this.doAjax({
			"a_action"				: "LoadCountryProgramInfo",
			"CountryId"				: this.country,
			"ProgramId"				: this.program,
			"ProgramTyp"			: this.programType
		});
	},
	
	setCoupon: function( coupon ) {
		this.Coupon = $('couponcode').value;
		if( this.Coupon == "" ) {
			this.cPrice = 0;
			this.calculatePrice();
			$('couponnotice').innerHTML = "";
			$('CouponId').value = 0;
			return false;
		}

		this.doAjax({
			"a_action"				: "LoadCoupon",
			"Coupon"				: this.Coupon,
			"CountryProgram"		: this.programcountry,
			"ProgramTyp"			: this.programType
		});
	},
	
	setExtraPrice: function( price ) {
		var ePrice = 0;
		for(i=0;i<this.ExtraObjectList.length;i++) {
			if( this.ExtraObjectList[i].type == "checkbox" ) {
				if( this.ExtraObjectList[i].checked ) {
					ePrice = parseInt( ePrice ) + parseInt( this.ExtraPriceList[ i ].Price );
				}
			} else if ( this.ExtraObjectList[i].type == "text" ) {
				ePrice = parseInt( ePrice ) + ( parseInt( this.ExtraPriceList[ i ].Price ) * this.ExtraObjectList[i].value );
			}
		}
		
		this.setePrice( ePrice );
	},
	
	setcPrice: function( price ) {
		this.cPrice = price;
		this.calculatePrice();
	},
	
	setpPrice: function( price ) {
		this.pPrice = price;
		this.calculatePrice();
	},
	
	setePrice: function( price ) {
		this.ePrice = price;
		this.calculatePrice();	
	},
	
	setlPrice: function( price ) {
		this.lPrice = price;
		this.calculatePrice();
	},
	
	calculatePrice: function() {
		this.gPrice = ( parseInt( this.pPrice ) + parseInt( this.lPrice ) + parseInt( this.ePrice ) ) - parseInt( this.cPrice );
		$('price').innerHTML = this.gPrice;
		$('ProgramPrice').value = this.gPrice;
	},
	
	addUsedExtra: function( extra, value ) {
		this.UsedExtra[ this.UsedExtra.length ] = { 'extra' : extra, 'value' : value };
	},
	
	addLanguageSkill: function( languageid, skill ) {
		this.LanguageSkill[ languageid ] = skill;
	},
	
	doAjax: function( dataObj ) {
		myRequest = new Request.JSON({
			"url"		: "include/ajax/ajax.php",
			"method"	: "post",
			"data"		: dataObj,
			"onSuccess"	: this.onAjaxSuccess.bind(this)
		});
		
		myRequest.send();
		this.ajaxrequests++;
		ajaxStart();
	},
	
	Numsort: function( a, b ) {
		return a - b;
	},
	
	sortCountryList: function(a, b) {
		var nameA=a.Country.toLowerCase(), nameB=b.Country.toLowerCase()
		 if (nameA < nameB) //sort string ascending
		  return -1
		 if (nameA > nameB)
		  return 1
		 return 0 //default return value (no sorting)
	},
	
	sortProgramList: function(a, b) {
		var nameA=a.Program.toLowerCase(), nameB=b.Program.toLowerCase()
		 if (nameA < nameB) //sort string ascending
		  return -1
		 if (nameA > nameB)
		  return 1
		 return 0 //default return value (no sorting)
	},
	
	onAjaxSuccess: function( obj, text ) {
		switch( obj.Name ) {
			
			case "CountryList":
				$('country_select').length = 0;
				
				obj.List.sort(this.sortCountryList);
				
				var opt = new Element( 'option', { 'html':'Bitte wählen' }).inject( $('country_select') );
				for(i=0;i<obj.List.length;i++) {
					var opt = new Element( 'option', {
						'html'		:	obj.List[i].Country,
						'value'		: 	obj.List[i].Id
					}).inject( $('country_select') );
				}
				
				var ce = $('country_select').getElements('option');
				for(i=0;i<ce.length;i++){
					if( ce[i].value == this.country ) ce[i].selected = 'selected';
				}
			break;
			
			case "ProgramList":
				$('program_select').length = 0;
				
				obj.List.sort(this.sortProgramList);
				
				var opt = new Element( 'option', { 'html':'Bitte wählen' }).inject( $('program_select') );
				for(i=0;i<obj.List.length;i++){
					var opt = new Element( 'option', {
						'html'		:	obj.List[i].Program,
						'value'		: 	obj.List[i].Id
					}).inject( $('program_select') );
				}
			
				var pe = $('program_select').getElements('option');
				for(i=0;i<pe.length;i++){
					if( pe[i].value == this.program ) pe[i].selected = 'selected';
				}
			break;
			
			case "CountryProgramInfo":
				this.programcountry = obj.CountryProgram;
			
				// Selectbox Programmdauer füllen
				$('programlength').length = 0;
				var opt = new Element( 'option', { 'html':'Bitte wählen' }).inject( $('programlength') );
				//obj.Length.Week.sort(this.Numsort);
				for(i=0;i<obj.Length.length;i++){
					var opt = new Element( 'option', {
						'html'		: obj.Length[i].Week + " Wochen",
						'value'		: obj.Length[i].Id
					}).inject( $('programlength') );
				}
				
				// richtige Programmdauer auswählen
				var ple = $('programlength').getElements('option');
				for(i=0;i<ple.length;i++){
					if( ple[i].value == this.programlength ) ple[i].selected = 'selected';
				}
				
				// Program Bemerkung anzeigen
				$('programnotice').innerHTML = obj.Notice;
				if( obj.Notice == "" || obj.Notice == null ) $('programnotice_container').style.display = 'none';
				else $('programnotice_container').style.display = 'block';
				
				// Sprachkurs Tabelle bauen
				$('languagecourse_container').innerHTML = "";
				if( obj.Languagecourses.length > 0 ) {
					//$('languagecourse').style.display = 'inline';
					$('languagecourse').style.display = 'block';
					//$('languagecourse_container').style.display = 'inline';
					
					var table = new Element( 'table', {}).inject( $('languagecourse_container') );
					var tablebody = new Element( 'tbody', {}).inject( table );
					var colg = new Element( 'colgroup', {}).inject( tablebody );
					var col = new Element( 'col', { 'width' : 140 }).inject( colg );
					var col = new Element( 'col', { 'width' : 140 }).inject( colg );
					var col = new Element( 'col', { 'width' : 140 }).inject( colg );
					var col = new Element( 'col', { 'width' : 140 }).inject( colg );
					var tr = new Element( 'tr', {}).inject( tablebody );
					var td = new Element( 'td', {'html'	: '' }).inject( tr );
					var td = new Element( 'td', {'html'	: 'Keine', 'align' : 'center' }).inject( tr );
					var td = new Element( 'td', {'html'	: 'Grundkenntnisse', 'align' : 'center' }).inject( tr );
					var td = new Element( 'td', {'html'	: 'gute Sprachkenntnisse', 'align' : 'center' }).inject( tr );
					for(i=0;i<obj.Languagecourses.length;i++) {
						var tr = new Element( 'tr', {}).inject( tablebody );
						var td = new Element( 'td', {
							'html'	: obj.Languagecourses[i].Name
						}).inject( tr );
						var tmp_skill = false;
						for(a=0;a<3;a++) {
							if( this.LanguageSkill[ obj.Languagecourses[i].Id ] == a ) tmp_skill = true;
						}
						for(a=0;a<3;a++){
							var td = new Element( 'td', { 'align' : 'center' }).inject( tr );
							if( tmp_skill == false && a == 0 ) {
								var opt = new Element( 'input', {
									'type'		: 'radio',
									'value'		: 'skill_' + a,
									'checked'	: 'checked',
									'name'		: 'languagecourse[' + obj.Languagecourses[i].Id + ']'
								}).inject( td );
							} else {
								var opt = new Element( 'input', {
									'type'		: 'radio',
									'value'		: 'skill_' + a,
									'checked'	: ( this.LanguageSkill[ obj.Languagecourses[i].Id ] == a ? 'checked' : '' ),
									'name'		: 'languagecourse[' + obj.Languagecourses[i].Id + ']'
								}).inject( td );
							}
						}
					}
				} else {
					$('languagecourse').style.display = 'none';
				}
				
				// Program Extras erstellen
				var oobj = $('programextras').getElements('div');
				for(i=0;i<oobj.length;i++){
					$('programextras').removeChild( $('programextras').lastChild );
				}
				
				for(i=0;i<obj.Extras.length;i++) {
					var d = new Element( 'div', {
					}).inject( $('programextras') );
					var c = new Element( 'input', {
						'type'		: (obj.Extras[i].Typ == 'fix' ? 'checkbox' : 'text'),
						'value'		: '',
						'style'		: 'width:30px;',
						'name'		: 'extra['+ obj.Extras[i].Id +']',
						'onchange'	: 'T.setExtraPrice(' + obj.Extras[i].Id + ')'
					}).inject( d );
					var p = new Element( 'span', {
						'text'		: ' ' + obj.Extras[i].Value
					}).inject( d );
					
					if( obj.Extras[i].Id > 0 ) {
						this.ExtraPriceList[ i ] = {
							"Typ"		: obj.Extras[i].Typ,
							"Price"		: obj.Extras[i].Price
						};
					}
					this.ExtraObjectList[i] = c;
				}
				if( obj.Extras.length > 0 ) $('programextras_container').style.display = 'block';
				else $('programextras_container').style.display = 'none';
				
				
				// Extras markieren/füllen
				for(i=0;i<this.ExtraObjectList.length;i++) {
					for(a=0;a<this.UsedExtra.length;a++){
						if( this.ExtraObjectList[i].name == 'extra['+ this.UsedExtra[a].extra +']' ) {
							if( this.ExtraObjectList[i].type == "checkbox" ) {
								this.ExtraObjectList[i].checked = 'checked';
							} else if( this.ExtraObjectList[i].type == "text" ) {
								this.ExtraObjectList[i].value = this.UsedExtra[a].value;
							}
						}
					}
				}
				
				// Extras preis berechnen
				this.setExtraPrice(0);
				
				// Kalender konfigurieren
				var DISABLED_DATES = {};
				for(i=0;i<obj.Starttimes.length;i++){
					DISABLED_DATES[obj.Starttimes[i].Date] = true;
					this.TravelPriceAry[obj.Starttimes[i].Date] = obj.Starttimes[i].Price;
				};
				
			    Calendar.setup({
			        trigger		: "calendar-trigger",
			        inputField	: "calendar-inputField",
			        dateFormat	: "%d.%m.%Y",
			        animation	: false,
			        disabled	: function(date) {
			        	date = Calendar.dateToInt(date);
			        	return !(date in DISABLED_DATES);
			        },
			        onSelect	: this.setStarttime.bind(this)
			    });
			    
			    $('calendar-trigger').style.display = 'inline';
			    
			    // Preis für Travel&Work setzen
			    if( $('calendar-inputField').value != '' ) this.setStarttime();
			    
			break;
			
			case "LanguageCourseList":
				$('languagecourselength').length = 0;
				var opt = new Element( 'option', { 'html':'Bitte wählen' }).inject( $('languagecourselength') );
				
				for(i=0;i<obj.LcLength.length;i++){
					var opt = new Element( 'option', {
						'html'		: obj.LcLength[i].Week + " Wochen",
						'value'		: obj.LcLength[i].Id
					}).inject( $('languagecourselength') );
				}
				
				// richtige Sprachkursdauer auswählen
				var le = $('languagecourselength').getElements('option');
				for(i=0;i<le.length;i++){
					if( le[i].value == this.languageCourseLength ) le[i].selected = 'selected';
				}
				
				
				if( obj.LcLength.length <= 0 ) {
					$('languagecourselength_container').style.display = 'none';
				} else {
					$('languagecourselength_container').style.display = 'block';
				}
				
				
				this.setpPrice( obj.LcPrice );
				
			break;
			
			case "Coupon":
				if( obj.CouponId != null ) {
					$('couponnotice').innerHTML = "";
					$('CouponId').value = obj.CouponId;
					this.setcPrice( obj.Price );
				} else {
					$('couponnotice').innerHTML = "Kein gültiger Gutschein! ";
					$('CouponId').value = "";
					this.setcPrice( 0 );
				}
			break;
			
			case "LPrice":
				this.setlPrice( obj.LanguagePrice );
			break;
			
			default:
				//this.Debug( "onAjaxSuccess: keine Action" );
			break;
			
		}
		this.ajaxrequests--;
		if( this.ajaxrequests == 0) {
			ajaxEnd();
			if( !this.reset ) this.reset = true;
		}
	},
	
	Debug: function( content ) {
		return false;
		//if( console ) console.log( content );
	}

});
