﻿/*=============================================================================
XmlHttp.js - Javascript for Http calls using XmlDOM object
	
author: Phil Allison
Date:   January 10, 2005
	
	
=============================================================================*/
app = navigator.appVersion; //window.clientInformation.appVersion;

// if the browser version is one that does not support the undefined property, then define it. 
if (app.search('5.') != -1 && app.search('6.') != -1)
    var undefined = 'undefined';

var bName = navigator.appName;
var bVer = parseInt(navigator.appVersion);
var NS4 = (bName == "Netscape" && bVer >= 4);
var IE4 = (bName == "Microsoft Internet Explorer" && bVer >= 4);
var NS3 = (bName == "Netscape" && bVer < 4);
var IE3 = (bName == "Microsoft Internet Explorer" && bVer < 4);
var blink_speed = 2000;
var i = 0;
var QPicks = 1;

var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0;
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5") != -1) ? 1 : 0;
var is_opera = ((navigator.userAgent.indexOf("Opera6") != -1) || (navigator.userAgent.indexOf("Opera/6") != -1)) ? 1 : 0; 

//definition of layer stuff for IE4 or Netscape4
if (NS4 || IE4) {
    if (bName == "Netscape") {
        layerStyleRef = "layer.";
        layerRef = "document.layers";
        styleSwitch = "";
    }
    else {
        layerStyleRef = "layer.style.";
        layerRef = "document.all";
        styleSwitch = ".style";
    }
}

// global variables to be used within various function
// noShow - boolean - controls whether a div should be shown
// GotFocus - boolean - set by the div when it has focus
// isNav    - boolean - probably not needed?
// urlpath  - string - user for additional url pathing for the Xml Pages
var noShow   = false;
var GotFocus = false;
var isNav = (bName == "Netscape");
var urlpath  = '';
var g_ajxDiv = 'InsetPopup';
var curFld;
var hidfld;
var price1;
var price2;
var pbreak;
var winW;
var winH;
var scrl;

// ----------------------------------------------------------------------------
// showInset - shows the inset div and calls the page to load it
// ----------------------------------------------------------------------------
function showInset(fld, url, w, h) {
    //w and h represent the division width and height, if known.
    setInset(fld, 'InsetPopup', w, h)
    openInset(url);
}

// ----------------------------------------------------------------------------
// setInset - sets the div below the specified field.
// ----------------------------------------------------------------------------
function setInset(fld, div, w, h) {
    if (noShow) 
    {
        noShow = false;
        return false;
    }
    winDims();
    var foh = 0;
    g_ajxDiv = div;

    if (fld == null) // if no field passed in, position the pop-up in the screen center.
    {
        fld = document.getElementById(div);
        if (!w)
            w = 0;

        if (!h)
            h = 0;

        if (w == 0)
            w = fld.style.width;

        if (h == 0)
            h = fld.style.height;

        left = (winW - w) / 2;
        tp = ((winH - h) / 2) + scrl;
    }
    else 
    {
        pos = findPos(fld);
        left = pos[0];
        foh = fld.offsetHeight;
       	tp = pos[1] + fld.offsetHeight;
    }
    fld = document.getElementById(div);

    fld.style.position = 'absolute';
    fld.style.display = 'block';
	// if the div is being positioned offscreen, try to make sure it is on screen
    if (winW - getWidth(fld) < left)
    {
    	left = winW - getWidth(fld);
    	if (left < 0)
    		left = 0;
    }

    //if the height is below the frame, move it above the field, if possible.
    var ht = getHeight(fld);
    if (isNaN(ht))
		ht = 0;
    if (winH - ht + scrl < tp)
    {
    	ht += foh;
    	if (tp - ht > 0)
    		tp -= ht;
    }

    if (isNav) 
    {
        fld.style.left = left + 'px';
        fld.style.top = tp + 'px';
    }
    else 
    {
        fld.style.pixelLeft = left;
        fld.style.pixelTop = tp;  
    }

    return true;
}

