var ui = {
	formatPhone: function(obj)
	{
		var str = obj.value;
		if(/^(?=\b\w{10}\b)\b\w*[\d]\w*\b/.test(str))
		{
			str = "(" + str.substr(0, 3) + ") " + str.substr(3, 3) + "-" + str.substr(6);
		}
		if(/^\d{3}[-\.]\d{3}[-\.]\d{4}/.test(str))
		{
			str = "(" + str.substr(0, 3) + ") " + str.substr(4, 3) + "-" + str.substr(8);
		}

		obj.value = str;
	},

	//This fuction recieves an HTML textbox as input, and changes the case of 
	//the text according to the value of the switch parameter, which can be:
	//  1 - capitalizes the first letter of the first word.
	//  2 - capitalizes the first letter of each word.
  capitalize: function(obj, which)
	{
		txt = obj.value;
		if(txt=="") return;

		if(which == 1)
		{
			rmdr = txt.substr(1);
			first = txt.substr(0, 1);
			obj.value = first.toUpperCase() + rmdr;
		}
	},

	lazyLoad: function(name) 
	{
		//How to deal with checking if exists??
    if (self.extraFunction) { // Already exists
      return;
    }

    var head = document.getElementsByTagName("head")[0];
		for(i=0; i < head.childNodes.length; i++)
		{
			node = head.childNodes[i];
			if(node && typeof(node.src) != "undefined" && node.src.match(name))
			{
				return;
				head.removeChild(node);
			}
		}

		//alert("Loading script " + name);
    script = document.createElement('script');
    script.id = 'extraScript';
    script.type = 'text/javascript';
    script.src = name;
    head.appendChild(script)
  },

  updateLoginResponse: function(xml, headers, context)
  {
    ui_utils.hideWorking($('imgWorking'));
    var resp = xml.getElementsByTagName("authenticationResponse");
    var msg = resp[0].childNodes[0].childNodes[0].nodeValue;

    $("msgArea").innerHTML = msg;

    //Make Ajax call to retrieve role-specific menu...
    //ajaxCaller.get('webservices/buildmenu.php?buildmenu', {}, ui.updateBuildMenuResponse, true, null);

    if(/success/.test(msg)){ window.location = "index.php?id=23"; /* buildmenu.ajaxGetMenu(); */ }

    //ui_utils.setPaneMaxHeight($('LeftMenuPane'), $('ContentPane'));
  },

  updateLogoutResponse: function(xml, headers, context)
  {
    ui_utils.hideWorking($('imgWorking'));
    var resp = xml.getElementsByTagName("authenticationResponse");
    var msg = resp[0].childNodes[0].childNodes[0].nodeValue;

    if(/userLoggedOut/.test(msg))
		{
			//buildmenu.ajaxGetMenu();
			//buildcontent.ajaxGetContent('webservices/buildcontent.php?home');
			window.location = "index.php";
		}
    else $("msgArea").innerHTML = msg;
  },

  expandcollapse: function(postid)
  {
    whichpost = document.getElementById(postid);
    if (whichpost.className=="shown") { whichpost.className="texthidden"; /* $('msgArea').innerHTML = ""; */ }
    else { whichpost.className="shown"; /* $('msgArea').innerHTML = "Expanded DIV"; */ }
  },

  setPaneHeight: function()
  {
    ui_utils.setPaneMaxHeight($('LeftMenuPane'), $('ContentPane'));
  },

	clickButton: function(e, btn)
	{
		var code = e.which ? e.which : e.keyCode;
		if(code == 13) btn.click();
	}
}
