//bad coding ~~~
var ctemp;

/*
 * makePost is the only function called by other scripts to create
 * comments form.  It will decide which form is needed and return
 * the div for it.  It needs to be passed the id of the rant (rrid)
 * in order for it to know which post it belongs to. (rid is a temp
 * variable that can't be depended on to remember the correct rant,
 * use rrid instead)
 */
function makePost( rrid ) {
  var username;

  //check for cookie data
  username = GetCookie( "username" );
  if ( !username ) {
    username = "Anonymous";
  }

  //by now, username is known
  return makePostForm( username, rrid );
}

function makeLogin( rrid ) {
  var postformdiv = document.createElement( "div" );

  var ctitle = document.createElement( "b" );
  ctitle.appendChild( document.createTextNode( "Comments Login:" ) );
  postformdiv.appendChild( ctitle );
  postformdiv.appendChild( document.createElement( "br" ) );

  var tinyspan = document.createElement( "span" );
  tinyspan.setAttribute( "class", "tiny" );
  tinyspan.className = "tiny";
  tinyankh = document.createElement( "a" );
  tinyankh.onclick = function(){ this.parentNode.parentNode.parentNode.replaceChild( makeRegister( rrid ), this.parentNode.parentNode ); };
  tinyankh.style.cursor = "pointer";
  tinyankh.appendChild( document.createTextNode( "click here to register" ) );
  tinyspan.appendChild( tinyankh );
  postformdiv.appendChild( tinyspan );

  tinyspan.appendChild( document.createTextNode( " / " ) );

  tinyankh = document.createElement( "a" );
  tinyankh.onclick = function(){ this.parentNode.parentNode.parentNode.replaceChild( makePost( rrid ), this.parentNode.parentNode ); };
  tinyankh.style.cursor = "pointer";
  tinyankh.appendChild( document.createTextNode( "click here to post anonymously" ) );
  tinyspan.appendChild( tinyankh );

  var postform = document.createElement( "form" );
  postform.setAttribute( "name", "clogin" );
  postform.onsubmit = function(){ return cloginn( this, rrid ); };

  postform.appendChild( document.createTextNode( "User: " ) );
  var usernameinput = document.createElement( "input" );
  usernameinput.setAttribute( "name", "cuser" );
  usernameinput.setAttribute( "id", "cuser" );
  usernameinput.setAttribute( "type", "text" );
  usernameinput.style.marginRight = "20px";
  postform.appendChild( usernameinput );

  postform.appendChild( document.createTextNode( "Password: " ) );
  var passwordinput = document.createElement( "input" );
  passwordinput.setAttribute( "name", "cpassword" );
  passwordinput.setAttribute( "id", "cpassword" );
  passwordinput.setAttribute( "type", "password" );
  passwordinput.style.marginRight = "20px";
  postform.appendChild( passwordinput );

  var submitinput = document.createElement( "input" );
  submitinput.setAttribute( "type", "submit" );
  submitinput.setAttribute( "value", "Login" );
  postform.appendChild( submitinput );

  postformdiv.appendChild( postform );

  return postformdiv;
}

