var isMoz = window.navigator.appName.toLowerCase() == "netscape";
var isIE = !isMoz;

//ajax object - xmlhttp1 - for nevigation
var xmlhttp1=false;
try {
	xmlhttp1 = new XMLHttpRequest()
}
catch ( e ) {
	try {
		xmlhttp1 = new ActiveXObject( "MSXML2.XMLHTTP.3.0" );
	}
	catch ( e ) {
		error( "All attempts to create an XMLHttpRequest object failed" );
	}
}	
//ajax object xmlhttp2 for getMonth
var xmlhttp2=false;
try {
	xmlhttp2 = new XMLHttpRequest()
}
catch ( e ) {
	try {
		xmlhttp2 = new ActiveXObject( "MSXML2.XMLHTTP.3.0" );
	}
	catch ( e ) {
		error( "All attempts to create an XMLHttpRequest object failed" );
	}
}	


/******** function getCalender ********/
// set nevigation and td_main (boxes)
function getCalender(year, month)
{
	//set nevigation
	getNavigation(year,month);
	//set month	
	getMonth(year,month,false);	
}
/******** END function getCalender ********/

/******** function getNavigation ********/
function getNavigation(year, month)
{
	var aspxTemplateName  = "getMonthCalendar.aspx";	
	xmlhttp1.open("GET","/Civil-Service/Services/"+ aspxTemplateName +"?functionName=getNavigation&year="+ year + "&month=" + month,true);
	xmlhttp1.onreadystatechange=function()	{
		if (xmlhttp1.readyState==4)				
		{			
			var topMonthNav =  xmlhttp1.responseText;			
			var objTD = document.getElementById("Calendar_UC1_PresentationPublishedModeContainer_td_navigation");
			if( objTD!=null && topMonthNav != null && topMonthNav != "")
			{
				//there is td and answer from ajex					
				objTD.innerHTML = topMonthNav;
			}
			jumpToSelected();
		}
	}
	xmlhttp1.send(null);
}
/******** END function getNavigation ********/
/*****Navigation var*******/
var loopCounter = 0;
var stopDrive = false;	
/***** end Navigation var*******/
/***** function moveNav *******/
//move the ruler back and forward by clicking the navigation icons
function moveNav(navDirection)
{
	var interval = 10;
	var pixelInterval = 5;
	if (navDirection == "back")
	{
		pixelInterval = -5;
	}
	
	
	var navDivObj = document.getElementById("navDiv");
	var navTableObj = navDivObj.getElementsByTagName("TABLE")[0];
	if(navTableObj != null && navDivObj!= null)
	{
		if (navTableObj.style.left == "")
			navTableObj.style.left = 0;
	
		var currLeft = Number(navTableObj.style.left.replace("px", ""))
				
		if (((navDirection == "forward" && currLeft < navTableObj.clientWidth-navDivObj.clientWidth) 
				|| (navDirection == "back" && currLeft > 0))
				&& (stopDrive == false))
		{			
			window.setTimeout(function() {
				loopCounter++
				navTableObj.style.left = currLeft + pixelInterval;
				moveNav(navDirection)
			}, interval);
		}
	}	
}
/***** end function moveNav *******/
/***** function jumpToSelected *******/
//make the selected month in the middle of the ruller
function jumpToSelected()
{
	var selectedTD = document.getElementById("td_rulerChossen");
	var navDivObj = document.getElementById("navDiv");
	if( selectedTD != null && navDivObj != null)
	{
		var navTableObj = navDivObj.getElementsByTagName("TABLE")[0];	
		var newLeftPos = (selectedTD.cellIndex+2) * selectedTD.clientWidth - navDivObj.clientWidth/2;
		
		if (newLeftPos <= 0)
		{
			// we are in the first cells... dont go too right
			newLeftPos = 0
		}
		else if (navTableObj.clientWidth - newLeftPos < navDivObj.clientWidth)
		{
			//we are in the last cells... dont go too left
			newLeftPos = navTableObj.clientWidth-navDivObj.clientWidth;
		}
		
		navTableObj.style.left = newLeftPos;
	}
	
}
/***** end  function jumpToSelected*******/
/******** function getMonth ********/
//get year, month and return the month table
function getMonth(year,month)
{
	var aspxTemplateName  = "getMonthCalendar.aspx";
	var tdObjName;

	xmlhttp2.open("GET","/Civil-Service/Services/"+ aspxTemplateName +"?functionName=getMonth&year="+ year + "&month=" + month ,true);

	xmlhttp2.onreadystatechange=function(){	

		if (xmlhttp2.readyState==4)				
		{	
			var calenderPaint =  xmlhttp2.responseText;
			var objTD = document.getElementById("Calendar_UC1_PresentationPublishedModeContainer_td_main");
			if( objTD!=null && calenderPaint != null && calenderPaint != "")
			{	
				//there is td and answer from ajex					
				objTD.innerHTML = calenderPaint;
			}
		}
	}
	xmlhttp2.send(null);	
}

