﻿// JavaScript Document
var noSend = 0;
var MESGMAINCAT="Special Occasions";
function occurrence(str, ch) {
  
  var iOccurrence = 0;

  for (i=0; i<str.length; i++)
    if (str.charAt(i)==ch)
      iOccurrence++;
    return iOccurrence;
}


function funOnReset() {
  noSend=0
  location.reload();
}

function funAddMsg() {
  for (var i=0; i<document.form.MESSAGE.length; i++){
    if (document.form.MESSAGE.options[i].selected == true){
      document.form.MSG.value+=document.form.MESSAGE.options[i].value;
      document.form.char_left.focus();
      document.form.MSG.focus();
      start_counter(this.form);
    }
  }
}

function funOnReload() {
  funToMsgCat(MESGMAINCAT);
}

function funToMsgCat(instr) {
  document.form.MESGMAINCAT.value=instr;
  document.form.action="/nwm/websms/sms_composer.jsp";
  document.form.submit();
}

function funCheckOpenScn() {
  var prompt;
  prompt="initbox";
  if (prompt == "") {
    window.location = "/nwm/websms/sms_outbox.jsp?MSISDN=&LANG=C&ACCESS_KEY=null&CATEGORY=COMM&MENUTYPE=SENDSMS&SERVICE_TYPE=SMSOUTBOX";
  } else {
    initbox();
  }
}

function ToEcardSelect() {
  document.form.action="/cgi-bin/nwm/websms/ecmsg?TYPE=SELECT";
  var tmpstr=document.form.MSG.value+"";
  document.form.MESSAGE.value=tmpstr.replace(/(\r\n)+/g,"\\n");
	
  if(document.form.TYPE.value != "SMS")	{
    if (confirm("You have already selected an E-card. Do you want to replace the previous one?"))
      document.form.submit();	
  }
  else
    document.form.submit();
}

var f;
var counting=false;
var start_time,now_time,timer_id=null;
var MESSAGE,escape_msg,escape_msg1;
var i,max_len,no_of_Eng_Char,no_of_Chi_Char,total_no_of_char,chi_position;
var max_eng_char=100,max_chi_char=40;

var error_msg="每個訊息最多為100個英文字/數字或40個中文字/數字";


function update_counter(form) {
  var msgLang = "";
  MESSAGE=form.MSG.value;
  escape_msg=escape(MESSAGE);
  chi_position=0;
  no_of_Eng_Char=0;
  no_of_Chi_Char=0;

  if ((occurrence(MESSAGE, "{") > 0) && (occurrence(MESSAGE, "}") > 0)) 
    msgLang="chn";
  else 
    msgLang="eng";

  for (i=0;i<escape_msg.length;i++) {
    if (escape_msg.charAt(i)=="%") {
      if (escape_msg.charAt(i+1)=="u" || escape_msg.charAt(i+1)=="A" || escape_msg.charAt(i+1)=="B" || escape_msg.charAt(i+1)=="C" || escape_msg.charAt(i+1)=="D" || escape_msg.charAt(i+1)=="E" || escape_msg.charAt(i+1)=="F") {
        no_of_Chi_Char++;
        if (chi_position==0) { 
          chi_position=no_of_Eng_Char+no_of_Chi_Char;
        }
        if (escape_msg.charAt(i+1)=="u") {
          i+=5;
        }
        else {
          i+=3;
          if (escape_msg.charAt(i)=="%") {
            i+=2;
          }
        }
      }
      else {
        no_of_Eng_Char++;
        i+=2;
      }
    }
    else { 
      no_of_Eng_Char++;
    }
    if ((no_of_Eng_Char+no_of_Chi_Char)==max_chi_char) {
      escape_msg1=escape_msg.substring(0,i+1);
    }
  }  // end for
  
  total_no_of_char=no_of_Eng_Char+no_of_Chi_Char;
  if ( (no_of_Chi_Char>0) || (msgLang == "chn") )  {
    document.form.MESG_LANG.value="chn";
    form.char_left.value=Math.max(max_chi_char-total_no_of_char,0);
    if (total_no_of_char>max_chi_char) {
      if (chi_position>max_chi_char) {
        form.MSG.value=MESSAGE.substring(0,chi_position-1);
      }
      else { 
        form.MSG.value=unescape(escape_msg1);
      }
      alert(error_msg);
    }
  }
  else {
    document.form.MESG_LANG.value="eng";
    form.char_left.value=Math.max(max_eng_char-total_no_of_char,0);
    if (total_no_of_char>max_eng_char) {
      form.MSG.value=MESSAGE.substring(0,max_eng_char);
      alert(error_msg);
    }
  }
  
  if (counting) {
    start_time=new Date();
    start_time=start_time.getTime();
    delay_counter(form);
  }
  
}  // end function update_counter

function delay_counter(form) {
  now_time=new Date();
  now_time=now_time.getTime();
  if (now_time-start_time<=100) {
    f=form;
    if (counting) { 
      timer_id=setTimeout("delay_counter(f)",100); }
    }
    else {
      if (navigator.appName=="Netscape") {
        form.char_left.focus();
        form.MSG.focus();
      }
    update_counter(form);
  }
}

function start_counter(form) {
  counting=true;
  update_counter(form);
}

function stop_counter(form) {
  counting=false;
  clearTimeout(timer_id);
  update_counter(form);
}

function append(form) {
  var ALIAS=form.ALIAS.value;
  var ALIAS_GROUP=form.ALIAS_GROUP.options[form.ALIAS_GROUP.selectedIndex].value;
  if (ALIAS_GROUP.length>0) {
    if (ALIAS.length==0) { 
      ALIAS=ALIAS_GROUP+";";
    }
    else {
      ALIAS+=ALIAS_GROUP+";";
    }
  }
  
  form.ALIAS_GROUP.selectedIndex=0;
  form.ALIAS.value=ALIAS;
  form.ALIAS.focus();
}

function funOnSubmit() {

  if (noSend==0) {
    var iOpenBrace, iCloseBrace;
    iOpenBrace = occurrence(document.form.MSG.value, "{");
    iCloseBrace = occurrence(document.form.MSG.value, "}");
    if (iOpenBrace == iCloseBrace) {
      if ((iOpenBrace > 0) || (iCloseBrace > 0)) {
        document.form.MESG_LANG.value="chn";
      }
    }
        
    if(document.form.TYPE.value=="SMS" && document.form.MSG.value == "") {
		
        alert("請輸入訊息內容");

    } 
    else {
      document.form.MESSAGE.value=document.form.MSG.value;
      noSend=1;
      document.form.submit();
    }
  }
  else {

      alert("系統現正執行你的指令, 請等候");

  }
}