//----------------------------------------------------------------------------------------------------
// File: Common_Function.js
//
// Description:
//	This JavaScript library provides functions for common funcionalities;
//
//	---------------------------------------------------------------------
//	Available Functions:
//	---------------------------------------------------------------------
//	* NumericOnly( oEvent )
//	* AlphaOnly( oEvent )
//	* NameOnly( oEvent )
//	*
//	* IsSelect_box( _box )
//	* find_selected( _box, val )
//	* insert_option( _box, _index, _val, _text )
//	* remove_selected_option( _box )
//	* remove_options( _box )
//	* append_option_last( _box, _val, _text )
//	* remove_option_last()
//	*
//	* get_radio_value( _box )
//	* set_radio_value( _box, _val )
//	*
//	* confirmX( str, type_id, title )
//	* model_dialog( _url, _wnd, _style, _value )
//	*
//	* Ajax_query( url, params, async )
//	* Ajax_execute( url, params, func, async )
//	*
//	* Sleep(naptime)
//	---------------------------------------------------------------------
//
// Revisions:
//	08-12-2009	Lei Tang - Created the initial version.
//	08-25-2009	Lei Tang - Modified
//
// Copyright (c)2008 Cogent Systems, Inc. All rights reserved.

/****************************************************************
 ****************************************************************/
var ASCII_BACKSPACE = 8;
var ASCII_TAB = 9;
var ASCII_END = 35,		ASCII_END_S = 63275;
var ASCII_HOME = 36,	ASCII_HOME_S = 63273;
var ASCII_LEFT = 37,	ASCII_LEFT_S = 63234;
var ASCII_RIGHT = 39,	ASCII_RIGHT_S = 63235;
var ASCII_DELETE = 46;

var ASCII_SPACE = 32;
var ASCII_DASH = 45;
var ASCII_DOT = 46;

var ASCII_0 = 48;
var ASCII_9 = 57;
var ASCII_A = 65;
var ASCII_Z = 90;
var ASCII_a = 97;
var ASCII_z = 122;



/************************************************
 *  *  *  *  *  INPUT EVENT *  *  *  *  *
 ************************************************/
function NumericOnly( oEvent )
{
	var nKeyCode;
	if ( window.event )	// IE or Safari
		nKeyCode = window.event.keyCode;
	else if ( oEvent )	// FF
		nKeyCode = oEvent.charCode || oEvent.keyCode;

	if ( ASCII_0 <= nKeyCode && nKeyCode <= ASCII_9
		|| nKeyCode == ASCII_BACKSPACE
		|| nKeyCode == ASCII_TAB
		|| nKeyCode == ASCII_END	|| nKeyCode == ASCII_END_S
		|| nKeyCode == ASCII_HOME	|| nKeyCode == ASCII_HOME_S
		|| nKeyCode == ASCII_LEFT	|| nKeyCode == ASCII_LEFT_S
		|| nKeyCode == ASCII_RIGHT	|| nKeyCode == ASCII_RIGHT_S )
		return true;
	return false;
}
function AlphaOnly( oEvent )
{
	var nKeyCode;
	if ( window.event )	// IE or Safari
		nKeyCode = window.event.keyCode;
	else if ( oEvent )	// FF
		nKeyCode = oEvent.charCode || oEvent.keyCode;

	if (   ASCII_A <= nKeyCode && nKeyCode <= ASCII_Z
		|| ASCII_a <= nKeyCode && nKeyCode <= ASCII_z
		|| nKeyCode == ASCII_BACKSPACE
		|| nKeyCode == ASCII_TAB
		|| nKeyCode == ASCII_END	|| nKeyCode == ASCII_END_S
		|| nKeyCode == ASCII_HOME	|| nKeyCode == ASCII_HOME_S
		|| nKeyCode == ASCII_LEFT	|| nKeyCode == ASCII_LEFT_S
		|| nKeyCode == ASCII_RIGHT	|| nKeyCode == ASCII_RIGHT_S )
		return true;
	return false;
}
function NameOnly( oEvent )
{
	var nKeyCode;
	if ( window.event )	// IE or Safari
		nKeyCode = window.event.keyCode;
	else if ( oEvent )	// FF
		nKeyCode = oEvent.charCode || oEvent.keyCode;

	if (   ASCII_A <= nKeyCode && nKeyCode <= ASCII_Z
		|| ASCII_a <= nKeyCode && nKeyCode <= ASCII_z
		|| nKeyCode == ASCII_SPACE
		|| nKeyCode == ASCII_DASH
		|| nKeyCode == ASCII_DOT
		|| nKeyCode == ASCII_BACKSPACE
		|| nKeyCode == ASCII_TAB
		|| nKeyCode == ASCII_END	|| nKeyCode == ASCII_END_S
		|| nKeyCode == ASCII_HOME	|| nKeyCode == ASCII_HOME_S
		|| nKeyCode == ASCII_LEFT	|| nKeyCode == ASCII_LEFT_S
		|| nKeyCode == ASCII_RIGHT	|| nKeyCode == ASCII_RIGHT_S )
		return true;
	return false;
}


