﻿var xCurDate = new Date();
var divPop;
var hInputBox;
var odiv;
var hOhour;
var CurrentView = 1
var CurrentUser = "" ;
var InputBoxDate;
var CurrentRowSpan = null;
var xDays = new Array
						( 
							"sunday",  
							"monday",  
							"Thusday",  
							"Wednesday",  
							"Thursday",  
							"Friday",  
							"Saturday" 
						);
var xHebMonthName	=	new	Array
						(
							"ינואר",
							"פברואר",
							"מרץ",
							"אפריל",
							"מאי",
							"יוני",
							"יולי",
							"אוגוסט",
							"ספטמבר",
							"אוקטובר",
							"נובמבר",
							"דצמבר"
						);

var xEngMonthName	=	new	Array
						(
							"Jan",
							"Feb",
							"Mar",
							"Apr",
							"May",
							"Jun",
							"Jul",
							"Aug",
							"Sep",
							"Oct",
							"Nov",
							"Dec"
						);
function GetMonthName(i){
   if (lang == "eng"){
		return xEngMonthName[i]
		}
	else{
		return xHebMonthName[i]
	}	
}
function changeDate(o){
   var nd = new Date(parseInt(o.dt, 10));
   xCurDate = nd
   if (o.mno)
		{
			switch (parseInt(o.mno,10))
			{
				case 0:
				  nd.setDate(1);
				  nd.setMonth(nd.getMonth() - 1);
				  break;
				
				case 1:
				  nd.setDate(1);
				  nd.setMonth(nd.getMonth() + 1);
				  break;
			}
			
		}
	else{
	 NavBigCal();
	}
  SmallCalendar.innerHTML =	MakeSmallCal(nd,"changeDate(this);")
}
function gotoday(o){
  var nd = StringToDate(o);
  xCurDate = nd;
  CurrentView = 3;
   
  NavBigCal();
  SmallCalendar.innerHTML =	MakeSmallCal(nd,"changeDate(this);")
}

function SelectDate(o){
   var nd = new Date(parseInt(o.dt, 10));
   xCurDate = nd
   if (o.mno)
		{
			switch (parseInt(o.mno,10))
			{
				case 0:
				  nd.setDate(1);
				  nd.setMonth(nd.getMonth() - 1);
				  break;
				
				case 1:
				  nd.setDate(1);
				  nd.setMonth(nd.getMonth() + 1);
				  break;
			}
			divPop.document.body.innerHTML =	MakeSmallCal(nd,"parent.SelectDate(this);")
		}
  else {
        hInputBox.valueutc = FormatDate(nd);
        hInputBox.value = FormatDateForDisplay(nd)
        //odiv.innerText 
        divPop.hide();
  }
}

function FormatDate(o)
{
	return String(o.getFullYear()) + "-" + AddNumber(String(o.getMonth() + 1)) + "-" + AddNumber(String(o.getDate())) + "T" + AddNumber(String(o.getHours())) + ":" + AddNumber(String(o.getMinutes())) + ":00";
}

function FormatDateForDisplay(o)
{
    var m = o.getMonth() + 1;
	var d = o.getDate();
	var y = o.getFullYear();
	
	return AddNumber(d) + "/" + AddNumber(m) + "/" + y;
}

function AddNumber(st)
{
	if (String(st).length == 1){return "0" + st;}
	return st;
}

function NavBigCal(){
  var nd = new Date(xCurDate.valueOf());
  BigCalendar.navigate("/WorkPad/Calendar/Calendar.aspx?View=" + CurrentView + "&InitDate=" + escape(FormatDate(xCurDate))+ "&CurUser=" + CurrentUser)  ;
}

function FirstMonthDay(xDate){
  xDate.setDate(1)
  var i = xDate.getDay()
  if (i == 0){return xDate;}
  xDate.setDate((i * -1) + 1);
  xDate.setHours(0);
  xDate.setMinutes(0);
  xDate.setSeconds(0);
  return xDate;
}

