$position = 0;
$dpPath = '';

var $dp = null;
function $getClientWidthHeight(w){
var win = w || window;
if (win.document.documentElement && win.document.documentElement.scrollTop){
return {'width':win.document.documentElement.clientWidth, 'height':win.document.documentElement.clientHeight};
}
else if (win.document.body){
return {'width':win.document.body.clientWidth, 'height':win.document.body.clientHeight};
}
}


function $getScroll(w){
var win = w || window;
if (win.document.documentElement && win.document.documentElement.scrollTop){return {'top':win.document.documentElement.scrollTop, 'left':win.document.documentElement.scrollLeft};}
else if (win.document.body){return {'top':win.document.body.scrollTop, 'left':win.document.body.scrollLeft};}
}


function $getAbsM(topWin)
{
if(topWin==null){topWin=top;}
var leftM = 0;var topM  = 0;
var tempWin = window;
while(tempWin!=topWin){
var ifs = tempWin.parent.document.getElementsByTagName('iframe');
for(var i=0; i<ifs.length; i++){

try{
if(ifs[i].contentWindow == tempWin.window){
var rc = ifs[i].getBoundingClientRect();
leftM += rc.left;
topM  += rc.top;
break;
}
}
catch(e){continue;}
}
tempWin = tempWin.parent;
}
return {'leftM':leftM,'topM':topM};
}


if(navigator.product == 'Gecko')
{
Document.prototype.attachEvent = function(sType,fHandler){
var shortTypeName=sType.replace(/on/,"");
fHandler._ieEmuEventHandler=function(e){
window.event=e;return fHandler();
};
this.addEventListener(shortTypeName,fHandler._ieEmuEventHandler,false);
};

HTMLElement.prototype.insertAdjacentElement=function(where,parsedNode){
       switch(where){
           case "beforeBegin":
               this.parentNode.insertBefore(parsedNode,this);
               break;
           case "afterBegin":
               this.insertBefore(parsedNode,this.firstChild);
               break;
           case "beforeEnd":
               this.appendChild(parsedNode);
               break;
           case "afterEnd":
               if(this.nextSibling){
                   this.parentNode.insertBefore(parsedNode,this.nextSibling);
   }
               else{
                   this.parentNode.appendChild(parsedNode);
   }
               break;
           }
       };
HTMLElement.prototype.getBoundingClientRect = function(){
var obj = this;
var top = obj.offsetTop;     
var left = obj.offsetLeft; 
var right = obj.offsetWidth; 
var bottom = obj.offsetHeight; 
while( obj = obj.offsetParent ) {  
if ( obj.style.position=='absolute' || obj.style.position=='relative' || ( obj.style.overflow!='visible' && obj.style.overflow!= '') ) { 
break; 
}  
top += obj.offsetTop; 
left += obj.offsetLeft; 
}
var theScroll = $getScroll();
left -= theScroll.left;
top  -= theScroll.top;
right += left;
bottom += top;
return {'left':left,'top':top,'right':right,'bottom':bottom};
}
HTMLIFrameElement.prototype.__defineGetter__("Document",function(){return this.contentDocument});
}

