/**************************************************************************************** Library of functions for using and validating IBS online applications ***************************************************************************************** _________________________________________________________________ Inbetween the
tags, put: You will need to include a reference to jquery Available Functions isDate(dateString) Purpose: To validate a string as a date and return useful information about errors Input: String Returns: Error String - global age variable is years between now and date. resetBackground(iForm) Purpose: To reset formatting on form elements Input: Form to be reset Returns: N/A qryHowOld(varBirthDate) Purpose: To return someones age in years Input: valid Date as a String in format (validate first with isDate()) Returns: Integer age in full years. mandatory(element) Purpose: To verify that the element of any type has had a response Input: Form element Returns: NA furtherInfo(element, strResponse, element) Purpose: If the first element has the response then second element response is required Input: Form element, response, element required Returns: NA isJointApp(option) Purpose: To decide id an app is joint and to jump accordingly Input: Char string representing page option id Returns: NA checkJointApp() getLabel(element) setError(element) clearError(element) mandatoryField(element) validateDate(element, mandatory) mandatoryRadioSet(element) cpyAddress(element) detetct ie 6 if ((navigator.appVersion.indexOf("MSIE 6.") != -1)) { _______________________________________________________________________________________*/ //GLOBALS var d = new Date(); var ValidationMessage = ""; var dtCh = "/"; var minYear = 1900; var maxYear = d.getFullYear() + 1; var age; // ---------------------------------------------------------------------------| // qryHowOld | // Description: How old someone is in the format: | // XXX Years XX Months X Weeks X Days | // Birth Date could be specified like Date.UTC(2002,8,16,17,42,0) | // | // Arguments: | // varAsOfDate: as of date | // varBirthDate: birth date | // | function qryHowOld(varBirthDate) { var dtAsOfDate; var dtBirth; var dtAnniversary; var intSpan; var intYears; var intMonths; var intWeeks; var intDays; var intHours; var intMinutes; var intSeconds; var strHowOld; // get born date dtBirth = new Date(varBirthDate); // get as of date dtAsOfDate = new Date(); // if as of date is on or after born date if (dtAsOfDate >= dtBirth) { // get time span between as of time and birth time intSpan = (dtAsOfDate.getUTCHours() * 3600000 + dtAsOfDate.getUTCMinutes() * 60000 + dtAsOfDate.getUTCSeconds() * 1000) - (dtBirth.getUTCHours() * 3600000 + dtBirth.getUTCMinutes() * 60000 + dtBirth.getUTCSeconds() * 1000); // start at as of date and look backwards for anniversary // if as of day (date) is after birth day (date) or // as of day (date) is birth day (date) and // as of time is on or after birth time if (dtAsOfDate.getUTCDate() > dtBirth.getUTCDate() || (dtAsOfDate.getUTCDate() == dtBirth.getUTCDate() && intSpan >= 0)) { // most recent day (date) anniversary is in as of month dtAnniversary = new Date(Date.UTC(dtAsOfDate.getUTCFullYear(), dtAsOfDate.getUTCMonth(), dtBirth.getUTCDate(), dtBirth.getUTCHours(), dtBirth.getUTCMinutes(), dtBirth.getUTCSeconds())); } // if as of day (date) is before birth day (date) or // as of day (date) is birth day (date) and // as of time is before birth time else { // most recent day (date) anniversary is in month before as of month dtAnniversary = new Date(Date.UTC(dtAsOfDate.getUTCFullYear(), dtAsOfDate.getUTCMonth() - 1, dtBirth.getUTCDate(), dtBirth.getUTCHours(), dtBirth.getUTCMinutes(), dtBirth.getUTCSeconds())); // get previous month intMonths = dtAsOfDate.getUTCMonth() - 1; if (intMonths == -1) { intMonths = 11; } // while month is not what it is supposed to be (it will be higher) while (dtAnniversary.getUTCMonth() != intMonths) { // move back one day dtAnniversary.setUTCDate(dtAnniversary.getUTCDate() - 1); } } // if anniversary month is on or after birth month if (dtAnniversary.getUTCMonth() >= dtBirth.getUTCMonth()) { // months elapsed is anniversary month - birth month intMonths = dtAnniversary.getUTCMonth() - dtBirth.getUTCMonth(); // years elapsed is anniversary year - birth year intYears = dtAnniversary.getUTCFullYear() - dtBirth.getUTCFullYear(); } // if birth month is after anniversary month else { // months elapsed is months left in birth year + anniversary month intMonths = (11 - dtBirth.getUTCMonth()) + dtAnniversary.getUTCMonth() + 1; // years elapsed is year before anniversary year - birth year intYears = (dtAnniversary.getUTCFullYear() - 1) - dtBirth.getUTCFullYear(); } // to calculate weeks, days, hours, minutes and seconds // we can take the difference from anniversary date and as of date // get time span between two dates in milliseconds intSpan = dtAsOfDate - dtAnniversary; // get number of weeks intWeeks = Math.floor(intSpan / 604800000); // subtract weeks from time span intSpan = intSpan - (intWeeks * 604800000); // get number of days intDays = Math.floor(intSpan / 86400000); // subtract days from time span intSpan = intSpan - (intDays * 86400000); // get number of hours intHours = Math.floor(intSpan / 3600000); // subtract hours from time span intSpan = intSpan - (intHours * 3600000); // get number of minutes intMinutes = Math.floor(intSpan / 60000); // subtract minutes from time span intSpan = intSpan - (intMinutes * 60000); // get number of seconds intSeconds = Math.floor(intSpan / 1000); // create output string if (intYears > 0) { if (intYears > 1) { strHowOld = intYears.toString(); } else { strHowOld = intYears.toString(); } } else { strHowOld = 0; } } // return string representation return strHowOld; } // | // qryHowOld | // ---------------------------------------------------------------------------| // ---------------------------------------------------------------------------| //Validate date //Group of functions to validate a string as correct date format // // isDate(dtStr) returns an string with errors also age variable containg age 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; } 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 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 isDate(dtStr) { var errors = ""; var daysInMonth = DaysArray(12); var pos1 = dtStr.indexOf(dtCh); var pos2 = dtStr.indexOf(dtCh, pos1 + 1); var strMonth = dtStr.substring(pos1 + 1, pos2); var strDay = dtStr.substring(0, pos1); var strYear = dtStr.substring(pos2 + 1); 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, 10); day = parseInt(strDay, 10); year = parseInt(strYr, 10); if (pos1 == -1 || pos2 == -1) { errors += "The date format should be : dd/mm/yyyy"; return errors; } if (strMonth.length < 1 || month < 1 || month > 12) { errors += "Please enter a valid month"; return errors; } if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) { errors += "Please enter a valid day"; return errors; } if (strYear.length != 4 || year === 0 || year < minYear || year > maxYear) { errors += "Please enter a valid 4 digit year between " + minYear + " and " + maxYear; return errors; } if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) === false) { errors += "Please enter a valid date"; return errors; } //updates the age variable for other use age = qryHowOld((strMonth + '/' + strDay + '/' + strYear)); return errors; } // | // END Validate date | // ---------------------------------------------------------------------------| function jumpSubmit(option) { ValidationMessage = ''; validateForm(); if (ValidationMessage !== "") { window.location.hash = ''; document.getElementById("formError").innerHTML = "The following information is required:" + "