﻿// ******************                      ************************ /
// ******************                      ************************ /
// ****************** VALIDATION functions ************************ /
// ******************                      ************************ /
// ******************                      ************************ /

function validateInt(value, msgNotValid) {
	var o = value;
	var isValid = false;

	switch (isInteger(o.value)) {
		case 1:
			isValid = true;
			//alert(o.value + " is an integer")
			break;
		case 0:
			alert(msgNotValid);
			break;
	}
	return isValid;
}


function validateRange(tValue, min, max, msgNotValid) {
	var isValid = false;

	switch (isIntegerInRange(tValue, min, max)) {
		case true:
			isValid = true;
			//alert(s + " is in range from " + A + " to " + B)
			break;
		case false:
			alert(msgNotValid);
			break;
	}
	return isValid;
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
function isIntegerInRange(intValue, min, max) {
	if (isEmpty(intValue))
		if (isIntegerInRange.arguments.length == 1) return false;
	else return (isIntegerInRange.arguments[1] == true);

	// Catch non-integer strings to avoid creating a NaN below,
	// which isn't available on JavaScript 1.0 for Windows.
	if (!isInteger(intValue, false)) return false;

	// Now, explicitly change the type to integer via parseInt
	// so that the comparison code below will work both on
	// JavaScript 1.2 (which typechecks in equality comparisons)
	// and JavaScript 1.1 and before (which doesn't).
	var num = parseInt(intValue);
	var bInRange = ((num >= min) && (num <= max));
	return bInRange;
}

function isInteger(s) {
	var i;

	if (isEmpty(s))
		if (isInteger.arguments.length == 1) return 0;
	else return (isInteger.arguments[1] == true);

	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);

		if (!isDigit(c)) return false;
	}

	return true;
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isDigit(c) {
	return ((c >= "0") && (c <= "9"))
}



function extractNumber(obj, decimalPlaces, allowNegative) {
	var temp = obj.value;

	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}

	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0, reg3Array.index) + '.' + reg3Right;
		}
	}

	obj.value = temp;
}



function blockNonNumbers(obj, e, allowDecimal, allowNegative) {
	var key;
	var isCtrl = false;
	var keychar;
	var reg;

	if (window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if (e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}

	if (isNaN(key)) return true;

	keychar = String.fromCharCode(key);

	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl) {
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;

	return isFirstN || isFirstD || reg.test(keychar);
}



function toggleVisibility(sender, bVis) {
	var myVisible = 'hidden';

	if (bVis == true)
		myVisible = 'visible';

	sender.style.visibility = myVisible;
}