function winDims()
{
	if (isNav) 
	{
		winW = window.innerWidth;
		winH = window.innerHeight;
		scrl = window.pageYOffset;
	}
	else 
	{
		var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
		winW = document.body.offsetWidth;
		winH = iebody.clientHeight;
		scrl = iebody.scrollTop;
		if (isNaN(scrl))
			scrl = 0;
	}
}

// setDIV - set position of divid relative to the field fld.
function setDIV(fld, div) 
{
	winDims();
    var foh = 0;

    pos = findPos(fld);
    left = pos[0];
    foh = fld.offsetHeight;
    if (foh == undefined)
    	foh = 0;
   	tp = pos[1] + foh;

    var divfld = document.getElementById(div);

   	if (isNav) 
    {
        divfld.style.left = left + 'px';
        divfld.style.top = tp + 'px';
    }
    else 
    {
        divfld.style.pixelLeft = left;
        divfld.style.pixelTop = tp;  
    }
    divfld.style.position = 'absolute';
    divfld.style.display = 'block';
 
    return true;
}

//adjustDIV - Reposition divid relative to fld, if it would be off-screen.
function adjustDIV(fld, divid)
{
	var divW;
	var divH;
	var foh = 0;
	var divfld = document.getElementById(divid);

    divW = divfld.offsetWidth;
    divH = divfld.offsetHeight;
    
    foh = fld.offsetHeight;
    pos = findPos(fld);
    left = pos[0]; //left of the parent field on the screen
   	tp = pos[1] + foh;

    // if the div is being positioned offscreen, try to make sure it is on screen

	//if the height is below the frame, move it above the field, if possible.
    if (winH < (divH + scrl + tp))
    {
		// if the height of the div is greater than the position on the screen (will scroll off the top).
    	if (divH > tp)
    	{
    		//PLA20100120 - removed this code because it was causing a loop situation
    		//divfld.style.width = winW - 20  + 'px';
    		//adjustDIV(fld, divid);
    		//return;
    		tp = 10;
    	}
    	else
    		tp = divfld.offsetTop - (divH+foh) + 2;
		if (isNav) 
			divfld.style.top = tp + 'px';
		else 
			divfld.style.pixelTop = tp;  
    }
    
    pos = findPos(fld);
    left = pos[0]; //left of the parent field on the screen

   	if (winW < divW + left)
    {
    	left = divfld.offsetLeft -((divW+left)-winW) - 25;
		if (isNav) 
			divfld.style.left = left + 'px';
		else 
			divfld.style.pixelLeft = left;
    }

    return;
}

function setGotFocus(flag) 
{
    if (noShow == false)
        GotFocus = flag;
    else
        GotFocus = false;
}

function findPos(obj) {
	var curleft = curtop = 0;

	if (!obj.offsetParent)
		return [0, 0]; 
	do 
	{
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop
		//if (isNav) //PLA20091112 - took this out
			curtop -= obj.scrollTop; //allow for scrolling within divs in browsers except IE
	} while (obj = obj.offsetParent);
	return [curleft,curtop];
}			


function getWidth(el)
{
	return parseInt(getStyle(el, 'width'));
}
function getHeight(el)
{
	return parseInt(getStyle(el, 'height'));
}

function getStyle(el, style) {
  if(!document.getElementById) return;
 
   var value = el.style[toCamelCase(style)];
 
  if(!value)
    if(document.defaultView)
      value = document.defaultView.
         getComputedStyle(el, "").getPropertyValue(style);
   
    else if(el.currentStyle)
      value = el.currentStyle[toCamelCase(style)];
  
   return value;
}
function setStyle(objId, style, value) {
  document.getElementById(objId).style[style] = value;
}
function toCamelCase( sInput ) {
  var oStringList = sInput.split('-');
  if(oStringList.length == 1)  
    return oStringList[0];
  var ret = sInput.indexOf("-") == 0 ?
      oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
  for(var i = 1, len = oStringList.length; i < len; i++){
    var s = oStringList[i];
    ret += s.charAt(0).toUpperCase() + s.substring(1)
  }
  return ret;
}

