//==============================================================================
// Tool.js
// Created by:	Yi
// Date:		October 19, 2006
// Functions:
// 1. readHttpString( ... )
//		u = url string, 
//		c = client object, 
//		o = option integer = 2 return Dom object else return text string, 
//		b = callback function, 
//		m = call method,
//		l = user name,
//		p = password
// 2. readHttpStringA(...) asynchronous call
//==============================================================================

var obj_xh_xmlhttp;
var obj_xh_xmlhttp_client;
var obj_xh_option;	// 1 = text, 2 = xml dom object;

function readHttpStringA(u, c, o, b, m, l, p)
{
	if ( !obj_xh_xmlhttp ) xh_init();
	obj_client = c;
	if ( !o )
		obj_xh_option = 1;
	else
		obj_xh_option = o;
	xh_openurl(m, u, true, l, p, b);

}

function readHttpString(u, c, o, b, m, l, p)
{
	if ( !obj_xh_xmlhttp ) xh_init();
	obj_client = c;
	if ( !o )
		obj_xh_option = 1;
	else
		obj_xh_option = o;
	xh_openurl(m, u, false, l, p, b);
	return obj_client;
}

function xh_init()
{
	obj_xh_xmlhttp = false;
    if ( window.XMLHttpRequest ) 
    {
	    // branch for native XMLHttpRequest object
    	try 
    	{
			obj_xh_xmlhttp = new XMLHttpRequest();
        } 
        catch(e) 
        {
			obj_xh_xmlhttp = false;
        }
    } 
    else
    { 
	    // branch for IE/Windows ActiveX version
		if ( window.ActiveXObject ) 
		{
       		try {
        		obj_xh_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      		} 
      		catch ( e ) 
      		{
        		try 
        		{
          			obj_xh_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        		} 
        		catch ( e ) 
        		{
          			obj_xh_xmlhttp = false;
        		}
			}
		}
    }
}

function xh_openurl(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword, objcallbackfunction)
{
	try 
	{
		window.status = bstrUrl;
		obj_xh_xmlhttp.open(( ! bstrMethod ) ? 'GET' : bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
		obj_xh_xmlhttp.onreadystatechange = ( ! objcallbackfunction ) ? xh_processReqChange : objcallbackfunction;
		obj_xh_xmlhttp.send( null );
	}
	catch( e )
	{
		alert(e)
	}
}

function xh_processReqChange() 
{
    // 0 = uninitialized; 1 = loading; 2 = loaded; 3 = interactive; 4 = complete;
    if (obj_xh_xmlhttp.readyState == 4) 
    {	      		
		window.status = '';
		// 404 for "Not Found" or 200 for "OK"
        if (obj_xh_xmlhttp.status == 200) {
			if (obj_xh_option == 2 )
				obj_client = obj_xh_xmlhttp.responseXML;		
			else
    			try 
    			{
					obj_client.value = obj_xh_xmlhttp.responseText;
				} 
				catch(e) 
				{
					obj_client = obj_xh_xmlhttp.responseText;
				}
	    } 
        else 
        {
            window.status = obj_xh_xmlhttp.statusText;
        }
    }
}

//==========================================================
//	get_Radio_Value(arrObjRadio) return radio value if selected else ''
	
//==========================================================
function get_Radio_Value(arrObjRadio)
{
	var rad_val = '';
	if ( !arrObjRadio.length )
	{
		rad_val = arrObjRadio.value;
	}
	else
	{	
		for (var i=0; i < arrObjRadio.length; i++)
		{
			if (arrObjRadio[i].checked)
			{
				rad_val = arrObjRadio[i].value;
				break;
			}
		}
	}
	return rad_val;
}

function xmlDecode(str)
{
	str = str.replace(/&lt;/ig, '<');
	str = str.replace(/&gt;/ig, '>');
	str = str.replace(/&amp;/ig, '&');
	str = str.replace(/&apos;/ig, '\'');
	str = str.replace(/&quot;/ig, '"');
	str = str.replace(/&equal;/ig, '=');
	return str;
}

function xmlEncode(str)
{
	str = str.replace(/\</ig, "&lt;");
	str = str.replace(/\>/ig, "&gt;");
	str = str.replace(/\&/ig, "&amp;");
	str = str.replace(/\'/ig, "&apos;");
	str = str.replace(/\"/ig, "&quot;");
	str = str.replace(/\=/ig, "&equal;");
	return str;
}

function regExEscapCode(strValue)
{
	strValue = strValue.replace(/\(/g, "\\(");
	strValue = strValue.replace(/\)/g, "\\)");
	strValue = strValue.replace(/\[/g, "\\[");
	strValue = strValue.replace(/\]/g, "\\]");
	strValue = strValue.replace(/\#/g, "\\#");
	strValue = strValue.replace(/\*/g, "\\*");
	return strValue;
}        

