dowstr = new Object;
dowstr[0]="Sunday";
dowstr[1]="Monday";
dowstr[2]="Tuesday";
dowstr[3]="Wednesday";
dowstr[4]="Thursday";
dowstr[5]="Friday";
dowstr[6]="Saturday";

var IGREG = (14 + 31 * (10 + 12 * 1582));
var JGREG = 2299160;
var IGREG2,JGREG;
var civcheck = new civil(5,23,1948,1);
var istwo = false;
var jdays = 0;
var bp, dayname, ampm, adbc, dow;
var firsttime=true;
var h1, h2, h3, h4, h5, h6, h7;
var chartname, chartmonth, chartdate, chartyear, chartdspdate, chartampm, chartadbc;
var tryear, trmonth, trdate, tradbc;
var natal2z = 30;
var transitz = 31;
var dasaz  = 12; 
var interpretz = 13;
var chartlistz = 14;
var planetchartz = 15;
var confirmz = 17;
var horaryz = 19;
var chart1ckd = false;

function resetZIndex() {
  parent.main_frame.document.styleSheets[0].rules[1].style.zIndex = dasaz;
  parent.main_frame.document.styleSheets[0].rules[3].style.zIndex = interpretz;
  parent.main_frame.document.styleSheets[0].rules[5].style.zIndex = chartlistz;
  parent.main_frame.document.styleSheets[0].rules[7].style.zIndex = planetchartz;
  parent.main_frame.document.styleSheets[0].rules[9].style.zIndex = transitz;
  parent.main_frame.document.styleSheets[0].rules[11].style.zIndex = natal2z;
  parent.main_frame.document.styleSheets[0].rules[12].style.zIndex = confirmz;
  parent.main_frame.document.styleSheets[0].rules[14].style.zIndex = horaryz;
}

function init() {
  document.birthdata_fm.reset();
  setGreg(document.birthdata_fm);
}

function setGreg() {
  IGREG2 = IGREG;
  JGREG2 = JGREG;
  getNatalDates();
  if (year == 0 || month == 0 || date == 0) {}
  else
  evalCiv();
}

function tooLong(val) {
  if (val >= 0) {
    return Math.floor(val);
  }
  else {
    return Math.ceil(val);
  }
}

