//  Date/Time Library
// (c) 2008 MRI
//
// Provides basic AJAX silent post-back functionality.
//
// Public Interface:
//
//      GetSelectControl(Name, Class, Current Value, List of Values, List of Labels, onChange Event,
//          Select All Text, Select Custom Text, Insert a Blank Entry Flag)

//      GetSelectList(Name, Class, Length, Width, Current Value, List of Values, List of Labels, 
//          onChange Event, Select All Text, Select Custom Text, Insert a Blank Entry Flag)

//      SetCheckBox(Control ID, Checked Flag)
//      GetCheckBoxStatus(Control ID)
//      RGBtoHex(R,G,B)
//      ColorToHex(sColor)
    
var iSelectorSelectedIndex = -1;

function GetSelectControl(sName,sClass,sCurrent,aValues,aLabels,sOnChange,sAll,sCustom,bBlank) {
	var s = "<select name='" + sName +"' id='" + sName + "' class='" + sClass + "' onchange='" + sOnChange + "'>";
	var i;
	
	iSelectorSelectedIndex = -1;
	
	if (bBlank) {
		s = s + "<option value=''></option>";
	}
	
	if (sAll != "") {
		s = s + "<option value='A'" + siif(sCurrent == 'A'," select='selected'","") + ">" + sAll + "</option>";
	}

	if (sCustom != "") {
		s = s + "<option value='C'" + siif(sCurrent == 'C'," select='selected'","") + ">" + sCustom + "</option>";
	}

	for (i=0;i<aValues.length;i++) {
		s = s + "<option value='" + aValues[i] + "'" + siif(sCurrent == aValues[i]," select='selected'","") + ">" + aLabels[i] + "</option>";
		
		if ((iSelectorSelectedIndex == -1) && (sCurrent == aValues[i])) {
			iSelectorSelectedIndex = i;
		}
	}
			
	s = s + "</select>";
	
	return s;
}

function GetSelectList(sName,sClass,sCurrent,iSize,iWidth,aValues,aLabels,sOnChange,sAll,sCustom,bBlank) {
	var s = "<select name='" + sName +"' id='" + sName + "' class='" + sClass + "' onchange='" + sOnChange + "' "+
			"multiple size='" + iSize+ "' style='width:" + iWidth + "'>";
	var i;
	
	iSelectorSelectedIndex = -1;
	
	if (bBlank) {
		s = s + "<option value=''></option>";
	}
	
	if (sAll != "") {
		s = s + "<option value='A'" + siif(sCurrent == 'A'," select='selected'","") + ">" + sAll + "</option>";
	}

	if (sCustom != "") {
		s = s + "<option value='C'" + siif(sCurrent == 'C'," select='selected'","") + ">" + sCustom + "</option>";
	}

	for (i=0;i<aValues.length;i++) {
		s = s + "<option value='" + aValues[i] + "'" + siif(sCurrent == aValues[i]," select='selected'","") + ">" + aLabels[i] + "</option>";
		
		if ((iSelectorSelectedIndex == -1) && (sCurrent == aValues[i])) {
			iSelectorSelectedIndex = i;
		}
	}
			
	s = s + "</select>";
	
	return s;
}

function SetCheckBox(sControl,bChecked) {
	var oControl = document.getElementById(sControl);
	
	if (oControl != null) {
		oControl.checked = bChecked;
	} else {
		alert("Checkbox not found " + sControl);
	}
	
	return oControl;
}

function GetCheckBoxStatus(sControl) {
	var oControl = document.getElementById(sControl);
	
	if (oControl != null) {
		if (oControl.checked) {
			return 1;
		} else {
			return 0;
		}
	} else {
		return 0;
	}
}

function toHex(N) {
	if (N==null) {
		return "00";
	}
	
	N=parseInt(N); 
	
	if (N==0 || isNaN(N)) {
		return "00";
	}
	
	N=Math.max(0,N); 
	N=Math.min(N,255); 
	N=Math.round(N);
	
 	return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}

function RGBtoHex(R,G,B) {
	return toHex(R)+toHex(G)+toHex(B)
}

function ColorToHex(sColor) {
	var iColor = parseInt(sColor);
	var iR     = (iColor - iColor % 65536) / 65536;
	var iG     = (iColor % 65536);
	    iG     = (iG - iG % 256) / 256;
	var iB     = iColor % 256;
	return RGBtoHex(iB,iG,iR);
}	
	
function PopOver(sDialog, iHeight, iWidth) {
    var iTop = Math.floor((window.screen.height / 2 - iHeight / 2) * 0.9);
    var iLeft = Math.floor((window.screen.width / 2 - iWidth /2) * 0.9);
    var oDialog = document.getElementById(sDialog);
    
    if (oDialog != null) {
        oDialog.style.position = "absolute";
        oDialog.style.top = iTop;
        oDialog.style.left = iLeft;
        oDialog.style.display = "block";
        oDialog.style.visibility = "visible";
    }
}


