/**
 *				StandardClassPA
 *	Language	:	JavaScript 1.5 & JScript 8.0
 *	Maintainer	:	Bernard Łabno <blabno@tlen.pl>
 *	Last change	:	2006 Nov 03
 *	Description	:	This is JavaScript generated HTMLTableElement. Created to improve navigation in CMS3.
 *
 */
/*{{{StandardClassPA Comment*/
/**
 * 	new StandardClassPA returns HTMLTableElement with form within it.
 * 	Parameters description :
 * 		fields		- array of objects describing fields displayed below column headers 
 		[
			{
				name : 'o[Module][name]',
				title : 'nazwa',
				htmlType : 'input',
				value : 'Jan%'
			},
			{
				name : 'o[Module][class_id]',
				title : 'klasa pochodzenia',
				htmlType : 'select',
				values : [
					{ value : '', text : '' },
					{ value : 1, text : 'Article' },
					{ value : 2, text : 'JoinUs', selected:true },
					{ value : 3, text : 'KoListaRequest' },
					{ value : 4, text : 'Menu' }
				]
			}
		];

		--------IMPORTANT IE does not allow final coma after last element in array

		hiddenFields	- array of objects describing fields that should be created in form as hidden inputs
		[
			{ name:'action',value:'ModulePrintAll' }
		]
		addButton	- object describing link that will be displayed (if addButton is not null)
			{ href : 'http://bernard.labno.pl', text : 'Dodaj' }
		rowActions	- array of objects describing what links should be put at the end of each row i.e. edit, delete
		massActions	- object describing massActionButton, if present, then each row will have checkbox in first cell,
				  which will then be put in new form element of input type; example of values : 12=1,13=0,14=0,15=1
				  where '1' means 'checked' and '0' - 'unchecked', and 12,13,14 and 15 are values of each rows PrimaryKey
			{ fieldName:'o[Module][ids]', text:'Usuń moduły', href:'?action=ModuleDelete' }

 *//*}}}*/
