String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g, ''); 
}

String.prototype.ltrim = function() {
    return this.replace(/^\s+/,'');
}

String.prototype.rtrim = function() {
    return this.replace(/\s+$/,'');
}

function openNewWindow(url)
{
	openNewWindow(url, 480, 120, 'no', 'no');
} 

function openNewWindow(url, x, y, scrollbars, resizable)
{
 	 aPopUp = window.open('','VALLIST');	
 	 aPopUp.close();
 	 
 	 var left = (screen.availWidth/2) - (x/2);
	 var top = (screen.availHeight/2) - (y/2);

     aPopUp= self.window.open(url,'VALLIST','toolbar=no,location=no,directories=no,status=yes,scrollbars='+scrollbars+',resizable='+resizable+', copyhistory=no,width='+x+',height='+y+',top='+top+',left='+left);
     self.aNoteWin = aPopUp;
} 

function closeAndRefresh() 
{
	window.opener.location.reload();
	window.close();
}


//***********************************************************************************/
function setAction(formName, action){
	form = document.forms[formName];
	form["F_ACTION"].value = action;
}

function setField(formName, fieldName, fieldValue){
	document.forms[formName].elements[fieldName].value = fieldValue;
}

function submitForm(formName, action){
	if( !window.validate || validate() ) {
		if (action=='DELETE'||action=='DELETE_ITEM'){
			if (confirm('Czy chcesz usunąć wybrany element?')) {
				document.forms[formName].submit();
			}
		}
		else {
			document.forms[formName].submit();
		}
	}
	else alert('proszę wypełnić formularz');
}

function submitFormWithHash(formName, action, hash){
	if(validate()) {
		document.forms[formName].action=document.forms[formName].action+hash;			
		document.forms[formName].submit();
	}
	else alert('proszę wypełnić formularz');
}

function setFieldAndSubmit(formName,fieldName, fieldValue) {
	setField(formName,fieldName, fieldValue);
	submitForm(formName);
}

function setAccept(formName){
	setField(formName, "F_ACCEPT_FORM", "TRUE");
}

function check(id,formName){
	form = document.forms[formName];
	currentSelected="currentSelected["+id+"].selected";
	form.elements[currentSelected].value = true;
}
function actionSubmit(formName, action){
	setAction(formName, action);
	submitForm(formName, action);
}
function actionSubmitWithHash(formName, action, hash){
	setAction(formName, action);
	submitFormWithHash(formName, action, hash);
}
function acceptSubmit (formName){
	setAccept(formName);
	submitForm(formName, '');
}

function acceptActionSubmit(formName,action){
	setAction(formName, action);
	setAccept(formName);
	submitForm(formName, action);
}
function setAcceptSubmit(id, formName){
	check(id, formName);
	setAccept(formName);
	submitForm(formName, action);
}
function checkAcceptSubmit(formName, action, id){
	check(id,formName);
	setAction(formName, action);
	setAccept(formName);
	submitForm(formName, action);
}
function checkActionSubmit(formName, action, id){
	check(id,formName);
	setAction(formName, action);
	submitForm(formName, action);
}
function setFieldActionSubmit(formName, fieldName, fieldValue, action){
	setField(formName,fieldName,fieldValue);
	setAction(formName, action);
	submitForm(formName, action);
}
function col(elem,ok) {
	if (ok) 
		{ 
		 elem.style.backgroundColor='#FFFFFF';
		 return true; 
		 }
	else elem.style.backgroundColor='#FFcc33'; 
		return false;
}
function focusOnLoad(fieldName){
	document.forms[0].elements[fieldName].focus();
}

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

function goToRecordClick(formName){
	form = document.forms[formName];
	currentPage = form.elements['pagingFilter.pageNr'].value;
	currentSelected = form.elements['pagingFilter.itemsOnPage'].selectedIndex;
	itemsOnPage = form.elements['pagingFilter.itemsOnPage'][currentSelected].value;
	recordNumber = form.elements['goToRecord'].value;
	
	pageNumber = 1;
	while ((pageNumber * itemsOnPage) < recordNumber) 
	{
		pageNumber++;
	}

	if ((pageNumber - 1) != currentPage ) 
	{
		pageNavigatorAction(formName, 'pagingFilter.pageNr', pageNumber-1); 
	}
}

/** 
	sortowanie po kolumnie 
**/
function sortByColumn(formName, column, order)
{

	form = document.forms[formName];
	form.elements['sortFilter.fieldName'].value = column;
	form.elements['sortFilter.sortOrder'].value = (order=='asc')?'true':'false';
	submitForm(formName);
}

/** 
	sortowanie po kolumnie z akcja 
**/

function sortByColumnAction(formName, column, order)
{
	form = document.forms[formName];
	setAction(formName, 'SORT');
	form.elements['sortFilter.fieldName'].value = column;
	form.elements['sortFilter.sortOrder'].value = (order=='asc')?'true':'false';
	submitForm(formName);
}

/**
	wybranie strony formularza
**/
function pageNavigatorAction(formName, formFieldName, selectedPage)
{
	document.forms[formName].elements[formFieldName].value = selectedPage;
	submitForm(formName);
}

/**
	zaznaczenie selecta
**/

function setSelect(selectId, value) 
{
	var sel = document.getElementById(selectId);
	for (var i=0; i< sel.length; i++) {
		if (sel.options[i].value==value) 
			sel.selectedIndex=i;
	}
}

