var mPrevURL=""

// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();

// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
// If you want to allow spoofing, take out the tests for opera and webtv.
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
            && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

function gotoPage(url,target) {
	// Searches frames up to top window for given frame
	// If frame is not found, loads given url in the document object
	var ltarget = null;			
	var lwindow = window;
	
	// Go on until target frame was found
	while (ltarget == null) {
			// Check if frame is in current layer
			ltarget = lwindow.frames[target];
			if (lwindow == lwindow.parent) {
				// If we are at the top and frame was not in current layer, set frame to document object
				if (ltarget == null) {
					if (target == '_top') {
						ltarget = window.top;
					} else {
						ltarget = document;
					}
				}
			} else {
				// We are not at top, move one layer up
				lwindow = lwindow.parent;
			}
	}

	// Assign url to found frame
	ltarget.location.href=url;
}	

function confirmaction(msg,url,target) {
	if (confirm(msg)) {
		gotoPage(url,target);
	}
}

function CheckNumericInput() {
	x=event.keyCode;
	
	if (x < 48 || x > 57) {
		event.keyCode=0;
	}
}

function CheckMaxLength(obj, maxlength) {
	if ((obj.value.length >= maxlength) && (maxlength > 0)){
		obj.value = obj.value.substr(0, maxlength);
		return (false);
	} else {
	 	return (true);
 	}	
}

function CheckMinMax(fMinValue, fMaxValue) {
	var value=event.srcElement.value;
	
	 if ( (fMinValue != 0) && (value < fMinValue)) {
		event.srcElement.value=fMinValue;
		alert("Minimum value is " + fMinValue);
	} else if ( (fMaxValue != 0) && (value > fMaxValue)) {
		event.srcElement.value=fMaxValue;
		alert("Maximum value is " + fMaxValue);
	}
}

function ShowPopupAtMouse(e, picturefile, width, height) {	
  if (picturefile==mPrevURL) {
  	mPrevURL="";
  	closeit();
  } else {
	  var cx;
	  var cy;
	  var left;
	  var top;

	  if (is_nav) {
	  	cx = e.pageX;
	  	cy = e.pageY;
	  } else {
	  	cx= e.x; 
	  	cy = e.y;
	  }
	
		if (cy > height + 10) {
			top = cy - height - 10;
		} else {
			top = cy;
		}
		
		if (cx > width + 10) {	
			left = cx - width - 10;
		} else {
			left = cx + 10;
		}
		
		mPrevURL = picturefile;
		loadwindow(picturefile, width, height, left, top);
	}
}

function ShowPopupAtPosition(e, picturefile, width, height, left, top) {	
  if (picturefile==mPrevURL) {
  	mPrevURL="";
  	closeit();
  } else {		
		mPrevURL = picturefile;
		loadwindow(picturefile, width, height, left, top);
	}
}

// ***************************************************************************
// ********************* COLOR FUNCTIONS   ***********************************
// ***************************************************************************

// convert a single digit (0 - 16) into hex
function enHex(aDigit) { return("0123456789ABCDEF".substring(aDigit, aDigit+1)) }

// convert a hex digit into decimal
function deHex(aDigit) { return("0123456789ABCDEF".indexOf(aDigit)) }

// Convert a 24bit number to hex
function toHex(n) {
	var hexstr = '';
	var hexvalue = '';
	
	hexvalue = enHex((0xf00000 & n) >> 20);
	if (hexvalue != '0' || hexstr.length > 0) hexstr += hexvalue;
	
	hexvalue = enHex((0x0f0000 & n) >> 16);
	if (hexvalue != '0' || hexstr.length > 0) hexstr += hexvalue;

	hexvalue = enHex((0x00f000 & n) >> 12);
	if (hexvalue != '0' || hexstr.length > 0) hexstr += hexvalue;

	hexvalue = enHex((0x000f00 & n) >> 8);
	if (hexvalue != '0' || hexstr.length > 0) hexstr += hexvalue;

	hexvalue = enHex((0x0000f0 & n) >> 4);
	if (hexvalue != '0' || hexstr.length > 0) hexstr += hexvalue;

	hexvalue = enHex((0x00000f & n) >> 0);
	hexstr += hexvalue;

	return hexstr;
}

// Convert a six character hex to decimal
function toDecimal(hexNum) {
	var tmp = ""+hexNum.toUpperCase()
	while (tmp.length < 6) tmp = "0"+tmp
	
	return ((deHex(tmp.substring(0,1)) << 20) +
		(deHex(tmp.substring(1,2)) << 16) + 
		(deHex(tmp.substring(2,3)) << 12) +
		(deHex(tmp.substring(3,4)) << 8) +
		(deHex(tmp.substring(4,5)) << 4) +
		(deHex(tmp.substring(5,6))))
}

function intcolor2hex(intcolor) {
    var hexcolor = '';
    var hexvalue = '';
    var intvalue = 0;

    //get the RGB values separately, then convert each to hex...
    intvalue = intcolor % 256;
    hexvalue = toHex(intvalue);
    if (hexvalue.length==1) hexvalue = '0' + hexvalue;
    hexcolor = "#" + hexvalue;
    
    //divide value by 256 to get just the G and B
    intcolor /= 256; intcolor.toFixed();
    intvalue = intcolor % 256;
    hexvalue = toHex(intvalue);
    if (hexvalue.length==1) hexvalue = '0' + hexvalue;
    hexcolor += hexvalue;
    
    //divide again by 256 to get just the B
    intcolor /= 256; intcolor.toFixed();
    intvalue = intcolor % 256;
    hexvalue = toHex(intvalue);
    if (hexvalue.length==1) hexvalue = '0' + hexvalue;
    hexcolor += hexvalue;

		return hexcolor;
}

function hexcolor2int(hexcolor) {
    var intcolor = 0;
    var hexvalue = '';
    var i = hexcolor.length;
    
    // get the Hex values separately, then convert each to dec...
    hexvalue = hexcolor.substring(i-2,i); i-=2;
    intcolor = toDecimal(hexvalue) << 16;

    hexvalue = hexcolor.substring(i-2,i); i-=2;
    intcolor += toDecimal(hexvalue) << 8;

    hexvalue = hexcolor.substring(i-2,i); i-=2;
	  intcolor += toDecimal(hexvalue);
    
		return intcolor;
}

// ***************************************************************************
// ***************** END COLOR FUNCTIONS   ***********************************
// ***************************************************************************
