﻿// JScript File

function _ie(){
    return (window.navigator.appVersion.indexOf('MSIE')==-1 ? false : true)
}

function parseContent(str,tag){
    var cArr=str.split('<cn:'+tag+'>')
    if (cArr.length==2){
        return cArr[1].split('</cn:'+tag+'>')[0]
    }else{
        return '';
    }
}

/**************************************** Data Table Streaming ******************************************/
function StreamTable(Data,Struc,keyCol){
    if(typeof(Struc)=='string'){
    
        var stru=Struc.split('{')[1].split('}')[0];
        var arr=stru.split(','),arr2;
        Struc=new Object();
        for(var idx=0;idx<arr.length;idx++){
            arr2=arr[idx].split(':');
            Struc[arr2[0]]=parseInt(arr2[1])
        }
    }
    
    var key,idx=0,tmp;
    this.rows=new Array()
    this.columns=new Array();

    for(key in Struc){
        this.columns[idx]=new Array();
        this.columns[idx].name=key;
        this.columns[idx].index=Struc[key];
        this.columns[key]=this.columns[idx];
        idx++;
    }
    
    if(typeof(keyCol)=='string'){
        keyCol=this.columns[keyCol].index;
    }else{
        if(!keyCol)keyCol=0;
    }
    
    
    Data=Data.split('^');
    for(idx=0;idx<Data.length;idx++){
        this.rows[idx]=new Array();
        tmp=Data[idx].split('~');
        for(key in Struc){
            this.rows[idx][key]=tmp[this.columns[key].index];
        }
        this.rows['F_'+tmp[keyCol]]=this.rows[idx];
    }
    
    this.find=function(tag){
        return this.rows['F_'+tag];
    }
    
}


/**************************************** Ajax ******************************************/

