//	This software is the unpublished, confidential, proprietary, intellectual
//	property of Kim David Software and may not be copied, duplicated, retransmitted
//	or used in any manner without expressed written consent from Kim David Software.
//	Kim David Software owns all rights to this work and intends to keep this
//	software confidential so as to maintain its value as a trade secret.

//	Copyright 2004-2006, Kim David Software.

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

// formatPhone
// Takes a phone field and validates that the value of the field is a phone number.
// First, it strips out all but numbers. The value must have at least 10 digits. Any
// digits after the first 10 are the extension. Removes 1 if it is the first digit.
// If it is still a valid phone number (10 or more digits), it is formatted as
// (999) 999-9999 x9999
//
function formatPhone(phoneField,countryField,showAlert) {
	if (showAlert == null) {
		showAlert = true;
	}
	if (countryField == null) {
		countryId = "1000";
	} else {
		countryId = countryField.options[countryField.selectedIndex].value;
	}
	if (countryId != "1000") {
		return true;
	}
 	num = "";
	if (phoneField.value.length == 0) {
		return true;
	}
	for (var x=0;x<phoneField.value.length;x++) {
		thisChar = phoneField.value.charAt(x);
		if (thisChar >= "0" && thisChar <= "9" && (x>0 || thisChar != "1")) {
			num = num + phoneField.value.charAt(x);
		}
	}
	if (num.length < 10) {
		if (showAlert) {
			alert('Invalid phone number. Enter 10 digits (plus optional extention)');
		}
		setTimeout("document." + phoneField.form.name + "." + phoneField.name + ".focus()",100);
		return false;
	} else {
		tempPhone=num.replace(/(\d{3})(\d{3})(\d{4})/,'('+'$1'+') '+'$2'+'-'+'$3'+' x');
		if (tempPhone.charAt((tempPhone.length - 1)) == "x") {
			tempPhone = tempPhone.substr(0,tempPhone.length - 2);
		}
		phoneField.value = tempPhone;
	}
	return true;
}

// formatDate
// Takes a date field and validates that it is a valid date. After entry, the date
// is formatted as according to the dateFormat. The default format is mm/dd/yy
//
function formatDate(dateField,dateFormat,maximumValue,showAlert) {
	var todayDate = new Date();
	if (showAlert == null) {
		showAlert = true;
	}
	dtStr = dateField.value;
	if (dtStr.length == 0) {
		return true;
	}
	
	var daysInMonth = DaysArray(12);
	if (dtStr.length == 3 && !isNaN(dtStr)) {
		dtStr = "0" + dtStr;
	}
	if (dtStr.length == 4) {
		if (!isNaN(dtStr)) {
			dtStr = dtStr + todayDate.getFullYear();
		} else {
			dtStr = "0" + dtStr;
		}
	}
	if (dtStr.length == 5) {
		if (!isNaN(dtStr)) {
			dtStr = "0" + dtStr;
		} else {
			dtStr = dtStr + "/" + todayDate.getFullYear();
		}
	}
	if (dtStr.length == 6 && !isNaN(dtStr)) {
		dtStr = dtStr.substring(0,2) + "/" + dtStr.substring(2,4) + "/" + dtStr.substring(4,6);
	}
	if (dtStr.length == 8 && !isNaN(dtStr)) {
		dtStr = dtStr.substring(0,2) + "/" + dtStr.substring(2,4) + "/" + dtStr.substring(4,8);
	}
	dtStr = dtStr.replace(/-/g, "/");
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	if (strYear.length==2) {
		if (strYear > 20) {
			strYear = "19" + strYear;
		} else {
			strYear = "20" + strYear;
		}
	}
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		if (showAlert) {
			alert("The date format should be: 'm/d/y'");
		}
		setTimeout("document." + dateField.form.name + "." + dateField.name + ".focus()",100);
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		if (showAlert) {
			alert("Please enter a valid month");
		}
		setTimeout("document." + dateField.form.name + "." + dateField.name + ".focus()",100);
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (showAlert) {
			alert("Please enter a valid day");
		}
		setTimeout("document." + dateField.form.name + "." + dateField.name + ".focus()",100);
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		if (showAlert) {
			alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		}
		setTimeout("document." + dateField.form.name + "." + dateField.name + ".focus()",100);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		if (showAlert) {
			alert("Please enter a valid date");
		}
		setTimeout("document." + dateField.form.name + "." + dateField.name + ".focus()",100);
		return false;
	}
	checkDate = year + "-";
	if (month < 10) {
		checkDate = checkDate + "0";
	}
	checkDate = checkDate + month + "-";
	if (day < 10) {
		checkDate = checkDate + "0";
	}
	checkDate = checkDate + day;
	
	if (maximumValue != null && maximumValue.length > 0 && checkDate > maximumValue) {
		if (showAlert) {
			alert("Invalid date");
		}
		setTimeout("document." + dateField.form.name + "." + dateField.name + ".focus()",100);
		return false;
	}
	
	newValue = day + "/";
	if (newValue.length < 3) {
		newValue = "0" + newValue;
	}
	newValue = month + "/" + newValue;
	if (newValue.length < 6 && dateFormat.indexOf("m") != -1) {
		newValue = "0" + newValue;
	}
	if (dateFormat.indexOf("y") != -1) {
		newValue = newValue + (year + "").substring(2,4);
	} else {
		newValue = newValue + year;
	}
	dateField.value = newValue;
	return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
    var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function stripCharsNotInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
    var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30};
		if (i==2) {this[i] = 29};
   }
   return this;
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

