function removeLoadMask() {
	setTimeout('$j(\'#loading-box\').slideUp(\'fast\')', 3000);
	$j('#loading-mask').fadeOut(2000);
}

var timeout = {
	timeouts: new Array(),
	add: function(module, func, time) {
		this.timeouts.push(module)
	}
}

function show_node(id) {
	var block = $(id);
	if((block != undefined) && (block != null)) {
		block.style.display = 'block';
	}
}

function hide_node(id) {
	var block = $(id);
	if((block != undefined) && (block != null)) {
		block.style.display = 'none';
	}
}

var tableObject = {
	
	table: null,
	newrow: null,
	newcell: null,
	currentrow: null,
	
	init: function(table) {
		this.table = $(table);
		this.newrow = null;
	},
	
	insertRow: function(current) {
		if(this.table != null) {
			this.deleteRow();	
			this.currentrow = $('row_' + current);
			this.newrow = this.table.insertRow(this.currentrow.rowIndex+1);
			this.newrow.id = 'newrow';
			this.newrow.style.width = '100%';
			this.newcell = newrow.insertCell(0);
			this.newcell.id = 'newcell';
			this.newcell.colSpan = this.table.getElementsByTagName('th').length;
		}
	},
	
	deleteRow: function() {
		if(this.newrow != null) {
			this.table.deleteRow(this.newrow.rowIndex);
		}
		this.reset();
	},
	
	reset: function() {
		this.newrow = null;
		this.newcell = null;
		this.currentrow = null;
	},
	
	getCell: function() {
		return this.newcell;
	}
}

function parseScript(div) {
	if($(div) != undefined) {
		var re = /<script.*?>([\s\S]*?)<\//igm;
		var match;
		while(match = re.exec($(div).innerHTML)) {
			eval(match[1]);
		}
	}
}

veil = function(id) {
	this.id = id;
	this.disabled = false;
    this.show = function(){
		$j("#pageContent").append('<div id="veil" class="veil"></div>');
		$j("#veil").show();
		$j("#veil").fadeTo('slow', 0.4);
    };
    this.hide = function(){
		if(this.disabled == true) {
			alert('Error in page - contact support');
		} else {
			xajax_removeElement(this.id);
			$j('#page-veil').fadeOut("slow");
		}
    };
	this.disable = function() {
		this.disabled = true;	
	}
	return this.show();
}