function makeRegister( rrid ) {
  var postformdiv = document.createElement( "div" );

  var ctitle = document.createElement( "b" );
  ctitle.appendChild( document.createTextNode( "Comments Register:" ) );
  postformdiv.appendChild( ctitle );
  postformdiv.appendChild( document.createElement( "br" ) );

  var tinyspan = document.createElement( "span" );
  tinyspan.setAttribute( "class", "tiny" );
  tinyspan.className = "tiny";
  tinyankh = document.createElement( "a" );
  tinyankh.onclick = function(){ this.parentNode.parentNode.parentNode.replaceChild( makeLogin( rrid ), this.parentNode.parentNode ); };
  tinyankh.style.cursor = "pointer";
  tinyankh.appendChild( document.createTextNode( "click here to login" ) );
  tinyspan.appendChild( tinyankh );

  tinyspan.appendChild( document.createTextNode( " / " ) );

  tinyankh = document.createElement( "a" );
  tinyankh.onclick = function(){ this.parentNode.parentNode.parentNode.replaceChild( makePost( rrid ), this.parentNode.parentNode ); };
  tinyankh.style.cursor = "pointer";
  tinyankh.appendChild( document.createTextNode( "click here to post anonymously" ) );
  tinyspan.appendChild( tinyankh );

  postformdiv.appendChild( tinyspan );

  var postform = document.createElement( "form" );
  postform.setAttribute( "name", "cregister" );
  postform.onsubmit = function(){ return cregisterr( this, rrid ); };

  postform.appendChild( document.createTextNode( "User: " ) );
  var usernameinput = document.createElement( "input" );
  usernameinput.setAttribute( "name", "cuser" );
  usernameinput.setAttribute( "id", "cuser" );
  usernameinput.setAttribute( "type", "text" );
  usernameinput.style.marginRight = "20px";
  postform.appendChild( usernameinput );

  postform.appendChild( document.createTextNode( "Email*: " ) );
  var emailinput = document.createElement( "input" );
  emailinput.setAttribute( "name", "cemail" );
  emailinput.setAttribute( "id", "cemail" );
  emailinput.setAttribute( "type", "text" );
  emailinput.style.marginRight = "20px";
  postform.appendChild( emailinput );

  postform.appendChild( document.createElement( "br" ) );

  postform.appendChild( document.createTextNode( "Password: " ) );
  var passwordinput = document.createElement( "input" );
  passwordinput.setAttribute( "name", "cpassword" );
  passwordinput.setAttribute( "id", "cpassword" );
  passwordinput.setAttribute( "type", "password" );
  passwordinput.style.marginRight = "20px";
  postform.appendChild( passwordinput );

  postform.appendChild( document.createTextNode( "Confirm Password: " ) );
  var password2input = document.createElement( "input" );
  password2input.setAttribute( "name", "cpassword2" );
  password2input.setAttribute( "id", "cpassword2" );
  password2input.setAttribute( "type", "password" );
  password2input.style.marginRight = "20px";
  postform.appendChild( password2input );

  postform.appendChild( document.createElement( "br" ) );

  postform.appendChild( document.createTextNode( "*Email is not required, but if you forget your password, I will not be able to send it to you." ) );
  postform.appendChild( document.createElement( "br" ) );

  var submitinput = document.createElement( "input" );
  submitinput.setAttribute( "type", "submit" );
  submitinput.setAttribute( "value", "Register" );
  postform.appendChild( submitinput );

  postformdiv.appendChild( postform );

  return postformdiv;  
}

function makePostForm( username, rrid ) {
  var postformdiv = document.createElement( "div" );

  var ctitle = document.createElement( "b" );
  ctitle.appendChild( document.createTextNode( "Post a Comment:" ) );
  postformdiv.appendChild( ctitle );
  postformdiv.appendChild( document.createElement( "br" ) );

  if ( username == "Anonymous" ) { //if this is an anonymous user, let them register/login
    var tinyspan = document.createElement( "span" );
    tinyspan.setAttribute( "class", "tiny" );
    tinyspan.className = "tiny";
    tinyankh = document.createElement( "a" );
    tinyankh.onclick = function(){ this.parentNode.parentNode.parentNode.replaceChild( makeLogin( rrid ), this.parentNode.parentNode ); };
    tinyankh.style.cursor = "pointer";
    tinyankh.appendChild( document.createTextNode( "click here to login" ) );
    tinyspan.appendChild( tinyankh );

    tinyspan.appendChild( document.createTextNode( " / " ) );

    tinyankh = document.createElement( "a" );
    tinyankh.onclick = function(){ this.parentNode.parentNode.parentNode.replaceChild( makeRegister( rrid ), this.parentNode.parentNode ); };
    tinyankh.style.cursor = "pointer";
    tinyankh.appendChild( document.createTextNode( "click here to register" ) );
    tinyspan.appendChild( tinyankh );

    postformdiv.appendChild( tinyspan );

  } else { //else this is a user, let them log out
    var tinyspan = document.createElement( "span" );
    tinyspan.setAttribute( "class", "tiny" );
    tinyspan.className = "tiny";
    tinyankh = document.createElement( "a" );
    tinyankh.onclick = function(){ logout( this, rrid ); };
    tinyankh.style.cursor = "pointer";
    tinyankh.appendChild( document.createTextNode( "click here to logout" ) );
    tinyspan.appendChild( tinyankh );
    postformdiv.appendChild( tinyspan );
  }

  var postform = document.createElement( "form" );
  postform.setAttribute( "name", "cpost" );
  postform.onsubmit = function(){ return cpostt( this, rrid ); };
  //postform.onsubmit = function(){ return cpostt( 7, 7 ); };

  postform.appendChild( document.createTextNode( "User: " + username ) );
  postform.appendChild( document.createElement( "br" ) );

  postform.appendChild( document.createTextNode( "Comment: " ) );
  postform.appendChild( document.createElement( "br" ) );
  var commentinput = document.createElement( "textarea" );
  commentinput.setAttribute( "name", "ccomment" );
  commentinput.setAttribute( "id", "ccomment" );
  commentinput.style.width = "70%";
  commentinput.style.height = "5em";
  postform.appendChild( commentinput );

  postform.appendChild( document.createElement( "br" ) );

  var cuidinput = document.createElement( "input" );
  cuidinput.setAttribute( "name", "cusername" );
  cuidinput.setAttribute( "id", "cusername" );
  cuidinput.setAttribute( "type", "hidden" );
  cuidinput.setAttribute( "value", username );
  postform.appendChild( cuidinput );

  var cridinput = document.createElement( "input" );
  cridinput.setAttribute( "name", "rid" );
  cridinput.setAttribute( "id", "rid" );
  cridinput.setAttribute( "type", "hidden" );
  cridinput.setAttribute( "value", rrid );
  postform.appendChild( cridinput );

  var submitinput = document.createElement( "input" );
  submitinput.setAttribute( "type", "submit" );
  submitinput.setAttribute( "value", "Post Comment" );
  postform.appendChild( submitinput );

  postformdiv.appendChild( postform );

  return postformdiv;  
}


