﻿function showSiteTellAFriend(source)
{
    this._txtYourName = document.getElementById("ctl00_txtYourNameSite");
    this._txtYourEmail = document.getElementById("ctl00_txtYourEmailSite");
    this._txtFriendName = document.getElementById("ctl00_txtFriendNameSite");
    this._txtFriendEmail = document.getElementById("ctl00_txtFriendEmailSite");
    this._btnSend = document.getElementById("ctl00_btnSendSite");
    this._hdnYourName = document.getElementById("ctl00_hdnYourName");
    this._hdnYourEmail = document.getElementById("ctl00_hdnYourEmail");
    
    this._txtYourName.value = this._hdnYourName.value;
    this._txtYourEmail.value = this._hdnYourEmail.value;
    this._txtFriendName.value = '';
    this._txtFriendEmail.value = '';
    
    this._txtYourName.className = 'resetErrorState';
    this._txtYourEmail.className = 'resetErrorState';
    this._txtFriendName.className = 'resetErrorState';
    this._txtFriendEmail.className = 'resetErrorState';
    
    this._btnSend.enabled = true;
    this._btnSend.disabled = false;
    this._btnSend.value = "Submit";
    
    this._source = source;
    this._popup = $find('mdlTellAFriendSite');
    this._popup.show();
}

function btnSubmitFriendSite_click()
{
    var isError = false;
    
    if (this._txtYourName.value == "")
    {
        this._txtYourName.className = 'invalidInput';
        isError = true;
    }
    else
    {
        this._txtYourName.className = 'resetErrorState';
    }
    
    if (this._txtYourEmail.value == "")
    {
        this._txtYourEmail.className = 'invalidInput';
        isError = true;
    }
    else
    {
        this._txtYourEmail.className = 'resetErrorState';
    }
    
    if (this._txtFriendName.value == "")
    {
        this._txtFriendName.className = 'invalidInput';
        isError = true;
    }
    else
    {
        this._txtFriendName.className = 'resetErrorState';
    }
    
    if (this._txtFriendEmail.value == "")
    {
        this._txtFriendEmail.className = 'invalidInput';
        isError = true;
    }
    else
    {
        this._txtFriendEmail.className = 'resetErrorState';
    }
    
    if (!(isError))
    {
        this._btnSend.enabled = false;
        this._btnSend.disabled = true;
        this._btnSend.value = "Processing...";
        
        // get the root of the site's URL
        var URLRoot = location.href;
        URLRoot = URLRoot.replace(location.pathname, "");
        URLRoot = URLRoot.replace(location.search, "");
        
        infostation.web.WebServices.Communications_Service.TellAFriendSite(this._txtYourName.value, this._txtYourEmail.value, this._txtFriendName.value, this._txtFriendEmail.value, URLRoot, tellAFriendSiteCallBack);
    }
}

function tellAFriendSiteCallBack(result)
{
    if (result)
    {
          btnFriendSiteModalClose_click();
    }
    else
    {
        this._btnSend.value = "Error occured. Close this window and try again.";
    }
}

function btnFriendSiteModalClose_click()
{
    this._txtYourName.value = '';
    this._txtYourEmail.value = '';
    this._txtFriendName.value = '';
    this._txtFriendEmail.value = '';
    
    this._txtYourName.className = 'resetErrorState';
    this._txtYourEmail.className = 'resetErrorState';
    this._txtFriendName.className = 'resetErrorState';
    this._txtFriendEmail.className = 'resetErrorState';
    
    this._btnSend.enabled = true;
    this._btnSend.disabled = false;
    this._btnSend.value = "Submit";
    
    this._txtYourName = null;
    this._txtYourEmail = null;
    this._txtFriendName = null;
    this._txtFriendEmail = null;
    
    this._popup.hide();
    this._popup = null;
    this._source = null;
}