// formatInteger
// Takes a number field and a money flag and validates that it is a valid number. Strips out all but digits
// and formats the number with commas and a dollar sign, if the money flag is true.
//
function formatInteger(numberField,moneyFlag,minimumValue,maximumValue,noCommas,showAlert) {
	if (showAlert == null) {
		showAlert = true;
	}
	strString = numberField.value;
	var strValidChars = "-$0123456789,.";
	var strChar;

	if (strString.length == 0) {
		return true;
	}

	newString = "";
	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			if (showAlert) {
				alert("Invalid number. Only chars '0123456789,' are allowed");
			}
			setTimeout("document." + numberField.form.name + "." + numberField.name + ".focus()",100);
			return false;
		}
		if (strChar == ".") {
			break;
		}
		if (strChar == "-" && newString.length == 0) {
			newString += "-";
		}
		if (strChar >= "0" && strChar <= "9") {
			newString += strChar;
		}
	}
	if (newString == "-") {
		newString = "0";
	}
	newString = newString - 0;
	if (minimumValue != null && newString < minimumValue) {
		newString = minimumValue;
	}
	if (maximumValue != null && newString > maximumValue) {
		newString = maximumValue;
	}
	if (noCommas == null || !noCommas) {
		newString = addCommas(parseInt(newString) + "");
	}
	numberField.value = (moneyFlag ? "$" : "") + newString;
	return true;
}

