var PodeFechar = false;
var ictrl = null;
var objCP,objED,objEDP,objDMin,objDMax;
var cF=null;
var cW=null;
var objTid=0;
var objFNoCal=false;
var objCalShown = false;
var objCalCB;
var daysMonth=new Array(12);
daysMonth[0]=daysMonth[2]=daysMonth[4]=daysMonth[6]=daysMonth[7]=daysMonth[9]=daysMonth[11]=31;
daysMonth[3]=daysMonth[5]=daysMonth[8]=daysMonth[10]=30;
daysMonth[1]=28;

function ShowCalendar(eP,eD,eDP,dmin,dmax,tp)
{
    PodeFechar = false;
    if(tp == 0){
        ExibeCalend(eP,eD,eDP,dmin,dmax);
	    ictrl = eP;
    }
    else if(ictrl && ictrl.value != ''){
        var mindate = ConvertToDate(ictrl.value);
        if(mindate != null)
            ExibeCalend(eP,eD,eDP,mindate,dmax);
        else
            ExibeCalend(eP,eD,eDP,dmin,dmax);
    }
    else
        ExibeCalend(eP,eD,eDP,dmin,dmax);
    
    if (document.getElementById("CalFrame") != null)
        document.getElementById("CalFrame").style.visibility = "visible";
}

function ExibeCalend(cP,eD,eDP,dmin,dmax)
{
    clearTimeout(objTid);
    if(objFNoCal){objFNoCal=false;return;}
    if(objCalShown && eD==objED)return;
    objCalShown = true;
    objCalCB = null;
    objCP=cP;
    objED=eD;
    objEDP=eDP;
    objDMin=dmin;
    objDMax=dmax;
    Aguarda();
 }

function Aguarda()
{ 
    for(var i = 0; i < frames.length; i++){
        if (frames[i].name == "CalFrame"){
            cW = frames[i];
            break;
        }
    }
    
    if(cW==null||cW.objFCL==null||cW.objFCL==false){
        objTid=setTimeout("Aguarda()", 200);
    }
    else{
        if(!cF)cF=getObj('CalFrame');
        cF.style.visibility="hidden";
        setTimeout("DoCal()",1);
    }
}

function DoCal()
{
    Posicao(objCP);
    if(!cW)cW=frames['CalFrame'];
    cW.DoCal(objED,objEDP,objDMin,objDMax);
}

function Posicao(cP)
{
	var dB=document.body;var eL=0;var eT=0;
    if(!cF)cF=getObj('CalFrame');
	
	for(var p=cP; p&&p.tagName!='BODY'; p=p.offsetParent)
	{
	    eL+=p.offsetLeft;
	    eT+=p.offsetTop;
    }
	
	var eH=cP.offsetHeight;
	var dH=parseInt(cF.style.height);
	var sT=getScrollTop();
	if(eT-dH>=sT&&eT+eH+dH>getWinHeight()+sT)eT-=dH;else eT+=eH;
	cF.style.left=eL+'px';
	cF.style.top=eT+'px';
	
	if (document.getElementById('CalFrame') != undefined){
	    document.getElementById('CalFrame').style.left = eL+'px';
	    document.getElementById('CalFrame').style.top = eT+'px';
    }
}

function ConvertToDate(valorstring){
	data = valorstring ;
	pos0 = data.indexOf("/");
	
	if(pos0 == -1) return null;
	
	pos1 = data.indexOf("/", pos0 + 1);
	
	if(pos1 == -1) return null;
	
	if(data.indexOf("/", pos1 + 1) != -1) return null;
	
	dia = data.substring(0,pos0);
	dia = (dia.charAt(0) == "0")?dia.charAt(dia.length - 1):dia ;
	
	mes = data.substring(pos0 + 1, pos1);
	mes = (mes.charAt(0) == "0")?mes.charAt(mes.length - 1):mes ;
	mes = mes - 1;
	
	ano = data.substring(pos1 + 1, data.length);
	ano = (ano.charAt(0) == "0")?ano.charAt(ano.length - 1):ano ;

	objData = new Date(ano,mes,dia);
	return objData;
}