function resetChartColors(houseid) { 
  parent.main_frame.document.getElementById(houseid + '1t_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '1_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '1_id').style.borderTop = 'none';
  parent.main_frame.document.getElementById(houseid + '1_id').style.borderBottom = 'none';
  parent.main_frame.document.getElementById(houseid + '1txt_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '1txt_id').innerHTML="&nbsp;";
  parent.main_frame.document.getElementById(houseid + '1b_id').style.backgroundColor='#ffcc99';

  parent.main_frame.document.getElementById(houseid + '2t_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '2_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '2_id').style.borderTop = 'none';
  parent.main_frame.document.getElementById(houseid + '2_id').style.borderBottom = 'none';
  parent.main_frame.document.getElementById(houseid + '2txt_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '2txt_id').innerHTML="&nbsp;";
  parent.main_frame.document.getElementById(houseid + '2b_id').style.backgroundColor='#ffcc99';

  parent.main_frame.document.getElementById(houseid + '3t_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '3_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '3_id').style.borderTop = 'none';
  parent.main_frame.document.getElementById(houseid + '3_id').style.borderBottom = 'none';
  parent.main_frame.document.getElementById(houseid + '3txt_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '3txt_id').innerHTML="&nbsp;";
  parent.main_frame.document.getElementById(houseid + '3b_id').style.backgroundColor='#ccffcc';

  parent.main_frame.document.getElementById(houseid + '4t_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '4_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '4_id').style.borderTop = 'none';
  parent.main_frame.document.getElementById(houseid + '4_id').style.borderBottom = 'none';
  parent.main_frame.document.getElementById(houseid + '4txt_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '4txt_id').innerHTML="&nbsp;";
  parent.main_frame.document.getElementById(houseid + '4b_id').style.backgroundColor='#ccffcc';

  parent.main_frame.document.getElementById(houseid + '5t_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '5_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '5_id').style.borderTop = 'none';
  parent.main_frame.document.getElementById(houseid + '5_id').style.borderBottom = 'none';
  parent.main_frame.document.getElementById(houseid + '5txt_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '5txt_id').innerHTML="&nbsp;";
  parent.main_frame.document.getElementById(houseid + '5b_id').style.backgroundColor='#ccffcc';

  parent.main_frame.document.getElementById(houseid + '6t_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '6_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '6_id').style.borderTop = 'none';
  parent.main_frame.document.getElementById(houseid + '6_id').style.borderBottom = 'none';
  parent.main_frame.document.getElementById(houseid + '6txt_id').style.backgroundColor='#ffcc99';
  parent.main_frame.document.getElementById(houseid + '6txt_id').innerHTML="&nbsp;";
  parent.main_frame.document.getElementById(houseid + '6b_id').style.backgroundColor='#ffcc99';

  parent.main_frame.document.getElementById(houseid + '7t_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '7_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '7_id').style.borderTop = 'none';
  parent.main_frame.document.getElementById(houseid + '7_id').style.borderBottom = 'none';
  parent.main_frame.document.getElementById(houseid + '7txt_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '7txt_id').innerHTML="&nbsp;";
  parent.main_frame.document.getElementById(houseid + '7b_id').style.backgroundColor='#ccffcc';

  parent.main_frame.document.getElementById(houseid + '7l_id').style.backgroundColor='#ccffcc';
  parent.main_frame.document.getElementById(houseid + '7r_id').style.backgroundColor='#ccffcc';
}

function resetTransitChart() {
  parent.main_frame.document.getElementById('transit1_id').innerHTML = "&nbsp;";
  parent.main_frame.document.getElementById('transit2_id').innerHTML = "&nbsp;";
  parent.main_frame.document.getElementById('transit3_id').innerHTML = "&nbsp;";
  parent.main_frame.document.getElementById('transit4_id').innerHTML = "&nbsp;";
  parent.main_frame.document.getElementById('transit5_id').innerHTML = "&nbsp;";
  parent.main_frame.document.getElementById('transit6_id').innerHTML = "&nbsp;";
  parent.main_frame.document.getElementById('transit7_id').innerHTML = "&nbsp;";
  resetChartColors('transit');
  parent.main_frame.document.getElementById('transitdiv').style.visibility = 'hidden';
  parent.main_frame.document.getElementById('transitdiv2').style.visibility = 'hidden';
}

function resetConfirmChart() {
  parent.main_frame.document.getElementById('confirmtext_id').innerHTML = "&nbsp;";
  parent.main_frame.document.getElementById('condate_id').value = "";
  parent.main_frame.document.getElementById('confirmdiv').style.visibility = 'hidden';
  parent.main_frame.document.getElementById('confirmdiv2').style.visibility = 'hidden';
}

function resetHoraryChart() {
  parent.main_frame.document.getElementById('horarytext_id').innerHTML = "&nbsp;";
  parent.main_frame.document.getElementById('horaryday_id').selectedIndex = 0;
  parent.main_frame.document.getElementById('horarytime_id').selectedIndex = 0;
  parent.main_frame.document.getElementById('horarydiv').style.visibility = 'hidden';
  parent.main_frame.document.getElementById('horarydiv2').style.visibility = 'hidden';
}

function getNatalDates() {
  year  = document.getElementById('birthyear_id').value;
  year  = parseInt(year);
  year  = isNaN(year) ? 0 : tooLong(year);
  month = document.getElementById('birthmonth_id').options[document.getElementById('birthmonth_id').selectedIndex].value;
  month = isNaN(month) ? 0 : parseInt(month);
  date  = document.getElementById('birthdate_id').options[document.getElementById('birthdate_id').selectedIndex].value;
  date  = isNaN(date) ? 0 : parseInt(date);
  ampm  = document.getElementById('ampm_id').options[document.getElementById('ampm_id').selectedIndex].value;
  adbc = document.getElementById('adbc_id').options[document.getElementById('adbc_id').selectedIndex].value;
}

function getTransitDates() {
  tryear  = parent.main_frame.document.getElementById('tryear_id').value;
  tryear  = parseInt(tryear);
  tryear  = isNaN(tryear) ? 0 : tooLong(tryear);
  trmonth = document.getElementById('birthmonth_id').options[document.getElementById('birthmonth_id').selectedIndex].value;
  trmonth = isNaN(month) ? 0 : parseInt(month);
  trdate  = document.getElementById('birthdate_id').options[document.getElementById('birthdate_id').selectedIndex].value;
  trdate  = isNaN(date) ? 0 : parseInt(date);
  trampm  = document.getElementById('ampm_id').options[document.getElementById('ampm_id').selectedIndex].value;
  tradbc  = (parent.main_frame.document.getElementById('tryear_id').toString().indexOf('-') != -1) ? "BC" : "AD";
}

function evalJD(houseid) {
  document.getElementById('ampmrow_id').style.display='none';
  document.getElementById('ampmrow2_id').style.display='inline';
  document.getElementById('ampmmsg_id').style.display='none';

  getNatalDates();
  if (year != 0 && month != 0 && date != 0) {}
  else return;
  iyyy = parseInt(year);
  iyyy = tooLong(iyyy);
  mm = parseInt(month);
  id = parseInt(date);
  var ysgn = (adbc == "BC") ? -1 : 1;

  if (mm == 0 || id == 0 || iyyy == "") return;

  var jy = ysgn * iyyy, ja, jm, jul;

  if (iyyy < 1) {
   // document.getElmentById('errmsg_id').innerText = "Enter positive year,\nthen select AD or BC\n(There was no year 0.)";
    return;
  }

  if (jy < 0) 
    ++jy;

  if (mm > 2) 
    jm = mm + 1;
  else {
    --jy; 
    jm = mm + 13;
  }

  jul = tooLong(Math.floor(365.25*jy) + Math.floor(30.6001*jm) + id + 1720995);

  if (id +31 * (mm + 12 * ysgn * iyyy) >= IGREG2) {
    ja = tooLong(0.01 * jy);
    jul += 2 - ja + tooLong(0.25*ja);
  }

  jdays = jul;  civcheck = caldat(jul);
  getDayOfWeek(jul);
  layoutNatalChart(houseid);
  if (houseid == 'house')
    parent.main_frame.document.getElementById('chart1name').innerText = document.getElementById('name_id').value;
  else 
    parent.main_frame.document.getElementById('chart2name').innerText = document.getElementById('name_id').value;
  if (houseid == 'house') {
    resetTransitChart();
  }
  resetConfirmChart();
  resetHoraryChart();

  if (civcheck.mm != mm || civcheck.dd != id || civcheck.yyyy != iyyy || civcheck.ab != ysgn) {
    document.getElementById('dayofweek_id').innerText = "Invalid Date";
    document.getElementById('bp_id').innerText = "";
    document.getElementById('ampmrow_id').style.display='none';
    document.getElementById('ampmrow2_id').style.display='inline';
    document.getElementById('ampmmsg_id').style.display='none';
  }
}

function evalCiv() {
  civcheck = caldat(jdays);
  getDayOfWeek(Math.round(jdays));
}

function civil(mm,dd,yyyy,ab) {
  this.mm = mm; 
  this.dd = dd; 
  this.yyyy = yyyy; 
  this.ab = ab;
}

function caldat(julian) {
  var civ = new civil(5,23,1948,1);
  var ja,jalpha,jb,jc,jd,je;
  julian = tooLong(julian);

  if (julian > JGREG2) {
    jalpha = tooLong(((julian - 1867216) - 0.25) / 36524.25);
    ja     = julian + 1 + jalpha - tooLong(0.25 * jalpha);
  } 
  else { 
    ja = julian; 
  }

  jb = ja + 1524;
  jc = tooLong(6680 + ((jb - 2439870) -122.1) / 365.25);
  jd = tooLong(365 * jc + 0.25 * jc);
  je = tooLong((jb - jd) / 30.6001);
  civ.dd = jb - jd - tooLong(30.6001 * je);
  civ.mm = je - 1;

  if (civ.mm > 12) 
    civ.mm -= 12;

  civ.yyyy = jc-4715;

  if (civ.mm > 2) 
    --civ.yyyy;

  civ.ab = 1;

  if (civ.yyyy <= 0) { 
    civ.yyyy = 1 - civ.yyyy; 
    civ.ab=-1;
  }
  return (civ);
}

function getDayOfWeek(jull) {
  dow = Math.round((jull + 1) % 7);
  document.getElementById('dayofweek_id').innerText = dowstr[dow]; 
  dayname = dowstr[dow];
}


function getHousePlanets(nt) {
  var wkyear  = (nt == "natal") ? year : tryear;
  var wkmonth = (nt == "natal") ? month : trmonth;
  var wkdate  = (nt == "natal") ? date : trdate;
  var planet1 = (wkyear - -4717) % 7;
  if (wkmonth <= 3 || (wkmonth == 4 && wkdate <= 15)) {
    planet1 = (planet1 == 0) ? 6 : planet1 - 1;
  } 

  // Determine birth planet
  //
  if (year != 0 && month != 0 && date != 0) {
    switch (dow) {
      case 0:
        dayname = "Sunday";
        bp = "Sun";
        break;
      case 1:
        dayname = "Monday";
        bp = "Moon";
        break;
      case 2:
        dayname = "Tuesday";
        bp = "Mars";
        break;
      case 3:
        dayname = "Wednesday";
        if (ampm == "AM") 
          bp = "Mercury";
        else
          bp = "Rahu"
	   document.getElementById('ampmrow_id').style.display='inline';
	   document.getElementById('ampmrow2_id').style.display='none';
	   document.getElementById('ampmmsg_id').style.display='inline';
        break;
      case 4:
        dayname = "Thursday";
        bp = "Jupiter";
        break;
     case 5:
       dayname = "Friday";
       bp = "Venus";
       break;
     case 6:
       dayname = "Saturday";
       bp = "Saturn";
       break;
     default:
       dayname = "Invalid";
    } 
  }

  if (firsttime) {
    parent.main_frame.document.getElementById('menupointer').innerText = 'Click Above for Menus';
    parent.main_frame.showMenuBar();
  }
  firsttime = false;
	
  switch (planet1) {
    case 0:
      h1 = "Saturn";
      h2 = "Mars";
      h3 = "Venus";
      h4 = "Moon";
      h5 = "Jupiter";
      h6 = "Sun";
      if (bp == "Rahu")
        h7 = "Rahu";
      else
        h7 = "Mercury";
      break;
    case 1:
      h1 = "Sun";
      if (bp == "Rahu")
        h2 = "Rahu";
      else
        h2 = "Mercury";
      h3 = "Saturn";
      h4 = "Mars";
      h5 = "Venus";
      h6 = "Moon";
      h7 = "Jupiter";
      break;
    case 2:
      h1 = "Moon";
      h2 = "Jupiter";
      h3 = "Sun";
      if (bp == "Rahu")
        h4 = "Rahu";
      else
        h4 = "Mercury";
      h5 = "Saturn";
      h6 = "Mars";
      h7 = "Venus";
      break;
    case 3:
      h1 = "Mars";
      h2 = "Venus";
      h3 = "Moon";
      h4 = "Jupiter";
      h5 = "Sun";
      if (bp == "Rahu")
        h6 = "Rahu";
      else
        h6 = "Mercury";
      h7 = "Saturn";
      break;
    case 4:
      if (bp == "Rahu")
        h1 = "Rahu";
      else
        h1 = "Mercury";
      h2 = "Saturn";
      h3 = "Mars";
      h4 = "Venus";
      h5 = "Moon";
      h6 = "Jupiter";
      h7 = "Sun";
      break;
    case 5:
      h1 = "Jupiter";
      h2 = "Sun";
      if (bp == "Rahu")
        h3 = "Rahu";
      else
        h3 = "Mercury";
      h4 = "Saturn";
      h5 = "Mars";
      h6 = "Venus";
      h7 = "Moon";
      break;
    case 6:
      h1 = "Venus";
      h2 = "Moon";
      h3 = "Jupiter";
      h4 = "Sun";
      if (bp == "Rahu")
        h5 = "Rahu";
      else
        h5 = "Mercury";
      h6 = "Saturn";
      h7 = "Mars";
      break;
    default:
      h1 = "Invalid";
  } 
}


function layoutNatalChart(houseid) {
  getNatalDates();
  getHousePlanets('natal');

  parent.main_frame.document.getElementById(houseid + '1_id').innerText = h1;
  parent.main_frame.document.getElementById(houseid + '2_id').innerText = h2;
  parent.main_frame.document.getElementById(houseid + '3_id').innerText = h3;
  parent.main_frame.document.getElementById(houseid + '4_id').innerText = h4;
  parent.main_frame.document.getElementById(houseid + '5_id').innerText = h5;
  parent.main_frame.document.getElementById(houseid + '6_id').innerText = h6;
  parent.main_frame.document.getElementById(houseid + '7_id').innerText = h7;
  hlBirthPlanet(houseid);

  getDasa(bp);
  parent.main_frame.document.getElementById('dasa_id').innerHTML = getDasa(bp);
  parent.main_frame.document.getElementById('dasadiv').style.visibility='visible';
  parent.main_frame.document.getElementById('dasadiv2').style.visibility='visible';
  if (houseid == 'n2house') {
    parent.main_frame.document.getElementById('natal2div').style.visibility='visible';
  }
}


function layoutTransitChart() {
  getTransitDates();
  getHousePlanets('transit');

  parent.main_frame.document.getElementById('transit1_id').innerText = h1;
  parent.main_frame.document.getElementById('transit2_id').innerText = h2;
  parent.main_frame.document.getElementById('transit3_id').innerText = h3;
  parent.main_frame.document.getElementById('transit4_id').innerText = h4;
  parent.main_frame.document.getElementById('transit5_id').innerText = h5;
  parent.main_frame.document.getElementById('transit6_id').innerText = h6;
  parent.main_frame.document.getElementById('transit7_id').innerText = h7;
  hlBirthPlanet('transit');

  parent.main_frame.document.getElementById('transitname').innerText=document.getElementById('name_id').value;
  if (parent.main_frame.document.getElementById('transitname').innerText == parent.main_frame.document.getElementById('chart1name').innerText) {
    parent.main_frame.document.getElementById('transitdiv').style.top = parent.main_frame.document.getElementById('nataldiv').style.top.toString().substring(0, parent.main_frame.document.getElementById('nataldiv').style.top.length - 2) - 36;
    parent.main_frame.document.getElementById('transitdiv').style.left = parent.main_frame.document.getElementById('nataldiv').style.left; 
  }
  if (parent.main_frame.document.getElementById('transitname').innerText == parent.main_frame.document.getElementById('chart2name').innerText) {
    parent.main_frame.document.getElementById('transitdiv').style.top = parent.main_frame.document.getElementById('natal2div').style.top.toString().substring(0, parent.main_frame.document.getElementById('natal2div').style.top.length - 2) - 36;
    parent.main_frame.document.getElementById('transitdiv').style.left = parent.main_frame.document.getElementById('natal2div').style.left; 
  }
  parent.main_frame.document.getElementById('transitdiv').style.visibility='visible';
  parent.main_frame.document.getElementById('transitdiv2').style.visibility='visible';
}


function hlBirthPlanet(houseid) {
  parent.main_frame.document.bp=bp;
  document.getElementById('bp_id').innerText = bp.toString().toUpperCase();
  document.getElementById('bp_id').style.backgroundColor = 'yellow'; 
  document.getElementById('bp_id').style.border = '1px solid black'; 

  parent.main_frame.document.getElementById(houseid + '1_id').innerText = parent.main_frame.document.getElementById(houseid + '1_id').innerText.toString().toLowerCase();
  parent.main_frame.document.getElementById(houseid + '2_id').innerText = parent.main_frame.document.getElementById(houseid + '2_id').innerText.toString().toLowerCase();
  parent.main_frame.document.getElementById(houseid + '3_id').innerText = parent.main_frame.document.getElementById(houseid + '3_id').innerText.toString().toLowerCase();
  parent.main_frame.document.getElementById(houseid + '4_id').innerText = parent.main_frame.document.getElementById(houseid + '4_id').innerText.toString().toLowerCase();
  parent.main_frame.document.getElementById(houseid + '5_id').innerText = parent.main_frame.document.getElementById(houseid + '5_id').innerText.toString().toLowerCase();
  parent.main_frame.document.getElementById(houseid + '6_id').innerText = parent.main_frame.document.getElementById(houseid + '6_id').innerText.toString().toLowerCase();
  parent.main_frame.document.getElementById(houseid + '7_id').innerText = parent.main_frame.document.getElementById(houseid + '7_id').innerText.toString().toLowerCase();
  resetChartColors(houseid);

  if (bp == h1) {
    parent.main_frame.document.getElementById(houseid + '1_id').style.backgroundColor='yellow';
    parent.main_frame.document.getElementById(houseid + '1_id').style.borderTop = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '1_id').style.borderBottom = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '1_id').innerText = parent.main_frame.document.getElementById(houseid + '1_id').innerText.toString().toUpperCase();
  }
  if (bp == h2) {
    parent.main_frame.document.getElementById(houseid + '2_id').style.backgroundColor='yellow';
    parent.main_frame.document.getElementById(houseid + '2_id').style.borderTop = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '2_id').style.borderBottom = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '2_id').innerText = parent.main_frame.document.getElementById(houseid + '2_id').innerText.toString().toUpperCase();
  }
  if (bp == h3) {
    parent.main_frame.document.getElementById(houseid + '3_id').style.backgroundColor='yellow';
    parent.main_frame.document.getElementById(houseid + '3_id').style.borderTop = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '3_id').style.borderBottom = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '3_id').innerText = parent.main_frame.document.getElementById(houseid + '3_id').innerText.toString().toUpperCase();
  }
  if (bp == h4) {
    parent.main_frame.document.getElementById(houseid + '4_id').style.backgroundColor='yellow';
    parent.main_frame.document.getElementById(houseid + '4_id').style.borderTop = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '4_id').style.borderBottom = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '4_id').innerText = parent.main_frame.document.getElementById(houseid + '4_id').innerText.toString().toUpperCase();
  }
  if (bp == h5) {
    parent.main_frame.document.getElementById(houseid + '5_id').style.backgroundColor='yellow';
    parent.main_frame.document.getElementById(houseid + '5_id').style.borderTop = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '5_id').style.borderBottom = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '5_id').innerText = parent.main_frame.document.getElementById(houseid + '5_id').innerText.toString().toUpperCase();
  }
  if (bp == h6) {
    parent.main_frame.document.getElementById(houseid + '6_id').style.backgroundColor='yellow';
    parent.main_frame.document.getElementById(houseid + '6_id').style.borderTop = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '6_id').style.borderBottom = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '6_id').innerText = parent.main_frame.document.getElementById(houseid + '6_id').innerText.toString().toUpperCase();
  }
  if (bp == h7) {
    parent.main_frame.document.getElementById(houseid + '7_id').style.backgroundColor='yellow';
    parent.main_frame.document.getElementById(houseid + '7_id').style.borderTop = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '7_id').style.borderBottom = '1px solid black';
    parent.main_frame.document.getElementById(houseid + '7_id').innerText = parent.main_frame.document.getElementById(houseid + '7_id').innerText.toString().toUpperCase();
  }
}