// ----------------------------------------------------------------------------
// hideInset - close the div that is being displayed by hiding it
// ----------------------------------------------------------------------------
function hideInset(force, div) {
    if (GotFocus && !force) {
        return;
    }
    if (div == null)
        div = g_ajxDiv;

    GotFocus = false;

    SetText(div, '&nbsp;')
    fld = document.getElementById(div);

    if (isNav && isNaN(fld.top) == false) {
        fld.top = '-500px';
    }
    else {
        fld.style.pixelTop = -500;
        fld.style.top = '-500px';
    }
}

function hideInsetNoClear(force, div) {
    if (GotFocus && !force) {
        return;
    }

    if (div == null)
        div = g_ajxDiv;

    GotFocus = false;

    fld = document.getElementById(div);

    fld.style.display = "none";
    return;
}

// ----------------------------------------------------------------------------
// openInset - opens a page through an AJAX call 
// ----------------------------------------------------------------------------
function openInset(url, rootPath) {
    if (rootPath == null)
        rootPath = false;

    if (rootPath)
        burl = getBaseServerName();
    else
        burl = getServerName();

    //alert(burl + urlpath + url );
    httpGet(burl + urlpath + url, insetDataFx);
}

// ----------------------------------------------------------------------------
// insetDataFx - return call function for openInset.
// ----------------------------------------------------------------------------
function insetDataFx(stream) {
    SetText(g_ajxDiv, stream)
}

// ----------------------------------------------------------------------------
// SetText - populates text in a division.
// ----------------------------------------------------------------------------
function SetText(block, txt) {
    if (txt.indexOf("<!DOCTYPE html ") > -1)
        return;
    
    document.getElementById(block).innerHTML = txt;
}

// ----------------------------------------------------------------------------
// xmlGetPage
// ----------------------------------------------------------------------------
function xmlGetPage(page, callback) {
    //	url = getServerName()+getModulePath()+ '/'+ page;
    url = getServerName() + getModuleCall() + '&page=' + page;

    httpGet(url, callback);
}

// ----------------------------------------------------------------------------
// httpGet - call AJAX through a GET
// ----------------------------------------------------------------------------
function httpGet(url, callback) {
    url = urladdinc(url)
    if (IE4)
        http = new ActiveXObject("Microsoft.XMLHTTP");
    else
        http = new XMLHttpRequest();
    http.open("GET", url, true);
    gCallBack = callback;
    http.onreadystatechange = procResponse;
    if (IE4)
        http.send();
    else
        http.send(null);
}

// ----------------------------------------------------------------------------
// procResponse - check for the response data coming back
// ----------------------------------------------------------------------------
function procResponse() {
    if (http.readyState == 4)
        gCallBack(http.responseText);
}

// ----------------------------------------------------------------------------
// httpPost - post instead of get through AJAX
// ----------------------------------------------------------------------------
function httpPost(url, request, callback) {
    url = urladdinc(url)
    if (IE4)
        http = new ActiveXObject("Microsoft.XMLHTTP");
    else
        http = new XMLHttpRequest();
    http.open("POST", url, true);
    gCallBack = callback;
    http.onreadystatechange = procResponse;
    http.send(request);
}

// ----------------------------------------------------------------------------
// urladdinc - This function tags a unique value to the end of the URL 
// being sent so that it does not send back cached data.
// ----------------------------------------------------------------------------
function urladdinc(url) {
    if (url.indexOf("?") == -1)
        url += '?';
    else
        url += '&';

    url += 'xfinc=' + Date();

    return url
}

// ----------------------------------------------------------------------------
// Drop down list functions
// technically, would like the page being called to supply the option values 
// for the same selection list each time.
// should the drop down have - a list; a clear button; a close button?
// ----------------------------------------------------------------------------
var ddwidth = 200;
var seljs = '';
function getHidden() {
    return document.getElementById(curFld.id.replace(/txtCB/gi,  "valCB"));
}
function clearDropText() {
    curFld.value = '';
    hidfld = getHidden();

    if (hidfld != null)
        hidfld.value = '';
    curFld.focus();
}

function GetSelection(fld) {
    noShow = true;
    hidfld = hidfld = getHidden();

    if (hidfld != null)
        hidfld.value = fld.options[fld.selectedIndex].value;

    curFld.value = fld.options[fld.selectedIndex].text;
    curFld.focus();

    if (seljs != '')
        eval(seljs);
    hideInset(true);
}

