//	mh 20050412
//	include in all js files to be sure!
var jslib;if(!jslib){jslib=[];}

//	***********************
//	Date functions
//	***********************

var actField;
var writeDateSpecial;

//	Takes date passed in like 06.03.2001 and returns the day of the week.
function dGetweekday(theDate){
	thisDate=new Date(dToUTC(theDate))
	return thisDate.getDay()
}

//	Takes date passed in like 06.03.2001 and returns a valid UTC date object.
function dToUTC(theDate){
	var aDate 	= theDate.split('.')
	var d		= aDate[0]
	var m		= aDate[1]-1
	var y		= aDate[2]
	//if(jslib.browser.ns&&jslib.browser.mac){d++}
	//	Date object makes January month 0 and December month 11. Fix that.
	if(m==0){
		m=12
		y--
	}
	var dateUTC=Date.UTC(y,m,d)
	return dateUTC
}

//	Takes a UTC date and converts to format 01.01.1970 
function dFromUTC(theDate){
	d=new Date(theDate)
	theDay=d.getDate()
	if(theDay<10){theDay="0"+theDay}
	theMonth=d.getMonth()+1
	if(theMonth<10){theMonth="0"+theMonth}
	theYear=d.getYear()
	if(theYear<70){theYear=theYear+2000}
	else if(theYear>=70&&theYear<1900){theYear=theYear+1900}
	//	Date object makes January month 0 and December month 11. Fix that.
	if(theMonth==0){
		theMonth="12"
		theYear--
		}
	dispDate=theDay+"."+theMonth+"."+theYear
	return dispDate
}


//	Pass in the number of the weekday and the desired language, 
//	and this function will return the corresponding weekday name.
function dDayname(lang,weekday){
	if(lang=="e"){dayarray=new Array(eWeekday)}
	if(lang=="d"){dayarray=new Array(dWeekday)}
	theweekday=dayarray[weekday]
	return theweekday
}

//	System works in milliseconds. Humans don't. Make the conversion, 
//	for calculation purposes.
function dSystemday(days){
	return days*86400000
}

function nullDate(a){
	return (a==""||a=="00.00.00"||a=="0.0.0")
}

//	Is this a valid date entry in the format dd.mm.yyyy?
function dValiddate(a){
	if(!nullDate(a)){
		var okFlag=true
		aDate=a.split('.')
		if(aDate.length<3||a.lastIndexOf('.')>=a.length-2){okFlag=false}

		var d=integer(aDate[0])
		var m=integer(aDate[1])
		var y=integer(aDate[2])
		if(d+""=="NaN"||y+""=="NaN"||y+""=="NaN"){okFlag=false}
		if(m>12){okFlag=false}
		if(d>31){okFlag=false}
		if(y<70){y=y+2000}
		else if(y>=70&&y<1900){y=y+1900}
		if(d<10){d="0"+d}
		if(m<10){m="0"+m}
		var theDate=d+"."+m+"."+y

		if(d>27 && m==2){
			var febdays=((((y % 4==0) && (y % 100!= 0))||(y % 400==0)))?29:28	//	leap year
			if(m==2 && d>febdays){okFlag=false}
		}

		if(okFlag){
			return theDate;
		}else{
			alert('Das eingegebene Datum ist ungültig!')
			return false;
		}
	}else{
		alert('Kein Datum vorhanden');
		return false;
	}
}

//	Returns the validated part of a date passed in as 20.03.2001
function datePart(inDate,datePart){
	inDate=dValiddate(inDate)
	if(inDate){
		myDate=new Date(dToUTC(inDate));
		theYear=myDate.getYear()
		if(theYear<1000){theYear+=1900 }
		if(theYear<1980){
			alert("'Datum von' muss nach dem 1.1.1980 sein.");
			clearDates();
			return false;
		}
		month=myDate.getMonth()+1
		if(month<10){month="0"+month }
		daym=myDate.getDate()
		day=myDate.getDay()
		if(daym<10){daym="0"+daym }

		if(datePart=="d"){
			theBit=daym;
		}else if(datePart=="m"){
			theBit=month;
		}else if(datePart=="y"){
			theBit=theYear;
		}else{
			theBit="NaN";
		}
		return theBit;
	}else{return false}
}