// formatFloat
// Takes a number field and a money flag and validates that it is a valid number. Strips out all but digits
// and formats the number with commas and a dollar sign, if the money flag is true.
//
function formatFloat(numberField,moneyFlag,decimals,minimumValue,maximumValue,noCommas,showAlert) {
	if (showAlert == null) {
		showAlert = true;
	}
	strString = numberField.value;
	var strValidChars = "-$0123456789,.";
	var strChar;

	if (strString.length == 0) {
		return true;
	}

	newString = "";
	if (decimals == null) {
		decimals = 0;
	}
	foundDecimal = false;
	totalDecimals = 0;

	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			if (showAlert) {
				alert("Invalid number. Only chars '0123456789,.' are allowed");
			}
			setTimeout("document." + numberField.form.name + "." + numberField.name + ".focus()",100);
			return false;
		}
		if (strChar == "-" && newString.length == 0) {
			newString += "-";
		}
		if ((strChar >= "0" && strChar <= "9") || (strChar == "." && !foundDecimal)) {
			if (strChar == ".") {
				foundDecimal = true;
				newString += strChar;
			} else if ((foundDecimal && totalDecimals < decimals) || !foundDecimal) {
				if (foundDecimal) {
					totalDecimals++;
				}
				newString += strChar;
			}
		}
	}
	if (newString == "-") {
		newString = "0";
	}
	newString = newString - 0;
	if (minimumValue != null && newString < minimumValue) {
		newString = minimumValue;
	}
	if (maximumValue != null && newString > maximumValue) {
		newString = maximumValue;
	}
	if (noCommas == null || !noCommas) {
		newString = (moneyFlag ? "$" : "") + addCommas(newString);
	} else {
		newString = (moneyFlag ? "$" : "") + newString;
	}
	if (decimals > 0) {
		if (newString.indexOf(".") == -1) {
			newString += ".";
		}
		var maxDecimals = 0;
		while (newString.substring(newString.length - decimals - 1,newString.length - decimals) != "." && maxDecimals < decimals) {
			newString += "0";
			maxDecimals++;
		}
	}
	numberField.value = newString;
	return true;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// checkEmail
// validates that the email field has a valid email address in it.
//
function checkEmail(emailField,showAlert) {
	if (showAlert == null) {
		showAlert = true;
	}
	var str = emailField.value;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (lstr == 0) {
		return true;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr ||
		str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr ||
		str.indexOf(at,(lat+1))!=-1 || str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot ||
		str.indexOf(dot,(lat+2))==-1 || str.indexOf(" ")!=-1) {
		if (showAlert) {
			alert("Invalid E-mail Address");
		}
		setTimeout("document." + emailField.form.name + "." + emailField.name + ".focus()",150);
		return false;
	}
	return true;
}

// checkPassword
// Take the first and second password field and validate that they are the same and that they contain
// the right number and type of characters.
//
function checkPassword(passwordField,password2Field,showAlert) {
	if (showAlert == null) {
		showAlert = true;
	}
	if (passwordField.value == "" && password2Field.value == "") {
		return true;
	}
	if (password2Field != null) {
		if (passwordField.value.length > 0 && password2Field.value.length > 0 && passwordField.value != password2Field.value) {
			if (showAlert) {
				alert("Passwords are not the same...try again");
			}
			passwordField.value = "";
			password2Field.value = "";
			setTimeout("document." + passwordField.form.name + "." + passwordField.name + ".focus()",100);
			return false;
		}
	}
	if (passwordField.value.length > 0 && passwordField.value.length < 6) {
		if (showAlert) {
			alert("Password must be 6 or more characters and contain at least 1 letter & 1 number");
		}
		passwordField.value = "";
		if (password2Field != null) {
			password2Field.value = "";
		}
		setTimeout("document." + passwordField.form.name + "." + passwordField.name + ".focus()",100);
		return false;
	}
	password = passwordField.value.toLowerCase();
	if (password.search(/[a-z]/) == -1 || password.search(/[0-9]/) == -1) {
		if (showAlert) {
			alert("Password must be 6 or more characters and contain at least 1 letter & 1 number");
		}
		passwordField.value = "";
		if (password2Field != null) {
			password2Field.value = "";
		}
		setTimeout("document." + passwordField.form.name + "." + passwordField.name + ".focus()",100);
		return false;
	}
	return true;
}

// showInfo
// take some text and display it in a small window
//
infoWindow=null;
function showInfo(infoCode,tableName) {
	var windowWidth=700;
	var windowHeight=400;
	if (infoWindow != null && !infoWindow.closed)
		infoWindow.close();
	windowOptions = 'scrollbars=yes,width=' + windowWidth + ',height=' + windowHeight + ',screenX=' + Math.floor(screen.width - windowWidth) / 2 + ',left=' + Math.floor(screen.width - windowWidth) / 2 + ',screenY=' + Math.floor(screen.height - windowHeight) / 2 + 'top=' + Math.floor(screen.height - windowHeight) / 2;
	infoWindow = window.open("","",windowOptions);
  	infoWindow.document.location = "shared/moreinfo.php?code=" + infoCode + "&table=" + tableName;
}

