﻿var PointerCurrentArticle=true
var xmlHttp,xmlHttpEventCal
var CurrentScrollingEventCalendar=0
var CrSdECCou
var TimerCal
var StrHTMLInfo

try //Internet Explorer
  {
	XmlDocEventCalendar = new ActiveXObject("MSXML.DOMDocument")
	XmlDocTitles= new ActiveXObject("MSXML.DOMDocument")
	XmlArticleInfo= new ActiveXObject("MSXML.DOMDocument")
  }
catch(e)
{
	try //Firefox, Mozilla, Opera, etc.
	{
		XmlDocEventCalendar=document.implementation.createDocument("","",null);
		XmlDocTitles=document.implementation.createDocument("","",null);
		XmlArticleInfo=document.implementation.createDocument("","",null);
	}
	catch(e) {alert(e.message)}
}

XmlDocEventCalendar.async = false
XmlDocTitles.async = false
XmlArticleInfo.async = false


function toDDMMYYYYString(Day,Month,Year,Revers) 
{
	if (Revers!=null && Revers!=undefined)
	{
		return [eval(Month + 1) > 9 ? eval(Month + 1) : '0' + eval(Month + 1),Day > 9 ? Day : '0' + Day,Year].join('/')
	}
	else
	{
		return [Day > 9 ? Day : '0' + Day,eval(Month + 1) > 9 ? eval(Month + 1) : '0' + eval(Month + 1),Year].join('/')
	}
}

function GetArticlesTitles(XmlDocTitles)
{
	var Str='<table border="0" id="tableEventCalendarContent" cellpadding="0" cellspacing="0">'
	for (I=1;I<=XmlDocTitles.length;I++)
	{
		Str+='<tr><td width="1%" nowrap="true" valign="top">'+ConvertToArabicNum(I)+'- &nbsp;</td><td align="right">'+XmlDocTitles[I-1][TextNodeDefine]+'</td></tr>'
	}
	Str+='</table>'
	return Str
}

//## This function Convert Any English NUMBER To Arabic NUMBER
 function ConvertToArabicNum(Num){
   
	  /* 
	     Private properties 
	     ----------------------------------------------------------------*/
	  //Created By : Moayad Al-Saleh
	  //List Style Type
	  //Three styles: numeric
	  this.list_style_type="numeric";
	  
	  //Arabic Digits
	  this.ar_digits = new Array();
      this.ar_digits[0]="&#1632;";
      this.ar_digits[1]="&#1633;";
      this.ar_digits[2]="&#1634;";
      this.ar_digits[3]="&#1635;";
      this.ar_digits[4]="&#1636;";
      this.ar_digits[5]="&#1637;";
      this.ar_digits[6]="&#1638;";
      this.ar_digits[7]="&#1639;";
      this.ar_digits[8]="&#1640;";
      this.ar_digits[9]="&#1641;";
	  
	     Num = Num.toString();
		 translated_n = "";
		 var Length=Num.length
		 if(Num <= 9)
		 translated_n=this.ar_digits[Num];
		 else
		 for(i=1; i<=Length; i++)
		{ translated_n = translated_n + this.ar_digits[Num.substring(0,i)]; 
		 Num=Num.substring(i)
		 }
         return translated_n.toString();		  
  } //end of class
  
function HighLightDayDates(Obj)
{
	var ICounter =document.getElementsByName('tdCalendarDayDate').length
	for (I=0;I<ICounter;I++)
	{
		document.getElementsByName('tdCalendarDayDate')[I].className='active'
	}
	Obj.className='calBlueBg'
}

//## Added By Moayad AL-Saleh
//## Get Event Calendar By Date
//## Begin