function hideShow(menu, pointer) {
    thisMenu = document.getElementById(menu);
    pm = document.getElementById(pointer)
    show = (thisMenu.style.display == "none");
    pm.innerHTML = (show ? "<img border='0' src='/includes/images/icon_minus.gif'>" : "<img border='0' src='/includes/images/icon_plus.gif'>");
    thisMenu.style.display = (show ? "block" : "none");
}

this.overdiv = null;

function forceModal()
{
	this.overdiv = document.createElement("div");
	this.overdiv.className = "modaldiv";
	document.body.appendChild(this.overdiv);
}

function forceModalClear()
{
	this.overdiv = document.createElement("div");
	this.overdiv.className = "modaldivclear";
	document.body.appendChild(this.overdiv);
}
/***********************************************************************/
var cr = 1; // current row
var lastval = '';
// override drop down that displays a table list
function initDropDown(fld, url, js, divid, clearit) 
{
	if (QPicks > 1)
	{
		QPicks = 1;
		return;
	}
    curFld = fld;

    if (clearit)
    	fld.value = '';
    if (js != null)
        seljs = js;
    cr = 0;
    lastval = '';
    noShow = false;
    setClassOfId('ispop', 'defPopup');
//    var ifld = document.getElementById('');
//    ifld.className = '';
//    
    if (setInset(fld, divid) == false)
        return;

    SetText(divid, "<table width='100%'></table>");
}

//close the table
function closeDropDown() 
{
    noShow = true;
    if (!GotFocus)
        hideInset(true);
	QPicks = 1;
}
//open the filter with data
function openDropFilter(url, e) 
{
    if (window.event) // && e != null) 
    {
        e = window.event;
    }

    //process keystrokes. 
    // had to add a button to user 'Enter' key. it is an invisible
    // button id=lkItemId
    if (e != null) 
    {
        normal_row_color();
        switch (e.keyCode) 
        {
        	case 13: //enter
        		if (onEnter != '') {
        			eval(onEnter);
        		}
        		break;
            case 39: //right-arrow
                addID(cr);
                break;
            case 33: //pgUp
                cr -= 10;
                change_row_color();
                break;
            case 34: //pgDown
                cr += 10;
                change_row_color();
                break;
            case 35: //end
                //cr = 500;
                //change_row_color();
                break;
            case 36: //home
                //cr = 0;
                //change_row_color();
                break;
            case 38: // focus up
                cr -= 1;
                change_row_color();
                break;
            case 40: // focus down
                cr += 1;
                change_row_color();
                break;

            default:
               	cnt = 1;
               	xmlHttp = GetXmlHttpObject(schFx);
               	url += '&' + Date();
               	xmlHttp.open('GET', url, true);
               	xmlHttp.send(null);
               	break;
        }
    }
    else {
        cnt = 1;
        xmlHttp = GetXmlHttpObject(schFx);
        url += '&' + Date();
        xmlHttp.open('GET', url, true);
        xmlHttp.send(null);
    }

}

function RePopSelect(fld, FixedC, Table, MatchField, MatchTo, ValueField, TextField, OrderBy)
{
	curFld = fld;
	var urlparms = 'ob=' + OrderBy + '&fc=' + FixedC + '&dc=' + Table + '&mf=' + MatchField;  
	urlparms += '&vf=' + ValueField + '&tf=' + TextField + '&Sch=' + MatchTo
	var url = '../AJAX/AjaxDropDownReload.aspx?' + urlparms;
	
	xmlHttp = GetXmlHttpObject(selectFinish);
	urladdinc += '&' + Date();
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}

function selectFinish()
{
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') 
    {
    	text = xmlHttp.responseText;

    	updateSelect(text);
    }
}

function updateSelect(text)
{
	DelOpts(curFld);

	rows = text.split('~');
	
	for (x = 0; x < rows.length; x++)
	{
		cols = rows[x].split('::');
		if (cols[0].trim() != '')
			AddOption(curFld, cols[0], cols[1]);
	}
}


