function sack(file){
	this.AjaxFailedAlert = "Your browser does not support the enhanced functionality of this website, and therefore you will have an experience that differs from the intended one.\n";
	this.requestFile = file;
	this.method = "POST";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;

	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp){
			this.failed = true; 
		}
	};
	
	this.setVar = function(name, value){
		if (this.URLString.length < 3){
			this.URLString = name + "=" + value;
		} else {
			this.URLString += "&" + name + "=" + value;
		}
	}
	
	this.encVar = function(name, value){
		var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
	return varString;
	}
	
	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.encVar(urlVars[0],urlVars[1]);
		}
	return varArray.join('&');
	}
	
	this.runResponse = function(){
		eval(this.response);
	}
	
	this.runAJAX = function(urlstring){
		this.responseStatus = new Array(2);
		if(this.failed && this.AjaxFailedAlert){ 
			alert(this.AjaxFailedAlert); 
		} else {
			if (urlstring){ 
				if (this.URLString.length){
					this.URLString = this.URLString + "&" + urlstring; 
				} else {
					this.URLString = urlstring; 
				}
			}
			if (this.encodeURIString){
				var timeval = new Date().getTime(); 
				this.URLString = this.encodeURLString(this.URLString);
				this.setVar("rndval", timeval);
			}
			if (this.element) { this.elementObj = document.getElementById(this.element); }
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					var totalurlstring = this.requestFile + "?" + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
				}
				if (this.method == "POST"){
  					try {
						this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
					} catch (e) {}
				}

				this.xmlhttp.send(this.URLString);
				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState){
						case 1:
							self.onLoading();
						break;
						case 2:
							self.onLoaded();
						break;
						case 3:
							self.onInteractive();
						break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
							self.onCompletion();
							if(self.execute){ self.runResponse(); }
							if (self.elementObj) {
								var elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							self.URLString = "";
						break;
					}
				};
			}
		}
	};
this.createAJAX();
}
var readSizeFromCookie = false;	// Determines if size and position of windows should be set/retreved by use of cookie
var windowMinSize = [300,200];	// Mininum width and height of windows.
	
var moveCounter = -1;	
var startEventPos = new Array();
var startPosWindow = new Array();
var startWindowSize = new Array();
var initResizeCounter = -1;	
var activeWindow = false;
var activeWindowContent = false;	
var windowSizeArray = new Array();
var windowPositionArray = new Array();
var currentZIndex = 10000;
var windowStateArray = new Array();	// Minimized or maximized
var activeWindowIframe = false;
var divCounter = 0;
var zIndexSet = false;
	
var MSIEWIN = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Win')>=0 && navigator.userAgent.toLowerCase().indexOf('opera')<0)?true:false;
var opera = navigator.userAgent.toLowerCase().indexOf('opera')>=0?true:false;

var ajaxObjects = new Array();

/*
  These cookie functions are downloaded from 
  http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm
*/	
function Get_Cookie(name) { 
    var start = document.cookie.indexOf(name+"="); 
    var len = start+name.length+1; 
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
    if (start == -1) return null; 
    var end = document.cookie.indexOf(";",len); 
    if (end == -1) end = document.cookie.length; 
    return unescape(document.cookie.substring(len,end)); 
} 
// This function has been slightly modified
function Set_Cookie(name,value,expires,path,domain,secure) { 
    expires = expires * 60*60*24*1000;
    var today = new Date();
    var expires_date = new Date( today.getTime() + (expires) );
    var cookieString = name + "=" +escape(value) + 
	    ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
	    ( (path) ? ";path=" + path : "") + 
	    ( (domain) ? ";domain=" + domain : "") + 
	    ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString; 
} 
			
function cancelEvent()
{
    return (moveCounter==-1 && initResizeCounter==-1)?true:false;
}

function initMove(e) {		
    if(document.all)e = event;
    moveCounter = 0;
    switchElement(false,this);
    startEventPos = [e.clientX,e.clientY];
    startPosWindow = [activeWindow.offsetLeft,activeWindow.offsetTop];
    startMove();
    if(!MSIEWIN)return false;
	
}
	
