var _documentActiveForm = null;

function slib_Browser()
{
	d=document;
	this.agt=navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.dom=(d.getElementById)?1:0;
	this.ns=(d.layers);
	this.ns4up=(this.ns && this.major >=4);
	this.ns6=(this.dom&&navigator.appName=="Netscape");
	this.op=(window.opera? 1:0);
	this.ie=(d.all);
	this.ie4=(d.all&&!this.dom)?1:0;
	this.ie4up=(this.ie && this.major >= 4);
	this.ie5=(d.all&&this.dom);
	this.win=((this.agt.indexOf("win")!=-1) || (this.agt.indexOf("16bit")!=-1));
	this.mac=(this.agt.indexOf("mac")!=-1);
};

var oBw = new slib_Browser();

var SLIB_SHIFT_KEYCODE = 16;
var SLIB_CTRL_KEYCODE = 17;
var SLIB_ALT_KEYCODE = 18;
var SLIB_SHIFT = "shift";
var SLIB_CTRL = "ctrl";
var SLIB_ALT = "alt";

slib_keyevt.count=0;

function slib_keyevt(elm)
{
	this.id = "keyevt"+slib_keyevt.count++;
	eval(this.id + "=this");
	this.keys = new Array();
	this.shift=0;
	this.ctrl=0;
	this.alt=0;
	this.addKey = slib_addKey;
	this.keyevent = slib_keyevent;
	this.checkModKeys = slib_checkModKeys;
};

function slib_addKey(cdom,cns4,a,m)
{
	var arrayIndex; 
	if(oBw.ie||oBw.dom) arrayIndex = cdom;
	else arrayIndex = cns4;
	
	if (typeof this.keys[arrayIndex] == "undefined")
		this.keys[arrayIndex] = new Array();
	this.keys[arrayIndex][this.keys[arrayIndex].length] = [a,m];				
};

var sLIB_COUNT=0;

function slib_keyevent(evt)
{
	if(oBw.ie||oBw.op) evt=event;
	var k = (oBw.ie||oBw.op||oBw.ns6)? evt.keyCode:evt.which;
	this.checkModKeys(evt,k);
	if(this.keys[k]==null) return false;
	
	for(var index=0; index < this.keys[k].length; index++) {
		var m = this.keys[k][index][1];
		if((this.shift && (m.indexOf(SLIB_SHIFT) != -1) || !this.shift && (m.indexOf(SLIB_SHIFT) == -1)) && (this.ctrl && (m.indexOf(SLIB_CTRL) != -1) || !this.ctrl && (m.indexOf(SLIB_CTRL) == -1)) && (this.alt && (m.indexOf("alt") != -1) || !this.alt && (m.indexOf("alt") == -1)))
		{
			var a = this.keys[k][index][0];
			a = eval(a); 
			if(typeof a == "function") a();
		}
	}
};

function slib_checkModKeys(e,k)
{
	if(oBw.dom)
	{ 
		this.shift = e.shiftKey;
		this.ctrl = e.ctrlKey;
		this.alt = e.altKey;
	}
	else
	{
		// for opera
		this.shift = (k==SLIB_SHIFT_KEYCODE) ? 1:0;
		this.ctrl = (k==SLIB_CTRL_KEYCODE) ? 1:0;
		this.alt = (k==SLIB_ALT_KEYCODE) ? 1:0;
	}
};

var oKey = new slib_keyevt();
////////////////////////////////////// functions //////////////////////
// lov
function loadLovShortcut(lovName, targetElement, formName) {
	if (event.srcElement.name != targetElement || 
		event.srcElement.form.name != formName)
		return;
		
	var buttonObj = event.srcElement.form.elements[lovName];
	if (typeof buttonObj != "undefined")
		buttonObj.click();
};	

// dbgrid
function appendRowShortcut(tableId, formName) {
	if (_documentActiveForm == null || _documentActiveForm.name != formName)
		return;
	if 	(document.getElementById(tableId).newFacility == 'false')
		return;
	appendRow(tableId);	
	
	var theTableBody = document.getElementById(tableId).tBodies[0];
	row = theTableBody.rows[theTableBody.rows.length - 1];
	focused(row);	
};

