/*
 * Script: jquery-seo-orderform.js
 * Author: Andy Gray
 *
 * Description
 * Manages the orderform context
 *
 * Development Note
 * Watch out for Cross Site security issues where AJAX calls fail!!!
 *
 * Requires
 * - autocomplete
 * - date validation
 * - place validation
 */

$(document).ready(

  function() {

    var url = 'http://www.world-of-wisdom.com/wsapi/acs/';
    var default_country = 'JX';

    /*
     * Manage autocomplete on birthplace
     */
      $('#frmHIOrderBirthplace').autocomplete(
	'proxy.php',
	{
	  delay: 10,
	  minChars: 2,
	  matchSubset: 1,
	  matchContains: 1,
	  cacheLength: 10,
	  autoFill: true,
	  extraParams: {
	    /* call (proxy) API function getPlaceList(state); */
	    method: 'getPlaceList',
	    state: function() { return $('#frmHIOrderBirthcountry').val(); }
	  }
	}
      );

    /*
     * Call a list of states/countries from the WOW server
     * Populate the birthcountry option list
     * Note: callback used for JSONP compliance, could route through proxy.php instead
     */
    $.getJSON(
      url+'atlas.json.php?method=getStatesXML&callback=?',
      function( data ) {
	$.each( data, function( key, value ) {
	  if( value.abbrev == default_country ) {
	    $('#frmHIOrderBirthcountry').append('<option value=' + value.abbrev + ' SELECTED' + '>' + value.name + '</option>');
	  } else {
	    $('#frmHIOrderBirthcountry').append('<option value=' + value.abbrev + '>' + value.name + '</option>');
	  }
	});
	$('#frmHIOrderBirthcountry').val( default_country );
      }
    );

    /*
     * Manage a change of selected State/Country
     * Call a list of states/countries from the WOW server
     * Populate the birthcountry option list
     */
    $('#frmHIOrderBirthcountry').change(
      function(){
	/* clear any value held in the birthplace field */
	$('#frmHIOrderBirthplace').val('');
      }
    );

  } /* function */

  /* end of ready context */
);