function cloginn( f, rrid ) {
  //alert( "I will now try to LOGIN using the username " + f.cuser.value + " and the password " + f.cpassword.value );
  //alert( "cuser=" + f.cuser.value + "&cpassword=" + f.cpassword.value );

  //store the rant id into rid
  rid = rrid;

  //try to login
  thedata = "";
  var status = sendData( "login.php", "cuser=" + f.cuser.value + "&cpassword=" + f.cpassword.value );
  ctemp = f;
  //alert( "about to try" );
  waitForData( "clogin2();" );

  return false;
}

function clogin2() {
  //alert( "From clogin2(): " + thedata );
  eval( thedata );
  //check the status
  if ( echostring[0] == 1 ) {
    SetCookie( "username", echostring[1] );
    //alert( GetCookie( "username" ) );
  } else {
    alert( "Invalid username or password." );
  }

  f = ctemp;
  var newForm = makePost( rid );
  f.parentNode.parentNode.replaceChild( newForm, f.parentNode );
  return false;
}

function cregisterr( f, rrid ) {
  //alert( "I will now try to REGISTER using the username " + f.cuser.value + " and the password " + f.cpassword.value + "/" + f.cpassword2.value + " and the email address " + f.cemail.value );

  //store the current rant id into rid
  rid = rrid;

  //try to register
  thedata = "";
  var status = sendData( "register.php", "cuser=" + f.cuser.value + "&cemail=" + f.cemail.value + "&cpassword=" + f.cpassword.value + "&cpassword2=" + f.cpassword2.value );
  ctemp = f;
  //alert( "about to try" );
  waitForData( "cregister2();" );

  return false;
}

function cregister2() {
  //alert( thedata );
  eval( thedata );
  //check the status
  if ( reginfo[0] == 1 ) {
    alert( reginfo[1] );
    SetCookie( "username", ctemp.cuser.value );
  } else {
    alert( reginfo[1] );
  }

  if ( reginfo[0] == 1 ) {
    f = ctemp;
    var newForm = makePost( rid );
    f.parentNode.parentNode.replaceChild( newForm, f.parentNode );
  }

  return false;
}

function cpostt( f, rrid ) {
  //alert( "I will now try to POST using the username " + f.cusername.value + " and the message " + f.ccomment.value + " into the rant with id #" + f.rid.value );

  //alert( "inside cpost" );

  //store the current rant id into rid
  rid = rrid;

  //try to register
  thedata = "";
  var status = sendData( "postcomment.php", "cusername=" + f.cusername.value + "&ccomment=" + f.ccomment.value + "&rid=" + f.rid.value );
  ctemp = f;
  //alert( "about to try" );
  waitForData( "cpost2();" );

  return false;
}

function cpost2() {
  //alert( thedata );
  eval( thedata );
  //var postresults = Array( 0, "Comments are borked right now..." );
  //check the status
  if ( postresults[0] == 1 ) {
    alert( postresults[1] );
    cdiv = ctemp.parentNode.parentNode.parentNode;
    hideComments( rid, ctemp.parentNode.parentNode.getElementsByTagName("a")[0] );
    comments( rid, cdiv.getElementsByTagName("div")[cdiv.getElementsByTagName("div").length-1].getElementsByTagName("a")[0] );
  } else {
    alert( postresults[1] );
  }

  return false;
}

function logout( a, rrid ) {
  alert( "Clearing COOKIE information..." );
  DeleteCookie( "username" );
  var newForm = makePost( rrid );
  a.parentNode.parentNode.parentNode.replaceChild( newForm, a.parentNode.parentNode );
}