/*{{{*/function StandardClassPA( fields, searchFieldsInitialDisplay, hiddenFields, addButton, rowActions, massActions )
{
	if( ! ( rowActions instanceof Array ) ) rowActions = [];
	if( ! ( fields instanceof Array ) ) fields = [];
	if( ! ( hiddenFields instanceof Array ) ) hiddenFields = [];
	var form, table;
	/*{{{*///Create form
	form =  document.createElement( 'form' );
	form.style.display = 'none';
	/*{{{*/form.textInputKeyPress = function(e)
	{
		var evt = ( StandardClassPA.prototype.isIE ) ? event : e;
		if( evt.keyCode == 13 ) {
			/**
			 *	Mozilla gets fucked up when you hit ENTER on field 
			 */
			if( ! StandardClassPA.prototype.isIE ) evt.preventDefault();
			form.onsubmit();
		}
	}/*}}}*/
	/*{{{*/form.search = function()
	{
		for( var i = 0 ; i < fields.length ; i++ ) if( fields[i].node.value != "" ) this.appendChild( fields[i].node );
		this.appendChild( table.tHead.searchFieldsRow.navigation.limitFrom );
		this.appendChild( table.tHead.searchFieldsRow.navigation.limitCount );
		this.appendChild( table.orderby );
		this.appendChild( table.orderbydir );
		//documentAppendCode( this.parentNode.innerHTML );
		this.submit();
		/**
		 *	Oh ! How cool !
		 *	In Mozilla try to call form.onsubmit() and it will do whatever sits within that method
		 *	but it will not submit form.
		 *	IE in oposite will submit form unless onsubmit returns false
		 */
		//return false;
	}/*}}}*/
	/*{{{*/form.massAction = function( actionId )
	{
		var yes = form.appendChild( StandardClassPA.prototype.createElement('input') );
		var no 	= form.appendChild( StandardClassPA.prototype.createElement('input') );
		no.name = massActions[actionId].fieldName + '[0]';
		yes.name = massActions[actionId].fieldName + '[1]';
		for( var i = 0 ; i < table.tBodies[0].childNodes.length ; i++ ) {
			if( table.tBodies[0].childNodes[i].firstChild.firstChild.checked ) 
				yes.value += rows[i][ massActions[actionId].valueFieldOffset ] + ',';
			else
				no.value += rows[i][ massActions[actionId].valueFieldOffset ] + ',';
		}
		yes.value = yes.value.substr(0, yes.value.length-1);
		no.value = no.value.substr(0, no.value.length-1);
		form.elements['action'].value = massActions[actionId].action;
		this.submit();
	}/*}}}*/
	form.onsubmit = form.search;
	/*{{{*///Put hidden fields into form
	for( var i = 0 ; i < hiddenFields.length; i++ ) {
		var field = form.appendChild( this.createElement('input','hidden') );
		field.name = hiddenFields[i].name;
		field.value = hiddenFields[i].value;
	}/*}}}*/
	/*}}}*/
	/*{{{*///Create table; apply tFoot,tHead,tBody and form into table
	table = document.createElement( 'table' );
	/*{{{*/table.loadRows = function( rows , limitFrom, limitCount, orderby, orderbydir )
	{
		if( ! ( rows instanceof Array ) ) rows = [];
		this.tHead.searchFieldsRow.navigation.limitFrom.value = limitFrom;
		this.tHead.searchFieldsRow.navigation.limitCount.value = limitCount;
		this.orderby.value = orderby;
		this.orderbydir.value = orderbydir;
		tbody = table.tBodies[0];
		for( var i = 0 ; i < rows.length ; i++ ) {
			tbody.insertRow(-1);
			if( i%2 == 0 ) tbody.lastChild.className = 'pair';
			if( massActions instanceof Array ) {
				tbody.lastChild.insertCell(-1).appendChild( StandardClassPA.prototype.createElement('input','checkbox') );
				tbody.lastChild.firstChild.className = 'checkbox';
				tbody.lastChild.firstChild.firstChild.checked = ( rows[i][massActions[0].checkedFieldOffset] == 'true' ) ? true : false;
				tbody.lastChild.onclick = function( e ) {
					var evt = ( StandardClassPA.prototype.isIE ) ? event : e;
					if( evt.target.tagName.toUpperCase() != 'INPUT' )
						this.firstChild.firstChild.checked = ! this.firstChild.firstChild.checked;
				}
			}
			for( var j = 0 ; j < fields.length ; j++ ) 
				tbody.lastChild.insertCell(-1).appendChild( document.createTextNode(rows[i][j]  ) );
			for( var j = 0 ; j < rowActions.length ; j++ ) {
				var link = tbody.lastChild.insertCell(-1).appendChild( document.createElement( 'a' ) );
				link.parentNode.className = 'action';
				var currentRow = rows[i];
				link.href = rowActions[j].href.replace(/([%])([^%]+)([%])/g, function( str , p1, p2, p3, offset, s ) {
					return currentRow[p2];
				});
				link.innerHTML = rowActions[j].text;
			}
		}
	}/*}}}*/
	/*{{{*/table.showSearchFields = function()
	{
		this.tHead.searchFieldsRow.style.display = '';
	}/*}}}*/
	/*{{{*/table.hideSearchFields = function()
	{
		this.tHead.searchFieldsRow.style.display = 'none';
	}/*}}}*/
	/*{{{*/table.rollSearchFields = function()
	{
		this.tHead.searchFieldsRow.style.display = (this.tHead.searchFieldsRow.style.display=='none') ? '' : 'none';
	}/*}}}*/
	table.createTHead();
	table.createTFoot();
	table.appendChild( document.createElement('tbody') );
	table.tHead.columnTitlesRow = table.tHead.insertRow(-1);
	table.tHead.searchFieldsRow = table.tHead.insertRow(-1);
	(searchFieldsInitialDisplay)?table.showSearchFields():table.hideSearchFields();
	/**
	 * 	form must be in document; if not browsers will go mad - IE stucks and Mozilla submits something
	 */
	table.appendChild( form );
	var field = form.appendChild( this.createElement('input','hidden') );
	field.name = 'orderby';
	table.orderby = field;
	var field = form.appendChild( this.createElement('input','hidden') );
	field.name = 'orderbydir';
	table.orderbydir = field;
	table.tFoot.insertRow(-1).insertCell(-1);
	table.tFoot.lastChild.lastChild.colSpan = fields.length + rowActions.length + ( ( massActions instanceof Array ) ? 1:0 );
	/*{{{*///Insert massActionSubmitButton into tFoot
	if( massActions instanceof Array ) for( var i = 0 ; i < massActions.length; i++ ) {
		table.tFoot.lastChild.lastChild.appendChild( this.createElement( 'input', 'button' ) ).onclick = function()
		{
			form.massAction( this.actionId );
		};
		table.tFoot.lastChild.lastChild.lastChild.actionId = i;
		table.tFoot.lastChild.lastChild.lastChild.value = massActions[i].text;
	}/*}}}*/
	/*{{{*///If massActions is to be displayed, some blank cell in title row and checkAll checkbox in searchFields row will be added
	if( massActions instanceof Array ) {
		table.tHead.searchFieldsRow.insertCell(-1);
		table.tHead.columnTitlesRow.insertCell(-1).appendChild( this.createElement('input','checkbox') ).onclick = function() {
			if( massActions instanceof Array ) for( var i = 0 ; i < table.tBodies[0].rows.length ; i++ )
				table.tBodies[0].rows[i].cells[0].firstChild.checked = this.checked;
		}
		table.tHead.columnTitlesRow.lastChild.className = 'checkbox';
	}/*}}}*/
	/*{{{*///Insert searchFields
	for( var i = 0 ; i < fields.length ; i++ ) {
			table.tHead.columnTitlesRow.insertCell(-1).innerHTML = fields[i].title;
			table.tHead.columnTitlesRow.lastChild.name = fields[i].orderByName;
			table.tHead.columnTitlesRow.lastChild.className = 'columnTitle';
			/*{{{*/table.tHead.columnTitlesRow.lastChild.onclick = function()
			{
				if( table.orderby.value == this.name ) {
					table.orderbydir.value = ( table.orderbydir.value == 'asc' ) ? 'desc' : 'asc';
				} else {
					table.orderby.value = this.name;
					table.orderbydir.value = 'asc';
				}
				form.onsubmit();
			}/*}}}*/
			var field = table.tHead.searchFieldsRow.insertCell(-1).appendChild( this.createElement( fields[i].htmlType ) );
			table.tHead.searchFieldsRow.lastChild.className = 'searchField';
			field.name = fields[i].name;
			field.orderByName = fields[i].orderByName;
			if( fields[i].htmlType.toLowerCase() == 'select' ) for( var j = 0 ; j < fields[i].values.length ; j++ ) {
					var option = field.appendChild( document.createElement( 'option' ) );
					option.value = fields[i].values[j].value;
					option.text = fields[i].values[j].text;
					option.selected = fields[i].values[j].selected;
			} else {
				field.value = fields[i].value;
			}

			field.onkeypress = form.textInputKeyPress;
			fields[i].node = field;
	}
	/*{{{*///Navigation buttons
	table.tHead.searchFieldsRow.insertCell(-1).colSpan = rowActions.length;
	table.tHead.searchFieldsRow.navigation = table.tHead.searchFieldsRow.lastChild;
	table.tHead.searchFieldsRow.lastChild.className = 'navigation';
	var prev = table.tHead.searchFieldsRow.lastChild.appendChild( this.createElement('input','button') );
	var limitFrom = table.tHead.searchFieldsRow.lastChild.appendChild( this.createElement('input') );
	var limitCount = table.tHead.searchFieldsRow.lastChild.appendChild( this.createElement('input') );
	var next = table.tHead.searchFieldsRow.lastChild.appendChild( this.createElement('input','button') );
	limitFrom.name = 'limitFrom';
	limitCount.name = 'limitCount';
	limitFrom.value = 0;
	limitCount.value = 30;
	table.tHead.searchFieldsRow.navigation.limitFrom = limitFrom;
	table.tHead.searchFieldsRow.navigation.limitCount = limitCount;
	prev.onclick = next.onclick = function() { 
		limitFrom.value = parseInt( limitFrom.value ) + ( ( this  == prev ) ? -1 : 1 ) * parseInt( limitCount.value );
		limitFrom.value = Math.max( limitFrom.value, 0 );
		form.onsubmit();
	}
	limitFrom.onkeyup = limitCount.onkeypress = function(e) {
		var evt = ( StandardClassPA.prototype.isIE ) ? event : e;
		this.value = parseInt( this.value );
		if( isNaN( this.value ) ) this.value = 0;
		form.textInputKeyPress(event);
	}
	prev.value = '<';
	next.value = '>';
	/*}}}*/
	/*}}}*/
	table.tHead.columnTitlesRow.insertCell(-1);
	table.tHead.columnTitlesRow.lastChild.className = 'search';
	table.tHead.columnTitlesRow.lastChild.colSpan = rowActions.length;
	/*{{{*///Add 'addButton' if needed
	if( addButton instanceof Object ) {
		var a = table.tHead.columnTitlesRow.lastChild.appendChild( document.createElement( 'a' ) );
		a.href = addButton.href;
		a.innerHTML = addButton.text;
	}/*}}}*/
	/*{{{*///Add 'searchButton'
	var a = table.tHead.columnTitlesRow.lastChild.appendChild( document.createElement( 'a' ) );
	a.href = 'javascript:;';
	a.innerHTML = 'Szukaj';
	a.onclick = function() { this.table.rollSearchFields() };
	a.table = table;
	/*}}}*/
	/*}}}*/
	return table;
}/*}}}*/
/*{{{*/StandardClassPA.prototype.createElement = function( name , type )
{
	/**
	 *	This method is used because IE does not allow to change input.type attribute due to security reasons,
	 *	thus following will not work :
	 *		document.createElement('input').type = 'checkbox';
	 */
	var node;
	if( ! type ) type = 'text';
	switch( name ) {
		case 'checkbox' : name = 'input'; type = 'checkbox'; break;
		case 'radio' : name = 'input'; type = 'radio'; break;
		case 'hidden' : name = 'input'; type = 'hidden'; break;
		case 'button' : name = 'input'; type = 'button'; break;
		case 'reset' : name = 'input'; type = 'reset'; break;
		case 'submit' : name = 'input'; type = 'submit'; break;
	}
	if( this.isIE && type != '' ) node = document.createElement( '<'+name+' type='+type+'>' );
	else {
		node = document.createElement( name );
		if( name == "input" ) node.type = type;
	}
	return node;
}/*}}}*/
StandardClassPA.prototype.isIE =  ( navigator.appName == "Microsoft Internet Explorer" );

/**
 * 	Debug support
 */
function documentAppendCode( text ) {
	document.body.appendChild( document.createElement('pre') ).appendChild( document.createTextNode( text.toString() ) );
}
