var incTime = "";

var directory = "";

var today = new Date();
var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var hiddenArray = new Array(42);

var currentyear = today.getYear();
if (currentyear < 1000) {currentyear += 1900;}
var currentmonth = today.getMonth();
var currentday = today.getDate();

var dp_dir = "";
var newWin_doc = null;
var newWin = null;
var txtboxObj1 = null;
var txtboxObj2 = null;
var formToBeSubmitted = "";

//customize colors
var bgOverHead = "#FFFFFF";
var bgWeekDay = "#F2F2F2";
var bgDateField = "#F2F2F2";
var bgDateFieldWkEnd = "#E2E2E2";
var bgSelDateField = "#AAAAAA";


function formatdate(dd,mm,yy){
	var strf = newWin_doc.getElementById("dateformat").value;
	var chr = '/';
	var dstr = "";
	if (strf.charAt(3)== '1') {chr = '-';}
	else if (strf.charAt(3)== '3') {chr = '.';}
	
	for (var a=0; a<3; a++){
		if (strf.charAt(a)== 'd') {
			if (new String(dd).length == 2)
				dstr += dd;
			else
				dstr = dstr + '0' + dd;
		}
		else if (strf.charAt(a)== 'm') {
			if (new String(mm).length == 2)
				dstr += mm;
			else
				dstr = dstr + '0' + mm;
		
		}
		else if (strf.charAt(a)== 'y') {dstr += yy;}
		
		if (a < 2) {dstr += chr;}
	}	
	if (newWin_doc.getElementById("including").value == "checked") {
		dstr += " " + newWin_doc.getElementById("time").value;
	}		
	return dstr;
}

function returnRangeDate(textfieldname1,textfieldname2){
	//alert(formToBeSubmitted);
	document.getElementById(textfieldname1).value = newWin_doc.getElementById('client_request_StartDate').value;
  	if (textfieldname2!=''){document.getElementById(textfieldname2).value = newWin_doc.getElementById('client_request_EndDate').value};
	if (formToBeSubmitted != null && formToBeSubmitted != ""){
		document.getElementById(formToBeSubmitted).submit();
	}
	newWin.close();	
}

function returndate(calendar_doc,textfieldname1,textfieldname2,indx){
//  	alert(formToBeSubmitted);
	if(hiddenArray[indx] > 0) {
		if (textfieldname1){
			if (mode == 'range'){
				if (newWin_doc.getElementById(textfieldname1).value == ''){
					newWin_doc.getElementById(textfieldname1).value = formatdate(hiddenArray[indx],newWin_doc.getElementById("months").options.selectedIndex+1,parseInt(newWin_doc.getElementById("years").value));		
				}else{
					if (textfieldname2){
						newWin_doc.getElementById(textfieldname2).value = formatdate(hiddenArray[indx],newWin_doc.getElementById("months").options.selectedIndex+1,parseInt(newWin_doc.getElementById("years").value));
					}else{
						newWin_doc.getElementById(textfieldname1).value = formatdate(hiddenArray[indx],newWin_doc.getElementById("months").options.selectedIndex+1,parseInt(newWin_doc.getElementById("years").value));
					}
				}
			}else{
				document.getElementById(textfieldname1).value = formatdate(hiddenArray[indx],newWin_doc.getElementById("months").options.selectedIndex+1,parseInt(newWin_doc.getElementById("years").value));
				if (formToBeSubmitted != null && formToBeSubmitted != ""){
					document.getElementById(formToBeSubmitted).submit();
				}
				newWin.close();
			}
		}
	}
}


function updateoutput() {
	var ml = newWin_doc.getElementById("months").options.selectedIndex;
	var sday = new Date(parseInt(newWin_doc.getElementById("years").value),ml,01)
	var startindex = sday.getDay();
	var numberOfDays = monthLength[ml];
	if (startindex==0) {startindex = 7;}
	
	if (ml==1 && (parseInt(newWin_doc.getElementById("years").value)%4) == 0){numberOfDays = 29;}
	
	for (var n=0; n<42; n++) {
		var str = "c"+n;
		newWin_doc.getElementById(str).src =dp_dir + "0.gif";
		hiddenArray[n] = 0;
	}
	
	for (var m=0; m<numberOfDays; m++)
	{
		var teh_td = newWin_doc.getElementById("td_"+((startindex+m)%42));
		
		if (teh_td != null)
		{
			teh_td.style.borderStyle = "none";
			if (currentday == (m+1))
			{
				teh_td.style.borderStyle = "solid";
				teh_td.style.borderColor = bgSelDateField;
				teh_td.style.borderWidth = "2px;";
			}
		}

		var str = "c"+((startindex+m-1)%42);
		newWin_doc.getElementById(str).src =dp_dir + (m+1) + ".gif";
		hiddenArray[((startindex+m-1)%42)] = (m+1);
	}
}
		
