﻿// Display popup message
function ShowContactPopup(itemName) 
{
  // Hide previous animation
  document.getElementById('ajaxLoderContactSent').style.visibility = "hidden";
  
  $find("contactModalBehaviour").show(); 
}

// Hide popup message
function HideContactPopup() 
{
 $find("contactModalBehaviour").hide(); 
}

// Clear all quote form elements
function ClearContacteFormGuests(firstName, lastName, email, details, phonePrefix, phoneNumber, guestsNum)
{
    // Remove all filled text
    document.getElementById(firstName).value         = "";
    document.getElementById(lastName).value          = "";
    document.getElementById(email).value             = "";
    document.getElementById(details).value           = "";
    document.getElementById(guestsNum).selectedIndex = 0;      
    
    // Set focus at the first field
    document.getElementById(firstName).focus();
}

// Sent contact email
function ValidateAndSendMailConfirm(firstName, lastName, email, details, phonePrefix, phoneNumber, guestsNum) {
    // Cause client validation
    if (Page_ClientValidate()) {
        // Show loading indication animation
        document.getElementById('ajaxLoderContactSent').style.visibility = "visible";

        var contact = new Object();

        contact.FirstName   = firstName   != null ? document.getElementById(firstName).value   : "";
        contact.LastName    = lastName    != null ? document.getElementById(lastName).value    : "";
        contact.Email       = email       != null ? document.getElementById(email).value       : "";
        contact.Comments    = details     != null ? document.getElementById(details).value     : "";
        contact.NumOfGuests = guestsNum   != null ? getSelectedText(guestsNum)                 : "";
        contact.PhonePrefix = phonePrefix != null ? document.getElementById(phonePrefix).value : "";
        contact.PhoneNumber = phoneNumber != null ? document.getElementById(phoneNumber).value : "";

        $.ajax({
            type:        "POST",
            contentType: "application/json; charset=utf-8",
            url:         "Ella.aspx/SendMail",
            data:        "{'contact':" + JSON.stringify(contact) + "}",
            dataType:    "json",
            success :    AjaxSucceeded,
            error :      AjaxFailed
        });
    }
}

function AjaxSucceeded(result) 
{
    document.getElementById('ajaxLoderContactSent').style.visibility = "hidden";

    // Close quote popup
    HideContactPopup();
    
    // If menu code it found, display the chosen data
    if (result.d != null) 
    {
        // Temp solution
        var isEllaPage   = (top.location.href.toLowerCase().indexOf('ella.aspx') > 0);
        var sentMessage  = isEllaPage ? "ההודעה נשלחה בהצלחה" : "Message sent successfully";
        var errorMessage = isEllaPage ? "בעיה בשליחת ההודעה, נסה שוב מאוחר יותר" : "Problem sending mail, please try again later";
        
        // Set reault message to the popup window (hebrew makes the short IF statment true/fales parts to be reversed)
        document.getElementById('spnMsgMailResult').innerHTML = (result.d ? sentMessage : errorMessage);

        // Show send result
        $find("mailResultModalBehaviour").show();
    }
    else 
    {
        top.location = "error.aspx";
    }
}

function AjaxFailed(result) {
   // alert(result.status + ' ' + result.statusText);
} 

// Get drop down list selected text
function getSelectedText(elementId)
{
    // Get values
    var selectList    = document.getElementById(elementId);
    var selectedIndex = selectList.selectedIndex;

    return selectList.options[selectedIndex].text;
}


// Clear all quote form elements
function ClearContacteForm(firstName, lastName, email, details) 
{
    // Remove all filled text
    document.getElementById(firstName).value = "";
    document.getElementById(lastName).value = "";
    document.getElementById(email).value = "";
    document.getElementById(details).value = "";

    // Set focus at the first field
    document.getElementById(firstName).focus();
}

// Execute command after modal popup been closed
function ModalPopupClosed() 
{
    // Nothing at the omment
}

// Display popup message
function ShowBlogsPopup(itemName) 
{
  $find("blogsModalBehaviour").show();
}

function OpenURL(url)
{
    window.open(url);
}