// showImage
// display an image in a small window
//
imageWindow=null;
function showImage(tableName,fieldName,keyName,keyValue) {
	if (imageWindow != null && !imageWindow.closed)
		imageWindow.close();
	windowOptions = 'scrollbars=yes,width=525,height=400,screenX=' + Math.floor(screen.width - 525) / 2 + ',left=' + Math.floor(screen.width - 525) / 2 + ',screenY=' + Math.floor(screen.height - 400) / 2 + 'top=' + Math.floor(screen.height - 400) / 2;
	imageWindow = window.open("","",windowOptions);
	imageWindow.document.write("<body leftmargin='10 topmargin='10' marginwidth='10' marginheight='10'>");
	imageWindow.document.write("<table width=100% height=100%><tr><td height=100% align=center><img src='shared/getimage.php?table=" + tableName + "&field=" + fieldName + "&key=" + keyName + "&value=" + keyValue + "'></td></tr></table>");
	imageWindow.document.write("</body>");
}

function checkKey(keyField,setUppercase) {
	newValue = "";
	if (setUppercase == null) {
		setUppercase = true;
	}
	for(var x=0;x<keyField.value.length;x++) {
		if (setUppercase) {
			thisChar = keyField.value.charAt(x).toUpperCase();
		} else {
			thisChar = keyField.value.charAt(x).toLowerCase();
		}
		if (thisChar.search(/[A-Z]/) >= 0 || thisChar.search(/[a-z]/) >= 0 || thisChar.search(/[0-9]/) >= 0 || "_-".indexOf(thisChar) >= 0) {
			newValue = newValue + thisChar;
		}
	}
	keyField.value = newValue;
}

// validateCreditCardNumber
// takes the credit card type field, the number field, and a flag indicating whether to set
// the type or not. Credit card number is validated according to industry standards.
//
function validateCreditCardNumber(typeField,numberField,setType,showAlert)
{
	if (showAlert == null) {
		showAlert = true;
	}
	var ccNum = numberField.value;
	if (numberField.value.length > 0 && numberField.value.charAt(0) != "*") {
		var newValue = "";
		for(var x=0;x<numberField.value.length;x++) {
			thisChar = numberField.value.charAt(x).toUpperCase();
			if (thisChar.search(/[0-9]/) >= 0) {
				newValue = newValue + thisChar;
			}
		}
		numberField.value = newValue;
		ccNum = newValue;
		if (!isInteger(ccNum)) {
			if (showAlert) {
				alert('Please enter only numbers (no dashes or spaces) for the Credit Card number');
			}
			setTimeout("document." + numberField.form.name + "." + numberField.name + ".focus()",150);
			return false;
		}

		if (!LuhnCheck(ccNum) || !validateCreditCardType(typeField,numberField,setType)) {
			if (showAlert) {
				alert('Invalid credit card number');
			}
			setTimeout("document." + numberField.form.name + "." + numberField.name + ".focus()",100);
			return false;
		}
	}
	return true;
}

// validateCreditCardType
// validate the credit card number after setting the type
//
function validateCreditCardType(typeField,numberField,setType) {
	var cardType = typeField.options[typeField.selectedIndex].value;
	var numType = "";

	var ccNum = numberField.value;
	var cardLen = ccNum.length;
	var firstdig = ccNum.substring(0,1);
	var seconddig = ccNum.substring(1,2);
	var first4digs = ccNum.substring(0,4);

	if (((cardLen == 16) || (cardLen == 13)) && (firstdig == "4")) {
		numType = "VISA";
	} else if ((cardLen == 15) && (firstdig == "3") && ("47".indexOf(seconddig)>=0)) {
		numType = "AMEX";
	} else if ((cardLen == 16) && (firstdig == "5") && ("12345".indexOf(seconddig)>=0)) {
		numType = "MC";
	} else if ((cardLen == 16) && (first4digs == "6011")) {
		numType = "DISC";
	} else if ((cardLen == 14) && (firstdig == "3") && ("068".indexOf(seconddig)>=0)) {
		numType = "DINERS";
	}

	if (numType == "") {
		return false;
	}

	if (setType) {
		typeWasSet = false;
		for (var i=0;i<typeField.options.length;i++) {
			if (typeField.options[i].value == numType) {
				typeWasSet = true;
				typeField.selectedIndex = i;
				break;
			}
		}
		return typeWasSet;
	} else {
		if (numType != cardType && cardType != "") {
			return false;
		}
	}
	return true;
}