function getObj(objID)
{
	if (document.getElementById) {return document.getElementById(objID);}
	else if (document.all) {return document.all[objID];}
	else if (document.layers) {return document.layers[objID];}
}
function getScrollTop()
{
	if(document.documentElement.scrollTop) return document.documentElement.scrollTop;
	if(document.body.scrollTop) return document.body.scrollTop;
	if(window.pageYOffset) return window.pageYOffset;
	return 0;
}

function getWinHeight()
{
	if(window.innerHeight) return window.innerHeight;
	if(document.documentElement.clientHeight) return document.documentElement.clientHeight;
	if(document.body.clientHeight) return document.body.clientHeight;
	return 0;
}

function SetFocusCalend(e){if(e){objFNoCal=true;e.focus();setTimeout("RemoveFocus()", 200);}}
function RemoveFocus(){objFNoCal=false;}

function CalDateSet(eInp,d,m,y,giveFocus)
{
	var ds=GetDateSep();
	var fmt=GetDateFmt();

	if(fmt=="mmddyy")eInp.value=m+ds+d+ds+y;
	else if(fmt=="ddmmyy")eInp.value=d+ds+m+ds+y;
	else eInp.value=y+ds+m+ds+d;
	if(!giveFocus)
	SetFocusCalend(eInp);
}

function SetCalShown(fcshown){objCalShown=fcshown;}
function CalendarCallback(){if(objCalCB)objCalCB();}

function GetInputDate(t)
{
	if(!t.length) return null;
	t=t.replace(/\s+/g,"");
	if(t.match(/[^-|\d|\.|\/]/)) return null;
	var rgt=t.split(/-|\.|\//);
	for(var i=0;i<rgt.length;i++) rgt[i]=parseInt(rgt[i],10);
	if(!rgt[1]) return null;
	var m,d,y;
	var fmt=GetDateFmt();
	if(fmt=="yymmdd")
	{
	if(!rgt[2]) return null;
	m=rgt[1];d=rgt[2];y=rgt[0];
	}
	else
	{
	if(fmt=="mmddyy"){m=rgt[0];d=rgt[1];}
	else{m=rgt[1];d=rgt[0];}
	if(rgt[2])y=rgt[2];
	else y=DefYr(m-1,d);
	}
	m-=1;if(y<100)y+=2000;
	if(y<1601||y>4500||m<0||m>11||d<1||d>GetMonthCount(m,y))return null;
	return new Date(y,m,d);
}

function GetMonthCount(m,y){var c=daysMonth[m];if((m==1)&&EhBissexto(y))c++;return c;}
function EhBissexto(y){if(y%4==0&&((y%100!=0)||(y%400==0)))return true;else return false;}
function DefYr(m,d){var dt=new Date();var yC=(dt.getYear()<1000)?1900+dt.getYear():dt.getYear();if(m<dt.getMonth()||(m==dt.getMonth()&&d<dt.getDate()))yC++;return yC;}

function UpdDt(e)
{    
    //if(null!=f&&null!=t&&null!=GetInputDate(f.value)&&(null==GetInputDate(t.value)||GetInputDate(f.value)>GetInputDate(t.value))){t.value=f.value;}    
}

function GetDowStart() {return 0;}
function GetDateFmt() {return "ddmmyy";}
function GetDateSep() {return "/";}

//chamar no onblur
function CloseCal(){
    PodeFechar = true;
    setTimeout("CloseCalNow();", 500);
}

function CloseCalNow(){
    if (document.getElementById("CalFrame") != null && PodeFechar)
        document.getElementById("CalFrame").style.visibility = "hidden";
}

function setaPodeFecharFalse(){
    PodeFechar = false;
} 
