﻿// JScript File

var XmlHttpRequestObj;
var JSTranslationHandlerURL;

var StrArrResourceId;
var StrArrResourceIdLength;

//This function will be used to display alert with localized strings
function ShowAlert(ResourceId, StrArrPlaceHolders)
{
    var LocalizedString = null;

    JSTranslationHandlerURL = document.getElementById("JSTranslationHandlerURL");
    if(JSTranslationHandlerURL == null)
    {
        alert("JSTranslationHandlerURL not found.");
        return;
    }
        
    if(ResourceId == null || ResourceId == "")
    {
        alert("Null Resource ID.");
        return;
    }
        
    //Get the localized string from cache (if it is not present the call will be made)
    LocalizedString = GetLocalizedString(ResourceId, StrArrPlaceHolders);
        
    //Display alert
    alert(LocalizedString);
    return;
}

//This function will be used to display confirm dialog with localized strings
function ShowConfirm(ResourceId, StrArrPlaceHolders)
{
    var LocalizedString = null;
    
    JSTranslationHandlerURL = document.getElementById("JSTranslationHandlerURL");
    if(JSTranslationHandlerURL == null)
    {
        alert("JSTranslationHandlerURL not found.");
        return;
    }
        
    if(ResourceId == null || ResourceId == "")
    {
        alert("Null Resource ID.");
        return;
    }
        
    //Get the localized string from cache (if it is not present the call will be made)        
    LocalizedString = GetLocalizedString(ResourceId, StrArrPlaceHolders);
    
    //Display confirm dialog
    return confirm(LocalizedString);
}

function CreateXmlHttpRequestObject()
{
    if(XmlHttpRequestObj != null)
        return;

    //creating the XmlHttpRequestObj object
	if (window.ActiveXObject)
    {
		try
		{
			XmlHttpRequestObj = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				XmlHttpRequestObj = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				XmlHttpRequestObj = null;
			}
		}
    }
    else if (window.XMLHttpRequest)
    {	
		//Creating object of XMLHTTP in Mozilla and Safari 
		if(!XmlHttpRequestObj && typeof XMLHttpRequest != "undefined")
		{
			XmlHttpRequestObj = new XMLHttpRequest();
		}
    }
}   

//This function will return the localized string (from array (used to cache localized string) or by invoking the method to get localized string)
function GetLocalizedString(ResourceId, StrArrPlaceHolders)
{
    var RedirectUrl   = null;
    var i             = 0;
    
    if(JSTranslationHandlerURL == null)
    {
        JSTranslationHandlerURL = document.getElementById("JSTranslationHandlerURL");
    }
    
    //Create an array (first time only)
    if(StrArrResourceId == null)
    {
        StrArrResourceIdLength = 0;
        StrArrResourceId = new Array();
    }
    else
    {
        //Check whether the resource id is present inside resource array, if yes return the localized string
        for(i = 0; i < StrArrResourceIdLength ; i++)
        {
            if(StrArrResourceId[i][0].toUpperCase() == ResourceId.toUpperCase())
            {
                return ReplacePlaceHolders(StrArrResourceId[i][1],StrArrPlaceHolders);
            }
        }
    }
    
    //Since the resource id is not present inside resource array, get the localized string for this resource id, store it inside array and return
    XmlHttpRequestObj = null;    
    CreateXmlHttpRequestObject();
        
    if(XmlHttpRequestObj == null)
        return ResourceId;
        
    /*  Note 1: In this case the third parameter is passed as false, it specifies that the request will not be asynchronous
    The reason is that this function needs to return the value from confirm dialog and if the request is made asynchronous, false will
    be returned since it continues the execution.
        Note 2: In case of synchronous request, do not set onreadystatechange property. Since the thread gets blocked for synchronous call
    until it completes/times out. The callback method can not be called in synchronous mode. Instead call that method explicitly after the
    synchronous call is made.
    */
    RedirectUrl = JSTranslationHandlerURL.value + "&ResourceID=" + ResourceId;

    XmlHttpRequestObj.open("GET",RedirectUrl,false);
    XmlHttpRequestObj.send(null);
    
    //Resource id is not present inside array, display response text from XmlHttpRequest
    if(XmlHttpRequestObj.readyState!=4)
	{
		return ResourceId;
	}
	else
	{
		if(XmlHttpRequestObj.status==200)
		{
            StrArrResourceId[StrArrResourceIdLength] = new Array(2);
            StrArrResourceId[StrArrResourceIdLength][0] = ResourceId;
            StrArrResourceId[StrArrResourceIdLength][1] = XmlHttpRequestObj.responseText;
            StrArrResourceIdLength ++;
            return ReplacePlaceHolders(XmlHttpRequestObj.responseText, StrArrPlaceHolders);
		}
	}
}

//This function will replace placeholders with the values in localized string
function ReplacePlaceHolders(StrLocalizedString,StrArrPlaceHolders)
{
    var Delimeter                       = "$AL$";
    var DelimeterForSearchOperation     = "\\$AL\\$";   //This delimeter is used for searching string using regular expression ($ is replaced by \$ since $ is a part of pattern of regular expression).
    var ObjRegEx                        = null;
    var ObjSearchResult                 = null;
    var StrPlaceHolderToSearch          = null;
    var i                               = 0;
    if(StrArrPlaceHolders != null)
    {
        //Iterate over 
        for(i = 0; i < StrArrPlaceHolders.length ; i++)
        {
            if(StrArrPlaceHolders[i][0] != null && StrArrPlaceHolders[i][0] != "")
            {
                //Prefix and suffix delimeter to placeholder, so that this string will be used to search place holder
                StrPlaceHolderToSearch = DelimeterForSearchOperation + StrArrPlaceHolders[i][0] + DelimeterForSearchOperation;
                StrArrPlaceHolders[i][0] = Delimeter + StrArrPlaceHolders[i][0] + Delimeter;
                
                //Define a new Regular Expression for searching strings ignoring case
                ObjRegEx        = new RegExp(StrPlaceHolderToSearch,"i");
                ObjSearchResult = ObjRegEx.exec(StrLocalizedString);
                
                //Replace all the occurances of placeholders
                while(ObjSearchResult != null)
                {
                    //Replace the placeholder with its value. In this case the ObjSearchResult returns the placeholder found in localized string (case sensitive string)
                    StrLocalizedString = StrLocalizedString.replace(ObjSearchResult,StrArrPlaceHolders[i][1],"ig");
                    ObjSearchResult = ObjRegEx.exec(StrLocalizedString);
                }
            }
        }
    }
    
    return StrLocalizedString;
}