﻿Event.observe(window, 'load', function(){   

    ionMxCustom = {};
    ionMxCustom.USGA = {};
    var currentDay = 1;
    var currentMonth = 0;
    var currentYear = 2008;
    
    ionMxCustom.USGA.Signup = function() {
	    return {
		    wireUpEventHandlers:function(){
		        Event.observe('ButtonSubmit', 'click',  function(){ionMxCustom.USGA.Signup.tryToSubmit(ionMxCustom.USGA.Signup.signupCallBack);});
		        Event.observe('ImageCalendar', 'click',  function(){ionMxCustom.USGA.Signup.ShowCalendar();});
	        },
	        initialize:function(){
	            YAHOO.namespace("example.calendar");

	            YAHOO.example.calendar.init = function() {

		        YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1", "calContainer", { title:"Choose a date:", navigator:true, close:true } );
		        YAHOO.example.calendar.cal1.selectEvent.subscribe(ionMxCustom.USGA.Signup.CalendarSelect, YAHOO.example.calendar.cal1, true);     
		        YAHOO.example.calendar.cal1.render();
	            }

	            YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init);
	            
	            var form = $('form1').serialize(true);
                form.request = 'currentDate';
                        
	            new Ajax.Request('ajax/signup.ashx', {
                    method:'get',
                    parameters:form,
                    onSuccess:function(r){
                        if(r.responseText.startsWith('error'))
                        {
                            alert(r.responseText);
                        }
                        else if (r.responseText.startsWith('date'))
                        {
                            var timeArray = new Array();
                            timeArray = r.responseText.split(':');
                            currentMonth = timeArray[1] - 1;
                            currentDay = timeArray[2];
                            currentYear = timeArray[3];
                        }
                    },
                    onFailure:function(r){
                        alert(r.responseText);
                    },
                    onComplete:function(r){                                                        
                    }
                });
	        },
	        tryToSubmit:function(callback) {
	               if (ionMxCustom.USGA.Signup.ValidateForm())
	               {
	                    var form = $('form1').serialize(true);
                        form.request = 'signup';
    	        
	                    new Ajax.Request('ajax/signup.ashx', {
                            method:'get',
                            parameters:form,
                            onSuccess:function(r){
                                if(r.responseText.startsWith('error'))
                                {
                                    alert(r.responseText);
                                }
                                else if (r.responseText.startsWith('success'))
                                {
                                   window.location.href='signupty02.htm';
                                }
                            },
                            onFailure:function(r){
                                alert(r.responseText);
                            },
                            onComplete:function(r){                                                        
                            }
                        });
                  }
            },
            signupCallBack:function(text) {alert(text); },
            IsFilled:function(str)
            { 
              return str.replace(/^\s*|\s*$/g, '').length != 0; 
            },
            IsEmailAddress:function(obj)
            {
                var regex = /\w+@[a-zA-Z0-9_-]+\.([a-zA-Z0-9_-]{2,3})|\w+@[a-zA-Z0-9_-]+\.([a-zA-Z0-9_-]{2,3}\.[a-zA-Z0-9_-]{2,3})/;
                return (obj.value.search(regex) != -1);
            },
            ShowCalendar:function()
            {
                YAHOO.example.calendar.cal1.show();
            },
            CalendarSelect:function(type, args, obj)
            {
                var selected = args[0];
                var selDate = this.toDate(selected[0]);
                var theMonth = selDate.getMonth() + 1;
                var theDay = selDate.getDate();
                var theYear = selDate.getFullYear();
                
                document.getElementById('TextBoxBirthMonth').value = theMonth.toString();
                document.getElementById('TextBoxBirthDay').value = theDay.toString();
                document.getElementById('TextBoxBirthYear').value = theYear.toString();
                YAHOO.example.calendar.cal1.hide();
            },
            ValidateForm:function()
            {
                var Invalid = false;
                var error = '';
                var field;
                var DoVerify = true;
                
                
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxFirstName')))
                {
                    error = 'First Name is required.\n' ;
                    field = ! field ? 'TextBoxFirstName': field;
                    Invalid = true;
                }
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxLastName')))
                {
                    error += 'Last Name is required.\n' ;
                    field = ! field ? 'TextBoxLastName': field;
                    Invalid = true;
                }
                
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxAddress1')))
                {
                    error += 'Street Address is required.\n' ;
                    field = ! field ? 'TextBoxAddress1': field;
                    Invalid = true;
                }
                
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxCity')))
                {
                    error += 'City is required.\n' ;
                    field = ! field ? 'TextBoxCity': field;
                    Invalid = true;
                }
                
                if(!ionMxCustom.USGA.Signup.IsFilled($F('DropDownState')))
                {
                    error += 'State is required.\n' ;
                    field = ! field ? 'DropDownState': field;
                    Invalid = true;
                }
                
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxZipCode')))
                {
                    error += 'Zip code is required.\n' ;
                    field = ! field ? 'TextBoxZipCode': field;
                    Invalid = true;
                }
                else
                {
                    var re = new RegExp(/^\d{5}([\-]\d{4})?$/); 
                    if (! re.match($F('TextBoxZipCode').strip()) )
                    {
                        Invalid = true;
                        field = ! field ? 'TextBoxZipCode': field;
                        error += 'Enter zip code in 5 digit or ZIP + 4 format.\n';
                    }
                }
                                
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxEmailAddress')))
                {
                    error += 'E-mail address is required.\n' ;
                    field = ! field ? 'TextBoxEmailAddress': field;
                    Invalid = true;
                    DoVerify = false;
                }
                else if(!ionMxCustom.USGA.Signup.IsEmailAddress($('TextBoxEmailAddress')))
                {
                    error += 'Enter valid E-mail address.\n' ;
                    field = ! field ? 'TextBoxEmailAddress': field;
                    Invalid = true;
                    DoVerify = false;
                }
                
                if (DoVerify)
                {
                    if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxEmailAddressVerify')))
                    {
                        error += 'Verification E-mail address is required.\n' ;
                        field = ! field ? 'TextBoxEmailAddressVerify': field;
                        Invalid = true;
                    }
                    else if($F('TextBoxEmailAddress') != $F('TextBoxEmailAddressVerify'))
                    {
                        error += 'E-mail address must match the Verification E-mail address.\n' ;
                        field = ! field ? 'TextBoxEmailAddressVerify': field;
                        Invalid = true;
                    }
                }
                
                DoVerify = true;
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxBirthMonth')))
                {
                    error += 'Birthday Month is required.\n' ;
                    field = ! field ? 'TextBoxBirthMonth': field;
                    Invalid = true;
                    DoVerify = false;
                }
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxBirthDay')))
                {
                    error += 'Birthday Day is required.\n' ;
                    field = ! field ? 'TextBoxBirthDay': field;
                    Invalid = true;
                    DoVerify = false;
                }
                if(!ionMxCustom.USGA.Signup.IsFilled($F('TextBoxBirthYear')))
                {
                    error += 'Birthday Year is required.\n' ;
                    field = ! field ? 'TextBoxBirthYear': field;
                    Invalid = true;
                    DoVerify = false;
                }
                
                var ageProblem = false;
                if (DoVerify)
                {
                    var theMonth = parseInt($F('TextBoxBirthMonth'));
                    var theDay = parseInt($F('TextBoxBirthDay'));
                    var theYear = parseInt($F('TextBoxBirthYear'));
                    
                    if ((theMonth == 0) || (theMonth.toString() == 'NaN')
                       || (theDay == 0) || (theDay.toString() == 'NaN')
                       || (theYear == 0) || (theYear.toString() == 'NaN'))
                    {
                        error += 'Please enter a valid Birthday as mm/dd/yyyy.\n' ; 
                        Invalid = true;
                    }   
                    else
                    {           
                        var theCurrentDate = new Date();
                        theCurrentDate.setFullYear(currentYear, currentMonth, currentDay);
                        var theCurrentTime = theCurrentDate.getTime();
                        
                        var theBirthDate = new Date();
                        theBirthDate.setFullYear(theYear, theMonth - 1, theDay);
                        
                        var theNewMonth = theBirthDate.getMonth();
                        
                        if (theNewMonth == theMonth - 1)
                        {
                            var theBirthTime = theCurrentTime - theBirthDate.getTime();
                            var ageDays = theBirthTime/1000/60/60/24;
                            var leapDays = 5;

                            if ((theYear == 1992) && (theMonth > 2))
                                leapDays = 4;
                            else if ((theYear == 1996) && (theMonth > 2))
                                leapDays = 3;
                            else if (theYear > 1992) 
                                leapDays = 4;
                                
                            var age = (ageDays - leapDays) / 365;
                                   
                            if (age < 13)
                            {
                                window.location.href='signupty03.htm';
                                Invalid = false;
                                ageProblem = true;
                                return false;
                            }
                            else if (age >= 18)
                            {
                                window.location.href='signupty01.htm';
                                Invalid = false;
                                ageProblem = true;
                                return false;
                            }
                        }
                        else
                        {
                            error += 'You must enter a valid Birth Date (mm/dd/yyyy)\n' ; 
                            Invalid = true;
                        }
                    }
                }

                if (!$('CheckBoxSignUp').checked && !ageProblem)
                {
                    error += 'You must click the Check Box to sign up\n' ; 
                    field = ! field ? 'CheckBoxSignUp': field;
                    Invalid = true;
                }
                    
                if(Invalid)
                {
                    alert(error);
                    $(field).focus();
                    return false;
                }
                return true;
           }
         }	    
	}();       
    ionMxCustom.USGA.Signup.wireUpEventHandlers();
    ionMxCustom.USGA.Signup.initialize();
});



