var firstDate = new Date();
var lastDate = new Date();
 
lastDate.setTime(firstDate.getTime()+(1000*(3600*24))*365);

var now = new Date(),ymdd,ddd,dow,top,left,cl,calDate;
var mDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
function Calendar(strValue,elementName,top,left){

	var objRegExp = /(([012]\d)|(3[01]))\/(0\d|1[012])\/((20\d\d)|(19\d\d))/;

	calDate = new Date();
	calDate.setDate(1);

	if(objRegExp.test(strValue)){
		var arrayDate = strValue.split("/");
		var intDay = (arrayDate[0]);
		var intMonth = (arrayDate[1]);
		var intYear = (arrayDate[2]);

		calDate.setYear(intYear);
		calDate.setMonth(intMonth-1);
	}


	var calStr = '<table width="175" border="0" cellspacing="0" cellpadding="0" class="bdr1 bdrBurgundy whiteBG"><tr>';







	var fontStr='style="font-family: verdana,arial,sans-serif; font-size: 11px; line-height: 150%;"';
	leapYear(calDate.getFullYear());

	calStr +="<tr>";

	//alert(calDate.getMonth());

	var borderL='style="border:0px;background:#FFF;"';
	if(calDate.getMonth()==0){
		calStr += (calDate>firstDate)?'<td class="center" colspan="2" width="45"><a href="javascript:Calendar(\''+ ((calDate.getDate()<10)?'0'+calDate.getDate():calDate.getDate())  +'/12/'+(calDate.getFullYear()-1)+'\',\''+elementName+'\','+top+','+left+')" class="cal"><img src="'+imgpath+'arrow_onwhiteleft.gif" alt='+prevMonth+' width="11" height="11" border="0"></td>':'<td colspan="2" class="e" width="45">&nbsp;</td>';
	}else{
		calStr += (calDate>firstDate)?'<td class="center" colspan="2" width="45"><a href="javascript:Calendar(\''+ ((calDate.getDate()<10)?'0'+calDate.getDate():calDate.getDate()) +'/'+(((calDate.getMonth())<10)?0+''+(calDate.getMonth()):(calDate.getMonth()))+'/'+calDate.getFullYear()+'\',\''+elementName+'\','+top+','+left+')" class="cal"><img src="'+imgpath+'arrow_onwhiteleft.gif" alt='+prevMonth+' width="11" height="11" border="0"></td>':'<td colspan="2" class="e" width="45">&nbsp;</td>';
	}
	if(now.getMonth()!=0){
		now.setMonth(now.getMonth()-1);
	}else{
		now.setMonth(11);now.setFullYear(now.getFullYear()-1);
	}

	calStr +='<td valign="top" colspan="3" class="emp VTlabel center b">'+mNames[calDate.getMonth()]+'<br>'+calDate.getFullYear()+'</td>'

	if(calDate.getMonth()!=11){
		calDate.setMonth(calDate.getMonth()+1);
	}else{
		calDate.setMonth(0);calDate.setFullYear(calDate.getFullYear()+1);
	}
	if(calDate<lastDate){
		calStr += '<td colspan="2" class="center"><a href="javascript:Calendar(\''+ ((calDate.getDate()<10)?'0'+calDate.getDate():calDate.getDate()) +'/';
		if(calDate.getMonth()!=12){																																		
			calStr += (((calDate.getMonth()+1)<10)?0+''+(calDate.getMonth()+1):(calDate.getMonth()+1))+'/'+calDate.getFullYear();
		}else{
			calStr += '1'+'/'+(calDate.getFullYear()+1);
		}
		calStr += '\',\''+elementName+'\','+top+','+left+')" class="cal"><img src="'+imgpath+'arrow_onwhite.gif" alt='+nextMonth+' width="11" height="11" border="0"></a></td></tr><tr>'
	}else{
		calStr += '<td colspan="2" class="whiteBG e" width="45">&nbsp;</td></tr><tr>';
	}

	if(calDate.getMonth()!=0){
		calDate.setMonth(calDate.getMonth()-1);
	}else{
		calDate.setMonth(11);calDate.setFullYear(calDate.getFullYear()-1);
	}
	for(var x=0;x<daysLong.length-1;x++){
		calStr += (x==0)?'<td  '+fontStr+borderL+' class="center whiteBG">'+daysLong[x+1].substr(0,1)+'</td>':'<td '+fontStr+' class="center whiteBG">'+daysLong[x+1].substr(0,1)+'</td>'
	}
	calStr+='</tr>';

	var cnt = 0,clDate = (calDate.getDay()==0)?6:calDate.getDay()-1;
	for(var x=0;x<clDate;x++){
		calStr +=(cnt==0)?'<tr><td '+fontStr+borderL+' class="fsmall e" width="24">&nbsp;</td>':'<td '+fontStr+' class="fsmall e">&nbsp;</td>';
		cnt++;
	}
	for(var x=0;x<mDays[calDate.getMonth()];x++){
		if(cnt > 4){
			calStr+=(cnt==0)?'<tr><td '+fontStr+' line-height: 150%;" width="24" '+borderL+' class="center">':'<td '+fontStr+' width="24" class="mmushroomBG center">';
		}else{
			calStr+=(cnt==0)?'<tr><td '+fontStr+' width="24" '+borderL+' class="center">':'<td '+fontStr+' width="24" class="center">';
		}

		calDate.setDate(x+1);

		if(calDate>=firstDate && calDate.getTime()< lastDate.getTime()+(1000*3600)*24){
			calStr +='<a href="javascript:setDay(\''+ (((x+1)<10)?0+''+(x+1):(x+1)) + '/' + (((calDate.getMonth()+1)<10)?0+''+(calDate.getMonth()+1):(calDate.getMonth()+1)) + '/' + calDate.getFullYear()+'\',\''+elementName+'\')" class="cal">'+(x+1)+'</a></td>';
		}else{
			calStr +="<span style='text-decoration: line-through;'>"+(x+1)+"</span></td>"
		}
		cnt++;
		if(cnt == 7){
			calStr +='</tr>';cnt=0;
		}
	}
	if(cnt<7 && cnt!=0){
		calStr+='<td class="e" valign="top" colspan="'+(7-cnt)+'">&nbsp;</td></tr>';
	}
	calStr +='<tr>';
	calStr+='<td '+fontStr+' colspan="7" valign="bottom" align="right" class="b2 r5"><a href="#" onclick="closeit(); return false;"  class="cal">'+calClose+'</a></td>';
	calStr+='</tr></table>';
	

	
	var divStr="";
	divStr+='<iframe width="100%" height="100%" src="javascript:\'<html></html>\';"  scrolling="no" frameborder="0"></iframe>';
	divStr+='<div id="webCalDivIFr" title="" style="top: 0px; width:0px; height: 0px;  left: 0px; position: absolute;  z-index: 9000; display: none;"></div>';	

	cl = document.getElementById("webCal");
	cl.style.top=''+top+"px";
	cl.style.left=''+left+"px";
	cl.style.width='176px';
	cl.style.height='167px';
	cl.innerHTML=divStr;
	cl.style.display="inline";
	
	clDiv = document.getElementById("webCalDivIFr");
	clDiv.innerHTML=calStr;
	clDiv.style.display="inline";
}
function setTch(){
	touched=true;
}
function swapDate(item,doIt){
	if(doIt){
		item.innerHTML=item.title;
	}else{
		if(String(document.location.href).indexOf(".de")!=-1){
			item.innerHTML=item.innerHTML.substr(0,2);
		}else{
			item.innerHTML=item.innerHTML.substr(0,3);
		}
	}
}

