// Chartoons JavaScript
// Keith Enevoldsen, thinkzone.wlonk.com

linenum=1;
maxlines=0;

function init(form)
{
form.Samples.selectedIndex=1;
generate_input(form, form.Samples.options[form.Samples.selectedIndex].text);
}


function generate_input(form, sample_name)
{
if (sample_name == "Bowler") {

form.intext.value =
" THE BOWLER"
+"\n io       ||||"
+"\n  io      ||||"
+"\n   io     ||||"
+"\n   i o    ||||"
+"\n   i  o   ||||"
+"\n   i   o  ||||"
+"\n   i    o ||||"
+"\n   i     o/|||"
+"\n   i      o/||"
+"\n   i      _o/|"
+"\n   i      __o/"
+"\n   i      ___o"
+"\n   Y      ____o"
+"\n THE END";

} else if (sample_name == "Archer") {

form.intext.value =
" THE ARCHER"
+"\n      i)        O"
+"\n      iD        O"
+"\n      i)-       O"
+"\n      i) -      O"
+"\n      i)  -     O"
+"\n      i)   -    O"
+"\n      i)    -   O"
+"\n      i)     -  O"
+"\n      i)      - O"
+"\n      i)       -O"
+"\n      i)       ~O"
+"\n      i)      - O"
+"\n      i)     -  O"
+"\n      i)    -   O"
+"\n      Y_   -    O"
+"\n     i _  -     O"
+"\n     i _ -      O"
+"\n    i  _-       O"
+"\n    i  -        O"
+"\n   i  -_        O"
+"\n   i - _        O"
+"\n  i -  _        O"
+"\n  i-   _        O"
+"\n  X    _        O"
+"\n -X    _        O"
+"\n- X    _        O"
+"\n  x    _        O"
+"\n  _    _        O"
+"\n THE END";

} else if (sample_name == "Melodrama") {

form.intext.value =
" LOVE ON THE LINE"
+"\n A Char-Toon Melodrama by Keith Enevoldsen"
+"\n"
+"\n Cast of Characters:"
+"\n \"&\" - Annabelle"
+"\n \"!\" - Harold"
+"\n \"R\" - Mr. Rich, Annabelle's Father"
+"\n \"X\" - Mr. X"
+"\n"
+"\n ------------------------------------------------------------"
+"\n Scene 1 - The Parlor of Mr. Rich"
+"\n     !      &"
+"\n      !     &"
+"\n       !    &"
+"\n Harold: \"Oh, Annabelle!\""
+"\n       !   &"
+"\n       !  &"
+"\n       ! &"
+"\n Annabelle: \"Oh, Harold!\""
+"\n        !&"
+"\n        &!"
+"\n        !&"
+"\n        &!"
+"\n  R     &!"
+"\n   R    &!"
+"\n    R   &!"
+"\n Mr. Rich: \"Hands off my daughter!\""
+"\n    R   &!"
+"\n    R  & !"
+"\n    R  &  !"
+"\n Annabelle: \"Oh, Father!\""
+"\n    R  &  !"
+"\n     R &  !"
+"\n      R&  !"
+"\n       &R !"
+"\n       & R!"
+"\n       &  R!"
+"\n       &   R!"
+"\n       &    R!"
+"\n       &     R!"
+"\n       &     R !"
+"\n       &     R  !"
+"\n       &     R   !"
+"\n Mr. Rich: \"Get out, you cad!\""
+"\n       &     R"
+"\n       &    R"
+"\n       &   R"
+"\n       &  R"
+"\n      &  R"
+"\n     &   R"
+"\n Mr. Rich: \"Annabelle, it's for the best.\""
+"\n     &   R"
+"\n     &  R"
+"\n    &   R"
+"\n    &  R"
+"\n   &   R"
+"\n Annabelle: \"Boo hoo! I shall run away!\""
+"\n   &   R"
+"\n  &    R"
+"\n &     R"
+"\n       R"
+"\n"
+"\n ------------------------------------------------------------"
+"\n Scene 2 - A Dark Alley"
+"\n           X"
+"\n          X"
+"\n         X"
+"\n        X"
+"\n         X"
+"\n          X"
+"\n           X"
+"\n          X"
+"\n         X"
+"\n          X"
+"\n           X"
+"\n Mr. X: \"I must hatch a scheme!\""
+"\n &          X"
+"\n  &          X"
+"\n   &         X"
+"\n    &       X"
+"\n     &     X"
+"\n     &    X"
+"\n Mr. X: \"Who goes there?\""
+"\n     &    X"
+"\n      &   X"
+"\n Annabelle: \"It is only I, poor mistreated Annabelle!\""
+"\n      &   X"
+"\n      &  X"
+"\n Mr. X: \"(Hmmm, Annabelle Rich!) Come with me, my sweet.\""
+"\n      &  X"
+"\n       & X"
+"\n        & X"
+"\n         & X"
+"\n          & X"
+"\n"
+"\n ------------------------------------------------------------"
+"\n Scene 3 - The Parlor of Mr. Rich"
+"\n       R"
+"\n        R"
+"\n         R"
+"\n        R"
+"\n       R"
+"\n The doorbell rings."
+"\n      R"
+"\n     R"
+"\n    R"
+"\n   R"
+"\n  R"
+"\n R"
+"\n Mr. Rich: \"A letter for me? Thank you.\""
+"\n R-"
+"\n  R-"
+"\n   R-"
+"\n    R-"
+"\n    R_"
+"\n   R _"
+"\n Mr. Rich: \"A ransom note! I must rescue her!\""
+"\n   R _"
+"\n  R  _"
+"\n R   _"

+"\n     _"
+"\n"
+"\n ------------------------------------------------------------"
+"\n Scene 4 - The Railroad Tracks"
+"\n _____X__&___________________"
+"\n ______X_&___________________"
+"\n _____X__&___________________"
+"\n ______X_&___________________"
+"\n Annabelle: \"Please, un-tie me!\""
+"\n _____X__&___________________"
+"\n ______X_&___________________"
+"\n _____X__&___________________"
+"\n ______X_&___________________"
+"\n Mr. X: \"Your father is coming now.\""
+"\n R_____X_&___________________"
+"\n $R____X_&___________________"
+"\n _$R___X_&___________________"
+"\n __$R__X_&___________________"
+"\n __$_R_X_&___________________"
+"\n Mr. Rich: \"Here is the money. Release my daughter!\""
+"\n __$_RX__&___________________"
+"\n __$_XR__&___________________"
+"\n __$__XR_&___________________"
+"\n __$___XR&___________________"
+"\n Mr. X: \"I'll tie you both down!\""
+"\n __$___XR&___________________"
+"\n __$__XR_&___________________"
+"\n __$__RX_&___________________"
+"\n __$_XR__&___________________"
+"\n __$_RX__&___________________"
+"\n __$__XR_&___________________"
+"\n __$__RX_&___________________"
+"\n __$__XR_&___________________"
+"\n __$___XR&___________________"
+"\n __$__X_R&___________________"
+"\n Mr. X: \"Goodbye! Happy 'rails to you! HA HA HA!\""
+"\n __$__X_R&___________________"
+"\n __$_X__R&___________________"
+"\n __$X___R&___________________"
+"\n _$X____R&___________________"
+"\n $X_____R&___________________"
+"\n X______R&___________________"
+"\n _______R&___________________"
+"\n _______R&___________________"
+"\n _______R&__________________c"
+"\n _______R&_________________ch"
+"\n _______R&________________cho"
+"\n _______R&_______________choo"
+"\n _______R&______________choo-"
+"\n _______R&_____________choo-c"
+"\n _______R&____________choo-ch"
+"\n _______R&___________choo-cho"
+"\n _______R&__________choo-choo"
+"\n _______R&_________choo-choo_"
+"\n _______R&________choo-choo__"
+"\n !______R&_______choo-choo___"
+"\n __!____R&______choo-choo____"
+"\n ____!__R&_____choo-choo_____"
+"\n ______!R&____choo-choo______"
+"\n _______R&!__choo-choo_______"
+"\n _______R&_!_choo-choo_______"
+"\n _______R&!__choo-choo_______"
+"\n _______R!&__choo-choo_______"
+"\n ______R!_&__choo-choo_______"
+"\n _____R_!_&__choo-choo_______"
+"\n Annabelle: \"Harold! My hero!\""
+"\n _____R_!_&__choo-choo_______"
+"\n _____R_!&___choo-choo_______"
+"\n _____R_&!___choo-choo_______"
+"\n _____R_!&___choo-choo_______"
+"\n Harold: \"Get on board! Maybe we can catch Mr. X!\""
+"\n _____R_!&___choo-choo_______"
+"\n ______R_!&__choo-choo_______"
+"\n _______R_!&_choo-choo_______"
+"\n ________R_!&choo-choo_______"
+"\n _________R_!choo-choo_______"
+"\n __________R_choo-choo_______"
+"\n ___________Rchoo-choo_______"
+"\n ____________choo-choo_______"
+"\n ___________choo-choo________"
+"\n __________choo-choo_________"
+"\n _________choo-choo__________"
+"\n _______choo-choo____________"
+"\n _____choo-choo______________"
+"\n ___choo-choo________________"
+"\n _choo-choo__________________"
+"\n hoo-choo____________________"
+"\n o-choo______________________"
+"\n choo________________________"
+"\n oo__________________________"
+"\n ____________________________"
+"\n"
+"\n Meanwhile, farther down the line..."
+"\n ______________$X____________"
+"\n _____________$X_____________"
+"\n ____________$X______________"
+"\n Mr. X: \"Mr. Rich should be 'flat broke' by now! HA HA HA!\""
+"\n ____________$X______________"
+"\n ___________$X______________c"
+"\n __________$X____________choo"
+"\n _________$X__________choo-ch"
+"\n ________$X________choo-choo_"
+"\n _______$X______choo-choo____"
+"\n ______$X____choo-choo_______"
+"\n _____$X__choo-choo__________"
+"\n ____$Xchoo-choo_____________"
+"\n ___choo-choo________________"
+"\n choo-choo___________________"
+"\n o-choo______________________"
+"\n hoo__.______________________"
+"\n _____.______________________"
+"\n"
+"\n ------------------------------------------------------------"
+"\n Scene 5 - The Railway Station"
+"\n       R   !&"
+"\n       R  ! &"
+"\n       R !  &"
+"\n Harold: \"Sir, may I have your daughter's 'and in marriage?\""
+"\n Mr. Rich: \"Why certainly ... Son!\""
+"\n       R  ! &"
+"\n       R  !&"
+"\n A close-up of the happy couple:"
+"\n   ( 00)    (00 )"
+"\n    ( 00)  (00 )"
+"\n     ( 00)(00 )"
+"\n     ( 0-)(00 )"
+"\n     ( 00)(00 )"
+"\n     ( 00)(-0 )"
+"\n     ( 00)(00 )"
+"\n & they lived happily ever after !"
+"\n THE END";

} else {
    clear_all(form);
}
count_lines(form);
first_line(form);
}

