﻿var swf = null;
var newToon = false;

function loaded()
{
    swf = getEmbedObject("swfToon");

    if (newToon)
    {
        getFields();
        updateFields();
    }
    else
        setFields();
    rewind();
}

function switchEcards()
{
    getObject("divViewer").style.display = "none";
    getObject("divSwitch").style.display = "block";
}

function cancelSwitch()
{
    as3Ready = false;
    getObject("divSwitch").style.display = "none";
    getObject("divViewer").style.display = "block";
}

function showShare()
{
    getObject("divSteps").style.display = "none";
    getObject("divShareStep").style.display = "block";
}

function loadNew()
{
    newToon = true;
}

function initializeToon()
{
    getObject("divAnimation").innerHTML = 
        getSwf("swfToon", getObject("_ctl0_bodyContent_hfAnimation").value, 400, 291, true, "direct", 8);   
}

function play()
{
    swf.TGotoLabel("/", "start");
    swf.Play();
}

function rewind()
{
    swf.TGotoLabel("/", "start");
    swf.Stop();
}

function setFields()
{
    updateFields();
}

function getFields()
{
    getTo();
    getFrom();
    getText1();
    getText2();
    getWrapup();
}

function filterHTML(text)
{
    if (text == null) text = "";
    
    text = text.replace(/\x01/g, "");
    text = text.replace(/<BR>/g, "\n");
    text = text.replace(/<br>/g, "\n");
    text = text.replace(/<\/P>/g, " \n");
    text = text.replace(/<\/p>/g, " \n");
    text = text.replace(/<[^>]*>/g, "");
    text = text.replace(/  /g, " ")
    text = text.replace(/&apos;/g, "'")
    text = text.replace(/&quot;/g, "\"")
    text = text.replace(/&nbsp;/g, " ")
    
    return text
}

function cancelKey(e)
{
    e = e || window.event;
    var key = e.which || e.keyCode;
    
    if (key == 13)
    {
        e.returnValue = false;
        return false;
    }
}

function checkNull(text)
{
    if (text.replace(/\n/g, "").replace(/^\s+|\s+$/g, "").length == 0)
        return true;
    else
        return false;
}

function updateFields()
{
    var to = getObject("_ctl0_bodyContent_txtTo").value.replace(/\r/g, "");
    if (checkNull(to))
        to = String.fromCharCode(1);
    else
        to = "To: " + to;
    var from = getObject("_ctl0_bodyContent_txtFrom").value.replace(/\r/g, "");
    if (checkNull(from))
        from = String.fromCharCode(1);
    else
        from = "From: " + from;
    var text1 = getObject("_ctl0_bodyContent_txtText1").value.replace(/\r/g, "");
    if (checkNull(text1)) text1 = String.fromCharCode(1);
    var text2 = getObject("_ctl0_bodyContent_txtText2").value.replace(/\r/g, "");
    if (checkNull(text2)) text2 = String.fromCharCode(1);
    var wrapup = getObject("_ctl0_bodyContent_txtWrapup").value.replace(/\r/g, "");
    if (checkNull(wrapup)) wrapup = String.fromCharCode(1);
    
    swf.updateFields(to, from, text1, text2, wrapup);
}

function setTo()
{
    swf.TGotoLabel("/", "to");
    updateFields();
}

function getTo()
{
    swf.TGotoLabel("/", "to");
    getObject("_ctl0_bodyContent_txtTo").value = 
        filterHTML(swf.GetVariable("to")).replace(/\n/g, "").replace(/^\s+|\s+$/g, "").replace(/^To: /g, "");
}

function setFrom()
{
    swf.TGotoLabel("/", "from");
    updateFields();
}

function getFrom()
{
    swf.TGotoLabel("/", "from");
    getObject("_ctl0_bodyContent_txtFrom").value =
    filterHTML(swf.GetVariable("from")).replace(/\n/g, "").replace(/^\s+|\s+$/g, "").replace(/^From: /g, ""); ;
}

function setText1()
{
    swf.TGotoLabel("/", "text1");
    updateFields();
}

function getText1()
{
    swf.TGotoLabel("/", "text1");
    getObject("_ctl0_bodyContent_txtText1").value = filterHTML(swf.GetVariable("text1"));
}

function setText2()
{
    swf.TGotoLabel("/", "text2");
    updateFields();
}

function getText2()
{
    swf.TGotoLabel("/", "text2");
    getObject("_ctl0_bodyContent_txtText2").value = filterHTML(swf.GetVariable("text2"));
}

function setWrapup()
{
    swf.TGotoLabel("/", "wrapup");
    updateFields();
}

function getWrapup()
{
    swf.TGotoLabel("/", "wrapup");
    getObject("_ctl0_bodyContent_txtWrapup").value = filterHTML(swf.GetVariable("wrapup"));
}

function switchEcard(id)
{
    getObject("_ctl0_bodyContent_hfSwitchID").value = id;
    if (getObject("divShareStep").style.display == "block")
    {
        getObject("_ctl0_bodyContent_hfCustomID").value = "";
        getObject("_ctl0_bodyContent_txtTitle").value = "";
    }
    document.forms['aspnetForm'].submit();
}

function setShareTab(tab)
{
    getObject("tabShareEmail").className = "CustomTabBack";
    getObject("tabShareFacebook").className = "CustomTabBack";
    getObject("tabShareLink").className = "CustomTabBack";
    
    getObject("divShareEmail").style.display = "none";
    getObject("divShareFacebook").style.display = "none";
    getObject("divShareLink").style.display = "none";
    
    getObject(tab).className = "CustomTabFront";
    
    switch (tab)
    {
        case "tabShareEmail":
            getObject("divShareEmail").style.display = "block";
            break;

        case "tabShareFacebook":
            getObject("divShareFacebook").style.display = "block";
            break;

        case "tabShareLink":
            getObject("divShareLink").style.display = "block";
            break;          
    }
}
