/* Wendy's : CRM ~ JAVASCRIPT : FORMS Author: Joel Nagy | dotglu Last Modified: June 9th, 2008 13:10:53 PM EST v1.0 Dependency: moo 1.11+ */ // FORM CONSTANTS var EMAIL_REGEXP = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,20})+$/); var STRING_REGEXP = new RegExp(/\w+/); var DIGIT_REGEXP = new RegExp(/\d+/); var ZIP_REGEXP = new RegExp(/^[0-9][0-9]{4}$/); var ZIP4_REGEXP = new RegExp(/^[0-9][0-9]{4}[ -_.x+|]?[0-9]{4}$/); var CAPOSTAL_REGEXP = new RegExp(/^[a-zA-Z][0-9][a-zA-Z][ -_.x+|]?[0-9][a-zA-Z][0-9]$/); var PHONE_REGEXP = new RegExp(/^[0-9]{7,25}$/); var DIGIT_ONLY_RX = new RegExp(/\D+/g); var errorBox = 'errorMessage'; var SurveyVer = ''; window.addEvent('domready', function() { try { // don't let the form get submitted via ENTER key /* if (window.Event) { window.captureEvents(Event.KEYDOWN); window.onkeydown = handlerKeyDownNetMoz; } else document.onkeydown = handlerKeyDownMS; */ // check the SurveyVer param and hide parts of form SurveyVer = getQuery('surveyver'); switch (SurveyVer.toLowerCase()) { case 'b': // show 1-5 $('tableQuestions3').setStyle('display', 'none'); // 6 & 7 break; case 'c': // show 1-2 $('howOftenQuestions').setStyle('display', 'none'); // 3 $('favoriteTime').setStyle('display', 'none'); // 4 $('gender').setStyle('display', 'none'); // 5 $('tableQuestions3').setStyle('display', 'none'); // 6 & 7 break; default: // show 1-7 } /* $('mobileNumber') .addEvent('keydown', function () { enterOnly(this, DIGIT_ONLY_RX); }) .addEvent('keyup', function () { enterOnly(this, DIGIT_ONLY_RX); }) .addEvent('change', function () { enterOnly(this, DIGIT_ONLY_RX); }) .addEvent('mousedown', function () { setTimeout("enterOnly($('"+ this.id +"'), DIGIT_ONLY_RX)", 1100); setTimeout("enterOnly($('"+ this.id +"'), DIGIT_ONLY_RX)", 2100); setTimeout("enterOnly($('"+ this.id +"'), DIGIT_ONLY_RX)", 3100); }); */ } catch (e) { } }); //: domready() function enterOnly(x, type) { x.value = x.value.replace(type, ''); } // URL QUERY ----------------------------/ var _query = new Object(); function getQuery(name) { try { qlen = 0; for (var x in _query) qlen++; if (qlen == 0) { var l = window.location.href; var q = l.indexOf('?'); if (l.indexOf('&') > 0) q = (l.slice(q+1)).split('&'); else q = [l.slice(q+1)]; for (var i = 0, l = q.length; i < l; i++) { if (q[i].indexOf('=') > 0) { qnv = q[i].split('='); qnv[0] = qnv[0].toLowerCase(); if (qnv.length == 2) _query[qnv[0]] = qnv[1]; } } } name = name != null && name != ''? name.toLowerCase(): ''; if (_query[name] != null) return _query[name]; else if (name != null && name != '') return ''; return _query; } catch (e) { return ''; } } function clickr(e) { try { // only needed for chartInfo if (document.getElementById('chartInfo') == null) return; t = this; id = t.id.toString().trim('_'); var s = id.indexOf('_'); row = id.slice(0, s); col = id.slice(s+1); // now deselect all in row and col $('chartInfo').getElements('input').each(function(t) { if (t.id.indexOf(id) < 0 && (t.id.indexOf(row) == 0 || t.id.lastIndexOf('_'+col) >= 0)) { t.checked = false; } }); $(row).value = $(id).value; // set hidden with value number } catch (e) { } } function isLeapYear(year) { return year % 4 == 0? year % 100 == 0? year % 400 == 0? true: false: true: false; } function validate(F, step, func) { var errors = new Array(); // Start validating data for form switch (step) { case 'info': validateField(F.firstname.value.match(STRING_REGEXP), F.firstname.id, errors); validateField(F.lastname.value.match(STRING_REGEXP), F.lastname.id, errors); validateField(F.address1.value.match(STRING_REGEXP), F.address1.id, errors); validateField(F.emailAddress.value.match(EMAIL_REGEXP), F.emailAddress.id, errors); validateField(F.confirmEmail.value.match(EMAIL_REGEXP), F.confirmEmail.id, errors); validateField(F.emailAddress.value == F.confirmEmail.value, F.emailAddress.id, errors); validateField(F.confirmEmail.value == F.emailAddress.value, F.confirmEmail.id, errors); validateField(F.emailFormat.selectedIndex > 0, F.emailFormat.id, errors); validateField(F.city.value.match(STRING_REGEXP), F.city.id, errors); validateField(F.stateProvince.selectedIndex > 0, F.stateProvince.id, errors); validateField(F.postalZip.value.match(ZIP_REGEXP) || F.postalZip.value.match(ZIP4_REGEXP) || F.postalZip.value.match(CAPOSTAL_REGEXP), F.postalZip.id, errors); validateField(F.country.selectedIndex > 0, F.country.id, errors); // if state and zip are us and country is ca then fail if ((F.postalZip.value.match(ZIP_REGEXP) || F.postalZip.value.match(ZIP4_REGEXP)) && (F.stateProvince.selectedIndex >= 1 && F.stateProvince.selectedIndex <= 52) && F.country.selectedIndex == 2) validateField(false, F.country.id, errors) // if zip and country are us and state is ca then fail else if ((F.postalZip.value.match(ZIP_REGEXP) || F.postalZip.value.match(ZIP4_REGEXP)) && F.country.selectedIndex == 1 && F.stateProvince.selectedIndex >= 53) validateField(false, F.stateProvince.id, errors) // if state and country are us and zip is ca then fail else if (F.country.selectedIndex == 1 && (F.stateProvince.selectedIndex >= 1 && F.stateProvince.selectedIndex <= 52) && F.postalZip.value.match(CAPOSTAL_REGEXP)) validateField(false, F.postalZip.id, errors) // if state and zip are ca and country is us then fail else if (F.postalZip.value.match(CAPOSTAL_REGEXP) && F.stateProvince.selectedIndex >= 53 && F.country.selectedIndex == 1) validateField(false, F.country.id, errors) // if zip and country are ca and state is us then fail else if (F.postalZip.value.match(CAPOSTAL_REGEXP) && F.country.selectedIndex == 2 && (F.stateProvince.selectedIndex >= 1 && F.stateProvince.selectedIndex <= 52)) validateField(false, F.stateProvince.id, errors) // if state and country are ca and zip is us then fail else if (F.country.selectedIndex == 2 && F.stateProvince.selectedIndex >= 53 && (F.postalZip.value.match(ZIP_REGEXP) || F.postalZip.value.match(ZIP4_REGEXP))) validateField(false, F.postalZip.id, errors) // Must be at least 13 var MinAge = 13; var today = new Date(); var month = parseInt(today.getMonth()) + 1; var date = parseInt(today.getDate()); var fullyear = parseInt(today.getFullYear()); var selYear = parseInt(F.year.options[F.year.selectedIndex].value); var selMonth = parseInt(F.month.options[F.month.selectedIndex].value); var selDay = parseInt(F.day.options[F.day.selectedIndex].value); var daysInMonth = [31,isLeapYear(selYear)? 29: 28,31,30,31,30,31,31,30,31,30,31]; //alert(selYear +'>'+ fullyear +'-'+ MinAge +':'+ (selYear > fullyear-MinAge)) //alert(selYear +'>='+ fullyear +'-'+ MinAge +'&&'+ selMonth +'>'+ month +':'+ (selYear >= fullyear-MinAge && selMonth > month)) if (selYear.toString() == 'NaN' || selMonth.toString() == 'NaN' || selDay.toString() == 'NaN' || selYear > fullyear-MinAge || /* too young based on year alone */ (selYear >= fullyear-MinAge && selMonth > month) || /* too young cause they turn 13 this year, but not in this month */ (selYear >= fullyear-MinAge && selMonth >= month && selDay > date) || (selDay > daysInMonth[selMonth-1]) ) { // mark as error, too young validateField(false, F.day.id, errors); validateField(false, F.month.id, errors); validateField(false, F.year.id, errors); } else { // no error, age validated validateField(true, F.day.id, errors); validateField(true, F.month.id, errors); validateField(true, F.year.id, errors); } validateField(F.emailOptIn.checked, F.emailOptIn.id, errors); break; case 'unsub': validateField(F.emailAddress.value.match(EMAIL_REGEXP), F.emailAddress.id, errors); validateField(F.confirmEmail.value.match(EMAIL_REGEXP), F.confirmEmail.id, errors); validateField(F.emailAddress.value == F.confirmEmail.value, F.emailAddress.id, errors); validateField(F.confirmEmail.value == F.emailAddress.value, F.confirmEmail.id, errors); break; } // if any errors report them, otherwise send if (errors.length > 0) return indicateErrors(errors); else { $(errorBox).setStyle('visibility', 'hidden'); if (func) func(); } } //: validate() function getRadioValue(radio) { try { for (var i = 0, l = radio.length; i < l; i++) if (radio[i].checked) return radio[i].value; return ''; } catch (e) { } } //: getRadioValue() function fixStyled(forID) { // bug: "styled" gets changed into "tyled" and causes issues try { $(document).getElements('label[for='+ forID +']').each(function (t) { var x = $(forID); if (x.className.indexOf("tyled") >= 0 && x.className.indexOf("styled") < 0) { x.removeClass('tyled'); x.addClass('styled'); } }); } catch (e) { } } function validateField(fieldmatch, field, errors) { // push error field IDs into array or reset the fields class try { if (fieldmatch == false || fieldmatch == null || fieldmatch == '') errors.push(field); else { $(field).removeClass('error'); $(document).getElements('label[for='+ field +']').removeClass('error'); fixStyled(field); } } catch (e) { } } //: validateField() function indicateErrors(errors) { try { // add class 'error' to the field's label for (var i = 0, l = errors.length; i < l; i++) { var errID = errors[i]; var forID = errors[i]; if (errors[i].indexOf(':') > 0) { // in the event that the for is inaccurately set by the CMS wizards!! var ee = errors[i].split(':'); errID = ee[0]; forID = ee[1]; } if (errID.indexOf('country') > 0) errID = 'countrySelect'; // there's already a country ID on page $(errID).addClass('error'); $(document.body).getElements('label[for='+ forID +']').addClass('error'); fixStyled(forID); } var tagtype = $(errors[0]).getAttribute('type'); if (tagtype == '' || (tagtype != 'checkbox' && tagtype != 'radio')) $(errors[0]).focus(); $(errorBox).setStyle('visibility', 'visible'); return false; } catch (e) { } } //: indicateErrors() // UTILS String.prototype.trim = function (x) { if (!x) x = '\s'; return eval('this.replace(/^'+ x +'*/, "").replace(/'+ x +'*$/, "");') } function handlerKeyDownNetMoz(e) { try { if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit' && e.target.type != 'image') return false; else return true; } catch (e) { } } function handlerKeyDownMS() { try { if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'input' && event.srcElement.type != 'submit' && event.srcElement.type != 'image') return false; else return true; } catch (e) { } } function dbg(s) { try { var d = $('errorMessage') d.innerHTML = '$$'+ s; d.setStyle('visibility', 'visible'); } catch (e) { } }