// // BMG JavaScript to ensure valid information is sent to the BMG. // // Content: Validate Submit Form - Destination Address, Reply to and SMS text. // Ensure complete message is not over MAX_COUNT characters. // Validate Status Form - Destination Address and Message Id // // Copyright (c) 2003, Schlumberger Private (http://legal.slb.com). // function strLTrim() { return this.replace(/^\s+/,''); } function strRTrim() { return this.replace(/\s+$/,''); } function strTrim() { return this.replace(/^\s+/,'').replace(/\s+$/,''); } // // Strip off all whitespaces // function strStripWhitespaces() { return this.replace(/\s+/g,''); } String.prototype.lTrim = strLTrim; String.prototype.rTrim = strRTrim; String.prototype.trim = strTrim; String.prototype.stripWhitespaces = strStripWhitespaces; // Character count for text message and email address only function getCurrentCount(){ if ( document.SubmitForm.reply_to_choice[1].checked ){ return document.SubmitForm.short_message.value.length + document.SubmitForm.reply_to.value.length + document.SubmitForm.sent_by.value.length; } else { return document.SubmitForm.short_message.value.length + document.SubmitForm.sent_by.value.length; } } function clearSubmitForm(){ document.SubmitForm.reset(); document.SubmitForm.char_count.value = MAX_COUNT; } function clearStatusForm(){ document.StatusForm.reset(); } function updateCharLeft(){ document.SubmitForm.char_count.value = MAX_COUNT - getCurrentCount(); return true; } function updateCharCountPress( field ) { var len = 0; len = getCurrentCount(); if (len > MAX_COUNT){ alert(MAX_CHARACTERS + MAX_COUNT + ".\n" + CURRENT_CHARACTERS + len); field.value = field.value.substr(0, field.value.length + MAX_COUNT - len); if ( field.value.charCodeAt(field.value.length - 2) == 13){ field.value = field.value.substr(0, field.value.length - 2); } document.SubmitForm.char_count.value = MAX_COUNT - getCurrentCount(); document.SubmitForm.char_count.focus(); } else{ document.SubmitForm.char_count.value=(MAX_COUNT-len); } } function clickReplyToChoiceSms(){ document.SubmitForm.char_count.value=(MAX_COUNT-getCurrentCount()); return true; } function clickReplyToChoiceEmail(){ var len = 0; len = getCurrentCount(); if (len > MAX_COUNT){ alert(MAX_CHARACTERS + MAX_COUNT + ".\n" + CURRENT_CHARACTERS + len); document.SubmitForm.reply_to_choice[0].checked = true; return false; } document.SubmitForm.char_count.value=(MAX_COUNT-len); return true; } // // Check that val is a digit string with length between nMinLength and nMaxLength. // function isMIN( val, nMinLength, nMaxLength ) { var code; if (val == null || val.length == 0) return -3; val = val.stripWhitespaces(); var len = val.length; if( len < nMinLength || len > nMaxLength ){ return -1; } for( var i = 0; i < len; ++i ){ code = val.charCodeAt(i); if( code < 48 || code > 57 ){ //Numbers must contain only digits return -2; } } return 0; } // // Check that val is a digit string. // function isDigit( val ) { var code; var len = val.length; for( var i = 0; i < len; ++i ){ code = val.charCodeAt(i); if( code < 48 || code > 57 ){ return false; } } return true; } // // Check for 10 MIN at most // function validateDestinationAddress( value ) { if( value.length == 0 ) return -3; value = value.stripWhitespaces(); var addresses = value.split( "," ); var len = addresses.length; if( len > 10 ){ return -2; } addresses = addresses.sort(); for( var i = 0; i < len; ++i ) { if( isMIN( addresses[i], MIN_LENGTH, MIN_LENGTH ) != 0 ) { return -1; } if (i > 0) { if ( addresses[i-1] == addresses[i] ) { return -4; //duplicate } } } return 0; } // // Check if address is a valid email address; see RFC 821 and 822 // Does not support the quoted string for the local part, e.g. "john doe"@company.com // Does not support the IP address for the domain part, e.g. john.doe@[123.123.123.123] // function isValidEmailAddress ( address ) { var pos; var specialChars = " <>|()[]\\,;:\""; var user; var domain; address = address.trim(); pos = address.indexOf('@'); if ( pos == -1 ) return false; //more than one @ if ( address.indexOf('@', pos+1) != -1) return false; for ( i=0; i < address.length; i++) { if ( address.charCodeAt(i) < 32 || address.charCodeAt(i) == 127 || address.charCodeAt(i) > 255 ) { return false; } } for ( i=0; i < specialChars.length; i++ ) { if ( address.indexOf(specialChars.charAt(i), 0) != -1 ) { return false; } } user = address.substr(0, pos); domain = address.substr (pos+1, address.length-pos); if (user == null || user == "") return false; if (domain == null || domain == "") return false; if ( user.indexOf('.') == 0 || (user.indexOf('.') == user.length-1) || user.indexOf('..') != -1 ) return false; if ( domain.indexOf('.') == 0 || (domain.indexOf('.') == domain.length-1) || domain.indexOf('..') != -1 ) return false; //domain at least one dot if ( domain.indexOf('.') == -1 ) return false; return true; } // // Validate the SubmitForm // function validateSubmitForm() { var alertMsg = ""; var retVal; var len; retVal = validateDestinationAddress( document.SubmitForm.destination_address.value ); if( retVal == -1 ){ alertMsg += "\n" + SEND_INVALID_NUMBER } if( retVal == -2 ){ alertMsg += "\n" + SEND_INVALID_MAX_NUMBER; } if( retVal == -3 ){ alertMsg += "\n" + SEND_REQUIRED; } if( retVal == -4 ){ alertMsg += "\n" + SEND_DUPLICATE; } if ( document.SubmitForm.reply_to_choice[1].checked ){ if( document.SubmitForm.reply_to != null && document.SubmitForm.reply_to.value != "" ){ if ( !isValidEmailAddress ( document.SubmitForm.reply_to.value ) ) { alertMsg += "\n" + EMAIL_INVALID; } } } else { retVal = isMIN( document.SubmitForm.reply_to.value, MIN_EX_MIN_LENGTH, MIN_EX_MAX_LENGTH ); if( retVal == -2 ){ alertMsg += "\n" + REPLY_INVALID_DIGIT; } if( retVal == -1 ){ alertMsg += "\n" + REPLY_INVALID_LENGTH; } } if( document.SubmitForm.short_message.value == "") { alertMsg += "\n" + MESSAGE_REQUIRED; } len = getCurrentCount(); if( len > MAX_COUNT ){ alertMsg += "\n" + MAX_CHARACTERS + MAX_COUNT + ".\n" + CURRENT_CHARACTERS + len; } if( document.SubmitForm.text_captcha.value == "") { alertMsg += "\n" + VALIDATION_REQUIRED; } if (alertMsg != "") { alert( alertMsg ); return false; } else { return true; } } // // Validate the StatusForm // function validateStatusForm() { var alertMsg = ""; var retVal; if( document.StatusForm.message_id.value == "" ){ alertMsg += "\n" + MESSAGE_ID_REQUIRED; } if( !isDigit (document.StatusForm.message_id.value) ){ alertMsg += "\n" + MESSAGE_ID_INVALID_DIGIT; } if( document.StatusForm.destination_address.value == "" ){ alertMsg += "\n" + MOBILE_REQUIRED; } else { retVal = isMIN( document.StatusForm.destination_address.value, MIN_LENGTH, MIN_LENGTH); if( retVal == -2 ){ alertMsg += "\n" + MOBILE_INVALID_DIGIT; } if( retVal == -1 ){ alertMsg += "\n" + MOBILE_INVALID_LENGTH; } } if (alertMsg != "") { alert( alertMsg ); return false; } else { return true; } } // // Bell.ca JavaScript to pop a new window // Taken from /jsp/content/personal/catalog/wireless/data/wireless_email/applications/epage.jsp // function popper(thisUrl,thisWindow,thisWidth,thisHeight,thisTop,thisLeft) { optionString = ('width=' + thisWidth + ',height=' + thisHeight + ',top=' + thisTop + ',left=' + thisLeft + ',status=no,menubar=no,resizable=no,scrollbars=no'); mainWin = window.open(thisUrl,thisWindow,optionString); }