﻿// JavaScript Document
function calendar_rang_obj(startAt, dateDisp){
	
	this.monthStrArr = this.monthStrArr? this.monthStrArr: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
	this.dayStrArr = this.dayStrArr? this.dayStrArr: "Sun,Mon,Tue,Wed,Thu,Fri,Sut"
	this.inputSelectedColor = "#EEFFEE"
	var inputs =  this.getElementsByTagName("INPUT")
	var tds =  this.getElementsByTagName("TD")
	var my_y = 0
	var my_x = 0
	var currentOpenDate = new Date()
	var Me = this
	var dateDisplay = "dd/mm/yyyy"
	var nowDate = new Date()
	var currDate  = new Date()
	var direction = getStyle(this, "direction")
	var startPoint  = inputs[0]
	var endPoint  = inputs[1]
	var currDate = new Date()
	var tmpInp
	
	startPoint.currDate = new Date()
	startPoint.isSet = false
	endPoint.currDate = new Date()
	endPoint.isSet = false
	calStartHolder =  startPoint.parentNode
	calEndHolder  =  endPoint.parentNode
	winCal = document.createElement("DIV")
	winCal.className = "calWin"
	winCal.style.zIndex=10000
	winCal.id = "winCal"
	closeDiv = document.createElement("DIV")
	closeDiv.id = "winCalClose"
	
	
	this.setDisplay = function(disp){
		dateDisplay = disp
	}
	this.setNowDate = function(dateStr){
		setObjDateFromStr(nowDate, dateStr,"-", "yyyy-mm-dd")
		adjustDate(nowDate, currDate)
		adjustDate(nowDate, startPoint.currDate)
		adjustDate(nowDate, endPoint.currDate)
		
	}
	function openWin(){
		startPoint.style.borderColor = ""
		endPoint.style.borderColor = ""
		inpOb =this
		tmpInp = this
		
		inpOb.style.borderColor=Me.inputSelectedColor
		my_x =direction == "rtl"? 50-this.offsetWidth :4;
		
		my_y = 30
		tmpPar = this
		while(tmpPar.tagName.toUpperCase() !="BODY"){
			//if (tmpPar.tagName=="BODY")
			if(getStyle(tmpPar, "position") == "relative" || getStyle(tmpPar, "position") == "absolute"   ){
				
				my_x +=	tmpPar.offsetLeft	
				my_y +=	tmpPar.offsetTop-tmpPar.scrollTop
				//alert(tmpPar.offsetLeft + "\n" + tmpPar.tagName + "\n" + getStyle(tmpPar, "position") + "\n" +  my_x + "\n " +  tmpPar.clientWidth)
						
			}
			
					tmpPar = tmpPar.parentNode
		}
			//alert(document.getElementById("winCal").style.zIndex)	
				
		winCal.innerHTML = getCallendarHTML(inpOb)
		winButs= winCal.getElementsByTagName("button")
		winButs[1].onclick = increaseCal
		winButs[0].onclick = decreaseCal
		
		calTable= winCal.getElementsByTagName("TABLE").callendarTable
		
		calTDs = calTable.getElementsByTagName("TD")
		for(i in  calTDs){
			if(calTDs[i].className=="active"){
				calTDs[i].onclick=function(){
					clickDate(inpOb,this)	
				}
			}
		}
		bodyWidth =getStyle( document.body.getElementsByTagName("TABLE")[0],"width")
		scrollL =  eval(getScrollXY()[0])
		winWidth = getWinSize()[0]
		bodyWidth = document.body.offsetWidth 
		//alert(my_x)
		
		/*if (direction=="ltr"){
			
			if(my_x + clrPx(getStyle(winCal, "width")) > bodyWidth )	{
				my_x =  my_x -(my_x + clrPx(getStyle(winCal, "width")) - bodyWidth)
				
			}
		}else{
			
			
			
			if(my_x < 3){
				my_x  =  0
			}
		}*/
		winCal.style.left  = my_x  + "px"
		winCal.style.top  = my_y + "px"
		winCal.style.display = "block"
		closeDiv.style.width = document.body.offsetWidth + "px"
		closeDiv.style.height = document.body.offsetHeight + "px"
		closeDiv.style.display = "block"
		
	}
	//window.document.body.onload = function(){
		window.document.body.appendChild(winCal)
		window.document.body.appendChild(closeDiv)
	//}
	startPoint.style.cursor = "Pointer"
	endPoint.style.cursor = "Pointer"
	startPoint.onclick = openWin
	endPoint.onclick = openWin
	function getCallendarHTML(objInp, increasement){
		if(!increasement) {
			increasement = 0
		}
		endingDate = new Date()
		adjustDate(nowDate, endingDate)
		if (objInp == startPoint){
			
			if(increasement==0){
				currDate.setMonth(startPoint.currDate.getMonth())
				currDate.setFullYear(startPoint.currDate.getFullYear())
			}
			beginDate = nowDate
			if (endPoint.isSet){
				endingDate	= endPoint.currDate
			}else{
				
				endingDate.setFullYear(nowDate.getFullYear()+2)
				}
		}else{ // ************** the input is end point ************;
			if(increasement==0){
				currDate.setMonth(endPoint.currDate.getMonth())
				currDate.setFullYear(endPoint.currDate.getFullYear())
			}
			beginDate = startPoint.currDate
			endingDate.setFullYear(nowDate.getFullYear()+1)
		}
		//alert(objInp == startPoint)
		currDate.setDate(1)
		currMonth = currDate.getMonth()
		dayArr =Me.dayStrArr.split(",")	
		monthArr =Me.monthStrArr.split(",")	
		HTM = '<TABLE width="100%" class="dateMove"  border=0>\
				<TR><td width="18"><img onclick="closeDiv.onclick()" src="/images/closeWin.gif"></td>\
						<TD align="center"><button> < </button> &nbsp; ' + monthArr[ currDate.getMonth()] + '&nbsp;'+ currDate.getFullYear()+  ' &nbsp;<button> > </button><TD>\
				</TR>\
				</TABLE>'
		HTM += '<table id="callendarTable" border=0 cellspacing=1 cellpadding=1>\
					<tr>'
				
		for(n in dayArr){
			HTM+=	'<th>' + dayArr[n] + '</th>'
		}
		HTM+='</tr>'
		for(n=0;n<6; n++){
				
			HTM+='<tr>'
			for(i=0;i<7;i++){
				
				if (currDate.getDay() == i && currMonth ==currDate.getMonth()  ){
					
					isActiveBegin =  beginDate.valueOf() <=  currDate.valueOf()
					isActiveEnd = endingDate.valueOf() >= currDate.valueOf()
				//	alert(currDate.getFullYear())
					if(isActiveBegin && isActiveEnd ){
						classInp = ' class="active" '
					}else{
						classInp = ""	
					}
					HTM+='	<td '+ classInp +'>' + currDate.getDate() + '</td>'
					currDate.setDate(currDate.getDate()+1)
				}else{
					HTM+='	<td>&nbsp;</td>'
				}
				
				
			}
			HTM+='</tr>'
		}
		
		HTM+='</table>'
		currDate.setMonth(currDate.getMonth()-1)
		return HTM
		
	}	
	function setObjDateFromStr(dateObj, dateStr , oporator, disp){
		
		
		if(!disp) disp = dateDisplay
		if(!oporator) oporator= "/"
		dateStrArr = dateStr.split(oporator)
		tmpDateDispArr = disp.split(oporator)
		for(i in tmpDateDispArr){
				
				switch (tmpDateDispArr[i].toLowerCase()){
					case "dd":
						dateObj.setDate(dateStrArr[i])
					break;
					case "mm":
						dateObj.setMonth(dateStrArr[i]-1)
					break;
					case "yyyy":
						dateObj.setFullYear(dateStrArr[i])
					break
				} 
		}
				
	}
	function getStrFromDate(dateObj, oporator, disp){
		
		if(!disp) disp = dateDisplay
		if(!oporator) oporator= "/"
		tmpDateDispArr = disp.split(oporator)
		HTM = ""
		
		for(i in tmpDateDispArr){
				switch (tmpDateDispArr[i].toLowerCase()){
					case "dd":
						if ( i>0) HTM +=oporator
						HTM +=	dateObj.getDate()
					break;
					case "mm":
						if ( i>0 ) HTM +=oporator
						HTM +=	(dateObj.getMonth()+1)
					break;
					case "yyyy":
						if ( i>0 ) HTM +=oporator
						HTM +=	dateObj.getFullYear();
					break;
					} 
				
		}
				return HTM
	}
	function adjustDate(source, destination){
		destination.setFullYear(destination.getFullYear())
		destination.setMonth(destination.getMonth())
		destination.setDate(destination.getDate())
		
		}
	
	function clickDate(inpOb,tdObj){
		inpOb.isSet = true
		
		adjustDate(currDate, inpOb.currDate)
		inpOb.currDate.setFullYear (currDate.getFullYear())
		inpOb.currDate.setMonth (currDate.getMonth())
		if(window.document.body. textContent){
				txt = tdObj.textContent
		}else{
				txt = tdObj.innerText
		}
		inpOb.currDate.setDate(eval(txt))
		inpOb.value = getStrFromDate(inpOb.currDate)
		closeDiv.onclick()
	}
	function increaseCal(){
		//currDate.setDate()
		currDate.setMonth(currDate.getMonth()+1)
		winCal.innerHTML = getCallendarHTML(tmpInp, 1)
		calTable= winCal.getElementsByTagName("TABLE").callendarTable
		winButs= winCal.getElementsByTagName("button")
		winButs[1].onclick = increaseCal
		winButs[0].onclick = decreaseCal
		calTDs = calTable.getElementsByTagName("TD")
		for(i in  calTDs){
			if(calTDs[i].className=="active"){
				calTDs[i].onclick=function(){
					clickDate(inpOb,this)	
				}
			}
		}
		
	}
	function decreaseCal(){
		currDate.setMonth(currDate.getMonth()-1)
		winCal.innerHTML = getCallendarHTML(tmpInp, 1)
		calTable= winCal.getElementsByTagName("TABLE").callendarTable
		winButs= winCal.getElementsByTagName("button")
		winButs[1].onclick = increaseCal
		winButs[0].onclick = decreaseCal
		calTDs = calTable.getElementsByTagName("TD")
		for(i in  calTDs){
			if(calTDs[i].className=="active"){
				calTDs[i].onclick=function(){
					clickDate(inpOb,this)	
				}
			}
		}
	
	
	}
	closeDiv.onclick=function(){
		winCal.style.display = "none"
		closeDiv.style.display = "none"
		startPoint.style.borderColor = ""
		endPoint.style.borderColor = ""
	}
	this.isvalidated = function(start, end){
		valid = true
		if (start && end ) valid = startPoint.isSet && endPoint.isSet
		if (start && !end ) valid = startPoint.isSet 
		if (!start && end ) valid = endPoint.isSet 
		if (!start && !end ) valid = true
		if (!valid)			alert("Please enter a valid  date range")	
		
		return valid
	}
	
	if(isDate(startPoint.value)){
		tmp5 = new Date()
		setObjDateFromStr(tmp5, startPoint.value , "/", "dd/mm/yyyy" )
		startPoint.isSet = true
		adjustDate(tmp5, startPoint.currDate)
		startPoint.currDate.setFullYear (tmp5.getFullYear())
		startPoint.currDate.setMonth (tmp5.getMonth())
		startPoint.currDate.setDate(tmp5.getDate())
		//startPoint.value = getStrFromDate(inpOb.currDate)
	}
	if(isDate(endPoint.value)){
		tmp5 = new Date()
		setObjDateFromStr(tmp5, endPoint.value , "/", "dd/mm/yyyy" )
		endPoint.isSet = true
		adjustDate(tmp5, endPoint.currDate)
		endPoint.currDate.setFullYear (tmp5.getFullYear())
		endPoint.currDate.setMonth (tmp5.getMonth())
		endPoint.currDate.setDate(tmp5.getDate())
		//startPoint.value = getStrFromDate(inpOb.currDate)
	}
	
	
}


