/* ---------------------------- Suggest Box -------------------------------*/
String.prototype.trim = function () {
	return this.replace(/^ +/,"").replace(/ +$/,"");
}

String.prototype.endsWith = function (ch){
	return this.match(ch + "$") != null;
}

var letterList = null;  // array

function getCaretPosition(textbox){
	// IE stuff
	if (document.selection){
		var sel = document.selection.createRange();

		sel.moveStart('character', -textbox.value.length);
		return sel.text.length;
	}
	else if (textbox.selectionEnd) return textbox.selectionEnd;
}

var range=0;
function setCaretPosition(obj, selectFrom) {   
   if(obj.createTextRange){ //ie + opera		
		if (range == 0) range = obj.createTextRange();
		range.moveEnd("character",obj.value.length);
		range.moveStart("character",selectFrom);		
		obj.focus();
		setTimeout('range.select()', 30);		
	} else if (obj.selectionEnd) {
      obj.selectionEnd = selectFrom;
   }
} 

function getCurrentWord(textbox){
	var caret = getCaretPosition(textbox);
	var str = textbox.value.substring(0, caret);
	var ary = str.split(',');
	var last = str;
	if (ary.length > 0) last = ary[ary.length-1];
	return last.trim();
}

function clearChildren(div){
	if (div.childNodes){
		for (var i = div.childNodes.length-1; i >= 0; i--){
			div.removeChild(div.childNodes[i]);
		}
	}
}

function showList(textbox){
	if (letterList == null) return;
	var div = document.getElementById(textbox.getAttribute("TargetControlID"));
	
	var list = new Array();
	var cword = getCurrentWord(textbox);
	for (var i = 0; i < letterList.length; i++)
	{
		if ((letterList[i].toUpperCase().indexOf(cword.toUpperCase()) == 0 || cword == "*") && (textbox.value.indexOf(letterList[i] + ",", 0) == -1))
		{			
			    list.push(letterList[i]);
		}
	}

	clearChildren(div);
	
	for (var i = 0; i < list.length; i++){
	    var container = document.createElement("div");
		var link = document.createElement("a");
		link.value = list[i];
		link.innerHTML = list[i].replace(/ /g, "&nbsp;");				
		bindClick(link, textbox.id);
		container.appendChild(link);
		div.appendChild(container);
		if ( i < list.length-1 )
		    div.appendChild(document.createTextNode(" "));
	}
	
	div.style.display = "block";
}

function suggestLinkClick (e, textboxID){
	var div, textbox, word;
	textbox = document.getElementById(textboxID);
	div = document.getElementById(textbox.getAttribute("TargetControlID"));
	
	//div.style.display = "none";
	
	e = (e ? e : window.event);
	word = (e && e.target ? e.target.value : e.srcElement.value);
	
	var caretpos = getCaretPosition(textbox);	
	var str = getCurrentWord(textbox);
	var str1, str2;
	
	str1 = textbox.value.substring(0, caretpos);
	str2 = textbox.value.substring(caretpos, textbox.value.length);	
	
	if ( textbox.value.indexOf(",") > -1 ){
	    var lastCommaLocation = str1.lastIndexOf(", ", caretpos);
	    str1 = str1.substring(0, lastCommaLocation+2);
	    
	}	
	else
        str1 = str1.replace(str, "");		
	
	str = word + ", ";
		
	textbox.value = str1 + str + str2;
	//letterList = null;
	textbox.focus();
	setCaretPosition(textbox, textbox.value.length);
}

function bindClick(link, textboxID){
	if (link.onclick) return;
	link.onclick = new Function("e", "suggestLinkClick(e, '" + textboxID + "'); this.parentNode.style.display = 'none';");
}



/* ---------------------------- =Popup Window -------------------------------*/

function popupWindow (url, name, width, height, properties) {
	if (!name) name = "popWin";
	if (!width) width = "700";
	if (!height) height = "480";
	
	var features = "width=" + width + ",height=" + height;

	//	-- properties --
	//	not set or false    : standard clean window with no address bar, etc.
	//	some string         : use the string
	
	if (!properties || properties == false) {
		features += ",left=100,top=100,toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=1,scrollbars=1";
	} else {
		features += properties;
	}
	
	var popWin = window.open(url, name, features);	
	checkPopped(popWin);
	return popWin;
}