function DelOpts(fld)
{
   while (fld.options.length > 0)
      fld.options[0] = null;
}

function AddOption ( fld, val, txt, vSel)
{
	var opt;

	opt = new Option();
   opt.value = val
   opt.text = txt
   if (vSel == 1)
		opt.selected = true;
		
   if (fld != undefined)
	  fld.options[fld.length] = opt;
}


function initQPicker(fld, js, divid) 
{
	if (curFld == fld && QPicks > 1)
	{
		QPicks = 1;
		return;
	}
    curFld = fld;
    if (js != null)
        seljs = js;
    cr = 0;
    lastval = '';
    noShow = false;

    if (divid == '')
    	return;

    fld.select();
    if (setInset(fld, divid) == false)
        return;
}

function closeQPicker() 
{
    noShow = true;
    if (!GotFocus)
    	hideInsetNoClear(true);
    return true;
}

function pickQPicker(inpval)
{
	curFld.value = inpval;
	//if (!is_ie)
		curFld.focus();
	GotFocus = false;
	noShow = true;

    fld = document.getElementById(g_ajxDiv);

    fld.style.display = "none";
    QPicks = 2;
	closeQPicker();
}

function incrQPicker(amt)
{
	inpval = curFld.value;
    if (inpval == '')
    	inpval = 0;
    else
    	inpval = parseInt(inpval);
    inpval += amt;
    curFld.value = inpval;
    curFld.focus();
}

var onEnter = "";
function openQPicker(e, inpval) 
{
    if (window.event)// && e != null) 
    {
        e = window.event;
    }
    
	if (inpval == null)
		inpval = curFld.value;
    if (inpval == '')
    	inpval = 0;
    else
    	inpval = parseInt(inpval);
    //process keystrokes. 
    // had to add a button to user 'Enter' key. it is an invisible
    // button id=lkItemId

    if (e != null) 
    {
        switch (e.keyCode) 
        {
        	case 37:
        		inpval -= 1;
        		break;
        	case 13: //enter: if an Add function exists, call it
        		var tfld = document.getElementById(onEnter);
        		closeQPicker();
        		if (tfld != null)
        			tfld.click();
        		return false;
        		break;
            case 39: //right-arrow
            	inpval += 1;
            	break;
            case 33: //pgUp
                inpval += 10;
                break;
            case 34: //pgDown
                inpval -= 10;
                break;
            case 35: //end
                break;
            case 36: //home
                break;
            case 38: // focus up
				inpval += 10;
                break;
            case 40: // focus down
				inpval -= 10;
                break;

               default:
               	break;
        }
    }
    if (inpval <= 0)
    	inpval = '';

	curFld.value = inpval;
    QPPriceCalc(price1, price2, pbreak);

    return true;
}

function QPPriceCalc(price1, price2, pbreak)
{
	qty = curFld.value;
	if (qty == '')
		qty = 0;
	else
		qty = parseInt(qty);

	if (qty >= pbreak && pbreak > 0)
		subt = qty * price2;
	else
		subt = qty * price1;
	fld = document.getElementById(curFld.id + 'T');

	if (fld == null)
		return;
	subt = subt.toFixed(2);
	if (subt != 0)
	{
		subt = '$' + subt;
//		toggleDIV('floatdiv', 'block');
	}
	else
		subt = '&nbsp;';
	fld.innerHTML = subt;
}

function GetXmlHttpObject(handler) {
    var objXmlHttp = null;
    if (is_ie) {
        var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
        try {
            objXmlHttp = new ActiveXObject(strObjName);
            objXmlHttp.onreadystatechange = handler;
        }
        catch (e) {
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled');
            return;
        }
    }
    else if (is_opera) {
        alert('Opera detected. The page may not behave as expected.');
        return;
    }
    else {
        objXmlHttp = new XMLHttpRequest();
        objXmlHttp.onload = handler;
        objXmlHttp.onerror = handler;
    }
    return objXmlHttp;
} 

function schFx() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {   //Gather the results from the callback 
        dropDataFx(xmlHttp.responseText);
    }
}

