// Datum ermitteln

var now;
var now_year;
var now_month;

var from;
var from_year;
var from_month;

var to;
var to_year;
var to_month;

var Monat = new Array("January", "February", "March", "April", "May", "June",
                      "July", "August", "September", "October", "November", "December");

function set_params(PeriodItem){
	now = new Date();
	now_year = now.getFullYear(); 	// 2009
	now_month = now.getMonth();		// 1->February
	
	//from = new Date(now_year, now_month+1, 1);
	from = new Date(now_year, now_month, 1);
	from_year = from.getFullYear(); // 2009
	from_month = from.getMonth();	// 1->February
	
	to = new Date(now_year, now_month+PeriodItem-2, 1);
	to_year = to.getFullYear(); 	// 2010
	to_month = to.getMonth();		// 7->August
}

                    
function update_auswahl(div)
{	
	var year_from_selection = document.forms.Select_Period.from_year;
	var month_from_selection = document.forms.Select_Period.from_month;
	var year_to_selection = document.forms.Select_Period.to_year;
	var month_to_selection = document.forms.Select_Period.to_month;
		
	for (i = 0; i < month_to_selection.options.length; i++){
	   	if (month_to_selection.options[i].defaultSelected == true){
	   		var value_month_to = month_to_selection.options[month_to_selection.selectedIndex].value;
	   		break;
	  	}else if (month_to_selection.options[i].selected == true){
	   		var value_month_to = month_to_selection.options[month_to_selection.selectedIndex].value;
	   		break;
	  	}else{
	  		var value_month_to = "NULL";
	  	}
	}
	
	for (i = 0; i < month_from_selection.options.length; i++){
	   	if (month_from_selection.options[i].defaultSelected == true){
	   		var value_month_from = month_from_selection.options[month_from_selection.selectedIndex].value;
	   		break;
	  	}else if (month_from_selection.options[i].selected == true){
	   		var value_month_from = month_from_selection.options[month_from_selection.selectedIndex].value;
	   		break;
	  	}else{
	  		var value_month_from = "NULL";
	  	}
	}
	
	if(div == "from"){
		
		month_from_selection.options.length = 0; // DropDown Men&uuml; entleeren
		
		var start = 0;
		var end = 11;
		
		switch (parseInt(year_from_selection.options[year_from_selection.selectedIndex].value)) {
			
			case from_year:	start = from_month;
							break;
			
			case to_year:	end = to_month;
							break;		
		}
		
		j=0;
		
		for(var i=start; i<=end; i++){
			month_from_selection.options[j] = ( new Option(Monat[i], i+1) );
			j++;
		}
		
		

		
		for (var i = 0; i < month_from_selection.options.length; i++) {
	        if (month_from_selection.options[i].value === value_month_from){
	        	month_from_selection.options[i].selected = true;
	        	break;
	        }else{
	        	month_from_selection.options[0].selected = true;
	        }
	    }
	    
		checkSelection();
	     
	}else if(div == "to"){
		
		month_to_selection.options.length = 0; // DropDown Men&uuml; entleeren
		
		var start = 0;
		var end = 11;
		
		switch (parseInt(year_to_selection.options[year_to_selection.selectedIndex].value)) {
			
			case from_year:	start = from_month+1;
							break;
			
			case to_year:	end = to_month+1;
							break;		
		}
		
		j=0;
		
		for(var i=start; i<=end; i++){
			month_to_selection.options[j] = ( new Option(Monat[i], i+1) );
			j++;
		}
		
		for (var i = 0; i < month_to_selection.options.length; i++) {
	        if (month_to_selection.options[i].value === value_month_to){
	        	month_to_selection.options[i].selected = true;
	        	break;
	        }else{
	        	month_to_selection.options[0].selected = true;
	        }
	    }
	    
	    //Falls doch Monat_from = Monat_to sein sollte
		if ((year_to_selection.options[year_to_selection.selectedIndex].value == year_from_selection.options[year_from_selection.selectedIndex].value) && 
			(parseInt(month_to_selection.options[month_to_selection.selectedIndex].value) <= parseInt(month_from_selection.options[month_from_selection.selectedIndex].value))){
			i = parseInt(month_from_selection.options[month_from_selection.selectedIndex].value);
			month_to_selection.options[i].selected = true;
		}
	}
}