// Cookie functions for chart operations
//
var namestart  = 7; 
var monthstart, datestart, yearstart, ampmstart, adbcstart;
var nameend, monthend, dateend, yearend, ampmend, adbcend;
var chartmax = 10
var expiredate = new Date(1970, 0, 1);

// Sort Charts
function sortCharts() {
  var charts = new Array();

  for (i=0; i<chartmax; i++) {
    cookiename = "chartname" + parseFloat(i+1);
    cookieval  = parent.main_frame.getCookieVal(cookiename, document.cookie);
    if (cookieval != "") {
	charts[i]   = cookieval;
    }
  }

  charts.sort();
  for (i=0; i<charts.length; i++) {
	getBegEnd(charts[i]);
	var cookiedata = "name="+chartname+"month="+chartmonth+"date="+chartdate+"year="+chartyear+"ampm="+chartampm+"adbc="+chartadbc;
	parent.main_frame.writeCookie("chartname" + parseFloat(i+1), " ", expiredate);
	parent.main_frame.writeCookie("chartname" + parseFloat(i+1), cookiedata, "");
  }
  return (charts);
}

// Load charts from cookie into array and sort
function getCharts() {
  var j = 0;
  var charts = new Array();

  for (i=0; i<chartmax; i++) {
    cookiename = "chartname" + parseFloat(i+1);
    cookieval  = parent.main_frame.getCookieVal(cookiename, document.cookie);
	charts[i]   = cookieval;
  }

  for (i=0; i<charts.length; i++) {
    if (charts[i] != "") { 
	getBegEnd(charts[i]);
	var cookiedata = "name="+chartname+"month="+chartmonth+"date="+chartdate+"year="+chartyear+"ampm="+chartampm+"adbc="+chartadbc;
	parent.main_frame.writeCookie("chartname" + parseFloat(i+1), " ", expiredate);
	parent.main_frame.writeCookie("chartname" + parseFloat(j+1), cookiedata, "");
	j += 1;
    }
  }
  return (sortCharts());
}