var multiSelect = {
	table: null,
	selected: new Array(),
	start: null,
	
	setTable: function(table) {
		//if(this.table != table) {
			this.table = table;	
			this.selected = new Array();
		//}
	},
	
	select: function(cell) {
		if(event.shiftKey == true) {
			var started = false;
			var ended = false;
			var start = this.start;
			var end = 'row_' + cell;
			
			$(this.table).getElementsBySelector('tr').each(function(row) {
				if(row.id == start) {
					started = true;
				}
				if((started == true) && (ended == false)) {
					if((row.id.indexOf('row_cell') != -1) && (row.id != start)) {
						this.markSelected(row.id.substr((row.id.indexOf('row_')+4)));
					}	
				}
				if(row.id == end) {
					ended = true;
				}				
			}.bind(this));

		} else {
			this.start = 'row_' + cell;
			this.markSelected(cell);
		}
	},
	
	markSelected: function(cell) {
		if(!in_array(cell, this.selected)) {
			this.makeSelect(cell);	
		} else {
			this.makeDeSelect(cell);	
		}
	},
	
	makeDeSelect: function(cell) {
		this.aPull(cell);
	},
	
	makeSelect: function(cell) {
		this.aPush(cell);
	},
	
	selectAll: function(box) {
		if(box.checked == false) {
			$(this.table).getElementsBySelector('tr').each(function(row) {
				if(row.id.indexOf('row_cell') != -1) {
					this.makeDeSelect(row.id.substr((row.id.indexOf('row_')+4)));
				}	
			}.bind(this));		
		} else {
			$(this.table).getElementsBySelector('tr').each(function(row) {
				if(row.id.indexOf('row_cell') != -1) {
					this.makeSelect(row.id.substr((row.id.indexOf('row_')+4)));
				}	
			}.bind(this));		
		}
	},
	
	selectQuantity: function(quantity) {
		
		this.selected.each(function(cell) {
			this.makeDeSelect(cell);
		}.bind(this));
		
		var rows = $(this.table).getElementsBySelector('tr');
		var x = 0;
		for(var i=0; i<rows.length; i++) {
			if((rows[i].id.indexOf('row_cell') != -1) && (x <= quantity)){
				this.makeSelect(rows[i].id.substr((rows[i].id.indexOf('row_')+4)));
				x++;
			}	
		}
	},
		
	aPush: function(cell) {
		var row = $('row_' + cell);
		if(!in_array(cell, this.selected)) {
			this.selected.push(cell);	
			$(cell).checked = true;
			$(row).className = 'dataTableRowSelected';
			Event.observe(row, 'mouseover', function() { $(row).className = 'dataTableRowSelected'; });
			Event.observe(row, 'mouseout', function() { $(row).className = 'dataTableRowSelected'; });			
		}
		this.showSelected();
	},
	
	aPull: function(cell) {
		var i = 0;
		this.selected.each(function(element) {
			if(element == cell) {
				var row = $('row_' + element);
				this.selected.splice(i, 1);	
				$(element).checked = false;
				$(row).className = 'dataTableRow';
				Event.observe(row, 'mouseover', function() { $(row).className = 'dataTableRowOver'; });
				Event.observe(row, 'mouseout', function() { $(row).className = 'dataTableRow'; });		
			}
			i++;
		}.bind(this));
		this.showSelected();
	},
	
	showSelected: function() {
		if(this.selected.length>0) {
			$('select_quantity').value = this.selected.length;
		} else {
			$('select_quantity').value = '';
		}
	}
}

function array_pull(item, array){
	for(var i=0; i<array.length; i++) {
		if(array[i] == item) {
			array.splice(i, 1);
		}
	}
}

function checkNumeric(input) {
	input.value=input.value.replace(/((\.[^.]*?)+)(\.)/g,"$1").replace(/[^0-9.-]/g,"");
}

function checkStrictNumeric(input) {
	input.value=input.value.replace(/((\.[^.]*?)+)(\.)/g,"$1").replace(/[^0-9]/g,"");
}

function checkUpperCase(input) {
	input.value = input.value.toUpperCase();
}

function rowOver(object, classname) {
	object.className = classname;
}

function rowOut(object, classname) {
	object.className = classname;
}

function getChartData(id) {
	var id = eval(id);
	return id.value;
}

function rowOverEffect(object) {
	object.className = 'dataTableRowOver';
}

function rowOutEffect(object) {
	object.className = 'dataTableRow';
}

function liOverEffect(object) {
	object.className = 'dropdown_over';
}

function liOutEffect(object) {
	object.className = 'dropdown';
}

function cellOverEffect(object) {
	object.className = 'tablecontentover';
}

function cellOverEffectsub(object) {
	object.className = 'tablecontentoversub';
}

function cellOutEffect(object) {
	object.className = 'tablecontent';
}

function divOverEffect(object) {
	object.className = 'divover';
}

function divOutEffect(object) {
	object.className = 'divout';
}

function getPosition(selector) {
        var targetOffset = $j('#' + selector).offset().top;
        $j('html,body').animate({scrollTop: targetOffset}, 300);
    }

function getDivPosition(container, selector) {
        var targetOffset = $j(selector).offset().top;
        $j(container).animate({scrollTop: targetOffset}, 300);
    }
	

/*Effect.OpenUp = function(element) {
     element = $(element);
     new Effect.BlindDown(element, arguments[1] || {});
 }
 

Effect.CloseDown = function(element) {
     element = $(element);
     new Effect.BlindUp(element, arguments[1] || {});
}

Effect.Combo = function(element) {
     element = $(element);
     if(element.style.display == 'none') { 
          new Effect.OpenUp(element, arguments[1] || {}); 
     }else { 
          new Effect.CloseDown(element, arguments[1] || {}); 
     }
}*/

