function addDatePicker(editorid, fieldref) {
	


	$(editorid + ' a[menubutton=add]').click( function() {
		setTimeout(' $("' + editorid + ' ' + fieldref + '").datepicker("destroy"); ',10);
		setTimeout(' $("' + editorid + ' ' + fieldref + '").datepicker({ dateFormat: "yy-mm-dd" }); ',20);
	});
	
}

function addDateTool(editorid, fieldref) {
	setTimeout(' $("' + editorid + ' ' + fieldref + '").datepicker("destroy"); ',1);
	setTimeout(' $("' + editorid + ' ' + fieldref + '").datepicker({ dateFormat: "yy-mm-dd" }); ',2);
}


function attachDatePicker(editorid, fieldref) {
	setTimeout(' $("' + editorid + ' ' + fieldref + '").datepicker("destroy"); ',497);
	setTimeout(' $("' + editorid + ' ' + fieldref + '").datepicker({ dateFormat: "yy-mm-dd" }); ', 499);
}function attachPopup(o, s) {

	$(o).unbind('dblclick').dblclick( function() {	

		$('#popupframe').remove();
		$('#page').append('<div id="popupframe" style="position: absolute; z-index: 2; top: 0px; left: 0px; width: 100%; height: 100%; background-color: transparent;"><div style="position: absolute; z-index: 2; top: 10%; left: 15%; width: 70%; background-color: white; border: 1px solid  black;"><div style="height: 16px; background-color: #D6DCE0; padding: 2px; margin-bottom: 2px;"><span ref="closepopup" style="float: right;">close</span></div><div style="padding: 2px;" id="popup">Working... please wait.</div></div></div>');
		
		$('[ref=closepopup]').click( function() {
			$('#popupframe').remove();
		});

		$.run(s);
	});


	$(o).prev().unbind('dblclick').dblclick( function() {	

		$('#popupframe').remove();
		$('#page').append('<div id="popupframe" style="position: absolute; z-index: 2; top: 0px; left: 0px; width: 100%; height: 100%; background-color: transparent;"><div style="position: absolute; z-index: 2; top: 10%; left: 15%; width: 70%; background-color: white; border: 1px solid  black;"><div style="height: 16px; background-color: #D6DCE0; padding: 2px; margin-bottom: 2px;"><span ref="closepopup" style="float: right;">close</span></div><div style="padding: 2px;" id="popup">Working... please wait.</div></div></div>');
		
		$('[ref=closepopup]').click( function() {
			$('#popupframe').remove();
		});

		$.run(s);
	});

}/* 

attachSuggestion (former suggestFor)
min chars to start
search patterns x%, %x% and %x%x%
selection mandatory or not
arrow navigation
... click support (* css hover) as run callback function (bind.bind)
foreign key support

BUG search wist space does not works
																		
ref 						- unique
script 					- full path to to the suggestion content script		
ismandatory 		- "man" or "not" (if so on blur selected value is taken)
minchars 				- minimum number charecters to generate suggestion, default = 2

*/

function attachSuggestion_keydown(o,e,ref) {
	var keyCode = e.keyCode || window.event.keyCode;
	
	if(keyCode != 9) {
	
		/* add id if not set */
	
		if( o.attr('id') == '')
			o.attr('id',ref);
		
		if( !o.next().attr('class') )
			o.after('<div id="' + ref + '" class="suggestion"></div>');
	}
}

function attachSuggestion_blur(o,ismandatory,istype) {
	
	/*
	if(ismandatory == 'man') {
		var result = o.next().find('div.selectedSuggestion').html();	
		o.attr('value',result);	
	}
	o.next().remove();
	*/
	
		if(istype == '') {
			
			var iskey = o.next().find('div.selectedSuggestion').find('span[istype=iskey]').html();
		
			if(ismandatory == 'man') {
				o.attr('value',iskey);
			}
			else if(iskey != '' && iskey != null) {
				o.attr('value',iskey);
			}
			o.next().remove();
		}

		if(istype == 'foreignkey') {
			var iskey = o.next().find('div.selectedSuggestion').find('span[istype=iskey]').html();
			var isforeignkey = o.next().find('div.selectedSuggestion').find('span[istype=isid]').html();
			
			if(o.next().attr('class') == 'suggestion') {
			
				if(ismandatory == 'man') {
					o.attr('value',iskey);
					o.next().next().attr('value',isforeignkey);
				}
				else if(iskey != '' && iskey != null) {
					
					o.attr('value',iskey);
					o.next().next().attr('value',isforeignkey);
				}
				
				o.next().remove();
			}	
	
		}
	

}

function attachSuggestion_keyup(o,e,ismandatory,ref,minchar,script,istype) {
		var keyCode = e.keyCode || window.event.keyCode;
	
		if(keyCode == 40 && o.find('div.selectedSuggestion').next().html() != '[end]')
			o.next().find('div.selectedSuggestion').removeClass('selectedSuggestion').next().addClass('selectedSuggestion');
	
		if(keyCode == 38 && o.find('div.selectedSuggestion').prev().html() != '[start]')
			o.next().find('div.selectedSuggestion').removeClass('selectedSuggestion').prev().addClass('selectedSuggestion');
	
		if(keyCode == 13 && istype == '') {
			var iskey = o.next().find('div.selectedSuggestion').find('span[istype=iskey]').html();
			
			if(ismandatory == 'man') {
				o.attr('value',iskey);
			}
			else if(iskey != '' && iskey != null) {
				o.attr('value',iskey);
			}
			o.next().remove();
		}

		if(keyCode == 13 && istype == 'foreignkey') {
			var iskey = o.next().find('div.selectedSuggestion').find('span[istype=iskey]').html();
			var isforeignkey = o.next().find('div.selectedSuggestion').find('span[istype=isid]').html();
			
			if(ismandatory == 'man') {
				o.attr('value',iskey);
				o.next().next().attr('value',isforeignkey);
			}
			else if(iskey != '') {
				o.attr('value',iskey);
				o.next().next().attr('value',isforeignkey);
			}
			o.next().remove();
		}
				
		/* run script (NOT arrows and enter) */	

		if(keyCode != 40 && keyCode != 38 && keyCode != 13 && o.attr('value').length >= minchar )
			$.run(script,'ref=' + ref + '&needle=' + o.attr('value') );
}