function LuhnCheck(str) {
	var result = true;

	var sum = 0;
	var mul = 1;
	var strLen = str.length;

	for (i = 0; i < strLen; i++) {
		var digit = str.substring(strLen-i-1,strLen-i);
		var tproduct = parseInt(digit ,10)*mul;
		if (tproduct >= 10)
			sum += (tproduct % 10) + 1;
		else
			sum += tproduct;
		if (mul == 1)
			mul++;
		else
			mul--;
	}
	if ((sum % 10) != 0)
		result = false;

	return result;
}

function checkUserName(userNameField) {
	var newUserName = "";
	var foundBadChar = false;
	for (var x=0;x<userNameField.value.length;x++) {
		var checkChar = userNameField.value.charAt(x);
		if (newUserName.length == 0 && "&-_".indexOf(checkChar) != -1) {
			foundBadChar = true;
			continue;
		}
		if ("1234567890abcdefghijklmnopqrstuvwxyz&-_".indexOf(checkChar.toLowerCase()) != -1) {
			newUserName += checkChar;
		}
		newUserName = newUserName.toLowerCase();
	}
	userNameField.value = newUserName;
	if (foundBadChar) {
		alert("Only letters, numbers, ampersand, dash & underscore are allowed");
	}
}

function addSelect(selectField,chosenField) {
	selectArray=new Array();
	nextOption = 0;
	for (x=chosenField.options.length;x>0;x--) {
		selectArray[selectArray.length] = chosenField.options[x-1].value;
		chosenField.options[x-1]=null;
	}
	for (x=0;x<selectField.options.length;x++) {
		useIt = selectField.options[x].selected;
		selectField.options[x].selected = false;
		if (!useIt) {
			for (y=0;y<selectArray.length;y++) {
				if (selectArray[y] == selectField.options[x].value) {
					useIt = true;
				}
			}
		}
		if (useIt) {
			var option1 = new Option(selectField.options[x].text,selectField.options[x].value);
			chosenField.options[nextOption++]=option1;
		}
	}
}

function removeSelect(chosenField,removeButton) {
	for (x=chosenField.options.length;x>0;x--) {
		if (chosenField.options[x-1].selected) {
			chosenField.options[x-1]=null;
		}
	}
	removeButton.disabled = true;
}

function checkSelect(chosenField,removeButton) {
	foundOne = false;
	for (x=chosenField.options.length;x>0;x--) {
		if (chosenField.options[x-1].selected) {
			foundOne = true;
			break;
		}
	}
	removeButton.disabled = !foundOne;
}

presentationWindow=null

function setToday(dateField,shortYear) {
	var today = new Date();
	day = today.getDate();
	month = today.getMonth() + 1;
	year = today.getFullYear();
	if (shortYear) {
		year = (year + "").substring(2,4);
	}
	newValue = day + "/";
	if (newValue.length < 3) {
		newValue = "0" + newValue;
	}
	newValue = month + "/" + newValue
	if (newValue.length < 5) {
		newValue = "0" + newValue;
	}
	if (newValue.length < 8) {
		newValue = "0" + newValue;
	}
	dateField.value = newValue + year;
}

