// General functions
function CheckDecimal(_textbox) {
	pattern = /^[0-9][,0-9]*\.?[0-9]*$/;
	if (_textbox.value == '')
	    _textbox.value = '';
	else if (pattern.test(_textbox.value) == false) {
		alert('The value entered is invalid');
		_textbox.value = '';
		_textbox.focus();
	}
	else if (isNaN(parseFloat(strip(_textbox.value))) && _textbox.value != '') {
		alert('The value entered is invalid');
		_textbox.value = '';
		_textbox.focus();
	}
	else if (parseFloat(strip(_textbox.value)) < 0 && _textbox.value != '') {
		alert('The value entered is invalid');
		_textbox.value = '';
		_textbox.focus();
	}
	else if (_textbox.value != '') {
		var decimal_loc, decNumber, strNumber;
		decNumber = parseFloat(strip(_textbox.value));
		strNumber = decNumber.toString();
		decimal_loc = strNumber.indexOf('.');
		if (decimal_loc == -1)
			strNumber += '.00';
		else if (strNumber.length - decimal_loc - 1 == 1)
			strNumber += '0';
		else
			strNumber = strNumber.slice(0 , decimal_loc + 3);
		_textbox.value = separate(strNumber);
	}
	if (document.getElementById('strAssetsChanged')) {
		if (document.getElementById('strAssetsChanged').value == 'False') {
			alert('Because asset values have changed, sheltering values now will be re-set.  ' +
				'When you are finished with this page, please go See Results ' +
				'before going to Select Processing Docs.');
		}
		document.getElementById('strAssetsChanged').value = 'True';
	}
}

function separate(input) { // format input using 'separator' to mark 000's
	var separator = ',';
	input = '' + input;
	var output = ''; // initialize output string
	for (var i=0; i < input.length; i++) {
		if (i != 0 && (input.length - i) % 3 == 0 
		&& (input.length - i) != 3) 
			output += separator;
		output += input.charAt(i);
	}
	return output;
}

function strip(input) { // strip all characters not in 'chars' from input
	var chars = '.0123456789'; // allow only chars which will create a valid number for parseFloat()
	var stripchars = '-,'; // these are the only valid chars to strip
	var output = ''; // initialize output string
	var thischar;
	for (var i=0; i < input.length; i++) {
		thischar = input.charAt(i);
		if (chars.indexOf(thischar) == -1) {
			if (stripchars.indexOf(thischar) == -1) {
				// input contains an invalid char
				return 'NaN';
			}
		}
		else
			output += input.charAt(i);
    }
	return output;
}

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);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if (cents < 10)
		cents = "0" + cents;
	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 + '.' + cents);
}

function currencyToDecimal(_numString) {
	var _returnValue="";
	var _array=_numString.split(""); /* make each character be in its own array slot */
	for (var i=0; i< _array.length;i++) {
		if (_array[i].search(/[$ , ]/) == -1) {
			_returnValue += _array[i].toString();
		}
	}
	return(parseFloat(_returnValue));
}

function CheckInteger(_textbox) {
	pattern = /^[1-9][0-9]*$/;
	if (_textbox.value == '')
		_textbox.value = '';
	else if (pattern.test(_textbox.value) == false) {
		alert('The value entered is invalid');
		_textbox.value = '';
		_textbox.focus();
	}
	else if (isNaN(parseInt(_textbox.value)) && _textbox.value != '') {
		alert('The value entered is invalid');
		_textbox.value = '';
		_textbox.focus();
	}
	else if (parseInt(_textbox.value) < 0 && _textbox.value != '') {
		alert('The value entered is invalid');
		_textbox.value = '';
		_textbox.focus();
	}
	else if (_textbox.value.indexOf(',') != -1) {
		alert('Please do not use commas');
		_textbox.value = '';
		_textbox.focus();
	}
	else if (_textbox.value != '') {
		var strNumber;
		strNumber = parseInt(_textbox.value);
		_textbox.value = strNumber;
	}
}

function doValidate(id) {
	ValidatorOnChange(id);
}

function onlyAscii() {
	if (document.layers) {
	}
	else {
		if (event.keyCode > 127) {
			//non-ASCII character
			alert('Only ASCII characters are permitted');
			event.returnValue = false;
		}
	}
}

function checkKey (textControl, evt) {
	var keyCode = evt.keyCode ? evt.keyCode :
                evt.charCode ? evt.charCode :
				evt.which ? evt.which : void 0;
	var key;
	if (keyCode) {
		key = String.fromCharCode(keyCode);
	}
	if (keyCode && window.event && !window.opera) {
		if (keyCode > 127) {
			alert('Only ASCII characters are permitted');
			return false;
		}
		else {
			return true;
		}
	}
	else if (typeof textControl.setSelectionRange != 'undefined') {
		if (keyCode > 127) {
			if (evt.preventDefault) {
				evt.preventDefault();
			}
			alert('Only ASCII characters are permitted');
			return false;
		}
		else {
			return true;
		} 
	}
	else if (keyCode > 127) {
		if (evt.preventDefault) {
			evt.preventDefault();
		}
		alert('Only ASCII characters are permitted');
		return false;
	}
	else {
		return true;
	}
}

function ctrlHighlight(ctrl) {
	ctrl.style.backgroundColor = "#bcf0bc";  
}

function ctrlNormal(ctrl) {
	ctrl.style.backgroundColor = "#ffffff";
}

function yeardiff(date1, _dest) {
	var dDate1;
	var today = new Date();
	var years;
	dDate1 = Date.parse(date1); 
	years = parseInt(((today - dDate1)/(365.25*24*60*60*1000)).toString());
	_dest.value = years.toString();
}

function Do_Update() {
	Anthem_InvokePageMethod('ActionUpdate');
}

function isDateInFuture(dateTest) {
	var today = new Date();
	if(dateTest <= today) {
		alert('Date must be after today');
		return false;
	}
	return true;
}

function getWindowHeight() { 
    var windowHeight = 0; 
    if (typeof(window.innerHeight) == 'number') { 
        windowHeight = window.innerHeight; 
    } else { 
        if (document.documentElement && document.documentElement.clientHeight) { 
            windowHeight = document.documentElement.clientHeight; 
        } else { 
            if (document.body && document.body.clientHeight) { 
                windowHeight = document.body.clientHeight; 
            } 
        }
    } 
    return windowHeight; 
}

function setBackgroundHeight() {
    var hgt;
    hgt = getWindowHeight();
    document.getElementById('Bgd').style.height = (hgt + 5) + 'px';
    document.getElementById('MsgTop').style.height = ((hgt / 2) - 43) + 'px';
    document.getElementById('MsgBox').style.height = 87 + 'px';
}

function setBackgroundHeightLarge() {
    var hgt;
    hgt = getWindowHeight();
    document.getElementById('Bgd').style.height = (hgt + 5) + 'px';
    document.getElementById('MsgTop').style.height = ((hgt / 2) - 58) + 'px';
    document.getElementById('MsgBox').style.height = 116 + 'px';
}