function getHTTP(){
    var request = false;
    try{
        request = new XMLHttpRequest();
    }catch(err1){
        try{
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(err2){
            try{
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(err3){
                request = false;
            }
        }
    }

    return request;
}
/**************************************** Ajax ******************************************/


function encodeParam(param){
    return param.replace(/<\//gi,'@ltb;').replace(/\/>/gi,'@gtb;').replace(/</gi,'@lt;').replace(/>/gi,'@gt;').replace(/&/gi,'@am;');
}

function umaskContent(){
    try{
    document.getElementById('_loading_').style.visibility='hidden';
    document.getElementById('_loading_').style.height='0px;';
    }catch(ee){}
}

function maskContent(obj){
    var mask=document.getElementById('_loading_')
    if(!obj){
        obj=document.getElementById('all_content')
    }
    var rec=obj.getBoundingClientRect()
    var sty=mask.style;
    
    sty.top=rec.top + document.body.scrollTop;
    sty.left=rec.left + document.body.scrollLeft;
    sty.height=rec.bottom - rec.top;
    sty.width=rec.right - rec.left;
    sty.visibility='visible';
}


/****************************** calendar *************************************************/
    function calMain(cnt,value,onChange,onMonthChange,startYear,endYear,yearHeight,fullListMonth,width,height){
        this.created=(new Date()).getTime();
        this.container=document.getElementById(cnt);
        
        var __m=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
        
        this.origDate=value
        if(!this.origDate)this.origDate=new Date();
        
        var cdt=__m[this.origDate.getMonth()] +' ' + this.origDate.getFullYear();
        //var r,c,ts='<table class="CAL" border="0" cellspacing="1" cellpadding="1" style="'+(width ? 'width:'+width+';' : '')+(height ? 'height:'+height+';' : '')'">';
        var r,c,ts='<table class="CAL" border="0" cellspacing="1" cellpadding="1" style="'+(width ? 'width:'+width+';' : '')+(height ? 'height:'+height+';' : '')+'">';
        ts+='<tr><th class="NP">&lt;</td><th class="MO" colspan="5" '+(height ? 'style="height:'+(parseInt(height)/8)+';"' : '')+'>'+cdt+'</th><th class="NP">&gt;</th></tr>'
        ts+='<tr><th class="SS">S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th class="SS">S</th></tr>'
        for (r=0;r<6;r++){
            ts+='<tr><td class="SS">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td class="SS">&nbsp;</td></tr>'
        } 
        //ts+='<tr><td colspan="7">Controls</td></tr>';
        ts+='</table>';
        
        this.container.innerHTML=ts;
        //alert(dt.getDate() + ' , ' + dt.getDay());
        this.table=this.container.childNodes[0];
        this.monthDate=this.origDate
        this.value=this.origDate;
        this.valueCell=null
        
        var compObj=this;
        
        this.table.calendar=compObj;
        
        this.prev=this.table.rows[0].cells[0];
        this.month=this.table.rows[0].cells[1];
        this.next=this.table.rows[0].cells[2];
        
        this.assignHovData=function(){
            if(!this.hovData)return;
            var r,c,cell,row,ky;
            for(r=0;r<this.hovData.rows.length;r++){
                row=this.hovData.rows[r];
                if(!row.EVT_TAG)continue;
                ky='c_'+row.EVT_DAY+'';
                cell=document.getElementById(ky);
                if(cell)cell.hovData=new Array();
            }
            for(r=0;r<this.hovData.rows.length;r++){
                row=this.hovData.rows[r];
                if(!row.EVT_TAG)continue;
                //cell=ge('c_'+row.EVT_DAY);
                //var ky=ge('c_'+row.EVT_DAY)
                ky='c_'+row.EVT_DAY+'';
                cell=document.getElementById(ky);
                if(cell){
                    cell.style.textDecoration='none';
                    cell.style.color='#0266b1';
                    cell.style.backgroundColor='#9fc5f8';
                    cell.style.fontWeight='bold';
                    cell.hovData[cell.hovData.length]=row;
                }
                
            }
        }
        
        this.dispose=function(){
            try{
                this.table.parentNode.removeChild(this.table);
                this.table=null;
            }catch(ee){}
            try{
                this.detailTable.parentNode.removeChild(this.detailTable);
                this.detailTable=null;
            }catch(ee){}
        }
        
        this.clear=function(){
            var r,c,cell;
            for(r=2;r<this.table.rows.length;r++){
                for(c=0;c<7;c++){
                    cell=this.table.rows[r].cells[c];
                    cell.innerHTML='&nbsp'
                    cell.hovData=new Array();
                    if(c==0 || c==6){   
                        cell.className='SSCLR';
                    }else{
                        cell.className='CLR';
                    }
                    
                    cell.id='';
                    cell.style.textDecoration='none';
                    cell.style.color='';
                    cell.style.backgroundColor='';
                    cell.style.fontWeight='normal';
                    cell.onmousemove=null;
                    cell.onmouseout=null;
                }
            }
        }
        
        this.prev.onclick=function(){
            compObj.genCal(new Date(compObj.value.getFullYear(),compObj.value.getMonth()-1,1))
            if(onMonthChange){
                onMonthChange(compObj);
            }
        }

        this.next.onclick=function(){
            compObj.genCal(new Date(compObj.value.getFullYear(),compObj.value.getMonth()+1,1))
            if(onMonthChange){
                onMonthChange(compObj);
            }

        }
        
        this.genCal=function(tDt){
            var tDt2=new Date(tDt.getFullYear(),tDt.getMonth(),this.value.getDate())

            this.month.innerHTML=__m[tDt.getMonth()] +' ' + tDt.getFullYear()

            if(tDt.getMonth() != tDt2.getMonth()){
                //alert('overflow')
                var nextMonth=new Date(tDt.getFullYear(),tDt.getMonth()+1,1)
                this.monthDate=new Date(nextMonth.getFullYear(),nextMonth.getMonth(),0)
            }else{
                this.monthDate=new Date(tDt.getFullYear(),tDt.getMonth(),this.origDate.getDate())
                //alert('within the new month')
            }
            this.value=compObj.monthDate
            this.clear();
            this.setDays()
        }
        
        
        this.month.onclick=function(){  
            // click on months list
            var sty=compObj.moList.style
            
            if(sty.visibility!='hidden'){
                sty.visibility='hidden';
                return;
            }
            
            var mrec=this.getBoundingClientRect();
            sty.width=mrec.right-mrec.left;
            compObj.moDiv.style.width=sty.width;
            sty.top=mrec.bottom;
            sty.left=mrec.left-2;
            sty.visibility='visible';
            
        }
        
        this.genMonths=function(sYear,eYear){
            var sty;
            if(!this.moList){
                this.moList=document.body.appendChild(document.createElement('table'));
                this.moList.className='CAL';
                this.moList.cellPadding='0';
                this.moList.cellSpacing='0';
                this.moList.border='0';
                
                sty=this.moList.style;
                sty.border='none';
                sty.zIndex=(this.container.style.zIndex+1)
                sty.position='absolute';
                sty.visibility='hidden';
                this.moCell=this.moList.appendChild(document.createElement('tbody')).appendChild(document.createElement('tr')).appendChild(document.createElement('td'));
                this.moDiv=this.moCell.appendChild(document.createElement('div'))
                this.moDiv.className='M';
                this.moDiv.style.overflow='auto';
                
                if(yearHeight){
                    this.moDiv.style.height=yearHeight;
                }
                
                this.detailTable=document.body.appendChild(document.createElement('table'));
                this.detailTable.className='CAL';
                this.detailTable.border='0';
                this.detailCell=this.detailTable.appendChild(document.createElement('tbody')).appendChild(document.createElement('tr')).appendChild(document.createElement('td'));
                this.detailCell.style.verticalAlign='top';
                var html='<table border="0" cellspacing="2" cellpadding="2" style="background-color:Lavender;">'+
                    '<tr><td id="day_evt" style="border:none;border-bottom:1px solid gray;white-space:nowrap;">&nbsp;</td></tr>'+
                    '<tr><td id="day_evt_list" style="border:none;white-space:nowrap;">&nbsp;</td></tr>'+
                    '</table>';
              
                this.detailCell.innerHTML=html;
              
                this.detailCell.className='DET';
                
                sty=this.detailTable.style;
                sty.position='absolute';
                sty.top='-500px';
                sty.left='0px';
            }
            var ts='';
            
            for(var yy = sYear;yy<=eYear;yy++){
                for (var mm in __m){
                    ts+='<span id="yr'+this.created+'_'+yy+'_'+mm+'" style="width:100%;text-align:center;" onmousemove="this.className=\'HL\';" onmouseout="this.className=\'\';">'+(fullListMonth ? __m[mm] : __m[mm].substr(0))+' ' + yy +'</span></br>';
                }
            }            
            this.moDiv.innerHTML=ts;
            var spns=this.moDiv.getElementsByTagName('SPAN');
            for(var ss=0;ss<spns.length;ss++){
                spns[ss].onclick=function(){
                    var iArr=this.id.split('_');
                    compObj.genCal(new Date(parseInt(iArr[1]),parseInt(iArr[2]),1))
                    if(onMonthChange){
                        onMonthChange(compObj);
                    }
                    compObj.moList.style.visibility='hidden';
                }
            }
            
        }

        this.setDays=function(){
            this.clear();
            var dd,ndt,col,pcol=0,rw=2,cell,crec,drec,dLeft,dTop;
            for(dd=1;dd<=31;dd++){
                ndt=new Date(this.value.getFullYear(),this.value.getMonth(),dd);
                if(ndt.getMonth() == this.monthDate.getMonth()){
                    col=ndt.getDay()
                    if(pcol>col)rw++;
                    cell=this.table.rows[rw].cells[col]
                    //cell.className='';

                    if(ndt.getDate() == this.monthDate.getDate()){
                        if(cell.className=='SS' || cell.className=='SSCLR'){
                            cell.className='SSSL';
                        }else{
                            cell.className='SL';
                        }
                        //cell.style.textDecoration='underline';
                        //cell.style.border='1px dotted gray';
                        
                        this.valueCell=cell;
                    }else{
                        //cell.style.border='1px solid #e2e2e2';
                        
                        cell.className=(cell.className.substr(0,2)=='SS' ? 'SS' : '');
                    }

                    //cell.style.padding='0 0 0 0';
                    
                    cell.innerHTML=dd;
                    cell.style.cursor='pointer';
                    cell.objTable=this.table;
                    
                    //cell.title=ndt.getDate() + ' ' + __m[ndt.getMonth()] + '\nEvent:Inauguration of C5 Elevated U-Turn\nSpeaker: Bayani Fernando\nTime:5:00pm\nLocation:C5 Kalayaan Intersection\nContact Person:MMDA\nPhone:0917 555-3333';
                    cell.onmousemove=function(){
                        if(this.className!='MM' && this.className!='SSMM'){
                            if(this.className=='SL' || this.className=='SSSL'){
                            }else if(this.className=='SS'){
                                this.className='SSMM';
                            }else{
                                this.className='MM';
                            }
                            if(this.hovData.length){
                                var iArr=this.id.split('_');
                                var ts='',i;
                                document.getElementById('day_evt').innerHTML='Events for '+ iArr[3] + ' ' + __m[parseInt(iArr[2])-1] + ' ' + iArr[1]
                                for(i=0;i<this.hovData.length;i++){
                                    ts+='[ ' + this.hovData[i].EVT_TIME + ' ] ' + this.hovData[i].EVT_TITLE.replace(/>/gi,'&gt;').replace(/</gi,'&lt;') +'<br />';
//                                        (this.hovData[i].EVT_LOCATION ? this.hovData[i].EVT_LOCATION.replace(/>/gi,'&gt;').replace(/</gi,'&lt;') +'<br />' : '');
                                }

                                document.getElementById('day_evt_list').innerHTML=ts;
                                crec=this.getBoundingClientRect();
                                drec=compObj.detailTable.getBoundingClientRect();
    //                            alert(document.body.scrollLeft + ' , '  + document.body.clientWidth)
                                dLeft=(document.body.scrollLeft + document.body.clientWidth)-(drec.right-drec.left);
                                dTop=(document.body.scrollTop + document.body.clientHeight)-(drec.bottom-drec.top);
                                
                                compObj.detailTable.style.top=document.body.scrollTop + (dTop < crec.bottom ? crec.top-(drec.bottom-drec.top) : crec.bottom);
                                compObj.detailTable.style.left=document.body.scrollLeft + (dLeft < crec.right ? crec.left-(drec.right-drec.left) : crec.right);
                                compObj.detailTable.style.zIndex=(compObj.container.style.zIndex+1)
                                
                                compObj.detailTable.style.display='inline';

                            }else{
                                compObj.detailTable.style.display='none';
                            }
                            
                        }
                        
                        window.status='id:'+this.id;
                    }
                    cell.onmouseout=function(){
                        if(this.className!='' && this.className!='SS'){
                            if(this.className=='SL' || this.className=='SSSL'){
                            }else if(this.className=='SSMM'){
                                this.className='SS';
                            }else{
                                this.className='';
                            }
                        }
                        compObj.detailTable.style.display='none';
                    }
                    
                    cell.id='c_'+this.value.getFullYear()+'_'+(this.value.getMonth()+1)+'_'+dd;
                    cell.onclick=function(){
                        // cell click function    
                        
                        compObj.valueCell.className=compObj.valueCell.className.substr(0,2)=='SS' ? 'SS' : '';
                        
                        this.className=(this.className.substr(0,2)=='SS' ? 'SSSL' : 'SL');
                        
                        compObj.valueCell=this;
                        
                        compObj.value=new Date(compObj.value.getFullYear(),compObj.value.getMonth(),parseFloat(this.innerHTML));
                        
                        if(typeof(onChange)=='function'){
                            onChange(this);
                        }
                    }
                    pcol=col;
                }
                //alert(ndt);
            }
        }
        
        this.processData=function(dat){
        }
        
        this.setDays();
        this.genMonths(startYear,endYear);
        if(onMonthChange){
            onMonthChange(this);
        }
        
    }