function addDays(dateField,numberDays) {
	var oneDay = 3600 * 1000 * 24
	dtStr = dateField.value;
	if (dtStr.length == 0) {
		var today = new Date();
		day = today.getDate();
		month = today.getMonth() + 1;
		year = today.getFullYear();
		year = (year + "").substring(2,4);
		newValue = day + "/";
		if (newValue.length < 3) {
			newValue = "0" + newValue;
		}
		newValue = month + "/" + newValue
		if (newValue.length < 5) {
			newValue = "0" + newValue;
		}
		if (newValue.length < 8) {
			newValue = "0" + newValue;
		}
		dtStr = newValue + year;
	}
	try {
		var thisDate = Date.parse(dtStr);
	} catch (err) {
		return;
	}
	
	var msTime = thisDate + (oneDay * numberDays);
	if (isNaN(msTime)) {
		return;
	}
	var newDate = new Date(msTime);
	var month = newDate.getMonth()
	var day = newDate.getDate()
	var year = newDate.getFullYear() + "";

	month = ((month < 10) ? "0" : "") + (month + 1);
	day = ((day < 10) ? "0" : "") + day;
	year = year.substring(2,4);
	dateField.value = month + "/" + day + "/" + year;
}

function addNote(noteField,addNoteField,initials,appendNotes,addBullet) {
	if (appendNotes == null) {
		appendNotes = false;
	}
	if (addBullet == null) {
		addBullet = false;
	}
	if (addNoteField.value != "") {
		var noteValue = noteField.value;
		while (noteValue.charCodeAt(noteValue.length-1) == 10 || noteValue.charCodeAt(noteValue.length-1) == 13) {
			noteValue = noteValue.substr(0,(noteValue.length - 1));
		}
		if (noteValue.length > 0) {
			noteValue += "\n";
		}
		var today = new Date();
		day = today.getDate();
		month = today.getMonth() + 1;
		year = today.getFullYear() + "";
		newValue = day + "";
		if (newValue.length < 2) {
			newValue = "0" + newValue;
		}
		newValue = month + "/" + newValue + "/" + year.substring(2,4);
		if (newValue.length < 8) {
			newValue = "0" + newValue;
		}
		addNoteValue = addNoteField.value;
		if (appendNotes) {
			noteField.value = noteValue + (addBullet ? String.fromCharCode(8226) + " " : "") + addNoteValue + '  [' + initials + ", " + newValue + "]\n";
		} else {
			noteField.value = (addBullet ? String.fromCharCode(8226) + " " : "") + addNoteValue + '  [' + initials + ", " + newValue + "]\n" + noteValue;
		}
		noteField.value += "\n";
		addNoteField.value = "";
		addNoteField.focus();
	}
}

