// ------------------------------------------------------------------------------------------------
// JavaScript version 1.3.6 (c) Mobile Innovation AS
// Matches AdServer v.1.2
// Customised for client : < generic >
// Customisation date :
//
// Version control :
// v.1.3.6, 19.02.2009 : FRK
// 	Split original script into two files one .HTML and one .JS to enable changing of code independently. Functions requiring access to 
//  document.getElementById("").value have been moved to the HTML file. Parameters have been introduced to the SendSMS function.
//
// v.1.3.5.4, 17.02.2009 : FRK
// 	Corrected bug introduced - when using script and the MI_ClientMemberID has been assigned a value. It did not keep the value
//
// v.1.3.5, 16.02.2009 : FRK
// 	Removed unneccessary code introduced by last change. Corrected lacking display of input field for registered members introduced by last change.
//
// v.1.3.4, 12 - 02 - 2009 : Hitesh Rathod, DSS
//  	Changed URL for adding MI_Member_Registration_Mode attribute
// 	Added country code to memberID for anonymous registration in AssignMemberID
//
// v.1.3, jan / feb 2009 : FRK :
// 	Functionality added : MI_Member_Registration_Mode added as parameter the to "orderMembershipCardAction.do" to enable insertions of new members based
// 	on membership_ids entered on the web - site. This is required in order to allow for direct web - based ordering of membership cards, e.g. www.Dogs - Bollox.com
//
// v.1.2, nov / dec 2008 : Frode R. Karlsen, Mobile Innovation AS. Modified
// v.1.1, mar 2008 : Vinit Javieri, Direction Software Solutions. Modified
// v.1.0, dec 2007 : NN, developed
// ------------------------------------------------------------------------------------------------

var EchoDebugInfo = false;

// ------------------------------------------------------------------------------------------------
//  OperatingEnvironment:
//  The variable is used for determining which environment is being used to 
//  execute the SendSMS function. This is used only for debugging and testing.
//
//  Possible values are:
//      "DEV" for Development environment
//      "UAT" for UAT environment and 
//      "PROD" for production environment
//
//  If this variable is changed the client organisation will not get the service that is configured.
// ------------------------------------------------------------------------------------------------
var OperatingEnvironment = "PROD";

// --- Development, DSS, Mumbai :
var DEVOrderMembershipCardURL = "http://59.163.21.140:8080/intelligentad12/orderMembershipCardAction.do?";
// --- UAT, MIServer, Oslo / Fredrikstad :
var UATOrderMembershipCardURL = "http://84.48.216.159:8080/intelligentad12/orderMembershipCardAction.do?";
// --- Production, Manu, Fredrikstad :
var PRODOrderMembershipCardURL = "http://www.mobileinnovation.no:8080/intelligentad/orderMembershipCardAction.do?";

// ------------------------------------------------------------------------------------------------
//  MI_Member_Registration_Mode:
// 		This parameter determines if the MI_ClientMemberID shall be found (looked up) or inserted (if not found) in the
// 		AdServer DB :
//
// 		Value                  Description
// 		------                 -----------------------------------------------------------------------------
// 		"MemberIsRegistered"   MI_ClientMemberIDs are registered in the AdServer.
// 		"MemberIsUnknown"      All members requesting a SMS w/URL are anonymous. 
//
//  The value is set in the HTML script and is transferred to the SendSMS as the parameter "MI_Member_Registration_Mode"
// ------------------------------------------------------------------------------------------------
var MemberIsRegistered = "MemberIsRegistered";
var MemberIsUnknown = "MemberIsUnknown";

function PrintDebugInfo( debug, text ) {
    if ( debug ){
        alert( text );
    }
}

