
var d = new Date();
var today_day = d.getDate();
var today_month = d.getMonth()+1;
var today_year = d.getFullYear();
var dt= new Date();
dt.setDate(dt.getDate()+1);
var tomorrow_day = dt.getDate();
var tomorrow_month = dt.getMonth()+1;
var tomorrow_year = dt.getFullYear();


var mystart = today_year+"-"+today_month+"-"+today_day;
var mytstart = tomorrow_year+"-"+tomorrow_month+"-"+tomorrow_day;


		if(calendar==false)
		{
				var addEvent = function(elem, type, eventHandle) {
				if (elem == null || elem == undefined) return;
				if ( elem.addEventListener ) {
					elem.addEventListener( type, eventHandle, false );
				} else if ( elem.attachEvent ) {
					elem.attachEvent("on" + type, eventHandle);
				}
			};

			addEvent(window, "load", function() { 
				loopDays('cimd',mystart,'');
				loopMonths('ciym',mystart,'');
				loopDays('comd',mytstart,'');
				loopMonths('coym',mytstart,'');

			} );
		}

		function loopDays(div,my,target)
		{
			 var month,year,selday;
			if(my=='' || my===undefined){
				var isset = false;
				 if(div=='cimd'){
					month= today_month;
					year = today_year;
					selday = today_day;
				}else{
					month= tomorrow_month;
					year = tomorrow_year;
					selday = tomorrow_day;
				}
			}else{
				var isset = true;
				var mysplit = my.split("-");
				year = mysplit[0];
				month = mysplit[1];
				if(target==''){
					selday = document.getElementById(div).value;
				}else{
					selday = mysplit[2];
				}
				if(!selday && div=="cimd") selday = today_day;
				if(!selday && div=="comd") selday = tomorrow_day;
			}



			if(month==2){
					loopto=28;
					skottar=",2012,2016,2020,2024,2028,2032,2038,";
					if(skottar.indexOf(year)>0) loopto=29;
			}else if((month==4) || (month==6) || (month==9) || (month==11)){
					loopto=30;
			}else{
				loopto=31;
			}
			var days="";

				if(target=="" || target=="this")
				{
					for(i=1;i<=loopto;i++)
					{
						document.getElementById(div).options[i] = new Option(i,i);
						if(i==selday) document.getElementById(div).options[i].selected = true;
					}
					document.getElementById('cal_checkin').style.display = 'none';
					document.getElementById('cal_checkout').style.display = 'none';

				}else{
					for(i=1;i<=loopto;i++)
					{
						parent.document.getElementById(div).options[i] = new Option(i,i);
						if(i==selday) parent.document.getElementById(div).options[i].selected = true;
					}
					parent.document.getElementById('cal_checkin').style.display = 'none';
					parent.document.getElementById('cal_checkout').style.display = 'none';

				}
					if(target==""){
						checkDiff(div,target);
					}
					if(target=="parent"){
						if(div=="cimd") div2="ciym";
						if(div=="comd") div2="coym";
						loopMonths(div2,my,target);
					}
		}

		
		function checkDiff(div,target)
		{
						
						if(target==""){
							checkinday = document.getElementById('cimd').value;
							checkinym = document.getElementById('ciym').value;

							checkoutday = document.getElementById('comd').value;
							checkoutym = document.getElementById('coym').value;

						}else{
							checkinday = parent.document.getElementById('cimd').value;
							checkinym = parent.document.getElementById('ciym').value;

							checkoutday = parent.document.getElementById('comd').value;
							checkoutym = parent.document.getElementById('coym').value;

						}
						if(!checkinym) checkinym = today_year+"-"+today_month;
						if(!checkoutday) checkoutday = tomorrow_day;
						if(!checkoutym) checkoutym = tomorrow_year+"-"+tomorrow_month;
						//var datein = new Date(checkinym.substr(0,4)+"-"+checkinym.substr(5,2)+"-"+checkinday);
						//var dateout = new Date(checkoutym.substr(0,4)+"-"+checkoutym.substr(5,2)+"-"+checkoutday)
						var split1 = checkinym.split("-");
						var datein = new Date(split1[1]+"/"+checkinday+"/"+split1[0]);
						var split2 = checkoutym.split("-");
						var dateout = new Date(split2[1]+"/"+checkoutday+"/"+split2[0]);
						//if((checkoutday<checkinday && chinm.getMonth()==chonm.getMonth()) || (chinm.getMonth()>chonm.getMonth() && chinm.getFullYear()==chonm.getFullYear()))
						
						if(datein.getTime()>=dateout.getTime())
						{
							if(div=='cimd' || div=='ciym'){
								datein.setDate(datein.getDate()+1);
								var tm = datein.getMonth()+1;
								var tm2 = tm.toString();
								if(tm2.length==1) tm2 = '0'+tm2;

								var td = datein.getDate();
								var td2 = td.toString();
								if(td2.length==1) td2 = '0'+td2;

								var findate = datein.getFullYear()+"-"+tm2+"-"+td2;
								target2 = target;
								if(target2=='') target2="this";
								
								loopDays('comd',findate,target2);
								loopMonths('coym',findate,target2);
							}
							if(div=='comd' || div=='coym'){
								dateout.setDate(dateout.getDate()-1);
								var tm = (dateout.getMonth()+1);
								var tm2 = tm.toString();
								if(tm2.length==1) tm2 = '0'+tm2;

								var td = dateout.getDate();
								var td2 = td.toString();
								if(td2.length==1) td2 = '0'+td2;

								var findate = dateout.getFullYear()+"-"+tm2+"-"+td2;
								target2 = target;
								if(target2=='') target2="this";
								loopDays('cimd',findate,target2);
								loopMonths('ciym',findate,target2);
							}
						}
		}
		
		function loopMonths(div,my,target)
		{
			 var month,year,monthlist,monthname,maxmonth,selected,thismy,startmonth;
			if(my=='' || my===undefined){
				 if(div=='ciym'){
					month= today_month;
					year = today_year;
					selday = today_day;
				}else{
					month= tomorrow_month;
					year = tomorrow_year;
					selday = tomorrow_day;
				}
			}else{
				var mysplit = my.split("-");
				year = mysplit[0];
				month = mysplit[1];
				selday = mysplit[2];
			}

			var sentmy = year+"-"+month;
			var loopto=12;
				monthlist="";
			


			if(div=="ciym" || div=="cimd"){
				startmonth = today_month;
				year = today_year;
			}
			if(div=="coym" || div=="comd"){
				startmonth = tomorrow_month;
				year = tomorrow_year;
			}
			var sd = selday.toString();
			if(sd.length==1) selday='0'+sd;

			if(startmonth!=1) maxmonth = (startmonth-1);
			if(startmonth==1) maxmonth = 12;

			if(target=="" || target=="this"){
					var order=0;
					var theselected='';
					//if(div=="coym") alert(month+" -- "+loopto);
					for(i=startmonth;i<=loopto;i++)
					{
						var m = i.toString();
						if(m.length==1) m='0'+m;

						thismy = year+'-'+m;
						monthlist = thismy+'-'+selday;
						monthname = monthName(i)+' '+year;


						document.getElementById(div).options[order] = new Option(monthname,monthlist);
						if(thismy==sentmy) theselected = order;
						if(startmonth!=1 && i==12){
							year = parseInt(year)+1;
							startmonth = 1;
							i=0;
							loopto = maxmonth;
						}
						order++;
					}
					if(theselected!='') document.getElementById(div).options[theselected].selected = true;
					document.getElementById('cal_checkin').style.display = 'none';
					document.getElementById('cal_checkout').style.display = 'none';
			}else{
					var order=0;
					var theselected='';
					for(i=startmonth;i<=loopto;i++)
					{
						var m = i.toString();
						if(m.length==1) m='0'+m;

						thismy = year+'-'+m;
						monthlist = thismy+'-'+selday;
						monthname = monthName(i)+' '+year;


						parent.document.getElementById(div).options[order] = new Option(monthname,monthlist);
						if(thismy==sentmy) theselected = order;
						if(i==12){
							year = parseInt(year)+1;
							startmonth = 1;
							i=0;
							loopto = maxmonth;
						}
						order++;
					}
					if(theselected!='') parent.document.getElementById(div).options[theselected].selected = true;
					parent.document.getElementById('cal_checkin').style.display = 'none';
					parent.document.getElementById('cal_checkout').style.display = 'none';
			}
					if(target!="this") checkDiff(div,target);

		}

		function monthName(m)
		{
		
			if(m==1) var monthname = ["Januari","January"];
			if(m==2) var monthname = ["Februari"," February"];
			if(m==3) var monthname = ["Mars","March"];
			if(m==4) var monthname = ["April","April"];
			if(m==5) var monthname = ["Maj","May"];
			if(m==6) var monthname = ["Juni","June"];
			if(m==7) var monthname = ["Juli","July"];
			if(m==8) var monthname = ["Augusti","August"];
			if(m==9) var monthname = ["September","September"];
			if(m==10) var monthname = ["Oktober","October"];
			if(m==11) var monthname = ["November","November"];
			if(m==12) var monthname = ["December","December"];


			return monthname[(langid-1)];
			
		}



		function showDiv(div)
		{
			document.getElementById(div).style.display = '';
		}

		function hideDiv(div)
		{
			document.getElementById(div).style.display = 'none';
		}
