//--Javascript Library: Generic Javascript functions--

var __BROWSER = new Object();
__BROWSER.isIE = (document.all) ? true : false;
__BROWSER.isNetscape = (document.layers) ? true : false;

function e(id) {
  return(document.getElementById(id));
}

function AjaxBindGrid(DataTable, HTMLTable, Fields){
  var fields = Fields.split(",");
  if (fields.length == 0)
    for (var c=0; c<DataTable.Columns.length; c++)
      fields.push(DataTable.Columns[c].ColumnName);

  for (var r=0; r<DataTable.Rows.length; r++) {
	  var oRow = HTMLTable.insertRow();
	  for(var c=0; c<fields.length; c++){
  	  oCell = oRow.insertCell();
      oCell.innerText = DataTable.Rows[r][c];
	  }
	}
}

function AjaxErrorWrapper(result) {
  if (result.error != null) {
    alert(result.error.Message);
    return(false);
  }
  else return(true);
}

function DataBindList(Select, DataValueField, DataTextField, DataSource, SelectedValue, Value0, Text0) {
  while(Select.options.length > 0) Select.options.remove(0); //first clear out existing values
  
  for(var i=0; i<DataSource.Rows.length; i++) {
    var o = document.createElement("OPTION");
    Select.options.add(o);
    o.value = DataSource.Rows[i][DataValueField];
    o.innerText = DataSource.Rows[i][DataTextField];
  }
}

function SortOrderDiv_Resort(txtboxName) {
  var sortarray = new Array();
  var txts = document.getElementsByName(txtboxName); //find all the textboxes holding the sort order values
  var container = txts[0].parentElement.parentElement; //establish the outer containing div so we can shuffle everything around in it
 
  for (var i=0; i<txts.length; i++) { //for every line...
    var txt = txts[i]; 
    var div = txt.parentElement;
    div.pos = txt.value; //record the desired position so we can sort on it
    sortarray.push(div); //and stuff this element in an array that we'll sort
  }
  
  sortarray.sort(function(o1, o2) { return( parseInt(o1.pos) - parseInt(o2.pos) );} ); //obvious :)
  
  var SortedKeys = "";
  for (var i=0; i<sortarray.length; i++) { //now for every element in the arry
    sortarray[i].removeNode(true); //remove it from where it was in the DOM
    var txt = sortarray[i].getElementsByTagName("INPUT")[0];
    SortedKeys += txt.key + ",";
    txt.value = i+1;  //set the new textbox value to be the new nice monotomically increasing sort order
    container.appendChild(sortarray[i]); //and re-add all the elements to the DOM in the new order
  }
  
  return(SortedKeys);
}

function testEnterKey(buttonname) {
  if (event.keyCode == 13) {
    event.cancelBubble = true;
    event.returnValue = false;
  	document.getElementById(buttonname).click();
  }
}

function EnableValidators(controlNames) {
  Page_Validators = new Array();
  if (controlNames == null) return;
  var arControlNames = controlNames.split(",");
  for (var i=0;i<arControlNames.length;i++)
    Page_Validators[i] = document.all(arControlNames[i]);
}

function submitPageOnKeyDown(page, keyCodeValue) {
	var code = keyCodeValue || 13;
	
	if (__BROWSER.isIE && window.event.keyCode == code) {
		if (page != null)
			document.location.href = page;
		else
			document.forms[0].submit();
		return false;
	}
	else
		return true;
}

function bannerMouse(tdtag, on) {
	if (on) {
		tdtag.background = "images/" + tdtag.id + "_dark.jpg";
		tdtag.style.border = '2px inset yellow';
	}
	else {
		tdtag.background = "images/" + tdtag.id + "_light.jpg";
		tdtag.style.border = 'none';
	}
}

function getInvoice(invoiceNumber) {
	var features = "toolbar=no, status=yes, menubar=no, location=no, resizable=yes";
	var url = "NoCompression/Invoice.aspx?InvoiceNumber=" + invoiceNumber;
	
	var win = window.open(url, "_blank", features);
	if (win) win.focus();
}

