﻿
/*Declare the namespaces*/
{
    _REAPPS = REAPPS = {};
    _UTIL = REAPPS.UTIL = {};
    _SCRIPTING = REAPPS.UTIL.SCRIPTING = {};
    _EXCEPTIONS = REAPPS.UTIL.EXCEPTIONS = {};
    _SCRIPTOBJECT = REAPPS.UTIL.SCRIPTOBJECT = {};
    _AJAX = REAPPS.UTIL.AJAX = {};
}


/*
<$Import>
    <About>
    Method to import(include) a javascript file with-in another javascript file. This method imports a ".js" file through AJAX (XMLHttp object).
    </About>
    <Input Parameters>
        <psScriptPath>Relative path (from source ".js" file) of target ".js" file to import</psScriptPath>
    </Input Parameters>
    <Results>
    This method does the real include like #include in C. The code/variables/functions of the "imported" file are immediately made available to the code of the source file just after the call to this function.
    </Results>
</$Import>
*/
$Import = _SCRIPTING.ImportScript = function (psScriptPath){
    //Function to create a XmlHttp object depending upon the user's browser.
    function fnGetXMLHttpObj(){
	    if(typeof(XMLHttpRequest)!='undefined')
		    return new XMLHttpRequest();
	    var axO=['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0',
		    'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'], i;
	    for(i=0;i<axO.length;i++)
		    try{
			    return new ActiveXObject(axO[i]);
		    }catch(e){}
	    return null;
    }

	var loXML = fnGetXMLHttpObj();
	/*the "false" parameter to "open" stops the code from advancing until the 
	call is complete. This is required to make sure that the code/objects of the
	imported file are available to the code in source file just after this method*/
	loXML.open('GET', psScriptPath, false);
	loXML.send('');
	var lsScriptCode = loXML.responseText;

    //Next, execute the import file's code in global scope of current window/document.
    if(window.execScript) // msie
        window.execScript(lsScriptCode);
//    add safari browser detection later, in Opera setTimeout puts a delay of 10ms even if you specify '0', otherwise this works well on all browsers
//    else if(safari) 
//        window.setTimeout(lsScriptCode,0);
    else // all others
        eval.call( window, lsScriptCode );
}



$NaO = _SCRIPTING.NotValidObject = function (puSomething)
{
    ls = String("typeof(puSomething) != 'function' || puSomething == null");
    ls = ls.replace(/puSomething/gi, puSomething);

    try
    {    
    alert(ls);
    lb = eval(ls.valueOf());   }
    catch(err)
    {   alert("ex");
    return true;    }
    
    return lb;
//    return ( typeof(puSomething) != "object" || puSomething == null);
//    alert(typeof(eval(puSomething)));
//    alert(ls);
//    alert(eval(ls));
//    return eval(ls);
}



$Exception = _EXCEPTIONS.ExceptionAlert = function (poError, psErrorMessage)
{
    alert("Exception Warning::\n" 
            + String(psErrorMessage)
            + "\nError Message: " + poError.message
            + "\nError Description: " + (poError.description!=poError.message?poError.description:'')
            + "\nError Name: " + poError.name
            );
}


/*
<$ProgrammaticChangeFormField>
    <About>
    Function to programmatically update a form field, and fire the field events that are otherwise not fired by programmatic updations. These are events, otherwise, are fired only when the cursor moves in-out of the field. For example - "OnChange" event.
    </About>
    <Parameters>
        <poFormField>Object Reference for the form field to update.</poFormField>
        <psValue>The new value to for the form field.</psValue>
    </Parameters>
</$ProgrammaticChangeFormField>
*/
$ProgrammaticChangeFormField = _SCRIPTING.ProgrammaticChangeFormField_HandleEvents = function (poFormField, psValue)
{
    if (typeof(poFormField) == "object")
    {    
        poFormField.value = psValue;   
        if (typeof(poFormField.onchange) == "function")
        {   poFormField.onchange();     }
    }
    
}