// validatelib_common.js // (C) 2006 IAG // Date: 13/10/2006 // ================================================================= // Objects and Variables // ================================================================= var ID_carComp = "carcomp"; var ID_ctp = "ctp"; var ID_carTppd = "cartppd"; var ID_carTpft = "cartpft"; var ID_bikeComp = "bikecomp"; var ID_bikeTppd = "biketppd"; var ID_homeBldg = "homebldg"; var ID_homeCont = "homecont"; var ID_homeComb = "homecomb"; var UNSUPPORTED_RISK = "UNSUPPORTED_RISK"; // Default error messages for postcode type errors var PCblank = "Postcode is blank. Please enter a valid value"; var PCnotAnInteger = "Only numeric values are accepted for postcode"; var PCinvalidLength = "Postcode must be 4 digits."; // define state var STATE_NSW = "NSW"; var STATE_ACT = "ACT"; var STATE_QLD = "QLD"; var STATE_VIC = "VIC"; var STATE_TAS = "TAS"; var STATE_WA = "WA"; var STATE_SA = "SA"; var STATE_UNSUPPORTED = "STATE_UNSUPPORTED"; var COUNTRY_STATE_NZ = "NZ"; // define state var STATE_CODE_NSW = "02"; var STATE_CODE_ACT = "01"; var STATE_CODE_QLD = "04"; var STATE_CODE_VIC = "03"; var STATE_CODE_TAS = "07"; var STATE_CODE_WA = "08"; var STATE_CODE_SA = "05"; var STATE_CODE_UNSUPPORTED = "00"; var COUNTRY_STATE_CODE_NZ = "64"; // define postcode range var pcodeACTstart = "2600"; var pcodeACTend = "2639"; var pcodeNSWstart = "2000"; var pcodeNSWend = "2999"; var pcodeVICstart = "3000"; var pcodeVICend = "3999"; var pcodeQLDstart = "4000"; var pcodeQLDend = "4999"; var pcodeSAstart = "5000"; var pcodeSAend = "5999"; var pcodeWAstart = "6000"; var pcodeWAend = "6999"; var pcodeTASstart = "7000"; var pcodeTASend = "7999"; //unsupported brand var UNSUPPORTED_BRAND = "UNSUPPORTED_BRAND"; // whitespace characters var whitespace = " \t\n\r"; // decimal point character var decimalPointDelimiter = "."; var daysInMonth = new Array(12); daysInMonth[1] = 31; daysInMonth[2] = 29 ; // must programmatically check this daysInMonth[3] = 31; daysInMonth[4] = 30; daysInMonth[5] = 31; daysInMonth[6] = 30; daysInMonth[7] = 31; daysInMonth[8] = 31; daysInMonth[9] = 30; daysInMonth[10] = 31; daysInMonth[11] = 30; daysInMonth[12] = 31; // Default error messages for postcode type errors var PCoutOfRange = "Postcode is out of range. \n" + "Premium Estimate only applies to postcodes that are in NSW, ACT or QLD."; var MandatoryNumberFieldInvalidMsg = "This field must be completed using numbers. Please check your answer and try again."; var MandatoryLetterFieldInvalidMsg = "This field must be completed using letters. Please check your answer and try again."; // ================================================================ // Common Validation routines // ================================================================ function buildStringList(stringList, key, value) /* This function is used to facilitate the packing of data to be sent to the server. A method on the web server is used to unpack this data into a Vector of Hashtables. The main purpose is to transfer lists of data to the server. */ { if (stringList == null) { stringList = "%%" + key + "%%" + value; } else { stringList = stringList + "%%" + key + "%%" + value; } return stringList; } function checkDate(day,month,year) { return checkDateWithMsg(day,month,year,false); } function checkDateWithMsg(day,month,year,displayMsg) { if (isEmpty(year)) { if (displayMsg) { alert("Please enter a Year."); } return false; } if (isEmpty(month)) { if (displayMsg) { alert("Please enter a Month."); } return false; } if (isEmpty(day)) { if (displayMsg) { alert("Please enter a Day of the month."); } return false; } if ((day == 31) && ((month == 2) || (month == 4) || (month == 6) || (month == 9) || (month == 11))) { if (displayMsg) { alert("Please enter a valid Day and Month combination."); } return false; } if (day == 30 && month == 2) { if (displayMsg) { alert("Please enter a valid Day and Month combination."); } return false; } // Leap year... if (day == 29 && month == 2){ if ( ( (year % 4 == 0) && (year % 100 != 0) ) || (year % 400 == 0) ) { return true; } else { if (displayMsg) { alert("Please enter a valid Day and Month combination."); } return false; } } if(!isInYear4DigitFormat(year, displayMsg)) { return false; } return true; } 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 dropLeadingZero(inputString) { var outputString = inputString; var c = outputString.indexOf("0"); while(outputString.indexOf("0") == 0) { outputString = outputString.substring(1, outputString.length); } return outputString; } function file_onclick(page,width,height) { var win = window.open(page,"_"+"blank","toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width="+ width + ",height="+height); } function forwardTo(dispMsg, urlFrom, urlTo) { if (confirm(dispMsg)) { var thisURL = document.URL; var fullS = (document.URL).toLowerCase(); for (var i=0; i= "a") && (c <= "z") ) || ( (c >= "A") && (c <= "Z") ) ); } //the following function returns TRUE if the input string contains Alphanumeric chars plus any chars supplied in the // the extraChars parameter, otherwise returns FALSE function isAlphaNumeric (inputString) { var i; var c; for (i = 0 ; i < inputString.length ;i++) { c = inputString.charAt(i); if ( (!(isDigit(c))) && (!(isAlpha(c))) ) { //not found return false; } } return true; } //the following function returns TRUE if the input string contains Alphanumeric chars plus any chars supplied in the // the extraChars parameter, otherwise returns FALSE function isAlphaNumericPlus (inputString, extraChars) { var i; var c; for (i = 0 ; i < inputString.length ;i++) { c = inputString.charAt(i); if ( (!(isDigit(c))) && (!(isAlpha(c))) && (!(onlyContainsMatchingChars(c, extraChars))) ) { //not found return false; } } return true; } //the following function returns TRUE if the input string contains Alpha chars plus any chars supplied in the // the extraChars parameter, otherwise returns FALSE function isAlphaPlus (inputString, extraChars) { var i; var c; for (i = 0 ; i < inputString.length ;i++) { c = inputString.charAt(i); if ((!(isAlpha(c))) && (!(onlyContainsMatchingChars(c, extraChars))) ) { //not found return false; } } return true; } //the following function returns TRUE if the input string contains Alpha chars plus any chars supplied in the // the extraChars parameter, otherwise returns FALSE function isAlphaSpacePlus (inputString, extraChars) { var i; var c; for (i = 0 ; i < inputString.length ;i++) { c = inputString.charAt(i); if ((!(isAlpha(c))) && (!(onlyContainsMatchingChars(c, extraChars))) && (!(onlyContainsMatchingChars(c, " ")))) { //not found return false; } } return true; } function isBrandSupportedRisk(brand, risk) { var riskSet = ""; if (risk == UNSUPPORTED_RISK) { return false; } if (brand == BRAND_STATENZ) { riskSet = ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (brand == BRAND_NRMA) { riskSet = ID_ctp + " ," + ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (brand == BRAND_NRMAQLD) { riskSet = ID_ctp + " ," + ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (brand == BRAND_RACV) { riskSet = ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (brand == BRAND_SGIC) { riskSet = ID_carComp + " ," + ID_carTpft + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (brand == BRAND_SGIO) { riskSet = ID_carComp + " ," + ID_carTpft + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } if (riskSet.indexOf(risk)!=-1) { return true; } else { return false; //risk not supported } } function isStateSupportedRisk(countryState, risk) { var riskSet = ""; if (risk == UNSUPPORTED_RISK) { return false; } if (countryState == COUNTRY_STATE_CODE_NZ) { riskSet = ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (countryState == STATE_CODE_ACT) { riskSet = ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (countryState == STATE_CODE_NSW) { riskSet = ID_ctp + " ," + ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (countryState == STATE_CODE_QLD) { riskSet = ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (countryState == STATE_CODE_VIC) { riskSet = ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (countryState == STATE_CODE_SA) { riskSet = ID_carComp + " ," + ID_carTpft + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } else if (countryState == STATE_CODE_WA) { riskSet = ID_carComp + " ," + ID_carTpft + " ," + ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; } if (riskSet.indexOf(risk)!=-1) { return true; } else { return false; //risk not supported } } function isBrandSupportedState(brand, countryState) { var stateSet = ""; if (brand == BRAND_STATENZ) { return true; } else if (brand == BRAND_NRMA) { stateSet = STATE_CODE_ACT + "," + STATE_CODE_NSW; } else if (brand == BRAND_NRMAQLD) { stateSet = STATE_CODE_QLD; } else if (brand == BRAND_RACV) { stateSet = STATE_CODE_VIC; } else if (brand == BRAND_SGIC) { stateSet = STATE_CODE_SA; } else if (brand == BRAND_SGIO) { stateSet = STATE_CODE_WA; } if (stateSet.indexOf(countryState)!=-1) { return true; } else { return false; //state not supported } } function isContainingData(value, name, displayMessageFlag, errorMsg) { if (isEmpty(value)) { if (displayMessageFlag) { if (!isEmpty(errorMsg)) { alert (errorMsg); } else { alert ( name + " is empty. Please enter a valid value"); } } return false; } return true; } function isDateInFuture(day, month, year, displayMessageFlag, messageText) { var inFuture = false; var d = new Date(); var currentDay = d.getDate(); var currentMonth = d.getMonth() + 1; var currentYear = d.getFullYear(); var dayValue = parseInt(day, 10); var monthValue = parseInt(month, 10); var yearValue = parseInt(year, 10); if (yearValue > currentYear) { inFuture = true; } else { if ((yearValue == currentYear) && (monthValue > currentMonth)) { inFuture = true; } else { if ((yearValue == currentYear) && (monthValue == currentMonth) && (dayValue > currentDay )) { inFuture = true; } } } // if date is not in future, return false if (!inFuture) { return false; } // decide on displaying options if (displayMessageFlag) { if (!isEmpty(messageText)) { alert (messageText); } else { alert ("You have entered a future Year."); } } return true; } function isDigit(c) { return ((c >= "0") && (c <= "9")); } function isEmpty(s) { return ((s == null) || (s.length == 0)); } function isFloatAndDecimal(s,n) { var i; var seenDecimalPoint = false; var counter =0; if (isEmpty(s)) { return false; } if (s == decimalPointDelimiter) { return false; } // Search through string's characters one by one // until we find a non-numeric character. // When we do, return false; if we don't, return true. for (i = 0; i < s.length; i++) { // Check that current character is number. var c = s.charAt(i); if ((c == decimalPointDelimiter) && !seenDecimalPoint) { seenDecimalPoint = true; } else if ((seenDecimalPoint) && (isDigit(c))) { // check how many digits after decimal point counter++; } else if (!isDigit(c)) { return false; } } // check if digits after the decimal point is greater than 1 digit if (counter> n) { return false; } // All characters are numbers. return true; } function isFloatPercentageValid(s) { // Now, explicitly change the type to float via parseFloat // so that the comparison code below will work both on // JavaScript 1.2 (which typechecks in equality comparisons) // and JavaScript 1.1 and before (which doesn't). var itcnum = parseFloat (s); if (itcnum > 100) { return false; } else { return true; } } function isGreaterThanZero (s) { // Now, explicitly change the type to float via parseFloat // so that the comparison code below will work both on // JavaScript 1.2 (which typechecks in equality comparisons) // and JavaScript 1.1 and before (which doesn't). var itcnum = parseFloat (s); if (itcnum > 0) { return true; } else { return false; } } function isHomeRisk(risk) { var homeRiskSet = ID_homeBldg + " ," + ID_homeCont + " ," + ID_homeComb; if (homeRiskSet.indexOf(risk)!=-1) { return true; } else { return false; //not a home risk } } function isInDayFormat(value, displayMessageFlag) { if (!isIntegerInRange (value, 1, 31)) { if (displayMessageFlag) { alert ("value for Day should be between 1 and 31"); } return false; } return true; } function isInMonthFormat(value, displayMessageFlag) { if (!isIntegerInRange (value, 1, 12)) { if (displayMessageFlag) { alert ("Month should be between 1 and 12"); } return false; } return true; } function isInteger(s) { var i; var inputLen = s.length; if (inputLen == 0){ return false; } for (i = 0; i < inputLen; i++) { // Check that current character is number. var c = s.charAt(i); if (!isDigit(c)) { return false; } } // All characters are numbers. return true; } function isIntegerInRange(s, a, b) { if (!isInteger(s)) { return false; } // Now, explicitly change the type to integer via parseInt // so that the comparison code below will work both on // JavaScript 1.2 (which typechecks in equality comparisons) // and JavaScript 1.1 and before (which doesn't). var num = parseInt(s,10); return ((num >= a) && (num <= b)); } function isIntegerInRangeWithMsg(value, lowr, uppr, name, displayMessageFlag, errorMsg) { alert('t' + value + 't'); if (!isIntegerInRange(value, lowr, uppr)) { if (displayMessageFlag) { if (!isEmpty(errorMsg)) { alert (errorMsg); } else { alert("the " + name + ' must be between ' + lowr + ' and ' + uppr) ; } } return false; } return true; } function isIntegerOfLength(value, name, requiredLength, displayMessageFlag, errorMsg) { if (!isInteger (value)) { if (displayMessageFlag) { alert ( name + " must be a numeric value."); // may add in another paramater to allow user to specify exact content of the message } return false; } if ( value.length != requiredLength ) { if (displayMessageFlag) { if (!isEmpty(errorMsg)) { alert (errorMsg); } else { alert ( name + "must be a " + requiredLength + " digit numeric value."); } } return false; } return true; } function isIntegerOutOfRange (s, a, b) { if (!isInteger(s)) { return false; } // Now, explicitly change the type to integer via parseInt // so that the comparison code below will work both on // JavaScript 1.2 (which typechecks in equality comparisons) // and JavaScript 1.1 and before (which doesn't). var num = parseInt(s,10); if ((num <= a) || (num >= b)) { return false; } else { return true; } } function isIntegerWithMsg(intValue, varName, displayMessageFlag, errorMsg) { if (!isInteger(intValue)) { if (displayMessageFlag) { if (!isEmpty(errorMsg)) { alert (errorMsg); } else { alert ( varName + " must be a numeric value."); } } return false; } return true; } // this function expects year to be four digits function isInYear4DigitFormat(value, displayMessageFlag) { if (!isIntegerOfLength(value, "Year", 4, true, "Year must be a 4 digit numeric value.")) { return false; } return true; } function isMotorRisk(risk) { var motorRiskSet = ID_carComp + " ," + ID_carTppd + " ," + ID_carTpft + " ," + ID_bikeComp + " ," + ID_bikeTppd; if (motorRiskSet.indexOf(risk)!=-1) { return true; } else { return false; //not a motor risk } } function isOneWhiteSpace(s) { var i; var counter = 0; if (isEmpty(s)) { return true; } for (i = 0;i < s.length;i++) { var c = s.charAt(i); if (isSpace(c)) { counter++; } } return (counter <= 1); } function isOtherValidNameChar(c) { return ((c == " ") || (c == "-") || (c == "_")); } function isPercentageWithMsg(val, name, displayMessageFlag, errorMsg) { if (!isNaN(val)) { if (val < 0 || val > 100) { alert(errorMsg); return false; } else if (val.indexOf('.' != -1)) { alert(errorMsg); return false; } } else { alert(errorMsg); return false; } return true; } function isPositiveInteger(s) { if (isNaN(s)) { return false; } else if(s < 0) { return false; } return true; } function isPositiveIntegerWithMsg(value, name, displayMessageFlag, errorMsg) { if (!isPositiveInteger(value)) { if (displayMessageFlag) { if (!isEmpty(errorMsg)) { alert (errorMsg); } else { alert("only positive numeric values are accepted for " + name) ; } } return false; } return true; } function isPostcodeValid_basic(value, displayMessageFlag) { if (isWhiteSpace(value)) { if (displayMessageFlag) { alert(PCblank); } return false; } if (!isInteger(value)) { if (displayMessageFlag) { alert (PCnotAnInteger); } return false; } if (value.length != 4) { if (displayMessageFlag) { alert(PCinvalidLength); } return false; } return true; } function isPostcodeValidForBrandAndRisk(postcode, displayMessageFlag, brand, risk, product) { if (!isPostcodeValid_basic(postcode, displayMessageFlag)) { return false; } var countryState = ""; if (brand == BRAND_STATENZ) { countryState = COUNTRY_STATE_CODE_NZ; } else { countryState = getPostalState(postcode); } var suggestBrand = ""; //check to see if risk is suppoted in state selected //eg. CTP isonly supported in NSW for NRMA if (isStateSupportedRisk(countryState, risk)) { //if the state entered is applicable for the current brand ... if (isBrandSupportedState(brand, countryState)) { return true; } else { //determine if on of our other brands is applicable for the //state entered suggestBrand = getAlternateBrand(brand, countryState, risk); if (suggestBrand != UNSUPPORTED_BRAND) { var alternateBrandMsg = getAlternateBrandMsg(brand, suggestBrand, risk, product); if (forwardTo(alternateBrandMsg, brand, suggestBrand)) { return false; } } } } var errorMsg = getUnsupportedStateBrandMsg(brand, suggestBrand); if (displayMessageFlag) { alert(errorMsg); } return false; } function isSelectionMade(list) { // assume the first item on list is default, which doesn't have a valid value if (!list.selectedIndex) { alert ("Please make a selection from " + list.name ); return false; } return true; } function isSpace (c) { return ((c == " ")); } function isValidDayMonth(day, month) { var intMonth = parseInt(month,10); var intDay = parseInt(day,10); // catch invalid days, except for February if ((intDay > daysInMonth[intMonth])) { return false; } else { return true; } } function isValidName(name,displayMessageFlag,errorMsg) { //give error if only white space if (isWhiteSpace(name)) { if (displayMessageFlag) { if (!isEmpty(errorMsg)) { alert (errorMsg); } else { alert("Name field must not contain only spaces"); } } return false; } for (i = 0; i < name.length; i++){ var c = name.charAt(i); if ( ! ( (isAlpha(c)) || (isOtherValidNameChar(c)) ) ) { if (displayMessageFlag) { if (!isEmpty(errorMsg)) { alert (errorMsg); } else { alert("Name contains invalid characters"); } } return false; } } // All characters are valid return true; } function isWhiteSpace(s) { var i; if (isEmpty(s)) { return true; } for (i = 0;i < s.length;i++) { // Check that current character isn't whitespace. var c = s.charAt(i); if (whitespace.indexOf(c) == -1) { return false; } } // All characters are whitespace. return true; } function isYearInFuture(value, displayMessageFlag, messageText) { var d = new Date(); var currentYear = d.getFullYear(); var yearValue = parseInt(value, 10); if (yearValue > currentYear) { if (displayMessageFlag) { if (!isEmpty(messageText)) { alert (messageText); } else { alert ("The year you have entered is in the future. Please check your answer and try again."); } } return true; } return false; } function isYearPlusOneInFuture(value, displayMessageFlag, messageText) { var d = new Date(); var currentYear = d.getFullYear(); var yearValue = parseInt(value, 10); if (yearValue > (currentYear + 1)) { if (displayMessageFlag) { if (!isEmpty(messageText)) { alert (messageText); } else { alert ("The year you have entered is in the future. Please check your answer and try again."); } } return true; } return false; } function onlyContainsMatchingChars(inputString, chars) { var i; var c; for (i = 0 ; i < inputString.length ;i++) { c = inputString.charAt(i); if (chars.indexOf(c) == -1) { //not found return false; } } return true; } function pdf_window(page) { var win = window.open(page,"_"+"blank","toolbars=no,menubars=no,location=no,scrollbars=yes,resizable=yes,width=500,height=500"); } /************************************************ Derive a person's age from their Date of Birth. *************************************************/ function personsAgeFullYears(dobDay, dobMonth, dobYear) { var age = personsAge(dobDay, dobMonth, dobYear); var ageString = "" + age; var re = /\./; var pos = ageString.search(re); if (pos != -1) { ageString = ageString.substring(0,pos); } return ageString; } function personsAge(dobDay, dobMonth, dobYear) { //This functions returns the driver age in years. //Note takes no account of day. var ageYears; //Get today's date var todayOb = new Object(); var today = new Date(); todayOb.year = today.getFullYear(); todayOb.month = today.getMonth() +1; todayOb.day = today.getDate(); var ageMonths = 0; var ageMonths1 = 0; var ageMonths2 = 0; ageMonths1 = ((todayOb.year.valueOf() - 1900) * 12) + todayOb.month.valueOf(); ageMonths2 = ((parseInt(dobYear.valueOf(),10) - 1900) * 12) + parseInt(dobMonth.valueOf(),10); ageMonths = ageMonths1 - ageMonths2; if (dobDay > todayOb.day) { ageMonths--; } ageYears = ageMonths / 12; return ageYears; } function removeChars(oldString, chars) { var i; var c; var newString = ""; for (i = 0 ; i < oldString.length; i++) { c = oldString.charAt(i); if (chars.indexOf(c) == -1) { newString += c; } } return newString; }