//	Written by Tan Ling wee
//	on 19 June 2005
//	email :	info@sparrowscripts.com
//      url : www.sparrowscripts.com


	var imagePath='images/';
	
	var ie    = document.all;
	var dom   = document.getElementById;
	var ns4   = document.layers;
	var bShow = false;
	var textCtl;

	function setTimePicker(t) {
		textCtl.value = t;
		closeTimePicker();
	}

	function refreshTimePicker() {
		
		sHTML = "<table><tr><td><table>";
		for (hr=0; hr<=23; hr++) {

			sHTML+="<tr align='right'>";

			for (j=0; j<4; j++) {
				sHTML+="<td class='time' onclick='setTimePicker(\""+ hr + ":" + padZero(j*15) + "\")'><a href='javascript:setTimePicker(\""+ hr + ":" + padZero(j*15) + "\")'>" + hr + ":"+padZero(j*15) + "</a></td>";
			}

			sHTML+="</tr>";
		}
		sHTML += "</table></td></tr></table>";
        
		document.getElementById("timePickerContent").innerHTML = sHTML;
	}

	if (dom){
		document.write ("<div id='timepicker' style='z-index:+999;position:absolute;visibility:hidden;'><table><tr><td><table><tr><th><strong>&nbsp;Zeit auswählen&nbsp;</strong></th><th align=right valign=center><img src='" + imagePath + "close_image.gif' onclick='closeTimePicker()' style='cursor:pointer'></th></tr></table></td></tr><tr><td><span id='timePickerContent'></span></td></tr></table></div>");
		refreshTimePicker();
	}

	var crossobj = (dom)?document.getElementById("timepicker").style : ie? document.all.timepicker : document.timepicker;
	var currentCtl;

	function selectTime (ctl,ctl2) {
    
		var leftpos = 0;
		var toppos  = 0;

		textCtl        = ctl2;
		currentCtl     = ctl;
		currentCtl.src = imagePath + "clock.gif";

		aTag = ctl;

		do {
			aTag = aTag.offsetParent;
			leftpos	+= aTag.offsetLeft;
			toppos += aTag.offsetTop;
		} while(aTag.tagName!="BODY");

		crossobj.left =	ctl.offsetLeft + leftpos;
		crossobj.top  = ctl.offsetTop  +	toppos + ctl.offsetHeight +	2;
		crossobj.visibility=(dom||ie)? "visible" : "show";
		hideElement( 'SELECT', document.getElementById("timepicker") );
		hideElement( 'APPLET', document.getElementById("timepicker") );			
		bShow = true;
	}

	// hides <select> and <applet> objects (for IE only)
	function hideElement( elmID, overDiv ){
		if( ie ){
			for( i = 0; i < document.all.tags( elmID ).length; i++ ){
				obj = document.all.tags( elmID )[i];
				if( !obj || !obj.offsetParent ){
						continue;
				}
				  // Find the element's offsetTop and offsetLeft relative to the BODY tag.
				  objLeft   = obj.offsetLeft;
				  objTop    = obj.offsetTop;
				  objParent = obj.offsetParent;
				  while( objParent.tagName.toUpperCase() != "BODY" )
				  {
					objLeft  += objParent.offsetLeft;
					objTop   += objParent.offsetTop;
					objParent = objParent.offsetParent;
				  }
				  objHeight = obj.offsetHeight;
				  objWidth = obj.offsetWidth;
				  if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
				  else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
				  else if( overDiv.offsetTop >= ( objTop + objHeight + obj.height ));
				  else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
				  else
				  {
					obj.style.visibility = "hidden";
				  }
			}
		}
	}
		 
	//unhides <select> and <applet> objects (for IE only)
	function showElement( elmID ){
		if( ie ){
			for( i = 0; i < document.all.tags( elmID ).length; i++ ){
				obj = document.all.tags( elmID )[i];
				if( !obj || !obj.offsetParent ){
						continue;
				}
				obj.style.visibility = "";
			}
		}
	}

	function closeTimePicker() {
		crossobj.visibility="hidden"
		showElement( 'SELECT' );
		showElement( 'APPLET' );
		currentCtl.src=imagePath + "clock.gif"
	}

	document.onkeypress = function hideTimePicker1 () { 
		if (event.keyCode==27){
			if (!bShow){
				closeTimePicker();
			}
		}
	}

	function isDigit(c) {
		
		return ((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9'))
	}

	function isNumeric(n) {
		
		num = parseInt(n,10);

		return !isNaN(num);
	}

	function padZero(n) {
		v="";
		if (n<10){ 
			return ('0'+n);
		}
		else
		{
			return n;
		}
	}

	function validateDatePicker(ctl) {

		t=ctl.value.toLowerCase();
		t=t.replace(" ","");
		t=t.replace(".",":");
		t=t.replace("-","");

		if ((isNumeric(t))&&(t.length==4))
		{
			t=t.charAt(0)+t.charAt(1)+":"+t.charAt(2)+t.charAt(3);
		}

		var t=new String(t);
		tl=t.length;

		if (tl==1 ) {
			if (isDigit(t)) {
				ctl.value=t+":00";
                if (parseInt(t,10)==0) {
                    ctl.value="00:00";
                }
			}
			else {
				return false;
			}
		}
		else if (tl==2) {
			if (isNumeric(t)) {
				if (parseInt(t,10)<24){
					if (t.charAt(1)!=":") {
						ctl.value= t + ':00';
					} 
					else {
						ctl.value= t + '00';
					}
				}
				else if (parseInt(t,10)==24) {
					ctl.value= "00:00";
				}
				else if (parseInt(t,10)<=60) {
					ctl.value= '0:'+padZero(t)+'';
				}
				else {
					ctl.value= '1:'+padZero(t%60)+'';
				}
			}
			else
   		    {
				if ((t.charAt(0)==":")&&(isDigit(t.charAt(1)))) {
					ctl.value = "0:" + padZero(parseInt(t.charAt(1),10)) + "";
				}
				else {
					return false;
				}
			}
		}
		else if (tl>=3) {

			var arr = t.split(":");
			if (t.indexOf(":") > 0)
			{
				hr=parseInt(arr[0],10);
				mn=parseInt(arr[1],10);


				if (isNaN(hr)) {
					hr=0;
				} else {
					if (hr>24) {
						return false;
					}
					else if (hr==24) {
						hr=0;
					}
				}
			
				if (isNaN(mn)) {
					mn=0;
				}
				else {
					if (mn>60) {
						mn=mn%60;
						hr+=1;
					}
				}
			} else {

				hr=parseInt(arr[0],10);

				if (isNaN(hr)) {
					hr=0;
				} else {
					if (hr>24) {
						return false;
					}
					else if (hr==24) {
						hr=0;
					}
				}

				mn = 0;
			}
			
			if (hr==24) {
				hr=0;
			}
			ctl.value=hr+":"+padZero(mn);
		}
	}