﻿// // Javascript for EMAILPRINTOUT user control.

// Defining constants for callback purposes
var EMAILPRINTOUT_SEPARATOR = 'EMAILPRINTOUT_&';
var EMAILPRINTOUT_EMAIL_ACTION = 'EMAIL';
var EMAILPRINTOUT_VALIDATE_ACTION = 'VALIDATE';
var EMAILPRINTOUT_FAIL_ACTION = 'FAIL';
var EMAILPRINTOUT_SUCCESS_ACTION = 'SUCCESS';

// Defining constant strings for user output
var EMAILPRINTOUT_START_MESSAGE = 'Enter your e-mail address and choose your map view type.';
var EMAILPRINTOUT_PROCESSING_MESSAGE = 'Processing your request.....';
var EMAILPRINTOUT_FAIL_MESSAGE = 'The operation has failed! Try again.';
var EMAILPRINTOUT_SUCCESS_MESSAGE = 'Your request has been processed successfully.';

// Defining constant strings control IDs
var EMAILPRINTOUT_MODAL_POPUP_ID = "TitleBanner1_EmailPrintout_ModalPopupExtender";

var EMAILPRINTOUT_MESSAGE_LABEL_ID = "TitleBanner1_EmailPrintout_lbMessage";
var EMAILPRINTOUT_EMAIL_TEXTBOX_ID = "TitleBanner1_EmailPrintout_tbEmail";
var EMAILPRINTOUT_EMAIL_DESCRIPTION_ID = "TitleBanner1_EmailPrintout_tbDescription";
var EMAILPRINTOUT_PRINTOUT_TYPE_DROPLIST_ID = "TitleBanner1_EmailPrintout_dlPrintoutType";
var EMAILPRINTOUT_ACTIVITY_IMAGE_ID = "TitleBanner1_EmailPrintout_activity_image";
var EMAILPRINTOUT_EMAIL_REQUIRED_LABEL_ID = "TitleBanner1_EmailPrintout_emailRequiredLabel";
var EMAILPRINTOUT_EMAIL_INVALID_LABEL_ID = "TitleBanner1_EmailPrintout_emailInvalidLabel";
var EMAILPRINTOUT_SUBMIT_ID = "TitleBanner1_EmailPrintout_buSend";
var EMAILPRINTOUT_CANCEL_ID = "TitleBanner1_EmailPrintout_buClose";

// Defines the amount of time (in milliseconds), that elapses between the user
// receiving the confirmation and the popup window automatically closing down.
var EMAILPRINTOUT_DELAY = 500;
var EMAILPRINTOUT_CLOSE_POPUP_AUTOMATICALLY = true;

var EMAILPRINTOUT_SUBMIT_oldOnMouseClick = null;
var EMAILPRINTOUT_CANCEL_oldOnMouseClick = null;

// CALLBACK METHODS *****************************************************

function EMAILPRINTOUT_callServer(message)
{   
    var action = message;
    
    switch (action) {            
        case EMAILPRINTOUT_VALIDATE_ACTION:
            
            // Disable buttons.
            EMAILPRINTOUT_disableButtons();    
                
            // VALIDATE FORM
            var isFormValid = EMAILPRINTOUT_validateForm();

            if(isFormValid)
            { 
                // Get controls.
                var lbMessage = document.getElementById(EMAILPRINTOUT_MESSAGE_LABEL_ID);
                var dlPrintoutType = document.getElementById(EMAILPRINTOUT_PRINTOUT_TYPE_DROPLIST_ID);
                var tbEmail = document.getElementById(EMAILPRINTOUT_EMAIL_TEXTBOX_ID);
                var tbDescription = document.getElementById(EMAILPRINTOUT_EMAIL_DESCRIPTION_ID);
                var imgActivity = document.getElementById(EMAILPRINTOUT_ACTIVITY_IMAGE_ID);
                                
                // Set components for user response.
                imgActivity.style.display = "inline";
                lbMessage.innerHTML = EMAILPRINTOUT_PROCESSING_MESSAGE;
                lbMessage.style.color = "red";
                
                // Get values from controls.
                var printoutType = dlPrintoutType.options[dlPrintoutType.selectedIndex].value;
                var email = tbEmail.value;
                var description = tbDescription.value;
                
                
                //Construct callback message.
                var callbackMessage =   EMAILPRINTOUT_EMAIL_ACTION + EMAILPRINTOUT_SEPARATOR +
                                        email + EMAILPRINTOUT_SEPARATOR +
                                        printoutType + EMAILPRINTOUT_SEPARATOR +
                                        description + EMAILPRINTOUT_SEPARATOR;
                
                // INVOKING CALLBACK
                EMAILPRINTOUT_DoCallBack(callbackMessage, "EmailPrintout"); 
            }
            else
            {
                // ENABLE BUTTONS
                EMAILPRINTOUT_enableButtons();
            }
            
            break
        default:
            break
    }//end Switch  
    
}


