
var __dp={};
__dp.guid = function(){return "_"+Math.random()*1000000000000000000;};
__dp.setPs = function(obj, ppts){
   /*for(var p in ppts){try{obj[p]=ppts[p];}catch(e){};};*/
   var args = __dp.toArr(arguments);
   var obj = args[0];
   var ppts = args.slice(1);
   for(var i=0; i<ppts.length; i++)
   {
     var ppt = ppts[i];
     for(p in ppt){
       try{
         if(p=="style")__dp.setPs(obj.style, ppt[p]);
         else obj[p] = ppt[p];     
       }
       catch(e){}
     }
   }   
};
__dp.union = function(){var args=__dp.toArr(arguments),ret={};for(var i=0; i<args.length;i++)for(n in args[i])ret[n]=(!ret[n])?args[i][n]:ret[n];return ret;};
__dp.toArr = function(iterable){if(!iterable)return[];var results=[];for(var i = 0; i < iterable.length; i++)results.push(iterable[i]);return results;};
__dp.getPos = function(ele){var t=0,l=0;e=ele;do {t+=e.offsetTop||0;l+=e.offsetLeft||0;if (e.offsetParent==document.body)if (e.style.position=='absolute') break;} while (e = e.offsetParent);return {left:l, top:t};};
__dp.newEle = function(str){return document.createElement(str);};
__dp.setSty = function(obj,sty){__dp.setPs((sty.className?obj:obj.style), sty);}
__dp.AddEvent = function(obj, evType, fn){
    if(obj.addEventListener){obj.addEventListener(evType, fn, false);return true;} 
    else if (obj.attachEvent){var r = obj.attachEvent("on"+evType, fn); return r;} 
    else {return false;} 
};
Function.prototype.dpBind=function(){var method=this,args=__dp.toArr(arguments),object=args.shift();return function() {return method.apply(object, args.concat(__dp.toArr(arguments)));}};

