/**
* 
* Forms.js
* 
*******************************************************************************
*  DEPENDANCIES:
*  - jquery.js
*  - Helper.js - should be common to the whole site.
*  - Ui.js
*  - jquery.validate.js - for the login submit functions.
*  
*******************************************************************************
*  DEPENDANTS:
*  - Pretty much all event registration forms use this for the State and 
*    Year/Month functions.  Specifically, the payment-info.shtml and billing-info.shtml 
*    includes (or in many cases, the billing info is written into the index.shtml).
* 
*******************************************************************************
*  VERION HISTORY:
*      v1- Initial Version.
* 
*/


/**
 * 
 * Page Load.
 * 
 * jQuery will stack the .ready() functions if it recieves
 * more than one.  
 * 
 */
$(document).ready(function () {
	//Forms.autoPopulate();
});


/**
 * 
 * Login namespace
 * 
 */
var Forms = (function (parent) {
  
  /**
   * Checks for a logged in user and
   * attempts to auto populate form fields if
   * one is found.
   */
  parent.autoPopulate = function() {    
    if(document.cookie.indexOf('alumni-auth') !== -1) {
      Helper.sendAjax("/./webservices/Alumni/Lookup.svc/GetUserRecord", {}, function (data) {
        Forms.populateForm(data.d);
      });
    }
  };
    
  /**
   * Attempts to match the conents of a given object to common form
   * fields.
   */  
  parent.populateForm = function(a) {
    if(a.d) a = a.d;
    
    Ui.closeDialog();
    
    if(a.firstname)    $('#firstname').val(a.firstname);
    if(a.middlename)   $('#middlename').val(a.middlename);
    if(a.lastname)     $('#lastname').val(a.lastname);
    if(a.personId)     $('#person_id').val(a.personId);       
    if(a.universityId) $('#univ_usr_id').val(a.universityId);
    if(a.address1)     $('#address1, #street').val(a.address1);
    if(a.address2)     $('#address2').val(a.address2);
    if(a.city)         $('#city').val(a.city);
    if(a.state)        $('#state').val(a.state);
    if(a.zip)          $('#zip').val(a.zip);
    if(a.phone)        $('#phonenum, #homephone').val(a.phone);
    if(a.email)        $('#email').val(a.email);
    if (a.country)     $('#country').val(a.country);
    if (a.emp_name)    $('#employer').val(a.emp_name);
    if (a.emp_title)   $('#title').val(a.emp_title);
    if (a.emp_address1)$('#emp_address1').val(a.emp_address1);
    if (a.emp_address2)$('#emp_address2').val(a.emp_address2);
    if (a.emp_city)    $('#emp_city').val(a.emp_city);
    if (a.emp_state)   $('#emp_state').val(a.emp_state);
    if (a.emp_zip)     $('#emp_zip').val(a.emp_zip);
    if (a.emp_country) $('#emp_country').val(a.emp_country);
    if (a.strBirthDate) $('#birthdate').val(a.strBirthDate);
    if(a.personId){
     	$('#aidentity').val(a.personId);
    }else if (a.universityId){
    	$('#aidentity').val(a.universityId);
    }
    if (a.country == null && a.state != null && a.zip != null)
    	$('#country').val("USA");
    if (a.emp_country == null && a.emp_state != null && a.emp_zip != null)
    	$('#emp_country').val("USA");
  }; 
  
  /**
   * Accepts a form element as jQuery object.
   * 
   * Usage: Helper.validateForm( $('#someForm') )
   */
  parent.validateForm = function(form) {
    jQuery.validator.messages.required = ""; 
    
    var validator = form.validate({
      onkeyup: false,
      ignore: ':hidden'
    });
	
    if (!form.valid()) {
		validator.showErrors();
		return false;
    }
    
    return true;
  };
  
  return parent; 
  
}(Forms || {}));


