var openedStructureContainerID = null;
var onBasket = false;

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showInsidePicture(id)
{
      document.getElementById('insidepicture').src = '/picture_w.php?ID='+id;
}

function showProductInsidePicture(id, maxwidth, maxheight)
{
	var strUrl = '/productpicture_w.php?ID='+id;
	if(maxwidth > 0)
	{
	    strUrl += "&maxwidth=" + maxwidth;
	}
	if(maxheight > 0)
	{
	    strUrl += "&maxheight=" + maxheight;
	}
    document.getElementById('product-insidepicture').src = strUrl;
}

function showPicture(content, width, height, link)
{
	if(link != undefined && link != '' && link != null)
	{
		var popup = window.open(link, 'popup', 'location=no,menubar=no,status=no,titlebar=no,toolbar=no');
	}
	else
	{
		var popup = window.open(content, 'popup', 'location=no,menubar=no,status=no,titlebar=no,toolbar=no,width='+width+',height='+height+'');
	}
	popup.focus();
}
var OS,browser,version,total,thestring;		

function doOnLoad()
{
	MM_preloadImages('/images/02-company1.jpg','/images/03-services1.jpg','/images/04-solutions1.jpg','/images/031-services1.jpg','/images/pealeht1.jpg','/images/koolitused1.jpg','/images/teenused1.jpg','/images/kontakt1.jpg','/images/leftmenu_green.gif','/images/leftmenu_grey.gif','/images/leftmenu_violet.gif','/images/leftmenu_blue.gif');
	/*
	var middleContainer = getNode("middle-container");
	var iframe = getNode("iframe");

	if(middleContainer.clientHeight > iframe.height)
	{
		
		iframe.height = middleContainer.clientHeight - 2;
	}
	*/
	/*
	resizeWindow();
    iframeBackground();
	*/
	getCalendar();
}

function getNode(id)
{
	if(document.all)
	{
	    var node = document.all[id];
	}
	else if(document.layers)
	{
	    var node = document.layers[id];
	}
	else
	{
	    var node = document.getElementById(id);
	}
	if(node)
	{
		return node;
	}
	else
	{
		return false;
	}
}

function getParentwindowNode(id)
{
	if(document.all)
	{
	    var node = parent.document.all[id];
	}
	else if(document.layers)
	{
	    var node = parent.document.layers[id];
	}
	else
	{
	    var node = parent.document.getElementById(id);
	}
	if(node)return node;
}

function emptyNode(node)
{
    if(node)
	{
	    while(node.firstChild)
		{
		    node.removeChild(node.firstChild);
		}
	}
}

function checkIt(string)
    {
	var detect = navigator.userAgent.toLowerCase();
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
    } 	
    function iframeBackground()
    {
	var detect = navigator.userAgent.toLowerCase();
	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser = "Safari"
	else if (checkIt('omniweb')) browser = "OmniWeb"
	else if (checkIt('opera')) browser = "Opera"
	else if (checkIt('webtv')) browser = "WebTV";
	else if (checkIt('icab')) browser = "iCab"
	else if (checkIt('msie')) browser = "Internet Explorer"
	else if (!checkIt('compatible'))
        {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
        }
	else browser = "An unknown browser";
	if (!version)version = detect.charAt(place + thestring.length);

	if(!((browser == "Netscape Navigator" && version >= 5) || (browser == "Internet Explorer" && parseInt(version) > 5)))
	{
	    frames['iframe'].document.body.style.background = '#051302';
	}
    }

    function getBodySize()
    {
        var myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' )
        {
            //Non-IE
            myHeight = window.innerHeight;
        }
        else if( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
        {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        }
        else if(document.body && (document.body.clientWidth || document.body.clientHeight))
        {
            //IE 4 compatible
            myHeight = document.body.clientHeight;
        }
        return myHeight;
    }

    function resizeWindow()
    {

        if(document.getElementById)
        {
            var bigcontainer = document.getElementById('bigcontainer');
            var rightmenu = document.getElementById('rightmenu-container');
            var content = document.getElementById('content-container');
            var bodycontainer = document.getElementById('body-container');
            var iframe = document.getElementById('iframe');
			var rightside = document.getElementById('rightside');
			var leftside = document.getElementById('leftside');
        }
        else if(document.layers)
        {
            var bigcontainer = document.layers['bigcontainer'];
            var rightmenu = document.layers['rightmenu-container'];
            var content = document.layers['content-container'];
            var bodycontainer = document.layers['body-container'];
            var iframe = document.layers['iframe'];
			var rightside = document.layers['rightside'];
			var leftside = document.layers['leftside'];
        }
        else if(document.all)
        {
            var bigcontainer = document.all['bigcontainer'];
            var rightmenu = document.all['rightmenu-container'];
            var content = document.all['content-container'];
            var bodycontainer = document.all['body-container'];
            var iframe = document.all['iframe'];
			var rightside = document.all['rightside'];
			var leftside = document.all['leftside'];
        }
	
	if(document.body.scrollHeight)
	{
	    var rightmenu_height = rightmenu.scrollHeight;
	}
	else if(document.body.offsetHeight)
	{
	    var rightmenu_height = rightmenu.offsetHeight;
	}
    var body_height = getBodySize();
	var footer_height = 35;
	var topcontainer_height = 173;

	var new_bodycontainer_height = rightmenu_height + 70;

	if((body_height - topcontainer_height) > new_bodycontainer_height)new_bodycontainer_height = body_height - topcontainer_height; 

    var rightmenu_height = new_bodycontainer_height - footer_height;
    var content_height = new_bodycontainer_height - footer_height;
    var iframe_height = content_height - 70;
    var bodycontainer_height = new_bodycontainer_height;
	var page_height = new_bodycontainer_height + topcontainer_height; 

    bigcontainer.style.height = body_height+'px';
    bodycontainer.style.height = rightmenu_height+'px';
    rightmenu.style.height = rightmenu_height+'px';
    content.style.height = content_height+'px';
    iframe.style.height = iframe_height+'px';
	leftside.style.height = page_height + 'px';
	rightside.style.height = page_height + 'px';
}

function getNodeContentWidth(node)
{
    if(node)
	{
		if(document.body.scrollWidth)
		{
			var width = node.scrollWidth;
		}
		else if(document.body.offsetWidth)
		{
			var width = node.offsetWidth;
		}
		if(width)return width;
	}
}

function getNodeContentHeight(node)
{
    if(node)
	{
		if(document.body.scrollHeight)
		{
			var height = node.scrollHeight;
		}
		else if(document.body.offsetHeight)
		{
			var height = node.offsetHeight;
		}
		if(height)return height;
	}
}

	function emptyNode(obj)
	{
	    while(obj.firstChild)obj.removeChild(obj.firstChild);
	}

	function isNumeric(sText)
	{
	   var ValidChars = "0123456789";
	   var IsNumber=true;
	   var Char;

	 
	   for (i = 0; i < sText.length && IsNumber == true; i++) 
		  { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) 
			 {
			 IsNumber = false;
			 }
		  }
	   return IsNumber;
   }