function popup(url, name, width, height, scroll, params) {
	params = (params == null)?"toolbar=no, status=no, menubar=no, location=no, resizable=yes":params;
	var _name = name || "_blank";
	var s = scroll || "no"
	var w = width || "";
	var h = height || "";
	
	if (w != "") params += ", width=" + w;
	if (h != "") params += ", height=" + h;
	if (s != "") params += ", scrollbars=" + s;
	
	var win = window.open('', _name, params); //don't pass the url here so we can pass it below and still avoid retrieving same page twice
	win.focus();
	win.location.replace(url); //this makes the popup refresh even if it's already open
}

function LoadMainWin(url, closepopup) {
  var w = window.open('', 'mainwin'); 
  w.location = url;
  w.focus(); 
  if (closepopup) window.close();
}

function showVideo(url) {
	var params = "toolbar=no, status=yes, menubar=no, location=no, resizable=yes, width=340, height=310";
	var _name = name || "_blank";
	
	var win = window.open(url, _name, params);
	win.focus();
}

function goToPage(page) {
	window.document.forms[0].__PAGE_EVENT_TYPE.value = 'PAGE';
	window.document.forms[0].__PAGENUMBER.value = page;
	window.document.forms[0].submit();
}

function goToFirst() {
	window.document.forms[0].__PAGE_EVENT_TYPE.value = 'MOVEFIRST';
	window.document.forms[0].submit();
}

function previous() {
	window.document.forms[0].__PAGE_EVENT_TYPE.value = 'MOVEPREVIOUS';
	window.document.forms[0].submit();
}

function next() {
	window.document.forms[0].__PAGE_EVENT_TYPE.value = 'MOVENEXT';
	window.document.forms[0].submit();
}

function goToLast() {
	window.document.forms[0].__PAGE_EVENT_TYPE.value = 'MOVELAST';
	window.document.forms[0].submit();
}

function ImageLoader() {
	this.images = new Object();
	this.addImage = _addImage;
	this.setImgSource = _setImgSource;
	this.length = 0;
}

function _addImage(src, name) {
	var img = new Image();
	img.src = src;
	img.name = name;
	
	this.images[name] = img;
	this.length++;
}

function _setImgSource(obj, name, altText) {
	var _altText = altText || null;
	obj.src = this.images[name].src;
	if (_altText != null) obj.alt = _altText;
}

function showImgInPopup(imgName, name, width, height, imgWidth, imgHeight) {
	var params = "toolbar=no, status=yes, menubar=no, location=no, resizable=yes, top=50, left=50";
	var _name = name || "_blank";
	var w = width || "300";
	var h = height || "300";
	var iw = imgWidth || "";
	var ih = imgHeight || "";
	
	if (w != "") params += ", width=" + w;
	if (h != "") params += ", height=" + h;
	
	var win = window.open("", _name, params);
	win.document.write("<br>");
	win.document.write("<div align='center'>");
	var tagStr = "<img src='" + imgName + "'";
	if (iw != "") tagStr += " width='" + iw + "'";
	if (ih != "") tagStr += " height='" + ih + "'";
	tagStr += ">";
	
	win.document.write(tagStr);
	win.document.write("<br><br><a href='javascript:window.close()' title='Close window'><font face='Verdana' size='2'>Close</font></a></div><br>");
	win.focus();
}

function showTextInPopup(text, name, width, height) {
	var params = "toolbar=no, status=yes, menubar=no, location=no, resizable=yes, top=25, left=25";
	var _name = name || "_blank";
	var w = width || "300";
	var h = height || "300";
	
	if (w != "") params += ", width=" + w;
	if (h != "") params += ", height=" + h;
	
	var win = window.open("", _name, params);
	win.document.write("");
	win.document.close();
	win.document.write("<br>");
	win.document.write("<div align='center'>");
	win.document.write(text);
	win.document.write("<br><br><a href='javascript:window.close()' title='Close window'><font face='Verdana' size='2'>Close</font></a></div><br>");
	win.focus();
}

