/*
<!-- -----------------------------------------------------------------------------------    -->
<!-- Source Control Id: $Id: gbtajax.js,v 1.7 2009/03/12 11:39:45 henning Exp $              -->
<!--                                                                                        -->
<!-- GlobeTOM Copyright Notice                                                              -->
<!-- All source code contained in this source file is protected by Copyright Laws of the    -->
<!-- Republic of South Africa. It is a copyright infringement to use this file as a whole   -->
<!-- or any part thereof without obtaining prior writtent consent from GlobeTOM.            -->
<!--                                                                                        -->
<!-- (c) GlobeTOM, 2008                                                                     -->
<!-- ------------------------------------------------------------------------------------   -->
<!--Source Control Id: $Id: gbtajax.js,v 1.7 2009/03/12 11:39:45 henning Exp $-->    
*/

var xmlhttp = false;

var l_success_obj1;
var l_success_obj2;
var l_fail_obj;
var l_dopost;

function ajax_test()
{
    alert('AJAX Request Recived!');
}  

function ajax_progress_message(message)
{
    var newdiv = document.getElementById('innerAjaxMessages');

    //var olddiv = document.getElementById('ajax_progress_msg_div');
    // if (olddiv != null)
    // {
    //      olddiv.parentNode.removeChild(olddiv);
    //     //document.forms[0].removeChild(olddiv);
    // }

    //var newdiv = document.createElement('div');
    //var divIdName = 'ajax_progress_msg_div';
    //newdiv.setAttribute('id',divIdName);


    // var strHtml = "<div>";                  
    //strHtml = strHtml + "<table class='ajax_progress_box'>";
    //strHtml = strHtml + "<td align=\"center\">";
    //strHtml = strHtml + "<img src=\"images/ajax_loader.gif\">";
    //strHtml = strHtml + "</td>";
    //strHtml = strHtml + "<td align=\"center\">";
         
    //strHtml = strHtml + message;
       
    //strHtml = strHtml + "</td>";
    //strHtml = strHtml + "</table><br></div>";

    newdiv.innerHTML = message;
     
//document.forms[0].appendChild(newdiv);
     
}

function ajax_progress_custom(htmlcontent)
{
    var olddiv = document.getElementById('ajax_progress_box'); 

    //if (olddiv != null)
    //{
    //    document.forms[0].removeChild(olddiv);
    //}

    //var newdiv = document.createElement('div');
    //var divIdName = 'ajax_progress_msg_div';
    //newdiv.setAttribute('id',divIdName);

    //var strHtml = "<div>" + htmlcontent + "</div>";
    olddiv.innerHTML = htmlcontent;
//document.forms[0].appendChild(newdiv);
}

function show_message()
{
    Popup.showModal('ajax_progress_box');
}

function hide_message()
{
    Popup.hide('ajax_progress_box');
}

function ajax_request(form, formhandlerURL, success_obj, fail_obj,do_postback) 
{
    l_success_obj1 = success_obj;
    l_fail_obj = fail_obj;
    l_dopost = do_postback;

    show_message();

    try
    {
        xmlhttp = new XMLHttpRequest();
    }
    catch (trymicrosoft)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (othermicrosoft)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed)
            {
                xmlhttp = false;
            }
        }
    }
	  
    //var DataToSend = form_valuebuilder(form.name);
    //xmlhttp.onreadystatechange = form_posthandler;

    //var combined = formhandlerURL + DataToSend;
        
    //xmlhttp.open('POST', combined, true);
    //xmlhttp.send(null);


    var url = formhandlerURL;
    var params = form_valuebuilder(form.name);
    xmlhttp.onreadystatechange = form_posthandler;
    xmlhttp.open("POST", url, true);

    //Send the proper header information along with the request
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);


}


function ajax_multi_request(form, formhandlerURL, success_obj1, success_obj2, fail_obj,do_postback)
{
    l_success_obj1 = success_obj1;
    l_success_obj2 = success_obj2;

    l_fail_obj = fail_obj;

    l_dopost = do_postback;
		
    show_message();

    try
    {
        xmlhttp = new XMLHttpRequest();
    }
    catch (trymicrosoft)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (othermicrosoft)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed)
            {
                xmlhttp = false;
            }
        }
    }
	  
    //var DataToSend = form_valuebuilder(form.name);
    //xmlhttp.onreadystatechange = form_posthandler;

    //var combined = formhandlerURL + DataToSend;
        
    //xmlhttp.open('POST', combined, true);
    //xmlhttp.send(null);


    var url = formhandlerURL;
    var params = form_valuebuilder(form.name);
    xmlhttp.onreadystatechange = form_posthandler;
    xmlhttp.open("POST", url, true);

    //Send the proper header information along with the request
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);

}



function form_valuebuilder(theFormName) 
{   
    theForm = document.forms[theFormName];
    var qs = '&t='
    for (e=0;e<theForm.elements.length;e++)
    {
        if (theForm.elements[e].name!='')
        {
            var name = theForm.elements[e].name;
            qs+=(qs=='')?'':'&'
            qs+= name+'='+escape(theForm.elements[e].value);
        }
    }
    //qs+="\n";
    return qs
} 


function form_posthandler() 
{  
    if (xmlhttp.readyState==4)
    {

        hide_message();

        if (xmlhttp.status == 200)
        {
            var ajxresult = trim(xmlhttp.responseText).split("!#");
            if (ajxresult[0] =="OK")
            {
                document.getElementById(l_fail_obj).innerHTML = "";
                document.getElementById(l_fail_obj).style.visibilty ='hidden';

                document.getElementById(l_success_obj1).innerHTML = ajxresult[1];
                          
                if (ajxresult.length > 2 && l_success_obj2 != null)
                {
                    document.getElementById(l_success_obj2).innerHTML = ajxresult[2];
                }

                if (l_dopost)
                    AJAX_SUCCESS(ajxresult);
                           
            }
            else if (ajxresult[0] =="FAIL")
            {
                document.getElementById(l_success_obj1).innerHTML = "";
                document.getElementById(l_fail_obj).innerHTML = ajxresult[1];

                document.getElementById(l_fail_obj).style.visibilty ='show';
                      
                if (l_dopost)
                    AJAX_FAILURE(ajxresult);
            }
        }
        else if(xmlhttp.status == "404")
        {
            document.getElementById(l_fail_obj).innerHTML = '<b>FATAL ERROR : Page not found - please contact support!</b>';
        }
        else
        {
            document.getElementById(l_fail_obj).innerHTML = '<b>"Error has occurred with status code: '+ xmlhttp.status + '</b>';
        }
    }
	 
}

 
function trim(stringValue)
{
    return stringValue.replace(/(^\s*|\s*$)/, "");
}
	


    
