//
// 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(){
return document.SubmitForm.short_message.value.length;
}
function clearSubmitForm(){
document.SubmitForm.reset();
//document.SubmitForm.char_count.value = MAX_COUNT;
$('.charsLeft').html(MAX_COUNT);
}
function clearStatusForm(){
document.StatusForm.reset();
}
function updateCharLeft(){
//document.SubmitForm.char_count.value = MAX_COUNT - getCurrentCount();
var counter = MAX_COUNT - getCurrentCount();
$('.charCountDownMaxJs').html(counter);
if (counter <= 0) {
//$('#errorMessages').empty();
//$('.warning').hide();
$('.jsCountingChar').css({'color':'#AA0000','font-weight':'700'});
//$('.warning').show();
//$('#errorMessages').append('
' + MAX_CHARACTERS + MAX_COUNT+'
'+CURRENT_CHARACTERS + getCurrentCount()+'');
} else {
$('.jsCountingChar').css({'color':'#555555','font-weight':'500'});
//$('#errorMessages').empty();
//$('.warning').hide();
}
return true;
}
function checkLength(ctrl) {
if (ctrl.value.length>=MAX_COUNT) {
ctrl.value = ctrl.value.substring(0,MAX_COUNT);
}
}
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());
$('#reply_to').removeAttr("maxlength");
$('#reply_to').val('');
updateCharLeft();
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);
$('#reply_to').attr("maxlength","50");
$('#reply_to').val('');
updateCharLeft();
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 )
{
var phoneRegex = /^\d{10}$/;
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 )
if (!addresses[i].match(phoneRegex)){
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 emailRegex = /^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$/;
address = address.trim();
if (address.match(emailRegex)) {
return true;
} else {
return false;
}
}
function resetForm() {
clearErrorFields();
$('#short_message').val("");
$('#destination_address').val("");
$('#text_captcha').val("");
updateCharLeft();
}
function clearErrorFields() {
$('#destAdd').removeClass("frmError");
$('#textmessage').removeClass("frmError");
$('#captcha').removeClass("frmError");
$('#errorMessages').empty();
$('.warning').hide();
$('#confirmation').hide();
}
//
// Validate the SubmitForm
//
function validateSubmitForm()
{
var alertMsg = "";
var retVal;
var len;
clearErrorFields();
retVal = validateDestinationAddress( document.SubmitForm.destination_address.value );
if( retVal == -1 ){
//alertMsg += "\n" + SEND_INVALID_NUMBER
alertMsg += "" + SEND_INVALID_NUMBER + "";
$('#destAdd').addClass("frmError");
}
if( retVal == -2 ){
//alertMsg += "\n" + SEND_INVALID_MAX_NUMBER;
alertMsg += "" + SEND_INVALID_MAX_NUMBER + "";
$('#destAdd').addClass("frmError");
}
if( retVal == -3 ){
alertMsg += "" + SEND_REQUIRED + "";
$('#destAdd').addClass("frmError");
//alertMsg += "\n" + SEND_REQUIRED;
}
if( retVal == -4 ){
alertMsg += "" + SEND_DUPLICATE + "";
$('#destAdd').addClass("frmError");
//alertMsg += "\n" + SEND_DUPLICATE;
}
if( document.SubmitForm.short_message.value == "") {
alertMsg += "" + MESSAGE_REQUIRED + "";
$('#textmessage').addClass("frmError");
//alertMsg += "\n" + MESSAGE_REQUIRED;
}
len = getCurrentCount();
if( len > MAX_COUNT ){
alertMsg += "" + MAX_CHARACTERS + MAX_COUNT;
alertMsg += "
"+ CURRENT_CHARACTERS + len + "";
}
if( document.SubmitForm.text_captcha.value == "") {
alertMsg += "" + VALIDATION_REQUIRED + "";
$('#captcha').addClass("frmError");
//alertMsg += "\n" + VALIDATION_REQUIRED;
}
if (alertMsg != "") {
//alert( alertMsg );
$('.warning').show();
$('#errorMessages').append(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);
}