// Scripting by oechai
/*----------------------------------
createWindow(filepath, winIdName, settingParam, htmlCoding)
settings :
1  ['width']
2  ['height']
3  ['toolbar']
4  ['menubar']
5  ['scrollbars']
6  ['statusbar']
7  ['resizable']
Last Modified : 20070604 - added winIdName
----------------------------------*/

function createWindow ( fileLink, winIdName, settingParam, htmlCoding ){

	//strings --> title=ABC,width=80,height=100
	//alert(fileLink);
	//alert(settingParam);
	//alert(htmlCoding);
	var windowSetting = new Array();
	
	var settingString = settingParam;
	//spliting the strings --> [0]= 'title=ABC' ; [1] = 'width=80'; [2] = 'height=100'; 
	var params = settingString.split(',');

	for ( p=0; p<params.length; p++ ){
		//searching for keyword, '=' and seperate them to key and value
		//[0] = key: title, value: ABC
		//[1] = key: width, value: 80
		//[2] = key: height, value: 100
		var equalPos = params[p].indexOf(':');
		if ( equalPos>0 ){
			var settingKey = params[p].substring(0, equalPos).toLowerCase();
			var settingValue = params[p].substring(equalPos+1).toLowerCase();
			windowSetting[settingKey] = settingValue;
		}
	}

	if ( ! windowSetting['width'] ){ return false; }
	else var width = eval( windowSetting['width'] );
	
	if ( ! windowSetting['height'] ){ return false; }
	else	var height = eval( windowSetting['height'] );
	
	if ( ! windowSetting['toolbar'] ){ var toolbar = 0 }
	else var toolbar = windowSetting['toolbar'];

	if ( ! windowSetting['menubar'] ){ var menubar = 0 }
	else var menubar = windowSetting['menubar'];

	if ( ! windowSetting['scrollbars'] ){ var scrollbars = 'auto' }
	else var scrollbars = windowSetting['scrollbars'];
	
	if ( ! windowSetting['statusbar'] ){ var statusbar = 0 }
	else var statusbar = windowSetting['statusbar'];
	
	if ( ! windowSetting['resizable'] ){ var resizable = 0; }
	else var resizable = windowSetting['resizable'];

	//width and height problem
	var dW = 100; //default size decrease
	var dH = 100; //default size decrease
	
	var dsW = -1; 
	var dsH = -1;
	
	var oW = width;
	var oH = height;
	//oH > screenH, oH+dH>screenH, |oH-screenH|<dH
	if ( height>=screen.height || Math.abs((height-screen.height))<dH )
	{ 
		//alert("H:"+Math.abs((height-screen.height)));
		dsH=dH; dW=0; height=screen.height-dH; width+=20; scrollbars = 1; 
	}
	if ( width>=screen.width || Math.abs((width-screen.width))<dW )
	{ 
		//alert("W:"+Math.abs((width-screen.width)));
		dsW=dW; width=screen.width-dW; scrollbars = 1; 
	}
	
	//window position
	wPosLeft = Math.round((dsW<0)?(screen.width - width)/2:(dsW/2)); //(dsW/2)/2
	wPosTop = Math.round((dsH<0)?(screen.height - height)/2:(dsH/2));
	
	//alert("x:"+ wPosLeft+", y:"+ wPosTop);
	//alert("screenW:"+ screen.width+", oW:" +oW+ ", wPosLeft:"+ wPosLeft +"\n" + 
	//	  "screenH:"+ screen.height+", oH:" +oH+ ", wPosTop:"+ wPosTop );

	//settings is string type variable;
	var settings= 'WIDTH='+ width + ',HEIGHT='+ height +
	',TOOLBAR=' + toolbar + ',MENUBAR=' + menubar + ',STATUS=' + statusbar +
	',SCROLLBARS='+ scrollbars + ',RESIZABLE='+resizable + 
	',LEFT=' + wPosLeft +',TOP=' +wPosTop;
	
	//alert(width+','+ height+','+ toolbar+','+ scrollbars+','+ resizable );

	/////if fileLink is NOT provided, then htmlCoding should be null
	/////else it will overwrite the existing content in fileLink
	file_obj_path = fileLink;

	if ( fileLink!=null && (htmlCoding=='' || htmlCoding==null) ){
		file_obj_path = fileLink;
		neWindow = window.open( file_obj_path, winIdName, settings );
		neWindow.focus();
	}else file_obj_path = '';

	/////if fileLink is NOT provided, htmlCoding should be provided
	/////in order to make the page run as wanted.
	if ( htmlCoding!=null && (fileLink=='' || fileLink==null) ){
		neWindow = window.open( file_obj_path, winIdName, settings );
		//if ( !htmlCoding ) { htmlCoding = 'error found!' }
		neWindow.document.write( htmlCoding );
		neWindow.focus();
	}
}
