//****************************************************************************
//>>> JAVASCRIPT MODULE: Max_Universal
//****************************************************************************
//
// $Id$
// Purpose: top level functions that all pages will have.
//
//****************************************************************************

//-- global vars -------------------------------------------------------------
//----------------------------------------------------------------------------

var Univ = new Object();
Univ.version = 0;
Univ.page_core_mode = "live";

var App = 0;
//var Ajax = new Object();
var Ajax = new MaxFire_Ajax_Manager();
var Mary = 0;


//-- feedback forms in footer ------------------------------------------------
//----------------------------------------------------------------------------
//
// print_crm_feedback_link - output a crm link.
//
function print_crm_feedback_link(mode, label, img_file, status)
{
    var doc = top.document;
    var q_status = "'" + status + "'";
    var q_mode = "'" + mode + "'";

    doc.writeln('<a'
    + ' class="footer_nav" href="http://www.maxim-ic.com/misc/need_javascript.htm"'
    + ' onMouseOver="status=' + q_status + '; return (true);"'
    + ' onMouseOut="status=\'\'; return (true);"'
    + ' onClick="open_crm_win('+q_mode + ', 650, 600); return(false);"'
    + '>'
    + '<img src="/maxkit/images/' + img_file + '"'
    + ' border=0 width=24 height=12 style="{margin-right: 5;}">'
    + '<font face="Arial, Helvetica, sans-serif" size=1>'
    + label + '</font></a>'
    );
}


//
// open_crm_feedback_win - pop open a feedback form window.
//
function open_crm_feedback_win(mode, w, h)
{
    var window_name = 'maxim_crm_' + mode + '_win';
    var this_url = escape(document.URL);
    var base_url = '';
    var new_url = '';
    var hbe_pn = '';

    if (mode == 'link') {
	base_url = '/page_link_to_us.cfm';
	new_url = base_url + '?getlink=Request&loc=' + this_url;
	new_url += '&pn=' + hbe_pn;
    }
    else {
	base_url = 'http://www.maxim-ic.com/feedback/forms/crm_';
	new_url = base_url + mode + '.cgi?loc=' + this_url;
    }

    var options = "width=" + w + ",height=" + h + ",";
    options += "resizable=yes,scrollbars=yes,status=yes,";
    options += "menubar=no,toolbar=no,location=no,directories=no,";
    options += "top=10,";
    options += "left=100";
    var new_win = window.open(new_url, window_name, options);
    new_win.focus();
}


//-- bookmarks ---------------------------------------------------------------
//----------------------------------------------------------------------------
//
// add_bookmark - add the present page to the user's bookmarks.
//
function add_bookmark(mid, folder)
{
    // Defaults.
    if (! folder) { folder = "Inbox"; }
    if (mid == undefined) { mid = Mary[1]; }
    if (!Mary[0] || mid <= 0) { return (""); }

    // Compose the url to add the bookmark.
    // xxx: cross-domain issues.
    //var url = "/mcc/ajax/add_bookmark.mvp";
    var url = "/mymaxim/wsi/bookmarks/add.mvp";
    var params = {
	"mid": mid,
	"folder": folder,
	"url": document.URL,
	"title": document.title
    };
    //alert(url); return ("");

    Ajax.prepare_get(0, 'add_bookmark', url, add_bookmark_handler, params);
    Ajax.run();
    return (1);
}


//
// add_bookmark_handler - process (ajax) results from adding a bookmark.
//
function add_bookmark_handler()
{
    var this_obj = Ajax.obj || Ajax;

    if (! Ajax.is_done()) { return (""); }
    var mdata = Ajax.process_json_response();
    if (! mdata) { alert(Ajax.error_msg); return (0); }
    var wrapper = Ajax.wrapper;

    // Handle errors.
    if (! wrapper.success) {
	var msg1 = "ERROR: Add Bookmark Failed.\n\n";
	var msg2 = wrapper.msg + "\n\nDetails:\n" + wrapper.details;
	alert(msg1 + msg2);
	return ("");
    }

    alert("Bookmark Added.");
}


//-- page core ops -----------------------------------------------------------
//----------------------------------------------------------------------------
/*-----
//
// select_page_core - switch to a different page core.
//
function select_page_core(new_mode)
{
    // Do nothing if already selected.
    var curr_mode = Univ.page_core_mode;
    //if (curr_mode == new_mode) { alert("Page already showing."); return (0); }
    if (curr_mode == new_mode) { return (0); }

    // Find the current modal element.
    var curr_name = curr_mode + "_page_core";
    var curr_elem = document.getElementById(curr_name);
    if (! curr_elem) {
	alert("Error: " + curr_name + " not found.");
	return ("");
    }

    // Find the new modal element.
    var new_name = new_mode + "_page_core";
    var new_elem = document.getElementById(new_name);
    if (! new_elem) {
	alert("Error: " + new_name + " not found.");
	return ("");
    }

    // Hide the current element.
    curr_elem.style.display = "none";

    // Show the new element.
    new_elem.style.display = "";

    // Flag the new mode.
    Univ.page_core_mode = new_mode;

    // Pass the new element back.
    return (new_elem);
}
-----*/