function SendSMS( MI_ClientNo, MI_ClientMemberID, inputClientMemberId, CountryCode, miMSISDN, TCAccepted, MI_Member_Registration_Mode ){

    var validCountryCode = false;
    var validMobileNo = false;
    var urlStr = "";
    var phoneNumber = {
      countryCode : '', mobileNo : ''
    };
    var sendSMSConfirmation = false;
    var clientMemberId = '';
    var termsAccepted = false;
    var DEBUG = false;

    DEBUG = EchoDebugInfo;
    termsAccepted = TCAccepted;
    
   if ( ! termsAccepted) {
      alert( "No SMS will be sent." );
   }
   else {
        PrintDebugInfo( DEBUG, "SendSMS (Input parameters): MI_ClientNo="+MI_ClientNo+". MI_ClientMemberID="+MI_ClientMemberID+". inputClientMemberId="+inputClientMemberId+". CountryCode="+CountryCode+". miMSISDN="+miMSISDN+". TCAccepted="+TCAccepted+". MI_Member_Registration_Mode="+MI_Member_Registration_Mode);
        
        switch (OperatingEnvironment) {
            case "DEV":
                urlStr = DEVOrderMembershipCardURL;
                break;
            case "UAT":
                urlStr = UATOrderMembershipCardURL;
                break;
            default:
                urlStr = PRODOrderMembershipCardURL;
        }

      PrintDebugInfo( DEBUG, "SendSMS (In): OperatingEnvironment="+OperatingEnvironment+". urlStr="+urlStr );

      phoneNumber.countryCode = CountryCode;
      FormatCountryCode( DEBUG, phoneNumber );
      validCountryCode = ValidateCountryCode( DEBUG, phoneNumber );

      phoneNumber.mobileNo = miMSISDN;
      FormatMobileNumber( DEBUG, phoneNumber );
      validMobileNo = ValidateMobileNumber( DEBUG, phoneNumber );

      clientMemberId = AssignClientMemberID( DEBUG, MI_ClientMemberID, inputClientMemberId, CountryCode, phoneNumber.mobileNo, MI_Member_Registration_Mode );

      if ( ! validCountryCode){
         alert( "The country code - " + phoneNumber.countryCode + " - is not valid. Please correct and try again." );
      }
      else if ( ! validMobileNo) {
         alert( "The mobile number - " + phoneNumber.mobileNo + " - is not valid. Please correct and try again." );
      }
      else {
         urlStr = urlStr+"CountryCode="+phoneNumber.countryCode+"&MI_MobileNumber="+phoneNumber.mobileNo;
         urlStr = urlStr+"&MI_ClientNo="+MI_ClientNo+"&MI_ClientMemberID="+clientMemberId+"&Accepted_TCs="+termsAccepted;
         urlStr = urlStr+"&MI_Member_Registration_Mode="+MI_Member_Registration_Mode;
         PrintDebugInfo( DEBUG, urlStr );
         sendSMSConfirmation = confirm( "The SMS vill be sent to: "+phoneNumber.countryCode+"-"+phoneNumber.mobileNo );
         if (sendSMSConfirmation){
            window.open( urlStr, 'OrderMembershipCard', 'width=800,height=50,titlebar=0' );
         } 
         else{
            alert("No SMS will be sent.");
         }
      }
   }
} // END SendSMS

function FormatCountryCode( DEBUG, phoneNo ){

   var str0 = '';
   var ch0 = '';
   var i0 = 0;
   var n0 = 0;
   var firstNonZeroNumberFound = false;
   var ValidChars = "123456789";

    PrintDebugInfo( DEBUG, "FormatCountryCode (Entry): countryCode = " + phoneNo.countryCode );

   // remove all non - numeric characters
   for (i0 = 0; i0 < phoneNo.countryCode.length; i0 ++ ){
      ch0 = phoneNo.countryCode.charAt(i0);
      if ((ValidChars.indexOf(ch0) != - 1) || (ch0 == '0')) {
         str0 += ch0;
      }
   }

   // remove leading zeroes and replace them with '+'
   firstNonZeroNumberFound = false;
   for (i0 = 0; ! firstNonZeroNumberFound; i0 ++ ){
      ch0 = str0.charAt(i0);
      firstNonZeroNumberFound = (ValidChars.indexOf(ch0) != - 1);
   }
   n0 = i0 - 1;
   phoneNo.countryCode = '+';
   for (i0 = n0; (i0 < str0.length); i0 ++ ){
      ch0 = str0.charAt(i0);
      phoneNo.countryCode += ch0;
   }
    PrintDebugInfo( DEBUG, "FormatCountryCode (Exit): countryCode = " + phoneNo.countryCode );

} //  FormatCountryCode


