var lib=
{

sID: 1,
imgWaitSave: "../images/waitani.gif",
imgWaitLoad: "../images/waitani1.gif",
Collection: {},
shiftKey: false,
ctrlKey: false,
Data: null,
Documents:
	{
		setDataObject: function( docName, Doc)
		{
			return eval( "this."+ docName+ "= Doc;");
		},

		getDataObject: function( docName)
		{
			return eval( "this."+ docName);
		}
	},

init: function()
{
	this.Data= new ActiveXObject( "Microsoft.XMLDOM");
	this.Data.loadXML( '<?xml version="1.0"?><Root><Edit /></Root>');
	this.Data= this.Data.documentElement;

	document.attachEvent( "onkeypress", this.keyPress);
	document.attachEvent( "onkeydown", this.onKeyDown);
	document.attachEvent( "onkeyup", this.onKeyUp);

	var path= window.location.pathname;
	if( path.lastIndexOf( "/")> -1)
	{
		path= path.substring( 0, path.lastIndexOf( "/"));
	}

	this.rqAddr= window.location.protocol+ "//"+
		window.location.hostname+
		path+ "/Request.php?action="
	;
},

getXPath: function( Node, idAttributeName)
{
	var xPath= Node.tagName+ "[@"+ idAttributeName+ "="+ Node.getAttribute( idAttributeName)+ "]";

	for( Node= Node.parentNode; Node.parentNode; Node= Node.parentNode)
	{
		xPath= Node.tagName+ "/"+ xPath;
	}

	return "/"+ xPath;
},

newWindow: function( sHTML, wName, features)
{
	if( !wName)
	{
		wName= "_blank";
	}

	if( !features)
	{
		features= "width=700,height=500,resizable=yes,scrollbars=yes";
	}

	var w= window.open( "", wName, features);

	w.document.write( sHTML);
	w.document.close();

	return w;
},

printHTML: function( sHTML, wName, features)
{
	var w= this.newWindow( sHTML, wName, features);

	w.print();
},

startWait: function( imageNick)
{
	this.imageNick= imageNick;

	if( !this.waitVisible)
	{
		this.waitVisible= true;
		this.showWaiter();
	}
},

stopWait: function()
{
	this.waitVisible= false;
	this.hideWaiter();
},

showWaiter: function()
{
	var div;
	if( div= document.all[ "waiter"])
	{
		if( lib.imageNick && lib.imageNick.toLowerCase()== "saving")
		{
			div.firstChild.src= lib.imgWaitSave;
		}
		else
		{
			div.firstChild.src= lib.imgWaitLoad;
		}

		try {
			div.firstChild.loop= -1;
			div.firstChild.beginElement();
		} catch( e) {}
		div.style.display= "";
	}
},

hideWaiter: function()
{
	var div;
	if( div= document.all[ "waiter"])
	{
		div.style.display= "none";
	}
},

checkSelectedNodes: function( nodePath, attributeName, targetXPath)
{
	var Node= this.Data.selectSingleNode( nodePath);
	if( !Node)
	{
		return;
	}

	var IDs= Node.getAttribute( attributeName);
	if( IDs)
	{
		IDs= IDs.split( ",");

		for( var i= 0; i< IDs.length; i++)
		{
			var Node;
			if( IDs[ i]> 0 &&
				( Node= this.Data.selectSingleNode( targetXPath.replace( /\$[\w\d]+(\W|$)/, IDs[ i]+ "$1")))
			)
			{
				Node.setAttribute( "checked", 1);
			}
		}
	}
},

onKeyDown: function()
{
	if( event.keyCode== 13 &&
		( !event.srcElement.tagName || event.srcElement.tagName!= "TEXTAREA")
	)
	{
		event.cancelBubble= true;
		event.returnValue= false;
		return false;
	}
},

onKeyUp: function()
{
},

keyPress: function()
{
	if( event.keyCode== 13 && event.ctrlKey && event.shiftKey)
	{
		window.clipboardData.setData( "Text", document.body.outerHTML);
	}
	else if( event.keyCode== 14 && event.ctrlKey && event.shiftKey)
	{
		window.clipboardData.setData(
			"Text",
			'<?xml version="1.0" encoding="windows-1251"?>'+ lib.Data.xml
		);
	}
},

getEditNode: function()
{
	return this.Data.selectSingleNode( "Edit");
},

isEditMode: function()
{
	return this.getEditNode().firstChild;
},

isDataModified: function()
{
	if( !this.getEditNode().getAttribute( "Modified"))
	{
		return false;
	}

	return this.getEditNode().getAttribute( "Modified")> 0;
},

clearEditNode: function()
{
	var Edit= this.getEditNode();
	while( Edit.firstChild)
	{
		Edit.removeChild( Edit.firstChild);
	}
},

setEditNode: function( newEdit)
{
	this.clearEditNode();
	var Edit= this.getEditNode();

	if( newEdit)
	{
		if( typeof( newEdit)== "string")
		{
			newEdit= Edit.ownerDocument.createElement( newEdit);
		}
		else
		{
			newEdit= newEdit.cloneNode( true);
		}

		Edit.appendChild( newEdit);
	}

	Edit.setAttribute( "Modified", 0);
},

logIn: function()
{
	var retValue= window.showModalDialog( "logIn.htm", [ window], "dialogHeight: 125px; dialogWidth: 250px; scroll: no; help: no; status: no");
	if( retValue== "close")
	{
		window.close();
		this.closing= true;
	}
	
},

doLogIn: function( dialog, login, pwd)
{
	var iFrame= document.all['hidden'];
	iFrame.currentHandler= this;
	iFrame.currentDialog= dialog;

	var fDoc= iFrame.contentWindow.document;
	fDoc.body.innerHTML=
		"<form name='f' method='post' action='logIn.php'>"+
		"<input name='login' /><input name='pwd' />"+
		"</form>"
	;
	fDoc.f.login.value= login;
	fDoc.f.pwd.value= pwd;
	fDoc.f.submit();

	dialog.window.setTimeout( this.waitLogIn, 1000);
},

waitLogIn: function()
{
	var iFrame= document.all['hidden'];
	var ob= iFrame.currentHandler;
	var dialog= iFrame.currentDialog;

	ob.waitCounter++;

	if( iFrame.readyState!= "complete" ||
		!iFrame.contentWindow.document ||
		!iFrame.contentWindow.document.body ||
		iFrame.contentWindow.document.body.readyState!= "complete" ||
		!iFrame.contentWindow.document.body.firstChild
	)
	{
		if( ob.waitCounter> 100)
		{
//	server doesn't responde
		}
		dialog.setTimeout( ob.waitLogIn, 250);
		return;
	}

	var oDoc= new ActiveXObject( "Microsoft.XMLDOM");
	if( !oDoc.loadXML( iFrame.contentWindow.document.body.innerText))
	{
		lib.stopWait();
		alert( "При обращении к серверу возникла ошибка\r\n\r\n"+ iFrame.contentWindow.document.body.innerText);
		return;
	}

	var login= oDoc.documentElement.getAttribute( "login");
	if( login)
	{
		alert( "Авторизация не пройдена!\r\n\r\nПопробуйте ввести данные еще раз");
	}
	else
	{
		dialog.close();
	}
},

loadDocument: function( Url)
{
	if( this.closing)
	{
		return null;
	}

	var oDoc= new ActiveXObject( "Microsoft.XMLDOM");
	oDoc.async = false;
	oDoc.resolveExternals = false;

	for( ; oDoc.load( Url) ;)
	{
		var login= oDoc.documentElement.getAttribute( "login");
		if( login)
		{
			this.logIn();
			if( this.closing)
			{
				return null;
			}
			continue;
		}

		return oDoc;
	}

	alert( "Ошибка чтения данных с сервера");
	return false;
},

getDataName: function( Url)
{
	var dataName= Url;
	var Slash= dataName.lastIndexOf( "/");
	if( Slash!= -1)
	{
		dataName= dataName.substring( Slash+ 1);
	}

	return dataName.replace( /[^\w\d]/g, "_");
},

isXSLInCache: function( Url)
{
	var dataName= this.getDataName( Url);
	var dataObject= this.Documents.getDataObject( dataName);

	return dataObject;
},

loadXSL: function( Url, Refresh)
{
	var dataName= this.getDataName( Url);
	var dataObject= this.Documents.getDataObject( dataName);

	if( Refresh || !dataObject)
	{
		dataObject= this.Documents.setDataObject( dataName, this.loadDocument( Url));
	}

	return dataObject;
},

getParentXPath: function( xPath)
{
	var parentXPath= ".";
	var Slash= xPath.lastIndexOf( "/");
	if( Slash!= -1)
	{
		parentXPath= xPath.substring( 0, Slash);
	}

	return parentXPath;
},

prepareUrl: function( Url)
{
	if( Url.match( /^[\w]+(\&|$)/))
	{
		Url= this.rqAddr+ Url;
	}

	return Url;
},

isDataInCache: function( Url, xPath)
{
	Url= this.prepareUrl( Url);

	var dataName= this.getDataName( Url);
	var dataObject= this.Documents.getDataObject( dataName);

	return dataObject;
},

loadData: function( Url, xPath, Refresh)
{
	Url= this.prepareUrl( Url);

	var dataName= this.getDataName( Url);
	var dataObject= this.Documents.getDataObject( dataName);
	var oNode= null;

	if( Refresh || !dataObject)
	{
		var oDoc= this.loadDocument( Url)
		if( !oDoc)
		{
			return null;
		}

		oDoc= oDoc.documentElement;
		if( !oDoc)
		{
			return null;
		}

		this.Documents.setDataObject( dataName, oDoc);

		if( oDoc.nodeName== "Response")
		{
			if( oDoc.getAttribute( "Success")!= 1 && oDoc.selectSingleNode( "Error"))
			{
				alert(
					"При обращении к серверу Базы данных возникла ошибка\r\n\r\n"+
					oDoc.selectSingleNode( "Error").getAttribute( "text")
				);

				return null;
			}

			oDoc= oDoc.firstChild;
		}

		if( !oDoc)
		{
			return null;
		}

		lib.setNodesId( oDoc);

		for( ; oDoc; oDoc= oDoc.nextSibling)
		{
			if( !xPath)
			{
				xPath= oDoc.tagName;
			}

			oNode= this.Data.selectSingleNode( xPath);

			if( oNode)
			{
				var parentNode= oNode.parentNode;
				parentNode.removeChild( oNode);
				oNode= parentNode.appendChild( oDoc.cloneNode( true));
				
			}
			else
			{
				oNode= this.Data.selectSingleNode( this.getParentXPath( xPath)).appendChild( oDoc.cloneNode( true));
			}

			oDoc.setAttribute( "dataXPath", xPath);
		}
	}
	else
	{
		var dataXPath=
			dataObject.nodeName== "Response"
			? dataObject.firstChild.getAttribute( "dataXPath")
			: dataObject.getAttribute( "dataXPath");

		oNode= this.Data.selectSingleNode( dataXPath);
	}

	return oNode;
},

findParentElement: function( el, fclass, eclass)
{
	var r1= new RegExp( "^"+ fclass+ "($| )", "");
	var r2= new RegExp( "^"+ eclass+ "($| )", "");

	while (el!= null && ( typeof(el.className)== 'unfefined' || !el.className.match( r2)))
	{
		if( typeof( el.className)!= 'unfefined' && el.className.match( r1))
		{
			return el;
		}

		el= el.parentElement;
	}

	return null;
},

findChildElement: function( el, fclass)
{
	var r1= new RegExp( "^"+ fclass+ "($| )", "");
	var retEl;

	for (el= el.firstChild; el; el= el.nextSibling)
	{
		if( el.className && el.className.match( r1))
		{
			return el;
		}

		if( el.firstChild && ( retEl= this.findChildElement( el, fclass)))
		{
			return retEl;
		}
	}

	return null;
},

getMe: function( el, fclass)
{
	var r1= new RegExp( "^"+ fclass+ "($| )", "");

	while (el!= null)
	{
		if( typeof(el.className)!= 'unfefined' && el.className.match( r1))
		{
			return eval( "lib.Collection."+ el.varname);
		}

		el= el.parentElement;
	}

	return null;
},

setNodesId: function( oDoc)
{
	var oNodes= oDoc.selectNodes( "//*");

	for( var i= 0; i< oNodes.length; i++, this.sID++)
	{
		if( oNodes[ i].getAttribute( "sID")== null)
		{
			oNodes[ i].setAttribute( "sID", this.sID);
		}
	}
},

loadScript: function( ScriptName)
{
	for( var i= 0; i< document.scripts.length; i++)
	{
		if( document.scripts[i].getAttribute("src")== ScriptName)
		{
			return;
		}
	}

	var sHTML = "<script language='JScript' src='" + ScriptName + "'></script>";
	document.all[1].appendChild( document.createElement(sHTML));
},

cancelEvent: function(event)
{
	if (event)
	{
		event.cancelBubble= true;
		event.returnValue= false;
	}
}

};

lib.init();