// JScript source code

//Common expressions

//Very Common
var regNonNull = /[A-Za-z_0-9]+/;
var regNonNullNumeric = /^[0-9]+$/;
var regMDYYYY = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{1,4}$/;
var regBoolean = new RegExp("^True|False$","i");

//Data Types:
var regInt16Null = new RegExp("^-?((3[0-1][0-9]{3})$|^(32[0-6][0-9]{2})$|^(327[0-5][0-9])$|^(3276[0-7])$|^([0-2]?[0-9]{1,4}))$|^32768$|^$");
var regInt16 = new RegExp("^-?((3[0-1][0-9]{3})$|^(32[0-6][0-9]{2})$|^(327[0-5][0-9])$|^(3276[0-7])$|^([0-2]?[0-9]{1,4}))$|^32768$");
var regInt32Null = new RegExp("^-?((20[0-9]{8})$|^-?(21[0-3][0-9]{7})$|^-?(214[0-6][0-9]{6})$|^-?(2147[0-3][0-9]{5})$|^-?(21474[0-7][0-9]{4})$|^-?(214748[0-2][0-9]{3})$|^-?(2147483[0-5][0-9]{2})$|^-?(21474836[0-3][0-9])$|^-?(214748364[0-7])$|^-?([0-1]?[0-9]{1,9}))$|^-2147483648$|^$");
var regInt32 = new RegExp("^-?((20[0-9]{8})$|^-?(21[0-3][0-9]{7})$|^-?(214[0-6][0-9]{6})$|^-?(2147[0-3][0-9]{5})$|^-?(21474[0-7][0-9]{4})$|^-?(214748[0-2][0-9]{3})$|^-?(2147483[0-5][0-9]{2})$|^-?(21474836[0-3][0-9])$|^-?(214748364[0-7])$|^-?([0-1]?[0-9]{1,9}))$|^-2147483648$");
var regInt64Null = new RegExp("^[0-9]{1,18}$|^$");
var regInt64 = new RegExp("^[0-9]{1,18}$");
var regDecimalNull = new RegExp("^-?[0-9]*[.]*[0-9]{0,27}$");
var regDecimal = new RegExp("^-?[0-9]+[.]*[0-9]{0,27}$");
var regDoubleNull = new RegExp("^-?[0-9]+[.]?[0-9]{0,308}$|^$");
var regDouble = new RegExp("^-?[0-9]+[.]?[0-9]{0,308}$");
var regSingleNull = new RegExp("^-?[0-9]+[.]?[0-9]{0,38}$|^$");
var regSingle = new RegExp("^-?[0-9]+[.]?[0-9]{0,38}$");
var regDateTime = new RegExp("^([1-9]|10|11|12)/([1-9]|[1-2][0-9]|3[0-1])/([0-9]{4}) ([1-9]|10|11|12):([0-5][0-9]):([0-5][0-9]) ([AaPp][Mm])$");
var regDateTimeNull = new RegExp("^([1-9]|10|11|12)/([1-9]|[1-2][0-9]|3[0-1])/([0-9]{4}) ([1-9]|10|11|12):([0-5][0-9]):([0-5][0-9]) ([AaPp][Mm])$|^$");

//Specialty Strings:
var regFileDocument = new RegExp("^[a-z]:\\[a-z_0-9\.\\\- ]+\.(doc|txt|xls|jpg|gif|jpeg|tif|tiff|pdf)$","i");
var regEMail = new RegExp("^[a-z_0-9]+@[a-z_0-9]+\.[a-z_0-9]+$","i");

function getExpression(intNegative, intPositive) {
	strExpression = "";

	intCommon = Math.abs(intNegative);
	if (intCommon > intPositive) {
		intCommon = intPositive;
	}

	strCommon = new String(intCommon);

	strExpression += "^";
	strExpression += "(-?";

	for (var i=0;i<strCommon.length;i++) {
		if (i == 0) {
			strExpression += strCommon.substring(i,i+1);
		} else {
			strExpression += "[0-" + strCommon.substring(i,i+1) + "]";
		}
	}
	
	strExpression += ")|(-?";
	
	for (var i=0;i<2;i++) {
		if ((i==0) && (strCommon.substring(i,i+1) != "1")) {
			strExpression += "[1-" + (strCommon.substring(i,i+1) - 1) + "]?";
		} else {
			strExpression += "[0-9]{0," + (strCommon.length - 1) + "}";
		}
	}
	
	strExpression += ")";
	
	if (intCommon < intPositive) {
		strExpression += "|(" + intPositive + ")";
	} else if (intCommon < Math.abs(intNegative)) {
		strExpression += "|(" + intNegative + ")";
	}
	
	strExpression += "$";
	
	return strExpression;
}

