﻿function createAccount()
{
    var email = $("e");
    var pw = $("pw");
    var cpw = $("cpw");
    var code = $("code");
    var acctType = $("t1");
    var compName = $("compName");
    var contact = $("contact");
    var phone = $("phone");
    var address = $("address");
    var employerAgree = $("employerAgree");
    
    if(compName.value.trim().length == 0 && acctType.checked)
    {
        message("Please enter your company name.");
        return;
    }
    else if(email.value.trim().length == 0)
    {
        message("Please enter your email.");
        return;
    }
    else if(!(new RegExp("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", "ig").test(email.value)))
    {
        message("Your email is in invalid format. Please make sure it's the right email.");
        return;
    }
    else if(pw.value.length == 0)
    {
        message("Please enter your password.");
        return;
    }
    else if(pw.value != cpw.value)
    {
        message("The 2 passwords do not match, please check for typos.");
        return;
    }
    else if(code.value.trim().length == 0)
    {
        message("Please enter your invitation code.");
        return;
    }
    else if(contact.value.trim().length == 0 && acctType.checked)
    {
        message("Please enter a billing contact.");
        return;
    }
    else if(phone.value.trim().length == 0 && acctType.checked)
    {
        message("Please enter a phone number.");
        return;
    }
    else if(address.value.trim().length == 0 && acctType.checked)
    {
        message("Please enter a billing address.");
        return;
    }
    else if($("t1").checked && !employerAgree.checked)
    {
        message("You must agree to our terms and conditions first.");
        return;
    }
    
    var callback =
    {
        success: function(o)
        {
            if(o)
            {
                var xmldoc = o.responseXML;

                if(xmldoc && xmldoc.documentElement && xmldoc.documentElement.firstChild && xmldoc.documentElement.firstChild.nodeValue.length > 0)
                {
                    message(xmldoc.documentElement.firstChild.nodeValue);
                    return;
                }
                else
                {
                    var options =
                    {
                        Login : function()
                        {
                            document.location.href = "../Login/";
                        }
                    };

                    message("Your account has been created.", options);
                }
            }
        }
    };

    YAHOO.util.Connect.asyncRequest("POST", "Ajax/Account/Create.aspx", callback, "e={0}&p={1}&c={2}&t={3}&cn={4}&bc={5}&bp={6}&ba={7}".format(email.value.trim().urlEncode(), pw.value.urlEncode(), code.value.trim().urlEncode(), (acctType.checked ? "2" : "3"), compName.value.trim().urlEncode(), contact.value.trim().urlEncode(), phone.value.trim().urlEncode(), address.value.trim().urlEncode()));
}

function toggleMemberTypePanel()
{
    var type1 = $("t1").checked;
    
    if(type1)
    {
        $("companyPanel").style.display = "";
        $("billingInfoPanel").style.display = "";
        $("employerAgreement").style.display = "";
    }
    else
    {
        $("companyPanel").style.display = "none";
        $("billingInfoPanel").style.display = "none";
        $("employerAgreement").style.display = "none";
    }
}