var Windows = new Array();
//var belenusHomeRootPath = belenusHome.location.href.substring(0, belenusHome.location.href.length-belenusHome.location.search.length);
//	belenusHomeRootPath = belenusHomeRootPath.substring(0, belenusHomeRootPath.lastIndexOf("/")+1);

/****************************************************************************************************************\
|** Window params
|** Windows['window'][0] = ''										<-- undefined param... maybe for later...
|** Windows['window'][1] = 'url'									<-- the url
|** Windows['window'][2] = 'name'									<-- window name
|** Windows['window'][3] = 'width=600,height=400'					<-- window properties
|** Windows['window'][4] = 'left=20,top=20'							<-- pos x|y, empty string == center 
|** Windows['window'][5] = new Array("Tab Caption", lockable, duplicate, scrolling) <-- tab able (if empty... it will not include into the tab registry!) (duplicate 0=yes, 1=no & reload page, 2=no &don't reload of the page in tab)
|** Windows['window'][99] = null									<-- reference to the window itself
|** 
\****************************************************************************************************************/

/****************************************************************************************************************/
// !! customize here please.... !!
var basketWinName = "basket"; 

var winNames = new Array(basketWinName, basketWinName+"Extra", "agb", "info", "help", "search", "basketPopup", "printPopup",
		"languageChooser", "splPrintPopup", "notes", "favorites", "movenote", "movefavorite", "partslinkConfirmWin", "extlink","searchX",
		"debugOutput","extPLPartInfo","salesman","salesmanPartInfo","excludedDocuments"); // the names of the different windows...

var winNamesWithLanguageDependantURL = new Array("help");