function startMove() {
    if(moveCounter>=0 && moveCounter<=10){
	moveCounter++;
	setTimeout('startMove()',5);
    }
}
	
function stopMove(e) {
    if(document.all)e = event;
    moveCounter=-1;
    initResizeCounter=-1;
    if(!activeWindow || !activeWindowContent)return;
    var state = '0';
    if(windowStateArray[activeWindow.id.replace(/[^0-9]/g,'')])state = '1';
		
    Set_Cookie(activeWindow.id + '_attr',activeWindow.style.left.replace('px','') + ',' + activeWindow.style.top.replace('px','') + ',' + activeWindow.style.width.replace('px','') + ',' + activeWindowContent.style.height.replace('px','') + ',' + activeWindow.style.zIndex + ',' + state,50);
}
	
function moveWindow(e) {
    if(document.all)e = event;
    if(moveCounter>=10){
	var left = startPosWindow[0] + e.clientX - startEventPos[0];
	var top = startPosWindow[1] + e.clientY - startEventPos[1];
	if (left < 0) {
	    left = 0;
	}
	if (top < 0) {
	    top = 0;
	}
	activeWindow.style.left = left  + 'px';
	activeWindow.style.top = top  + 'px';
    }	
		
    if(initResizeCounter>=10){
	var newWidth = Math.max(windowMinSize[0],startWindowSize[0] + e.clientX - startEventPos[0]);
	var newHeight = Math.max(windowMinSize[1],startWindowSize[1] + e.clientY - startEventPos[1]);
	activeWindow.style.width =  newWidth + 'px';
	activeWindowContent.style.height = newHeight  + 'px';		
			
	if(MSIEWIN && activeWindowIframe){
	    activeWindowIframe.style.width = (newWidth) + 'px';	
	    activeWindowIframe.style.height = (newHeight+20) + 'px';	
	}
    }
    if(!document.all)return false;
}

function initResizeWindow(e) {
    if(document.all)e = event;
    initResizeCounter = 0;
    switchElement(false,document.getElementById('dhtml_goodies_id' + this.id.replace(/[^\d]/g,'')));

    startWindowSize = [activeWindowContent.offsetWidth,activeWindowContent.offsetHeight];
    startEventPos = [e.clientX,e.clientY];
		
    if(MSIEWIN)activeWindowIframe = activeWindow.getElementsByTagName('IFRAME')[0];
    startResizeWindow();
    return false;
			
}
	
function startResizeWindow() {
    if(initResizeCounter>=0 && initResizeCounter<=10){
	initResizeCounter++;
	setTimeout('startResizeWindow()',5);
    }
}
	
function switchElement(e,inputElement) {
    if(!inputElement)inputElement = this;
    var numericId = inputElement.id.replace(/[^0-9]/g,'');
    var state = '0';
    if(windowStateArray[numericId])state = '1';
			
    if(activeWindow && activeWindowContent){
	Set_Cookie(activeWindow.id + '_attr',activeWindow.style.left.replace('px','') + ',' + activeWindow.style.top.replace('px','') + ',' + activeWindow.style.width.replace('px','') + ',' + activeWindowContent.style.height.replace('px','') + ',' + activeWindow.style.zIndex + ',' + state,50);
	
    }
    if (activeWindow == document.getElementById('dhtml_goodies_id' + numericId) && activeWindowContent == document.getElementById('windowContent' + numericId)) {
	return 0;
    }

    currentZIndex = currentZIndex/1 + 1;
    activeWindow = document.getElementById('dhtml_goodies_id' + numericId);	
    activeWindow.style.zIndex = currentZIndex;
    activeWindowContent = document.getElementById('windowContent' + numericId);

    Set_Cookie(activeWindow.id + '_attr',activeWindow.style.left.replace('px','') + ',' + activeWindow.style.top.replace('px','') + ',' + activeWindow.style.width.replace('px','') + ',' + activeWindowContent.style.height.replace('px','') + ',' + activeWindow.style.zIndex + ',' + state,50);
}

function closeWindow( id ) {
    switchElement(false,document.getElementById('dhtml_goodies_id' + id));
    activeWindow.style.display='none';
    var obj = document.getElementsByTagName("body")[0];
    if (obj) {
	var o = document.getElementById('dhtml_goodies_id' + id);
	obj.removeChild( o );
    }
}