function ValidateCountryCode ( DEBUG, phoneNo ) {
   // Currently only verifying correct format, i.e. '+nnn'

   var validCountryCode = false;
   var MaxLenghtOfValidCountryCode = 3;
   var ch0 = '';
   var i0 = 0;
   var ValidChars = "+0123456789";

    PrintDebugInfo( DEBUG, "ValidateCountryCode (Entry): CountryCode= " + phoneNo.countryCode + ", Valid= " + validCountryCode );
    
    validCountryCode = phoneNo.countryCode.length <= MaxLenghtOfValidCountryCode;
   if (validCountryCode) {
      for (i0 = 0; (validCountryCode && (i0 < phoneNo.countryCode.length)); i0 ++ ){
         ch0 = phoneNo.countryCode.charAt(i0);
         validCountryCode = (ValidChars.indexOf(ch0) != - 1);
      }
   }
    PrintDebugInfo( DEBUG, "ValidateCountryCode (Exit): CountryCode=" + phoneNo.countryCode + ", Valid= " + validCountryCode );
   
   return validCountryCode;
   
} // ValidateCountryCode

function FormatMobileNumber( DEBUG, phoneNo ) {

   var str0 = '';
   var str1 = '';
   var ch0 = '';
   var i0 = 0;
   var ValidChars = "123456789";
   var noOfLeadingZeros = 0;
   var leadingZero = true;

    PrintDebugInfo( DEBUG, "FormatMobileNumber (Entry): mobileNo= " + phoneNo.mobileNo );
   // remove all non - numeric characters
   for (i0 = 0; i0 < phoneNo.mobileNo.length; i0 ++ ){
      ch0 = phoneNo.mobileNo.charAt(i0);
      if ((ValidChars.indexOf(ch0) != - 1) || (ch0 == '0')) {
         str0 += ch0;
      }
   }

   // find the number of leading zeros :
   for (noOfLeadingZeros = 0; ((noOfLeadingZeros < str0.length) && (leadingZero));
   noOfLeadingZeros ++ ){
      leadingZero = str0.charAt(noOfLeadingZeros) == '0';
   }
   noOfLeadingZeros = noOfLeadingZeros - 1;

   // Copy the rest of the number after leading zeros
   for (i0 = 0; i0 < str0.length - noOfLeadingZeros; i0 ++ ){
      str1 += str0.charAt(i0 + noOfLeadingZeros);
   }

   phoneNo.mobileNo = str1;
   PrintDebugInfo( DEBUG, "FormatMobileNumber (Exit): mobileNo= " + phoneNo.mobileNo );
   
}// FormatMobileNumber

function ValidateMobileNumber ( DEBUG, phoneNo ) {
   // Currently only validating that the number provided is numeric

   var i0 = 0;
   var ch0 = '';
   var ValidChars = "0123456789";
   var numericNo = true;

   // Check numeric number
   for (i0 = 0; numericNo && (i0 < phoneNo.mobileNo.length); i0 ++ ){
      ch0 = phoneNo.mobileNo.charAt(i0);
      numericNo = (ValidChars.indexOf(ch0) != - 1);
   }
    PrintDebugInfo( DEBUG, "ValidateMobileNumber (Entry): Mobile number= " + phoneNo.mobileNo + ", Valid= " + numericNo );
   return numericNo;
   
} // ValidateMobileNumber

function AssignClientMemberID( DEBUG, memberID_GlobalVariable, memberID_InputField, countryCode, validMSISDN, Update_Command ) {

    var clientMemberId = '';
	var positiveSign = '+';
	var firstChar = countryCode.substr(0,1);

      PrintDebugInfo( DEBUG, "AssignClientMemberID (Entry): memberID_GlobalVariable= " +memberID_GlobalVariable+"; memberID_InputField=" + memberID_InputField );
    if ( Update_Command == MemberIsRegistered ) {
        clientMemberId = memberID_GlobalVariable;
        if( clientMemberId.length == 0) {
	       clientMemberId = memberID_InputField;
        }
    } else if ( Update_Command == MemberIsUnknown ) {
        firstChar = countryCode.substr(0, 1);
        if (firstChar == positiveSign) {
            countryCode = countryCode.substr(1, countryCode.length);
        }
        clientMemberId = countryCode + validMSISDN;
    }

    PrintDebugInfo( DEBUG, "AssignClientMemberID (Exit): clientMemberId="+clientMemberId+". CountryCode="+countryCode+". validMSISDN= "+validMSISDN+". Update_Command="+Update_Command);

    return clientMemberId;

} // END AssignClientMemberID