/******** end function getMonth ********/

/******** function showItemsToEdit ********/
//takes parameters -year amd month from DDL and show items
function showItemsToEdit()
{
	var month;
	var year;	
	
	//takes parameters -year amd month from DDL
	objDDlYear= document.getElementById("Calendar_UC1_presentationModeContainerContentSpan_DDL_Year");	
	objDDlMonth= document.getElementById("Calendar_UC1_presentationModeContainerContentSpan_DDL_Month");	
	if(objDDlMonth != null && objDDlYear != null)
	{
		month = objDDlMonth.options[objDDlMonth.selectedIndex].value;
		year = objDDlYear.options[objDDlYear.selectedIndex].value;
	}
	var aspxTemplateName  = "getMonthCalendar.aspx";	
	xmlhttp1.open("GET","/Civil-Service/Services/"+ aspxTemplateName +"?functionName=showItemsToEdit&year="+ year + "&month=" + month,true);
	xmlhttp1.onreadystatechange=function() {
		if (xmlhttp1.readyState==4)				
		{	
			var calenderPaint =  xmlhttp1.responseText;
			var objTD = document.getElementById("Calendar_UC1_presentationModeContainerContentSpan_td_Edit");
			if( objTD!=null && calenderPaint != null && calenderPaint != "")
			{
				//there is td and answer from ajex					
				objTD.innerHTML = calenderPaint;
			}
		}
	}
	xmlhttp1.send(null);
}
/******** end function showItemsToEdit ********/

/******** function clearCache ********/
//take from DDL_Year the range of years to refresh
//call function deleteFromCache delete it from the Cache
//sending to her:	- the years seperated by *
//					- pstGuid - for refreshing the page
function clearCache(pstGuid)
{
	var strYears = "";
	objDDlYear= document.getElementById("Calendar_UC1_presentationModeContainerContentSpan_DDL_Year");	
	if(objDDlYear != null)
	{
		for(i=0; i<objDDlYear.options.length;i++)
			{
				strYears +=  objDDlYear.options[i].value + "*";
			}
	}	
	//call function deleteFromCache - AJAX
	if(strYears.length > 0)
	{
		var aspxTemplateName  = "getMonthCalendar.aspx";	
		xmlhttp1.open("GET","/Civil-Service/Services/"+ aspxTemplateName +"?functionName=deleteFromCache&yearsRange="+ strYears +"&pstGuid=" + pstGuid,true);
		xmlhttp1.onreadystatechange=function() {
			if (xmlhttp1.readyState==4)				
			{	
				var strUrl =  xmlhttp1.responseText;
				if(strUrl != null && strUrl.length > 0)
				{				
					//do redirct / refresh
					window.location = strUrl;
				}
				else
				{
					//emptyString -> meaning error in cleaning cache
					alert("בעיה ברענון הדף. אנא רענן את הדף באופן עצמאי");
				}
				
			}
		}
		xmlhttp1.send(null);
	}
}
/******** end function clearCache ********/