function minimizeWindow(e,inputObj) {
    if(!inputObj)inputObj = this;
    var numericID = inputObj.id.replace(/[^0-9]/g,'');
    switchElement(false,document.getElementById('dhtml_goodies_id' + numericID));
    var state;	
    if(inputObj.src.indexOf('minimize')>=0){
	activeWindowContent.style.display='none';
	document.getElementById('resizeImage'+numericID).style.display='none';
	inputObj.src = inputObj.src.replace('minimize','maximize');	
	windowStateArray[numericID] = false;
	state = '0';		
    }else{			
	activeWindowContent.style.display='block';
	document.getElementById('resizeImage'+numericID).style.display='';
	inputObj.src = inputObj.src.replace('maximize','minimize');
	windowStateArray[numericID] = true;
	state = '1';
    }
		
    Set_Cookie(activeWindow.id + '_attr',activeWindow.style.left.replace('px','') + ',' + activeWindow.style.top.replace('px','') + ',' + activeWindow.style.width.replace('px','') + ',' + activeWindowContent.style.height.replace('px','') + ',' + activeWindow.style.zIndex + ',' + state,50);
}

function fscreenWindow(e,inputObj) {
    if(!inputObj)inputObj = this;
    var numericID = inputObj.id.replace(/[^0-9]/g,'');
    switchElement(false,document.getElementById('dhtml_goodies_id' + numericID));
    activeWindowContent.style.display='block';

    var newWidth;
    var newHeight;
    if (typeof window.innerWidth != 'undefined') {
	newWidth = window.innerWidth - 20;
	newHeight = window.innerHeight - 20;
    } else if (typeof document.documentElement != 'undefined'
	    && typeof document.documentElement.clientWidth !=
	    'undefined' && document.documentElement.clientWidth != 0) {
	newWidth = document.documentElement.clientWidth;
	newHeight = document.documentElement.clientHeight - 20;
    } else {
	newWidth = document.getElementsByTagName('body')[0].clientWidth;
	newHeight = document.getElementsByTagName('body')[0].clientHeight - 20;
    }

    activeWindow.style.width =  newWidth + 'px';
    activeWindowContent.style.height = newHeight  + 'px';		
    activeWindow.style.top = '0px';
    activeWindow.style.left = '0px';
    if(MSIEWIN && activeWindowIframe){
	activeWindowIframe.style.width = (newWidth) + 'px';	
	activeWindowIframe.style.height = (newHeight+20) + 'px';	
	activeWindowIframe.style.top = '0px';	
	activeWindowIframe.style.left = '0px';	
    }
}