function dropDataFx(stream) {

//    sstream = "<div id='content_top'></div><table id='cnt_inset' border='0' cellspacing='0' cellpadding='0' width='100%' class='ajxDrop'>" + stream;
//    sstream += "</table><div id='content_bottom'></div>";
    SetText(g_ajxDiv, stream);
   	fld = document.getElementById('hidrowno');
   	
   	/// if there is a hidden row number, use it to set the selected row in the list
	if (fld != null)
	{
		cr = parseInt(fld.value);
		change_row_color();
	}
}
// Add the ID to the entry field
function addID(id) 
{
    noShow = true;
    hidfld = getHidden();
    if (id==0)
    {
    	hidfld.value = curFld.value;
    }
    else
    {
    valfld = document.getElementById('hv' + id); //get value from the selected text. id should be the counter cr
    txtfld = document.getElementById('dv' + id); //get value from the selected text. id should be the counter cr

	QPicks = 2;
    if (valfld == null || txtfld == null)
        return;

    curFld.value = txtfld.value;
    hidfld.value = valfld.value;
    }
    noShow = true;
    GotFocus = false;
    if (seljs == '')
		curFld.focus();

    closeDropDown();
    
    // do javascript last?
    if (seljs != '')
        eval(seljs);
}

// sets the row background to white.
function normal_row_color() {
    elem = document.getElementById('row' + cr)
    if (elem != null)
        elem.className = elem.className.substr(0, elem.className.length - 2);  
//    elem.bgColor = "white";
}

//set the row to the highlight color (cyan)
function change_row_color() {
    if (cr == null || cr < 0)
        cr = 0;
    elem = document.getElementById('row' + cr);

    if (elem != null) {
        elem.className = elem.className + "Hi";
        elem = document.getElementById(g_ajxDiv); //scroll the DIV by 12 * the row selected.
        elem = document.getElementById("inset_scroll"); //scroll the DIV by 12 * the row selected.
		var cells = document.getElementsByTagName( 'td' );
		tdh = getTDHeight(); 
        if (elem != null) 
        {
			st = (cr - 2) * tdh;
            if (st < 0) st = 0;
            elem.scrollTop = st;
        }
    }
    else { // if we cannot find something to scroll, then assume the cr is greater than the list size.
        cr = cr - 1;
        change_row_color();
    }
}

function getTDHeight(){
	topd = document.getElementById("content_top")
	botd = document.getElementById("content_bottom")
	
	if (topd == null || botd == null) // return a default value of 15;
		return 15;
		
	cTop = getPixelsFromTop(topd);
	cBottom = getPixelsFromTop(botd);
	cHeight = cBottom - cTop;
	var rowCount = document.getElementById("cnt_inset").getElementsByTagName("TR").length;
	return (cHeight / rowCount);
}

function getPixelsFromTop(obj){
	objFromTop = obj.offsetTop;
	while(obj.offsetParent!=null) {
		objParent = obj.offsetParent;
		objFromTop += objParent.offsetTop;
		obj = objParent;
	}
	return objFromTop;
}

function toggleCK(cnt)
{
	fld = document.getElementById('ck' + cnt);

	if (fld == null)
		return;

	fld.checked = true;
}

function addRangeCK()
{
	var x = 1;
	var vallist = '';
	fld = document.getElementById('ck'+x);
	while (fld != null)
	{
		if (fld.checked)
			vallist += fld.value + "~";
		x++;
		fld = document.getElementById('ck'+x);
	}

    hidfld = getHidden();
        
    curFld.value = vallist;
    if (hidfld != null)
		hidfld.value = vallist;
    curFld.focus();
    noShow = true;
    GotFocus = false;
    closeDropDown();
    if (seljs != '')
        eval(seljs);
}

function toggleDIV(divname, setto)
{	//warning: do not use debug_ within this function
	var fld = document.getElementById(divname);

	if (fld == null)
	return;

	if (isNav) 
	{
        scrl = window.pageYOffset;
        if (scrl > 0)
			fld.style.top += scrl + 'px';
    }
    else 
    {
        var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
        scrl = iebody.scrollTop;
        if (scrl > 0)
			fld.style.pixelTop += scrl;
    }

    if (setto == null)
    {
		if (fld.style.display != 'block')
			setto = 'block';
		else
			setto = 'none';
	}

	fld.style.display = setto;
	return;
}