function updateyears(){
	var tempyear = newWin_doc.getElementById("years").value;
	var styear = currentyear - 1;
	for (var i=0; i <= 11; i++) {
		var s = "yopt"+i;
		newWin_doc.getElementById(s).text = (styear+i);
		newWin_doc.getElementById(s).value = (styear+i);
	}
	for (var i = 0; i < newWin_doc.getElementById("years").options.length; i++ )
	{
		if (newWin_doc.getElementById("years").options[i].value == ""+tempyear)
		{
	  		newWin_doc.getElementById("years").options.selectedIndex = i;
			break;
		}
	}
}	

function show_calendar(textfieldname1,textfieldname2,dir,includingTime,aFormToBeSubmitted,aMode,aLang) {
	if (dir) {dp_dir = dir;}
	newWin = window.open("","_blank","width=220,height=280,status=no,resizable=no,scrollbars=no,top=200,left=300");
	incTime = includingTime;
	newWin.opener = self;
	newWin_doc = newWin.document;
	if (aMode=='range'){
		txtboxObj1 = 'client_request_StartDate';
		txtboxObj2 = 'client_request_EndDate';
	} else {
		txtboxObj1 = textfieldname1;
		txtboxObj2 = textfieldname2;
	}
	formToBeSubmitted = aFormToBeSubmitted;
	mode = aMode;
	lang = aLang;
	writeIt(mode,txtboxObj1,txtboxObj2,textfieldname1,textfieldname2,lang);
}