function checkSelection(){
	// Selectierung in month_to und year_to
	    // abhaengig von month_from und year_from
	    var year_to_selection = document.forms.Select_Period.to_year;
		var month_to_selection = document.forms.Select_Period.to_month;
	    var year_from_selection = document.forms.Select_Period.from_year;
		var month_from_selection = document.forms.Select_Period.from_month;
	    var value_year_to = "NULL";
	    var value_year_from = "NULL";
	    var value_month_to = "NULL";
	    var value_month_from = "NULL";	    
		
		for (i = 0; i < month_from_selection.options.length; i++){
	    	if (month_from_selection.options[i].defaultSelected == true){
	      		var value_month_from = month_from_selection.options[month_from_selection.selectedIndex].value;
	      		break;
	    	}else if (month_from_selection.options[i].selected == true){
	      		var value_month_from = month_from_selection.options[month_from_selection.selectedIndex].value;
	      		break;
	    	}
		}
	    for (i = 0; i < year_from_selection.options.length; i++){
	    	if (year_from_selection.options[i].defaultSelected == true){
	      		var value_year_from = year_from_selection.options[year_from_selection.selectedIndex].value;
	      		break;
	    	}else if (year_from_selection.options[i].selected == true){
	      		var value_year_from = year_from_selection.options[year_from_selection.selectedIndex].value;
	      		break;
	    	}
		}
		for (i = 0; i < year_to_selection.options.length; i++){
	    	if (year_to_selection.options[i].defaultSelected == true){
	      		var value_year_to = year_to_selection.options[year_to_selection.selectedIndex].value;
	      		break;
	    	}else if (year_to_selection.options[i].selected == true){
	      		var value_year_to = year_to_selection.options[year_to_selection.selectedIndex].value;
	      		break;
	    	}
		}
		for (i = 0; i < month_to_selection.options.length; i++){
	    	if (month_to_selection.options[i].defaultSelected == true){
	      		var value_month_to = month_to_selection.options[month_to_selection.selectedIndex].value;
	      		break;
	    	}else if (month_to_selection.options[i].selected == true){
	      		var value_month_to = month_to_selection.options[month_to_selection.selectedIndex].value;
	      		break;
	    	}
		}
	    
		
		//Datums anpassen
		if(   (parseInt(value_year_to) <= parseInt(value_year_from))
		   || (parseInt(value_month_to) <= parseInt(value_month_from))){
		   	
//		   	alert(value_year_from+" :: "+value_year_to+" | "+value_month_from+" :: "+value_month_to);
		   	
		   	new_year_1 = parseInt(value_year_from);
		   	
		   	if((parseInt(value_month_to) <= parseInt(value_month_from) && parseInt(value_year_to) <= parseInt(value_year_from))){
		   		
//		   		alert(value_month_from + " :: "+value_month_to);
		   		
		   		new_month = parseInt(value_month_from) + 1;
		   		
//		   		alert(value_month_from + " :: "+value_month_to+" :: "+new_month);
		   		
		   		if(new_month > 12){
		   			new_month = new_month - 12;
		   			new_year_1 = parseInt(value_year_to) + 1;
		   		}
		   		
//		   		alert(value_month_from + " :: "+value_month_to+" :: "+new_month);
		   		
		   		for (var i = 0; i < month_to_selection.options.length; i++) {
//			        alert("new_month:"+new_month+" :: i:"+i+":: monat.value: "+month_to_selection.options[i].value);
					if (month_to_selection.options[i].value == new_month){
			        	month_to_selection.options[i].selected = true;
			        	break;
			        }else{
			        	month_to_selection.options[0].selected = true;
			        }
		    	}
		   	}
		   	
		   	
		   	if(parseInt(value_year_to) <= parseInt(value_year_from)){
		   		
		   		
		   		
		   		if(new_year_1 >= parseInt(value_year_from)){
		   			new_year_2 = new_year_1;
		   		}else{
		   			new_year_2 = parseInt(value_year_from) + 1;
		   		}
		   		
//		   		alert(value_year_to + " :: "+new_year_1+" :: "+new_year_2);
		   		
		   		for (var i = 0; i < year_to_selection.options.length; i++) {
					if (year_to_selection.options[i].value == new_year_2){
			        	year_to_selection.options[i].selected = true;
			        	break;
			        }else{
			        	year_to_selection.options[0].selected = true;
			        }
		    	}
		   	}
		}
		
		update_auswahl('to');
}