// Set Start and End position for each field in cookie
function getBegEnd(cookieval) {
  monthstart = cookieval.toString().indexOf('month%3D')+8;
  datestart  = cookieval.toString().indexOf('date%3D')+7;
  yearstart  = cookieval.toString().indexOf('year%3D')+7;
  ampmstart  = cookieval.toString().indexOf('ampm%3D')+7;
  adbcstart  = cookieval.toString().indexOf('adbc%3D')+7;
  nameend    = monthstart - 8;
  monthend   = datestart - 7;
  dateend    = yearstart - 7;
  yearend    = ampmstart - 7;
  ampmend    = adbcstart - 7;
  adbcend    = cookieval.length;

  // Parse Cookie into global variables
  chartname    = cookieval.toString().substring(namestart, nameend);
  chartname    = chartname.toString().split('%20').join(' ').split('%2C').join(',').split('%28').join('(').split('%29').join(')');
  chartname    = (chartname.length < 18) ? chartname : chartname.toString().substring(0, 18);
  chartmonth   = cookieval.toString().substring(monthstart, monthend) + '';
  chartdate    = cookieval.toString().substring(datestart, dateend) + '';
  chartyear    = cookieval.toString().substring(yearstart, yearend) + '';
  chartdspdate = mthnames[chartmonth-1] + " " + ((chartdate.length < 2) ? ("0" + chartdate) : chartdate ) + " " + chartyear;
  chartampm    = cookieval.toString().substring(ampmstart, ampmend);
  chartadbc    = cookieval.toString().substring(adbcstart, adbcend);
}

