var info = new Array(); 

var req;

function callPage(pageUrl, divElementId, pageErrorMessage) {
     try {
     req = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(e) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
       } catch (e) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
         } catch (E) {
          req = false;
         } 
       } 
     }
     req.onreadystatechange = function() {responsefromServer(divElementId, pageErrorMessage);};
     req.open("GET",pageUrl,true);
     req.send(null);
  }
 
function submitform()
{
  document.msg_form.submit();
}

function submitform_search()
{
  document.search.submit();
}

function responsefromServer(divElementId, pageErrorMessage) {
   var output = '';
   if(req.readyState == 4) {
      if(req.status == 200) {
         output = req.responseText;
         document.getElementById(divElementId).innerHTML = parseScript(output);
         } else {
         document.getElementById(divElementId).innerHTML = pageErrorMessage+"\n"+output;
         }
      }
  }
 
function parseScript(_source) {
		var source = _source;
		var scripts = new Array();
 
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
 
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
 
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(ex) {
				// do what you want here when a script fails
			}
		}
		// Return the cleaned source
		return source;
}



	function loadXML(url,id) {
	  if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	  }
	  if (x) {
		x.onreadystatechange = function() {
		  if (x.readyState == 4 && x.status == 200) {
			el = document.getElementById(id);
			output = x.responseText;
			el.innerHTML = output;
		  }
		}
		x.open("GET", url, true);
		x.send(null);
	  }
	}

	function loadNews(day,month,year,lang){
		loadXML('loadnews.php?lang='+lang+'&day='+day+'&month='+month+'&year='+year,'news');
	}

	function loadContent(item,lang){
		loadXML('loadcontent.php?lang='+lang+'&cat=1&item='+item,'content');
	}

	function loadGallery(item,pic,lang,vector){
		loadXML('loadgallery.php?vector='+vector+'&pic='+pic+'&lang='+lang+'&item='+item,'content');
	}

	function db_insert_resolution(resolution,div){
		loadXML('resolution.php?resolution='+resolution,div);
	}

	function checkInArray(a)
	{
	  var o = {};
	  for(var i=0;i<a.length;i++)
	  {
		o[a[i]]='';
	  }
	  return o;
	}


	function ChangeMonth(value)
	{
		aMonth=document.getElementById('month').innerHTML;
		aYear=document.getElementById('year').innerHTML;

		for (i in myMonths) { 
			if (myMonths[i]==aMonth)
			{	
				nextValue=parseInt(i)+value;
			}
		};

		elMonth=document.getElementById('month');
		elYear=document.getElementById('year');

		var year = parseInt(elYear.innerHTML);
		if (nextValue == -1)
		{
			nextValue=11;
			year = year - 1;
		}

		if (nextValue == 12)
		{
			nextValue=0;
			year = year + 1;
		}

		elYear.innerHTML = year;

		newDate = new Date(year, nextValue, 1);

		elMonth.innerHTML = myMonths[nextValue];

		firstDay = newDate.getDay() + 1;

		days = getMonthLength(nextValue, year);


		beforeValue = nextValue-1;
		var byear = parseInt(elYear.innerHTML);
		if (beforeValue == -1)
		{
			beforeValue=11;
			byear = byear - 1;
		}

		if (beforeValue == 12)
		{
			beforeValue=0;
			byear = byear + 1;
		}

		newDate = new Date(byear, beforeValue, 1);
		bdays = getMonthLength(beforeValue, byear);



		activeDays = document.getElementById('active_days');

		days_string = activeDays.innerHTML;
	
		var days_arrayT=days_string.split("T");
		if(days_arrayT.length>1)
		{
			if (value<0)
			{
				var days_array=days_arrayT[0].split(",");
			}
			else
				var days_array=days_arrayT[1].split(",");
		}

		callPage('active_days.php?month='+(nextValue)+'&year='+year,'active_days','2');

		day = 1;
		todays=parseInt(days)+firstDay - 1;

		endrow=document.getElementById('calrow_5');
		if (todays>35)
		{
			endrow.style.visibility='visible';
		}
		else
		{
			endrow.style.visibility='hidden';
		}

		for (d = firstDay;d<=todays;d++)
		{
			elDay=document.getElementById('day_' + d);

			if (day in checkInArray(days_array))
			{
				elDay.innerHTML = '<b>'+day+'</b>';
				elDay.style.color = '#895b93';
				elNews=document.getElementById('loadnews_' + d);
				elNews.href = 'javascript:loadNews('+day+','+(nextValue+1)+','+year+',"'+window.lang+'");';
			}
			else
			{
				elDay.innerHTML = day;
				elDay.style.color = '#5b5b5b';
			}

			cell=document.getElementById('cellday_'+d);
			cell.style.backgroundImage = "url(cal_datework.jpg)";

			day = day + 1;
		};

		bdays = bdays - firstDay + 2;
		for (d = 1;d<firstDay;d++)
		{
			elDay=document.getElementById('day_' + d);
			elDay.innerHTML = bdays;
			cell=document.getElementById('cellday_'+d);
			cell.style.backgroundImage = "url(cal_datefree.jpg)";
			elNews=document.getElementById('loadnews_' + d);
			elNews.removeAttribute("href");
			bdays = bdays + 1;
		};

		fromdays = todays + 1;

		nd = 1;
		for (d = fromdays;d<=42;d++)
		{
			elDay=document.getElementById('day_' + d);
			elDay.innerHTML = nd;
			cell=document.getElementById('cellday_'+d);
			cell.style.backgroundImage = "url(cal_datefree.jpg)";
			elNews=document.getElementById('loadnews_' + d);
			elNews.removeAttribute("href");
			nd = nd + 1;
		};

	}

	function checkleapyear(datea)
	{
		   datea = parseInt(datea);
			if(datea%4 == 0)
			{
					if(datea%100 != 0)
					{
							return true;
					}
					else
					{
							if(datea%400 == 0)
									return true;
							else
									return false;
					}
			}
		return false;
	}





   function getMonthLength(month,year){
      switch(month){
         case 1:
            if (checkleapyear(year))
               return 29;
            else
               return 28;
         case 3:
            return 30;
         case 5:
            return 30;
         case 8:
            return 30;
         case 10:
            return 30
         default:
            return 31;
      }
   }