/************************************************
 *  *  *  *  *  SELECT COMBO BOX *  *  *  *  *
 ************************************************/
function IsSelect_box( _box )
{
	if ( _box == "undefined" )
		return false;
	try
	{
		if ( _box.type != "select-one" &&
			 _box.type != "select-multiple" )
			return false;
	}
	catch (e)
	{
		return false;
	}
	return true;
}

function find_selected( _box, val )
{
	if ( !IsSelect_box( _box ) )
		return;
	opts = _box.options;
	if ( typeof(opts) == "object" || typeof(opts) == "function" ) // select.options is of type 'function' in Safari
	{
		for (var _i = 0; _i < opts.length; _i++ )
		{
			if (opts[_i].value == val)
			{
				opts[_i].selected = true;
				break;
			}
		}
	}
}

function insert_option( _box, _index, _val, _text )
{
	if ( !IsSelect_box( _box ) )
		return;
	if (_box.selectedIndex < 0)
		_box.selectedIndex = 0;
	
	var elOptNew = document.createElement('option');
	elOptNew.text = _text;
	elOptNew.value = _val;
	var elOptOld = _box.options[_box.selectedIndex];  
	try {
		_box.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
	}
	catch(e) {
		_box.add(elOptNew, _box.selectedIndex); // IE only
	}
}

function remove_selected_option( _box )
{
	if ( !IsSelect_box( _box ) )
		return;
	if (_box.selectedIndex < 0)
		return;
	
	for (var _i = _box.length - 1; _i >= 0; _i--) {
		if (_box.options[_i].selected) {
			_box.remove(_i);
		}
	}
}

function remove_options( _box )
{
	if ( !IsSelect_box( _box ) )
		return;
	for ( var _i = _box.length-1; _i >= 0; _i-- )
		_box.remove(_i);
	return;
}

function append_option_last( _box, _val, _text )
{
	if ( !IsSelect_box( _box ) )
		return;
	var elOptNew = document.createElement('option');
	elOptNew.text = _text;
	elOptNew.value = _val;
	
	try {
		_box.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		_box.add(elOptNew); // IE only
	}
}

function remove_option_last( _box )
{
	if ( !IsSelect_box( _box ) )
		return;
	if (_box.length > 0)
	_box.remove(_box.length - 1);
	return;
}

/************************************************
 *  *  *  *  *  RADIO COMBO BOX *  *  *  *  *
 ************************************************/

function get_radio_value( _box )
{
	if ( _box == undefined )
		return "";
	for (var _i = 0; _i < _box.length; _i++)
	{
		if (_box[_i].checked)
		{
			return _box[_i].value;
		}
	}
	return "";
}

function set_radio_value( _box, _val )
{
	if ( _box == undefined )
		return 0;
	for (var _i = 0; _i < _box.length; _i++) {
		if (_box[_i].value == _val)
		{
			_box[_i].checked = true;
			_box.value = _val;
			return ( _i + 1 );
		}
	}
	return 0;
}


/************************************************
 *  *  *  *  CUSTOMIZED DIALOG BOX *  *  *  *  *
 ************************************************/
/*@cc_on @*/
function confirmX( _str, _type_id, _title )
{
/*@if (@_win32 && @_jscript_version>=5)
	if ( _str == undefined )
		return false;
	_str = _str.replace( /\n/g, "\" & Chr(13) & Chr(10) & \"" );
	_str = _str.replace( /'/g, "\'" );
	if ( _type_id == undefined )
		_type_id = 33; // vbOKCancel (1) + vbQuestion (32)
	if ( _title == undefined )
		_title = "";
	var n_btn_type = _type_id;
	if ( n_btn_type >= 4096 ) n_btn_type -= 4096;
	if ( n_btn_type >= 768 ) n_btn_type -= 768;
	if ( n_btn_type >= 512 ) n_btn_type -= 512;
	if ( n_btn_type >= 64 ) n_btn_type -= 64;
	if ( n_btn_type >= 48 ) n_btn_type -= 48;
	if ( n_btn_type >= 32 ) n_btn_type -= 32;
	if ( n_btn_type >= 16 ) n_btn_type -= 16;
	var n_res = 1;
	var b_res = true;
	switch ( n_btn_type )
	{
	case 0: n_res = 1; // vbOKOnly
		break;
	case 1: n_res = 1; // vbOKCancel
		break;
	case 2: n_res = 2; // vbAbortRetryIgnore
		b_res = false;
		break;
	case 3: n_res = 6; // vbYesNoCancel
		b_res = false;
		break;
	case 4: n_res = 6; // vbYesNo
		break;
	case 5: n_res = 4; // vbRetryCancel
		break;
	default:
		break;
	}
	
	var s_cmd = 'n = msgbox("' + _str + '", "' + _type_id + '", "' + _title + '")'
	execScript( s_cmd, "vbscript" );
	if ( b_res) 
		return( n == n_res );
	else
		return n_res;
@else
	return false;
@end @*/
	return false;
}

