/**
 * @filename stp-prepare.js
 * @date October 2008
 * @updated December 2008
 * @fileoverview Prepares HTML document and forms for STP. Writes hidden fields
 *    to document and sets cookies.
 */

/**
 * Templates for the STP box
 * Add another template if desired
 */

if(typeof(boxToDraw)!='undefined') {

var stpTemplate = [];

stpTemplate['default'] =
    '<div class="stp-content">' +
    '<form onsubmit="post();" id="stp-landing" action="#">' +
    '<input type="hidden" id="stp-encrypted-captcha-answer" value="" name="encrypted_captcha_answer"/>' +
    '<input type="hidden" value="ajax" id="stp-client" name="client"/>' +
    '<input type="hidden" value="" id="stp-from" name="from"/>' +
    '<input type="hidden" value="1" id="stp-c" name="c"/>' +
    '<input type="hidden" value="ec" id="stp-ec" name="ec"/>' +
    '<input type="hidden" value="'+gl+'" id="stp-gl" name="gl"/>' +
    '<input type="hidden" value="'+hl+'" id="stp-hl" name="hl"/>' +
    '<input type="hidden" value="{Source}" id="stp-source" name="source"/>' +
    '<input type="hidden" value="{Message}" id="stp-text" name="text"/>' +
    '<input type="hidden" value="'+hash+'" id="stp-hash" name="h"/>' +
    '<h3>'+title+'</h3>' +
    '<p>'+subTitle+'</p>' +
    '<span class="error" id="stp-error-general"></span>' +
    '<span class="error" id="stp-error-text-to-send"></span>' +
    '<span class="error" id="stp-error-mobile-user-id"></span>' +
    '<div class="stp-number"><input type="text" onclick="this.value=\'\'" value="'+defaultValue+'" size="30" name="mobile_user_id" id="stp-number" /></div>' +
    '<div style="display: none;" id="stp-carrier"><span class="error" id="stp-error-carrier"></span> @ <select id="stp-carriers" name="carrier"></select></div>' +
    '<div class="btn-flextoolbtn">' +
    '<div onclick="post();"><p><span><a href="#">'+buttonText+'</a></span></p></div>' +
    '</div>' +
    '<div style="display: none;" id="stp-captcha">' +
    '  <div style="display: none;" id="stp-captcha-error" class="error"></div>' +
    '  <img id="stp-captcha-image" alt="'+captchaAlt+'" src="#" />' +
    '  <br />' +
    '  <input type="text" id="stp-captcha-answer" name="user_captcha_answer"/> ' +
       captchaInstructions +
    '</div>' +
    '</form>' +
    '<div style="display: none;" id="stp-success">' +
    '  <h3>'+successTitle+'</h3>' +
       successText +
    '</div>' +
    '</div>';

stpTemplate['mgcnew'] =
    '<div class="stp-content">' +
    '<form onsubmit="post();" id="stp-landing" action="#">' +
    '<input type="hidden" id="stp-encrypted-captcha-answer" value="" name="encrypted_captcha_answer"/>' +
    '<input type="hidden" value="ajax" id="stp-client" name="client"/>' +
    '<input type="hidden" value="" id="stp-from" name="from"/>' +
    '<input type="hidden" value="1" id="stp-c" name="c"/>' +
    '<input type="hidden" value="ec" id="stp-ec" name="ec"/>' +
    '<input type="hidden" value="'+gl+'" id="stp-gl" name="gl"/>' +
    '<input type="hidden" value="'+hl+'" id="stp-hl" name="hl"/>' +
    '<input type="hidden" value="{Source}" id="stp-source" name="source"/>' +
    '<input type="hidden" value="{Message}" id="stp-text" name="text"/>' +
    '<input type="hidden" value="'+hash+'" id="stp-hash" name="h"/>' +
    '<h3 class="short">'+title+'</h3>' +
    '<p>'+subTitle+'</p>' +
    '<span class="error" id="stp-error-general"></span>' +
    '<span class="error" id="stp-error-text-to-send"></span>' +
    '<span class="error" id="stp-error-mobile-user-id"></span>' +
    '<div class="stp-number"><input type="text" onclick="this.value=\'\'; this.onclick=\'\'" value="'+defaultValue+'" size="30" name="mobile_user_id" id="stp-number" /></div>' +
    '<div style="display: none;" id="stp-carrier"><span class="error" id="stp-error-carrier"></span> @ <select id="stp-carriers" name="carrier"></select></div>' +
    '<div class="g-button">' +
    '<div onclick="post();"><span><span><a href="javascript:{}">'+buttonText+'</a></span></span></div>' +
    '</div>' +
    '<div class="clearer"></div>' +
    '<div style="display: none;" id="stp-captcha">' +
    '  <div style="display: none;" id="stp-captcha-error" class="error"></div>' +
    '  <img id="stp-captcha-image" alt="'+captchaAlt+'" src="#" />' +
    '  <br />' +
    '  <input type="text" id="stp-captcha-answer" name="user_captcha_answer"/> ' +
       captchaInstructions +
    '</div>' +
    '</form>' +
    '<div style="display: none;" id="stp-success">' +
    '  <h3>'+successTitle+'</h3>' +
       successText +
    '</div>' +
    '</div>';

stpTemplate['btn-exp-hover'] =
    '<div class="stp-content">' +
    '<form onsubmit="post();" id="stp-landing" action="#">' +
    '<input type="hidden" id="stp-encrypted-captcha-answer" value="" name="encrypted_captcha_answer"/>' +
    '<input type="hidden" value="ajax" id="stp-client" name="client"/>' +
    '<input type="hidden" value="" id="stp-from" name="from"/>' +
    '<input type="hidden" value="1" id="stp-c" name="c"/>' +
    '<input type="hidden" value="ec" id="stp-ec" name="ec"/>' +
    '<input type="hidden" value="'+gl+'" id="stp-gl" name="gl"/>' +
    '<input type="hidden" value="'+hl+'" id="stp-hl" name="hl"/>' +
    '<input type="hidden" value="{Source}" id="stp-source" name="source"/>' +
    '<input type="hidden" value="{Message}" id="stp-text" name="text"/>' +
    '<input type="hidden" value="'+hash+'" id="stp-hash" name="h"/>' +
    '<h3 class="short">'+title+'</h3>' +
    '<p>'+subTitle+'</p>' +
    '<span class="error" id="stp-error-general"></span>' +
    '<span class="error" id="stp-error-text-to-send"></span>' +
    '<span class="error" id="stp-error-mobile-user-id"></span>' +
    '<div class="stp-number"><input type="text" onclick="this.value=\'\'; this.onclick=\'\'" value="'+defaultValue+'" size="30" name="mobile_user_id" id="stp-number" /></div>' +
    '<div style="display: none;" id="stp-carrier"><span class="error" id="stp-error-carrier"></span> @ <select id="stp-carriers" name="carrier"></select></div>' +
    '<div class="g-button g-button-hover">' +
    '<div onclick="post();"><span><span><a href="javascript:{}">'+buttonText+'</a></span></span></div>' +
    '</div>' +
    '<div class="clearer"></div>' +
    '<div style="display: none;" id="stp-captcha">' +
    '  <div style="display: none;" id="stp-captcha-error" class="error"></div>' +
    '  <img id="stp-captcha-image" alt="'+captchaAlt+'" src="#" />' +
    '  <br />' +
    '  <input type="text" id="stp-captcha-answer" name="user_captcha_answer"/> ' +
       captchaInstructions +
    '</div>' +
    '</form>' +
    '<div style="display: none;" id="stp-success">' +
    '  <h3>'+successTitle+'</h3>' +
       successText +
    '</div>' +
    '</div>';

stpTemplate['btn-exp-rhs'] =
    '<div class="stp-content">' +
    '<form onsubmit="post();" id="stp-landing" action="#">' +
    '<input type="hidden" id="stp-encrypted-captcha-answer" value="" name="encrypted_captcha_answer"/>' +
    '<input type="hidden" value="ajax" id="stp-client" name="client"/>' +
    '<input type="hidden" value="" id="stp-from" name="from"/>' +
    '<input type="hidden" value="1" id="stp-c" name="c"/>' +
    '<input type="hidden" value="ec" id="stp-ec" name="ec"/>' +
    '<input type="hidden" value="'+gl+'" id="stp-gl" name="gl"/>' +
    '<input type="hidden" value="'+hl+'" id="stp-hl" name="hl"/>' +
    '<input type="hidden" value="{Source}" id="stp-source" name="source"/>' +
    '<input type="hidden" value="{Message}" id="stp-text" name="text"/>' +
    '<input type="hidden" value="'+hash+'" id="stp-hash" name="h"/>' +
    '<h3 class="short">'+title+'</h3>' +
    '<p>'+subTitle+'</p>' +
    '<span class="error" id="stp-error-general"></span>' +
    '<span class="error" id="stp-error-text-to-send"></span>' +
    '<span class="error" id="stp-error-mobile-user-id"></span>' +
    '<div class="stp-number"><input style="width:160px; float:left;" type="text" onclick="this.value=\'\'; this.onclick=\'\'" value="'+defaultValue+'" size="15" name="mobile_user_id" id="stp-number" />' +
    '<div class="g-button g-button-hover" style="width:160px;float:right;">' +
    '<div onclick="post();"><span><span><a href="javascript:{}">'+buttonText+'</a></span></span></div>' +
    '</div> </div>' +
    '<div style="display: none;" id="stp-carrier"><span class="error" id="stp-error-carrier"></span> @ <select id="stp-carriers" name="carrier"></select></div>' +
    '<div class="clearer"></div>' +
    '<div style="display: none;" id="stp-captcha">' +
    '  <div style="display: none;" id="stp-captcha-error" class="error"></div>' +
    '  <img id="stp-captcha-image" alt="'+captchaAlt+'" src="#" />' +
    '  <br />' +
    '  <input type="text" id="stp-captcha-answer" name="user_captcha_answer"/> ' +
       captchaInstructions +
    '</div>' +
    '</form>' +
    '<div style="display: none;" id="stp-success">' +
    '  <h3>'+successTitle+'</h3>' +
       successText +
    '</div>' +
    '</div>';

}