//Modified by Tal Tzur on 10/4/2005
function MakeSmallCal(xCurDate,OCFun)
{
	var nextm = new Date(xCurDate.valueOf())
	var prevm = new Date(xCurDate.valueOf())
	var xHTM = "";
	var mnolside;
	var mnorside;
	if (lang == "heb"){mnolside=1;mnorside=0;}else{mnolside=0;mnorside=1;}
	xHTM += "<table style='border-collapse:collapse;table-layout:fixed;background-color:#CAC8BC;height:160px;width:148px;'>"
	xHTM += "<tr>"
	xHTM += "   <td mno='" + mnolside + "' width='20' dt='"+ nextm.valueOf() +"' onclick='"+ OCFun +"' style='cursor:hand;'><img src='/_imgs/arrow_left1.gif' width='20' height='15' alt='' border='0'></td>"
	xHTM += "   <td align='center' colspan='5'><font style='font-family : Arial, Helvetica, sans-serif;font-size : 14px;font-weight : bold;color : #565656;'>"
	xHTM +=     xCurDate.getYear() + " " + GetMonthName(xCurDate.getMonth())
	xHTM += "      </font></td>"
	xHTM += "   <td mno='" + mnorside + "' width='20' dt='"+ prevm.valueOf() +"' onclick='"+ OCFun +"' style='cursor:hand;'><img src='/_imgs/arrow_right1.gif' width='20' height='15' alt='' border='0'></td>"
	xHTM += "</tr>"
	if (lang == "heb"){
		xHTM += "<tr>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(35) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(34) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(33) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(32) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(31) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(30) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(29) + "</td>"
		xHTM += "</tr>"
	}else{
	    xHTM += "<tr>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(29) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(30) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(31) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(32) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(33) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(34) + "</td>"
			xHTM += "<td style='background-color:#D5E1EF;font-family : Arial, Helvetica, sans-serif;font-size : 12px;font-weight : bold;color : Black;text-align : center;border: 1px solid #CAC8BC;'>" + getExp(35) + "</td>"
		xHTM += "</tr>"
	}
	xCurDate.setHours(0);
	xCurDate.setMinutes(0);
	xCurDate.setSeconds(0);
	  
	var dTemp = new Date (xCurDate.valueOf())
	var cday = FirstMonthDay(dTemp)
	var iCurrMonth = xCurDate.getMonth();
	var iValToday = xCurDate.valueOf();
	var IsInMonth;
	var iCurrDay = 0;
	var dtNow = new Date();


	for (i = 0; i < 6; i++)
		{
		sRow = "";
		xHTM += "<tr>"
		for(x = 0; x < 7; x++)
			{
			iCurrDay = cday.getDate();
			iVal	   = cday.valueOf();
			tdStyle  = "";
			sTD = "";
			IsInMonth = ( cday.getMonth() == xCurDate.getMonth() );
			if (IsInMonth){
				tdStyle = "background-color:#ffffff;font-family:Arial,Helvetica,sans-serif;font-size:11px;color:Black;text-align:center;border:1px solid #CAC8BC;height:20px;width:20px;cursor:hand;"
			}else{
				tdStyle = "background-color:#ffffff;font-family:Arial,Helvetica,sans-serif;font-size:11px;color:Silver;text-align:center;border:1px solid #CAC8BC;height:20px;width:20px;cursor:hand;"
			}
			/*if (iVal == iValToday)
				{
					tdStyle = "background-color : #DCDCDC;font-size : 11px;color : Black;text-align : center;border:2px solid #70D845;height:20px;width: 20px;cursor:hand;"
				}*/
			//Mark today with green
			if (cday.getYear() == dtNow.getYear() && cday.getMonth() == dtNow.getMonth() && cday.getDate() == dtNow.getDate())
				{
					tdStyle = "background-color : #DCDCDC;font-size : 11px;color : Black;text-align : center;border:2px solid #70D845;height:20px;width: 20px;cursor:hand;"
				}
			//Mark the chosen date with red
			if (InputBoxDate != null)
				if (cday.getYear() == InputBoxDate.getYear() && cday.getMonth() == InputBoxDate.getMonth() && cday.getDate() == InputBoxDate.getDate())
				{
					tdStyle = "background-color : #DCDCDC;font-size : 11px;color : Black;text-align : center;border:2px solid #BB0909;height:20px;width: 20px;cursor:hand;"
				}
			 
			sTD = "<td onclick='"+ OCFun +"' style='"+ tdStyle +"' dt='"+ iVal +"'>"+ cday.getDate() +"</td>"
			if (lang == "heb"){
			   sRow = sTD + sRow;
			}else{
			   sRow = sRow + sTD;
			}
			
			  
			cday.setDate(iCurrDay + 1);
			}
			xHTM += sRow + "</tr>"
		}
		xHTM += "</table>"
		
	return xHTM
}

function OpenCalendar(oimg){
   //odiv = oimg.parentElement.previousSibling.firstChild
   //hInputBox = oimg.nextSibling.nextSibling
   hInputBox = oimg.parentElement.previousSibling.firstChild
   if ( hInputBox.value.length > 0){
       cd = StringToDate(hInputBox.value)
       InputBoxDate = cd
   }else{
       cd = new Date()
   }
   CreatePopupCalendar(cd,hInputBox,"parent.SelectDate(this);");
}

function OpenCalendarComplex(oimg){
   odiv = oimg.parentElement.previousSibling.firstChild
   hInputBox = oimg.nextSibling.nextSibling
   if ( hInputBox.value.length > 0){
       cd = StringToDate(hInputBox.value)
        InputBoxDate = cd
   }else{
       cd = new Date()
   }
   CreatePopupCalendar(cd,hInputBox,"parent.SelectDate(this);");
}