function DatePicker(dateInput, options, response) {
    var opts = __dp.union({},(options?options:{}),{
       target:document.body, 
       showPosition:'topleft', 
       divStyle:{border:'1px gray solid', height:'auto', width:'auto', fontFamily:'Arial', fontWeight:'bold', fontSize:'12px', color:'#688C32', backgroundColor:'#BDE087', fontWeight:'bold', font:''}, 
       dayLabel:{color:'#688C32', fontSize:'10px', fontWeight:'bold'}, 
       thisMonth:{color:'#5c5c5c', fontSize:'11px', width:'20px', fontWeight:'bold'}, 
       otherMonth:{color:'#b0b0b0', fontSize:'11px', width:'20px'}, 
       today:{backgroundColor:'#8BBB43'}, 
       weekDay:{backgroundColor:'#ffffff'}, 
       workingDay:{backgroundColor:'#F0FFD9'}, 
       dayFocus:{backgroundColor:'highlight', color:'highlighttext'}, 
       timeControl:{fontSize:'11px', width:'40px'}, 
       hidingTime:1.5*1000,	
       months:["January","February","March","April","May","June","July","August","September","October","November","December"], 
       monthsHeader:["January","February","March","April","May","June","July","August","September","October","November","December"], 
       days:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"], 
       times:["AM", "PM"],
       timeLabel:"Time",
       showTimeControl:true,
       getSty:function(name){return (typeof(this[name])=="string")?{className:this[name]}:{style:this[name]};}});	
	var months = opts.months;
	var monthsHeader = opts.monthsHeader;
	var days = opts.days;
	var sWeek = 0; 
	var iDate = dateInput==null||isNaN(dateInput)||dateInput==""?new Date():dateInput;
	var pMonth = new Date(iDate); pMonth.setMonth(iDate.getMonth()-1);
	var nMonth = new Date(iDate); nMonth.setMonth(iDate.getMonth()+1);
	var fDay = new Date(iDate);	
	fDay.setDate(1);
	fDay.setDate(1-(7+fDay.getDay()-sWeek)%7);
	var lDay = new Date(nMonth);
	lDay.setDate(0);
    
    if(!DatePicker.div){
       DatePicker.div=__dp.newEle("span");
       document.body.appendChild(DatePicker.div);
       DatePicker.div.isSel=false;
       __dp.setPs(DatePicker.div, {Show:function(){clearTimeout(this.timer);this.style.display='';}, Hide:function(){this.style.display='none';} });
       __dp.setPs(DatePicker.div.style, {position:'absolute'});
    }
    DatePicker.div.onmouseout=function(){if(this.isSel)return; this.timer=setTimeout(function(){this.Hide()}.dpBind(DatePicker.div), opts.hidingTime);};
    DatePicker.div.onmousemove=function(){clearTimeout(this.timer);};
    DatePicker.div.style.zIndex = 1000;//(opts.target.style.zIndex>?opts.target.style.zIndex+100:0);

	var div_ID=__dp.guid(), con_ID=__dp.guid(), pIcon_ID=__dp.guid(), nIcon_ID=__dp.guid(), hr_ID=__dp.guid(), mn_ID=__dp.guid(), ap_ID=__dp.guid();

	var str = ""; 
	str+="<table cellspacing='4' cellpadding='0' width='100%' id='" + div_ID + "'>"+
		 "<tr><td style='cursor:pointer' id='" + pIcon_ID + "' >&#9668;</td>"+
		 "	<td align='center'>"+monthsHeader[iDate.getMonth()]+"&nbsp;"+iDate.getFullYear()+"</td>"+
		 "	<td align='right' style='cursor:pointer' id='" + nIcon_ID + "'>&#9658;</td></tr>" + 
		 "<tr><td colspan='3' valign='top'><table cellspacing='1' cellpadding='0' width='100%'><thead><tr><td colspan='7'></td></tr></thead><tbody id='" + con_ID + "'></tbody></table></td></tr>"+		
	     "<tr><td colspan='3' align='center' style='display:" + (opts.showTimeControl?'':'none') + "'>"+opts.timeLabel+":"+
	     "<select id='" + hr_ID + "'>";for(var i=1;i<13;i++) str+="<option value='"+i+"' " + (iDate.getHours()%12==i%12?"selected":"") + ">"+(i.toString().length==1?'0'+i:i)+"</option>";str += "</select>:"+
	     "<select id='" + mn_ID + "'>";for(var i=0;i<60;i++) str+="<option value='"+i+"' " + (iDate.getMinutes()==i?"selected":"") + ">"+(i.toString().length==1?'0'+i:i)+"</option>";str += "</select>:"+
	     "<select id='" + ap_ID + "'><option value='"+opts.times[0]+"' " + (iDate.getHours()<12?"selected":"") + ">"+opts.times[0]+"</option><option value='"+opts.times[1]+"' " + (iDate.getHours()>=12?"selected":"") + ">"+opts.times[1]+"</option></select>"+
	     "</td></tr>"+
	     "</table>";
	DatePicker.div.innerHTML = str;
	var div = document.getElementById(div_ID);
	
	__dp.setPs(div, opts.getSty("divStyle"));
	
	function tEvt(c){return {onclick:function(){this.isSel=true; clearTimeout(this.timer);}.dpBind(DatePicker.div), onchange:function(){setTimeout(function(){this.blur()}.dpBind(this), 1);}.dpBind(c), onblur:function(){this.isSel=false;}.dpBind(DatePicker.div)};};
	var hr = document.getElementById(hr_ID);
	var mn = document.getElementById(mn_ID);
	var ap = document.getElementById(ap_ID);
	__dp.setPs(hr, opts.getSty("timeControl"));
	__dp.setPs(mn, opts.getSty("timeControl"));
	__dp.setPs(ap, opts.getSty("timeControl"));
	__dp.setPs(hr, tEvt(hr));
	__dp.setPs(mn, tEvt(mn));
	__dp.setPs(ap, tEvt(ap));
	
	function iEvt(c, m){return{onclick:function(time,opts,res){DatePicker(time,opts,res);}.dpBind(DatePicker.div, m, opts, response), onmouseover:function(){this.oldColor=this.style.color;this.style.color='highlight';}.dpBind(c), onmouseout:function(){this.style.color=this.oldColor;}.dpBind(c)};};
	var pIcon = document.getElementById(pIcon_ID);
	__dp.setPs(pIcon, iEvt(pIcon, pMonth));
	var nIcon = document.getElementById(nIcon_ID);
    __dp.setPs(nIcon, iEvt(nIcon, nMonth));
    
	var tPos = __dp.getPos(opts.target);
	var tSize = {width:opts.target.offsetWidth, height:opts.target.offsetHeight};	
	var sp = opts.showPosition.toLowerCase();
	__dp.setPs(DatePicker.div.style, {top:(tPos.top+(sp.indexOf('top')>=0?0:tSize.height))+"px", left:(tPos.left+(sp.indexOf('left')>=0?0:tSize.width))+"px"});

	var con = document.getElementById(con_ID);
    var lr = __dp.newEle("tr");
	for(var n=0; n<7; n++){   
       var d = __dp.newEle("td");	   
       //__dp.setPs(d.style, __dp.union({textAlign:'center'}, opts.dayLabel));
       __dp.setPs(d, opts.getSty("dayLabel"));
       __dp.setPs(d.style, {textAlign:'center'});
       d.innerHTML = days[(sWeek+n)%7];
       lr.appendChild(d);
	}
	con.appendChild(lr);

	var cday = new Date(fDay);
	while (cday.getMonth() == iDate.getMonth() || cday.getMonth() == fDay.getMonth()){
	  var r = __dp.newEle("tr");
	  for(var i=0; i<7; i++){
        var d=__dp.newEle("td");
        d.innerHTML = cday.getDate();
        //__dp.setPs(d.style, __dp.union({cursor:'pointer', textAlign:'center'}, ((cday.getDate()==iDate.getDate()&&cday.getMonth()==iDate.getMonth())?opts.today/*current date*/:((cday.getDay()==0||cday.getDay()==6)?opts.weekDay/*weekend days*/:opts.workingDay/*working days*/)), (cday.getMonth()==iDate.getMonth()?opts.thisMonth/*current month*/:opts.otherMonth/*other month*/)));
        __dp.setPs(d.style, {cursor:'pointer', textAlign:'center'});
        var aa = ((cday.getDate()==iDate.getDate()&&cday.getMonth()==iDate.getMonth())?opts.getSty("today")/*current date*/:((cday.getDay()==0||cday.getDay()==6)?opts.getSty("weekDay")/*weekend days*/:opts.getSty("workingDay")/*working days*/));
        var bb = (cday.getMonth()==iDate.getMonth()?opts.getSty("thisMonth")/*current month*/:opts.getSty("otherMonth")/*other month*/);
        var cc = (aa.className && bb.className)?{className:aa.className +" "+ bb.className}:{style:__dp.union(bb.style, aa.style)};
        __dp.setPs(d, cc);
//        __dp.setPs(d, ((cday.getDate()==iDate.getDate()&&cday.getMonth()==iDate.getMonth())?opts.getSty("today")/*current date*/:((cday.getDay()==0||cday.getDay()==6)?opts.getSty("weekDay")/*weekend days*/:opts.getSty("workingDay")/*working days*/)), (cday.getMonth()==iDate.getMonth()?opts.getSty("thisMonth")/*current month*/:opts.getSty("otherMonth")/*other month*/));
        d.onclick = function(dt, hr, mn, ap, res){res(new Date(dt.getFullYear(),dt.getMonth(),dt.getDate(),(parseInt(hr.value)%12)+(ap.value==opts.times[0]?0:12),mn.value,0));DatePicker.div.Hide();}.dpBind(DatePicker.div, new Date(cday.toUTCString()), hr, mn, ap, response);
        d.onmouseover = function(o){var cn=this.className, os=this.style; this.oldStyle=(o.style?{'style':__dp.union({font:''},os)}:{'className':cn}); __dp.setPs(this, o); }.dpBind(d, opts.getSty("dayFocus"));
        d.onmouseout = function(o){__dp.setPs(this, this.oldStyle);}.dpBind(d, opts.getSty("dayFocus"));
        cday.setDate(cday.getDate()+1);
        r.appendChild(d);
	  }
	  con.appendChild(r);
	}
	DatePicker.div.Show();
};

if(window.navigator.appName.toLowerCase().indexOf("microsoft")<0){
       DatePicker.div=__dp.newEle("span");
       document.body.appendChild(DatePicker.div);
       DatePicker.div.isSel=false;
       __dp.setPs(DatePicker.div, {Show:function(){clearTimeout(this.timer);this.style.display='';}, Hide:function(){this.style.display='none';} });
       __dp.setPs(DatePicker.div.style, {position:'absolute'});
};




