function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    if (evt.charCode == 0 && (evt.keyCode > 34 && evt.keyCode < 47)) return true;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
    return true;
}
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    num = Math.floor(num/100).toString();
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num);
}
// Global vars. You don't need to make changes here to change your sliders.
// Changing the attributes in your (X)HTML file is enough.
var isSliding = false;
var sliders   = [];
var txtBoxes  = [];
var slider    = {};
var txtBox    = {};

function movePos(elm, pos) {
	elm.style.width = (pos + 18) + 'px';
}

function getPos(elm) {
	pos = parseInt(elm.style.width);
	if (isNaN(pos)) pos = 0;
	return pos - 18;
}
// moveSlider: Handles slider and display while dragging
function moveSlider(evnt)
{
	var evnt = (!evnt) ? window.event : evnt; // The mousemove event
	if (isSliding) { // Only if slider is dragged
		slider.x = slider.startOffsetX + (evnt.screenX-(evnt.screenX*slider.direction)); // Horizontal mouse position relative to allowed slider positions
		if (slider.x > slider.xMax) slider.x = slider.xMax; // Limit horizontal movement
		if (slider.x < slider.xMin) slider.x = slider.xMin; // Limit horizontal movement
		movePos(slider, slider.x);  // move slider to new horizontal position
		var sliderVal = slider.x; // pixel value of slider regardless of orientation
		var sliderPos = (slider.distance / txtBox.valuecount) * 
			Math.round(txtBox.valuecount * sliderVal / slider.distance);
		var v = Math.round((sliderPos * slider.scale + slider.from) * // calculate display value
			Math.pow(10, txtBox.decimals)) / Math.pow(10, txtBox.decimals);
		txtBox.value = v; // put the new value in the slider display element
		return false;
	}
	return
}
// slide: Handles the start of a slider move.
function slide(evnt) {
	if (!evnt) evnt = window.event; // Get the mouse event causing the slider activation.
	slider = (evnt.target) ? evnt.target.parentNode : evnt.srcElement.parentNode; // Get the activated slider element.
	if (slider.className=="rangeSlider") {
		return;
	}
	var dist = parseInt(slider.getAttribute('distance')); // The allowed slider movement in pixels.
	slider.distance = dist;
	var displayId = slider.getAttribute('display'); // ID of associated display element.
	txtBox = document.getElementById(displayId); // Get the associated display element.
	txtBox.sliderId = slider.id; // Associate the display with the correct slider.
	var dec = parseInt(txtBox.getAttribute('decimals')); // Number of decimals to be displayed.
	txtBox.decimals = dec ? dec : 0; // Default number of decimals: 0.
	var val = parseInt(txtBox.getAttribute('valuecount'))  // Allowed number of values in the interval.
	txtBox.valuecount = val ? val : slider.distance + 1 // Default number of values: the sliding distance.
	var from = parseFloat(txtBox.getAttribute('from')) // Min/start value for the display.
	from = from ? from : 0 // Default min/start value: 0.
	var to = parseFloat(txtBox.getAttribute('to')) // Max value for the display.
	slider.scale = (to - from) / slider.distance // Slider-display scale [value-change per pixel of movement].
	// Set limits for horizontal sliders.
	slider.from = from;
	
	slider.xMax = getPos(slider.parentNode) - 17 - getPos(slider.otherSlider);
	
	slider.startOffsetX = getPos(slider) - (evnt.screenX-(evnt.screenX*slider.direction)); // Slider-mouse horizontal offset at start of slide.
	isSliding = true;
	document.onmousemove = moveSlider; // Start the action if the mouse is dragged.
	document.onmouseup = sliderMouseUp; // Stop sliding.
	return false;
}
// sliderMouseUp: Handles the mouseup event after moving a slider.
// Snaps the slider position to allowed/displayed value. 
function sliderMouseUp() {
	if (isSliding) {
		snapIntoPos();
		if (document.removeEventListener) { // Remove event listeners from 'document' (W3C).
			document.removeEventListener('mousemove', moveSlider, false);
			document.removeEventListener('mouseup', sliderMouseUp, false);
		}
		else if (document.detachEvent) { // Remove event listeners from 'document' (IE).
			document.detachEvent('onmousemove', moveSlider);
			document.detachEvent('onmouseup', sliderMouseUp);
			document.releaseCapture();
		}
		if (typeof sliderHook == 'function') {
			sliderHook();
		}
	}
	isSliding = false; // Stop the sliding.
}