function rgbConvert(str) {
   str = str.replace(/rgb\(|\)/g, "").split(",");
   str[0] = parseInt(str[0], 10).toString(16).toLowerCase();
   str[1] = parseInt(str[1], 10).toString(16).toLowerCase();
   str[2] = parseInt(str[2], 10).toString(16).toLowerCase();
   str[0] = (str[0].length == 1) ? '0' + str[0] : str[0];
   str[1] = (str[1].length == 1) ? '0' + str[1] : str[1];
   str[2] = (str[2].length == 1) ? '0' + str[2] : str[2];
   return ('#' + str.join(""));
}

function roll_over(el,img_src)
   {

	menu=document.getElementById('menuinfo');
	if (img_src=='item2')
	{
		menu.innerHTML = info[el];
	}
	else
	{
		menu.innerHTML = '';
	}

	cell=document.getElementById('menu_middle_'+el);
	cell.style.backgroundImage = "url("+img_src+"_middle.jpg)";

	//color = cell.style.color;
	//if (color.length>7)
	//{
	//	color = rgbConvert(cell.style.color);
	//}

	//if (color == '#5b5b5b')
	//{
	//	cell.style.color = '#895b93';
	//}
	//else
	//{
	//	cell.style.color = '#5b5b5b';
	//}

	cell=document.getElementById('menu_left_'+el);
	cell.style.backgroundImage = "url("+img_src+"_left.jpg)";
	
	cell=document.getElementById('menu_right_'+el);
	cell.style.backgroundImage = "url("+img_src+"_right.jpg)";

	cell=document.getElementById('submenu_'+el);

	if (cell.style.visibility == 'visible')
	{
		cell.style.visibility = 'hidden';
	}
	else
	{
		cell.style.visibility = 'visible';
	}

   }

function gal_left(){
	cell=document.getElementById('gal_left');

	if (cell.style.visibility == 'visible')
	{
		cell.style.visibility = 'hidden';
	}
	else
	{
		cell.style.visibility = 'visible';
	}
}

function gal_right(){
	cell=document.getElementById('gal_right');

	if (cell.style.visibility == 'visible')
	{
		cell.style.visibility = 'hidden';
	}
	else
	{
		cell.style.visibility = 'visible';
	}
}

function roll_over_cal(el,action)
   {
		cell=document.getElementById('cellday_'+el);

		if (cell.style.backgroundImage=='url(cal_datefree.jpg)' || cell.style.backgroundImage=='url(cal_adatefree.jpg)')
		{
			cell.style.backgroundImage = "url(cal_"+action+"datefree.jpg)";	
		}
		else
		{
			cell.style.backgroundImage = "url(cal_"+action+"datework.jpg)";
		}
   }

function change_image(el,img_src)
   {
		image=document.getElementById(el);
		image.src = img_src+".jpg";
   }

function setbottom(){
	d = document.body.offsetHeight;
	e = window.screenTop;
	f = window.outerHeight;
	g = document.body.scrollHeight;
	h = document.body.clientHeight;
	i = window.innerHeight;
	j = screen.height;

	r = g - 20;

	document.getElementById('footer').style.top=r;

}

function scrollEvent() {
   setbottom();
}
