﻿// JScript File

/// THE FUNCTION THAT GETS DATA USING AJAX - requires ShowDiv() & HideDiv()  - OMAR AA - V[1.0.8] - 4/8/2007 11:13:32 AM -
function funcGetAjaxData(AjaxURL, DataDivName, LoadingDivName, UseLoadingDiv, ErrorMsg)
{
    var ObjAJAX = new JeeranRemoteScripting();
	ObjAJAX.SetRequestMethod("GET");
	ObjAJAX.SetRequestURL(AjaxURL);
	ObjAJAX.SetData("");
	ObjAJAX.SetRequestAsynch(true);
    ObjAJAX.InitializeRequester();
    if (UseLoadingDiv == 'true')
       { ShowDiv(LoadingDivName); }
	ObjAJAX.SendRequest();
	//waiting?

	ObjAJAX.OnLoaded = function() {
        if (UseLoadingDiv == 'true')
           { HideDiv(LoadingDivName); }
	    document.getElementById(DataDivName).innerHTML = ObjAJAX.GetText();
	    ObjAJAX = null;
	}
	ObjAJAX.OnFailure = function() {
	    if (UseLoadingDiv == 'true')
	       { HideDiv(LoadingDivName); }
	    document.getElementById(DataDivName).innerHTML = ErrorMsg;
	    ObjAJAX = null;   
	}
}

///  FILL DATA FROM AJAX TO BOXES   - OMAR AA - V[1.0.8] - 4/25/2007 5:35:55 PM -
function FillDataForBoxes(AjaxURL, DivToFill, DivLoading, qsType)
{
    if (document.getElementById(DivToFill).innerHTML == "")
    {
        funcGetAjaxData(AjaxURL + '&type=' + qsType, DivToFill, DivLoading, 'true', 'request failed');
    }
}

function LoadAjaxInBox(BoxID, RequestURL, RequestMethod, RequestData, LoadingText, AllowRefresh)
{
    var AjaxRequest;
    var AllowLoad;
    var ErrorMsg;
     

    // Check if we want to allow this request t ogo through 
    if (AllowRefresh == false)
    {
 
        if (document.getElementById(BoxID).innerHTML == "")
        {
            AllowLoad = true;
        }
        else
        {
            AllowLoad = false;
        }
    }
    else {
        AllowLoad = true;
    }  

    if(AllowLoad == true)
    {
        // Set the text in the loading message
        if (LoadingText == "")
        {
            LoadingText = "Loading...";
        }
        
        //  
        if (RequestMethod.toLowerCase() == "get")
        {
            RequestURL += "&" + RequestData;
        }

        AjaxRequest = CreateAjaxRequest(RequestURL, RequestMethod, RequestData);
        
        SetTextOfLayer(BoxID, "<div style='position: relative; top:40px; margin: auto; height:31px; width:150px; line-height:31px; font-size: 10px;'><img src='/im/j2/ajax/loading.gif' style='height: auto; width: auto; border:0; padding:0;' />&nbsp;" + LoadingText + "</div>");
        
        AjaxRequest.OnLoaded = function() {     
	        var ResponseText = AjaxRequest.GetText();
            /// THIS IS A FIX FOR IE6 - IE6 DOESN'T ALLOW <form> TO BE ADDED USING innerHTML
            /// SO WE REPLACE IT WITH DIV
	        ResponseText = ResponseText.replace("form ","div ")
	        
	        SetTextOfLayer(BoxID, ResponseText);
	        AjaxRequest = null;
	    }

	    AjaxRequest.OnFailure = function() {
            ErrorMsg = "Load failed. Please try again!";
	        SetTextOfLayer(BoxID, ErrorMsg);
	        AjaxRequest = null;   
	    }    
    }    
}