//*************************************************************************************
// PAGE:    Captcha_VerifyCode.js
// YEAR:    2008 (September)
// AUTHOR:  IE
// CLIENT:  IMC Security Code Check
// PURPOSE: Verifies the entered Security Code is valid
//*************************************************************************************

var lngTimer = null;
var lngRPC_Result = 100;    // Need to Lookup
var strRPC_Error = "You entered the wrong Security Code.";
var strEnteredCode_URL = strNASA_RootHttps + "_ASP/Captcha_VerifyCode.asp?";
//var strEnteredCode_URL = "http://www.n-a-s-a.com/_ASP/Captcha_VerifyCode.asp?";
//
//    alert( "strEnteredCode_URL = " + strEnteredCode_URL );



function ValidateCaptcha( objThis )
 {
    var strCodeNumberTextBox = objThis.value;
/*
    alert( "lngRPC_Result = " + lngRPC_Result );
    if (lngRPC_Result == 100)
       {
       }
*/
    strCodeNumberTextBox = strCodeNumberTextBox.toUpperCase();

    objThis.value = strCodeNumberTextBox;
    document.getElementById( "btnSubmit" ).disabled = true;

    if (!Captcha_VerifyCode( strCodeNumberTextBox ))
       {
        alert( strRPC_Error );

        RefreshImage( 'imgCaptcha' );

        lngRPC_Result == 101;    // Need to Lookup

        //objThis.value = "";
        document.getElementById( "btnSubmit" ).disabled = true;

        //objThis.focus() = false;

        return( false );
       }

    return( true );
 }



function RefreshImage( valImageId )
 {
    var objImage = document.images[valImageId];
/*
    alert( "valImageId = '" + valImageId + "'\n" +
           "objImage = " + objImage + "\n" +
           "objImage.src = '" + objImage.src + "'" );
*/
    if (objImage == undefined) return;

    var now = new Date();
    objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();

    document.getElementById( "txtCodeNumberTextBox").value = '';
    document.getElementById( "btnSubmit" ).disabled = true;
 }



//*************************************************************************************
// Sends the RPC given in url.  The server will return JavaScript that
// calls rpcComplete() with the result.
//*************************************************************************************
function sendRPC( url, objCallBack )
 {
    // If an RPC is pending, Cancel it.
    clearTimeout( lngTimer );

    var newScript = document.createElement( 'script' );

    newScript.src = url;
    newScript.type = "text/javascript";


    lngRPC_Result = 102;    // Waiting for Lookup Results
    if (objCallBack != null)
       {
        lngTimer = setTimeout( objCallBack, 200 );     // Check for completion in 200ms
       }

    document.body.appendChild( newScript );
 }



//*************************************************************************************
// VERIFYCODE -- Perform Edit Check, Check is Valid and Check matches Security Image
//*************************************************************************************
function Captcha_VerifyCode( strEnteredCode )
 {
    //alert("VERIFYING Security Code  '" + strEnteredCode + "'\n");
    //alert("ENTEREDCODE Length =  '" + strEnteredCode.length + "'\n"); 

    var intChar;
    var intMinSize;
    var intMaxSize;

    var params;
    var strChar;
    //var strEnteredCode
    var strEnteredCodeChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


    intMinSize =  6;
    intMaxSize =  6;


    lngRPC_Result = 101;    // Need to Lookup

    document.getElementById( "btnSubmit" ).disabled = true;
  

    //****CHECK FOR LENGTH, AND NULL VALUES****//
    if ((strEnteredCode == null)  ||  
        (strEnteredCode == undefined)  ||  (strEnteredCode.length == 0))
       {
        strRPC_Error = "You must enter the Security Code.";

        //document.getElementById( "btnSubmit" ).disabled = false;

        return( false );
       }


    strEnteredCode = strEnteredCode.toUpperCase();
    document.getElementById( "txtCodeNumberTextBox" ).value = strEnteredCode;

    if ( (strEnteredCode.length < intMinSize)  ||  (strEnteredCode.length > intMaxSize) )
       {
        strRPC_Error = "You entered the wrong Security Code!.";

        //document.getElementById( "btnSubmit" ).disabled = false;

        return( false );
       }


    strChar = strEnteredCode.substr( 0, 1 )
    intChar = strChar.charCodeAt( 0 );

    if ((intChar < 33)  ||  (intChar > 126)) 
       {
        strRPC_Error = "You entered the wrong Security Code!.";

        //document.getElementById( "btnSubmit" ).disabled = false;

        return( false );
       }
    else
       {
        intIndex1Max = strEnteredCode.length;
        for ( intIndex1 = 0 ;  intIndex1 <= intIndex1Max ;  intIndex1++ )
            {
             strChar = strEnteredCode.substr( intIndex1, 1 )
             intChar = strChar.charCodeAt( 0 );

             if ((intChar < 33)  ||  (intChar > 126)  ||  
                 (strEnteredCodeChars.indexOf( strChar ) == -1)) 
                {
                 strRPC_Error = "You entered the wrong Security Code!.";

                 //document.getElementById( "btnSubmit" ).disabled = false;

                 return( false );
                }
            }
       }


    params = "strEnteredCode=" + escape( strEnteredCode );

//alert( "strEnteredCode_URL + params = '" + strEnteredCode_URL + params + "'" );
    sendRPC( strEnteredCode_URL + params, null );
    //sendRPC( strEnteredCode_URL + params, RPC_ReadResponse( ) );

    return( true );
 }



function RPC_Complete( rpcResult )
 {
    // RPC is complete, so check the result
    if (rpcResult == true)
       {
        lngRPC_Result = 103;    // Results - NOT Matched

        //alert( "ERROR!!  Invalid Code Entered" );

        document.getElementById( "btnSubmit" ).disabled = true;
       }
    else
        if (rpcResult == false)
           {
            lngRPC_Result = 111;    // Results - Match Found

            //alert( "ERROR!! Our Records indicate a Valid Security Code entered." );

            document.getElementById( "btnSubmit" ).disabled = false;
           }
        else
           {
            lngRPC_Result = 112;    // Results - Match Found
 
            alert( "ERROR!!  NOT a true or false." );
           }

 }



function RPC_ReadResponse( )
 {
    // Check if RPC has completed
    if (lngRPC_Result == 102)
       {
        lngTimer = setTimeout( RPC_ReadResponse, 100 );     // Check for completion in 100ms

        return;
       }

 /*
    // Else RPC has completed, so check lngRPC_Result code
    // lngRPC_Result = 101  ' Need to Lookup
    // lngRPC_Result = 102  ' Lookup in progress
    // lngRPC_Result = 103  ' Security Code NO Match found
    // lngRPC_Result = 111  ' Security Code    Match found
    if (lngRPC_Result == 111)
       {
        clearTimeout( lngTimer );

        lngTimer = null;
        //lngRPC_Result = 101;

        document.getElementById( "btnSubmit" ).disabled = false;

        //ValidationPassed( );
       }
*/

    clearTimeout( lngTimer );

    lngTimer = null;
    //lngRPC_Result = 101;

    //document.getElementById( "btnSubmit" ).disabled = false;
    
 }



function CheckEnteredCaptchaLength( strObjectType, objThis, lngCharLimit )
 {
    var strProfile = objThis.value;
    var lngProfileLength = strProfile.length;

    objThis.value = strProfile.toUpperCase()
    lngCharLimit = parseInt( lngCharLimit );

    if (lngProfileLength < lngCharLimit)
       {
        return( true );
       }

    if (lngProfileLength >= lngCharLimit)
       {
        ValidateCaptcha( objThis );

        return( false );
       }

    return( true );
 }