var touched = false;
function calcDay(tch){
var inDateVal = document.getElementById(ind).options[document.getElementById(ind).selectedIndex].value;
	var inMonthVal = document.getElementById(inmy).options[document.getElementById(inmy).selectedIndex].value;
	var outDateVal = document.getElementById(outd).options[document.getElementById(outd).selectedIndex].value;
	var outMonthVal = document.getElementById(outmy).options[document.getElementById(outmy).selectedIndex].value;
	if(!isNaN(tch)){touched = true;}
	var lDay = new Date();
	var rDay = new Date();
	lDay.setDate(1);
	lDay.setMonth(parseInt(inMonthVal.substring(4,6)-1));
	lDay.setFullYear(inMonthVal.substring(0,4));
	leapYear(lDay.getFullYear());
	(mDays[lDay.getMonth()]<=inDateVal)?lDay.setDate(mDays[lDay.getMonth()]):lDay.setDate(inDateVal);
	rDay.setDate(1);
	rDay.setMonth(parseInt(outMonthVal.substring(4,6)-1));
	rDay.setFullYear(outMonthVal.substring(0,4));
	leapYear(rDay.getFullYear());
	(mDays[rDay.getMonth()]<=outDateVal)?rDay.setDate(mDays[rDay.getMonth()]):rDay.setDate(outDateVal);
	var minRDay = new Date(serverTime.getTime()+parseInt(getDayAsMilli(offSet)));
	if(touched == false && minRDay.getTime()>rDay.getTime()){
		rDay.setDate(minRDay.getDate());
		rDay.setFullYear(minRDay.getFullYear());
		rDay.setMonth(minRDay.getMonth());
	}
		if(serverTime.getTime()>rDay.getTime()){
			if(serverTime.getDate()<rDay.getDate()){
				rDay.setTime(serverTime.getTime()+parseInt(getDayAsMilli(first)));
			}else{
				if(serverTime.getMonth()!=11){
					rDay.setMonth(serverTime.getMonth()+1)
				}else{
					rDay.setMonth(0);rDay.setFullYear(rDay.getFullYear()+1)
				}
		}
	}
	if(serverTime.getTime()>lDay.getTime()){
		if(serverTime.getDate()<lDay.getDate()){
			lDay.setTime(serverTime.getTime()+parseInt(getDayAsMilli(first)));
		}else{
			if(serverTime.getMonth()!=11){
				lDay.setMonth(serverTime.getMonth()+1)
			}else{
				lDay.setMonth(0);lDay.setFullYear(lDay.getFullYear()+1)
			}
		}
	}
	if(touched == false && lDay.getTime()>=rDay.getTime()){
		rDay.setTime(lDay.getTime()+parseInt(getDayAsMilli(offSet)));
	}
	if(rDay.getTime()>parseInt(serverTime.getTime()+parseInt(getDayAsMilli(last)))){
		rDay.setTime(serverTime.getTime()+parseInt(getDayAsMilli(last)));
	}
	if(lDay.getTime()>parseInt(serverTime.getTime()+parseInt(getDayAsMilli(last)))){
		lDay.setTime(serverTime.getTime()+parseInt(getDayAsMilli(last-1)));
	}

	setDDValue(document.getElementById(inmy),String(lDay.getFullYear()+''+(((lDay.getMonth()+1)<10)?0+''+(lDay.getMonth()+1):(lDay.getMonth()+1))));
	setDDValue(document.getElementById(ind),lDay.getDate());
	setDDValue(document.getElementById(outmy),String(rDay.getFullYear()+''+(((rDay.getMonth()+1)<10)?0+''+(rDay.getMonth()+1):(rDay.getMonth()+1))));
	setDDValue(document.getElementById(outd),rDay.getDate());
	setDates(ind,inmy,idow);
	setDates(outd,outmy,odow);
}
function setDDValue(oDropDown, sValue){
	for(var i=0; i<oDropDown.options.length; ++i){
		if(oDropDown.options[i].value == sValue){oDropDown.selectedIndex = i;break;}
	}
}
function closeit(){
	cl.style.display='none';
}
function leapYear(cyear){
	var isLeapYear = (((cyear % 4 == 0) && (cyear % 100 != 0)) || (cyear % 400 == 0));
	mDays[1] = (isLeapYear)?29:28;
}