function EMAILPRINTOUT_processMyResult (returnMessage, context)
{
    var splittedReturnMessage = returnMessage.split(EMAILPRINTOUT_SEPARATOR);
    var action = splittedReturnMessage[0];
    
    switch (action) {
        case EMAILPRINTOUT_FAIL_ACTION:
            
            // GET CONTROLS TO SET.
            var lbMessage = document.getElementById(EMAILPRINTOUT_MESSAGE_LABEL_ID);
            var imgActivity = document.getElementById(EMAILPRINTOUT_ACTIVITY_IMAGE_ID);
            
            // SET CONTROLS.
            imgActivity.style.display = 'none';
            lbMessage.innerHTML = EMAILPRINTOUT_FAIL_MESSAGE;
            
            // ENABLE BUTTONS
            EMAILPRINTOUT_enableButtons();
            
            break
        case EMAILPRINTOUT_SUCCESS_ACTION:
            /* !!! old
            // GET CONTROLS TO SET.
            var lbMessage = document.getElementById(EMAILPRINTOUT_MESSAGE_LABEL_ID);
            var imgActivity = document.getElementById(EMAILPRINTOUT_ACTIVITY_IMAGE_ID);
            
            // SET CONTROLS.
            imgActivity.style.display = 'none';
            lbMessage.innerHTML = EMAILPRINTOUT_SUCCESS_MESSAGE;
            
            // CLOSE WINDOW
            if(EMAILPRINTOUT_CLOSE_POPUP_AUTOMATICALLY)
                window.setTimeout("EMAILPRINTOUT_Close();", EMAILPRINTOUT_DELAY);  
            */
            
            EMAILPRINTOUT_Close();
            window.setTimeout("CONFIRMATIONDIALOG_ShowDialog('EMAIL');",EMAILPRINTOUT_DELAY);
            
            break            
        default:
            break
    }//end Switch
}

function EMAILPRINTOUT_postMyError(returnmessage, context)
{
    alert("Callback Error: " + returnmessage + ", " + context);
}

// ****************************************************************

// VALIDATE FORM
function EMAILPRINTOUT_validateForm()
{
    var formIsValid = true;
    var emailTextField = document.getElementById(EMAILPRINTOUT_EMAIL_TEXTBOX_ID);
    var emailRequiredLabel = document.getElementById(EMAILPRINTOUT_EMAIL_REQUIRED_LABEL_ID);
    var emailInvalidLabel = document.getElementById(EMAILPRINTOUT_EMAIL_INVALID_LABEL_ID);
    
    // RESET LABELS' VISIBILITY
    emailRequiredLabel.style.display = "none";
    emailInvalidLabel.style.display = "none";
    
    // VALIDATE EMAIL FIELD
    if(emailTextField.value == '')
    {
        formIsValid = false;
        emailRequiredLabel.style.display = "inline";
    }
    else
    {
        var isEmailValid = STRING_echeck(emailTextField.value);
        if(!isEmailValid)  
        {
            formIsValid = false;
            emailInvalidLabel.style.display = "inline";
        }
    }
       
    return formIsValid;
}

function EMAILPRINTOUT_Close()
{      
    // CLOSE MODAL POPUP WINDOW
    var modalPopupBehavior = $find(EMAILPRINTOUT_MODAL_POPUP_ID);
    if(modalPopupBehavior != null)
    {
        modalPopupBehavior.hide();
    }  
}