//	Take a date in the format 01.01.1970 and work out which issue it is in.
//	Support for ausgabe, takes Event and Zeitung issue days into account.
function setIssue(){
	var iDays;
	var vonFeld=theForm.elements[formFrom];
	var bisFeld=theForm.elements[formTo];
	if(!vonFeld||!bisFeld){return false}

	vDat=dValiddate(vonFeld.value)
	
	if(!datePart(vDat,"y")){
		return false;
	}else{
		setIt=dValiddate(vDat);
		pubField=theForm.elements["Publikation"]
		if(pubField.selectedIndex<0){pubField.selectedIndex=0}
		pubType=pubField.options[pubField.selectedIndex].value;
		special=(pubType.indexOf("special") !=-1);
		eventissue=(pubType.indexOf("event") !=-1 && !special);
		if(setIt && special){
			bisFeld.value="";
			setWeekday(vDat,theForm.elements["Wochentag"]);
			alert("Sie haben eine Sonderausgabe erstellt.\nTragen Sie bitte <Gültig bis> manuell ein.");

		}else if(setIt && !special){
			theWeekday=dGetweekday(vDat);
			if(!eventissue && theWeekday==2){			//	Tuesday Zeitung
				iDays=2; issueDays=new Array(zIssueDays);

			}else if(!eventissue && theWeekday==5){	//	Friday Zeitung
				iDays=3; issueDays=new Array(zIssueDays);

			}else if(eventissue && theWeekday==2){		//	Tuesday Events
				iDays=6; issueDays=new Array(eIssueDays);
			}

			if(eventissue){calcDays=eJump; issueDays=eIssueDays}
			else{calcDays=zJump; issueDays=zIssueDays}

			if(iDays){
				vonUTC=dToUTC(vDat);
				iSpan=dSystemday(iDays);
				bisDatum=dFromUTC(vonUTC+iSpan);
				bisFeld.value=bisDatum;
				setWeekday(vDat,theForm.elements["Wochentag"]);
			}else{
				changeDate=deConfirm("Dies ist kein normales Ausgabedatum.\nAuf die letzt m(oe)gliche Ausgabe setzen?");
				if(changeDate){
					jumpdays=dSystemday(calcDays[theWeekday]);
					vonDatum=dFromUTC(dToUTC(vDat)-jumpdays);
					newWeekday=dGetweekday(vonDatum);
					if(newWeekday==2){iDays=2}
					else if(newWeekday==5){iDays=3}

					startDate=dToUTC(vonDatum);
					issuespan=dSystemday(issueDays[newWeekday]);
					bisDatum=dFromUTC(startDate+issuespan);
					vonFeld.value=vonDatum;
					bisFeld.value=bisDatum;
					setWeekday(vonDatum,theForm.elements["Wochentag"]);
				}else{
					bisFeld.value="";
					setWeekday(vDat,theForm.elements["Wochentag"]);
					alert("Sie haben eine Sonderausgabe erstellt.\nTragen Sie bitte <Gültig bis> manuell ein.");
				}
			}
		}
	}
}

//	Takes the date (01.01.1970) from the dateField and puts the day of the week in the targetField.
function setWeekday(dateFieldValue,targetField){
	if(dateFieldValue.length>7){
		day=dGetweekday(dateFieldValue);
		targetField.value=dWeekday[day];
	}
}

//	Open popup window containing calendar.
function dOpenCalendar(a){
	actField=a
	actDate=a.value;
	if(((actDate!="")&&(actDate!="00.00.00"))){if(!dValiddate(actDate)){return false}}
	wWidth=180;
	wHeight=220;
	var windowPosition=centre_window(wWidth,wHeight)
	options="menubar=0,location=0,toolbar=0,directories=0,status=0,resizable=0,scrollbars=0,width="+wWidth+",height="+wHeight+",left="+windowPosition[0]+",top="+windowPosition[1];
	link="/common/calendar.gos";
	locCalendarWin=window.open(link,'calendarWin',options);
	if(parent){
		if(parent.extWin){
			parent.extWin[parent.counter]=locCalendarWin;
			parent.counter++;
		}
	}
	return false
}

//	Return the selected value from the calendar to the specified HTML form 
//	field in the calling page, and set issue date and weekday where appropriate.
function writeDate(a,t){
	if(writeDateSpecial){
		writeDateSpecial(a,t)
	}else{
		targetElement=actField
		targetElement.value=a;
		dateSelected=a;
		if(t=="setIssue"){setIssue()}
	}
	locCalendarWin.close()
}

//	calculate special "to" dates and update form fields
function makeBisDates(srcFormField,trgForm){
	if(daysTopstory&&daysFrontstory&&daysTicker&&daysGalleria){
		dateFieldValue=srcFormField.value
		if(dValiddate(dateFieldValue)){
			uVon=dToUTC(dateFieldValue)
			fT=trgForm.elements["datumBisTopstory"];dT=daysTopstory;
			fF=trgForm.elements["datumBisFrontstory"];dF=daysFrontstory;
			fN=trgForm.elements["datumBisTicker"];dN=daysTicker;
			fG=trgForm.elements["datumBisSpecialChannel"];dG=daysGalleria;
			if(fT){fT.value=dFromUTC(uVon+dSystemday(dT))}
			if(fF){fF.value=dFromUTC(uVon+dSystemday(dF))}
			if(fN){fN.value=dFromUTC(uVon+dSystemday(dN))}
			if(fG){fG.value=dFromUTC(uVon+dSystemday(dG))}
		}
	}
}

function convert_date(valIn,dateType){
	//	returns valid UTC date object
	switch(dateType){
		case "UNIX":		//	2004-01-31
			var aDate=valIn.split("-");
			var tDate=aDate[2]+"."+aDate[1]+"."+aDate[0];
			break;

		case "G-OS":		//	31.01.2004
			var tDate=valIn
			break;
	}
	tDate=dValiddate(tDate)
	if(tDate){
		return(new Date(dToUTC(tDate)))
	}else{
		return -1
	}
}