// Show charts by loading them into layer in main_frame
function showCharts() {
  var chartlist = "<table cellspacing='0' cellpadding='0' border='0'>";
  var charts = new Array();
  charts = getCharts();

  for (i=0; i<charts.length; i++) {
    if (charts[i] != "") {
    getBegEnd(charts[i]);
    var dspname  = (chartname.length < 18) ? chartname : chartname.toString().substring(0, 18);

    while (chartyear.length < 4) {
	chartyear = "0" + chartyear;
    }
    chartlist += "<tr>" + "<td style='width:120'>" + dspname + "<\/td>" +
			"<td style='width:80' align='right'>" +
			   chartdspdate +
			"<\/td>" + 
			"<td style='width:40' align='center'>" +
			  "<input type='radio' name='chart1' style='cursor:hand' border='0' onClick=\"parent.left_frame.loadChart('" + parseFloat(i+1) + "', 'house')\; oNode = this.parentNode.parentNode.childNodes[0]\; document.getElementById('chart1name').innerHTML = oNode.innerText\; parent.main_frame.document.getElementById('transitdiv').style.visibility = 'hidden'\; parent.main_frame.document.getElementById('transitdiv2').style.visibility = 'hidden'\;\">" +
			"<\/td>" +
			"<td style='width:40' align='center'>" +
			  "<input type='radio' name='chart2' style='cursor:hand' border='0' onClick=\"if (parent.left_frame.checkChart1()) { parent.left_frame.loadChart('" + parseFloat(i+1) + "', 'n2house')\; oNode = this.parentNode.parentNode.childNodes[0]\; document.getElementById('chart2name').innerHTML = oNode.innerText\; parent.main_frame.document.getElementById('transitdiv').style.visibility = 'hidden'\; parent.main_frame.document.getElementById('transitdiv2').style.visibility = 'hidden'\; } else this.checked = false\;\">" +
			"<\/td>" +
			"<td style='width:40' align='center'>" +
			  "<input type='checkbox' name='delete' style='cursor:hand' border='0' onClick=\"parent.left_frame.deleteChart('" + parseFloat(i+1) + "')\">" +
			"<\/td>" + "<\/tr>\n";
    }
  }
  chartlist += "<\/table>";
  resetZIndex();
  parent.main_frame.document.styleSheets[0].rules[5].style.zIndex = 99;
  parent.main_frame.document.getElementById('charts_id').innerHTML = chartlist;
  parent.main_frame.document.getElementById('chartlistdiv').style.visibility='visible';
  parent.main_frame.document.getElementById('chartlistdiv2').style.visibility='visible';
}