function EMAILPRINTOUT_Show()
{      
    // OPEN MODAL POPUP WINDOW
    var modalPopupBehavior = $find(EMAILPRINTOUT_MODAL_POPUP_ID);
    if(modalPopupBehavior != null)
    {
        modalPopupBehavior.show();
    }
   
    // RESET CONTROLS
    EMAILPRINTOUT_resetControls(); 
   
    // Set focus on Email text field.
    var lbEmail = document.getElementById(EMAILPRINTOUT_EMAIL_TEXTBOX_ID);
    lbEmail.focus();
    
}

function EMAILPRINTOUT_resetControls()
{
    // Get controls.
    var lbMessage = document.getElementById(EMAILPRINTOUT_MESSAGE_LABEL_ID);
    var dlPrintoutType = document.getElementById(EMAILPRINTOUT_PRINTOUT_TYPE_DROPLIST_ID);
    var tbEmail = document.getElementById(EMAILPRINTOUT_EMAIL_TEXTBOX_ID);
    var imgActivity = document.getElementById(EMAILPRINTOUT_ACTIVITY_IMAGE_ID);
    var lbEmailRequired = document.getElementById(EMAILPRINTOUT_EMAIL_REQUIRED_LABEL_ID);
    var lbEmailInvalid = document.getElementById(EMAILPRINTOUT_EMAIL_INVALID_LABEL_ID);
    var tbDescription = document.getElementById(EMAILPRINTOUT_EMAIL_DESCRIPTION_ID);
    
    //Reset controls.
    lbMessage.innerHTML = EMAILPRINTOUT_START_MESSAGE;
    lbMessage.style.color = "black";
    tbEmail.value = "";
    tbDescription.value = "";
    dlPrintoutType.selectedIndex = 0;
    lbEmailRequired.style.display = 'none';
    lbEmailInvalid.style.display = 'none';
    imgActivity.style.display = 'none';

    
    // Enable buttons.
    EMAILPRINTOUT_enableButtons()
    
}

function EMAILPRINTOUT_enableButtons()
{    
    var closelLabel = document.getElementById(EMAILPRINTOUT_CANCEL_ID);    
    var submitLabel = document.getElementById(EMAILPRINTOUT_SUBMIT_ID); 
        
    if(EMAILPRINTOUT_SUBMIT_oldOnMouseClick != null)
        submitLabel.onclick = EMAILPRINTOUT_SUBMIT_oldOnMouseClick;
    submitLabel.disabled = false;
    
    if(EMAILPRINTOUT_CANCEL_oldOnMouseClick != null)
        closelLabel.onclick = EMAILPRINTOUT_CANCEL_oldOnMouseClick;
    closelLabel.disabled = false;
}

function EMAILPRINTOUT_disableButtons()
{    
    var closelLabel = document.getElementById(EMAILPRINTOUT_CANCEL_ID);    
    var submitLabel = document.getElementById(EMAILPRINTOUT_SUBMIT_ID); 
        
    EMAILPRINTOUT_SUBMIT_oldOnMouseClick = submitLabel.onclick;
    submitLabel.onclick = null;
    submitLabel.disabled = true;    
    
    EMAILPRINTOUT_CANCEL_oldOnMouseClick = closelLabel.onclick;
    closelLabel.onclick = null;
    closelLabel.disabled = true;
}

// ****** KEY EVENTS *******************************
// **
// **
function EMAILPRINTOUT_stopEvent(e) {
	if(!e) var e = window.event;
	
	//e.cancelBubble is supported by IE - this will kill the bubbling process.
	e.cancelBubble = true;
	e.returnValue = false;

	//e.stopPropagation works only in Firefox.
	if (e.stopPropagation) {
		e.stopPropagation();
		e.preventDefault();
	}
	return false;
}

function EMAILPRINTOUT_OnKeyDown(e)
{
  if (e.keyCode == 27) 
  {
    EMAILPRINTOUT_Close();
    EMAILPRINTOUT_stopEvent(e);
  } 
  else if (e.keyCode == 13) 
  {
      EMAILPRINTOUT_callServer(EMAILPRINTOUT_VALIDATE_ACTION);
      EMAILPRINTOUT_stopEvent(e);
  }
}


// ************************************************

