// <script>

var Profile = Class.create();
Profile.prototype = {

	initialize : function() {	
		
		// handle additions via pop-up
		try{
		 if ($$('#validationNotification.success').size() > 0){
        window.opener.register.closeChildWindow();
        return;
		  }
		}catch(e){};
				
		// establish js validation
		this.validation = new Form.Behavior.Validation("editUser", "../profile/xml/profile.xml", this.handleValidationErrors.bind(this));
		this.notification = new Notification.Area( {
			id		   : "validationNotification",
			insertion  : "After",
			parent     : $("notificationArea"),
			showEffect : "Appear",
			showOptions: {duration:1.0}
		});
	
		// select correct radio button
		try{
  		if($F('clubName') != ''){     $('ogaMemberClub_no').checked = true;   }
  		else if($F('clubId') != ''){  $('ogaMemberClub_yes').checked = true;  }
  		else if($('noMemberClub')){   $('noMemberClub').checked = true;       }
  		else{                         $('ogaMemberClub_yes').checked = true;  }
  	}catch(e){};
	
		Form.Behavior.Manager.setup(function(d) {with(d) {	  
			show('fsHomeClubName').when('ogaMemberClub').is('N');
			show('fsHomeClubSelect').when('ogaMemberClub').is('Y');
		}});
		 
		// final validation check on submit button click
		if($('submit')){
			Event.observe('submit','click', this.checkForm.bindAsEventListener(this));
		}
		
		// change profile from listbox
		if($('pidOption')){
			Event.observe('pidOption', 'change', this.changeProfile.bindAsEventListener(this));
		}
	
		// update large player header with recent name
		if($('newFirstName')){
			['firstName','lastName','suffix'].each(function(e){
				Event.observe(e, 'change', this.updateProfileName.bindAsEventListener(this)); 
			}.bind(this));
			
			if ($F('firstName')!='') this.updateProfileName();
		}
		
		// check for guardian information requirement based on legal age
		if($('birthDate') && $('guardianInformation')){
			Event.observe('birthDate','change', this.checkAge.bindAsEventListener(this));
			this.checkAge();			
		}
		
		// handle tournament jumper on success page
		if($('onlineEvent')){
			Event.observe('onlineEvent','change',this.registerEvent.bindAsEventListener(this));
		}

	},
	
	changeProfile : function(){
		
		var newPid = $F('pidOption');
		if (newPid == ''){
			// add a new child to this account
			window.location = 'http://www.ohiogolf.org/accounts/setup/?parentPid=' + $F('pid');
		}
		else if($F('pidOption')!=$F('pid')){
			window.location = 'http://www.ohiogolf.org/accounts/edit/?pid=' + newPid;
		}
		
	},
	
	updateProfileName : function(){
		
		$('newFirstName').innerHTML = $F('firstName');
		$('newLastName').innerHTML  = $F('lastName');
		$('newSuffix').innerHTML 	= $F('suffix');
		
	},
	
	getGHIN : function(){
  	new Ajax.Request('/accounts/setup/setup.asp',
		  {
		    method:'get',
		    asynchronous:false,
			  parameters:{ action:'getGHIN', ghin:$F('ghin') },
		    onSuccess: function(transport,json){
          if (json.payload.success == '-200'){
            alert('The GHIN validation service is currently down, so we are unable to properly validate your GHIN number.  Please try resubmitting later.');
          }		      
          $('validGHIN').value = json.payload.success;
		    },
		    onFailure: function(){ alert('Something went wrong...') }
		  });
	},
	
	validateGHIN : function(){
    if ($F('ghin') != ''){ 
      this.validation.enable('ghin'); 
      this.getGHIN(); 
    } else {
      this.validation.disable('ghin');
    };	  
	},
	
	checkForm : function(){

    this.validateGHIN();
		$('userName') ? this.validation.enable('credentials') : this.validation.disable('credentials');		
		this.needGuardian() ? this.validation.enable('guardian') : this.validation.disable('guardian');
		
	},
	
	checkAge : function(){
		
		if (this.needGuardian()){
			$('guardianInformation').show();
			$('guardianAddress').show();
		}
		else{
			$('guardianInformation').hide();
			$('guardianAddress').hide();
		}
		
	},
	
	needGuardian : function(){
		
		if(!$('guardianInformation')){ return };
		
		try{
			var now		  = new Date();
			var birthdate = new Date($F('birthDate'));
			var age		  = (now - birthdate) / (365.25 * 24 * 60 * 60 * 1000);
			return (age < 18);
		}
		catch(e){
			return false;
		}
		
	},
	
	handleValidationErrors : function(errors) {
		
		if (errors.length <=0) return;
		 
		// pass the errors to the notification area
		this.notification.handleValidationErrors(errors);
			
		var errorHeaderHTML = "<div class='vHeader jsError'>Oops! We found some problems that need fixed:</div>";
		new Insertion.Top($('validationNotification'),errorHeaderHTML);		
		
		Element.hide($('notificationArea'));
		
		$('mainContent').scrollTo();
			
	},
	
	registerEvent : function(){
		
		if ($F('onlineEvent')!=''){
			if(!$('method')){
				window.location = 'http://www.ohiogolf.org/tournaments/register/accept/?tid=' + $F('onlineEvent') + '&pid=' + $F('updatedPid');
			}
			else{
				window.location = 'http://www.ohiogolf.org/tournaments/register/?tid=' + $F('onlineEvent') + '&pid=' + $F('updatedPid') + '&method=' + $F('method');
			}
		}
	}
			
};

var profile = Event.observe(window, "load", function() { profile = new Profile() } );


