/* Global Javascript File */ function openPopup(strLocation, height, width){ var newWin newWin = window.open(strLocation,"",'resizable=yes,scrollbars=yes,width=' + width + ',height=' + height + ',locationbar=no,toolbar=no') } //end openPopup function number_format(myNumber) { if (myNumber != "" || myNumber != "undefined") { myNumber = myNumber.toString(); } return addSeparatorsNF(myNumber, ".", ".", ","); } //end number_format function addSeparatorsNF(nStr, inD, outD, sep) { nStr += ''; var dpos = nStr.indexOf(inD); var nStrEnd = ''; if (dpos != -1) { nStrEnd = outD + nStr.substring(dpos + 1, nStr.length); nStr = nStr.substring(0, dpos); } var rgx = /(\d+)(\d{3})/; while (rgx.test(nStr)) { nStr = nStr.replace(rgx, '$1' + sep + '$2'); } return nStr + nStrEnd; } //End addSeparatorNF() //Only allows the user to enter positive numbers (allows point: 52.75) function isNumberKey(evt){ var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode != 46)){ return false; } else{ return true; } } //end isNumberKey // Verify that an input field is really some form of date. This function uses // date2Timestamp to do all the work (so all the ugly code is in one place). It // returns the value for any non-false return. For a false return, it outputs // an alert and returns false (bad date). function verifyInputDate(inputDate, inputLabel){ var testDate = date2Timestamp(inputDate.value, inputLabel); // Does all the work if (testDate !== false){ if (testDate != 0) { // If the value is not zero, replace the input date with a "clean" // version that the PHP code will like since the code below is able // to handle more formats than the PHP date parser. dateStamp = new Date(testDate); // Make a date object var newDate = dateStamp.toDateString(); // Returns string like "Sun Jul 15 2007" var newDateArray = newDate.split(" "); // Break at spaces newDate = newDateArray[2] + "-" + newDateArray[1] + "-" + newDateArray[3]; inputDate.value = newDate; // Replace the input value } return testDate; } alert("Bad format for " + inputLabel + ": '" + inputDate.value + "'; please correct it"); inputDate.focus(); return false; } //end verifyInputDate // Verify that an input field is really some form of date and return the date // as a timestamp. This handles both numeric forms with "-" and "/", the // displayed version of "dd-mon-year" and also "ddmonyr" or "ddmonyear". // First, if the date is blank, it returns the value 0. // Then it replaces any "-"s with "/"s and then uses Date.parse to try to // convert it to a timestamp (this handles the all-number forms and also the // mixed form under Firefox). On success, it returns the timestamp. // If that fails, it replaces any "-"s with spaces and then tries Date.parse // again to convert it to a timestamp (this handles the mixed-form dates under // IE 6). On success, it returns the timestamp. // If that fails, it tests for date forms of "ddmonyr" or "ddmonyear"; if either // form it converts the date to "dd mon year" and uses Date.parse the last time // to convert it to a timestamp. function date2Timestamp(inputDate, inputLabel){ var testDate = inputDate.replace(/-/g,"/"); // Replace any "-" with "/" // alert("date2Timestamp: " + inputLabel + " -1- " + inputDate.value + " => " // + testDate); if (testDate == ""){ // alert(inputLabel + " - date is blank"); return 0; } var testParse = Date.parse(testDate); // Will handle various numeric forms // alert("date2Timestamp - date: " + inputLabel + " = " + // inputDate + " : " + testDate + " => " + testParse); if (testParse < 0){ // The timestamp is negative // Firefox Date.parse returns a negative timestamp if the form is // "dd/mn/yr" and the year is less than "70". In normal usage, the // years of "00" thru "69" should be taken as the range of 2000 to // 2069. Test if the date has the forms "dd/mn/yr" or "dd-mn-yr" or any // combination of these separators ("-" has already been replaced with // "/" above). If is does, convert it to "dd/mn/year" and try again. var splitDate = testDate.match(/^(\d+)\/(\d+)\/(\d|\d\d)$/); if (splitDate != null) { testDate = splitDate[1] + " " + splitDate[2] + " "; var year = splitDate[3]; // alert("splitDate[3] = '" + splitDate[3] + "'; year = '" + year + "'"); if (year.length == 1) // If 1-digit year, make 4-digit year testDate += "200" + year; else if (year.length == 2) // If 2-digit year, make 4-digit year testDate += (Number(year) < 70) ? "20" + year : "19" + year; else testDate += year; testParse = Date.parse(testDate); // Will handle various mixed forms // alert("verifyInputDate: " + inputLabel + " -3- " + inputDate.name + " = " + // inputDate.value + " : " + testDate + " => " + testParse); if (!isNaN(testParse)){ // Did this work? return testParse; // Yes, date is ok } } } if (isNaN(testParse)){ // In case mixed date, try again with spaces testDate = inputDate.replace(/-/g," "); // Replace any "-" with " " testParse = Date.parse(testDate); // Will handle various mixed forms // alert("date2Timestamp - mixed date: " + inputLabel + " = " + // inputDate + " : " + testDate + " => " + testParse); } if (!isNaN(testParse)){ // Did either parse work? d = new Date(testParse); var temp = d.toDateString(); return testParse; // Yes, return the value } // Test if the date has forms "ddmonyr" or "dd?mon?yr" or "ddmonyear" or // "dd?mon?year" where the "?" could be a space, "-" or "/". If is does, // convert it to "dd mon year" for parsing. splitDate = inputDate.match(/^(\d+)[ -/]?([a-zA-Z]{3})[ -/]?(\d|\d\d|\d\d\d\d)$/); if (splitDate != null) { testDate = splitDate[1] + " " + splitDate[2] + " "; var year = splitDate[3]; // alert("splitDate[3] = '" + splitDate[3] + "'; year = '" + year + "'"); if (year.length == 1) // If 1-digit year, make 4-digit year testDate += "200" + year; else if (year.length == 2) // If 2-digit year, make 4-digit year testDate += (Number(year) < 70) ? "20" + year : "19" + year; else testDate += year; testParse = Date.parse(testDate); // Will handle various mixed forms // alert("verifyInputDate: " + inputLabel + " -3- " + inputDate.name + " = " + // inputDate.value + " : " + testDate + " => " + testParse); if (!isNaN(testParse)){ // Did this work? return testParse; // Yes, date is ok } } return false; // Failed to parse bad date } //end date2Timestamp function subtractADay(dateName, dateField) { var dateValue = document.getElementById(dateField).value; var timestamp; if ((timestamp = date2Timestamp(dateValue, dateName)) === false) { alert("Bad format for " + dateName + ": '" + dateValue + "'; can't decrement it"); return false; } if (timestamp == 0) { alert(dateName + " is blank; can't decrement it"); return false; } var dateStamp = new Date(timestamp); // alert("timestamp before = " + timestamp + " => " + dateStamp.toDateString()); timestamp -= 86400000; // Subtract a day in milliSeconds if (timestamp < 0) { timestamp = 0; document.getElementById(dateField).value = ""; return true; } dateStamp = new Date(timestamp); // Make a date object var newDate = dateStamp.toDateString(); // Returns string like "Sun Jul 15 2007" var newDateArray = newDate.split(" "); // Break at spaces newDate = newDateArray[2] + "-" + newDateArray[1] + "-" + newDateArray[3]; // alert("timestamp after = " + timestamp + " => " + dateStamp.toDateString() + " or " + newDate); document.getElementById(dateField).value = newDate; return true; } function addADay(dateName, dateField) { var dateValue = document.getElementById(dateField).value; var timestamp; if ((timestamp = date2Timestamp(dateValue, dateName)) === false) { alert("Bad format for " + dateName + ": '" + dateValue + "'; can't increment it"); return false; } if (timestamp == 0) { alert(dateName + " is blank; can't increment it"); return false; } var dateStamp = new Date(timestamp); // alert("timestamp before = " + timestamp + " => " + dateStamp.toDateString()); timestamp += 86400000; // Add a day in milliSeconds if (timestamp < 0) { timestamp = 0; document.getElementById(dateField).value = ""; return true; } dateStamp = new Date(timestamp); // Make a date object var newDate = dateStamp.toDateString(); // Returns string like "Sun Jul 15 2007" var newDateArray = newDate.split(" "); // Break at spaces newDate = newDateArray[2] + "-" + newDateArray[1] + "-" + newDateArray[3]; // alert("timestamp after = " + timestamp + " => " + dateStamp.toDateString() + " or " + newDate); document.getElementById(dateField).value = newDate; return true; } // Verify that an input field looks like a telephone number. First remove any // of the characters "(", ")", "-" and " " (space), then verify that only 7 or // 10 digits are left. function verifyInputPhoneNumber(inputPhone, inputLabel){ var cleanPhone = inputPhone.value.replace(/[-() ]/g,""); // Remove "()- " // alert("verifyinputPhone - " + inputPhone); if (cleanPhone == ""){ //alert("verifyinputPhone - number: is blank"); return true; } var testPhone = /^\d*$/.test(cleanPhone); // Must be all digits // alert("verifyinputPhone - date: " + inputPhone.name + " = " + // inputPhone.value + " : " + cleanPhone + " => " + testPhone); if (!testPhone || (cleanPhone.length != 7 && cleanPhone.length != 10)){ alert("Bad format for " + inputLabel + ": '" + inputPhone.value + "'; please correct it. A phone number field may only be blank " + "or contain exactly 7 or 10 digits with optional parentheses, " + "dash and space characters"); inputPhone.focus(); return false; } else { if (cleanPhone.length == 7) inputPhone.value = cleanPhone.replace(/(\d{3})(\d{4})/, "$1-$2"); else inputPhone.value = cleanPhone.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"); return true; } } //end verifyInputPhoneNumber