function my_dislog_value_passing_class_cogent_2009( _value )
{
	this._value = _value;
}
function model_dialog( _url, _wnd, _style, _value )
{
	if ( typeof(_url) != "string" )
	{
		return undefined;
	}
	if ( typeof(_wnd) == "undefined" )
	{
		_wnd = window;
	}
	if (!_wnd.showModalDialog)
	{
		alert("Your browser does not support Modal Dialog. Please try using IE, Firefox, Chrome or Safari with higher version.")
	}
	
	if ( typeof(_style) == "undefined" )
	{
		_style = "dialogHeight:150px;dialogWidth:300px;";
	}
	if ( typeof(_value) == "undefined" )
		_value = undefined;
	
	var _my_obj = new my_dislog_value_passing_class_cogent_2009( _value );
	
	if ( !_wnd.showModalDialog( _url, _my_obj, _style ) )
		return undefined;
	
	return _my_obj._value;
}


/************************************************
 *  *  *  *  *  *   AJAX FUNCTIONS   *  *  *  *  *  *
 ************************************************/
function my_ajax_query_object_2009()
{
	this.o_xml_obj = null;
	this.n_status = -1;
	this.value = undefined;
}
function Ajax_query( url, params, async )
{
	var _res_obj = new my_ajax_query_object_2009();
	if ( typeof(url) != "string" || url == "" )
		return _res_obj;
	// For AJAX value query, we set async FALSE as default, so that the result can be return in time fashion
	if ( async == "false" )
		async = false;
	if ( typeof(async) != "boolean" )
		async = false;
	
	try {
		if (window.XMLHttpRequest) 
			_res_obj.o_xml_obj = new XMLHttpRequest();
		else
			_res_obj.o_xml_obj = new ActiveXObject("Msxml2.XMLHTTP");
		if (_res_obj.o_xml_obj != null)
		{
			_res_obj.o_xml_obj.open("POST", url, async);  
			//Send the proper header information along with the request
			_res_obj.o_xml_obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			_res_obj.o_xml_obj.setRequestHeader("Content-length", params.length);
			_res_obj.o_xml_obj.setRequestHeader("Connection", "close");
			if (async)
			{
				_res_obj.o_xml_obj.onreadystatechange = function()
				{
					if (_res_obj.o_xml_obj.readyState == 4)
					{
						if (_res_obj.o_xml_obj.status == 200)
						{
							_res_obj.n_status = 0;
							_res_obj.value = _res_obj.o_xml_obj.responseText;
						}
					}
				}
				_res_obj.o_xml_obj.ontimeout = function()
				{
					alert("AJAX request time out!");
					throw("Timeout Error");
				}
			}
			_res_obj.o_xml_obj.send(params);
			if (!async)
			{
				_res_obj.n_status = 0;
				_res_obj.value = _res_obj.o_xml_obj.responseText;
			}
		}
	}
	catch (e) {
		_res_obj.n_status = -1;
		_res_obj.value = undefined;
	}
	return _res_obj;
}


function Ajax_execute( url, params, func, async )
{
	var _res_obj = new my_ajax_query_object_2009();
	if ( typeof(url) != "string" || url == "" )
		return _res_obj;
	if ( async == "false" )
		async = false;
	// For AJAX function execution, we set async TRUE as default, so that the application won't get stuck by AJAX request
	if ( typeof(async) != "boolean" )
		async = true;
	
	try {
		if (window.XMLHttpRequest) 
			_res_obj.o_xml_obj = new XMLHttpRequest();
		else
			_res_obj.o_xml_obj = new ActiveXObject("Msxml2.XMLHTTP");
		if (_res_obj.o_xml_obj != null)
		{
			_res_obj.o_xml_obj.open("POST", url, async);  
			//Send the proper header information along with the request
			_res_obj.o_xml_obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			_res_obj.o_xml_obj.setRequestHeader("Content-length", params.length);
			_res_obj.o_xml_obj.setRequestHeader("Connection", "close");
			if (async)
			{
				_res_obj.o_xml_obj.onreadystatechange = function()
				{
					if (_res_obj.o_xml_obj.readyState == 4)
					{
						if (_res_obj.o_xml_obj.status == 200)
						{
							_res_obj.n_status = 0;
							_res_obj.value = _res_obj.o_xml_obj.responseText;
							func(_res_obj);
						}
					}
				}
				_res_obj.o_xml_obj.ontimeout = function()
				{
					alert("AJAX request time out!");
					throw("Timeout Error");
				}
			}
			_res_obj.o_xml_obj.send(params);
			if (!async)
			{
				_res_obj.n_status = 0;
				_res_obj.value = _res_obj.o_xml_obj.responseText;
				func(_res_obj);
			}
		}
	}
	catch (e) {
		_res_obj._n_status = -1;
		_res_obj._value = "";
		return _res_obj;
	}
}
 
/************************************************
 *  *  *  *  *  ACCESSORY FUNCTIONS  *  *  *  *  *
 ************************************************/
function Sleep( naptime )
{
	var sleeping = true;
	var now = new Date();
	var alarm;
	var startingMSeconds = now.getTime();
	while(sleeping)
	{
		alarm = new Date();
		alarmMSeconds = alarm.getTime();
		if( alarmMSeconds - startingMSeconds > naptime )
			sleeping = false
	}
}
