//this file works with getNotified.php - located at the root on the server.  The stage and live versions of the php file are slightly different in that one points to copper.upromise.com and one points to lty.s.upromise.com


function subscribe(strEmail)
{
    if (strEmail == '' )
    {
        alert('Please enter an email address in the text box');
    }
    else
    {
        if (testEmail(strEmail))
        {
            getNotified('subscribe', 'finish_subscribe(transport)', strEmail);  
        }
        else
        {
            alert('Please enter a valid email address in the text box');
        }
    }
}

function finish_subscribe(strResponse)
{
   // alert('finish subscribe happening');
    //alert('strResponse=' + strResponse);
    objDiv = document.getElementById('subscriptionSection');
    if (strResponse == '200')
    {
        objDiv.innerHTML = '<b>Thank you for subscribing.</b>';
    }
    else
    {
        objDiv.innerHTML = '<b>Service is temporarily unavailable. Please retry shortly.</b>';
    }   
    document.getElementById('lnkUnsubscribe').style.visibility = 'hidden';
    document.getElementById('lblGNSeparator').style.visibility = 'hidden';
    
}

function finish_unsubscribe(strResponse)
{
   // alert('finish unsubscribe happening');
    //alert('strResponse=' + strResponse);
    objDiv = document.getElementById('subscriptionSection');
    if (strResponse == '200')
    {
        objDiv.innerHTML = '<b>Thank you. We have received your unsubscribe request.</b>';
    }
    else
    {
        objDiv.innerHTML = '<b>Service is temporarily unavailable. Please retry shortly.</b>';
    }       
    document.getElementById('lnkUnsubscribe').style.visibility = 'hidden';
    document.getElementById('lblGNSeparator').style.visibility = 'hidden';
    
}

function unsubscribe(strEmail)
{
    if (strEmail == '' )
    {
        alert('Please enter an email address in the text box');
    }
    else
    {
        if (testEmail(strEmail))
        {
            getNotified('unsubscribe', 'finish_unsubscribe(transport)', strEmail);  
        }
        else
        {
            alert('Please enter a valid email address in the text box');
        }
    }
       
}

function getNotified(strMethod,strReturnFunction,strEmail)
{
    strPath = '/getNotified.php?gn_method=' + strMethod + '&email=' + strEmail;
        //alert(strReturnFunction);

    $.ajax(
        {
            url: strPath,
            type: 'GET',
            timeout: 15000,
            success: function(transport)
            {
                eval(strReturnFunction);
            }
            ,
            error: function (xhr, error)
            {
                alert('error loading get notified');
                alert(error);

            }
        });
}


function testEmail(src) {
     var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }
