﻿// JScript File
var xmlHttp;


// Function to create 'xmlHttp', to be used for sending HTTP requests and receiving HTTP responses.
function GetXmlHttpObject()
{
    // Initializes 'xmlHttp' to 'null'.
    var xmlHttp = null;
    
    // Tries to create 'xmlHttp' for different browsers.
    try
    {
        // For Firefox, Opera 8.0+, Safari.
        xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        try
        {
            // For Microsoft Internet Explorer 6.0+.
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            // For Microsoft Internet Explorer 5.5+.
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    // Returns 'xmlHttp'.
    return xmlHttp;
}


//
//
//      Code block having grammar checking related functions, starts.
//
//

// Function to show required controls for grammar checking.
function ShowGrammarChecker()
{
    document.getElementById("GrammarChecking").style.display = "";
    document.getElementById("lblBanner").innerHTML = "Punjabi Grammar Checker";
    
    // Hides other controls.
    document.getElementById("Morph").style.display = "none";
    document.getElementById("Tagging").style.display = "none";
    document.getElementById("Chunking").style.display = "none";

    document.getElementById("linkbtnGrammarChecker").style.color = "gray";
    document.getElementById("linkbtnMorph").style.color = "firebrick";
    document.getElementById("linkbtnTagger").style.color = "firebrick";
    document.getElementById("linkbtnChunker").style.color = "firebrick";

    document.getElementById("txtMain").focus();
}

// Function to clear various fields.
function ClearGrammar()
{
    document.getElementById("txtMain").value = "";
    document.getElementById("txtResults").value = "";
    document.getElementById("lblResultsMessage").innerHTML = "";
    document.getElementById("linkbtnShowHideResults").innerHTML = "";
    document.getElementById("Results").style.display = "none";

    document.getElementById("txtMain").focus();
}

// Function to show/hide results textbox.
function ShowHideResults()
{
    // Shows the required controls if hidden.
    if(document.getElementById("Results").style.display == "none")
    {
        document.getElementById("Results").style.display = "block";
        document.getElementById("linkbtnShowHideResults").innerHTML = "Hide Details";
    }
    // Otherwise, hides them.
    else
    {
        document.getElementById("Results").style.display = "none";
        document.getElementById("linkbtnShowHideResults").innerHTML = "Show Details";
    }
}

// Function to send AJAX request for grammar checking of the given text.
function CheckGrammar()
{
    // Returns back, if there is nothing to process.
    if(document.getElementById("txtMain").value.length == 0)
    {
        // Clears some controls.
        document.getElementById("txtResults").value = "";
        document.getElementById("lblResultsMessage").innerHTML = "";
        document.getElementById("linkbtnShowHideResults").innerHTML = "";        
        document.getElementById("Results").style.display = "none";
        return;
    }
    
    // Also returns back, if grammar checking is going on.
    if(document.getElementById("btnGrammarCheck").value == "Processing...")
    {
        //alert("Please wait, your earlier request for grammar checking is still in process.");
        return;
    }
    
    // Creates the 'xmlHttp' object.
    xmlHttp = GetXmlHttpObject();
 
    // Shows warning message, if object could not be created.
    if(xmlHttp == null)
    {
        alert("Your browser does not support AJAX!");
        return;
    }
    
    // Clear old results and other fields.
    document.getElementById("txtResults").value = "";
    document.getElementById("lblResultsMessage").innerHTML = "";
    document.getElementById("linkbtnShowHideResults").innerHTML = "";
    
    // Prepares the input i.e. converts unicode to akhar for sending as HTTP request.
    var input = UnicodeToAkhar(document.getElementById("txtMain").value);
    // Calls the method to perform grammar checking, this 'ShowGrammarResults' method
    // will be called everytime 'xmlHttp' changes state.
    xmlHttp.onreadystatechange = ShowGrammarResults;
    
    // Calls the required file to perform grammar checking, this file should return the output in 'Response' object.
    // First agrument tells the method to be used - POST or GET etc.
    // Second argument tells the url to be used.
    // Third arguments tells that request should be handled asynchronously - true or false.
    // Math.random() is used to avoid caching results, using this server thinks it is new request everytime.
    xmlHttp.open("GET", "ajax.aspx?action=CheckGrammar&inputText=" + input + "&id=" + Math.random(), true);
    
    // Calls the method to send the request.
    xmlHttp.send(null);
}

// Function to show the results of grammar checking performed by server.
function ShowGrammarResults()
{
    // If the request is not complete then shows the processing message.
    if(xmlHttp.readyState != 4)
    {
        document.getElementById("btnGrammarCheck").value = "Processing...";
        
        // Clears some results.
        document.getElementById("txtResults").value = "";
        document.getElementById("lblResultsMessage").innerHTML = "";
        document.getElementById("linkbtnShowHideResults").innerHTML = "";
    }

    // Checks if the state of 'xmlHttp' is 4 i.e. request is complete.
    // Other states can be 0 (the resuest is not initialized), 1(the resuest has been set up), 2(the request has been sent),
    // 3(the request is in progress), 4 (the request is complete).
    // status == 200 ensures that results are shown only if processing succeeded without any error message.
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
    {
        try
        {
            
            // Gets the data returned by server and show it to the user.
            // Gets the corrected input.
            var correctedText = xmlHttp.responseXML.getElementsByTagName("CorrectedText")[0].firstChild.data;
            // Gets the error count message.
            var errorCountMessage = xmlHttp.responseXML.getElementsByTagName("ErrorCountMessage")[0].firstChild.data;
            // Gets the error and suggestion results.
            var errorSuggestions = xmlHttp.responseXML.getElementsByTagName("ErrorSuggestions")[0].firstChild.data;

            // Shows the corrected text.
            document.getElementById("txtMain").value = correctedText;
            // Shows the error count.
            //document.getElementById("lblResults").innerHTML = errorCountMessage;
            document.getElementById("lblResultsMessage").innerHTML = errorCountMessage;
            // Shows the error and suggestion results.
            document.getElementById("txtResults").value = errorSuggestions;
            
            // Hides some controls, if no errors found.
            // This '0' value is set in Ajax.aspx.
            if(errorSuggestions == "0")
            {
                document.getElementById("linkbtnShowHideResults").innerHTML = "";
                document.getElementById("Results").style.display = "none";
            }
            // Otherwise, shows the show/hide message, if not showing earlier.
            else if(document.getElementById("linkbtnShowHideResults").innerHTML == "")
            {
                if(document.getElementById("Results").style.display == "block")
                {
                    document.getElementById("linkbtnShowHideResults").innerHTML = "Hide Details";
                }
                else
                {
                    document.getElementById("linkbtnShowHideResults").innerHTML = "Show Details";
                }
            }
        }
        catch(e)
        {
            // Shows the error message.
            document.getElementById("txtResults").value = "Some errors occurred while performing grammar checking, this may be due to improper input format.\nPlease try again, if you have to repeatedly encounter this problem then inform us.";
            
            // Processes some controls.
            document.getElementById("linkbtnShowHideResults").innerHTML = "";
            document.getElementById("Results").style.display = "block";
        }

        // Resets some controls.
        document.getElementById("btnGrammarCheck").value = "Correct Grammar";
    }
}

//
//
//      Code block having grammar checking related functions, ends.
//
//


//
//
//      Code block having morphlogical analysis related functions, starts.
//
//

// Function to show required controls for morphological analysis.
function ShowMorph()
{
    document.getElementById("Morph").style.display = "block";
    document.getElementById("lblBanner").innerHTML = "Punjabi Morphological Analysis";

    // Hides other controls.
    document.getElementById("GrammarChecking").style.display = "none";
    document.getElementById("Tagging").style.display = "none";
    document.getElementById("Chunking").style.display = "none";

    document.getElementById("linkbtnGrammarChecker").style.color = "firebrick";
    document.getElementById("linkbtnMorph").style.color = "gray";
    document.getElementById("linkbtnTagger").style.color = "firebrick";
    document.getElementById("linkbtnChunker").style.color = "firebrick";

    document.getElementById("txtMorphInput").focus();
}

// Funtion to change morph options.
function ChangeMorphOption(option)
{
    // Changes controls for 'Perform morphological analysis'.
    if(option == 0)
    {
        document.getElementById("lblMorphInput").innerHTML = "Specify a word:";
        document.getElementById("btnMorphProcess").value = "Perform Morphological Analysis";
        document.getElementById("btnMorphProcess").title = "Click to perform morphological analysis";
    }
    // Otherwise, changes controls for 'Generate word forms'.
    else if(option == 1)
    {
        document.getElementById("lblMorphInput").innerHTML = "Specify a root word:";
        document.getElementById("btnMorphProcess").value = "Generate Word Forms";
        document.getElementById("btnMorphProcess").title = "Click to generate word forms from given root";
    }
}

// Function to clear various fields.
function ClearMorph()
{
    document.getElementById("txtMorphInput").value = "";
    document.getElementById("txtMorphResults").value = "";

    document.getElementById("txtMorphInput").focus();
}

// Function to send AJAX request for morphological analysis of the given word.
function PerformMorph()
{
     // Returns back, if there is nothing to process.
    if(document.getElementById("txtMorphInput").value.length == 0)
    {
        // Clears some controls.
        document.getElementById("txtMorphResults").value = "";
        return;
    }

   // Creates the 'xmlHttp' object.
    xmlHttp = GetXmlHttpObject();
    
    // Shows warning message, if object could not be created.
    if(xmlHttp == null)
    {
        alert("Your browser does not support AJAX!");
        return;
    }
 
    // Clear old results and other fields.
    document.getElementById("txtMorphResults").value = "";
    
    // Gets the process option i.e. perform morphological analysis or generate word forms.
    var morphOption;
    
    if(document.getElementById("radioMorphOption0").checked)
    {
        morphOption = "Perform";
    }
    else
    {
        morphOption = "Generate";
    }
    
    // Prepares the input i.e. converts unicode to akhar for sending as HTTP request.
    var input = UnicodeToAkhar(document.getElementById("txtMorphInput").value);
    
    // Calls the method to perform morphological analysis, this 'ShowMorphResults()' method
    // will be called everytime 'xmlHttp' changes state.
    xmlHttp.onreadystatechange = ShowMorphResults;
    
    //xmlHttp.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
    // Calls the required file to perform grammar checking, this file should return the output in 'Response' object.
    // First agrument tells the method to be used - POST or GET etc.
    // Second argument tells the url to be used.
    // Third arguments tells that request should be handled asynchronously - true or false.
    // Math.random() is used to avoid caching results, using this server thinks it is new request everytime.
    xmlHttp.open("GET", "ajax.aspx?action=Morph&morphOption=" + morphOption + "&inputText=" + input + "&id=" + Math.random(), true);
    
    // Calls the method to send the request.
    xmlHttp.send(null);
}

// Function to show the results of morphological analysis performed by server.
function ShowMorphResults()
{
    // If the request is not complete then shows the processing message.
    if(xmlHttp.readyState != 4)
    {
        document.getElementById("btnMorphProcess").value = "Processing...";
        
        // Clears some results.
        document.getElementById("txtMorphResults").value = "";
        //document.getElementById("lblResultsMessage").innerHTML = "";
    }

    // Checks if the state of 'xmlHttp' is 4 i.e. request is complete.
    // Other states can be 0 (the resuest is not initialized), 1(the resuest has been set up), 2(the request has been sent),
    // 3(the request is in progress), 4 (the request is complete).
    // status == 200 ensures that results are shown only if processing succeeded without any error message.
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
    {
        try
        {
            // Shows the results.
            document.getElementById("txtMorphResults").value = xmlHttp.responseText;
        }
        catch(e)
        {
            // Shows the error message.
            document.getElementById("txtResults").value = "Some error occurred while performing grammar checking, this may be due to improper input format.\nPlease try again, if you have to repeatedly encounter this problem then inform us.";
            
            // Processes some controls.
            document.getElementById("linkbtnShowHideResults").innerHTML = "";
            document.getElementById("Results").style.display = "block";
        }
    
        // Resets some controls.
        if(document.getElementById("radioMorphOption0").checked)
        {
            document.getElementById("btnMorphProcess").value = "Perform Morphological Analysis"
        }
        else
        {
            document.getElementById("btnMorphProcess").value = "Generate Word Forms"
        }
    }
}


//
//
//      Code block having morphlogical analysis related functions, ends.
//
//


//
//
//      Code block having POS tagging related functions, starts.
//
//

// Function to show required controls for POS tagging.
function ShowTagger()
{
    document.getElementById("Tagging").style.display = "block";
    document.getElementById("lblBanner").innerHTML = "Punjabi POS Tagger";
    
    // Hides other controls.
    document.getElementById("GrammarChecking").style.display = "none";
    document.getElementById("Morph").style.display = "none";
    document.getElementById("Chunking").style.display = "none";

    document.getElementById("linkbtnGrammarChecker").style.color = "firebrick";
    document.getElementById("linkbtnMorph").style.color = "firebrick";
    document.getElementById("linkbtnTagger").style.color = "gray";
    document.getElementById("linkbtnChunker").style.color = "firebrick";

    document.getElementById("txtTaggingInput").focus();
}

// Function to clear various fields.
function ClearTagging()
{
    document.getElementById("txtTaggingInput").value = "";
    document.getElementById("txtTaggingResults").value = "";

    document.getElementById("txtTaggingInput").focus();
}

// Function to send AJAX request for POS tagging of the given word.
function PerformTagging()
{
    // Returns back, if there is nothing to process.
    if(document.getElementById("txtTaggingInput").value.length == 0)
    {
        // Clears some controls.
        document.getElementById("txtTaggingResults").value = "";
        return;
    }
    
    // Creates the 'xmlHttp' object.
    xmlHttp = GetXmlHttpObject();
    
    // Shows warning message, if object could not be created.
    if(xmlHttp == null)
    {
        alert("Your browser does not support AJAX!");
        return;
    }
 
    // Clear old results and other fields.
    document.getElementById("txtTaggingResults").value = "";
    
    // Prepares the input i.e. converts unicode to akhar for sending as HTTP request.
    var input = UnicodeToAkhar(document.getElementById("txtTaggingInput").value);
    
    // Calls the method to perform morphological analysis, this 'ShowTaggingResults()' method
    // will be called everytime 'xmlHttp' changes state.
    xmlHttp.onreadystatechange = ShowTaggingResults;
    
    //xmlHttp.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
    // Calls the required file to perform grammar checking, this file should return the output in 'Response' object.
    // First agrument tells the method to be used - POST or GET etc.
    // Second argument tells the url to be used.
    // Third arguments tells that request should be handled asynchronously - true or false.
    // Math.random() is used to avoid caching results, using this server thinks it is new request everytime.
    xmlHttp.open("GET", "ajax.aspx?action=Tagging&inputText=" + input + "&id=" + Math.random(), true);
    
    // Calls the method to send the request.
    xmlHttp.send(null);
}

// Function to show the results of POS tagging performed by server.
function ShowTaggingResults()
{
    // If the request is not complete then shows the processing message.
    if(xmlHttp.readyState != 4)
    {
        document.getElementById("btnTaggingPerform").value = "Processing...";
        
        // Clears some results.
        document.getElementById("txtTaggingResults").value = "";
    }

    // Checks if the state of 'xmlHttp' is 4 i.e. request is complete.
    // Other states can be 0 (the resuest is not initialized), 1(the resuest has been set up), 2(the request has been sent),
    // 3(the request is in progress), 4 (the request is complete).
    // status == 200 ensures that results are shown only if processing succeeded without any error message.
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
    {
        try
        {
            // Shows the tagging results.
            document.getElementById("txtTaggingResults").value = xmlHttp.responseText;
        }
        catch(e)
        {
            // Shows the error message.
            document.getElementById("txtTaggingResults").value = "Some error occurred while performing POS tagging, this may be due to improper input format.\nPlease try again, if you have to repeatedly encounter this problem then inform us.";
        }
            
        // Resets some controls.
        document.getElementById("btnTaggingPerform").value = "Perform POS Tagging";
    }
}


//
//
//      Code block having POS tagging related functions, ends.
//
//


//
//
//      Code block having phrase chunking related functions, starts.
//
//

// Function to show required controls for Phrase chunking.
function ShowChunker()
{
    document.getElementById("Chunking").style.display = "block";
    document.getElementById("lblBanner").innerHTML = "Punjabi Phrase Chunker";
    
    // Hides other controls.
    document.getElementById("GrammarChecking").style.display = "none";
    document.getElementById("Morph").style.display = "none";
    document.getElementById("Tagging").style.display = "none";

    document.getElementById("linkbtnGrammarChecker").style.color = "firebrick";
    document.getElementById("linkbtnMorph").style.color = "firebrick";
    document.getElementById("linkbtnTagger").style.color = "firebrick";
    document.getElementById("linkbtnChunker").style.color = "gray";

    document.getElementById("txtChunkingInput").focus();
}

// Function to clear various fields.
function ClearChunking()
{
    document.getElementById("txtChunkingInput").value = "";
    document.getElementById("txtChunkingResults").value = "";

    document.getElementById("txtChunkingInput").focus();
}

// Function to send AJAX request for phrase chunking of the given word.
function PerformChunking()
{
    // Returns back, if there is nothing to process.
    if(document.getElementById("txtChunkingInput").value.length == 0)
    {
        // Clears some controls.
        document.getElementById("txtChunkingResults").value = "";
        return;
    }
    
    // Creates the 'xmlHttp' object.
    xmlHttp = GetXmlHttpObject();
    
    // Shows warning message, if object could not be created.
    if(xmlHttp == null)
    {
        alert("Your browser does not support AJAX!");
        return;
    }
 
    // Clear old results and other fields.
    document.getElementById("txtChunkingResults").value = "";
    
    // Prepares the input i.e. converts unicode to akhar for sending as HTTP request.
    var input = UnicodeToAkhar(document.getElementById("txtChunkingInput").value);
    
    // Calls the method to perform morphological analysis, this 'ShowChunkingResults()' method
    // will be called everytime 'xmlHttp' changes state.
    xmlHttp.onreadystatechange = ShowChunkingResults;
    
    //xmlHttp.setRequestHeader("Content-Type","text/xml;charset=UTF-8");
    // Calls the required file to perform grammar checking, this file should return the output in 'Response' object.
    // First agrument tells the method to be used - POST or GET etc.
    // Second argument tells the url to be used.
    // Third arguments tells that request should be handled asynchronously - true or false.
    // Math.random() is used to avoid caching results, using this server thinks it is new request everytime.
    xmlHttp.open("GET", "ajax.aspx?action=Chunking&inputText=" + input + "&id=" + Math.random(), true);
    
    // Calls the method to send the request.
    xmlHttp.send(null);
}

// Function to show the results of phrase chunking performed by server.
function ShowChunkingResults()
{
    // If the request is not complete then shows the processing message.
    if(xmlHttp.readyState != 4)
    {
        document.getElementById("btnChunkingPerform").value = "Processing...";
        
        // Clears some results.
        document.getElementById("txtChunkingResults").value = "";
    }

    // Checks if the state of 'xmlHttp' is 4 i.e. request is complete.
    // Other states can be 0 (the resuest is not initialized), 1(the resuest has been set up), 2(the request has been sent),
    // 3(the request is in progress), 4 (the request is complete).
    // status == 200 ensures that results are shown only if processing succeeded without any error message.
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
    {
        try
        {
            // Shows the tagging results.
            document.getElementById("txtChunkingResults").value = xmlHttp.responseText;
        }
        catch(e)
        {
            // Shows the error message.
            document.getElementById("txtChunkingResults").value = "Some error occurred while performing phrase tagging, this may be due to improper input format.\nPlease try again, if you have to repeatedly encounter this problem then inform us.";
        }
        
        // Resets some controls.
        document.getElementById("btnChunkingPerform").value = "Perform Phrase Chunking";
    }
}


//
//
//      Code block having phrase chunking related functions, ends.
//
//
