var EC_ACCESS_DENIED = 0;
var EC_DB = 1;
var EC_MISSING_PARAMETER = 2;
var EC_INTERNAL = 3;
var EC_INVALID_PARAMETER = 4;
var EC_EXTERNAL = 5;
var section;
function getBackendErrorName( t )
{
    switch( t )
    {
        case EC_ACCESS_DENIED:
            return "access denied";
            break;
        case EC_DB:
            return "database";
            break;
        case EC_MISSING_PARAMETER:
            return "missing parameter";
            break;
        case EC_INTERNAL:
            return "internal";
            break;
        case EC_INVALID_PARAMETER:
            return "invalid parameter";
            break;
            
        case EC_EXTERNAL:
            return "external";
            break;
            
        default:
            return "unknown";
            break;
    }
}
function MLBTrack( v )
{
    //alert( v );
}
function goTop()
{
    window.location = '#top';
    window.location = '#';
}
function setFlashHeight( h )
{
    if (window.ActiveXObject)
    {
 		document.all['flashWrapper'].style.pixelHeight = parseInt(h.substring(0,h.length-2));	
	}
	else
	    $('flashWrapper').setStyle( 'height', h );
}
function signupFor( v )
{
    $('rgfm').setStyle( 'visibility', 'visible' );
    $('rgfm').setStyle( 'margin-left', '0' );
	
    $('okButton').setStyle( 'background', 'url(images/' + v + '.gif)' );
    $('okButton').setStyle( 'background-repeat', 'no-repeat' );

    $('MLBDreams').request( 'LoadRegistration.aspx', { }, 'onRegistrationLoadResponse', '' );
}
function rules()
{
    signupHide();
    $('MLBDreams').mlbScroll( 0 );
    $('MLBDreams').setMLBDreamContent( 'Rules' );
}
function rules1()
{
    signupHide();
	 $('MLBDreams').mlbScroll( 241 );
    $('MLBDreams').setMLBDreamContent( 'Rules' );
}
function rules2()
{
    signupHide();
	$('MLBDreams').mlbScroll( 118 );
    $('MLBDreams').setMLBDreamContent( 'Rules' );
}
function privacy()
{
    signupHide();
    $('MLBDreams').setMLBDreamContent( 'MLBPrivacy' );
}
function prizes()
{
    signupHide();
    $('MLBDreams').setMLBDreamContent( 'Prizes' );
}
function signupHide()
{
    $('rgfm').setStyle( 'visibility', 'hidden' );
    $('rgfm').setStyle( 'margin-left', '-2000px' );
    $('cnfpg').setStyle( 'visibility', 'hidden' );
    $('cnfpg').setStyle( 'margin-left', '-2000px' );
}
function onRegistrationLoadResponse( response )
{
    if (window.ActiveXObject)
    {
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML( response );
    }
    else
    {
        var parser = new DOMParser();
        var doc = parser.parseFromString( response, "text/xml" );
    }
    var xml = doc.documentElement;
    var loggedIn = true;
    for( var i = 0; i < xml.childNodes.length; i++ )
    {
        var node = xml.childNodes[i];
        switch( node.nodeName )
        {
            case 'error':
                
                var code = parseInt( node.attributes.code.value );
                if( code == EC_ACCESS_DENIED )
                    loggedIn = false;
                else
                    alert( 'Unhandled Error [' + getBackendErrorName( code ) + ']: ' + node.firstChild.nodeValue );
                break;
                
            case 'email':
                $('emailBeA').value = node.firstChild.nodeValue;
            case 'first':
            case 'last':
            case 'phone':
            case 'address1':
            case 'address2':
            case 'city':
            case 'zip':
                $(node.nodeName).value = node.firstChild == undefined ? "" : node.firstChild.nodeValue;
                break;
                
           case 'state':
                var n = $(node.nodeName);
                for( var j = 0; j < n.options.length; j++ )
                {
                    if( n.options[j].value == node.firstChild.nodeValue )
                    {
                        n.options[j].selected = true;
                        break;
                    }
                }
                break;
        }
    }
}
var sweepstakes;
var iwouldbea;
var voting;
var swfDiv;
function writeContent( id, src, div, base, vars )
{
	swfDiv = div;
    AC_FL_RunContent(
	        'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
	        'width', '100%',
	        'height', '100%',
	        'src', src,
	        'quality', 'high',
	        'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
	        'align', 'middle',
	        'play', 'true',
	        'loop', 'true',
	        'scale', 'showall',
	        'wmode', 'window',
	        'devicefont', 'false',
	        'id', id,
	        'bgcolor', '#000000',
	        'name', src,
	        'menu', 'true',
	        'allowFullScreen', 'false',
	        'allowScriptAccess','always',
	        'movie', src,
	        'wmode', 'opaque',
	        'salign', '',
	        'base', base,
	        'FlashVars', vars
	    );
}
function onNodeValidated( n )
{
    $('balancer').setStyle( 'visibility', 'hidden' );
    
    var l = window.location.href.split('#');
    
	writeContent( 'MLBDreams', 'http://' + n + '/MLBDream', $('flashWrapper'), 'http://' + n + '/', "link=" + ( l.length == 1 ? "" : l[l.length-1] ) );
}
window.onload = function()
{           
	writeContent( 'balance', 'balance', $('balancer'), '/', 'server0=web1.mastercarddreams.mlbam.com&server1=web2.mastercarddreams.mlbam.com' );
    setFlashHeight( "1130px" );
	rslbl = new Array();

	function reset()
	{
	    $('errmsg').setHTML( '' );
	    
	    for (var i = 0; i < rslbl.length; i++ )
            rslbl[i][0].className = rslbl[i][1];    
        rslbl = new Array();
	    $('rgfm').reset();
	}
	$('okButton').addEvent('click', function()
	{
	    $('rgfm').setStyle( 'visibility', 'hidden' );
	    $('rgfm').setStyle( 'margin-left', '-2000px' );
        $('cnfpg').setStyle( 'visibility', 'hidden' );
        $('cnfpg').setStyle( 'margin-left', '-2000px' );
        $('MLBDreams').registrationComplete();
	});
	$('resetBtn').addEvent('click', reset);
	function fvl( v )
	{
	    if( v.value.trim().length == 0 ) return undefined;
	    return v.value;
	}
	$('submitBtn').addEvent('click', function()
	{
	    $('errmsg').setHTML( '' );
	    for (var i = 0; i < rslbl.length; i++ )
            rslbl[i][0].className = rslbl[i][1];    
        rslbl = new Array();
	    $('MLBDreams').request( 'RegisterFull.aspx', 
	    { 
	        first:fvl($('first')),
	        last:fvl($('last')),
	        email:fvl($('email')),
	        address1:fvl($('address1')),
	        address2:fvl($('address2')),
	        city:fvl($('city')),
	        phone:fvl($('phone')),
	        state:fvl($('state')),
	        zip:fvl($('zip')),
	        isResident:$('isResident').checked ? "1" : "0",
	        isMastercard:$('isMastercard').checked ? "1" : "0",
	        is18Week:$('is18Week').checked ? "1" : "0",
	        readRules:$('readRules').checked ? "1" : "0",
	        agreePrivacyWeek:$('agreePrivacyWeek').checked ? "1" : "0",
	        receiveEmail:$('receiveEmail').checked ? "1" : "0",
	        notBroadcasting:$('notBroadcasting').checked ? "1" : "0",
	        readAcceptRules:$('readAcceptRules').checked ? "1" : "0",
	        understandProperty:$('understandProperty').checked ? "1" : "0",
	        password:fvl($('password')),
	        confirmPassword:fvl($('confirmPassword'))
	    }, 'onRegistrationResponse', '' );
	});
	
    emailText = $('email');
    emailBeAText = $('emailBeA');
    password = $('password');
    confirmPassword = $('confirmPassword');
    emailText.addEvent( 'keyup', function()
    {
        emailBeAText.value = emailText.value;   
    });
    
    emailBeAText.addEvent( 'keyup', function()
    {
        emailText.value = emailBeAText.value;   
    });
}
function onRegistrationResponse( response )
{
    if (window.ActiveXObject)
    {
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML( response );
    }
    else
    {
        var parser = new DOMParser();
        var doc = parser.parseFromString( response, "text/xml" );
    }
    var xml = doc.documentElement;
    var uerr = new Array();
    var err = '';
    for( var i = 0; i < xml.childNodes.length; i++ )
    {
        var node = xml.childNodes[i];
        switch( node.nodeName )
        {
            case 'error':
                var code = parseInt( (node.attributes.code == undefined ? node.attributes[0].nodeValue : node.attributes.code.value ) );
                switch( code )
                {
                    case EC_INVALID_PARAMETER:
                        if( node.firstChild.nodeValue == 'is18Week' )
                            err += 'You must be 18 years of age or older to Register and join the MasterCard MLB® Dreams community.<br/>';
                        else if( node.firstChild.nodeValue == 'isResident' )
                            err += 'You must be a legal resident of the 50 U.S. including DC to Register and join MasterCard’s MLB® Dreams community.';
                        else if( node.firstChild.nodeValue == 'readAcceptRules' )
                            err += 'You need to accept the rules to enter the contest.<br/>';
                        else if( node.firstChild.nodeValue == 'agreePrivacy' )
                            err += 'You need to agree to the Privacy Policy to enter the contest.<br/>';
                        else if( node.firstChild.nodeValue == 'acceptRulesIWouldBeA' )
                            err += 'You need to accept the rules for creating an MLB Dreams video clip to enter the contest.<br/>';
                        else
                            uerr[code] = code;
                        var label = $("label_" + node.firstChild.nodeValue);
                        rslbl.push( new Array( label, label.className ) );
                        label.className = 'error';
                        break;
                    case EC_ACCESS_DENIED:
                        err += node.firstChild.nodeValue;
                        var label = $("label_email");
                        rslbl.push( new Array( label, label.className ) );
                        label.className = 'error';
                        var label = $("label_emailBeA");
                        rslbl.push( new Array( label, label.className ) );
                        label.className = 'error';
                        uerr[code] = code;
                        break;
                    case EC_DB:
                    case EC_MISSING_PARAMETER:
                    case EC_INTERNAL:                             
                    case EC_EXTERNAL:
                    default:
                        uerr[code] = code;
                        alert( 'UE [' + getBackendErrorName( code ) + ']: ' + node.firstChild.nodeValue );
                        break;
                }
                break;
            default:
                alert( 'URE: ' + node.nodeName );
        }
    }
    uerr.each(function(code)
    {
        switch( code )
        {
            case EC_INVALID_PARAMETER:
                err += "One or more of the fields contain incomplete or incorrect information. Correct the fields highlighted in red to continue.";
                break;
            case EC_ACCESS_DENIED:
                //err += "An account with that email address already exists, please sign in with your email addres or retrieve your password.";
                break;
        }
    });
    $('errmsg').setHTML( err );
    
    if( uerr.length == 0 )
    {
        $('rgfm').setStyle( 'visibility', 'hidden' );
        $('rgfm').setStyle( 'margin-left', '-2000px' );
        $('cnfpg').setStyle( 'visibility', 'visible' );
        $('cnfpg').setStyle( 'margin-left', '0' );
		window.location = '#top';
        
        $('registeredList').empty();
        if( $('isResident').checked )
        {
            var li = new Element( "li" );
            li.appendText("MLB® Week of Your Dreams Promotion");
            li.injectInside( 'registeredList' );
        }
        if( $('notBroadcasting').checked )
        {
            var li = new Element( "li" );
            li.appendText("I Would Be A");
            li.injectInside( 'registeredList' );
        }
    }
}