// Leave this line or you'll be sorry

/*
   Format a dictionary of attributes into a string suitable
   for inserting into the start tag of an element.  Be smart
   about escaping embedded quotes in the attribute values.
*/
var APOS = "'"; QUOTE = '"';
var ESCAPED_QUOTE = {  };
ESCAPED_QUOTE[QUOTE] = '&quot;';
ESCAPED_QUOTE[APOS] = '&apos;';
var UNAPOS = new RegExp('&apos;', 'g');

function unEscapeAPOS(value)
{
    return value.replace(UNAPOS, "'");
}

function escapeValue(value) 
{
    var att_value;
    var apos_pos, quot_pos;
    var use_quote, escape, quote_to_escape;
    var att_str;
    var re;
    var result = '';
   
    // Find first quote marks if any
    apos_pos = value.indexOf(APOS);
    quot_pos = value.indexOf(QUOTE);

    re = new RegExp('&', 'g');
    value = value.replace(re, '&amp;');
    re = new RegExp('<', 'g');
    value = value.replace(re, '&lt;');
    re = new RegExp('>', 'g');
    value = value.replace(re, '&gt;');
       
    // Determine which quote type to use around 
    // the attribute value
    if (apos_pos == -1 && quot_pos == -1) 
    {
	return "'" + value +  "'";
    }
        
    // Prefer the single quote unless forced to use double
    if (quot_pos != -1 && quot_pos < apos_pos) {
	use_quote = APOS;
    }
    else {
	use_quote = QUOTE;
    }
   
    // Figure out which kind of quote to escape
    // Use nice dictionary instead of yucky if-else nests
    escape = ESCAPED_QUOTE[use_quote];
        
    // Escape only the right kind of quote
    re = new RegExp(use_quote,'g');
    value = value.replace(re, escape);
    return use_quote + value + use_quote;
}

var req;

function sendRequest(url, xml, callback)
{
    req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) 
    {
	try 
	{
	    req = new XMLHttpRequest();
        } 
	catch(e) 
	{
	    req = false;
        }
	// branch for IE/Windows ActiveX version
    }
    else if(window.ActiveXObject) 
    {
	try 
	{
	    req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) 
	{
	    try 
	    {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
	    catch(e) 
	    {
		req = false;
	    }
	}
    }
    if (req) 
    {
	req.onreadystatechange = callback;
	req.open("POST", url, true);
	req.send(xml);
    }
    else
    {
	alert("Javascript did not get request object.");
    }
}

function loginmsg(group)
{
    alert('Please unlock map with a member or admin key.');
}

function message(txt)
{
}

function deleteAllIBs()
{
    var m = document.getElementById('map');
    var ibs = ['eb', 'ib', 'qb', 'qp', 'em', 'ab', 'ra', 'mb'];
    for (var i = 0; i < ibs.length; i++)
    {
	var a = document.getElementById(ibs[i]);
	if (a != null)
	    m.removeChild(a);
    }
}

function createTextInput(id, nm, s)
{
    var d = document.createElement('div');
    d.style.paddingTop = '5px';

    var t = document.createTextNode(nm + " ");
    d.appendChild(t);
    
    var i = document.createElement('input');
    i.type  = 'text';
    i.size  = s;
    i.id    = id;
    d.appendChild(i);
    return d;
}

function getValue(id)
{
    var e = document.getElementById(id);
    if (e == null)
    {
	return '';
    }
    else
    {
	return e.value;
    }
}

function getTagValue(r, t)
{
    var idl = r.getElementsByTagName(t);
    if (idl != null && idl.length == 1)
    {
	var t = idl[0].lastChild;
	if (t != null)
	    return idl[0].lastChild.nodeValue;
    }
    return '';
}

function showPict(url)
{
    window.open(url, '_blank', 
		'height=500,width=500,location=no,menubar=no,'
		+ 'resizable=yes,titlebar=no,toolbar=no');
}

var Utils = function() {
	return {
		get_box_width: function(w) {
			if (window.innerWidth)
				return Math.min(window.innerWidth-16, w);
			if (document.documentElement && document.documentElement.offsetWidth)
				return Math.min(document.documentElement.offsetWidth-20, w);
			return w;
		},

		get_box_height: function(h) {
			if (window.innerHeight)
				return Math.min(window.innerHeight-16, h);
			if (document.documentElement && document.documentElement.offsetHeight)
				return Math.min(document.documentElement.offsetHeight-20, h);
			return h;
		},

		create_frame: function(n) {
			if (!Ext.isEmpty(document.frames) && !Ext.isEmpty(document.frames[n]))
				return;
			var iframeHTML='<iframe id="' + n + '" name="' + n + '" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"></iframe>';
			document.body.innerHTML+=iframeHTML;
		},

		create_xml_doc: function() {
			var xmlDoc = null;
			if (window.ActiveXObject) {
		    	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				var r = xmlDoc.createElement(root);
				xmlDoc.documentElement = root;
			} else if (document.implementation && document.implementation.createDocument)
			    xmlDoc = document.implementation.createDocument("","ac",null);
			else
				alert('Error creating XML doc. Please contact support.');
			return xmlDoc;
		}
	};
}();