/*
	var elDragged = null  // Track current item.

  function doMouseMove() {
    // Check if mouse button is down and if an element is being dragged
    if ((1 == event.button) && (elDragged != null)) {
      // Move the element
      // Where the mouse is in the document
      var intTop = event.clientY+document.body.scrollTop;
      var intLeft = event.clientX + document.body.scrollLeft;
      // Calculate what element the mouse is really in
      var intLessTop  = 0; var intLessLeft = 0;
      var elCurrent = elDragged.offsetParent;
      while (elCurrent.offsetParent!=null) {
	intLessTop+=elCurrent.offsetTop;
	intLessLeft+=elCurrent.offsetLeft;
	elCurrent = elCurrent.offsetParent;
      }
      // Set new position
      elDragged.style.pixelTop = intTop  - intLessTop - elDragged.y;
      elDragged.style.pixelLeft = intLeft - intLessLeft  - elDragged.x;
      event.returnValue = false;
    };
  };

  function checkDrag(elCheck) {
    // Check if the clicked inside an element that supports dragging
    while (elCheck!=null) {
      if (null!=elCheck.getAttribute("dragEnabled"))
	return elCheck
      elCheck = elCheck.parentElement
    }
    return null
  }

  function doMouseDown() {
    // Store element on mousedown.  Called from click handler in code below .
    // All elements that have a "dragEnabled" attribute and are positioned
    // can be dragged.
    var elCurrent = checkDrag(event.srcElement)
    if (null!=elCurrent) {
	elDragged = elCurrent;
	// Determine where the mouse is in the element
	elDragged.x = event.offsetX
	elDragged.y = event.offsetY
	var op = event.srcElement
	// Find real location in respect to element being dragged.
	if ((elDragged!=op.offsetParent) && (elDragged!=event.srcElement)) {
	  while (op!=elDragged) {
	    elDragged.x+=op.offsetLeft
	    elDragged.y+=op.offsetTop
	    op=op.offsetParent
	  }
	}
    }
  }

  function doSelectStart() {
    // Don't start text selections in dragged elements.
    return (null==checkDrag(event.srcElement) && (elDragged!=null))
  }
  // Process mousemove.
  document.onmousedown = doMouseDown;
  document.onmousemove = doMouseMove;
  // Reset element on mouseup.
  document.onmouseup = new Function("elDragged=null");
  document.onselectstart = doSelectStart;
 */

//Position calendar using event
function setCalendar(strValue, elementName, ev) {
	var isIE = (navigator.userAgent.indexOf("MSIE")!=-1);
	if (!ev)
		var ev = window.event||window.Event;
	if('undefined'!=typeof ev.pageX){
		mouseX = ev.pageX;
		mouseY = ev.pageY;
	}else{
		mouseX = ev.clientX;
		mouseY = ev.clientY;
		if (isIE){
			mouseX += document.body.scrollLeft?document.body.scrollLeft:document.documentElement.scrollLeft;
			mouseY += document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop;
		}
	}
	Calendar(strValue, elementName, mouseY , mouseX+10);
	return false;
}
//Set valid dates
function setCalendarInteval(initialDate, finalDate) {
	firstDate = initialDate;
	lastDate = finalDate;
}
//set an interval of 2 years
function setCalendar2YearInteval() {
	var oneYear = 1000 * 3600 * 24 * 365;
	var now = new Date();
	var initialDate = new Date()
	initialDate.setTime(now.getTime() - oneYear);
	var finalDate = new Date()
	finalDate.setTime(now.getTime() + oneYear);
	setCalendarInteval(initialDate, finalDate)
}