// Load selected chart from pop up layer into chart
function loadChart(chartnum, houseid) {
  chartnum = parseFloat(chartnum);
  cookieval = parent.main_frame.getCookieVal("chartname"+chartnum, document.cookie)
  getBegEnd(cookieval);

  document.getElementById('name_id').value = chartname;
  document.getElementById('birthmonth_id').value = chartmonth;
  document.getElementById('birthdate_id').value = chartdate;
  document.getElementById('birthyear_id').value = chartyear;
  document.getElementById('ampm_id').value = chartampm;
  document.getElementById('adbc_id').value = chartadbc;
  
  for (i=0; i<document.getElementById('birthmonth_id').options.length; i++) {
    if (document.getElementById('birthmonth_id').options[i].value == chartmonth) {
	document.getElementById('birthmonth_id').selectedIndex = i;
    }
  }
  for (i=0; i<document.getElementById('birthdate_id').options.length; i++) {
    if (document.getElementById('birthdate_id').options[i].value == chartdate) {
	document.getElementById('birthdate_id').selectedIndex = i;
    }
  }

  if (chartampm == "AM") 
    document.getElementById('ampm_id').selectedIndex = 0;
  else 
    document.getElementById('ampm_id').selectedIndex = 1;

  if (chartadbc == "AD") 
    document.getElementById('adbc_id').selectedIndex = 0;
  else 
    document.getElementById('adbc_id').selectedIndex = 1;

  evalJD(houseid);
}

