/******************** DOCUMENT AJAX STATES JAVASCRIPT LIBRARY *********************************/
/************ NOTES START *******************************************************************************************
* 
* Date: 05/28/09
* Purpose: Utility functions for calling ajax_states.xhtml page
* Instructions:
* Prerequisites: (note required versions on top of the page)
*   0.0.0+  jseone/base.js
*   1.1.5+  jseone/prototype/prototype.js
************* NOTES END *********************************************************************************************/

/**
 * 
 * @param form
 */
function populateMemberStateField(form) {
  var countryId = form.country.options[form.country.selectedIndex].value;
  var stateType='member';
  var addressSequence;
  var addressType;
  if (form.addrseq) {
    addressSequence = 'addrseq=' + form.addrseq.value;
  }
  if (form.addrtype) {
    addressType = 'addrtype=' + form.addrtype.value;
  }

  var otherparms = addressSequence + '&' + addressType;
  populateStates(form, countryId, stateType, otherparms, '');

}

/**
 * 
 * @param form
 */
function populateStateField(form) {
  var countryId = form.country.options[form.country.selectedIndex].value;
  var stateType='';
  var addressSequence;
  var addressType;
  if (form.addrseq) {
    addressSequence = 'addrseq=' + form.addrseq.value;
  }
  if (form.addrtype) {
    addressType = 'addrtype=' + form.addrtype.value;
  }

  var otherparms = addressSequence + '&' + addressType;
  populateStates(form, countryId, stateType, otherparms, '');

}

/**
 * 
 * @param form
 */
function populateShippingStateField(form) {
  var countryId = form.country.options[form.country.selectedIndex].value;
  var stateType='sack_shipping';
  var addressSequence;
  var addressType;
  if (form.addrseq) {
    addressSequence = 'addrseq=' + form.addrseq.value;
  }
  if (form.addrtype) {
    addressType = 'addrtype=' + form.addrtype.value;
  }

  var otherparms = addressSequence + '&' + addressType;
  populateStates(form, countryId, stateType, otherparms, '');

}

/**
 * 
 * @param form
 */
function populateBillingStateField(form) {
  var countryId = form.country.options[form.country.selectedIndex].value;
  var stateType='sack_billing';
  var addressSequence;
  var addressType;
  if (form.addrseq) {
    addressSequence = 'addrseq=' + form.addrseq.value;
  }
  if (form.addrtype) {
    addressType = 'addrtype=' + form.addrtype.value;
  }

  var otherparms = addressSequence + '&' + addressType;
  populateStates(form, countryId, stateType, otherparms, '');

}

/**
 * 
 * @param form
 * @param countryId
 * @param stateType
 * @param otherparms
 * @return
 */
function populateStates(form, countryId, stateType, otherparms, returnparms, response) {
  // intialize parm key/values
  // var actn = "actn=" + "shoplistadditem";
  var dnXMLNodeName = "&dnXMLNodeName=" + "statefield_response";
  var dontUpdateDspNav = "&updateDspNav=false";
  var dsp = '&dsp=ajaxstatefield';
  var country = '&country=' + countryId;
  var stateTypeParm = '&statetype=' + stateType;
  
  if (otherparms) {
    otherparms = '&' + otherparms;
  } else {
    otherparms = '';
  }
  
  if (returnparms) {
	returnparms = '&returnparms=' + returnparms;
  } else {
	returnparms = '';
  }
  
  //
  
  var parms = dnXMLNodeName + dontUpdateDspNav + dsp + country + stateTypeParm + otherparms + returnparms;

  // div id that will be populated with error message
  var errorDivID = '';
  
  // available for custom response (default set to blank)
  if(!response) {
    response = '';
  }

  // call to action processor
  ajaxShop(parms, errorDivID, response);
}



