function showNext(containerId,upArrowId,downArrowId)
{
	firstShowDiv=true;     
	container = document.getElementById(containerId);   
	// hide last month
	for (i=0;i<(container.childNodes).length;i++)
	{
	   if ((container.childNodes[i].nodeType == 1) && (container.childNodes[i].style.display != 'none'))
	   {
	   	 	if (firstShowDiv) 
	   	 	{
	   	 	    firstShowDivValue = i;
	   	 	    firstShowDiv = false;
			}
			lastShowDiv = i;
	   }
	}  	
	container.childNodes[lastShowDiv].style.display = 'none';
	// display first month
	for (j=0;j<firstShowDivValue;j++)
	{
	   if ((container.childNodes[j].nodeType == 1) && (container.childNodes[j].style.display == 'none'))
	   {                        
	   		lastNonShowDivValue=j;                  
	   	 	// Get href
	   	 	href = escape(container.childNodes[j].getElementsByTagName('A')[0].href);
	   }
	}
	container.childNodes[lastNonShowDivValue].style.display = 'block';  
	document.getElementById(downArrowId).style.display = 'block';
	findNextMonth(containerId,lastNonShowDivValue,upArrowId,downArrowId, href);
	returnPosition();
}
function showPrevious(containerId,upArrowId,downArrowId)
{                             
	firstShowDiv=true;
	firstNonShowDiv=true;
	container = document.getElementById(containerId);   
	// hide first month
	for (i=0;i<(container.childNodes).length;i++)
	{
	   if ((container.childNodes[i].nodeType == 1) && (container.childNodes[i].style.display != 'none'))
	   {
	   	 	if (firstShowDiv) 
	   	 	{
	   	 	    container.childNodes[i].style.display = 'none';
	   	 	    firstShowDiv = false;
			}
			lastShowDiv = i;
	   }
	}  	
	// display previous month
	for (j=lastShowDiv;j<(container.childNodes).length;j++)
	{
	   if ((container.childNodes[j].nodeType == 1) && (container.childNodes[j].style.display == 'none'))
	   {
	   	 	if (firstNonShowDiv) 
	   	 	{
	   	 	    container.childNodes[j].style.display = 'block';
	   	 	    firstNonShowDiv = false;
	   	 	    firstNonShowDivValue = j;
	   	 	    // Get href
	   	 	    href = escape(container.childNodes[j].getElementsByTagName('A')[0].href);
			}                  			                
	   }
	}
	document.getElementById(upArrowId).style.display = 'block';
	findPreviousMonth(containerId,firstNonShowDivValue,upArrowId,downArrowId, href);
	returnPosition();
}

function findPreviousMonth(containerId,firstNonShowDivValue,upArrowId,downArrowId, href)
{
	previousMonthExist = false ;
	container = document.getElementById(containerId); 
	for (j=firstNonShowDivValue;j<(container.childNodes).length;j++)
	{
	   if ((container.childNodes[j].nodeType == 1) && (container.childNodes[j].style.display == 'none'))
	   {
	   		previousMonthExist = true ;
	   }
	}
	if (!previousMonthExist)
	{
		document.getElementById(downArrowId).style.display = 'none';
		ajaxRequest('/submit.php', 'ajax=1&command=diaryCalendarGetMonth' + '&type=2' + '&href=' + href + '&containerId='+containerId+'&upArrowId='+upArrowId+'&downArrowId='+downArrowId, 'previousMonthAjaxResponse', false);  
	}
}

function findNextMonth(containerId,lastNonShowDivValue,upArrowId,downArrowId, href)
{
	nextMonthExist = false ;       
	container = document.getElementById(containerId); 
	for (j=0;j<lastNonShowDivValue;j++)
	{
	   if ((container.childNodes[j].nodeType == 1) && (container.childNodes[j].style.display == 'none'))
	   {
	   		nextMonthExist = true ;
	   }
	}  	
	if (!nextMonthExist)
	{
		document.getElementById(upArrowId).style.display = 'none';  
		ajaxRequest('/submit.php', 'ajax=1&command=diaryCalendarGetMonth' + '&type=1' + '&href=' + href + '&containerId='+containerId+'&upArrowId='+upArrowId+'&downArrowId='+downArrowId, 'nextMonthAjaxResponse', false);
	}
}

function previousMonthAjaxResponse(xmlDoc){
	linkValue = xmlDoc.getElementsByTagName('linkValue')[0];
	if (linkValue)
	{
		// Get required params
		containerId = xmlDoc.getElementsByTagName('containerId')[0].firstChild.nodeValue;
		linkText = xmlDoc.getElementsByTagName('linkText')[0].firstChild.nodeValue;
		linkValue = unescape(xmlDoc.getElementsByTagName('linkValue')[0].firstChild.nodeValue);
		downArrowId = xmlDoc.getElementsByTagName('downArrowId')[0].firstChild.nodeValue;
		// Activate
		appendNewChild(containerId, linkValue, linkText);	
		document.getElementById(downArrowId).style.display = 'block';
	}
}
function nextMonthAjaxResponse(xmlDoc){	
	linkValue = xmlDoc.getElementsByTagName('linkValue')[0];
	if (linkValue)
	{
		// Get required params
		containerId = xmlDoc.getElementsByTagName('containerId')[0].firstChild.nodeValue;
		linkText = xmlDoc.getElementsByTagName('linkText')[0].firstChild.nodeValue;
		linkValue = unescape(xmlDoc.getElementsByTagName('linkValue')[0].firstChild.nodeValue);
		upArrowId = xmlDoc.getElementsByTagName('upArrowId')[0].firstChild.nodeValue;
		// Activate
		insertNewChild (containerId, linkValue, linkText);
		document.getElementById(upArrowId).style.display = 'block';  
	}
}
function appendNewChild (containerId, linkValue, linkText)   
{    
	container = document.getElementById(containerId);              	
	newNode = document.createElement ('div');
	newNode.style.display = "none";
	newNode.setAttribute ('id', 'new_div');                                  
	container.appendChild(newNode);
	link = document.createElement ('a');
	link.setAttribute('href', linkValue);
	link.setAttribute ('id', 'new_link');
	document.getElementById ('new_div').appendChild(link); 
	document.getElementById ('new_link').innerHTML = linkText;
	document.getElementById ('new_div').id = '';
	document.getElementById ('new_link').id = '';
}
function insertNewChild (containerId, linkValue, linkText)
{
	container = document.getElementById(containerId);              	
	newNode = document.createElement ('div');
	newNode.style.display = "none";
	newNode.setAttribute ('id', 'new_div');                                  
	container.insertBefore(newNode,container.firstChild);
	link = document.createElement ('a');
	link.setAttribute('href', linkValue);
	link.setAttribute ('id', 'new_link');
	document.getElementById ('new_div').appendChild(link); 
	document.getElementById ('new_link').innerHTML = linkText;
	document.getElementById ('new_div').id = '';
	document.getElementById ('new_link').id = '';
}
function returnPosition(){
	navRoot = document.getElementById('calendarMonthContainer');   
    var vectorLI=navRoot.getElementsByTagName('a');  
    position = 1;
    for (i=0; i<vectorLI.length; i++)  
    {                
        node = vectorLI[i];   
        if  (node.parentNode.style.display != 'none')
        {
        	node.setAttribute ('name',position);   
        	position ++; 
		}
        node.onclick=function(){                      
        	 this.href=this.href+"&datePos="+this.name;  
		}           
	}                  
}

window.onload=returnPosition;