function flagCheckboxes(element) {
  var elementForm = element.form;
  var i = 0;
  for (i = 0; i < elementForm.length; i++) {
    if (elementForm[i].type == 'checkbox') {
      elementForm[i].checked = element.checked;
    }
  }
}

function getFlaggedCheckboxes(buttonGroup) {
	var retArr = new Array();
   	var lastElement = 0;
   if(buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for(var i=1; i<buttonGroup.length; i++) {
         if(buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = buttonGroup[i].value;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
}

var Search = {
	bufferText: '',
	bufferTime: 500,
	timeout: null,
	field: null,
	module: null,
	parameters: null,
	
	initialise: function(module, parameters) {
		this.field = $('search_field');
		this.field.focus();
		this.field.value = this.field.value;
		this.module = module;
		this.parameters = parameters;
	},
	
	modified: function() {
		if(this.timeout != null) {
			clearTimeout(this.timeout);	
		}
		this.timeout = setTimeout(this.compareBuffer.bind(this), this.bufferTime);
	},
	
	compareBuffer: function() {
		if((this.field.value != this.bufferText) && (this.field.value.length > 2)){
			this.bufferText = this.field.value;
			this.makeRequest();
		}
	},
			
	makeRequest: function() {
		xajax.call('renderModule', {callback:renderLoading, parameters:[this.module, "{'search':'" + this.field.value + "'}"]});
	},
	
	clear: function(div_reload) {
		this.bufferText = '';	
		this.field.value = '';
		if(div_reload === true) {
			xajax.call('renderModule', {callback:renderLoading, parameters:[this.module, 'force_clear']});
		}
	}
}

function isArray(array){
	if(array.length>0)
		return true;
	else
		return false;
}

function in_array( needle, haystack )
{
	for( var i in haystack )
	{
		if( haystack[i] == needle )
			return true;
	}
	
	return false;
}


function radio_checked( obj )
{
	var res = 0;
	
	for( var i = 0; i < obj.length; i++ )
		if( obj[i].checked ) return obj[i].value;
	
}

function multicheckbox_checked(checkboxes_keys) {
	for(var i in checkboxes_keys)
		if($(checkboxes_keys[i]).checked)
			return true;
	
	return false;
}

function str2lower( value )
{
	rExp	= /[^\w]/gi;
	low_value = value.toLowerCase();	
	return low_value.replace( rExp, "_" );
}


function IsNumeric(strString){
  var strValidChars = "0123456789.-";
  var strChar;
  var blnResult = true;
   if (strString.length == 0) return false;
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}


function checkNumber(input) {
input.value=input.value.replace(/((\.[^.]*?)+)(\.)/g,"$1").replace(/[^0-9.-]/g,"");
}

function confirmMessage(text){
	var answer = confirm(text)
	if (answer){
	return true;
	} else {
	return false;
	}
}

function setSelectedIndex(s, v) {
    for ( var i = 0; i < s.options.length; i++ ) {
        if ( s.options[i].value == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}

function downloadSpreadsheet(filename) {
    if(screen) {
		xPos = (screen.availWidth - 200)/2;
        yPos = (screen.availHeight - 200)/2;        
    }
	args = "width=200,height=200,screenX=" + xPos + ",screenY=" + yPos + ",left=" + xPos + ",top=" + yPos + ",status=0, scrollbars=1";
	report = window.open(host + '/_work/spreadsheets/'+filename, 'downloadReport', args);
	report.focus();
}

function getWYSIWYG(field) { 
    var oFCKeditor = FCKeditorAPI.GetInstance(field); 
    return oFCKeditor.GetXHTML(); 
}

document.onkeydown = function(e) {   
	if (e == null) {
    	keycode = event.keyCode;
	} else {
        keycode = e.which;
	}
    if(keycode == 27) {
		if($(current)) {
			Containers.close(current);
		}
	} 
}	