function switchRowShortcut(tableId, formName) {
	if (_documentActiveForm == null || _documentActiveForm.name != formName)
		return;	
	if (focusedRow != null && focusedRow.modified == true)
		return;
	if (focusedRow != null && focusedRow.newRow != true && 
		document.getElementById(tableId).editFacility != 'true')
		return;
    if (focusedRow.deleteStatus == 'delete')
		return;
	var theTableBody = document.getElementById(tableId).tBodies[0];
	if (theTableBody.rows.length <= 2)
		return;
		
	if (focusedRow == null ) { // no row selected yet
		row = theTableBody.rows[2];
		focused(row);
	}

	if (focusedRow.cells[0].ondblclick != null) {
		mouseOverdRow = focusedRow;
		focusedRow.cells[0].ondblclick();
	}	

	event.returnValue = false;	
}

function postRowShortcut(tableId, formName) {
	if (_documentActiveForm == null || _documentActiveForm.name != formName)
		return;
	
	var theTableBody = document.getElementById(tableId).tBodies[0];
	if (theTableBody.rows.length <= 2)
		return;
		
	if (focusedRow == null) 
		return;
		
	var strId = focusedRow.id.toString();
	if ( strId.charAt(strId.length-1) != "1" ) //this is view row
		return;
				
	mouseOverdRow = focusedRow;				
	postRow();
	event.returnValue = false;	
}

function cancelRowShortcut(tableId, formName) {
	if (_documentActiveForm == null || _documentActiveForm.name != formName)
		return;
	if (focusedRow == null) 
		return;	
		
	var strId = focusedRow.id.toString();
	if ( strId.charAt(strId.length-1) != "1" ) //this is view row
		return;
		
	mouseOverdRow = focusedRow;					
	switchRow();	
	event.returnValue = false;		
}

function removeRowShortcut(tableId, formName) {
	if (_documentActiveForm == null || _documentActiveForm.name != formName)
		return;		
	if 	(focusedRow != null && focusedRow.newRow != true && 
		 document.getElementById(tableId).delFacility == 'false') 
		return;
	var theTableBody = document.getElementById(tableId).tBodies[0];
	if (theTableBody.rows.length <= 2)
		return;
	if (focusedRow == null) { // no row selected yet
		row = theTableBody.rows[2];
		focused(row);
	}
	mouseOverdRow = focusedRow;
	if (focusedRow.deleteStatus == 'delete')
		undeleteRow();
	else
		removeRow();
}

function navigateDownRowShortcut(tableId, formName) {
	if (_documentActiveForm == null || _documentActiveForm.name != formName)
		return;
	event.returnValue = false;						
	var theTableBody = document.getElementById(tableId).tBodies[0];
	if (theTableBody.rows.length <= 2)
		return;
	
	if (mouseOverdRow == null || focusedRow == null) { // no row selected yet
		row = theTableBody.rows[2];
		focused(row);
		mouseOverdRow = row;
	} 
	else {
		row = theTableBody.rows[focusedRow.index*2 + 4];
		if (row == null)
			return;
		if (row.style.display == 'none')
			row = theTableBody.rows[focusedRow.index*2 + 5];					
	    focused(row);
		mouseOverdRow = row;
	}
}

function navigateUpRowShortcut(tableId, formName) {
	if (_documentActiveForm == null || _documentActiveForm.name != formName)
		return;
	event.returnValue = false;						
	var theTableBody = document.getElementById(tableId).tBodies[0];
	if (theTableBody.rows.length <= 2)
		return;
	
	if (mouseOverdRow == null || focusedRow == null) { // no row selected yet
		row = theTableBody.rows[2];
		focused(row);
		mouseOverdRow = row;
	} 
	else {
		if (focusedRow.index == 0)
			return;
		row = theTableBody.rows[focusedRow.index*2];
		if (row == null)
			return;		
		if (row.style.display == 'none')
			row = theTableBody.rows[focusedRow.index*2 + 1];		
	    focused(row);
		mouseOverdRow = row;
	}
}

// form
function backShortcut(buttonName, formName) {
	if (_documentActiveForm == null || _documentActiveForm.name != formName)
		return;
		
	_documentActiveForm.elements(buttonName).click();	
}