// look description above

	// create the basket window... 
	Windows[basketWinName] = new Array();
	Windows[basketWinName][0] = '';
	Windows[basketWinName][1] = 'basket/index.html';
	Windows[basketWinName][2] = 'basket';
	Windows[basketWinName][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
	Windows[basketWinName][4] = '';
	Windows[basketWinName][5] = new Array("belenusHome.ld['nav_basket']",false,1,0);
	Windows[basketWinName][99] = null;
  
	// create the basket window... 
	Windows[basketWinName+"Extra"] = new Array();
	Windows[basketWinName+"Extra"][0] = '';
	Windows[basketWinName+"Extra"][1] = 'basket/index.html';
	Windows[basketWinName+"Extra"][2] = 'basket';
	Windows[basketWinName+"Extra"][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
	Windows[basketWinName+"Extra"][4] = '';
  Windows[basketWinName+"Extra"][5] = '';
	Windows[basketWinName+"Extra"][99] = null;  
  
  // create the agb window...
  Windows['agb'] = new Array();
  Windows['agb'][0] = '';
  Windows['agb'][1] = 'partslink/agb_load.html';
  Windows['agb'][2] = 'agb';
  Windows['agb'][3] = 'width=800,height=650,scrollbars=yes,resizable=yes';
  Windows['agb'][4] = '';
  Windows['agb'][5] = '';
  Windows['agb'][99] = null;
	
	// create the info window...
	Windows['info'] = new Array();
	Windows['info'][0] = '';
	Windows['info'][1] = 'info.html';
	Windows['info'][2] = 'info';
	Windows['info'][3] = 'width=300,height=400,scrollbars=no,resizable=no';
	Windows['info'][4] = '';
  Windows['info'][5] = '';
	Windows['info'][99] = null;	
	
	// create the help window...
	Windows['help'] = new Array();
	Windows['help'][0] = '';
	Windows['help'][1] = 'help/'+belenusHome.currentLanguage+'/default.htm';
	Windows['help'][2] = 'help';
	Windows['help'][3] = 'width=760,height=500,scrollbars=yes,resizable=yes,toolbar=yes';
	Windows['help'][4] = '';
  Windows['help'][5] = '';
	Windows['help'][99] = null;		
	
	// create the search window...
	Windows['search'] = new Array();
	Windows['search'][0] = '';
	Windows['search'][1] = 'search/index.html';//index.html';
	Windows['search'][2] = 'search';
	Windows['search'][3] = 'width=650,height=450,resizable=yes';
	Windows['search'][4] = '';
	Windows['search'][5] = new Array("belenusHome.ld['nav_search']",false,0,0);
	Windows['search'][99] = null;			
	
	// create the addToBasketConfirm window...
	Windows['basketPopup'] = new Array();
	Windows['basketPopup'][0] = '';
	Windows['basketPopup'][1] = 'basket/confirm_window.html';
	Windows['basketPopup'][2] = 'basketPopup';
	Windows['basketPopup'][3] = 'width=600,height=100';
	Windows['basketPopup'][4] = '';
  Windows['basketPopup'][5] = '';
	Windows['basketPopup'][99] = null;	
	
  // create the basketPrintPopup window...
	Windows['printPopup'] = new Array();
	Windows['printPopup'][0] = '';
	Windows['printPopup'][1] = 'basket/print.html';
	Windows['printPopup'][2] = 'printPopup';
	Windows['printPopup'][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
	Windows['printPopup'][4] = '';
  Windows['printPopup'][5] = '';
	Windows['printPopup'][99] = null;		
	
	// create language chooser popup
	Windows['languageChooser'] = new Array();
	Windows['languageChooser'][0] = '';
	Windows['languageChooser'][1] = 'data_gui_language_chooser.html';
	Windows['languageChooser'][2] = 'languageChooser';
	Windows['languageChooser'][3] = 'width=430,height=500';
	Windows['languageChooser'][4] = '';
  Windows['languageChooser'][5] = '';
	Windows['languageChooser'][99] = null;	
	
	// create the print window...
	Windows['splPrintPopup'] = new Array();
	Windows['splPrintPopup'][0] = '';
	Windows['splPrintPopup'][1] = 'about:blank';
	Windows['splPrintPopup'][2] = 'splPrintPopup';
	Windows['splPrintPopup'][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
	Windows['splPrintPopup'][4] = '';
  Windows['splPrintPopup'][5] = '';
	Windows['splPrintPopup'][99] = null;					
  
	// create the device print window...
	Windows['devicePrintPopup'] = new Array();
	Windows['devicePrintPopup'][0] = '';
	Windows['devicePrintPopup'][1] = 'deviceprint.html';
	Windows['devicePrintPopup'][2] = 'devicePrintPopup';
	Windows['devicePrintPopup'][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
	Windows['devicePrintPopup'][4] = '';
  Windows['devicePrintPopup'][5] = '';
	Windows['devicePrintPopup'][99] = null;					
  
  // create the notes window...
  Windows['notes'] = new Array();
  Windows['notes'][0] = '';
  Windows['notes'][1] = '../jsp/personals/notes.html';
  Windows['notes'][2] = 'notes';
  Windows['notes'][3] = 'width=500,height=600,scrollbars=yes,resizable=yes';
  Windows['notes'][4] = '';
  Windows['notes'][5] = new Array(belenusHome.ld['p_titleN'],false,2,0);
  Windows['notes'][99] = null;	
  
  // create the favorites window...
  Windows['favorites'] = new Array();
  Windows['favorites'][0] = '';
  Windows['favorites'][1] = '../jsp/personals/favorites.html';
  Windows['favorites'][2] = 'favorites';
  Windows['favorites'][3] = 'width=500,height=600,scrollbars=yes,resizable=yes';
  Windows['favorites'][4] = '';
  Windows['favorites'][5] = new Array(belenusHome.ld['p_titleF'],false,2,0);
  Windows['favorites'][99] = null;							
  
  // create the move notes window...
  Windows['movenote'] = new Array();
  Windows['movenote'][0] = '';
  Windows['movenote'][1] = '../jsp/personals/move.html';
  Windows['movenote'][2] = 'movenote';
  Windows['movenote'][3] = 'height=400,width=280';
  Windows['movenote'][4] = '';
  Windows['movenote'][5] = '';
  Windows['movenote'][99] = null;							
  
  // create the move favorites window...
  Windows['movefavorite'] = new Array();
  Windows['movefavorite'][0] = '';
  Windows['movefavorite'][1] = '../jsp/personals/move.html';
  Windows['movefavorite'][2] = 'movefavorite';
  Windows['movefavorite'][3] = 'height=400,width=280';
  Windows['movefavorite'][4] = '';
  Windows['movefavorite'][5] = '';
  Windows['movefavorite'][99] = null;		  

  // create window for the confirm window 
  Windows['partslinkConfirmWin'] = new Array();
  Windows['partslinkConfirmWin'][0] = '';
  Windows['partslinkConfirmWin'][1] = 'partslink/partslink_add_part_confirm_window.html';
  Windows['partslinkConfirmWin'][2] = 'basketPopup';
  Windows['partslinkConfirmWin'][3] = 'width=300,height=180';
  Windows['partslinkConfirmWin'][4] = '';
  Windows['partslinkConfirmWin'][5] = '';
  Windows['partslinkConfirmWin'][99] = null;	  
	
  // create the extlink window...
  Windows['extlink'] = new Array();
  Windows['extlink'][0] = '';
  Windows['extlink'][1] = 'about:blank';
  Windows['extlink'][2] = 'extlink';
  Windows['extlink'][3] = 'width=600,height=500,scrollbars=yes,resizable=yes';
  Windows['extlink'][4] = '';
  Windows['extlink'][5] = '';
  Windows['extlink'][99] = null;	
  
	// create the search window...
	Windows['searchX'] = new Array();
	Windows['searchX'][0] = '';
	Windows['searchX'][1] = 'search/dynamic/dyn_search_wrapper.html';//index.html';
	Windows['searchX'][2] = 'searchX';
	Windows['searchX'][3] = 'width=796,height=350,resizable=yes';
	Windows['searchX'][4] = '';
	Windows['searchX'][5] = new Array(belenusHome.ld['nav_search'],false,0,0);
	Windows['searchX'][99] = null;
	
	// create the search window...
	Windows['debugOutput'] = new Array();
	Windows['debugOutput'][0] = '';
	Windows['debugOutput'][1] = 'debugoutput.html';
	Windows['debugOutput'][2] = 'debugOutput';
	Windows['debugOutput'][3] = 'width=596,height=350,resizable=yes,scrollbars=yes';
	Windows['debugOutput'][4] = '';
	Windows['debugOutput'][5] = new Array("Error",false,1,0);
	Windows['debugOutput'][99] = null;	
	
	Windows['extPLPartInfo'] = new Array();
  Windows['extPLPartInfo'][0] = '';
  Windows['extPLPartInfo'][1] = 'about:blank';
  Windows['extPLPartInfo'][2] = 'extPLPartInfo';
  Windows['extPLPartInfo'][3] = 'width=750,height=500,scrollbars=yes,resizable=yes';
  Windows['extPLPartInfo'][4] = '';
  Windows['extPLPartInfo'][5] = '';
  Windows['extPLPartInfo'][99] = null;	
  
	// create the basket window... 
	Windows["salesman"] = new Array();
	Windows["salesman"][0] = '';
	Windows["salesman"][1] = '../order/basket';
	Windows["salesman"][2] = 'salesman';
	Windows["salesman"][3] = 'width=700,height=500,scrollbars=yes,resizable=yes';
	Windows["salesman"][4] = '';
	Windows["salesman"][5] = '';
	Windows["salesman"][99] = null;  
	
	// create the basket window... 
	Windows["salesmanPartInfo"] = new Array();
	Windows["salesmanPartInfo"][0] = '';
	Windows["salesmanPartInfo"][1] = '../order/articleQuery';
	Windows["salesmanPartInfo"][2] = 'salesmanPartInfo';
	Windows["salesmanPartInfo"][3] = 'width=700,height=500,scrollbars=yes,resizable=yes';
	Windows["salesmanPartInfo"][4] = '';
	Windows["salesmanPartInfo"][5] = '';
	Windows["salesmanPartInfo"][99] = null;  	
	
	var w = new Array("",
		"width="+(screen.width/2),
		"height="+(screen.height/2),
		"left="+((screen.width/2) - ((screen.width/2) / 2)),
		"top="+((screen.height/2) - ((screen.height/2) / 2))
	); // window with a size half of the screen and centered...
	Windows["excludedDocuments"] = new Array();
	Windows["excludedDocuments"][0] = '';
	Windows["excludedDocuments"][1] = 'empty.html';
	Windows["excludedDocuments"][2] = 'excludedDocuments';
	Windows["excludedDocuments"][3] = "status=no,toolbar=no,menubar=no,hotkeys=no,location=no,resizable=yes"+w.join(",");
	Windows["excludedDocuments"][4] = '';
	Windows["excludedDocuments"][5] = '';
	Windows["excludedDocuments"][99] = null;  						
	
/*****************************************************************************************************************/
	

/*****************************************************************************************************************/
// internal functions
	
function getCenterCoordinates(wName) { // returns the start position
	var winProps = Windows[wName][3];
	if(winProps.indexOf("width=") != -1 && winProps.indexOf("height=") != -1) {
		var x = "left=";
		var y = "top=";
		var difValues = winProps.split(",");
		for(i in difValues) {
			if(difValues[i].indexOf("width") != -1) {
				x += (screen.width / 2) - (parseInt(difValues[i].split("=")[1]) / 2);
			}
			if(difValues[i].indexOf("height") != -1) {
				y += (screen.height / 2) - (parseInt(difValues[i].split("=")[1]) / 2);
			}
		}
		return (x+","+y);
	} else return "left=0,top=0";
}

/*****************************************************************************************************************/



/*****************************************************************************************************************/
// object methods

function openWindow(wName, queryStr, reload) {
	if(Windows[wName] != null) {	// test if the wished window is defined at the WindowManager....

		//if a param with the name 'tabCaption' is given. The tabManager is expect now to params escaped in this param
		//the names of the params are shortTabCaption & longTabCaption
		var tabLabel = Windows[wName][5][0];
		if(queryStr && queryStr.indexOf('tabCaption') != -1) { 
			tabLabel = belenusHome.getQueryField(queryStr,'tabCaption');
			var queryStrWithoutTabCaption = "";
			var startCut = queryStr.indexOf('tabCaption');
			var stopCut = eval(parseInt(startCut)+11+escape(tabLabel).length);
			queryStrWithoutTabCaption = queryStr.substring(0,startCut);
			queryStrWithoutTabCaption+=queryStr.substring(stopCut,queryStr.length);
			queryStr=queryStrWithoutTabCaption;
		}

		if(Windows[wName][5] != "" && belenusHome.useTabManager && queryStr.indexOf("loadPage=") == -1) {
//			var tabLabel = Windows[wName][5][0];
			Windows[wName][99] = belenusHome.TAM.appendTab(Windows[wName][2],belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr),tabLabel,Windows[wName][5][1],Windows[wName][5][2],Windows[wName][5][3]);
		} else { // not included as tab in the registry
			var xtr = Windows[wName][4]; // set pos x,y.....
			if(Windows[wName][4] == "") {
				xtr = getCenterCoordinates(wName) // if pos x,y param is empty... set window centered
			}
			if(Windows[wName][99] != null) { // if the window contains a window obj..... and not null
				try {
					if(Windows[wName][99].closed == false) { // if the window is open....
						if(reload) {
							//Windows[wName][99].location.reload();
							if(Windows[wName][99].location.search.indexOf("?") != -1) {
								if(Windows[wName][99].location.search != "?"+queryStr) {
									var uri = Windows[wName][99].location;
									if(uri != "about:blank") Windows[wName][99].location.href = belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr);
								} else {
									Windows[wName][99].location.reload();
								}
							} else {
								Windows[wName][99].location.reload();
							}
						}
						Windows[wName][99].focus();
					} else { // if the window is closed, but in the past it has exists...
						var uri = Windows[wName][1];
						if(uri != "about:blank") uri = belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr);
						Windows[wName][99] = belenusHome.open(uri,Windows[wName][2],Windows[wName][3].concat(",").concat(xtr)); // open window with the right parameters...
						Windows[wName][99].name = Windows[wName][2];
					}
				} catch(e) {
					// catched a non remoteServerComputer..
					var uri = Windows[wName][1];
					if(uri != "about:blank") uri = belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr)
					Windows[wName][99] = belenusHome.open(uri,Windows[wName][2],Windows[wName][3].concat(",").concat(xtr)); // open window with the right parameters...
					Windows[wName][99].name = Windows[wName][2];					
				}				
			} else { // if it was null
				var uri = Windows[wName][1];
				if(uri != "about:blank") uri = belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr)
				Windows[wName][99] = belenusHome.open(uri,Windows[wName][2],Windows[wName][3].concat(",").concat(xtr)); // open window with the right parameters...
				try {
					Windows[wName][99].name = Windows[wName][2];			
				} catch(e) {
					belenusHome.doPopupBlockerTest();
				}	
			}
		}	
	} else {
		alert("The window that you try to open is not defined in the WindowManager!");
	}
}


