﻿// JScript File

/*
  Called to set up or update the timer driven slide effect.
  If the specified cookie value is not set, the dialog is shown
*/
function FriendlyPopUp_SetupDialogIfCookieBlank(cookieName, idDialog, xNextPosition, endPosition, timerIncrement)
{
   var cookieValue = GetCookie(cookieName);
   if(cookieValue == null)
   {
    FriendlyPopUp_UpdateSlidingDialog(idDialog, xNextPosition, endPosition, timerIncrement);
   }
}


/* 
 Text for the the function callback we place into the timer
*/
function FriendlyPopUp_UpdateSlidingDialog_Parameters(idDialog, xNextPosition, endPosition)
{
    return "FriendlyPopUp_UpdateSlidingDialog('" + idDialog + "' ," + xNextPosition + ", " + endPosition + ")";
}

/*
  Called to set up or update the timer driven slide effect
*/
function FriendlyPopUp_UpdateSlidingDialog(idDialog, xNextPosition, endPosition, timerIncrement)
{
    //Get the dialog Div
	var elementHelperText = document.getElementById(idDialog);
	var func_call_text;
	
    //If we have no element yet; then the page is not yet fully loaded
    //and we want to defer this call
    if(elementHelperText == null)
    {
        func_call_text = FriendlyPopUp_UpdateSlidingDialog_Parameters(idDialog, xNextPosition, endPosition);

        if(timerIncrement < 500) timerIncrement = 500;
        
        //Set the callback timer
        window.setTimeout(func_call_text, timerIncrement);
        return;
    }
 
    //Make sure we don't overshoot
    if(xNextPosition > endPosition)
    {
        xNextPosition = endPosition;
    }

    //Put it into position
	elementHelperText.style["left"] = xNextPosition + "px";


    //If we still have room to move, then set the timer to fire again
    if(xNextPosition < endPosition)
    {
        //Set of for our next move
        xNextPosition = xNextPosition + 20;

        //If no timer increment is supplied, have it wait
        if(timerIncrement == null) timerIncrement = 35;
        
        //Set the callback timer
        func_call_text = FriendlyPopUp_UpdateSlidingDialog_Parameters(idDialog, xNextPosition, endPosition);
        window.setTimeout(func_call_text, timerIncrement);
    }
}


/*
 Called when the user clicks on the hint-text that sits on top of the form
*/
function FriendlyPopUp_TextBoxInputHintClicked(idHelperText, idTextBox)
{

    //Get the control
	var elementHelperText = document.getElementById(idHelperText);

    //hide it
	elementHelperText.style["visibility"] = "hidden";
	
	//Change the focus to the underlying textbox
	var elementInputBox = document.getElementById(idTextBox);
	elementInputBox.focus(); 
}

/*
 Called when the user has chosen a dialog option and we want to post the form back to the server
 for processing.
*/
function FriendlyPopUp_HideDialogAndSubmit(idDialogResult, idDialogDiv)
{
    //Store the 'OK' value to POST up to the server
	var elementDialogResult = document.getElementById(idDialogResult);
    elementDialogResult.value = "OK";

    //Get the dialog Div
	var elementHelperText = document.getElementById(idDialogDiv);
    //hide it right away, so the user get's this visual response (w/out the page re-serve latency)
	elementHelperText.style["visibility"] = "hidden";

    FriendlyPopUp_SumbitForm();
}

/*
 Called when the user has chosen a dialog option and we want to post the form back to the server
 for processing.
*/
function FriendlyPopUp_HideDialogAndSubmitCancel(idDialogResult, idDialogDiv, idTextBox)
{
    //Store the CANCEL value to post up to the server
	var elementDialogResult = document.getElementById(idDialogResult);
    elementDialogResult.value = "CANCEL";

    //Clear out any existing text in the textbox; so it will be blank if the 
    //dialog is reshown
    if(idTextBox != null)
    {
	    var elementTextbox = document.getElementById(idTextBox);
        elementTextbox.value = "";
    }

    //Get the dialog Div
	var elementHelperText = document.getElementById(idDialogDiv);
    //hide it right away, so the user get's this visual response (w/out the page re-serve latency)
	elementHelperText.style["visibility"] = "hidden";

    FriendlyPopUp_SumbitForm();
}

/*
 Post the form back to the server
*/
function FriendlyPopUp_SumbitForm()
{
   //UNDONE: Assumption is that we only have 1 form on the page. (ASP.NET seems to insist on this being the case)
   //Submit the form
    document.forms[0].submit();   
}

/* Test to see if the user hits Enter in the textbox; if so treat it as a Form submit*/
function FriendlyPopUp_TextBoxKeyPress(e, idDialogResult, idDialogDiv)
{
	if (!e.keyCode) var e = window.event; //Possibly IE6? (IE7 passes in an 'e'
	    
    //NOTE: Firefox also uses a mutually exclusive e.keyChar for visible chars
    if(e.keyCode == 13)
    {
        //Submit the response...
        FriendlyPopUp_HideDialogAndSubmit(idDialogResult, idDialogDiv);
    }

}