// Get Article Calendar Information 
function GetArticleCalendarInfoEventCal(Year,Month)
{	
	
	xmlHttpEventCal=GetXmlHttpObjectEventCal();
	if (xmlHttpEventCal==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/Studies/Aspx/GetEventCalendarByDate.aspx";
	url=url+"?Year="+Year;
	url=url+"&Month="+eval(Month+1);
	url=url+"&Rand="+Math.random();
	xmlHttpEventCal.onreadystatechange=stateChangedEventCal;
	xmlHttpEventCal.open("GET",url,true);
	xmlHttpEventCal.send(null);
}
function stateChangedEventCal() 
{ 
	if (xmlHttpEventCal.readyState==4)
	{ 
		try //Internet Explorer
		{
			XmlDocEventCalendar.loadXML(xmlHttpEventCal.responseText);
		}
		catch(e)
		{
			try //Firefox, Mozilla, Opera, etc.
			{
				parser=new DOMParser();
				XmlDocEventCalendar=parser.parseFromString(xmlHttpEventCal.responseText,"text/xml");
			}
			catch(e) 
			{
				alert(e.message)
			}
		}
		FillAllEventCalendarAndArticles()
	}
}
function GetXmlHttpObjectEventCal()
{
	var xmlHttpEventCal=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttpEventCal=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttpEventCal=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttpEventCal=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttpEventCal;
}



//## End


// Get Article Calendar Information 
function GetArticleCalendarInfo(Guid,Obj)
{	
	if (Obj!=undefined)
		HighLightDayDates(Obj)
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/Studies/Aspx/GetEventCalendarArticle.aspx";
	url=url+"?Guid="+Guid;
	url=url+"&Rand="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		var Str=''
		var ICounter;
		try //Internet Explorer
		{
			XmlArticleInfo.loadXML(xmlHttp.responseText)
		}
		catch(e)
		{
			try //Firefox, Mozilla, Opera, etc.
			{
				parser=new DOMParser();
				XmlArticleInfo=parser.parseFromString(xmlHttp.responseText,"text/xml");
			}
			catch(e) 
			{
				alert(e.message)
			}
		}
		ICounter=XmlArticleInfo.selectNodes("//Root/Items/Item").length
		CrSdECCou=ICounter
		Str+='<TABLE cellSpacing="0" cellPadding="0" border="0" id="tblEventCalendarArticles"><tr>'
		for (I=0;I<ICounter;I++)
		{
			Str+='<td valign="top">'
			Str+='<TABLE cellSpacing="0" cellPadding="0" width="142" border="0">'
			Str+='<TR>'
			Str+='<TD width="1"><IMG height="62" src="/Studies/Images/imgLeftBrd.jpg" width="1"></TD>'
			Str+='<TD><IMG class="calImgBord" height="60" src="'+XmlArticleInfo.selectNodes("//Root/Items/Item/ImagePath")[I][TextNodeDefine]+'" width="140" id="imgArticleImageCalendar"></TD>'
			Str+='<TD width="1"><IMG height="62" src="/Studies/Images/imgLeftBrd.jpg" width="1"></TD>'
			Str+='</TR>'
			Str+='</TABLE>'
			Str+='<TABLE class="searchLink" id="Table4" cellSpacing="0" cellPadding="0" width="140" align="right" border="0">'
			Str+='<TR>'
			Str+='<TD dir="rtl" style="PADDING-TOP: 5px;padding-right:5px" vAlign="top" align="right" width="112">'
			//Str+='<A href="'+XmlArticleInfo.selectNodes("//Root/Items/Item/Guid")[I][TextNodeDefine].replace('{','').replace('}','')+'" id="hrefArticleTitleCalendar">'
			Str+=XmlArticleInfo.selectNodes("//Root/Items/Item/HeadLine")[I][TextNodeDefine]
			//Str+='</A>'
			Str+='</TD></TR>'
			Str+='</TABLE>'
			Str+='</td>'
			if (I<ICounter-1)
				Str+='<td style="padding-right:5px"></td>'
			else
				Str+='<td style="padding-right:3px"></td>'
		}
		Str+='</tr></TABLE>'
		if (ICounter==1)
			document.getElementById('tblScrollingEventCalendar').style.display='none'
		else
			document.getElementById('tblScrollingEventCalendar').style.display=''
			
			document.getElementById('divEventCalendarArticlesInfo').innerHTML='<table border="0" width="100%" height="100%" cellpadding="0" cellspacing="0"><tr><td align="center"><img src="/Studies/Images/LoadingCalendar.gif"</td></tr></table>'
			StrHTMLInfo=Str
			window.setTimeout("FillDivEventCalendarData()",2000)
	}
}
function FillDivEventCalendarData()
{
	document.getElementById('divEventCalendarArticlesInfo').innerHTML=StrHTMLInfo
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			mlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function ScrollingEventCalendarRight(Obj)
{	
	if (CurrentScrollingEventCalendar<CrSdECCou-1)
	{
		CurrentScrollingEventCalendar++
		window.clearTimeout(TimerCal)
		ScrollingDivToRight(Obj,document.getElementById(Obj).scrollLeft,CurrentScrollingEventCalendar*147)
	}
}

function ScrollingDivToRight(Obj,RightSize,EndSize)
{
	if (document.getElementById(Obj).scrollLeft<EndSize)
	{
		document.getElementById('LeftScrollingCalendarArticles').src='/Studies/Images/leftArrow.jpg';
		document.getElementById('LeftScrollingCalendarArticles').style.cursor='pointer'
		document.getElementById(Obj).scrollLeft=RightSize
		TimerCal=window.setTimeout("ScrollingDivToRight('"+Obj+"',"+eval(eval(RightSize)+eval(4))+","+EndSize+")",1)
	}
	else
	{
		window.clearTimeout(TimerCal)
		if (CurrentScrollingEventCalendar==eval(CrSdECCou-1))
		{
			document.getElementById('RightScrollingCalendarArticles').src='/Studies/Images/riteArrow-D.jpg';
			document.getElementById('RightScrollingCalendarArticles').style.cursor='default'
		}
	}
}

function ScrollingEventCalendarLeft(Obj)
{	
	if (CurrentScrollingEventCalendar>0)
	{
		CurrentScrollingEventCalendar--
		window.clearTimeout(TimerCal)
		ScrollingDivToLeft(Obj,document.getElementById(Obj).scrollLeft,CurrentScrollingEventCalendar*147)
	}
	
}
function ScrollingDivToLeft(Obj,LeftSize,EndSize)
{
	if (document.getElementById(Obj).scrollLeft>EndSize)
	{
		document.getElementById('RightScrollingCalendarArticles').src='/Studies/Images/riteArrow.jpg';
		document.getElementById('RightScrollingCalendarArticles').style.cursor='pointer'
		document.getElementById(Obj).scrollLeft=LeftSize
		TimerCal=window.setTimeout("ScrollingDivToLeft('"+Obj+"',"+eval(eval(LeftSize)-eval(4))+","+EndSize+")",1)
	}
	else
	{
		window.clearTimeout(TimerCal)
		if (CurrentScrollingEventCalendar==0)
		{
			document.getElementById('LeftScrollingCalendarArticles').src='/Studies/Images/leftArrow-D.jpg';
			document.getElementById('LeftScrollingCalendarArticles').style.cursor='default'
		}
	}
}
function FillArticleInfoCalendar(DateEvent,Obj)
{
	document.getElementById('divEventCalendarArticlesInfo').scrollLeft=0
	document.getElementById('RightScrollingCalendarArticles').src='/Studies/Images/riteArrow.jpg';
	document.getElementById('RightScrollingCalendarArticles').style.cursor='pointer'
	document.getElementById('LeftScrollingCalendarArticles').src='/Studies/Images/leftArrow-D.jpg';
	document.getElementById('LeftScrollingCalendarArticles').style.cursor='default'
	
	CurrentScrollingEventCalendar=0
	var Counter
	var ArrGuids=''
	Counter=XmlDocEventCalendar.selectNodes("//Root/EventsCalendar/Event[EventDate='"+DateEvent+"']/Guid").length
	for(I=0;I<Counter;I++)
	{
		ArrGuids+=XmlDocEventCalendar.selectNodes("//Root/EventsCalendar/Event[EventDate='"+DateEvent+"']/Guid")[I][TextNodeDefine]+','
	}
	
	ArrGuids=ArrGuids.substring(0,ArrGuids.lastIndexOf(','))
	GetArticleCalendarInfo(ArrGuids,Obj)
}

function RendaringCurrentDate(Month,Year)
{
		GetArticleCalendarInfoEventCal(Year,Month)
}

function FillAllEventCalendarAndArticles(Month,Year)
{
		PointerCurrentArticle=true
		var month_num
		// Set varible to the current date
		
		month_num=CurrentMonth
		
		// set varible to the current day value (1-31)
		var HTMLCalendar='';
		// create an array for the month name
		var month_name = new Array ("January ","February ","March ","April ","May ","June ","July ","August ","September ","October ","November ","December ");
		// Create a varible right_year with the current year
		var right_year=CurrentYear;
		var theday = 0;
		
		
		//Fill Header Event Calendar
		document.getElementById('lblMonth').innerHTML=month_name[CurrentMonth]
		document.getElementById('lblYear').innerHTML=CurrentYear;
		
		if (right_year < 2000) 
			right_year = right_year + 1900; 
		// create a varible to specify what the
		// last day for the current month is

		if (month_num == 0 || month_num == 2 || month_num == 4 || month_num == 6 || month_num == 7 || month_num == 9 || month_num == 11)
		{ 
			endofmonth=31;
		}
		if (month_num == 3 || month_num == 5 || month_num == 8 || month_num == 10)
		{ 
			endofmonth=30;
		}

		if (month_num == 1)
		{ 
			// This will check for a leap year
			// If the year is evenly divisible by four
			// or in the case of a new century evenly divisible
			// by 400 then the end of the February month should be the 29th

			right_year_divided=right_year/4;
			right_year_divided_string= new String(right_year_divided);
			var is_decimal = right_year_divided_string.indexOf('.');
			if (is_decimal != -1)
			{ 
				endofmonth=28; 
			}
			else
			{ 
				endofmonth=29; 
			}

			right_year_string= new String(right_year);
			var the_century=new String(right_year_string.charAt(2)) 
			the_century= the_century + new String(right_year_string.charAt(3));
			if (the_century == "00")
			{ 
				right_year_divided=right_year/400;
				right_year_divided_string= new String(right_year_divided);
				var is_decimal = right_year_divided_string.indexOf('.');
				if (is_decimal != -1)
				{
					endofmonth=28;
				}
				else
				{
					endofmonth=29;
				}
			}
		}

		// Start building the table
		HTMLCalendar+="<table cellSpacing='2' cellPadding='0' width='228' border='0'>";
		// Write the table header row
		HTMLCalendar+="<tr>";

		// Figure out which day of the week the 1st of the 
		// current month belongs to
		first_day = new Date(right_year,month_num,1)

		// Write the first row in the calendar with dates
		// Check which date of the month is the first to
		// fill it into the appropriate day of the week
		for (counter = 0; counter < 7; counter++)
		{ 

				// Check the counter aganst the first day of the month value (0 - 6)

				if (counter >= first_day.getDay() ) 
				{ 

					// Start counter for the calendar daysl
					theday=theday+1;
					// Check if the current day is in the first week if so, place bold text
					if (XmlDocEventCalendar.selectNodes("//Root/EventsCalendar/Event[EventDate='"+toDDMMYYYYString(theday,month_num,right_year)+"']/EventDate")[0]!=null) 
					{
						if (toDDMMYYYYString(theday,month_num,right_year)>=toDDMMYYYYString(new Date().getDate(),new Date().getMonth(),new Date().getFullYear()) && PointerCurrentArticle)
						{
							PointerCurrentArticle=false;
							HTMLCalendar+="<td class='calBlueBg' align='center' onmouseover='showToolTip(this,\""+toDDMMYYYYString(theday,month_num,right_year)+"\")' EventDate='"+toDDMMYYYYString(theday,month_num,right_year)+"' onmouseout='hideToolTip()' onclick='FillArticleInfoCalendar(\""+toDDMMYYYYString(theday,month_num,right_year)+"\",this)' height='22' id='tdCalendarDayDate' name='tdCalendarDayDate'>" + theday + "</td>";
							FillArticleInfoCalendar(toDDMMYYYYString(theday,month_num,right_year))
						}
						else
							HTMLCalendar+="<td class='active' align='center' onmouseover='showToolTip(this,\""+toDDMMYYYYString(theday,month_num,right_year)+"\")' EventDate='"+toDDMMYYYYString(theday,month_num,right_year)+"' onmouseout='hideToolTip()' id='tdCalendarDayDate' name='tdCalendarDayDate' onclick='FillArticleInfoCalendar(\""+toDDMMYYYYString(theday,month_num,right_year)+"\",this)' height='22'>" + theday + "</td>";
					}
					else 
					// If it's not the current date output without bold text	
					{
						HTMLCalendar+="<td class='inactive' align='center' height='22'>" + theday + "</td>";
					}
				} 
				else 
				// if there is no day yet output an empty cell
				{
					HTMLCalendar+="<td></td>"; 
				}
		}
		
		// End row for the first week of the month
		HTMLCalendar+="</tr>"; 

		// Loop for the rest fo the weeks in the month 
		for (weeks = 0; weeks < 5; weeks++)
		{ 
			HTMLCalendar+="<tr>"; 
			// loop for the days with the remaining weeks
			for (week = 0; week < 7; week++)
			{

				// counter for the day of the month
				theday=theday+1
				// if the couter = the current date display in bold 
				if (XmlDocEventCalendar.selectNodes("//Root/EventsCalendar/Event[EventDate='"+toDDMMYYYYString(theday,month_num,right_year)+"']/EventDate")[0]!=null) 
				{
					if (toDDMMYYYYString(theday,month_num,right_year)>=toDDMMYYYYString(new Date().getDate(),new Date().getMonth(),new Date().getFullYear()) && PointerCurrentArticle)
					{
						PointerCurrentArticle=false;
						HTMLCalendar+="<td class='calBlueBg' align='center' id='tdCalendarDayDate' onmouseover='showToolTip(this,\""+toDDMMYYYYString(theday,month_num,right_year)+"\")' EventDate='"+toDDMMYYYYString(theday,month_num,right_year)+"' onmouseout='hideToolTip()' name='tdCalendarDayDate' onclick='FillArticleInfoCalendar(\""+toDDMMYYYYString(theday,month_num,right_year)+"\",this)' height='22'>" + theday + "</td>";
						FillArticleInfoCalendar(toDDMMYYYYString(theday,month_num,right_year))
					}
					else
					{
						HTMLCalendar+="<td class='active' align='center' onmouseover='showToolTip(this,\""+toDDMMYYYYString(theday,month_num,right_year)+"\")' onmouseout='hideToolTip()' EventDate='"+toDDMMYYYYString(theday,month_num,right_year)+"' name='tdCalendarDayDate' id='tdCalendarDayDate' onclick='FillArticleInfoCalendar(\""+toDDMMYYYYString(theday,month_num,right_year)+"\",this)' height='22'>" + theday + "</td>";
					}					
				}
				else 
				{ 

					// if the counter is higher then than the number of days
					// in the month then display a blank cell
					if (theday > endofmonth) 
					{
						HTMLCalendar+="<td></td>";
					}
					else 
					// If it's not the cureent day display the date wioth bold type
					{
						HTMLCalendar+="<td class='inactive' align='center' height='22'>" + theday + "</td>"; 
					}
				}
			}
			HTMLCalendar+="</tr>";
		}
	
	HTMLCalendar+="</table>";
	document.getElementById('tdCalendar').innerHTML=HTMLCalendar
}

