
/*function removeLoadMask() {
	if($('loading-box') != undefined) {
		Effect.Fade('loading-mask', {from:1.0, to:0, duration:1});
		Effect.Fade('loading-box', {duration:0.5, queue: 'end'});
	}
}*/

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]);
		}
	}
}

var veil = {
	content: null,
	disabled: false,
        
    show: function(content, domfocus){
		this.content = content
		Effect.Appear('page-veil', {from: 0.0, to: 0.7});
		if(content != undefined) {
			show_node(this.content);
			$(this.content).style.zIndex = '100';
			if(domfocus != undefined) {
				$(domfocus).focus();
			}
		}
    },
        
    hide: function(){
		if(this.content != null) {
			if(this.disabled == true) {
				alert('Error in page - contact support');
			} else {
				xajax_removeElement(this.content);
				Effect.Fade('page-veil', {duration:0.7});
			}
		}
    },
	
	disable: function() {
		this.disabled = true;	
	}
}

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(div, offset) {
  var selectedPosX = 0;
  var selectedPosY = 0;
              
    selectedPosX += $(div).offsetLeft;
    selectedPosY += $(div).offsetTop;
	selectedPosY += offset;
    theElement = $(div).offsetParent;
    
 window.scrollTo(selectedPosX,selectedPosY);
}

function completeTask(task, divID, date) {
	$(divID).innerHTML = '<input style="width:60px; background-color:#f5f8fa; color:#999999; cursor:pointer;" type="text" id="completed_task_' + task + '" name="completed_task_' + task + '" value="' + date + '" readonly onclick="var cal = new CalendarPopup(\'cal_completed_task_' + task + '\'); cal.select($(\'completed_task_' + task + '\'),\'completed_task_' + task + '\',\'yyyy-MM-dd\'); return false;" /><div class="calendar_div" id="cal_completed_task_' + task + '"></div>';
}

function clearCompleteTask(task, divID) {
	$(divID).innerHTML = '';
}

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;
    }
  }
}

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( prefix, checkboxes_keys )
{
	for( var i in checkboxes_keys )
		if( $(prefix+'_'+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('http://highcommand/_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){
        veil.hide();
    } 
}	