/******** ToolTip ********/
function showToolTip(obj)
{	
	// create the new div
	var toolTipId = "toolTipInfo";
	
	
	var newDiv = document.createElement("div");	
	newDiv.className = "toolTipStyle";
	newDiv.id = toolTipId;
	newDiv.innerHTML = obj.getAttribute("strToolTip");
	
	// set the tool tip position	
	var parentObj = obj;
	
	newDiv.style.top = findPosY(parentObj);	
	if(isMoz)
	{
		newDiv.style.left = findPosX(parentObj) + parentObj.offsetWidth - 250;
	}
	else
	{
		newDiv.style.left = findPosX(parentObj) + parentObj.offsetWidth - 55;
	
	}
	if(newDiv.style.left.charAt(0) == "-")
	{
		// if position left is negative --> make it Posstive
		var intPosLeft = newDiv.style.left;
		intPosLeft = intPosLeft.replace("-","");
		intPosLeft = intPosLeft.replace("px","");
		intPosLeft = parseInt(intPosLeft) + 180;
		newDiv.style.left = intPosLeft;
		
	}
	document.body.appendChild(newDiv);
	
}
function removeToolTip(obj)
{
	var toolTipId = "toolTipInfo";
	try
	{
		var dv = document.getElementById(toolTipId); 	
		dv.parentNode.removeChild(dv);
	}
	catch(ex){}
	
}
function findPosX(obj)
{
	var curleft = 0;
	while (obj.offsetParent)
	{
		curleft += obj.offsetLeft
		obj = obj.offsetParent;
	}
	return curleft;
}
function findPosY(obj)
{
	var curtop = 0;
	while (obj.offsetParent)
	{
		curtop += obj.offsetTop
		obj = obj.offsetParent;
	}
	return curtop;
}
/******** end ToolTip ********/

/******** fadding ********/
function showOpacDiv()
{
	var dv = document.createElement("div");
	dv.className = "opacDiv";
	dv.id = "opacDiv";
	dv.style.top = 0;
	dv.style.left = 0;

   	document.body.appendChild(dv);			
	
	var bodyW =  document.body.scrollWidth;
	var bodyH =  document.body.scrollHeight;
	
	dv.style.width = bodyW;
	dv.style.height = bodyH;
}
function removeOpacDiv()
{
	var dv = document.getElementById("opacDiv"); 
	if(dv == null)
		return;
	
	dv.removeNode(true);
}
/******** end fadding ********/


/******** Print ******/
var firstTime="true";
var senderName, senderMail, comments;
var ajaxResponse;
function showPopDiv(url, divId)
{
    if(document.getElementById(divId) != null)
        return;

	// show div as page
	var xmlText;
	httpRequestAjax(url);
	firstTime="false";
	
	// create the new div
    var newDiv = document.createElement("div");
	newDiv.className = "printDiv";
	newDiv.id = divId;
   	// get the content
 
 
    newDiv.innerHTML = ajaxResponse;
    // define the location
    document.body.appendChild(newDiv);
    
    var divWidth = newDiv.clientWidth;
    var divHeight = newDiv.clientHeight;
	   
    var clientBodyW =  document.body.clientWidth;
	var clientBodyH =  document.body.clientHeight;
	newDiv.style.left = document.body.scrollLeft + (clientBodyW/2) - (newDiv.offsetWidth/2);
	newDiv.style.top = document.body.scrollTop + (clientBodyH/2) - (newDiv.offsetHeight/2);
    if(document.getElementById("opacDiv")==null)
    {
		// show the disable div
        showOpacDiv();
      }
}

function closePopUpDiv(nDiv)
{
	var nPopDiv = document.getElementById(nDiv);
	
   if(isNullOrUndefined(nPopDiv)==true)
   {
        return;
   }
    nPopDiv.parentNode.removeChild(nPopDiv);
    removeOpacDiv(); 
    firstTime="true";
}  
  
function isNullOrUndefined(obj)
{
	return ((obj==null) || (obj==undefined) || (typeof(obj)=="undefined"));
}

function httpRequestAjax(url)
{
	xmlhttp1.open("GET",url,false);	
	xmlhttp1.onreadystatechange=function() 
	{
		if (xmlhttp1.readyState==4)				
		{	
			ajaxResponse = xmlhttp1.responseText;	
		}
	}
	xmlhttp1.send(null)
}

/******** end Print ******/

/******** PrintMenu ******/
// function uncheckAllOther
//when the last option is checked call this function
//make sure other options arent checked
//'last option' is  id = 'ckb_7'
function uncheckAllOther()
{
	objckb =  document.getElementsByName("ckbHolidays");
	
	if(objckb == null)
	{
		return;
	}
	
	for (var j = 0; j < objckb.length; j++)
	{
		if (objckb[j].checked == true) 
		{
			if(objckb[j].id != 'ckb_7')
			{			
				objckb[j].checked = false;
			}
		}
	}
}