// Save new chart into cookie
function saveChart() {
  var name = document.getElementById('name_id').value;
  if (name <= " ") {
    alert("You must enter a name for this chart.");
    return;
  }

  // Find next cookie seq #
  //
  var i = 0;
  do {
    i += 1;
    cookiename = "chartname" + i;
  } while(parent.main_frame.getCookieVal(cookiename, document.cookie) != "");

  if (i > chartmax) {
    alert("Only ten charts can be stored in the non-subscriber area of the website.");
    return;
  }

  var cookiedata = "name="+name+"month="+month+"date="+date+"year="+year+"ampm="+ampm+"adbc="+adbc;
  parent.main_frame.writeCookie("chartname"+i, cookiedata, "");
  parent.main_frame.document.getElementById('chart1name').innerText = document.getElementById('name_id').value;
  showCharts();
}

// Delete Chart
function deleteChart(chartnum) {
  cookieval = parent.main_frame.getCookieVal("chartname"+chartnum, document.cookie)
  parent.main_frame.writeCookie("chartname" + chartnum, " ", expiredate);
  showCharts();
}


// See if Chart 1 is loaded
function checkChart1() {
  if (document.getElementById('name_id') > "") 
    chart1ckd = true;
  else 
    chart1ckd = false;

  return (chart1ckd);
}