//-- common dyna ops ---------------------------------------------------------
//----------------------------------------------------------------------------
//
// toggle_display - utility to hide/show a page element.
//
function toggle_display(elem_id)
{
    var elem = top.document.getElementById(elem_id);
    if (elem.style.display == "none") { elem.style.display = ""; return (1); }
    else { elem.style.display = "none"; return (0); }
}


//-- old ajax support --------------------------------------------------------
//----------------------------------------------------------------------------
/*-----
//
// ajax_req_new - instance a new ajax request.
//
function ajax_req_new()
{
    var req = null;

    if (window.XMLHttpRequest) {
	req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
	req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    return (req);
}
-----*/


//-- lite ajax support -------------------------------------------------------
//----------------------------------------------------------------------------
//
// new - instance a new manager.
//
function MaxFire_Ajax_Manager()
{
    this.success = 1;
    this.error_msg = "";
    this.mode = "get";
    this.action_id = "generic";
    return (this);
}


MaxFire_Ajax_Manager.prototype.set_error = function(msg)
{
    this.error_msg = "ERROR " + this.action_id + ": " + msg;
    this.success = 0;
}


MaxFire_Ajax_Manager.prototype.prepare_get = function(obj, action_id, url, handler, params)
{
    // Save values.
    this.mode = "get";
    this.obj = obj;
    this.action_id = action_id;
    this.url = url;
    this.handler = handler;
    this.params = params || 0;				// a hash ref

    // If params were provided, augment the url with them.
    // Params don't have to be passed if already integrated.
    // Caller can call Ajax.build_param_string() on their own, for example.
    if (params) {
	var param_str = this.build_param_string(params);
	var delim = url.indexOf("?") >= 0? '&' : '?';
	url += delim + param_str;
	this.url = url;
    }

    // Reset conditions.
    this.success = 1;
    this.error_msg = "";

    // Setup the request.
    this.init_req(handler);
    if (this.req == null) { this.set_error("No Ajax Support"); return (0); }
    return (1);
}


MaxFire_Ajax_Manager.prototype.prepare_post = function(obj, action_id, url, handler, params)
{
    // Save values.
    this.mode = "post";
    this.obj = obj;
    this.action_id = action_id;
    this.url = url;
    this.handler = handler;
    this.params = params || 0;

    // Reset conditions.
    this.success = 1;
    this.error_msg = "";

    // Setup the request.
    this.init_req(handler);
    if (this.req == null) { this.set_error("No Ajax Support"); return (0); }
    return (1);
}


MaxFire_Ajax_Manager.prototype.run = function()
{
    return (this.mode == "post"? this.run_post() : this.run_get());
}


MaxFire_Ajax_Manager.prototype.run_get = function()
{
    this.req.open("GET", this.url, true);
    this.req.send(null);
}


MaxFire_Ajax_Manager.prototype.run_post = function()
{
    var ctype = "application/x-www-form-urlencoded";
    var param_str = this.build_param_string(this.params);

    this.req.open("POST", this.url, true);
    this.req.setRequestHeader("Content-Type", ctype);
    this.req.setRequestHeader("Content-Length", param_str.length);
    this.req.setRequestHeader("Connection", "close");
    this.req.send(param_str);
}


MaxFire_Ajax_Manager.prototype.build_param_string = function(params)
{
    var pairs = [ ];
    for (key in params) { pairs.push(key + "=" + escape(params[key])); }
    return (pairs.join("&"));
}


MaxFire_Ajax_Manager.prototype.init_req = function(handler)
{
    var req = null;

    if (window.XMLHttpRequest) {
	req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
	req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    this.req = req;
    if (req && handler) { this.req.onreadystatechange = handler; }
    return (req);
}


MaxFire_Ajax_Manager.prototype.is_done = function()
{
    return (this.req.readyState == 4 || this.req.readyState == "complete");
}


MaxFire_Ajax_Manager.prototype.process_json_response = function()
{
    //alert(this.req.responseText);
    var wrapper = 0;
    this.wrapper = 0;
    eval(this.req.responseText);
    if (! wrapper) { this.set_error("Bad wrapper"); return (0); }
    this.wrapper = wrapper;                     // save the wrapper.
    if (! wrapper.success) { this.set_error(wrapper.msg); return (0);}
    return (wrapper.mdata);                     // pass back the payload
}

//****************************************************************************