function attachSuggestion(ref, script, ismandatory, minchar) {

	/* ref only or editor id and ref*/

	var refa = ref.split(" ");

	if(refa[1] == undefined) {
		eid = '';
		ref = refa[0];
	} else {
		eid = refa[0];
		ref = refa[1];
	}
	
	/* set default values */

	if(ismandatory == undefined)
		ismandatory = 'not';
	
	if(minchar == undefined)
		minchar = 2;

	/* keydown - build suggestion container */	

	$(eid + ' input[ref=' + ref + ']').unbind('keydown.suggestion').bind('keydown.suggestion', function(e) {
		attachSuggestion_keydown($(this),e,ref);
	});

	$(eid + ' input[ref=' + ref + ']').prev().unbind('keydown.suggestion').bind('keydown.suggestion', function(e) {
		attachSuggestion_keydown($(this),e,ref);
	});

	/* blur - destroy suggestion container */

	$(eid + ' input[ref=' + ref + ']').unbind('blur.suggestion').bind('blur.suggestion', function() {
		attachSuggestion_blur($(this),ismandatory,'');
	});

	$(eid + ' input[ref=' + ref + ']').prev().unbind('blur.suggestion').bind('blur.suggestion', function() {
		attachSuggestion_blur($(this),ismandatory,'foreignkey');
	});

	/* keyup */

	$(eid + ' input[ref=' + ref + ']').unbind('keyup.suggestion').bind("keyup.suggestion", function(e) {
		attachSuggestion_keyup($(this),e,ismandatory,ref,minchar,script,'');
	});

	$(eid + ' input[ref=' + ref + ']').prev().unbind('keyup.suggestion').bind("keyup.suggestion", function(e) {
		attachSuggestion_keyup($(this),e,ismandatory,ref,minchar,script,'foreignkey');
	});
	
}	/*
	
	callTableOf
	+ links tableOf with field
	
	linkTableOf
	+ links tableOf with field
	+ relinks tableOf with field after "add"
	- uses callTableOf
	- uses setTimeout
	
	linkTableOf is OBSOLETE (editor reloads jscript after add in callback manner
	
	*/
	
	function callTableOf(tableof,editorid,ref)
	{
		$('#'+editorid+' input[ref='+ref+']').unbind();
		$("#"+editorid+" input[ref="+ ref +"]").click( function() {
			/* $(this).hide(); */
			$(this).attr("disabled", true);
			$('#'+tableof).clone(true).insertAfter(this).show();	
		});
	}	
	
	function linkTableOf(tableof,editorid,ref)
	{		
		callTableOf(tableof,editorid,ref);
		$('#'+editorid+' a[menubutton=add]').click( function() {
			setTimeout("callTableOf('" + tableof + "','" + editorid + "','" + ref + "')",500);		
		});
	}
	

