/**
 * formchecksupportscripts.js  v0.1
 * 
 *	NOTE - The following functions are used by the kdsForm object's Validation routines, and will be
 *	included in the appropriate files and locations as needed. Do NOT include this file when ALSO using
 *	the kdsForm object to manage validation
**/
function requireValue(e, msg) {
	if (e.options) {
		// element is a listbox...
		//if (e.selectedIndex < 1) {          // the very first option was selected (or there are no options!)
		//	e.focus();
		//	alert(msg);
		//	return false;
		//}
		if (isEmpty(e.options[e.selectedIndex].value)) {
			e.focus();
			alert(msg);
			return false;
		}
	}
	else {
		// is NOT a ListBox
		if (isEmpty(e.value)) {
			e.focus();
			e.select();
			alert(msg);
			return false;
		}
	}
	return true;
}
function requireGroup(g, msg) {
    // requires that at least one selection in a radio or checkbox group has been selected.
    var blnTemp = false;
    for (i=0; i<g.length; i++){
        if (g[i].checked) blnTemp = true;
    }
    if (!blnTemp) {
        alert(msg);
    }
    return blnTemp;
}
function requireMoney(e, msg) {
	if (!isMoney(e.value)) {
		e.focus();
		e.select();
		alert(msg);
		return false;
	}
	return true;
}



function isEmpty(s) {
    return ((s == '' || s == null) ? true : false);
}
function isEmail(theEmail) {
    return ((theEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) ? true : false);
}
function isValidDate(strDate) {
    // accepts date value as a string in the format MM/DD/YYYY
    return (!isNaN(Date.parse(strDate)));
}
function isMoney(theMoney) {
	var theReg = /^\-?[0-9]*(\.[0-9]{0,2})?$/ ;
	// since a blank value passes the RegExp, we catch this in the following line:
	return (theReg.test(theMoney)?(!isEmpty(theMoney)):false);
}
function isInt(i) {
	//var theReg = /^\d+$/ ;
	// allow for negative ints!!
	var theReg = /^\-?\d{1,10}$/ ;
	return (theReg.test(i));
}
function isFloat(i) {
	var theReg = /^-?\d+(\.\d*)?$/ ;
	return (theReg.test(i));
}


function checkInt(e,strLabel) {
	if (!isEmpty(e.value)) {
		// we only validate the value if it is not empty!
		if (!isInt(e.value)) {
			e.focus();
			e.select();
			alert('Please enter a valid integer (whole number) for \'' + strLabel + '\'.');
			return false;
		}
	}
	// must be good
	return true;
}
function checkFloat(e,strLabel) {
	if (!isEmpty(e.value)) {
		// we only validate the value if it is not empty!
		if (!isFloat(e.value)) {
			e.focus();
			e.select();
			alert('Please enter a valid number for \'' + strLabel + '\'.');
			return false;
		}
	}
	// must be good
	return true;
}
function checkMoney(e, msg) {
	if (!isEmpty(e.value)) {
		// we only validate the value if it is not empty!
		if (!isMoney(e.value)) {
			e.focus();
			e.select();
			alert(msg);
			return false;
		}
	}
	return true;
}
function checkRegPattern(e, strLabel, theReg) {
    if (!theReg.test(e.value)) {
        e.focus();
        e.select();
        alert('The field \'' + strLabel + '\' does not match the expected pattern. Please verify the entry.');
		return false;
    }
	return true;
}



// following three functions mimic VBScript's same-named functions...
function Left(str, ilen) {
	return str.substr(0, ilen);
}
function Right(str, ilen) {
	return str.substr(str.length - ilen);
}
function Mid(str, ipos, ilen) {				// note the different indexing implementation here versus JScript's similar substr function...
	return str.substr(ipos-1, ilen);
}