function checkPopped (winName) {
	if (winName)
		winName.focus();
	else {
	    var popupWarning = 'Pop-up blocking software in your browser has prevented us from opening a new window. In order for this site to function properly please disable your Pop-up blocking software for this site.';
		alert(popupWarning);
		return;
	}
}

/* Catch Right Clicks */
function sourceProtect(e) 
{   
	var ie = (document.all)? true:false;

	if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2))
	{
		alert("Source protected.");
		return false;
	}
	else if (ie&&(window.event.button==2)) 
	{
		alert("Source protected.");
		return true;
	}
	else if( e == 'oncontextmenu' ) //FireFox's oncontextmenu passes in 'oncontextmenu' instead of this.
	{
		alert("Source protected.");
		return false;
	}
}


/* ---------------------------- =Toggle Display -------------------------------*/

// elmID          : ID of element to toggle
// defaultDisplay : Overides the default display for an element (block, inline, none)
// force          : If set to true, forces the element to use the defaultDisplay (overides toggling)

function toggleDisplay (elmID, defaultDisplay, force) {
	// Get the style property of our element
	if (document.getElementById) {
		// standards way
		var elmStyle = document.getElementById(elmID).style;
	} else if (document.all) {
		// old msie version way
		var elmStyle = document.all[elmID].style;
	} else if (document.layers) {
		// nn4 way
		var elmStyle = document.layers[elmID].style;
	}
	
	// Force display to defaultDisplay if force is set
	if (force)
	{
		elmStyle.display = defaultDisplay; return;
	}
	// If there is no defaultDisplay set check the default state for the element
	else if (!defaultDisplay)
	{
		var elmType = document.getElementById(elmID).tagName.toLowerCase();
		if (elmType == 'a' || elmType == 'span' || elmType == 'strong' || elmType == 'em')
			var defaultDisplay = 'inline';
		else
			var defaultDisplay = 'block';
	}
	
	// If not forced toggle it's display
	if (elmStyle.display == 'none') {
		// if set to 'none' toggle to whatever the default for this element is
		elmStyle.display = defaultDisplay;
	} else {
		// if set to 'block' or default (which can't be read but is 'block')
		// toggle to 'none'
		elmStyle.display = 'none';
	}
}


function toggleEnabled(elmID, active) {
	// Get the style property of our element
	if (document.getElementById) {
		// standards way
		var elm = document.getElementById(elmID);
	} else if (document.all) {
		// old msie version way
		var elm = document.all[elmID];
	} else if (document.layers) {
		// nn4 way
		var elm = document.layers[elmID];
	}
		
	elm.disabled = !active;
	
	//fix for checkboxes and radio buttons enclosed in span	
	if (elm.parentNode.tagName.toLowerCase()== 'span' && elm.parentNode.disabled != elm.disabled)
	    elm.parentNode.disabled = elm.disabled;	
}
          

/* ------ Radio Buttons Spanning Downwards in Repeater or Datagrids ------------------------
   -- have naming issues on their groups because they want to go by row.  Attaching this  ---
   -- to the radiobutton's OnClick attribute will cause only element to be checked in the ---
   -- given column (nameregex is a regular expression matching all the radiobuttons in your -
   -- group.  Do '<repeater_name>.*<radio_button_group_name>' and pass in 'this' for current
   -- is the standard arguments. ----------------------------------------------------------- */
function SetUniqueRadioButton(nameregex, current, clearAll )
{
	// stringAlert added for debugging.
	//var stringAlert = '';
   
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
	  elm = document.forms[0].elements[i]
	  if (elm.type == 'radio')
	  {
		 //stringAlert += 'radio, ';
		 if (re.test(elm.name))
		 {
			//if current isnt passed in, set it to the first radiobutton that is checked and fire the click event. 
			//This is used for pages that have posted back and need to reset based on the radio buttons.
			if(current == '' && elm.checked == true && clearAll == false)
			{
				current = elm;
				current.focus();
				current.click();
			}
			else
				elm.checked = false;
		 }
		 else
		 {
			//stringAlert += 'no match, ' + elm.name + '; ';
		 }
		 //stringAlert += '\n';
	  }
	  else
	  {
		//stringAlert += 'no radio; \n';
	  }
   }
   // alert( stringAlert );
   current.checked = true;
}

function SetInnerHtml(elmID, newtext)
{
	var element = document.all ? document.all[elmID] : document.getElementById(elmID);
	element.innerHTML = newtext;
}