//return the value of an id field. 
function getValueOfId(id)
{
	var fld = document.getElementById(id)
	if (fld == null)
		return '';

	// then likely it is a span field
	
	if (fld.type == null)
	{
		return fld.innerHTML;
	}
	return escape(fld.value);
}

function setValueOfId(id, val)
{
	val = unescape(val);
	var lfld = document.getElementById(id)

	if (lfld == null)
		return;

	if (lfld.type == null)
	{
		val = val.replace('<form', '<div');
		val = val.replace('</form', '</div');
		lfld.innerHTML = val;
		return;
	}
	if (lfld.type == 'checkbox')
	{
		if (lfld.checked)
			return 1;
		return 0;
	}
	lfld.value = val;
}
function setClassOfId(id, cssClass)
{
    var ifld = document.getElementById(id);
    if (ifld)
		ifld.className = cssClass;
}
function debug_(txt)
{
	toggleDIV('debugdiv', 'block');
	var str = getValueOfId('debugdiv');
	setValueOfId('debugdiv', str + '<br>'+ txt);
}

// append a value to a field name e.g. thisvar=<val>
function appendfield(varnm, fldid)
{
	return varnm + getValueOfId(fldid);
}

function appendrb(varnm, fldid, max)
{
	var x;
	for (x=0; x<max; x++)
	if (document.getElementById(fldid+x).checked)
		return varnm + getValueOfId(fldid+x);
	return '';
}
var ajaxCall = null;
function AJAXCall(fullurl, returnfx)
{
	ajaxCall = returnfx;
	xmlHttp = GetXmlHttpObject(AJAXRet);
	fullurl += '&' + Date();
	xmlHttp.open('GET', fullurl, true);
	xmlHttp.send(null);
}



function AJAXRet()
{
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') 
    {
		if (ajaxCall != null)
		{
			ajaxCall(xmlHttp.responseText); // nothing should get returned.
			ajaxCall = null;
		}
	}
}

var prefix = "";

// This function handles a returned string of field names and their values. 
// e.g. State=SC&FirstName=Phil
// It attempts to find and populate each field.
// It also handles some special fields:
//   MsgPanel - A messgae to display to the user. 
//   fx - a javascript function call to run. This will be eval'd
//   fcs - a field to set the focus to. 
function AJAXFieldData(text)
{
	var list = text.split('&');
	var x;
	var ra;
	
	// populate the fields returned with the returned values
	for (x=0; x < list.length; x++)
	{
		ra = list[x].split('=');
		if (ra.length == 2)
		{
			if (!setValueOfId(prefix + '_' + ra[0], ra[1])) // attempts to find the field with the prefix. if not, try without
				setValueOfId(ra[0], ra[1]);
			if (ra[0] == "MsgPanel")
			{
				if (ra[1] == '')
					toggleDIV(ra[0], 'none');
				else
					toggleDIV(ra[0], 'block');
			}
			// if we have a javascript function to call
			if (ra[0] == "fx" && ra[1] != "")
				eval(ra[1]);
				
			// if we have a field to set the focus to, do it
			if (ra[0] == "fcs" && ra[1] != "")
			{
				var fld = document.getElementById(prefix + '_' + ra[1]);
				if (fld != null)
					fld.focus();
			}
		}
	}
}

function enterDropDown(e)
{
	var keyCode = decipherKey(e);
	if(keyCode == 13)
	{
		addID(cr);
		return false; 
	}
	return true; 
}

function enterQP(e)
{
	var keyCode = decipherKey(e);
	if(keyCode == 13)
	{
		closeQPicker();
		return false; 
	}
	return true; 
}

function decipherKey(e)
{
	var keyCode = null;
	if (window.event != null)
		keyCode = window.event.keyCode;
	else
	keyCode = e.keyCode? e.keyCode : e.charCode;
	return keyCode;
}