//
// Directory Data Request Script
//
// Author: Geoffrey Richey
// Date: March 2009
//

EI.namespace('DirectoryDataRequest');

EI.DirectoryDataRequest = function(page,options) {

    var self = this;

    var _page           = page || '';
    var _options        = options || {};
    var _apiUrl         = '/mod-bin/';

    function doRemoteRequest() {
        var params = {
            data_req:           1,
            page:               _page,
            action:             _options.action             || '',
            state_id:           _options.state_id           || 0,
            mma_id:             _options.mma_id             || 0, 
            specialty_id:       _options.specialty_id       || 0,
            subspec_id:         _options.subspec_id         || 0,
            product_id:         _options.product_id         || 0,
            form_selections:    _options.form_selections    || {},
            callback:           _options.callback
        };

        if( _page == 'template' ){
            var _template_type  = _options.template_type;
            var _product_id     = _options.product_id;
            var _updateId       = _options.update;

            if(( _template_type ) && ( _updateId )){
                var _pars   = 'data_req=1&page=template&template_type='+escape(_template_type)+'&product_id='+_product_id;
                var request = new Ajax.Updater( _updateId, _apiUrl, {method: 'get', parameters: _pars });
            }

        }

        else{
            var scriptId    = EI.Util.generateRandomElementId();
            var scriptSrc   = _apiUrl + '?' + $H(params).toQueryString();

            EI.Util.appendScriptElement(scriptSrc, scriptId);
        }
    }

    this.updateOptionsAndSend = function(options) {
        options.each(function(opt){
            _options[opt.name] = opt.value;
            doRemoteRequest();
        });
    }

	this.updateStateAndSend = function(state) {
        _options.state_id = state;
        doRemoteRequest();
    };

    var initialize = function() {
        doRemoteRequest();
    }();
};

//
// Util core javascript
//

EI.namespace("Util");

// This namespace is for commonly utility functions used across multiple applications. 

EI.Util = function() {

	var self = this;

	// Called automatically by anonymous timeout function set by appendScriptElement.
	// Removes script tag from document <head>
	// 
	function removeScriptElement(id) {
		if (headElem = document.getElementsByTagName('head')[0]) headElem.removeChild($(id));
	}

	return {

		// Creates script tag and appends to document <head>
		// 
		appendScriptElement: function(src, id) {
			var scriptElem = document.createElement('script');
			scriptElem.setAttribute('type', 'text/javascript');
			scriptElem.setAttribute('charset', 'UTF-8');
			scriptElem.setAttribute('id', id)
			scriptElem.setAttribute('src', src);
			// Adds script tag and set timeout to remove after 5 seconds
			if (headElem = document.getElementsByTagName('head')[0]) {
				headElem.appendChild(scriptElem);
				window.setTimeout(function(){ removeScriptElement($(id)) }, 5000);
			}
			else return false;
		},

		// Generates a random valid ID for usage with DOM elements
		// 
		generateRandomElementId: function() {
			return '_EI-' + (new Date).getTime().toString(36);
		}

	}
}();

