    var xmlHttp     = null;
    var ajaxRunning = false;
    var ajaxQueue   = new Array();
    var ajaxResult  = null;

    /*--------------------------------------------------------------------
     Purpose : To create and XmlHttpObject based on different 
               types of browsers              
    ---------------------------------------------------------------------*/

    function GetXmlHttpObject(){
        var xmlHttp=null;

        try{
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
        }catch (e){
            // Internet Explorer
            try{
                //Internet Explorer 6.0+
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e){
                //Internet Explorer 5.5+
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }

    /*--------------------------------------------------------------------
     Purpose : To run ajax function wrt to the given url 
               and statechanged function                
    ---------------------------------------------------------------------*/
    function runAjax(ajaxParm){ 

        var oForm       = null;
        var oField      = "";
        var oFieldName  = "";
        var oValue      = "";
        var dataString  = "";        

        if (ajaxRunning){
            ajaxQueue.push(ajaxParm);
            return;
        }

        ajaxResult  = null;
        ajaxRunning = true;
        showMsgBox('Retrieving...');       
       
        xmlHttp=GetXmlHttpObject();

        if (xmlHttp==null){
            alert ("Your browser does not support AJAX!"); //Don't even have IE 5.5..
            return;
        }            
        
        xmlHttp.onreadystatechange = function(){readyStateHandler(ajaxParm.targetFn)};
        xmlHttp.open("POST",ajaxParm.url,true);    
        
        if (!ajaxParm.fmObj){ 
            xmlHttp.send(null);
        }
        else{
            oForm = ajaxParm.fmObj;

            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");      
            
            for(var i = 0;i < oForm.elements.length;i++) {
                    oField = oForm.elements[i];                      
                    
                    if(oField.name != undefined ){                                

                        if (oField.name.replace(/^s+/, '').replace(/s+$/, '').length >0) {
                            oValue = getValue(oField);                

                            if (dataString.length>0)
                                dataString += "&";

                             dataString += encodeURIComponent(oField.name) + "=" + encodeURIComponent(oValue.length>0?oValue:"");                   
                        }  //-if
                    }
            }//-for

            xmlHttp.send(dataString); 
        } //-form submission
 
    }

    /*--------------------------------------------------------------------
        Purpose : Main ready state function, all call back function
                  must go thru this
        Input   : arCallBack - function to process result return by Ajax
    ---------------------------------------------------------------------*/
    function readyStateHandler(argCallBack){
       if (xmlHttp.readyState==4){    
            ajaxResult  = xmlHttp.responseText.replace(/^\s+|\s+$/g, ''); 
            ajaxRunning = false;
            argCallBack(); 
            delete xmlHttp;
            hideMsgBox();

            if (ajaxQueue.length > 0)
                runAjax(ajaxQueue.pop());

       }
    }

    /*--------------------------------------------------------------------
     Purpose : To get INPUT element value based on different input TYPE            
    ---------------------------------------------------------------------*/
    function getValue(argField){

            var fType   = argField.type;
            var fValue = "";

            switch(fType) {

                case "select-one":
                    fValue = argField.options[argField.selectedIndex].value;
                    break;							

                case "text":
                case "password":
                case "textarea":				
                default:
                    fValue = argField.value;					

            } // switch

            return fValue.replace(/^\s+|\s+$/g, '');   
     }
     
    /*==================================================================*      
     *  Different statechanged functions                                *
    ====================================================================*/

   /*--------------------------------------------------------------------
        Purpose : Write main page (index.jsp) body content    
        Input   : argFn   - (optional) Another Ajax function to be called
                             after the page is fully loaded     
    ---------------------------------------------------------------------*/

    function writeMainBody(argFn){      
    
        if (document.ifBody)
            document.ifBody.document.body.innerHTML = ajaxResult;                  
        else if (parent.document.ifBody)
            parent.document.ifBody.document.body.innerHTML = ajaxResult;   
        else if (parent.parent.document.ifBody)
            parent.parent.document.ifBody.document.body.innerHTML = ajaxResult;  
         
        //Second Ajax function to be loaded
        if (argFn)
            argFn();    
         
    }

    /*--------------------------------------------------------------------
     Purpose : For retrieving property Address           
    ---------------------------------------------------------------------*/

     function writeAddr(prefix, suffix, argForm){

        var addr      = ajaxResult.split('|');

        if (addr.length == 1){ //addr[0]=='ERROR' 
            
            document.getElementById(prefix + 'hdAddrPrefix' + suffix).value          = '';                    
            document.getElementById(prefix + 'txtAddrBlkNo' + suffix).value          = '';

            if (document.getElementById(prefix + 'txtAddrUnitNo' + suffix))
                document.getElementById(prefix + 'txtAddrUnitNo' + suffix).value      = '';  

            document.getElementById(prefix + 'txtAddrProject' + suffix).value         = '';
            document.getElementById(prefix + 'txtAddrRoad' + suffix).value            = '';   

            if (document.getElementById(prefix + 'txtAddrPostCode' + suffix))
                document.getElementById(prefix + 'txtAddrPostCode' + suffix).value    = '';  

            if (document.getElementById(prefix + 'txtAddrCity' + suffix))
                document.getElementById(prefix + 'txtAddrCity' + suffix).value        = ''; 

            if (document.getElementById(prefix + 'selAddrCtryCode' + suffix)){
                document.getElementById(prefix + 'selAddrCtryCode' + suffix).value    = 'SG';                                 
            }
        }
        else{
            document.getElementById(prefix + 'hdAddrPrefix' + suffix).value           = addr[0];                     
            document.getElementById(prefix + 'txtAddrBlkNo' + suffix).value           = addr[1];
           
            if (document.getElementById(prefix + 'txtAddrUnitNo' + suffix))
                document.getElementById(prefix + 'txtaddrUnitNo' + suffix).value      = addr[2];   

            document.getElementById(prefix + 'txtAddrProject' + suffix).value         = addr[3];
            document.getElementById(prefix + 'txtAddrRoad' + suffix).value            = addr[4];   

            if (document.getElementById(prefix + 'txtAddrPostCode' + suffix))
                document.getElementById(prefix + 'txtAddrPostCode' + suffix).value    = addr[5];  

            if (document.getElementById(prefix + 'txtAddrCity' + suffix))
                document.getElementById(prefix + 'txtAddrCity' + suffix).value        = addr[7]; 

            if (document.getElementById(prefix + 'hdAddrCtryCode' + suffix)){
                document.getElementById(prefix + 'hdAddrCtryCode' + suffix).value     = addr[8];                
            }
        }              
        
        if (argForm)
            loadSelectList(argForm);     
    }

    /*==================================================================*      
     *  Different ajax objects                                          *
    ====================================================================*/
    /*----------------------------------------------------------------------------
            Generic Ajax object
            Input : argUrl      - url for the page to be loaded
                    argTargetFn - readyState Function
                    argForm     - (optional) form object                         
    ----------------------------------------------------------------------------*/            
    function objAjax(argUrl, argTargetFn, argForm){                             
        this.url        = argUrl; 
        this.targetFn   = argTargetFn;          
        this.fmObj      = (argForm == undefined)?null:argForm;
    }

    /*----------------------------------------------------------------------------
            Ajax object to get main body content
            Input : argPage - url for the page to be loaded
                    argFn   - (optional) Another Ajax function to be called
                               after the page is fully loaded
    ----------------------------------------------------------------------------*/            
    function objAjaxMainBody(argPage, argFn){                           
        this.url        = argPage; 
        this.targetFn   = function (){writeMainBody(argFn);};                  
        this.fmObj      = null
    }
