/*****************************************************************************
 * GetMSDS
 *
 * Author: Ger O'Sullivan
 * Date:   30-January-2007
 *
 *
 * OVERVIEW
 * Instantiates an ajax object to get a specified MSDS content based upon 
 * parameters pass in the query string and displays the resulting output
 * in a DIV element.
 * 
 * USAGE
 * Providing a valid product id parameter will return the output of the products
 * corresponding MSDS.
 *
 * NOTES
 *
 *
 *****************************************************************************/

/**
 * CONSTANTS
 */


var APPLICATION_TARGET_URL = '/servlet/MSDS'; // override with $AJAX_URL for application target component
var DIV_AJAX_RECEIVER = 'msds';

var DEFAULT_LANGUAGE_CODE = 'EN';
var DEFAULT_COUNTRY_CODE = 'US'


/**
 * Instantiates an ajax object to get a specified MSDS content based upon 
 * parameters pass in the query string and displays the resulting output
 * in a DIV element.
 *
 * @param 
 * 
 * @return
 */
function getMSDS() {
	writeLoadingMsg(DIV_AJAX_RECEIVER);
  var req = new AjaxRequest(APPLICATION_TARGET_URL, handleApplicationTarget);

  // Retrieve query string parameters
  var lg = getQueryVariable('lg');
  var ctry = getQueryVariable('ctry');
  var product = getQueryVariable('product');
  var pc = getQueryVariable('pc');
  var ctryLangReadable = getQueryVariable('ctry-lang-readable');
  
  var printAll = getQueryVariable('print-all');
  var format  = getQueryVariable('format');

  if ( product!= "" && typeof(product)!="undefined") {
  
// we now use the session preference if undefined  	
//	if ( lg =="" || typeof(lg)=="undefined") lg = DEFAULT_LANGUAGE_CODE;			// set to default if undefined
//	if ( ctry=="" || typeof(ctry)=="undefined" ) ctry = DEFAULT_COUNTRY_CODE;	// set to default if undefined
	
    req.setMethod(AjaxRequest.METHOD_GET);
        
  	setParameterIfDefined(req, 'product', product);
  	setParameterIfDefined(req, 'sap-code', pc);
  	setParameterIfDefined(req, 'lg', lg);
  	setParameterIfDefined(req, 'ctry', ctry);
  	setParameterIfDefined(req, 'ctry-lang-readable', ctryLangReadable);
  	
  	setParameterIfDefined(req, 'print-all', printAll);
  	setParameterIfDefined(req, 'format', format);
  	
  	req.submit();
  } else {
  	writeMSDS("Error:  No product id provided. ");
  }

}


/**
* Sets the parameter, but only if it is not 'undefined'
*/
function setParameterIfDefined(req, key, value)
{
  if (key != null && key != "" && key != "undefined")
  {
    if (value != null && value != "undefined")
    {
      req.setParameter(key, value);
    }
  }
}



/**
 * Handler function. Once the request completes, the handler is invoked and passed the response data.
 *
 * @param xml dom 
 *        response text
 * @return
 */
function handleApplicationTarget(xmlDoc, text)
{
  outputText = text;
  writeMSDS(outputText);
  
}


/**
 * Writes the output contents from the request to a specified DIV element
 * 
 * @param  response text
 * @return 
 */
function writeMSDS(outputText) {
      out = document.getElementById(DIV_AJAX_RECEIVER);
      out.innerHTML = "Loading MSDS";
      out.innerHTML = outputText;
}