function validateField(fldField, regPattern, strErrorText) {
	if (fldField == null) {
		return false;
	}

	if (fldField.disabled == true) {
		return true;
	}

	blnPassed = true;
	
	switch(fldField.type) {
		case "text":
		case "password":
		case "file":
			strValue = fldField.value;
			break;
		case "textarea":
		case "hidden":
			strValue = fldField.value;
			while ((strValue.indexOf('\n') > -1) || (strValue.indexOf('\r') > -1)) {
				strValue = strValue.replace('\n',' ');
				strValue = strValue.replace('\r',' ');
			}
			break;
		case "select-one":
		case "select-multiple":
			if (fldField.selectedIndex != -1) {
				strValue = fldField.options[fldField.selectedIndex].value;
			} else {
				strValue = "";
			}
			break;
		default:
			if (fldField.length > 0) {
				switch(fldField[0].type) {
					case "radio":
						strValue = getRadioValue(fldField);
						break;
					case "checkbox":
						strValue = getCheckBoxValue(fldField);
						break;
				}
			}
	}

	if (regPattern != null) {
		if (regPattern.test(strValue) != true) {
			blnPassed = false;
		}
	}

	if (blnPassed == false) {
		if ((navigator.appName == "Microsoft Internet Explorer") && ((fldField.type == "select-one") || (fldField.type =="select-multiple"))) {	//Another IE glitch
			return false;
		}
	
		if (fldField.type != null) {
			if (fldField.style.display == "none") {
				fldField.style.display = "";
				popUp(fldField, "<div style=\"padding: 2px 3px 2px 3px; border: 1px solid gray;\">" + strErrorText + "</div>", false, true, "bottom", "center-left", true);
				fldField.focus();
				fldField.style.display = "none";
			} else {
				popUp(fldField, "<div style=\"padding: 2px 3px 2px 3px; border: 1px solid gray;\">" + strErrorText + "</div>", false, true, "bottom", "center-left", true);
				try {
					fldField.focus();
				} catch(e) { }
			}
		} else {
			if (fldField.length > 0) {
				if (fldField[0].style.display == "none") {
					fldField[0].style.display = "";
					popUp(fldField[0], "<div style=\"padding: 2px 3px 2px 3px; border: 1px solid gray;\">" + strErrorText + "</div>", false, true, "bottom", "center-left", true);
					fldField[0].focus();
					fldField[0].style.display = "none";
				} else {
					popUp(fldField[0], "<div style=\"padding: 2px 3px 2px 3px; border: 1px solid gray;\">" + strErrorText + "</div>", false, true, "bottom", "center-left", true);
					fldField[0].focus();
				}
			}
		}
		return false;
	} else {
		return true;
	}
}

function popUp(objThis, strHTML, blnFollowMouse, blnDisappear, strVerticalAlign, strHorizontalAlign, blnShadow) {
	var divPopUp = document.createElement("DIV");
	var bdyMain = document.getElementsByTagName("BODY")[0];
	
	if (objThis.id == '') {
		objThis.id = objThis.name;
	}
	
	var strThisId = objThis.id;
	
	divPopUp.id = strThisId + "pop";
	divPopUp.style.position = "absolute";
	//divPopUp.className = "Popup";
	divPopUp.innerHTML = strHTML;
	divPopUp.style.zIndex = 10;

	bdyMain.appendChild(divPopUp);

	var blnParentUsed = false;

	if (getElementTop(objThis) == 0) {
		blnParentUsed = true;
		objThis = getParentElement(objThis, "TD");
		strVerticalAlign = "middle";
	}

    var intLeft = 0;
    var intTop = 0;

	switch(strHorizontalAlign) {
		case "left":
			intLeft = getElementLeft(objThis) - (divPopUp.offsetWidth + 4);
			break;
		case "center-left":
		case "middle-left":
			intLeft = getElementLeft(objThis);
			break;
		case "center":
		case "middle":
			intLeft = getElementLeft(objThis) - (divPopUp.offsetWidth / 2);
			break;
		case "center-right":
		case "middle-right":
			intLeft = getElementLeft(objThis) + objThis.offsetWidth - divPopUp.offsetWidth;
			break;
		case "right":
		    intLeft = getElementLeft(objThis) + objThis.offsetWidth + 4
			break;
	}
	
	switch(strVerticalAlign) {
		case "top":
			intTop = getElementTop(objThis) - divPopUp.offsetHeight - 5;
			break;
		case "center":
		case "middle":
			intTop = getElementTop(objThis) + (objThis.offsetHeight / 2) - (divPopUp.offsetHeight / 2);
			break;
		case "bottom":
			intTop = getElementTop(objThis) + objThis.offsetHeight + 1;
			break;
	}

    divPopUp.style.left = parseInt(intLeft) + 'px';
    divPopUp.style.top = parseInt(intTop) + 'px';
    
	//Border patrol
	divPopUp.style.left = parseFloat(divPopUp.style.left) < 0 ? 0 : divPopUp.style.left;
	divPopUp.style.left = parseFloat(divPopUp.style.left) + parseFloat(divPopUp.offsetWidth) > parseFloat(document.body.offsetWidth) ? parseFloat(document.body.offsetWidth) - parseFloat(divPopUp.offsetWidth) : divPopUp.style.left;

	divPopUp.style.top = parseFloat(divPopUp.style.top) < 0 ? 0 : divPopUp.style.top;
	divPopUp.style.top = parseFloat(divPopUp.style.top) + parseFloat(divPopUp.offsetHeight) > parseFloat(document.body.offsetHeight) ? parseFloat(document.body.offsetHeight) - parseFloat(divPopUp.offsetHeight) : divPopUp.style.top;

	if (blnDisappear == true) {
		divPopUp.onmouseout = function() { setTimeout("killPopUp(document.getElementById(\"" + strThisId + "\"));", 1000); };
		divPopUp.onclick = function() { killPopUp(document.getElementById(strThisId)); };
	}

	//Make the shadow
	if (blnShadow == true) {
		if (navigator.appName != "Microsoft Internet Explorer") {
			makeShadowForDiv(divPopUp, 3);    //Temporary disabled until Internet Explorer stops acting gay.
		}
	}
}

