//grid global startup js
function qrygridstartup()
{
	ajaxtt_hide();
}

function SaveButtonBusy(e)
{
	e.value = "<img src='img/working.gif' />" + e.value;
}

	function toggle_div(el1name, el2name, showDiv, hideDiv) 
	{	
		var el1 = ''; 
		var el2 = ''; 
		if (el1name.indexOf(',') > 0)	
			el1 = window.document.getElementById (el1name.substring(0,el1name.indexOf(','))); 
		else	
			el1 = window.document.getElementById (el1name);
		if (el1.className == showDiv) 
		{ 
			while (el1name.indexOf(',') > 0) 
			{
				window.document.getElementById((el1name.substring(0,el1name.indexOf(',')))).className = hideDiv;	
				el1name = el1name.substring(el1name.indexOf(',') + 1);
			}	
			while (el2name.indexOf(',') > 0)	
			{	
				window.document.getElementById((el2name.substring(0,el2name.indexOf(',')))).className = showDiv; 
				el2name = el2name.substring(el2name.indexOf(',') + 1);	
			}
		}
		else
		{ 
			while (el2name.indexOf(',') > 0)	
			{	
				window.document.getElementById((el2name.substring(0,el2name.indexOf(',')))).className = hideDiv;	
				el2name = el2name.substring(el2name.indexOf(',') + 1);
			}	
			while (el1name.indexOf(',') > 0)
			{	
				window.document.getElementById((el1name.substring(0,el1name.indexOf(',')))).className = showDiv;
				el1name = el1name.substring(el1name.indexOf(',') + 1);
			}
		}
	}
	
	function toggle_div_single (id, showDiv, hideDiv)
	{
		var el1 = id.substring(0, id.indexOf(','));
		if (document.getElementById (el1).className == showDiv)
			document.getElementById (el1).className = hideDiv;
		else
			document.getElementById (el1).className = showDiv;
	}

	function div_toggle (id)
	{
		if (document.getElementById (id).className == "opendiv")
			document.getElementById (id).className = "closeddiv";
		else
			document.getElementById (id).className = "opendiv";
	}

//excel export stuff
function dumpTableToExcel (id, opt)
{
	var el = document.getElementById(id);
	if (!el)
		return;
	var tbltext = el.innerHTML;
	var oldtarg = document.forms[0].target;
	var oldact = document.forms[0].action;
	var sopt = opt.split ("&");
	var i;
	for (i = 0; i < sopt.length; i++)
	{
		var soptitem = sopt[i].split("=");
		var optionname = soptitem[0].toLowerCase();
		if (optionname == "filename")
		{
			if (!document.getElementById("__dump_table_to_excel_filename"))
			{
				currentElement = document.createElement("input");
				currentElement.setAttribute("type", "hidden");
				currentElement.setAttribute("name", "__dump_table_to_excel_filename");
				currentElement.setAttribute("id", "__dump_table_to_excel_filename");
				document.forms[0].appendChild(currentElement);
			}
			document.getElementById("__dump_table_to_excel_filename").value = soptitem[1];
		}
	}
	if (!document.getElementById("__dump_table_to_excel"))
	{
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", "__dump_table_to_excel");
		currentElement.setAttribute("id", "__dump_table_to_excel");
		document.forms[0].appendChild(currentElement);
	}
	if (!document.getElementById("__dump_table_to_excel"))
		return;	//bad news
	document.getElementById("__dump_table_to_excel").value = tbltext;
	document.forms[0].target = "_new";
	document.forms[0].action = "dumptabletoexcel.aspx";
	__doPostBack("","");
	
	document.forms[0].__EVENTTARGET.value = "";
	document.forms[0].__EVENTARGUMENT.value = "";
	document.forms[0].target = oldtarg;
	document.forms[0].action = oldact;
	var nel = document.getElementById("__dump_table_to_excel");
	nel.parentNode.removeChild(nel);
}

