﻿// JScript File
////////////////////

function CultureDateElementOffset( srcObj ) {
    this.IsPointInBox = function ( cx, cy ) {
        return( cx >= this.left && cx <= this.right &&  cy >= this.top && cy <= this.bottom )
    }
    
    this.IsIntersec = function ( obj ) {
        //alert( obj.left + "," + obj.top+","+obj.right+","+obj.bottom +"###"+this.left + "," + this.top+","+this.right+","+this.bottom);
        if( this.IsPointInBox(obj.left,obj.top) ) return true
        if( this.IsPointInBox(obj.left,obj.bottom) ) return true
        if( this.IsPointInBox(obj.right,obj.top) ) return true
        if( this.IsPointInBox(obj.right,obj.bottom) ) return true
        
        if( obj.IsPointInBox(this.left,this.top) ) return true
        if( obj.IsPointInBox(this.left,this.bottom) ) return true
        if( obj.IsPointInBox(this.right,this.top) ) return true
        if( obj.IsPointInBox(this.right,this.bottom) ) return true
        
        return false
    }
    
	this.x = 0
	this.y = 0
	var refObj = srcObj
	this.width = refObj.offsetWidth
	this.height = refObj.offsetHeight
	while( refObj ) {
        this.x += refObj.offsetLeft
        this.y += refObj.offsetTop
	    refObj = refObj.offsetParent
	}
	
	this.left = this.x
	this.right = this.x + this.width
	this.top = this.y
	this.bottom = this.y + this.height
}
////////////////////
function CultureDate( srcObj ) {
	this.SetDate = function( newDate ) {
		if( newDate == null || newDate == "" || isNaN(newDate) )
		{
		    this.DateValue = new Date();
    		this.DateControl.value = ""
		}
		else {
		    this.DateValue = newDate

		    var d = this.DateValue.getDate()
		    var m = this.DateValue.getMonth()
		    var y = this.DateValue.getFullYear() - this.DiffYear
		    this.DateControl.value = y.toString() + "/" + m.toString() + "/" + d.toString()
		}
		
		this.ShowDate()
	}

	this.ToDate = function(str) {
		if( str == null || str == "" ) return new Date();
		var part = str.split('/');
		return new Date( parseInt(part[0],10), parseInt(part[1],10), parseInt(part[2],10) );
	}
	
	this.Popup = function() {
		var div
		div = document.all("divCultureDate" ) 
		if( div == null ) {
			div = document.createElement("div")
			div.id = "divCultureDate"
			document.body.appendChild(div)
		}
		else
		{
			div = document.all("divCultureDate") 
		}
		div.className = "CultureDateDiv"
		div.innerHTML = "";
		
		div.style.display = "none"
		
		var ShowOffset = new CultureDateElementOffset( this.ShowControl )
		var PopOffset = new  CultureDateElementOffset( div )
		PopOffset.left = ShowOffset.x
		PopOffset.top = parseInt(ShowOffset.y,10)  + parseInt(ShowOffset.height,10)
		PopOffset.right = PopOffset.left + 250;
		PopOffset.bottom = PopOffset.top + 180;

		for( var i = 0; i < document.all.length ; i++ ) {
		    var refObj = document.all(i)
		    if( refObj.tagName == "SELECT"  ) {
		        //alert(refObj.id)
		        var refOffset = new CultureDateElementOffset( refObj )
		        if( PopOffset.IsIntersec(refOffset) ) {
		            refObj.style.display = "none"
		            refObj.cultureDateHidden = true;
		        }
		    }
		}
		
		div.style.left = PopOffset.left
		div.style.top = PopOffset.top

		div.onBlur = "CultureDate_Hide();";
		div.appendChild( this.CreateBar() )
		div.appendChild( this.CreatePage() )
		div.outerHTML = div.outerHTML
		window.setTimeout( "divCultureDate.style.display='';divCultureDate.focus();", 1 );

	}
	
	this.CreateBar = function () {
		var bar, tr, td, alink
		bar = document.createElement("table")
		bar.className = "CultureDateBar"
		bar.border = 0
		tr = document.createElement("tr")
		
		td = document.createElement("td")
		td.align = "left"
		
		alink = document.createElement("span")
		alink.className = "CultureDateNextBack"
		alink.style.fontFamily = "Webdings"
		alink.style.cursor = "hand"
		alink.innerText = "3"
		alink.onClick = this.ShowControl.id + ".cultureDate.GoBackMonth();"
		td.appendChild( alink )
		tr.appendChild( td )
		
		td = document.createElement("td")
		td.className = "CultureDateYearMonthName"
		td.align = "center"
		var syear = this.DatePage.getFullYear() + parseInt(this.DiffYear,10)
		td.innerText = this.Months[this.DatePage.getMonth()] + " " + syear.toString()
		tr.appendChild( td )
		
		td = document.createElement("td")
		td.align = "right"
		alink = document.createElement("span")
		alink.className = "CultureDateNextBack"
		alink.style.cursor = "hand"
		alink.style.fontFamily = "Webdings"
		alink.innerText = "4"
		
		alink.onClick = this.ShowControl.id + ".cultureDate.GoNextMonth();"
		td.appendChild( alink )
		tr.appendChild( td )
		
		bar.appendChild( tr )
		return bar
	}
	
	this.CreatePage = function() {
		var drun =  new Date()
		var dend =  new Date()
		drun.setTime( this.DatePage.getTime() )
		dend.setTime( this.DatePage.getTime() )

		var cmonth = drun.getMonth()
		while( drun.getDate() > 1 )
			drun.setTime( drun.getTime() - (1000*60*60*24)  );
		while( drun.getDay() != 0 )
			drun.setTime( drun.getTime() - (1000*60*60*24)  );
		while( dend.getMonth() == cmonth )
			dend.setTime( dend.getTime() + (1000*60*60*24)  );
		while( dend.getDay() != 6 )
			dend.setTime( dend.getTime() + (1000*60*60*24)  );
		
		var tb, tr, td
		tb = document.createElement("table")
		tb.className = "CultureDateTable"
		
		tr = document.createElement("tr")
		for( var di = 0; di < this.AbbDays.length; di ++ ) { 
			td = document.createElement("td")
			td.className = "CultureDateDayName"
			td.innerHTML = this.AbbDays[di]
			td.align = "center"
			tr.appendChild(td)
		}
		tb.appendChild(tr)
		
		while( drun.getTime() <= dend.getTime() ) {
			if( drun.getDay() == 0 ) { 
				tr = document.createElement("tr")
			}
		
			td = document.createElement("td")
			td.innerHTML = drun.getDate() + "<span style='display:none'>" + drun.getFullYear().toString() + "/" + drun.getMonth().toString() + "/" + drun.getDate().toString() + "</span>"
			td.style.cursor = "hand"
			td.align = "center"
			td.onClick = this.ShowControl.id + ".cultureDate.SelectDate(this.children(0).innerText)"
			if( drun.getMonth() == cmonth ) {
				if( drun.getDay() == 0  || drun.getDay() == 6 )
					td.className = "CultureDateTextSatSun"
				else
					td.className = "CultureDateText"
			}
			else {
				if( drun.getDay() == 0  || drun.getDay() == 6 )
					td.className = "CultureDateTextSatSunOtherMonth"
				else
					td.className = "CultureDateTextOtherMonth"
			}
						
			tr.appendChild(td)
			
			if( drun.getDay() == 6 ) { 
				tb.appendChild(tr)
			}			
			drun.setTime( drun.getTime() + (1000*60*60*24)  );
		}
		
		return tb
	}
	
	this.GoNextMonth = function() {
		
		this.DatePage.setDate(1);
		if( this.DatePage.getMonth() < 11 ) {
			this.DatePage.setMonth( this.DatePage.getMonth() + 1 );
		}
		else {
			this.DatePage.setYear( this.DatePage.getFullYear() + 1 );
			this.DatePage.setMonth( 0 );
		}
		this.Popup();
	}
	
	this.GoBackMonth = function() {

		this.DatePage.setDate(1);
		
		if( this.DatePage.getMonth() > 0 ) {
			
			this.DatePage.setMonth( this.DatePage.getMonth() - 1 );
		}
		else {
			this.DatePage.setYear( this.DatePage.getFullYear() - 1 );
			this.DatePage.setMonth( 11 );
		}
		this.Popup();
	}
	
	this.ShowDate = function () {
	    if( this.DateControl.value != null && this.DateControl.value != "" ) {
		    var dpart = this.DateControl.value.split('/')
		    this.ShowControl.children[0].value = dpart[2];
		    this.ShowControl.children[1].value = this.Format("@@@");//this.Months[parseInt(dpart[1])];
		    this.ShowControl.children[2].value = parseInt(dpart[0],10) + parseInt(this.DiffYear,10) ;
		}
		else {
		    this.ShowControl.children[0].value = "";
		    this.ShowControl.children[1].value = "";
		    this.ShowControl.children[2].value = "";
		}
		//window.status = "show" + this.DateControl.value
	}
	
	this.SelectDate = function( strDate ) {
		var dpart = strDate.split('/')
		this.DateValue = new Date( dpart[0], dpart[1], dpart[2] );

		var m = parseInt(dpart[1])
		strDate = dpart[0] + "/" + m.toString() + "/" + dpart[2];
		this.DateControl.value = strDate;
		divCultureDate.style.display = "none";
		
		this.ShowDate();
		//window.status = "select:" + this.DateControl.value
		this.ShowControl.DoChange()
	} 
	
	this.AddDay = function( d ) {
		var newDate = new Date()
		newDate.setTime( this.DateValue.getTime() + (d*(1000*60*60*24)) )
		this.DateValue = newDate;
		

		var d = this.DateValue.getDate()
		var m = this.DateValue.getMonth()
		var y = this.DateValue.getFullYear()
		this.DateControl.value = y.toString() + "/" + m.toString() + "/" + d.toString()
		
		this.ShowDate()
	}
	
	this.AddMonth = function( d ) {
		var cy = this.DateValue.getFullYear()
		var cm = this.DateValue.getMonth()
		var cd = this.DateValue.getDate()
		
		var ny = cy + ( (Math.abs(d)/d) * ((Math.abs(d) - (Math.abs(d)%12))/12))
		var nm = (cm + d)%12
		if( nm < 0 ) 
			nm = 12 + nm
		if( cm + (d%12) >= 12  || cm + (d%12) < 0 ) {
			ny += (Math.abs(d)/d) ;
		}
		
		this.DateValue = new Date( ny, nm, cd )

		while( this.DateValue.getMonth() != nm )
			this.AddDay(-1); 

		var d = this.DateValue.getDate()
		var m = this.DateValue.getMonth()
		var y = this.DateValue.getFullYear()
		this.DateControl.value = y.toString() + "/" + m.toString() + "/" + d.toString()
		
		this.ShowDate()
	}
	
	this.AddYear = function( d ) {
		var ny = this.DateValue.getFullYear() + d
		var nm = this.DateValue.getMonth()
		var nd = this.DateValue.getDate()
	
		this.DateValue = new Date( ny, nm, nd )
		
		while( this.DateValue.getDate() != nd )
		    this.AddDay(-1); 
		
		var d = this.DateValue.getDate()
		var m = this.DateValue.getMonth()
		var y = this.DateValue.getFullYear()
		this.DateControl.value = y.toString() + "/" + m.toString() + "/" + d.toString()
		this.ShowDate()
	}
	
	this.Format = function( strFormat ) {
		var d = this.DateValue.getDate()

		var m = this.DateValue.getMonth() + 1
		var y = this.DateValue.getFullYear() //+ parseInt(this.DiffYear,10)
		var mm = "0" + m.toString()
		var dd = "0" + d.toString()
		if( mm.length > 2 ) mm = mm.substr(1,2)
		if( dd.length > 2 ) dd = dd.substr(1,2)

		var strOut = strFormat
		strOut = strOut.replace("yyyy", y.toString() )
		strOut = strOut.replace("yy", y.toString().substr(2,2) )
		strOut = strOut.replace("@@@@", this.Months[m-1] )
		strOut = strOut.replace("@@@", this.AbbMonths[m-1] )
		strOut = strOut.replace("@@", mm )
		strOut = strOut.replace("@", m.toString() )
		strOut = strOut.replace("!!!!", this.Days[this.DateValue.getDay()] )
		strOut = strOut.replace("!!!", this.AbbDays[this.DateValue.getDay()] )
		strOut = strOut.replace("!!", dd )
		strOut = strOut.replace("!", d.toString() )
		return strOut
	}
	
	this.ShowControl = srcObj
	this.DateControl = srcObj.children[3]
	this.Days = this.DateControl.DayNames.split(',')
	this.Months = this.DateControl.MonthNames.split(',')
	this.AbbDays = this.DateControl.AbbDayNames.split(',')
	this.AbbMonths = this.DateControl.AbbMonthNames.split(',')
	this.DiffYear = this.DateControl.DiffYear
	this.DateValue = new Date()
	this.DatePage = new Date()
	this.DateValue = this.ToDate(this.DateControl.value)
	this.DatePage = this.ToDate(this.DateControl.value)
	this.ShowDate()

}
////////////////////
function CultureDate_Hide( datObj ) {
	var obj = document.activeElement
	while( obj != null ){
		if( obj.id == "divCultureDate" ) {
			divCultureDate.focus();
			return;
		}
		obj = obj.parentElement
	}
	for( var i = 0; i < document.all.length ; i++ ) {
         var refObj = document.all(i)
	    if(  refObj.cultureDateHidden == true  ) {
	        refObj.style.display = ""
	    }
	}
	divCultureDate.style.display = "none"
} 
function CultureDate_Popup( datObj ) {
	datObj.cultureDate = new CultureDate( datObj ) 
	datObj.cultureDate.Popup()
}
function CultureDateDayKeyDown() {
	var datObj = window.event.srcElement.parentElement
	datObj.cultureDate = new CultureDate( datObj )
	
	if( window.event.keyCode == 38 ) // up
	{
		datObj.cultureDate.AddDay(1)
		datObj.DoChange()
	} else if( window.event.keyCode == 40 ) // down
	{
		datObj.cultureDate.AddDay(-1)
		datObj.DoChange()
	}
	else if( window.event.keyCode != 9 ) // tab
		window.event.returnValue = false
}
function CultureDateMonthKeyDown() {
	var datObj = window.event.srcElement.parentElement
	datObj.cultureDate = new CultureDate( datObj )
	
	if( window.event.keyCode == 38 ) // up
	{
		datObj.cultureDate.AddMonth(1)
		datObj.DoChange()
	} else if( window.event.keyCode == 40 ) // down
	{
		datObj.cultureDate.AddMonth(-1)
		datObj.DoChange()
	}
	else if( window.event.keyCode != 9 ) // tab
		window.event.returnValue = false
}
function CultureDateYearKeyDown() {
	var datObj = window.event.srcElement.parentElement
	datObj.cultureDate = new CultureDate( datObj )
	
	if( window.event.keyCode == 38 ) // up
	{
		datObj.cultureDate.AddYear(1)
		datObj.DoChange()
	} else if( window.event.keyCode == 40 ) // down
	{
		datObj.cultureDate.AddYear(-1)
		datObj.DoChange()
	}
	else if( window.event.keyCode != 9 ) // tab
		window.event.returnValue = false
}
function CultureDateDisableKeyDown() {
	window.event.returnValue = false
}
