/****************************************************/
/*  Title      : Chinese Horoscope                  */
/*  Date       : Oct 31, 2002                       */
/*                                                  */
/*  Programmer : Inchul Koo                         */
/*  E-mail     : inchul@sunflowerwebstudio.com      */
/*  Web Address: http://www.sunflowerwebstudio.com  */
/*                                                  */
/*  License    : Free for Non-Commercial Use        */
/****************************************************/ 

/****************************/
/*     global variables     */
/****************************/
var timeID;
var picNum;
var showSide;
var animalNum;
var elementNum;

/*************************/
/*         init          */
/*************************/
function init(cur)
{ if(animalNum && animalNum != 0)
  { var objectName = "document.pix_" + animalNum;
    eval(objectName).src = "images/im_" + animalNum + ".gif";
  }
  timeID=0;
  picNum=1;
  showSide=true;
  animalNum=0;
  elementNum=0;
 
  document.animalImg.src = "images/dragon.gif";
  cur.birthTitle.value   = "";
  cur.theYearTitle.value = "";
  cur.theYearDesc.value  = "";
  cur.elementTitle.value = "";
  cur.elementDesc.value  = "";
  cur.compatiTitle.value = "";
  cur.compatibDesc.value = "";
  cur.theHourTitle.value = "";
  cur.etcDesc.value      = "";
  cur.theHourDesc.value  = "";
}

/*************************/
/*     resetTestForm     */
/*************************/
function resetTestForm(cur)
{ cur.reset();
  init(cur);
  location.href = "#top";
}

/*************************/
/*     showTheAnswer     */
/*************************/
function showTheAnswer(cur)
{ if(formValid(cur))
  { if(timeID==0)
    { location.href = "#answer";
      init(cur);
      timeID=setInterval("startFlip()",40);
    }
    getAnimalNum(cur);
  }
}

/*************************/
/*       formValid       */
/*************************/
function formValid(cur)
{ //month, dd, yyyy has been selected?
  if(cur.birthMonth.selectedIndex == 0)
  { alert("Please select your Month of Birth. ");
    cur.birthMonth.focus();
    return false;
  }
  else if(cur.birthDay.selectedIndex == 0)
  { alert("Please select your Day of Birth. ");
    cur.birthDay.focus();
    return false;
  }
  else if(cur.birthYear.selectedIndex == 60)
  { alert("Please select your Year of Birth. ");
    cur.birthYear.focus();
    return false;
  }
  else
    return true;
}

/*************************/
/*      validateDay      */
/*************************/
function validateDay(cur)
{ // adjust the end day of the month
  for(i=29;i<=31;i++)
  { if(!(cur.birthDay.options[i]))
    { opt = new Option(i);
      cur.birthDay.options[i] = opt;
    }
  }
  switch(cur.birthMonth.selectedIndex)
  { case  2 : cur.birthDay.options[31] = null;
              cur.birthDay.options[30] = null;
              var year = cur.birthYear.selectedIndex + 1900;
              if(cur.birthYear.selectedIndex > 60)
                year--;
              if(!isLeapYear(year) || cur.birthYear.selectedIndex == 60)
                cur.birthDay.options[29] = null;
              break;
    case  4 :
    case  6 :
    case  9 :
    case 11 : cur.birthDay.options[31] = null;
  }
}

/*************************/
/*      isLeapYear       */
/*************************/
function isLeapYear(year)
{ // the value is leap year?
  var leap = false;
  if(year%400 == 0)
    leap = true;
  else if(year%100 == 0)
    leap = false;
  else if(year%4 == 0)
    leap = true;
  return leap;
}

/*************************/
/*     getAnimalNum     */
/*************************/
function getAnimalNum(cur)
{ //find chinese zodiac animal number
  var year = cur.birthYear.selectedIndex + 1900;
  if(cur.birthYear.selectedIndex > 60)
    year--;
  var month=cur.birthMonth.selectedIndex;
  var day=cur.birthDay.selectedIndex;
  var birthDate = new Date(year,month-1,day);
  cur.birthTitle.value = " "
                       + weekday[birthDate.getDay()] + ","
                       + monthname[birthDate.getMonth()] + "."
                       + birthDate.getDate() + ", "
                       + birthDate.getFullYear();
  var mmdd = chineseNewYear[year-1900];
  month = Math.floor(mmdd);
  day   = Math.floor((mmdd-month)*100);
  var chineseDate = new Date(year,month-1,day);

  if(birthDate < chineseDate)
    year--;
  animalNum = (year-4)%12 + 1;
  elementNum = Math.floor((year%10)/2)+1;
}