/**
 * Draws the default STP form for MGC pages
 */

function drawStpForm(){
  var dc = getDc(pageDc);
  var ReadyText = stpMessage.replace("[dc]", dc);
  var ReadySource = location.pathname;
  ReadySource = ReadySource.replace("/mobile/","mobile-web/");
  if(ReadySource.indexOf(".html") == -1)
    ReadySource += "index/"+dc;
  else
    ReadySource = ReadySource.replace(".html","/"+dc);
  var textToWrite = stpTemplate[templateName].replace("{Message}",ReadyText);
  var textToWrite = textToWrite.replace("{Source}",ReadySource);
  boxId = document.getElementById(boxToDraw);
  boxId.style.height = "auto";
  boxId.innerHTML = textToWrite;
}

/**
 * Returns DC from cookie, if it exists
 * If DC is set in hash, returns DC from hash and sets cookie
 * If DC is not set in hash, returns DC specified on the page
 */

function getDc(defaultDc) {
  var dc = getDcFromCookie();
  if (dc) {
    return dc;
  }
  var hash = document.location.hash;
  if (hash.length) {
    var regex = new RegExp('dc=([^&]*)');
    var results = regex.exec(hash);
    if (results != null) {
      dc = results[1];
    } else {
      return defaultDc;
    }
  } else {
    return defaultDc;
  }

  // Set DC in cookie because it was explicitly declared in the hash
  var expiry = new Date();
  expiry.setTime(expiry.getTime() + (24*60*60*1000));
  document.cookie = 'dc=' + dc + ';expires=' + expiry.toGMTString() + ';path=' + getDcPath() + ';';
  return dc;
}