function attachTableOf(tableof,editorid,ref,free) {

	/* make ref from ref */

	var prev = '';
	ref = editorid + ' input'+ '[ref='+ ref +']';

	/* check if input has foreignkey */

	if( $(ref).attr('foreignkey') ) { 
		prev = ref;
		ref = $(ref).prev();
	}
		
	/* backward transparency */
	
	if(free == undefined) 
		free = 'no';

	/* click : unbind tableof */

	$(ref).unbind('click.tableof');
	
	/* click : bind tableof */
	
	$(ref).bind('click.tableof', function() {
		$('#'+tableof).clone(true).insertAfter(this).show();
	});

	/* mark first and last */
	
	$('#' + tableof + ' div:first').attr('limitary','first');
	$('#' + tableof + ' div:last').attr('limitary','last');

	/* keydown : bind */
	
	$(ref).bind("keydown.tableof", function(e) {
		var keyCode = e.keyCode || window.event.keyCode;
		if(free != 'free' && keyCode != 9)
			e.preventDefault();
	});	

	/* keyup : unbind */

	$(ref).unbind('keyup.tableof');

	/* keyup : bind */

	$(ref).bind("keyup.tableof", function(e) {
		var keyCode = e.keyCode || window.event.keyCode;
		
		/* show tableof on first down arrow (40) */
		
		if(keyCode == 40 && $(this).next().attr('class') != 'tableOf' ) {
			$('#'+tableof).clone(true).insertAfter(this).show();
			$('#' + tableof + ' div:first').addClass('tableOfSelected');
		} 			
		
		/* select next (40) / previous (38) */

		else if(keyCode == 40) {
			$('#' + tableof + ' > div[class=tableOfSelected][limitary!=last]').removeClass('tableOfSelected').next().addClass('tableOfSelected');
		}
		else if(keyCode == 38) {
			$('#' + tableof + ' > div[class=tableOfSelected][limitary!=first]').removeClass('tableOfSelected').prev().addClass('tableOfSelected');	
		}

		/* select on enter (13) */
		
		else if(keyCode == 13 && prev == '') {
			var span = $('#' + tableof + ' > div[class=tableOfSelected]').find('span').html();
			$(this).parent().find('input').attr( { value : span } );
			$(editorid+' #' + tableof).remove();
		}
		else if(keyCode == 13 && prev != '') {

			var span = $('#' + tableof + ' > div[class=tableOfSelected]').find('span:eq(0)').html();
			$(this).parent().find('input:eq(1)').attr( { value : span } );
			var span = $('#' + tableof + ' > div[class=tableOfSelected]').find('span:eq(1)').html();
			$(this).parent().find('input:eq(0)').attr( { value : span } );		
			
			$(editorid+' #' + tableof).remove();
		}

		/* close tableof on esc (27) */

		else if(keyCode == 27) {
			$(editorid+' #' + tableof).remove();
		}
			
	});

}function chapterof2(page) {

	/* set indeks and cookie */
	
	var i = 0;
	$.each( $('[chapterof]'), function() {
		$(this).attr('indeks',i);
		if( !getCookie(page + i) )	
			setCookie(page + i,'closed',14);
	
		
	
		if( getCookie( page + i ) == 'closed' || !getCookie(page + i) )
						switch( $(this).attr('chapterof') ) {
			case '4':
				$(this).next().next().next().next().hide();
			case '3':
				$(this).next().next().next().hide();
			case '2':
				$(this).next().next().hide();
			case '1':
				$(this).next().hide();	
		};
	
		i = i + 1;
	});



	$('[chapterof]').unbind('click').click( function() {

		if( getCookie( page + $(this).attr('indeks') ) == 'opened' )
			setCookie( page + $(this).attr('indeks'), 'closed', 14 );
		else
			setCookie( page + $(this).attr('indeks'), 'opened', 14 );
		
		switch( $(this).attr('chapterof') )
		{
			case '4':
				$(this).next().next().next().next().toggle();
			case '3':
				$(this).next().next().next().toggle();
			case '2':
				$(this).next().next().toggle();
			case '1':
				$(this).next().toggle();	
		}			

	});
}function divLoad() {
	$('#page div[load]').each( function() {
		$.run( $(this).attr('load') );
	});
}/* 
editorNUMBER.js -> functionNUMBER()

- addNewRow8
- bindMarking8
- saveForm8
- remove_empty_rows8
*/