function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function replaceSelection (input, replaceString) {
	if (input.setSelectionRange) {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
    
		if (selectionStart != selectionEnd){ 
			setSelectionRange(input, selectionStart, selectionStart + 	replaceString.length);
		}else{
			setSelectionRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
		}

	}else if (document.selection) {
		var range = document.selection.createRange();

		if (range.parentElement() == input) {
			var isCollapsed = range.text == '';
			range.text = replaceString;

			 if (!isCollapsed)  {
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
	}
}


// We are going to catch the TAB key so that we can use it, Hooray!
function catchTab(item,e){
	if(navigator.userAgent.match("Gecko")){
		c=e.which;
	}else{
		c=e.keyCode;
	}
	if(c==9){
		replaceSelection(item,String.fromCharCode(9));
		setTimeout("document.getElementById('"+item.id+"').focus();",0);	
		return false;
	}
		    
}

function tb_keypress_filter_numericonly()
{
	var e = event || evt;
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

// CPMS Ajax Stuff
function AjaxTxCb (u, id, ch0, ch1, chbusy)
{
	this.u = u;
	this.id = id;
	this.ch0 = ch0;
	this.ch1 = ch1;
	this.chbusy = chbusy;
	this.retrieve ();
}

AjaxTxCb.prototype.update = function (req)
{
	var t = parseInt (req.responseText);
	window.document.getElementById (this.id).src = (t == 1) ? this.ch1 : this.ch0;
}

AjaxTxCb.prototype.retrieve = function ()
{
	var tx = new Ajax.Request (this.u, {method: 'get', onComplete: this.update.bind(this)});
}

function ajaxtx_cb (u, id, ch0, ch1, chbusy)
{
	document.getElementById (id).src = chbusy;
	var newtx = new AjaxTxCb (u, id, ch0, ch1, chbusy);
}

// tooptip using tipster
var current_ajax_tooltip = '';
function AjaxTt (obj, u, w)
{
	current_ajax_tooltip = u;
	this.ID = 'fw_ajax_tooltip';
	this.u = u;
	this.w = w;
	this.show (obj);
	//this.retrieve ();
}

AjaxTt.prototype.update = function (req)
{
//alert (this.u);
//alert (current_ajax_tooltip);
//alert((this.u == current_ajax_tooltip));
	if (this.u == current_ajax_tooltip)
	{
		//"<div style='float:right;width:15px;'><a href='javascript:TipObj.list[\"" + this.ID + "\"].hide(\"main\");'><img src='img/close-button.gif' /></a></div>"
		TipObj.list[this.ID].tips["main"][3] = req.responseText;
//		alert(req.responseText);
//		TipObj.list[this.ID].actTip = '';
//		TipObj.list[this.ID].show("main");
//		TipObj.list[this.ID].div.write("chris hoke");
		TipObj.list["fw_ajax_tooltip"].replaceContent ("main");
//		var showStr = 'with (TipObj.list["fw_ajax_tooltip"]) {  ' +
//			'replaceContent("fw_ajax_tooltip"); ' + '/* no fade */ }';
//		alert (showstr);
		//eval(showStr);
	}
}

AjaxTt.prototype.retrieve = function ()
{
	var urand = this.u + ((this.u.indexOf("?") > 0) ? "&" : "?") + "zrnd=" + Math.random();
	var tx = new Ajax.Request (urand, {method: 'get', onComplete: this.update.bind(this)});
}

function ajaxtt_hide ()
{
	if (TipObj.list['fw_ajax_tooltip'])
		TipObj.list['fw_ajax_tooltip'].hide();
}

AjaxTt.prototype.show = function (obj)
{
	if (TipObj.list[this.ID])
	{
		var tp = TipObj.list[this.ID];
//		if (!obj.onmouseout)
//			obj.onmouseout = new Function ('evt', 'TipObj.list["' + this.ID + '"].hide()');
    tp.tips.main = new Array (5, 5, this.w, 'Loading...');
		//TipObj.list[this.ID].actTip = -1;
		tp.tipStick = 0;
		tp.ajaxTt = this;
		tp.showajax('main');
	}
	else
	{
		var ttdiv = document.createElement('div');
		//ttdiv.innerHTML = "<div class='infobox'><div id='div_tt_inner_" + this.ID + "' style='width:" + this.w + "px;'>Loading...</div></div>";
    ttdiv.id = this.ID + "Layer";
    ttdiv.style.position = 'absolute';
    ttdiv.style.zIndex = 10001;
    ttdiv.style.width = 'auto';
    ttdiv.style.backgroundColor = '#FFFFFF';
    document.body.appendChild(ttdiv);
		var tp = new TipObj(this.ID);
		tp.fadeInSpeed = 100;	//87;
		tp.fadeOutSpeed = 100; /* 87; */
		tp.showDelay = 250;
		tp.hideDelay = 250;
		var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? 1 : 0;
		tp.IESelectBoxFix = !ie7 & isIE;
		//document.getElementById("debugtext").innerHTML = ie7 && isIE;
    tp.template = "<div class='infoBox'><div id='div_tt_inner_" + this.ID + "' style='width:" + this.w + "px;z-index:10050;'>%3%</div></div>";
    tp.tips.main = new Array (5, 5, this.w, 'Loading...');
		tp.tipStick = 0;
//		if (!obj.onmouseout)
//			obj.onmouseout = new Function ('evt', 'TipObj.list["' + this.ID + '"].hide()');
		//TipObj.list[this.ID].actTip = null;
		tp.ajaxTt = this;
		tp.showajax('main');
	}
}

function ajaxtt_show (obj, u, w)
{
	ajaxtt_hide();
	var tt = new AjaxTt (obj, u, w);
}

//CPMS Ajax TableEdit object
function AjaxTxTableEdit()
{
	this.u = "TableEdit.aspx";
	this.tableName = "";
	this.keys = "";
	this.values = "";
	this.sql = "";
	this.unescape = "";
	this.rowsAffected = -1;
	this.error = false;
	this.errorMessage = "";
	this.returnCode = null;
}

AjaxTxTableEdit.prototype.update = function (req)
{
	var s = "" + req.responseText;
	if (s.length < 1)
	{
		this.error = true;
		this.errorMessage = "No value returned from AJAX update.";
	}
	else
	{
		var rv = s.split("&");
		this.returnCode = parseInt (rv[0]);
		//alert(rv.length);
		if (this.returnCode != 1)
		{
			this.error = true;
			this.errorMessage = "Unspecified error.";
		}
		var i=0;
		for (i=0; i<rv.length; i++)
		{
			var v = rv[i].split("=");
			if (v[0] == "rowsaffected")
				this.rowsAffected = parseInt (v[1]);
			else if (v[0] == "error")
				this.errorMessage = v[1];
			else
			{
				//do something with junk
			}
		}
	}
}

AjaxTxTableEdit.prototype.save = function ()
{
	var postBody = "z=0";
	if (this.tableName.length > 0)
	{
		postBody += "&inpTableName=" + this.tableName;
		postBody += "&inpKeys=" + escape (this.keys);
	}
	else
		postBody += "&inpSql=" + this.sql;
	postBody += "&inpUnescape=" + this.unescape;
	postBody += "&inpValues=" + escape (this.values);
	var tx = new Ajax.Request (this.u, {asynchronous: false, method: 'post', postBody: postBody, onComplete: this.update.bind(this)});
}