function initWindows(e,divObj) {
    var divs = document.getElementsByTagName('DIV');

    if(divObj){
	var tmpDivs = divObj.getElementsByTagName('DIV');
	var divs = new Array();
	divs[divs.length] = divObj;

	for(var no=0;no<tmpDivs.length;no++){
	    divs[divs.length] = tmpDivs[no];
	}
    }

    for(var no=0;no<divs.length;no++){
	if(divs[no].className=='dhtmlgoodies_window'){	
	    if(MSIEWIN){
		var iframe = document.createElement('IFRAME');
		iframe.style.border='0px';
		iframe.frameborder=0;
		iframe.style.position = 'absolute';
		iframe.style.backgroundColor = '#FFFFFF';
		iframe.style.top = '0px';
		iframe.style.left = '0px';
		iframe.style.zIndex = 100;
		iframe.style.width = parseInt(divs[no].style.width.replace('px','')) + 'px';

		var subDiv = divs[no].getElementsByTagName('DIV')[0];
		divs[no].insertBefore(iframe,subDiv);
	    }
	    if(divObj){
		divs[no].style.zIndex = currentZIndex;
		currentZIndex = currentZIndex /1 + 1;
	    }

	    divCounter = divCounter + 1;
	    if(divCounter==1) { 
		activeWindow = divs[no];
	    }
	    divs[no].id = 'dhtml_goodies_id' + divCounter;	
	    divs[no].onmousedown = switchElement;
	    if(readSizeFromCookie) {
		var cookiePos = Get_Cookie(divs[no].id + '_attr') + '';	
	    } else {
		cookiePos = '';
	    }
	    if(divObj)cookiePos='';
	    var cookieValues = new Array();

	    if(cookiePos.indexOf(',')>0){
		cookieValues = cookiePos.split(',');
		if(!windowPositionArray[divCounter])windowPositionArray[divCounter] = new Array();
		windowPositionArray[divCounter][0] = Math.max(0,cookieValues[0]);
		windowPositionArray[divCounter][1] = Math.max(0,cookieValues[1]);
	    }

	    if(cookieValues.length==5 && !zIndexSet){
		divs[no].style.zIndex = cookieValues[4];
		if(cookieValues[4]/1 > currentZIndex)currentZIndex = cookieValues[4]/1;					
	    }
	    if(windowPositionArray[divCounter]){
		divs[no].style.left = windowPositionArray[divCounter][0] + 'px';	
		divs[no].style.top = windowPositionArray[divCounter][1] + 'px';	
	    }
				
	    var subImages = divs[no].getElementsByTagName('IMG');
	    for(var no2=0;no2<subImages.length;no2++){
		if(subImages[no2].className=='resizeImage'){
		    subImages[no2].style.cursor = 'nw-resize';
		    subImages[no2].onmousedown = initResizeWindow;
		    subImages[no2].id = 'resizeImage' + divCounter;
		    break;
		}	
		if(subImages[no2].className=='closeButton'){
		    subImages[no2].id = 'closeImage' + divCounter;
//		    subImages[no2].onclick = hideWindow;	
		}	
		if(subImages[no2].className=='refreshButton'){
		    subImages[no2].id = 'refreshImage' + divCounter;
		}	
		if(subImages[no2].className=='fscreenButton'){
		    subImages[no2].id = 'fscreenImage' + divCounter;
		    subImages[no2].onclick = fscreenWindow;
		}	
		if(subImages[no2].className=='minimizeButton') {
		    subImages[no2].id = 'minimizeImage' + divCounter;
		    subImages[no2].onclick = minimizeWindow;
		    if(cookieValues.length==6 && cookieValues[5]=='0'){
			setTimeout('minimizeWindow(false,document.getElementById("minimizeImage' + divCounter + '"))',10);
		    }
		    if(cookieValues.length==6 && cookieValues[5]=='1'){							
			windowStateArray[divCounter] = 1;
		    }
		}
	    }
	}
	if(divs[no].className=='dhtmlgoodies_windowMiddle' || divs[no].className=='dhtmlgoodies_window_bottom'){
	    divs[no].style.zIndex = 1000;
	}
	if(divs[no].className=='dhtmlgoodies_window_top'){
	    divs[no].onmousedown = initMove;
	    divs[no].id = 'top_bar'+divCounter;
	    divs[no].style.zIndex = 1000;
	}

	if(divs[no].className=='dhtmlgoodies_windowContent'){
	    divs[no].id = 'windowContent'+divCounter;
	    divs[no].style.zIndex = 1000;

	    if (MSIEWIN) {
		iframe.style.height = (parseInt(divs[no].style.height.replace('px','')) + 20) + 'px';
	    }

	    if(cookieValues && cookieValues.length>3){
		if(!windowSizeArray[divCounter])windowSizeArray[divCounter] = new Array();
		windowSizeArray[divCounter][0] = cookieValues[2];
		windowSizeArray[divCounter][1] = cookieValues[3];
	    }
	    if(cookieValues && cookieValues.length==5){
		activeWindowContent = document.getElementById('windowContent' + divCounter);
	    }
	    if(windowSizeArray[divCounter]){
		divs[no].style.height = windowSizeArray[divCounter][1] + 'px';
		divs[no].parentNode.parentNode.style.width = windowSizeArray[divCounter][0] + 'px';
		if(MSIEWIN){
		    iframe.style.width = (windowSizeArray[divCounter][0]) + 'px';
		    iframe.style.height = (windowSizeArray[divCounter][1]+20) + 'px';
		}
	    }
	}
    }

    if(!divObj){
	document.documentElement.onmouseup = stopMove;
	document.documentElement.onmousemove = moveWindow;
	document.documentElement.ondragstart = cancelEvent;
	document.documentElement.onselectstart = cancelEvent;
    }

    var xob = document.getElementById('dhtml_goodies_id' + divCounter);
    if ( xob ) {
	switchElement( false, xob );
    }

    return divCounter;
}