/*************************/
/*       startFlip       */
/*************************/
function startFlip()
{ //start animation - flip animal card (show back)
  if(picNum==13)
  { clearInterval(timeID);
    timeID=0;
    picNum=1;
    timeID=setInterval("showFront()",40);
  }
  else
  { var objectName = "document.pix_" + picNum;
    if(showSide)
    { eval(objectName).src = "images/im_side.gif";
      showSide=false;
    }
    else
    { eval(objectName).src = "images/im_back.gif";
      showSide=true;
      picNum++;
    }
  }
}

/*************************/
/*       showFront       */
/*************************/
function showFront()
{ //flip animal card (show front)
  if(picNum==13)
  { clearInterval(timeID);
    timeID=0;
    picNum=1;
    //display horoscope when the animation finished
    showHoroscope(document.horoscopeForm);
  }
  else
  { var objectName = "document.pix_" + picNum;
    if(showSide)
    { eval(objectName).src = "images/im_side.gif";
      showSide=false;
    }
    else
    { if(picNum==animalNum)
        eval(objectName).src = "images/im_" + picNum + "_fcs.gif";
      else
        eval(objectName).src = "images/im_" + picNum + ".gif";
      
      showSide=true;
      picNum++;
    }
  }
}

/*************************/
/*     showHoroscope     */
/*************************/
function showHoroscope(cur)
{ document.animalImg.src = "images/ani_" + animalNum + ".gif";

  //The Year of the Animal - Title & Description
  cur.theYearTitle.value = zodiacAnimals[animalNum-1];
  cur.theYearDesc.value  = "People born in the year of the "
                         + zodiacAnimals[animalNum-1]
                         + " are\n"
                         + commentsOfTheYear[animalNum-1];

  //Influential Element - Title
  cur.elementTitle.value = infElements[elementNum-1];
  //Influential Element - Description
  var ary = (influentialElements[animalNum-1]).split('|');
  var ary2 = (ary[elementNum-1]).split(',');
  cur.elementDesc.value  = "Traits of "
                         + infElements[elementNum-1] + " "
                         + zodiacAnimals[animalNum-1]
                         + " are\n"
                         + ary2[0] + ", "
                         + ary2[1] + ", and "
                         + ary2[2];

  //Compatibility Meter - Title
  var str = compatibilities[animalNum-1]
  cur.compatiTitle.value = "from best["
                         + zodiacAnimals[hexToDec(str.charAt(0))-1]
                         + "] to worst["
                         + zodiacAnimals[hexToDec(str.charAt(11))-1]
                         + "]";
  //Compatibility Meter - Description
  var ans = "";
  var num;
  for(i=0; i<12; i++) 
  { num = hexToDec(str.charAt(i));
    ans += zodiacAnimals[num-1];
    if(i != 11) ans += ", ";
  }
  cur.compatibDesc.value = ans;

  //Fixed Elements Description
  ary = (fixedElements[animalNum-1]).split('|');
  
  cur.etcDesc.value = "+ Direction:\n"
                    + "    "          + ary[0] + "\n"
                    + "+ Season   : " + ary[1] + "\n"
                    + "+ Month    : " + ary[2] + "\n"
                    + "+ Elements : " + ary[3] + "\n"
                    + "+ Stem     : " + ary[4];

  //The Hours of the Animal - Title & Description
  var hourNum = cur.birthHour.selectedIndex;
  if(hourNum == 0) 
    cur.theHourDesc.value = "To see this horoscope, please select your hours of birth.";
  else
  { cur.theHourTitle.value = zodiacAnimals[hourNum-1];
    num = (animalNum-1)*12 + (hourNum-1);
    cur.theHourDesc.value = commentsOfTheHour[num];
  }
}

/*************************/
/*        hexToDec       */
/*************************/
function hexToDec(num)
{ //convert hex number to dec (1 digit only)
  switch(num)
  { case 'A' : num = 10; break;
    case 'B' : num = 11; break;
    case 'C' : num = 12; break;
  }
  return num;
}