function killPopUp(objThis) {
	if (objThis != null) {
		if (document.getElementById(objThis.getAttribute("ID") + "pop") != null) {
			document.getElementsByTagName("BODY")[0].removeChild(document.getElementById(objThis.getAttribute("ID") + "pop"));
		}
	}
}

function checkForPopUp(objThis) {
	if (document.getElementById(objThis.id + "pop") != null) {
		return true;
	} else {
		return false;
	}
}

function makeShadowForDiv(divObject, intOffset) {
	var divShadow = document.createElement("DIV");

	divShadow.className = "Shadow";
	divShadow.style.position = "absolute";

	divShadow.id = divObject.id + "shadow";

	if (divObject.style.zIndex == null) {
		divObject.style.zIndex = 10;
	}
	
	divShadow.style.zIndex = -10;
	
	divShadow.style.height = divObject.clientHeight + (divObject.style.borderTopWidth != '' ? parseInt(divObject.style.borderTopWidth) : 0) + (divObject.style.borderBottomWidth != '' ? parseInt(divObject.style.borderBottomWidth) : 0);
	divShadow.style.width = divObject.clientWidth + (divObject.style.borderLeftWidth != '' ? parseInt(divObject.style.borderLeftWidth) : 0) + (divObject.style.borderRightWidth != '' ? parseInt(divObject.style.borderRightWidth) : 0);

	divShadow.style.left = intOffset - (divObject.style.borderLeftWidth != '' ? parseInt(divObject.style.borderLeftWidth) : 0);
	
	divShadow.style.top = intOffset - (divObject.style.borderTopWidth != '' ? parseInt(divObject.style.borderTopWidth) : 0);

	var bdyMain = document.getElementsByTagName("BODY")[0];
	divObject.appendChild(divShadow);
}

function getElementLeft(eElement) {
	if (!eElement && this) {
		eElement = this;
	}
	
	var nLeftPos = eElement.offsetLeft;
	var eParElement = eElement.offsetParent;
	while (eParElement != null) {
		nLeftPos += eParElement.offsetLeft;
		eParElement = eParElement.offsetParent;
	}
	
	return nLeftPos;
}

function getElementTop(eElement) {
	if (!eElement && this) {
		eElement = this;
	}

	var nTopPos = eElement.offsetTop;
	var eParElement = eElement.offsetParent;
	while (eParElement != null) {
		nTopPos += eParElement.offsetTop;
		eParElement = eParElement.offsetParent;
	}
	return nTopPos;
}

function getCheckBoxValue(checkBox) {
	var strValues = '';
	for (var i=0;i<checkBox.length;i++) {
		if (checkBox[i].checked == true) {
			if (strValues.length > 0) {
				strValues += ",";
			}
			strValues += checkBox[i].value;
		}
	}
	return strValues;
}

function getRadioValue(radioButton) {
	for (x=0;x<radioButton.length;x++) {
		if (radioButton[x].checked == true) {
			return radioButton[x].value;
		}
	}
	return "";
}