/**
 * Returns the DC entry point from a cookie, if it exists
 */

function initialiseDc(defaultDc) {
  if (!getDcFromCookie()) {
    var hash=document.location.hash;
    var dcIndex = hash.indexOf('dc=');
    if (dcIndex < 0) {
      var dc = defaultDc;
    } else {
      var endDcIndex = hash.indexOf('&', dcIndex);
      if (endDcIndex < 0) {
        dc = hash.substring(dcIndex + 3);
      } else {
        dc = hash.substring(dcIndex + 3, endDcIndex);
      }
    }
    var expiry = new Date();
    expiry.setTime(expiry.getTime() + (24*60*60*1000));
    document.cookie = 'dc=' + dc + ';expires=' + expiry.toGMTString() + ';path=' + getDcPath() + ';';
  }
}

function getDcFromCookie() {
  var allcookies = document.cookie;
  var dcParamIndex = allcookies.indexOf('dc=');
  if (dcParamIndex != -1) {
    var dcValueIndex = dcParamIndex + 3;
    var dcValueEnd = allcookies.indexOf(';', dcValueIndex);
    if (dcValueEnd == -1) dcValueEnd = allcookies.length;
    return allcookies.substring(dcValueIndex, dcValueEnd);
  }
  return null;
}

function getDcPath() {
  var path = document.location.pathname;
  var mobileIndex = path.indexOf('mobile');
  if (mobileIndex > 0) {
    path = path.substring(0, mobileIndex + 'mobile'.length);
  } else {
    path = path.substring(0, path.lastIndexOf('/'));
  }
  return path;
}

/**
 * Interacts with the /sendtophone web service
 */

function Handler() {
  this.delegate = new DefaultSTPResponseHandler();
}

Handler.prototype.onError = function(a,b,c) {
  this.delegate.onError(a,b,c);
  mgcTrack._trackEvent('SendToPhone', 'Error | ' + a, document.location.pathname);
}

Handler.prototype.onLanding = function(a,b) {
  this.delegate.onLanding(a,b);
}

Handler.prototype.onSubmit = function() {
  this.delegate.onSubmit();
}

Handler.prototype.onSuccess = function() {
  this.delegate.onSuccess();
  mgcTrack._trackEvent('SendToPhone', 'Message Sent', document.location.pathname);
}

var stp;
window.onload = function() {
  stp = new SendToPhoneClient("/sendtophone", new Handler());
}

/**
 * Include the STP Ajax Client
 */

document.write('<script charset="utf-8" type="text/javascript" src="'
  + '/sendtophone/js/ajax_client.js"></scr' + 'ipt>');

function post() {
  return stp.post();
}