function createNewWindow(name,refreshlink,closelink,title,content,width,height,left,top) {
    var div = document.createElement('DIV');
    div.className='dhtmlgoodies_window';
    div.id = name + 'win';
    document.body.appendChild(div);

    var topDiv = document.createElement('DIV');
    topDiv.className='dhtmlgoodies_window_top';
    div.appendChild(topDiv);
		
    var img = document.createElement('IMG');
    img.src = 'img/fwin/top_left.gif';
    img.align='left';
    topDiv.appendChild(img);
		
    var img = document.createElement('IMG');
    img.src = 'img/fwin/top_center.gif';
    img.className='topCenterImage';
    topDiv.appendChild(img);
		
    var titleDiv = document.createElement('DIV');
    titleDiv.className='top_title';
    titleDiv.innerHTML = title;
    topDiv.appendChild(titleDiv);

    var buttonDiv = document.createElement('DIV');
    buttonDiv.className='top_buttons';
    topDiv.appendChild(buttonDiv);
		
    var img = document.createElement('IMG');
    img.src = 'img/fwin/fscreen.gif';
    img.className='fscreenButton';
    img.title='Pilns ekrāns';
    buttonDiv.appendChild(img);

    var img = document.createElement('IMG');
    img.src = 'img/fwin/minimize.gif';
    img.className='minimizeButton';
    img.title='Samazināt / Normalizēt';
    buttonDiv.appendChild(img);	

    var img = document.createElement('IMG');
    img.src = 'img/fwin/refresh.gif';
    img.className='refreshButton';
    img.onclick=function() { 
	eval(refreshlink);
    };
    img.title='Atjaunot';
    buttonDiv.appendChild(img);	
		
    var img = document.createElement('IMG');
    img.src = 'img/fwin/close.gif';
    img.className='closeButton';
    img.onclick=function() { eval(closelink); };
    img.title='Aizvērt';
    buttonDiv.appendChild(img);	
		
    var img = document.createElement('IMG');
    img.src = 'img/fwin/top_right.gif';
    buttonDiv.appendChild(img);		
		
    var middleDiv = document.createElement('DIV');
    middleDiv.className='dhtmlgoodies_windowMiddle';
    div.appendChild(middleDiv);
		
    var contentDiv = document.createElement('DIV');
    contentDiv.className='dhtmlgoodies_windowContent';
    contentDiv.innerHTML=content;
    middleDiv.appendChild(contentDiv);
		
    var bottomDiv = document.createElement('DIV');
    bottomDiv.className='dhtmlgoodies_window_bottom';
    div.appendChild(bottomDiv);
		
    var img = document.createElement('IMG');
    img.src = 'img/fwin/bottom_right.gif';
    img.className='resizeImage';
    bottomDiv.appendChild(img);

    windowSizeArray[windowSizeArray.length] = [width,height];
    windowPositionArray[windowPositionArray.length] = [left,top];

    div.style.width =  width + 'px';
    contentDiv.style.height = height  + 'px';
    div.style.left =  left + 'px';
    div.style.top = top  + 'px';

    return initWindows(false,div);
}

function showAjaxContent(ajaxIndex,windowId) {
    document.getElementById('windowContent' + windowId).innerHTML = ajaxObjects[ajaxIndex].response;
}

function addContentFromUrl(url,windowId) {
    var ajaxIndex = ajaxObjects.length;
    ajaxObjects[ajaxIndex] = new sack();
    ajaxObjects[ajaxIndex].requestFile = url;	// Specifying which file to get
    ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxContent(ajaxIndex,windowId); };
    ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function customFunctionCreateWindow(urlToExternalFile,width,height,left,top) {
    var divId = createNewWindow(width,height,left,top);
    document.getElementById('windowContent' + divId).innerHTML = 'Content of new window';
}
window.onload = initWindows;