function saveForm13(homepage, editorId, rules, page) { 

	editorId = editorId.replace(/#/g, "");
	editorId = '#' + editorId;

	remove_empty_rows13(editorId);

	var v = validate(editorId,rules);
					
	if( v['result'] === true )
		$.run(homepage, v['serialized']);
	else
		msg('Validation failed.','red');
	
}

/*
	remove_empty_rows(str)
	works with <tr datarow="yes">
*/

function remove_empty_rows13(editorId) {
			
	$.each($(editorId).find("tr[datarow=yes]"), function() {
		
		var remove = 'yes';
		var row = $(this);
		
		$.each( $(this).find('input[type=text]'), function() {
			if($(this).attr('value') != '' && $(this).attr('value') != undefined && $(this).attr('value') != '0')
				remove = 'no';
		});
		
		$.each( $(this).find('textarea'), function() {
			if($(this).attr('value') != '' && $(this).attr('value') != undefined && $(this).attr('value') != '0')
				remove = 'no';
		});
			
		if(remove == 'yes') {
			
			var rowid = $(row).attr('rowid');
			var todelete = $(editorId + ' #todelete').attr('value');			

			if(	todelete.match(',' + rowid + ',') != ',' + rowid + ',' && rowid != undefined)
				$(editorId + ' #todelete').attr('value', todelete + rowid + ',' );

			$(row).remove();
		}

	});
}


function isrighta13(styleString) {
	var lookfor = 'text-align: right;';
	if( styleString.search(lookfor) != -1 )
		return ' ' + lookfor;
	return '';
}

/*
copy input and textarea
sets background-color: #FFFFCC;
copy readonly 										(input only)
copy text-align: right; 					(input only)
*/


function addNewRow13(editorid,objSpan) { 
	var tds = '';

	// take td form prev th		
	$.each( $(objSpan).parent().parent().parent().prev().find('td'), function() {
	
	//  if there is input with id 		
	if( prev = $(this).find('input').attr('id') )
	{
		// !!! DOTS are not allowed, becouse
		prev = prev.split(".");
		// set index for inputs in new row (this should be done only one, but it does not matter)
		if(prev[1] == 'new') 
			cIndex = '.new.' + (++prev[2]); 
		else 
			cIndex = '.new.1';
		// set text-align: right;
		var righta = isrighta13( $(this).find('input').attr('style') );
		
		var isreadonly = $(this).find('input').attr('readonly') === true ? ' readonly' : '';
			
		// build input inside td					
		tds = tds + '<td><input type="text" ref="'+prev[0]+'" name="'+prev[0]+cIndex+'" id="'+prev[0]+cIndex+'" style="background-color: #FFFFCC;' + righta + '"' + isreadonly + ' value=""></td>';
	}
	else if( prev = $(this).find('textarea').attr('id') )
	{
		// !!! DOTS are not allowed, becouse
		prev = prev.split(".");
		// set index for inputs in new row (this should be done only one, but it does not matter)
		if(prev[1] == 'new') 
			cIndex = '.new.' + (++prev[2]); 
		else 
			cIndex = '.new.1';
		// set text-align: right;	
		tds = tds + '<td><textarea type="text" ref="'+prev[0]+'" name="'+prev[0]+cIndex+'" id="'+prev[0]+cIndex+'" rows="' + $(this).find('textarea').attr('rows') + '" style="background-color: #FFFFCC;"></textarea></td>';
	}
	else tds = tds + '<td class="' + $(this).attr('class') + '"></td>';
		
	});
	$(objSpan).parent().parent().parent().before('<tr datarow="yes">'+tds+'</tr>');
	
	bindMarking13(editorid);

	/* editorjscript(); */

  var MyFuncName= 'jscript' + editorid.replace(/#/g,''); 
  var MyRealFunc = eval(MyFuncName);
  MyRealFunc(); 


}

/*
+ works with NOT readonly inputs,
+ focus event,
+ and light yellow (#FFFFCC)
+ stores changed field row in haschanged hiddon input
*/

function bindMarking13(editorid) {
	$('#page ' + editorid + ' input:not([readonly]), #page ' + editorid + ' textarea').unbind("focus").bind('focus',  function() {	
		$(this).css("background-color","#FFFFCC");

		var rowid =  getRowId($(this));
		
		var haschanged = $(editorid + ' #haschanged').attr('value');
		if(	haschanged.match(',' + rowid + ',') != ',' + rowid + ',' && rowid != undefined)
			$(editorid + ' #haschanged').attr('value', haschanged + rowid + ',' );
	});
}

/* 
This one meant to be used in conjunction with not standard editor menu buttons which provides operation on one or multiple rows.
Requires editor id.
Returns coma separated id of checked rows.
 */ 

function getCheckedRows13(editorid) {
	var checkedrows = '';
	$.each($(editorid).find("tr[datarow=yes]"), function() {
		if($(this).find('input[ref=rowcheckbox]').attr('checked') != false) {
			checkedrows += ',' + getRowId($(this));
		}
	});
	return checkedrows.substring(1);
}/* 
editorNUMBER.js -> functionNUMBER()

- addNewRow8
- bindMarking8
- saveForm8
- remove_empty_rows8
*/

function saveForm14(homepage, editorId, rules, page) { 

	editorId = editorId.replace(/#/g, "");
	editorId = '#' + editorId;

	remove_empty_rows14(editorId);

	var v = validate(editorId,rules);
					
	if( v['result'] === true )
		$.run(homepage, v['serialized']);
	else
		msg('Validation failed.','red');
	
}

/*
	remove_empty_rows(str)
	works with <tr datarow="yes">
*/

function remove_empty_rows14(editorId) {
			
	$.each($(editorId).find("tr[datarow=yes]"), function() {
		
		var remove = 'yes';
		var row = $(this);
		
		$.each( $(this).find('input[type=text]'), function() {
			if($(this).attr('value') != '' && $(this).attr('value') != undefined && $(this).attr('value') != '0' && $(this).attr('value') != '0.00')
				remove = 'no';
		});
		
		$.each( $(this).find('textarea'), function() {
			if($(this).attr('value') != '' && $(this).attr('value') != undefined && $(this).attr('value') != '0')
				remove = 'no';
		});
			
		if(remove == 'yes') {
			
			var rowid = $(row).attr('rowid');

			var todelete = $(editorId + ' input[name=todelete]').attr('value');			

			if(	todelete.match(',' + rowid + ',') != ',' + rowid + ',' && rowid != undefined )
				$(editorId + ' input[name=todelete]').attr('value', todelete + rowid + ',');

			$(row).remove();
		}

	});
}


function isrighta14(styleString) {
	var lookfor = 'text-align: right;';
	if( styleString.search(lookfor) != -1 )
		return ' ' + lookfor;
	return '';
}

/*
copy input and textarea
sets background-color: #FFFFCC;
copy readonly 										(input only)
copy text-align: right; 					(input only)
*/

function addNewRow14(editorid,objSpan) { 
	var tds = '';

	/* take td form prev th */
	$.each( $(objSpan).parent().parent().parent().prev().find('td'), function() {
	
	/* if there is input with id */
	if( prev = $(this).find('input[foreignkey]').attr('id') ) {
		/* !!! DOTS are not allowed, becouse */
		prev = prev.split(".");
		/* set index for inputs in new row (this should be done only one, but it does not matter) */
		if(prev[1] == 'new')
			cIndex = '.new.' + (++prev[2]); 
		else 
			cIndex = '.new.1';
		/* set text-align: right; */
		var righta = isrighta14( $(this).find('input').attr('style') );
		
		var isreadonly = $(this).find('input').attr('readonly') === true ? ' readonly' : '';
			
		/* build input inside td */
		tds = tds + '<td>';
		tds = tds + '<input type="text" style="background-color: #FFFFCC;' + righta + '"' + isreadonly + ' value="">';
		tds = tds + '<input type="hidden" foreignkey="yes" ref="'+prev[0]+'" name="'+prev[0]+cIndex+'" id="'+prev[0]+cIndex+'" style="background-color: #FFFFCC;' + righta + '"' + isreadonly + ' value="">';
		tds = tds + '</td>';

	} else if( prev = $(this).find('input').attr('id') ) {

		/* !!! DOTS are not allowed, becouse */
		prev = prev.split(".");
		/* set index for inputs in new row (this should be done only one, but it does not matter) */
		if(prev[1] == 'new') 
			cIndex = '.new.' + (++prev[2]); 
		else 
			cIndex = '.new.1';
		/* set text-align: right; */
		var righta = isrighta14( $(this).find('input').attr('style') );
		
		var isreadonly = $(this).find('input').attr('readonly') === true ? ' readonly' : '';
			
		/* build input inside td */
		tds = tds + '<td>';
		tds = tds + '<input type="text" ref="'+prev[0]+'" name="'+prev[0]+cIndex+'" id="'+prev[0]+cIndex+'" style="background-color: #FFFFCC;' + righta + '"' + isreadonly + ' value="">';
		tds = tds + '</td>';
		
	}
	else if( prev = $(this).find('textarea').attr('id') )
	{
		/* !!! DOTS are not allowed, becouse */
		prev = prev.split(".");
		/* set index for inputs in new row (this should be done only one, but it does not matter) */
		if(prev[1] == 'new') 
			cIndex = '.new.' + (++prev[2]); 
		else 
			cIndex = '.new.1';
		/* set text-align: right;	*/
		tds = tds + '<td><textarea type="text" ref="'+prev[0]+'" name="'+prev[0]+cIndex+'" id="'+prev[0]+cIndex+'" rows="' + $(this).find('textarea').attr('rows') + '" style="background-color: #FFFFCC;"></textarea></td>';
	}
	else tds = tds + '<td class="' + $(this).attr('class') + '"></td>';
		
	});
	$(objSpan).parent().parent().parent().before('<tr datarow="yes">'+tds+'</tr>');
	
	bindMarking14(editorid);

	/* editorjscript(); */

  var MyFuncName= 'jscript' + editorid.replace(/#/g,''); 
  var MyRealFunc = eval(MyFuncName);
  MyRealFunc(); 


}

/*
+ works with NOT readonly inputs,
+ focus event,
+ and light yellow (#FFFFCC)
+ stores changed field row in haschanged hiddon input
*/

function bindMarking14(editorid) {
	$('#page ' + editorid + ' input:not([readonly]), #page ' + editorid + ' textarea').unbind("focus").bind('focus',  function() {	
		$(this).css("background-color","#FFFFCC");

		var rowid =  getRowId($(this));
		
		var haschanged = $(editorid + ' input[name=haschanged]').attr('value');
		
		if(	haschanged.match(',' + rowid + ',') != ',' + rowid + ',' && rowid != undefined)
			$(editorid + ' input[name=haschanged]').attr('value', haschanged + rowid + ',' );
	});
}

/* 
This one meant to be used in conjunction with not standard editor menu buttons which provides operation on one or multiple rows.
Requires editor id.
Returns coma separated id of checked rows.
 */ 

function getCheckedRows14(editorid) {
	var checkedrows = '';
	$.each($(editorid).find("tr[datarow=yes]"), function() {
		if($(this).find('input[ref=rowcheckbox]').attr('checked') != false) {
			checkedrows += ',' + getRowId($(this));
		}
	});
	return checkedrows.substring(1);
}/* 
editorNUMBER.js -> functionNUMBER()

- addNewRow8
- bindMarking8
- saveForm8
- remove_empty_rows8
*/

function saveForm15(homepage, editorId, rules, page) { 

	editorId = editorId.replace(/#/g, "");
	editorId = '#' + editorId;

	remove_empty_rows15(editorId);

	var v = validate(editorId,rules);
						
	if( v['result'] === true )
		$.run(homepage, v['serialized']);
	else
		msg('Validation failed.','red');
	
}

/*
	remove_empty_rows(str)
	works with <tr datarow="yes">
*/

function remove_empty_rows15(editorId) {
			
	/* $.each($(editorId).find("tr[datarow=yes]"), function() { */
	$.each($(editorId).find("[datarow=yes]"), function() {		
		
		var remove = 'yes';
		var row = $(this);
		
		$.each( $(this).find('input[type=text]'), function() {
			if($(this).attr('value') != '' && $(this).attr('value') != undefined && $(this).attr('value') != '0' && $(this).attr('value') != '0.00')
				remove = 'no';
		});
		
		$.each( $(this).find('textarea'), function() {
			if($(this).attr('value') != '' && $(this).attr('value') != undefined && $(this).attr('value') != '0')
				remove = 'no';
		});
			
		if(remove == 'yes') {
			
			var rowid = $(row).attr('rowid');

			var todelete = $(editorId + ' input[name=todelete]').attr('value');			

			if(	todelete.match(',' + rowid + ',') != ',' + rowid + ',' && rowid != undefined )
				$(editorId + ' input[name=todelete]').attr('value', todelete + rowid + ',');

			$(row).remove();
		}

	});
}


function isrighta15(styleString) {
	var lookfor = 'text-align: right;';
	if( styleString.search(lookfor) != -1 )
		return ' ' + lookfor;
	return '';
}

/*
copy input and textarea
sets background-color: #FFFFCC;
copy readonly 										(input only)
copy text-align: right; 					(input only)
*/

function addNewRow15(editorid,objSpan) { 
	var tds = '';

	/* take td form prev th */
	$.each( $(objSpan).parent().parent().parent().prev().find('td'), function() {
	
	/* if there is input with id */
	if( prev = $(this).find('input[foreignkey]').attr('id') ) {
		/* !!! DOTS are not allowed, becouse */
		prev = prev.split(".");
		/* set index for inputs in new row (this should be done only one, but it does not matter) */
		if(prev[1] == 'new')
			cIndex = '.new.' + (++prev[2]); 
		else 
			cIndex = '.new.1';
		/* set text-align: right; */
		var righta = isrighta15( $(this).find('input').attr('style') );
		
		var isreadonly = $(this).find('input').attr('readonly') === true ? ' readonly' : '';
			
		/* build input inside td */
		tds = tds + '<td>';
		tds = tds + '<input type="text" style="background-color: #FFFFCC;' + righta + '"' + isreadonly + ' value="">';
		tds = tds + '<input type="hidden" foreignkey="yes" ref="'+prev[0]+'" name="'+prev[0]+cIndex+'" id="'+prev[0]+cIndex+'" style="background-color: #FFFFCC;' + righta + '"' + isreadonly + ' value="">';
		tds = tds + '</td>';

	} else if( prev = $(this).find('input').attr('id') ) {

		/* !!! DOTS are not allowed, becouse */
		prev = prev.split(".");
		/* set index for inputs in new row (this should be done only one, but it does not matter) */
		if(prev[1] == 'new') 
			cIndex = '.new.' + (++prev[2]); 
		else 
			cIndex = '.new.1';
		/* set text-align: right; */
		var righta = isrighta15( $(this).find('input').attr('style') );
		
		var isreadonly = $(this).find('input').attr('readonly') === true ? ' readonly' : '';
			
		/* build input inside td */
		tds = tds + '<td>';
		tds = tds + '<input type="text" ref="'+prev[0]+'" name="'+prev[0]+cIndex+'" id="'+prev[0]+cIndex+'" style="background-color: #FFFFCC;' + righta + '"' + isreadonly + ' value="">';
		tds = tds + '</td>';
		
	}
	else if( prev = $(this).find('textarea').attr('id') )
	{
		/* !!! DOTS are not allowed, becouse */
		prev = prev.split(".");
		/* set index for inputs in new row (this should be done only one, but it does not matter) */
		if(prev[1] == 'new') 
			cIndex = '.new.' + (++prev[2]); 
		else 
			cIndex = '.new.1';
		/* set text-align: right;	*/
		tds = tds + '<td><textarea type="text" ref="'+prev[0]+'" name="'+prev[0]+cIndex+'" id="'+prev[0]+cIndex+'" rows="' + $(this).find('textarea').attr('rows') + '" style="background-color: #FFFFCC;"></textarea></td>';
	}
	else tds = tds + '<td class="' + $(this).attr('class') + '"></td>';
		
	});
	$(objSpan).parent().parent().parent().before('<tr datarow="yes">'+tds+'</tr>');
	
	bindMarking15(editorid);

	/* editorjscript(); */

  var MyFuncName= 'jscript' + editorid.replace(/#/g,''); 
  var MyRealFunc = eval(MyFuncName);
  MyRealFunc(); 


}

/*
+ works with NOT readonly inputs,
+ focus event,
+ and light yellow (#FFFFCC)
+ stores changed field row in haschanged hiddon input
*/

function bindMarking15(editorid) {
	$('#page ' + editorid + ' input:not([readonly]), #page ' + editorid + ' textarea').unbind("focus").bind('focus',  function() {	
		$(this).css("background-color","#FFFFCC");

		var rowid =  getRowId($(this));
		var haschanged = $(editorid + ' input[name=haschanged]').attr('value');
		
		if(	haschanged.match(',' + rowid + ',') != ',' + rowid + ',' && rowid != undefined)
			$(editorid + ' input[name=haschanged]').attr('value', haschanged + rowid + ',' );
	});
}

/* 
This one meant to be used in conjunction with not standard editor menu buttons which provides operation on one or multiple rows.
Requires editor id.
Returns coma separated id of checked rows.
 */ 

function getCheckedRows15(editorid) {
	var checkedrows = '';
	$.each($(editorid).find("tr[datarow=yes]"), function() {
		if($(this).find('input[ref=rowcheckbox]').attr('checked') != false) {
			checkedrows += ',' + getRowId($(this));
		}
	});
	return checkedrows.substring(1);
}function getRowId(element) {


		var rowid = $(element).attr('rowid');

		if(rowid == undefined)		
			rowid = $(element).parent().attr('rowid');

		if(rowid == undefined)
			rowid = $(element).parent().parent().attr('rowid');

		if(rowid == undefined)
			rowid = $(element).parent().parent().parent().attr('rowid');

		if(rowid == undefined)
			return false;
		else
			return rowid;
}function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}/*
- only script executed on page load
- expand / colapse #message
-
*/

$(document).ready(function(){

  $('#message').html('');
	$('#pageMenu').html('');
	
	/* $.run('page/gate/gate.php'); */
	
	
	/* $.run('page.header/navalmar_contact.php'); */

	$.run('page.menu/header.php');	
	$.run('page.menu/menu.php');
  
	/* expand / colapse #message */

	$('#message').toggle( function () {
		$(this).css( { minHeight : '100px', overflow: 'auto' } );
	}, function () {
		$(this).css( { minHeight : '14px', overflow: 'hidden' } );
	});

});function insertCurrentDate(element) { 		
	$(element).unbind().click( function() {	
		var t = new Date(); 
		var y = t.getFullYear(); 
		var m = t.getMonth() + 1; 
		if(m < 10) m = '0'+m; 
		var d = t.getDate();  
		if(d < 10) d = '0'+d;
		if($(this).attr("value") == undefined)
		$(this).attr("value",y+'-'+m+'-'+d);
	});
}

function insertCurrentDateTime(element) {
	$(element).unbind().click( function() {	
		var t = new Date(); 
		var y = t.getFullYear(); 
		var m = t.getMonth() + 1; 
		if(m < 10) m = '0'+m; 
		
		var hs = t.getHours();
		if(hs < 10) hs = '0'+hs; 
		
		var ms = t.getMinutes();
		if(ms < 10) ms = '0'+ms;
		
		var d = t.getDate();  
		if(d < 10) d = '0'+d;
		if($(this).attr("value") == '')
			$(this).attr("value",y+'-'+m+'-'+d+' '+hs+':'+ms);
	});
}

function insertCurrentDateTimeWithAdd(editorId, fieldRef, withAdd) {
		
	$(editorId + ' ' + fieldRef).unbind().click( function() {	
		var t = new Date(); 
		var y = t.getFullYear(); 
		var m = t.getMonth() + 1; 
		if(m < 10) m = '0'+m; 
		
		var hs = t.getHours();
		if(hs < 10) hs = '0'+hs; 
		
		var ms = t.getMinutes();
		if(ms < 10) ms = '0'+ms;
		
		var d = t.getDate();  
		if(d < 10) d = '0'+d;
		if($(this).attr("value") == '')
			$(this).attr("value",d+'/'+m+'/'+y+' '+hs+':'+ms);
	});

	if(withAdd == undefined)
		$(editorId + ' a[menubutton=add]').click( function() {
			setTimeout("insertCurrentDateTimeWithAdd('" + editorId + "','" + fieldRef + "', 'no')",498);
		});

}function listingSelect(e,v,s) {
	$(e + ' tr[datarow]').unbind('hover').hover( function() {
		$(this).css("cursor", "pointer");
		$(this).find('td:not(:eq(0))').css("background-color","#F5F5F5");
	}, function() {
		$(this).find('td:not(:eq(0))').css("background-color","white");
	});

	if(s != undefined)
		$(e + ' tr[datarow]').click( function() {
			$.run(s,v + '=' + getRowId($(this)));
		});

}function msg(content, color)
{ 
	if(content != undefined)
	{
		color = color == undefined ? '' : ' style="color: '+ color +'"'; 
		$('#message').prepend( '<div ' + color + '>' + content + '</div>' );

	}
	else
	{ 
		$('#page span[class=log]').each( function() {
			alert('!' + $(this).attr('message'));
			$('#message').prepend( $(this).attr('message') );
			$(this).remove();
		});
	}
}function simpleTabs(eid,open) {

	$('#' + eid + ' > span').addClass('tabhead');
	$('#' + eid + ' > div').addClass('tabbody');

	/* load tab contentnt */
	/* highlight tab */

	$('#' + eid +' span.tabhead').unbind('click').click( function() {
		$('#' + eid + ' > div').empty();
		$.run($(this).attr('link'), 'tab=' + eid);
		$('#' + eid +' > span').removeClass('tabactive').addClass('tabhead');
		$(this).removeClass('tabhead').addClass('tabactive');
	});
	
	if(open != undefined) {
		if(open * 1 == open)
			$('#' + eid + ' > span:eq(' + (open * 1 - 1) + ')').click();
		else
			$('#' + eid + ' > span[ref=' + open + ']').click();
	}
}function updateTablesOf() { 
	$.run('skeleton/tableof.php');
}function uploadFile(uploadScript,returnToScript,buttonRef) {

	if(buttonRef == undefined)
		buttonRef = 'buttonUpload';
	
$('a[ref=' + buttonRef + ']').click( function() {
	
		var inputFileId = $(this).parent('form').find('[input[type=file]').attr('id');
	
	
		$("#loading").ajaxStart(function(){
			$(this).show();
		}).ajaxComplete(function(){
			$(this).hide();
		});

		$.ajaxFileUpload
		(
			{
				url: uploadScript, 
				secureuri:false,
				fileElementId: inputFileId,
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
						}else
						{
							/* alert(data.msg); */
							$.run(returnToScript);
						}
					}
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		);
		
		return false;
});

};/*
vaidateform.js

1. usage example:

rules = array(
	'fieldName1,man',
	'fieldName2,not',
	'fieldName3,man,minlength=3',
	'fieldName4,not,numeric,minvalue=10'
);

var v = validate('#formId',rules);

if( v['result'] === false ) alert('errors!');

! 1st must be field id
! 2nd must be "man" or "not"

2. rules:

+ someas			= fieldId
+ numeric
+ minlength 	= number
+ maxlength 	= number
+ minvalue 		= number
+ maxvalue 		= number
+ money				(1,000,000.00)

+ alphabet
+ alphanumeric
+ text
+ email
+ yyyy-mm-dd hh:mm
+ yyyy-mm-dd

- positive
- dd/mm/yyyy

2. dependances:

+ minvalue, maxvalue, positive -> numeric

3. password check

	'PasswordField1,not,minlength=2',
	'PasswordField2,not,someas=PasswordField1'

4. field has to have "id" attr

5. "name" attr is added for jquery .serialize()

6. error background #FF4500 otherwise white

7. works with "testarea" and "input" (but not button) fields 

8. returns array: errorsNo, serialized, result (true/false)

*/

/* 
addName
+ takes (#) form id 
+ adds "name" attr to "input" and "textareas" with value of "id"
*/

function addName(formId)
{ 
	jQuery.each($(formId + ' input[type!=button], ' + formId + ' textarea'), function(i, n) {
  	var id = $(this).attr('id');
    $(this).attr("name", id );
   });
}


function checkRule(v,r)
{
	var e = 'ok';
	var r = r.split('=');

	if(r[0] == 'someas') 
		if(!(v == document.getElementById(r[1]).value))
			e = ''; 

	if(r[0] == 'minlength')
		if(v.length < r[1])
			e = ''; 
	if(r[0] == 'maxlength')
		if(v.length > r[1])
			e = ''; 

	if(r[0] == 'minvalue')
		if(v * 1 < r[1] * 1)
			e = '';  

	if(r[0] == 'maxvalue')
			if(v * 1> r[1] * 1)
				e = ''; 

	if(r[0] == 'moneyg')
  {
  	var m = v.replace(/\,/g, "");
    if(!(m.match(/^\d*\.?\d*$/)))
    e = '';
  }

	if(r[0] == 'money')
  {
  	var m = v.replace(/\,/g, "");
    if(!(m.match(/^(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/)))
    e = '';
  }

	if(r[0] == 'numeric' || r[0] == 'maxvalue' || r[0] == 'minvalue' || r[0] == 'positive')
		if(!(v.match(/^[-]?\d*\.?\d*$/)))
			e = ''; 

	if(r[0] == 'alphabet')
		if(!(v.match(/^[a-zA-Z]+$/)))
			e = ''; 	

	if(r[0] == 'alphanumeric')
		if(!(v.match(/^[0-9a-zA-Z]+$/)))
			e = ''; 	

	if(r[0] == 'text')
		if(!(v.match(/^[0-9a-¿A-¯\s,.!?]+$/)))
			e = ''; 	


	if(r[0] == 'email')
		if(!(v.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/)))
			e = ''; 	

	if(r[0] == 'yyyy')
		if(!(v.match(/(19|20)\d\d/)) || v.length != 4)
			e = '';

	if(r[0] == 'yyyy-mm-dd hh:mm')
		if(!(v.match(/(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[ ]([0-1][0-9]|[2][0-3])[:]([0-5][0-9])/)) || v.length != 16)
			e = ''; 

	if(r[0] == 'mm/dd/yyyy hh:mm')
		if(!(v.match(/(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d[ ]([0-1][0-9]|[2][0-3])[:]([0-5][0-9])/)) || v.length != 16)
			e = ''; 

	if(r[0] == 'dd/mm/yyyy hh:mm')
		if(!(v.match(/(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d[ ]([0-1][0-9]|[2][0-3])[:]([0-5][0-9])/)) || v.length != 16)
			e = ''; 

	if(r[0] == 'yyyy-mm-dd')
		if(!(v.match(/(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])/)))
			e = ''; 

	if(r[0] == 'mm/dd/yyyy')
		if(!(v.match(/(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d/)))
			e = ''; 

	if(r[0] == 'dd/mm/yyyy')
		if(!(v.match(/(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d/)))
			e = ''; 

	if(e == 'ok')
		return 0;
	else
		return 1;
}

function validateField(id,value,rules) { 
	rules = rules.split(',')
	rules.shift();
	var mandatory = rules.shift();

	var errors = 0;

	if(value.length > 0)
	{ 
		for (x in rules)
			errors = errors + checkRule(value,rules[x]);
	}
	else if(value.length == 0 && mandatory == 'man')
		errors++;

	if(errors == 0 && document.getElementById(id).style.backgroundColor != 'rgb(255, 69, 0)')
	{
		document.getElementById(id).style.backgroundColor = 'white';
	}
	else
	{	
 		document.getElementById(id).style.backgroundColor = '#FF4500';
	}

 	/* (try to) mark foreign key (if exists) */

	if(errors == 0)
		$('[id="' + id + '"]').prev().css('background-color','white');
	else
 		$('[id="' + id + '"]').prev().css('background-color','#FF4500');

	return errors;
}


function validate(formId,rules) {

	if( typeof(xrr) == "undefined")
		xrr = 0;

	addName(formId);

	var err = 0 + xrr;

	$.each( $(formId + ' input[type!=button], ' + formId + ' textarea'), function() {
		var fieldId = $(this).attr('id');
		
		if(fieldId != undefined)
		{
			var fieldValue = $(this).attr('value');
		
			fieldValue = fieldValue == undefined ? '' : fieldValue;
		
			var fieldRule = fieldId.split(".");
			fieldRule = fieldRule[0];

			$.each( rules, function() {
				var rule = this.split(',');
				rule = rule[0];
				if(rule == fieldRule) 
					err = err + validateField(fieldId,fieldValue,this);
			});
		}
	});

	
	var r = new Array()
	
	r['errorsNo'] = err;
	r['serialized'] = $(formId + ' input[type!=button], ' + formId + ' textarea').serialize();
	r['result'] = err == 0 ? true : false;
	
	return r;
}

function xalidate_rule(editorid,rules) {

	var keyref = rules.shift();
	var ref = rules[0];
		
	keyref = keyref.split("=");
	keyref = '[ref=' + keyref[0] + '][value=' + keyref[1] + ']';

	$.each( $(editorid + ' ' + keyref), function() {

		if( $(this).parent().parent().find('input[type=checkbox]').attr('style') == undefined ) {
			var id = $(this).parent().parent().find('[ref=' + ref + ']').attr('id');
			var value = $(this).parent().parent().find('[ref=' + ref + ']').attr('value');
			xrr = xrr + validateField(id,value,rules.toString());
		}
	});
}

function xalidate(editorid,rules) {

	xrr = 0;
	var rules = rules.split(";");

	$.each( rules, function() {
		var rule = this.split(',');
		xalidate_rule(editorid,rule);
	});

}