function writeIt(mode,txtboxObj1,txtboxObj2,textfieldname1,textfieldname2,lang){
	if (window.navigator.userAgent.indexOf("Mac")>=0){
		newWin_doc.open("text/html");
	}
	newWin_doc.writeln('<html><head><title>DatePicker</title></head>');
	newWin_doc.writeln('<body marginwidth="0" marginheight="0" style="margin: 0px;"  onBlur="self.focus()">');
	newWin_doc.writeln('<div align="center"><table bgcolor="' + bgOverHead + '" width="200" cellpadding="0" cellspacing="1"><tr><td align="center" height="35">');
	newWin_doc.writeln('<select style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" id="months" name="months" onchange="window.opener.updateoutput();">');
		if (lang == 'de_CH'){
			newWin_doc.writeln('<option value="1">Januar</option>');
			newWin_doc.writeln('<option value="2">Februar</option>');
			newWin_doc.writeln('<option value="3">März</option>');
			newWin_doc.writeln('<option value="4">April</option>');
			newWin_doc.writeln('<option value="5">Mai</option>');
			newWin_doc.writeln('<option value="6">Juni</option>');
			newWin_doc.writeln('<option value="7">Juli</option>');
			newWin_doc.writeln('<option value="8">August</option>');
			newWin_doc.writeln('<option value="9">September</option>');
			newWin_doc.writeln('<option value="10">Oktober</option>');
			newWin_doc.writeln('<option value="11">November</option>');
			newWin_doc.writeln('<option value="12">Dezember</option>');
		}else{
			newWin_doc.writeln('<option value="1">January</option>');
			newWin_doc.writeln('<option value="2">February</option>');
			newWin_doc.writeln('<option value="3">March</option>');
			newWin_doc.writeln('<option value="4">April</option>');
			newWin_doc.writeln('<option value="5">May</option>');
			newWin_doc.writeln('<option value="6">June</option>');
			newWin_doc.writeln('<option value="7">July</option>');
			newWin_doc.writeln('<option value="8">August</option>');
			newWin_doc.writeln('<option value="9">September</option>');
			newWin_doc.writeln('<option value="10">October</option>');
			newWin_doc.writeln('<option value="11">November</option>');
			newWin_doc.writeln('<option value="12">December</option>');		
		}
	newWin_doc.writeln('</select>&nbsp;');
	
	newWin_doc.getElementById("months").options.selectedIndex = parseInt(currentmonth);
	newWin_doc.writeln('<select style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" name="years" id="years" onchange="window.opener.updateoutput();window.opener.updateyears();">');
			var styear = currentyear - 1;
			for (var i=0; i <= 11; i++) {
				newWin_doc.writeln("<option id=\"yopt"+ i +"\" value=\""+ (styear+i) +"\">" + (styear+i) +"</option>\n");
			}
	newWin_doc.writeln('</select>&nbsp;');
	
	for (var i = 0; i < newWin_doc.getElementById("years").options.length; i++ )
	{
		if (newWin_doc.getElementById("years").options[i].value == ""+currentyear)
		{
	  		newWin_doc.getElementById("years").options.selectedIndex = i;
			break;
		}
	}
	if (lang == 'de_CH'){
		newWin_doc.writeln('<input type="hidden" name="dateformat" id="dateformat" class="choice" value="dmy3">');
	} else {
		newWin_doc.writeln('<input type="hidden" name="dateformat" id="dateformat" class="choice" value="dmy2">');
	}
	newWin_doc.writeln('</td></tr>');
	newWin_doc.writeln('<tr><td>');
	
		newWin_doc.writeln('<TABLE bgcolor="#111111" width="100%" cellpadding="1px" cellspacing="1px">');
		newWin_doc.writeln('<TBODY>');
		newWin_doc.writeln('<TR>');
		if (lang == 'de_CH'){
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Mo</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Di</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Mi</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Do</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Fr</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Sa</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">So</TD>');
		}else{
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Mo</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Tu</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">We</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Th</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Fr</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Sa</TD>');
			newWin_doc.writeln('<TD style="color:#000000;font-size:10px;font-family:Verdana;font-weight:bold;" align="center" width="24" bgcolor="' + bgWeekDay + '">Su</TD>');		
		}

		newWin_doc.writeln('</TR>');
			newWin_doc.writeln("<tr>");
			for (var j=1;j<=42;j++){
				if (j==6 || j==7 || j==13 || j==14 || j==20 || j==21 || j==27 || j==28 || j==34 || j==35 || j==41 || j==42) {
					newWin_doc.writeln("<td id=\"td_"+j+"\" align=\"center\" valign=\"middle\" width=\"24px\" bgcolor='" + bgDateFieldWkEnd + "' onmouseover=\"this.bgColor='" + bgSelDateField + "';\" onmouseout=\"this.bgColor='" + bgDateFieldWkEnd + "'\" onclick=\"window.opener.returndate(window.document, '"+txtboxObj1+"','"+txtboxObj2+"',"+(j-1)+");\"><a href=\"javascript:var i=1;\"><img id=\"c"+(j-1)+"\" src=\""+ dp_dir + "0.gif\" border=\"0\"></a></td>");
				} else {
					newWin_doc.writeln("<td id=\"td_"+j+"\" align=\"center\" valign=\"middle\" width=\"24px\" bgcolor='" + bgDateField + "' onmouseover=\"this.bgColor='" + bgSelDateField + "';\" onmouseout=\"this.bgColor='" + bgDateField + "'\" onclick=\"window.opener.returndate(window.document, '"+txtboxObj1+"','"+txtboxObj2+"',"+(j-1)+");\"><a href=\"javascript:var i=1;\"><img id=\"c"+(j-1)+"\" src=\""+ dp_dir + "0.gif\" border=\"0\"></a></td>");
				}
				if ((j % 7) == 0) {newWin_doc.writeln("</tr>");}
			}
			newWin_doc.writeln("</tr>");
			
			newWin_doc.writeln("<tr>");
			newWin_doc.writeln("<td colspan=\"7\" align=\"center\" valign=\"top\"><font face=\"Arial\" size=\"-1\" color=\"#ffffff\"><strong></strong> <input type=\"hidden\" name=\"time\" id=\"time\" value=\"00:00\" size=\"6\">");
			newWin_doc.writeln("<input type=\"hidden\" id=\"including\" value=\"" + incTime + "\"></font></td>");			
			newWin_doc.writeln("</tr>");			
		newWin_doc.writeln('</TBODY></TABLE>');
	newWin_doc.writeln('</td></tr>');
	if (mode=='range'){
		if (lang == 'de_CH'){
			strDate = 'Datum';
			strSend = 'wählen';
		}else{
			strDate = 'Date';
			strSend = 'search';
		}
		newWin_doc.writeln('<tr><td style="color:#000000;font-size:10px;font-family:Verdana;"><form><br><b>' + strDate + '</b><br><input style="color:#000000;font-size:10px;font-family:Verdana;" type="Text" id="client_request_StartDate" name="client_request_StartDate" size="11">&nbsp;-&nbsp;<input style="color:#000000;font-size:10px;font-family:Verdana;" type="Text" id="client_request_EndDate" name="client_request_EndDate" size="11"><br><br>');
		newWin_doc.writeln("<input style=\"height:20px;font-size:10px;font-family:Verdana;font-style:normal;font-weight:bold;color:#6699AA;background:#EFF5F6;border:1px;border-style:solid;border-color:#979797\" type=\"submit\" name=\"client_request_dummy\" value=\"" + strSend + "\" onclick=\"window.opener.returnRangeDate('" + textfieldname1 + "','" + textfieldname2 + "');\">&nbsp;&nbsp;<input style=\"height:20px;font-size:10px;font-family:Verdana;font-style:normal;font-weight:bold;color:#6699AA;background:#EFF5F6;border:1px;border-style:solid;border-color:#979797\" type=\"reset\" name=\"client_request_dummy\" value=\"reset\"></form></td></tr>");	
	}
	newWin_doc.writeln('</table></div>');
	updateoutput();
	newWin_doc.writeln('</body></html>');
	if (window.navigator.userAgent.indexOf("Mac")>=0){
		newWin_doc.close();	
	}
}