var Crc32Tab = new Array( /* CRC polynomial 0xEDB88320 */
/*
  C/C++ language:

  unsigned long Crc32Tab[] = {...};
*/
0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x06DC419,0x706AF48F,0xE963A535,0x9E6495A3,
0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,0x09B64C2B,0x7EB17CBD,0xE7B82D07,0x90BF1D91,
0x1DB71064,0x6AB020F2,0xF3B97148,0x84BE41DE,0x1ADAD47D,0x6DDDE4EB,0xF4D4B551,0x83D385C7,
0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,0x14015C4F,0x63066CD9,0xFA0F3D63,0x8D080DF5,
0x3B6E20C8,0x4C69105E,0xD56041E4,0xA2677172,0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B,
0x35B5A8FA,0x42B2986C,0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59,
0x26D930AC,0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,0xCFBA9599,0xB8BDA50F,
0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,0x2F6F7C87,0x58684C11,0xC1611DAB,0xB6662D3D,
0x6DC4190,0x01DB7106,0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,0x9FBFE4A5,0xE8B8D433,
0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,0x086D3D2D,0x91646C97,0xE6635C01,
0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457,
0x65B0D9C6,0x12B7E950,0x8BBEB8EA,0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65,
0x4DB26158,0x3AB551CE,0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,0xA4D1C46D,0xD3D6F4FB,
0x4369E96A,0x346ED9FC,0xAD678846,0xDA60B8D0,0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9,
0x5005713C,0x270241AA,0xBE0B1010,0xC90C2086,0x568B525,0x206F85B3,0xB966D409,0xCE61E49F,
0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,0xB7BD5C3B,0xC0BA6CAD,
0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,0xEAD54739,0x9DD277AF,0x04DB2615,0x73DC1683,
0xE3630B12,0x94643B84,0x0D6D6A3E,0x7A6A5AA8,0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1,
0xF00F9344,0x8708A3D2,0x1E01F268,0x6906C2FE,0xF62575D,0x806567CB,0x196C3671,0x6E6B06E7,
0xFED41B6,0x89D32BE0,0x10DA7A5A,0x67DD4ACC,0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5,
0xD6D6A3E8,0xA1D1937E,0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC567,0x3FB506DD,0x48B2364B,
0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,0x316E8EEF,0x4669BE79,
0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,0xCC0C7795,0xBB0B4703,0x220216B9,0x5505262F,
0xC5BA3BBE,0xB2BD0B28,0x2BB45A92,0x5CB36A04,0xC2D7FFA7,0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D,
0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,0x9C0906A9,0xEB0E363F,0x7206785,0x05005713,
0x95BF4A82,0xE2B87A14,0x7BB12BAE,0x0CB61B38,0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21,
0x86D3D2D4,0xF1D4E242,0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777,
0x88085AE6,0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,0x616BFFD3,0x166CCF45,
0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,0xA672661,0xD06016F7,0x4969474D,0x3E6E77DB,
0xAED16A4A,0xD9D65ADC,0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,0x47B2CF7F,0x30B5FFE9,
0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,0xCDD70693,0x54DE5729,0x23D967BF,
0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D);

function Crc32Add(crc,c) {
  return Crc32Tab[(crc^c)&0xFF]^((crc>>8)&0xFFFFFF);
}

function Crc32Str(str) {
  var n;
  var len=str.length;
  var crc;

  crc=0xFFFFFFFF;
  for (n=0; n<len; n++)
  {
	var code = str.charCodeAt(n);
	if (code == 13 && n<(len - 1) && str.charCodeAt(n+1) == 10) {
		continue;
	}
	if (code > 122) {
		continue;
	}
	if (code < 32) {
		code = 32;
	}
    crc=Crc32Add(crc,code);
  }
  return crc^0xFFFFFFFF;
}

function Hex32(val)
/*
  Convert value as 32-bit unsigned integer to 8 digit hexadecimal number prefixed with "0x".
*/
{
  var n;
  var str1;
  var str2;

  n=val&0xFFFF;
  str1=n.toString(16).toUpperCase();
  while (str1.length<4)
  {
    str1="0"+str1;
  }
  n=(val>>>16)&0xFFFF;
  str2=n.toString(16).toUpperCase();
  while (str2.length<4)
  {
    str2="0"+str2;
  }
  return str2+str1;
}

function popup(name, leftPosition, topPosition) {
	var obj = document.getElementById(name);
	with( obj.style ) {
		position = "absolute";
		left = leftPosition + "px";
		top = topPosition + "px";
		display = "block";
	}
}

function unpop(name) {
	var obj = document.getElementById(name);
	obj.style.display = "none";
}

var globalIE = document.all?true:false;
var globalMouseX = 0;
var globalMouseY = 0;
function getMouseXY(e) {
	if (globalIE) {
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	if (tempX < 0) {
		tempX = 0;
	}
	if (tempY < 0) {
		tempY = 0;
	}
	globalMouseX = tempX;
	globalMouseY = tempY;
}

function dateKeyHandler(dateField,e) {
	if (document.layers) {
		Key = e.which;
	} else {
		Key = window.event.keyCode;
	}
	if (Key == 43 || Key == 61) {
		addDays(dateField,1);
		return false;
	}
	if (Key == 45) {
		addDays(dateField,-1);
		return false;
	}
}