function WdatePicker(el,dateFmt,showTime,skin,onPicked){

this.win = window;
this.top = window;
while(this.top.parent.document != this.top.document && this.top.parent.document.getElementsByTagName("frameset").length == 0){this.top = this.top.parent;}


if(navigator.product == 'Gecko'){
this.top.HTMLElement.prototype.insertAdjacentElement = HTMLElement.prototype.insertAdjacentElement;
this.top.Document.prototype.attachEvent = Document.prototype.attachEvent;
this.top.HTMLElement.prototype.getBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
}


this.eCont = (typeof el=='string')?document.getElementById(el):el;
this.dateFmt = dateFmt;
this.showTime = showTime;
this.skin = skin;

if(this.top.document.dateDiv == null){

if($dpPath == ''){
$dpPath = this._createDpPath();
}

this.dd = this.top.document.createElement("DIV");
this.dd.style.cssText = 'position:absolute;z-index:197;';
this.dd.obj = this;
this.dd.className = "WdateDiv";
this.dd.innerHTML = '<iframe src="'+$dpPath+'My97DatePicker.htm#97" frameborder="0" border="0" width="1px" height="1px" scrolling="no"></iframe>';

this.top.document.body.insertAdjacentElement('afterBegin', this.dd);
this.top.document.dateDiv = this.dd;
}
else{
var td = this.top.document.dateDiv;
td.obj.eCont = this.eCont;
td.obj.dateFmt = this.dateFmt;
td.obj.showTime = this.showTime;
td.obj.skin = this.skin;

var dwin = td.childNodes[0].contentWindow;
var d = dwin.$d;
this.dd = this.top.document.dateDiv;
d.obj.init();

d.yDiv.style.display = d.mDiv.style.display = 'none';

d.navLeftImg.src = d.obj.skin + "/navLeft.gif";
d.leftImg.src = d.obj.skin + "/left.gif";
d.rightImg.src = d.obj.skin + "/right.gif";
d.navRightImg.src = d.obj.skin + "/navRight.gif";


d.mInput.setAttribute('realValue',d.obj.month);
d.mInput.value = dwin.dpcfg.aMonStr[d.obj.month-1];
d.yInput.value = d.obj.year;

if(this.showTime){
d.tDiv.style.display = 'block';
d.hhInput.value = d.obj.hour;
d.mmInput.value = d.obj.minute;
d.ssInput.value = d.obj.sec;
}
else{
d.tDiv.style.display = 'none';
}
this.dd.style.display = 'block';

d.obj._setOkInput();

d.obj._setShowAndHide();

d.obj.redraw();
}

$dp = this.top.$dp = this.top.document.dateDiv;


var objxy = this.eCont.getBoundingClientRect();


var mm = $getAbsM(this.top);

var currWinSize = $getClientWidthHeight(this.top);

var theScroll = $getScroll(this.top);
var ddTop = mm.topM + objxy.bottom;
var ddLeft = mm.leftM + objxy.left;
var ddHeight = Math.max(parseInt(this.dd.offsetHeight),180);
var ddWidth = Math.max(parseInt(this.dd.offsetWidth),180);


if(($position==2) || ( ($position==0) && ( (ddTop+ddHeight < currWinSize.height) || (ddTop-ddHeight < ddHeight*0.8)) ) )
{
this.dd.style.top = (theScroll.top + ddTop + 1)+'px';
}
else{
this.dd.style.top = (theScroll.top + ddTop - ddHeight - this.eCont.offsetHeight - 3)+'px';
}


this.dd.style.left = -1 + theScroll.left + Math.min(ddLeft, currWinSize.width-ddWidth-5) +'px';


this.win.document.attachEvent('onmousedown',disposeDatePicker);
this.top.document.attachEvent('onmousedown',disposeDatePicker);
this.win.document.body.onunload = function(){$dp.style.display='none';}
}


WdatePicker.prototype._createDpPath = function(){
var path = '';
var i;

var scripts = document.getElementsByTagName("script");
for(i=0; i<scripts.length; i++){
if(scripts[i].src.substring(scripts[i].src.length-14).toLowerCase() == 'wdatepicker.js'){
path = scripts[i].src.substring(0,scripts[i].src.length-14);
break;
}
}
if(path.substring(0,1) != "/" && path.indexOf('://') == -1){


var a = this.top.location.href;
var b = location.href;

var al='',bl='',bls='';
var j,s='';
for(i=0; i<Math.max(a.length,b.length); i++){
if(a.charAt(i).toLowerCase() != b.charAt(i).toLowerCase()){
j = i;
while(a.charAt(j) != '/'){if(j==0){break;}j-=1;}
al = a.substring(j+1,a.length);
al = al.substring(0,al.lastIndexOf('/'));
bl = b.substring(j+1,b.length);
bl = bl.substring(0,bl.lastIndexOf('/'));

break;
}
}
if(al!=''){for(i=0; i<al.split('/').length; i++){s += "../";}}
if(bl!=''){
s += bl + '/';
}
path = s + path;
}

return path; 
};

function disposeDatePicker()
{
    if($dp!=undefined && $dp.obj!=undefined && $dp.style.display != 'none')
    {
        var d = $dp.childNodes[0].contentWindow.$d;

        if( d.obj.eCont.value == '' || d.obj._judgeCorrectDateTime(d.obj.eCont.value) )
        {
            d.obj._markValue(true);
            if( d.obj.eCont.value != '')
            {
                d.obj._initDate(d.obj.eCont.value,d.obj.dateFmt);

                d.obj._setRealValue();
            }
            else
            {

                d.obj.eCont.setAttribute("REALVALUE","");
            }
        }
        else
        {
            d.obj._markValue(false);
        }

        $dp.style.display = 'none';
    }
}