function OpenTaskDueDateCalendar(oimg,ohour){
   //odiv = oimg.parentElement.previousSibling.firstChild
   //hInputBox = oimg.nextSibling
   hOhour = ohour
   hInputBox = oimg.parentElement.previousSibling.firstChild
   if ( hInputBox.value.length > 0){
       cd = StringToDate(hInputBox.value)
       InputBoxDate = cd
   }else{
       cd = new Date()
   }
   CreatePopupCalendar(cd,hInputBox,"parent.SelectTaskDueDate(this);");
}
function SelectTaskDueDate(o){
   var nd = new Date(parseInt(o.dt, 10));
   xCurDate = nd
   if (o.mno)
		{
			switch (parseInt(o.mno,10))
			{
				case 0:
				  nd.setDate(1);
				  nd.setMonth(nd.getMonth() - 1);
				  break;
				
				case 1:
				  nd.setDate(1);
				  nd.setMonth(nd.getMonth() + 1);
				  break;
			}
			divPop.document.body.innerHTML = MakeSmallCal(nd,"parent.SelectTaskDueDate(this);")
		}
  else {
        nd.setHours(hOhour.value.substring(0,2),hOhour.value.substring(3,5),0)
        hInputBox.valueutc = FormatDate(nd);
        hInputBox.value = FormatDateForDisplay(nd)
        divPop.hide();
  }
}
function OpenCalendarActivity(oimg,ohour){
   odiv = oimg.parentElement.previousSibling.firstChild
   hInputBox = oimg.nextSibling
   hOhour = ohour
   if ( hInputBox.value.length > 0){
       cd = StringToDate(hInputBox.value)
       InputBoxDate = cd
   }else{
       cd = new Date()
   }
   CreatePopupCalendar(cd,hInputBox,"parent.SelectActivityDate(this);");
}

function CreatePopupCalendar(currd,hib,oclick){
   if ( isNaN(currd) )
	{
		currd = new Date();
	}
	divPop = window.createPopup();
	divPop.document.body.innerHTML = MakeSmallCal(currd,oclick);
	h = 160;
	w = 148;
	divPop.show(0, 20, w, h, event.srcElement);
}

function SelectActivityDate(o){
   var nd = new Date(parseInt(o.dt, 10));
   xCurDate = nd
   if (o.mno)
		{
			switch (parseInt(o.mno,10))
			{
				case 0:
				  nd.setDate(1);
				  nd.setMonth(nd.getMonth() - 1);
				  break;
				
				case 1:
				  nd.setDate(1);
				  nd.setMonth(nd.getMonth() + 1);
				  break;
			}
			divPop.document.body.innerHTML = MakeSmallCal(nd,"parent.SelectActivityDate(this);")
		}
  else {
        nd.setHours(hOhour.value.substring(0,2),hOhour.value.substring(3,5),0)
        hInputBox.value = FormatDate(nd);
        odiv.innerText = FormatDateForDisplay(nd)
        
        try{
            if (hInputBox.id == "scheduledstart"){
			    window.parent.DisplayForm.scheduledend.value = hInputBox.value;
			    window.parent.DisplayForm.scheduledend.parentElement.parentElement.firstChild.firstChild.innerText = odiv.innerText;
		    }
		}catch(er){};
        divPop.hide();
  }
}

function StringToDate(str)
{
	if (str.length > 10)
	{
		// Date + Time
		return new Date(parseInt(str.substr(0, 4), 10), (parseInt(str.substr(5, 2), 10) - 1), parseInt(str.substr(8, 2), 10), parseInt(str.substr(11, 2), 10), parseInt(str.substr(14, 2), 10), parseInt( str.substr( 17, 2), 10 ));
	}
	else
	{
		// date only, no time info
		return new Date(parseInt(str.substr(0, 4), 10), (parseInt(str.substr(5, 2), 10) - 1), parseInt(str.substr(8, 2), 10));
	}
}

var objLastRowOver

function mouseover()
{
    obj = event.srcElement;
	while (obj.tagName != "TR")
	{
		obj = obj.parentElement;
	}
	obj.runtimeStyle.backgroundColor = "#ADC3E7";
	objLastRowOver = obj
}
function mouseout()
{
   if (objLastRowOver)
	{
		objLastRowOver.runtimeStyle.backgroundColor = "";
		objLastRowOver = null;
	}
}

function WeekRowClicked(){
    var obj = event.srcElement;
    
    switch (obj.tagName)
	{
		case "NOBR": obj = obj.parentElement; break;
		case "IMG": obj = obj.parentElement; break;
		default:     
	}
	var parentTR = obj;
	while (parentTR.tagName != "TR")
	{
	   parentTR = parentTR.parentElement;
	   if (parentTR == null) return; 	
	}
	if (!parentTR.oid) {return false;}
	openObjectWindow(parentTR.oid,6);
}

function setCrmUser(oselect){
   CurrentUser = oselect.value ;
   NavBigCal();
}
function DayRowClicked(oSpan){
   if (!IsNull(CurrentRowSpan)){
      CurrentRowSpan.className = 'DayRowSpan'
   }
   oSpan.className = 'DayRowSpanClicked'
   CurrentRowSpan = oSpan
}
function DayCellClicked(oTD){

}
