//alert("common.js loaded");
// <script type="text/javascript" src="./includes/common.js"></script>

ar_haber = new Array();				// these auxillary verbs used by conjComplex() for complex tenses
ar_haber = [["he","has","ha","hemos","han"],["había","habías","había","habíamos","habían"],["hube","hubiste","hubo","hubimos","hubieron"],["habré","habrás","habrá","habremos","habrán"],["habría","habrías","habría","habríamos","habrían"],["haya","hayas","haya","hayamos","hayan"],["hubiera","hubieras","hubiera","hubiéramos","hubieran"]];

ar_pronombres_reflexivos = new Array();				// reflexive pronouns used when conjugating verbos reflexivos
ar_pronombres_reflexivos = ["me","te","se","nos","se"];


/* as long as index[1] does not exceed 99 this works
- Number req'd by most browsers to force numeric addition
*/
function arraySort (a, b) {
   var a = (Number(a[0])+(a[1]/100));
   var b = (Number(b[0])+(b[1]/100));
   //myvar += "(a="+a+", b="+b+", a-b="+(a-b)+"), ";
   return (a-b);
}


/* ar_verbs_w_limited_compuestos so we can modify display of a few verbs, ie. costar
*/
function getCompoundTenses ()
{
  if (ar_reqd_compound.length < 1) return;
  //if (typeof (ar_reqd_compound) != "object") return;	// declared therefore should be empty object
  tense_irreg = false;
  for (var i in ar_verbs)
  {
    tense_name_idx = ar_tense_names.length - ar_reqd_compound.length;
    for (var j in ar_reqd_compound)
    {
      ar_verbs[i][ar_verbs[i].length] = new Array();
      for (var k in ar_subj)
      {
        ar_verbs[i][ar_verbs[i].length-1][k] = ar_haber[ar_reqd_compound[j]][ar_subj[k]] + " " + ar_verbs[i][2];
        for (var l in ar_verbs_w_limited_compuestos)
        {
          if (ar_verbs_w_limited_compuestos[l] == ar_verbs[i][0] && (ar_subj[k]  < "2" || ar_subj[k]=="3"))
            ar_verbs[i][ar_verbs[i].length-1][k] = "";
        }
      }
      ar_conj[ar_conj.length] = new Array();
      pushed = ar_conj[ar_conj.length-1].push(i, ar_verbs[i].length-1, tense_name_idx++);
    }
  }
  ar_conj.sort(arraySort);
}


function accentThisVowel (vowel)	// rename this accentAcute ?
{
  //alert(vowel);
  if (vowel == "a") accented = "á";
  if (vowel == "e") accented = "é";
  if (vowel == "i") accented = "í";
  if (vowel == "o") accented = "ó";
  if (vowel == "u") accented = "ú";
  return accented;
}


function accentLastVowel (str)
{
  newstr = str.replace(/[aeiou](?=[^aeiou]?$)/i, accentThisVowel);	// match last vowel, parenthesized substr not part of match
  return (newstr);
}


function CE (el_type)
{
  var el_obj=document.createElement(el_type);
  return el_obj;
}


function GE (el_id)					// Get Element must be called from the same window/document 
{							// context or with the window path back to the calling doc
   var el_obj = document.getElementById(el_id);
   return el_obj;
}


function jumpPrev ()
{
  if (exercise == "conjugate") {
    if (conj_idx == 0) conj_idx = ar_conj.length;
    --conj_idx;
  } else {						// covers translate and review
    if (v_idx == 0) v_idx = ar_verbs.length;
    --v_idx;
  }
}
function jumpNext ()
{
  if (exercise == "conjugate") {
    if ((ar_conj.length - 1) == conj_idx) conj_idx = -1;
    ++conj_idx;
  } else {						// covers translate and review
    if ((ar_verbs.length - 1) == v_idx) v_idx = -1;
    ++v_idx;
  }
}


function tShootObj (obj, prop)
{
  var props = "";
  for(var i=0; i<obj.length; i++) {
    props += eval("obj." + prop);
  }
  return props;
}


function popUp () {
    //myWin = open("", "displayWindow", "width=250, height=150");
    myWin = open("", "displayWindow");
    // open document for further input
    myWin.document.open();
    // create document
    exercise == "translate"  ?  ar = ar_verbs  :  ar = ar_conj;
    myWin.document.write(print_r(ar));
    // close the document - (not the window!)
    myWin.document.close();
}


function print_r (obj)			// usually called with document.write(obj)
{
  if (typeof(obj) == "undefined") return "undefined";
  if (typeof(obj) == "boolean") return "boolean " + obj;
  if (typeof(obj) == "string") return "string " + obj;
  if (typeof(obj) == "function") return "<pre>" + obj + "</pre>";
  if (typeof(obj) == "number") return "number " + obj;
  // if none of the above then must be an object, so let's dig a little deeper
  myVar="";
  glue_r = "] => ";
  str = "<pre>Javascript " + typeof(obj) + "<br />(<br />";
  for (var i=0; i < obj.length; i++)
  {
      if (typeof (obj[i]) == "undefined") continue;
      if (typeof (obj[i]) != "object")
          str += "   [" + i + glue_r + obj[i] + "<br />";
      else {
          str += "   [" + i + glue_r + "Object<br />   (<br />";
          for (var j=0; j < obj[i].length; j++)
          {
              if (typeof (obj[i][j]) == "undefined") continue;
              if (typeof (obj[i][j]) != "object")
                  str += "	[" + j + glue_r + obj[i][j] + "<br />";
              else {
                  str += "	[" + j + glue_r + "Object<br />	(<br />";
                  for (var k=0; k < obj[i][j].length; k++)
                  {
                      if (typeof (obj[i][j][k]) == "undefined") continue;
                      str += "		[" + k + glue_r + obj[i][j][k] + "<br />";
                      if (k+1 == obj[i][j].length) str += "<br />	)<br />";
                  }
              }
              if (j+1 == obj[i].length) str += "<br />   )<br />";
          }
      }
  }
  myVar += str + ")</pre>"
  return myVar;
}