function snapIntoPos() {
	var v = (txtBox.value) ? txtBox.value : 0 // Find last display value.
	var pos = (v - slider.from)/(slider.scale) // Calculate slider position (regardless of orientation).
	pos = (pos > slider.xMax) ? slider.xMax : pos;
	pos = (pos < 0) ? 0 : pos;
	movePos(slider, pos); // Snap horizontal slider to corresponding display position.
	if (slider.currency) txtBox.value = formatCurrency(txtBox.value);
}


function getElementsByIdPrefx(tag, prfx) {
	var returnElms = new Array();
	var elms = document.getElementsByTagName(tag);
	var el = elms.length;
	for (var i=0, j=0; i<el; i++) {
		if (elms[i].id.substr(0,4) == prfx) {
			returnElms[j] = elms[i];
			j++;
		}
	}
	return returnElms;
}

function manualChange(ctl, sldrid) {
	txtBox = ctl;
	txtBox.value = txtBox.value.replace(/[\$,]/g,"");
	slider = document.getElementById(sldrid);
	slider.xMax = getPos(slider.parentNode) - 17 - getPos(slider.otherSlider);	
	if (slider.direction == 2) {
		var txtBoxL = document.getElementById("txt_L" + txtBox.id.substring(5));
		txtBox.value = (parseFloat(txtBox.value) > parseFloat(txtBoxL.value.replace(/[\$,]/g,"")) ? (parseFloat(txtBox.value) < slider.minOrg) ? txtBox.value : slider.minOrg : txtBoxL.value); 
	}
	else {
		var txtBoxR = document.getElementById("txt_R" + txtBox.id.substring(5));
		txtBox.value = (parseFloat(txtBox.value) < parseFloat(txtBoxR.value.replace(/[\$,]/g,"")) ? (parseFloat(txtBox.value) > slider.minOrg) ? txtBox.value : slider.minOrg : txtBoxR.value); 
	}
	snapIntoPos();
	if (typeof sliderHook == 'function') {
		sliderHook();
	}
}

function loadSliders() {
	sliders = getElementsByIdPrefx("DIV", "sld_") // Find the horizontal sliders.
	for (var i = 0; i < sliders.length; i++) {
		slider = sliders[i];
		txtBox = document.getElementById(slider.getAttribute('display'))
		txtBox.value = txtBox.defaultValue; // Resets display on page reload.
		slider.currency = (parseInt(slider.getAttribute('currency')) == 1);
		slider.distance = parseInt(slider.getAttribute('distance'));
		slider.from = parseFloat(txtBox.getAttribute('from'));
		var to = parseFloat(txtBox.getAttribute('to'));
		slider.scale = (to - slider.from) / slider.distance;		
		snapIntoPos(txtBox,slider);		
		slider.minOrg = slider.from;
		if (slider.from > to) {
			slider.otherSlider = document.getElementById("sld_L" + slider.id.substring(5));
			slider.xMin = 0;
			slider.direction = 2;			
		}
		else {
			slider.otherSlider = document.getElementById("sld_R" + slider.id.substring(5));
			//slider.xMin = slider.from;
			slider.xMin = 0;
			slider.direction = 0;			
		} 
		slider.onmousedown = slide; // Attach event listener.
	}
}
if (window.attachEvent) { // IE
	window.attachEvent("onload", loadSliders);
} else if (window.addEventListener) { // Gecko / W3C
	window.addEventListener("load", loadSliders, false);
} else {
	window["onload"] = loadSliders;
}