//--Begin Marquee Object--
var __MARQUEE_TEXT = null;
var __MARQUEE_TEXT_CURSOR = null;
var __MARQUEE_INTERVAL = null;
var __MARQUEE_SUBSTRLEN = null;
var __MARQUEE_ACTIVE = null;
var __MARQUEE_CURSOR = null;
var __MARQUEE_FLOW = null;
var __MARQUEE_BANNER_FLOW = null;

function Marquee(text, interval, substrlen) {
	__MARQUEE_TEXT = new Array();
	__MARQUEE_INTERVAL = interval || 100;
	__MARQUEE_SUBSTRLEN = substrlen || 2;
	
	if (text != null && text != "")
		__MARQUEE_TEXT[__MARQUEE_TEXT.length] = text;
	
	__MARQUEE_ACTIVE = false;
	__MARQUEE_FLOW = true;
	__MARQUEE_CURSOR = 0;
	__MARQUEE_TEXT_CURSOR = -1;
	__MARQUEE_BANNER_FLOW = 0;
	
	this.start = _start;
	this.stop = _stop;
	this.setBannerFlow = _setBannerFlow;
	this.addMarqueeText = _addMarqueeText;
	this._marquee = __marquee;
}

function _addMarqueeText(text) {
//--Add a new banner text for rotation in the marquee--
	__MARQUEE_TEXT[__MARQUEE_TEXT.length++] = text;
}

function _start() {
	__MARQUEE_ACTIVE = true;
	_setCursor();
	window.setTimeout(this._marquee, __MARQUEE_INTERVAL);
}

function _stop() {
	__MARQUEE_ACTIVE = false;
	__MARQUEE_CURSOR = 0;
}

function _setBannerFlow(flow) {
//--Set the flow of the marquee: 0 - chronological, 1 - random--
	if (isNaN(flow) == false)
		__MARQUEE_BANNER_FLOW = flow;
}

function __marquee() {
	if (__MARQUEE_ACTIVE) {
		if (__MARQUEE_FLOW)
			window.status = __MARQUEE_TEXT[__MARQUEE_TEXT_CURSOR].substring(0, __MARQUEE_CURSOR);
		else
			window.status = __MARQUEE_TEXT[__MARQUEE_TEXT_CURSOR].substring(__MARQUEE_CURSOR, __MARQUEE_TEXT[__MARQUEE_TEXT_CURSOR].length);
		
		__MARQUEE_CURSOR += __MARQUEE_SUBSTRLEN;
		
		if (__MARQUEE_CURSOR <= __MARQUEE_TEXT[__MARQUEE_TEXT_CURSOR].length + 1) {
			var interval = __MARQUEE_FLOW ? __MARQUEE_INTERVAL : Math.floor(__MARQUEE_INTERVAL * 1.25);
			if (typeof(window.setTimeout) == "object" || typeof(window.setTimeout) == "function")
				window.setTimeout(__marquee, interval);
		}
		else
			__marquee_flow();
	}
}

function __marquee_flow() {
	__MARQUEE_FLOW = !__MARQUEE_FLOW;
	__MARQUEE_CURSOR = 0;
	
	_setCursor();
	
	if (typeof(window.setTimeout) == "object" || typeof(window.setTimeout) == "function")
		window.setTimeout(__marquee, __MARQUEE_INTERVAL * 25);
}

function _setCursor() {
	if (__MARQUEE_BANNER_FLOW == 0) {	//--Serial flow--
		if (__MARQUEE_FLOW) __MARQUEE_TEXT_CURSOR = __MARQUEE_TEXT_CURSOR == (__MARQUEE_TEXT.length - 1) ? 0 : __MARQUEE_TEXT_CURSOR + 1;
	}
	else if (__MARQUEE_BANNER_FLOW == 1) {	//--Random flow--
		if (__MARQUEE_FLOW) __MARQUEE_TEXT_CURSOR = Math.round((__MARQUEE_TEXT.length - 1) * Math.random());
	}
}
//--End Marquee Object--
