var txtYourName;
var txtYourEmail;
var txtTheirName;
var txtTheirEmail;
var lblResult;
var lnkSend;
var imgSend;
var MASTERPAGE_NAME_PREFIX = "ctl00$main$";
var MASTERPAGE_ID_PREFIX = "ctl00_main_";
var FIRST_SEP = '|';

String.prototype.trim = function () 
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function pageLoad()
{
    txtYourName = $get(MASTERPAGE_ID_PREFIX + "txtYourName");
    txtYourEmail = $get(MASTERPAGE_ID_PREFIX + "txtYourEmail");
    txtTheirName = $get(MASTERPAGE_ID_PREFIX + "txtTheirName");
    txtTheirEmail = $get(MASTERPAGE_ID_PREFIX + "txtTheirEmail");
    lblResult = $get("lblResult");
    lnkSend = $get("lnkSend");
    imgSend = $get("imgSend");
}

function validatePage()
{
    if (txtYourName.value.trim() == "" || txtYourEmail.value.trim() == "" ||
        txtTheirName.value.trim() == "" || txtTheirEmail.value.trim() == "")
    {
        lnkSend.href = "";
        return false;    
    }
    else
    {
        lnkSend.href = "mailto:" + txtTheirEmail.value.trim() + "?subject=CellarDoorMETRICS" +
            "&body=Hello " + txtTheirName.value.trim() + ",%0A%0A" +
            "I've just checked out the CellarDoorMETRICS website.  It's a great web tool for wineries to identify ways to increase profits through our cellar door and tasting rooms.%0A%0A" +
            "Just by updating visitor and sales information online each week, you can follow trends in consumer direct sales from your winery and compare results with local wineries - and even wineries across the country and internationally.%0A%0A" +  
            "Here's a sample of what you could learn......%0A" +
            "Q: How many visitors do wineries attract to their cellar doors and tasting rooms in my region?  ......do I get my fair share?%0A" +
            "Q: Which days attract the highest number of visitors? ......now I know which trading days are likely to bring the best results.%0A" +
            "Q: What kind of revenues do other cellar doors attract each week?......how do my takings compare?%0A%0A" +
            "Q: What difference does having a restaurant, function rooms or accommodation make to weekly revenues in my region?  What about winery tours and merchandise?......now I can see the likely contributions that these facilities and services could bring to my business.%0A%0A" +
            "By tracking wine tourism performance on CellarDoorMETRICS, you can answer these questions - and keep more profits at the winery.%0A%0A" +
            "Check out CellarDoorMETRICS at www.cellardoormetrics.com.%0A%0A" +
            "Regards,%0A" + txtYourName.value.trim();
        return true;
    }
}

function SendEmail()
{
    if (validatePage())
    {
        PageMethods.SendEmail(txtYourName.value.trim(), txtYourEmail.value.trim(),
            txtTheirName.value.trim(), txtTheirEmail.value.trim(), OnSucceeded, OnFailed);
        
        return true;
    }
    else
    {
        lblResult.innerHTML = 'Please fill in all of the required fields';
        return false;
    }
}


function OnSucceeded(result, userContext, methodName) 
{
    if (methodName == "SendEmail")
    {
        return true;
    }
    return true;
}

function OnFailed(error, userContext, methodName) 
{
    if(error !== null) 
    {
        alert("An error occurred: " + error.get_message());
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
