var contactInfoForm;
var donationToRegistrationForm;
var cardForm;
var tshirtForm;

$(document).ready(function(){
    $("#submissionResult").fadeOut(0);
    (function(){
        var max = 0;
        $("#contactInfoForm").find("label").each(function(){
           if ($(this).width()>max)
           max=$(this).width();
        });
        $("#contactInfoForm").find("label").width(max+15);
        
        max=0;
        $("#donationToRegistrationForm").find("span.floatLeft").each(function(){
            if ($(this).width()>max)
            max=$(this).width();
        });
        $("#donationToRegistrationForm").find("span.floatLeft").width(max-20);
        
        max=0;
        $("#donationInfo").find("label").each(function(){
           if ($(this).width()>max)
           max=$(this).width();
        });
        $("#donationInfo").find("label").width(max+15);
        $("#creditCard").width(max+15);
    })();
    contactInfoForm = document.getElementById("contactInfoForm");
    donationToRegistrationForm = document.getElementById("donationToRegistrationForm");
    tshirtForm = document.getElementById("tshirtForm");
    cardForm = document.getElementById("donationInfo");
    
    $(contactInfoForm).validate({
        rules:{
            fName:{
                required:true,
                minlength:3
            },
            lName:{
                required:true,
                minlength:3
            },
            address1:{
                required:true
            },
            city:{
                required:true
            },
            state:{
                required:true
            },
            zipcode:{
                required:true
            },
            email:{
                required:true,
                email:true
            }
       }
    });
    
    $(cardForm).validate({
        rules:{
            donationAmount:{
                required:true
            },
            cardName:{
                required:true
            },
            cardNumber:{
                required:true
            },
            expDate:{
                required:true
            }
        }
    });
    
    /*var contactForm = document.getElementById("contactForm");
    $(contactForm).validate({
        rules:{
            name:{
                required:true,
                minlength:3
            }
        },
        submitHandler:function(form){
            var values = {
                name:form.name.value,
                email:form.email.value,
                subject:form.subject.value,
                message:form.message.value
            };
            console.log(values);
            $.post("./php/mailer.php?name="+values.name+"&email="+values.email+"&subject="+values.subject+"&message="+values.message,function(data){
                $(contactForm).fadeOut(1000);
                var results = document.getElementById("results");
                $(results).fadeIn(1000);
                results.innerHTML = data;
                console.log(data);
            });
        }
    });*/
});

function submitClicked()
{
    if ($(contactInfoForm).valid() && $(cardForm).valid())
    {
        document.getElementById("checkFields").style.visibility="hidden";
        var data = {
            fName:contactInfoForm.fName.value,
            lName:contactInfoForm.lName.value,
            title:contactInfoForm.title.value,
            organization:contactInfoForm.organization.value,
            address1:contactInfoForm.address1.value,
            address2:contactInfoForm.address2.value,
            city:contactInfoForm.city.value,
            state:contactInfoForm.state.value,
            zipcode:contactInfoForm.zipcode.value,
            phone:contactInfoForm.phone.value,
            fax:contactInfoForm.fax.value,
            email:contactInfoForm.email.value,
            donationAmount:cardForm.donationAmount.value,
            cardholderName:cardForm.cardName.value,
            cardNumber:cardForm.cardNumber.value,
            expDate:cardForm.expDate.value
        };
        var reg10K = document.getElementById('k10');
            data.reg10K = reg10K[reg10K.selectedIndex].value;
        
        var reg2Mi = document.getElementById('mi2');
            data.reg2Mi = reg2Mi[reg2Mi.selectedIndex].value;
        
        var reg1Mi = document.getElementById('mi1');
            data.reg1Mi = reg1Mi[reg1Mi.selectedIndex].value;
        
        var regKids = document.getElementById('childrenRace');
            data.regKids = regKids[regKids.selectedIndex].value;
        
        var cardType = document.getElementById('cardType');
            data.cardType = cardType[cardType.selectedIndex].value;
        
        var shirts = ['XXXL','XXL','XL','L','M','S','KidsT'];
        for (var i=0;i<shirts.length;i++)
        {
            if (document.getElementById(shirts[i]).checked)
            {
                data[shirts[i]+"Quantity"] = document.getElementById(shirts[i]+'Quantity').value;    
            }
            else
            {
                data[shirts[i]+"Quantity"] = '0';
            }
        }
        $.post("./php/mailer.php?"+$.param(data),function(dataBack){
            $("#formContainer").hide();
            $("#submissionResult").fadeIn(1000);
        });
    }
    else
    {
        document.getElementById("checkFields").style.visibility="visible";
    }
}