function switchFormShortcut() {	
	var currentFrom = document.activeElement.form;
	var frmIndx;
	for(frmIndx=0; frmIndx < document.forms.length; frmIndx++) 
		if (document.forms[frmIndx] == currentFrom)
			break;
	var frmIndx = (frmIndx+1)%document.forms.length;	
	var nextForm = document.forms[frmIndx];
	if (nextForm == currentFrom){
		event.returnValue = false;
		return;
	}			
	if (typeof nextForm.onfocus == 'function')
		nextForm.onfocus();		
		
	for(var idx=0; idx < nextForm.elements.length; idx++) {
			obj = nextForm.elements[idx];
			try{
				obj.focus();
				isFocused = true;
				break;
			}
			catch (ex) {
			}
	} 
	 		
	event.returnValue = false;
}
// page
function maximizePageShortcut() {
	if (parent.document.frames[0] == null || 
		parent.document.frames[0].document.getElementById('toggle_after') == null
		)
		return
	parent.document.frames[0].document.getElementById('toggle_after').click();	
	event.returnValue = false;
}

function printShortcut(sisEXEAPP) {
	if (parent.frames[sisEXEAPP].window.print_func == null) {
		parent.frames[sisEXEAPP].focus();
		window.print();
    }
    else {
    	parent.frames[sisEXEAPP].window.print_func();
	}	
	event.returnValue = false;
}

// report
function nextReportPageShortcut() {
	parent.document.frames['reportPager'].next();
	event.returnValue = false;	
}
function previousReportPageShortcut() {
	parent.document.frames['reportPager'].previous();
	event.returnValue = false;	
}
function firstReportPageShortcut() {
	parent.document.frames['reportPager'].first();
	event.returnValue = false;		
}
function lastReportPageShortcut() {
	parent.document.frames['reportPager'].last();
	event.returnValue = false;		
}
function zoomInReportShortcut() {
	var zoomObj = parent.document.frames['reportPager'].document.getElementById('zoom');	
	if (zoomObj.value == '500%')
		return;
	if (zoomObj.value == 'FTP') {
		var index;
		for(index = 0; index < zoomObj.options.length; index++)	
			if (zoomObj.options[index].value == '100%') 
				break;
				
		zoomObj.selectedIndex = index - 1;
	}
	else {
		zoomObj.selectedIndex = zoomObj.selectedIndex - 1;
	}		
	zoomObj.onchange();	
	event.returnValue = false;	
}
function zoomOutReportShortcut() {
	var zoomObj = parent.document.frames['reportPager'].document.getElementById('zoom');	
	if (zoomObj.selectedIndex == zoomObj.options.length - 1)
		return;
	if (zoomObj.value == 'FTP') {
		var index;
		for(index = 0; index < zoomObj.options.length; index++)	
			if (zoomObj.options[index].value == '100%') 
				break;
		zoomObj.selectedIndex = index + 1;
	}
	else {
		zoomObj.selectedIndex = zoomObj.selectedIndex + 1;
	}	
	zoomObj.onchange();
	event.returnValue = false;	
}
function fitToPageReportShortcut() {
	var zoomObj = parent.document.frames['reportPager'].document.getElementById('zoom');	
	zoomObj.value = 'FTP';
	zoomObj.onchange();
	event.returnValue = false;	
}

// pager
function nextPagerPageShortcut(formName) {	
	if (_documentActiveForm == null && formName != '')
		return;
	if (_documentActiveForm != null && _documentActiveForm.name != formName)	
		return;
	var linkObj = document.getElementById('_next_' + formName);
	if (linkObj != null)
		linkObj.click();
	event.returnValue = false;	
}

function previousPagerPageShortcut(formName) {
	if (_documentActiveForm == null && formName != '')
		return;
	if (_documentActiveForm != null && _documentActiveForm.name != formName)	
		return;
	var linkObj = document.getElementById('_previous_' + formName);
	if (linkObj != null)
		linkObj.click();
	event.returnValue = false;	
}

function firstPagerPageShortcut(formName) {
	if (_documentActiveForm == null && formName != '')
		return;
	if (_documentActiveForm != null && _documentActiveForm.name != formName)	
		return;
	var linkObj = document.getElementById('_first_' + formName);
	if (linkObj != null)
		linkObj.click();
	event.returnValue = false;	
}

function lastPagerPageShortcut(formName) {
	if (_documentActiveForm == null && formName != '')
		return;
	if (_documentActiveForm != null && _documentActiveForm.name != formName)	
		return;
	var linkObj = document.getElementById('_last_' + formName);
	if (linkObj != null)
		linkObj.click();
	event.returnValue = false;	
}