function closeWindow(wName) {
	if(Windows[wName] != null) {
		if(Windows[wName][99] != null) {
			try {
			if(Windows[wName][99].closed == false) {
				Windows[wName][99].close();
				Windows[wName][99] = null;
			}
			} catch(e) {
				// catched a non remoteServerComputer..
			}
		}
	} else {
		alert("The window, that you try to close, is not defined in the WindowManager!");
	}
}


function closeAllWindows() {
	for(i in winNames) {
		closeWindow(winNames[i]);
	}
}


function getWindowReference(wName) {
	if(Windows[wName] != null) {
		if(Windows[wName][99] != null) {
			return Windows[wName][99];
		} else {
      if(Windows[wName][5] != "") {
      return Windows[wName][99];
      } else return null;// the window wasn't init....
		}
	} else {
		alert("The window, that you try to open, is not defined in the WindowManager!\n& can't be referred\nThe returning value of this function is null");
		return null;
	}
}

function resetLanguageDependantsLinks(currrentLanguage, newLanguage) {
	for(var ldl = 0; ldl < winNamesWithLanguageDependantURL.length; ldl++) {
		var newURL = Windows[winNamesWithLanguageDependantURL[ldl]][1];
		var startIndex = newURL.indexOf(currentLanguage);
		if(startIndex!=-1) {
			newURL = newURL.substring(0,startIndex)+newLanguage+newURL.substring(startIndex+5);
			Windows[winNamesWithLanguageDependantURL[ldl]][1] = newURL;
		}
	}
}

/*****************************************************************************************************************/




/*****************************************************************************************************************\
|** WindowManager
|** WindowManager.openWindow(wName, queryStr, reload) 
|** WindowManager.closeWindow(wName)
|** WindowManager.closeAllWindows()
|** WindowManager.getWindowReference(wName) <-- return a window object or null if it dosn't exists or wasn't init...
|**
\*****************************************************************************************************************/


function WindowManager() { // add the methods to the object...
	this.openWindow = openWindow;
	this.closeWindow = closeWindow;
	this.closeAllWindows = closeAllWindows;
	this.getWindowReference = getWindowReference;
	this.resetLanguageDependantsLinks = resetLanguageDependantsLinks;
}

/*****************************************************************************************************************/


/*****************************************************************************************************************/
// make the object... init the WindowManager.....

belenusHome.WM = new WindowManager();

/*****************************************************************************************************************/