//function unCheckedLastOption
//when one of the option (ckb - as input) is checkd (except of 'ckb_7') make sure 'ckb_7' is not checekd
function unCheckedLastOption(ckb)
{
	if(ckb != null && ckb.checked == true)
	{
		var boxId_1 =  document.getElementById('ckb_7'); 
		if(boxId_1 != null)
		{
			boxId_1.checked = false;
		}
	}
	
}

//when a DDL is open
//make sure the radioButton near it is checkd
function radioButtonChoseIt(DDL)
{	
	if(DDL.id == 'DDL_m_Year' || DDL.id == 'DDL_m_Month')
	{
		//checked radio_id_month
		radioBth = document.getElementById('radio_id_month');
	}
	else if(DDL.id == 'DDL_y_Year')
	{
		//checked radio_id_year
		radioBth = document.getElementById('radio_id_year');
	}
	if( radioBth != null)
	{
		radioBth.checked = true;
	}
}

//sendToPrint
//collect param such as: year, month and hlokidays to collect and send it to
function sendToPrint()
{
	
	var params;
	var add;
	var radio_choice;
	var strSelected;
	var strHolidaysToCollect="";
	
	//find out what was chosen - (year and month) or all Year
	var objRadioOptions;
	var month=0, year=0;
	objRadioOptions = document.getElementsByName('groupDate'); 
	
	if(objRadioOptions != null)
	{
		// Loop from zero to the one minus the number of radio button selections
		for (counter = 0; counter < objRadioOptions.length; counter++)
		{
			if (objRadioOptions[counter].checked)
			{
				strSelected = objRadioOptions[counter].value;
			}
		}
		if(strSelected != null && strSelected=='month')
		{
			//month and year were selected
			//takes values from DDL_m_Month and DDL_m_Year
			obj_ddlMonth = document.getElementById('DDL_m_Month'); 
			obj_ddlYear = document.getElementById('DDL_m_Year'); 
			if(obj_ddlYear != null && obj_ddlMonth != null)
			{
				month = obj_ddlMonth.options[obj_ddlMonth.selectedIndex].value;
				year = obj_ddlYear.options[obj_ddlYear.selectedIndex].value;				
			}
		}
		else if(strSelected != null && strSelected=='year')
		{
			//only month was selected
			//takes values from DDL_y_Month 
			obj_ddlYear = document.getElementById('DDL_y_Year');
			if(obj_ddlYear != null)
			{
				year = obj_ddlYear.options[obj_ddlYear.selectedIndex].value;
			}
		}
		
	

	}
	
	//find out which holidays to collect (strHolidaysToCollect)
	objckb =  document.getElementsByName("ckbHolidays");
	
	if(objckb == null)
	{
		//if there is a problem - collect all holidays- can send null in 'strHolidaysToCollect'
		strHolidaysToCollect = ""
	}
	else
	{
		//collect holidays	
		for (var j = 0; j < objckb.length; j++)
		{			
				if (objckb[j].checked == true) 
				{
					switch (objckb[j].id)
					{
						case "ckb_0": 
							strHolidaysToCollect += "^jewishHag";
							break;
						case "ckb_1": 
						strHolidaysToCollect += "^other";
						break;
						case "ckb_2": 
							strHolidaysToCollect += "^armeniHag";
							break;
						case "ckb_3": 
							strHolidaysToCollect += "^muslemiHag";
							break;
						case "ckb_4": 
							strHolidaysToCollect += "^ortodoksiHag";
							break;
						case "ckb_5": 
							strHolidaysToCollect += "^katolHag";
							break;
						case "ckb_6": 
							strHolidaysToCollect += "^droziHag";
							break;
						default : // include "ckb_7": collect all - can send null
							strHolidaysToCollect = "";
					}
				}
		}
	}
	
	//finished collect holidays		
	
	params = "month=" + month + "&year=" + year + "&strHolidaysToCollect=" + strHolidaysToCollect;	
	add = '/Civil-Service/TopNavHe/ToolsAndInformation/Calendar/printVersion?';
	
	openWin(add + params,'','');
	
}

/******** end PrintMenu ******/