function clear_all(form)
{
form.Samples.value = "-";
form.intext.value = "";
form.linenumber.value = "";
maxlines = 0;
first_line(form);
}

function count_lines(form)
{
str = form.intext.value;
len = str.length;
maxlines = 1;
for (i = 0; i < len; i++) {
    if (str.charAt(i) == "\n") {
        maxlines++;
    }
}
if (str.charAt(len-1) == "\n") maxlines--;
}

function get_line(str, lnum)
{
len = str.length;
iline = 1; ichar = 0; jchar = -1;
for (i = 0; i < len; i++) {
    if (str.charAt(i) == "\n") {
        iline++;
        if (iline == lnum) {
            ichar = i + 1;
        } else if (iline == (lnum + 1)) {
            jchar = i - 1;
            if (str.charAt(jchar) == "\r") {
               jchar--;
            }
            break;
        }
    }
}
if (jchar < 0) jchar = len - 1;
// Note: substring() doesn't work consistently on all browsers
s = "";
for (i = ichar; i <= jchar; i++) {
   s = s + str.charAt(i);
}
return s;
}

function show_line(form)
{
form.linenumber.value = linenum;
form.oneline.value = get_line(form.intext.value, linenum);
}

function first_line(form)
{
linenum = 1;
show_line(form);
}

function prev_line(form)
{
if (linenum > 1) {
    linenum = linenum - 1;
    show_line(form);
}
}

function next_line(form)
{
count_lines(form);
if (linenum < maxlines) {
    linenum = linenum + 1;
    show_line(form);
}
}

function last_line(form)
{
count_lines(form);
linenum = maxlines;
show_line(form);
}