﻿var accentedPinyin = "aāáǎàeēéěèiīíǐìoōóǒòuūúǔùvǖǘǚǜü";

function fformat(p) {
var q;
 if (p < 10) q = "00"+ p; else if (p < 100) q = "0"+ p; else q = p;
 return q;
}

function firstChar(c) {
 if ((c.indexOf('+')<0) && (c.indexOf('-')<0)) return c.charAt(0); else return c;
}

function get_random(m,n) {  // returns a pseudo-random number r with  m <= r < n

    var ranNum= m + Math.floor(Math.random()*(n-m));
    return ranNum;
}

function deaccentPinyin(s) {
var c,i,j,k,u;

 u = "";
 for (i=0;i<s.length;i++) {
   c = s.charAt(i);
   k = accentedPinyin.indexOf(c);
   if (k<0) u += c; else {
     j = 5 * Math.floor(k/5); // first vowel in the each series of 5 is unaccented
     u += accentedPinyin.charAt(j);
   }
 }
 return (u);
}

function stringCompare (s,t) {
var a,b,c,i,m,n;

 m = s.length; n = t.length;
 if (n<m) n=m;
 for (i=0;i<n;i++) {
  c = s.charCodeAt(i)-t.charCodeAt(i);
  if (c) return c; 
 }
 return (m - t.length);
}

function charlistIndexof0000000(s,t) {
  return(s.indexOf(t));
}

function charlistIndexof(s,t) {
var j,k,c,d,e,l;
var build;

// return position of t in s, return -1 if not found
// t is not limited to a single char. , can include A+B or B-C

  l = s.length;
  e = '';
  build = 0;
  for (j=k=0; k<l; k++) {
    c = s.charAt(k);
    e += c;
    if (k<(l-1)) d = s.charAt(k+1); else d = '';
    if ((d=='+') || (d=='-')) {
      e += d;
      build++;
      k++;
    }
    else if (build) { 
      if (stringCompare(e,t)==0) return j; else j++;
      build=0;
      e = '';
    }
    else {
      if (c==t) return j; else j++;
      e = '';
    }
  }
  return -1;
}

function charlistAt(s,n) {
var j,k,c,d,e,l;
var build;

// return char. at position n in s, return -1 if not found
// s is not limited to single chars. , can include A+B or B-C

  l = s.length;
  e = '';
  build = 0;
  for (j=k=0; k<l; k++) {
    c = s.charAt(k);
    e += c;
    if (k<(l-1)) d = s.charAt(k+1); else d = '';
    if ((d=='+') || (d=='-')) {
      e += d;
      build++;
      k++;
    }
    else {
      if (j==n) return e; else j++;
      build=0;
      e = '';
    }
  }
  return -1;
}

function charlistTable(s) {  // handles X+Y or X-Y cases without parentheses
var j,k,c,d,e,l;
var build;
var t = new Array;

  l = s.length;
  e = '';
  build = 0;
  for (j=k=0; k<l; k++) {
    c = s.charAt(k);
    e += c;
    if (k<(l-1)) d = s.charAt(k+1); else d = '';
    if ((d=='+') || (d=='-')) {
      e += d;
      build++;
      k++;
    }
    else {
      t[j++] = e;
      e = '';
      build=0;
    }
  }
  return t;
}

function charlistLength(s) {  // handles X+Y or X-Y cases without parentheses
var j,k,c,d,l;
var build;

  l = s.length;
  build = 0;
  for (j=k=0; k<l; k++) {
    c = s.charAt(k);
    if (k<(l-1)) d = s.charAt(k+1); else d = '';
    if ((d=='+') || (d=='-')) {
      build++;
      k++;
    }
    else {
      j++;
      build=0;
    }
  }
  return j;
}

function closeModal() {
   document.getElementById('modal').style.left = 0;
   document.getElementById('modal').style.top  = 0;
   // document.getElementById('modal').style.height = 1;
   // document.getElementById('modal').style.width  = 1;
   document.getElementById('modal').innerHTML = nbsp;
}


function isnumeric(s) {
var i,c;

 for(i=0;i<s.length;i++) {
  c = s[i];  //  c = s.charAt(i);
  if (base10.indexOf(c) < 0) return false;
 }
 return(true);
}

/*

function charTable(s) { // handles cases where no single unicode exist, notation (X+Y) or (X-Y) etc. - remove parentheses
var b,c,i,k;
var build;
var t = new Array;

  build = 0;
  for (i=k=0; k<s.length; k++) {
    c = s.charAt(k);
    if (!build) {
      if (c == openpar) {b = ''; build++; } else t[i++] = c;
    }
    else {
     if (c == closepar) { t[i++] = b; build--; } else  b += c;
   }
  }
  return t;
}

function charlistLength(s) {
var i,k,c;
var build;

  i = build = 0;
  for (i=k=0; k<s.length; k++) {
    c = s.charAt(k);
    if (!build) {
      i++;
      if (c == openpar) build++;
    }
    else if (c == closepar) build--; 
  }
  return i;
}

*/