function refreshPage()
{
	document.location.reload();
}

function showProductPicture(picturesrc, width, height)
{
    // background div
    var nodeBgDiv = document.createElement("div");
    nodeBgDiv.id = 'openedpicturebackground-container';
    document.body.appendChild(nodeBgDiv);
    
    // real div
    var nodeDiv = document.createElement("div");
    nodeDiv.id = 'openedpicture-container';
    nodeDiv.onclick = hideProductPicture;
    
    // image
    var nodeImage = document.createElement("img");
    nodeImage.src = picturesrc;
    nodeImage.height = height;
    nodeImage.width = width;
    nodeImage.onclick = hideProductPicture;
    nodeDiv.appendChild(nodeImage);
    document.body.appendChild(nodeDiv);
    
    // set background geight
    var height = getNodeContentHeight(nodeDiv);
    nodeBgDiv.style.height = height + 'px';

	// disable selects
	var listSelect = document.getElementsByTagName("SELECT");
	if(listSelect.length > 0)
	{
		for(var i = 0; i < listSelect.length; i++)
		{
			listSelect[i].style.visibility = 'hidden';
		}
	}
}

function hideProductPicture()
{
    var nodeDiv = getNode('openedpicture-container');
    if(nodeDiv)
    {
        nodeDiv.parentNode.removeChild(nodeDiv, true);
    }
    
    var nodeBgDiv = getNode('openedpicturebackground-container');
    if(nodeBgDiv)
    {
        nodeBgDiv.parentNode.removeChild(nodeBgDiv, true);
    }

	// enable selects
	var listSelect = document.getElementsByTagName("SELECT");
	if(listSelect.length > 0)
	{
		for(var i = 0; i < listSelect.length; i++)
		{
			listSelect[i].style.visibility = 'visible';
		}
	}
}

function changePage(uri)
{
	document.location.href = uri;
}

var http_request;
function sendAjaxRequest(url)
{
	http_request = false;

	if(window.ActiveXObject) // IE
	{
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){}
		}
	}
	else if(window.XMLHttpRequest) // Mozilla and Safari
	{
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType)http_request.overrideMimeType('text/xml');
	}

	if(!http_request)
	{
		alert('Ei suuda initsialiseerida XMLHttpRequest -i');
		return false;
	}

	http_request.onreadystatechange = ajaxRequestReturnBg;

	http_request.open('GET', url, true);
	http_request.send(null);
}

function ajaxRequestReturnBg()
{
	if(http_request.readyState == 4)
	{
		if(http_request.status == 200)
		{
			var vastusString = http_request.responseText;
			//var responseJson = vastusString.parseJSON();
			
			ajaxRequestReturn(vastusString);
		}
	}
}

function ajaxRequestReturn(strResponse)
{
	getNode("calendar").innerHTML = strResponse;
}

function getCalendar()
{
	sendAjaxRequest("/getCalendar.php");
}

function changeDate(strDate)
{
	sendAjaxRequest("/getCalendar.php?date=" + strDate);
}
