var emailSubmit = function()
{
    this.version =  '1.1';
    this.defaultOptions =
    {
        formHandlerURL: 'http://ebm.cheetahmail.com/r/regf2',
        iframeId: 'registration'
    };

    this.baseFormData =
    {
        'aid': '1385098083',
        'n': '1',
        'a': '1',
        'SOURCE': 'I'
    };
    
    this.initialize();
};

emailSubmit.prototype.initialize = function(options)
{
    options = options || {};
    this.setOptions(YAHOO.lang.merge(this.defaultOptions, options));
};

emailSubmit.prototype.isEmail = function(s)
{    
    var email_1_regexp = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
    return s.match(email_1_regexp) != null;
};

emailSubmit.prototype.isNumber = function(s)
{
    var isWhole_re       = /^\s*\d+\s*$/;
    return s.match(isWhole_re) != null;
};

emailSubmit.prototype.clearSignUpInputStyle = function()
{
    document.getElementById('signup_firstname').style.border = "none";
    document.getElementById('signup_email').style.border = "none";
};

emailSubmit.prototype.showError = function(types)
{
    if (types.length == 0) { return; };

    this.clearSignUpInputStyle();
    var input_error = "<ul>";
    var i_box;
    var tooltip;
    for (var i=0; i < types.length; i++)
    {
        if(types[i] == nameAdventureError)
        {
            i_box = document.getElementById('signup_firstname');
            tooltip = 'signup_firstname';
        }
        else if(types[i] == emailAdventureError)
        {
            i_box = document.getElementById('signup_email');
            tooltip = 'signup_email';
        }
        if(i_box) i_box.style.border = "2px solid #f00";

        input_error +="<li><span>"+types[i]+"</span></li>";
    };

    input_error +="</ul>";

    //display errors
    //if(types.length > 1){
        //document.getElementById('ov-error-container').style['display'] = 'block';
        document.getElementById('signin-errors').style['display'] = 'block';
        document.getElementById('error-body').innerHTML = input_error;
    //}else{
        // myTooltip = new YAHOO.widget.Tooltip("myTooltip", {
            // context: tooltip,
            // text: "You have hovered over myContextEl.",
            // showDelay: 500,
            // visible: true,
            // iframe: true,
            // monitorresize: true,
            // zIndex: 200
        // });
    // }

};

emailSubmit.prototype.submitForm = function(email, firstName, lastName, zip, birthMonth, birthDay, birthYear, cardholder, whichForm, module)
{
    // sitecatalyst tagging goes here in case the email subscription call throws an error
    var sc = new Object();
    sc.events = 'event4';
    sc.pageName = 'Email subscription';
    if(typeof(s) != 'undefined') { s.t(sc); }

    var types = new Array();

    if (!email || !firstName || !this.isEmail(email))
    {
        if (!firstName)
        {
            types.push(nameAdventureError);
        }

        if (!email || !this.isEmail(email))
        {
            types.push(emailAdventureError);
        }

        this.showError(types);

        return;
    }

    if (!this.isEmail(email))
    {
        types.push(emailAdventureError);
        this.showError(types);

        return;
    }

    if (cardholder) cardholder = "y";

    if (module != "account") module = "main";

    if (birthMonth || birthDay)
    {
        if (!birthMonth || !birthDay)
        {
            alert("Please fill out all your birthday information.");
            return;
        }

        if (birthMonth > 12 || !this.isNumber(birthMonth) || birthMonth.length < 2)
        {
            alert("Your birthday month should be a number between 01 and 12.");
            return;
        }

        if (birthDay > 31 || !this.isNumber(birthDay) || birthDay.length < 2)
        {
            alert("Your birthday day should be a number between 01 and 31.");
            return;
        }

    }
    var data = {};
    var months = ['','January','February','March','April','May','June','July','August','September','October','November','December'];

    if (lastName || zip || birthMonth || birthDay || birthYear || cardholder)
    {
        data = YAHOO.lang.merge({'email1': (email?email:""), 'email2': (email?email:""), 'FNAME': (firstName?firstName:""), 'LNAME': (lastName?lastName:""), 'ZIP': (zip?zip:""), 'BIRTH_MONTH': (birthMonth?months[parseInt(birthMonth, 10)]:""), 'BIRTH_DAY': (birthDay?birthDay:""), 'BIRTH_YEAR': (birthYear?birthYear:""), 'CARDHOLDER': (cardholder?cardholder:"")}, this.baseFormData);

        data.n = "2";
    }
    else
    {
        data = YAHOO.lang.merge({'email': (email?email:""), 'FNAME': (firstName?firstName:"")}, this.baseFormData);
    }
    var sendMessage = function() 
    {
        //close emailsignup overlay
        my_sfcPanel.hide();
        //create confirmation overlay
        getPageOverlay('emailsignup_panel', 'e-mail confirmation','/'+module+'/emailsignupconfirmation','435px');
        
    };    
    
    //YAHOO.util.Event.removeListener(this.options.iframeId, "load");
    //YAHOO.util.Event.addListener(this.options.iframeId, "load", sendMessage);
    getPageOverlay('emailsignup_panel', 'e-mail confirmation','/main/emailsignupconfirmation','435px');
    YAHOO.util.Dom.get(this.options.iframeId).src = this.options.formHandlerURL + "?" + this.toQueryString(data);
    
    //clean form
    if(whichForm == 'customer'){
        document.getElementById('signup_first_name').value = "";
        document.getElementById('signup_email').value = "";
        document.getElementById('signup_last_name').value = "";
        document.getElementById('signup_zip').value = "";
        document.getElementById('signup_bday_month').value = "";
        document.getElementById('signup_bday_day').value = "";
        document.getElementById('signup_holder').checked = false;
    }
    else{
        this.clearSignUpInputStyle();
        document.getElementById('signin-errors').style['display'] = 'none';
        document.getElementById('signup_firstname').value = "";
        document.getElementById('signup_email').value = "";
    }

};

emailSubmit.prototype.setOptions = function(options)
{
    this.options = options;
};

emailSubmit.prototype.toQueryString = function(o)
{
    if(typeof o !== 'object') 
    {
        return false;
    }
    
    var _p, _qs = [];
    
    for(_p in o) 
    {
        _qs.push(encodeURIComponent(_p) + '=' + encodeURIComponent(o[_p]));
    }
    
    return _qs.join('&');
}
