var xhttp = false;
var i = false;

function createHTTPObject() {
  if (navigator.appName == "Microsoft Internet Explorer") {
    xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    xhttp = new XMLHttpRequest();
  }
}


function $x(x) {
  if (document.getElementById) {
    try {
      return document.getElementById(x);
    } catch (e) {
      return false;
    }
  }
}


function sndPostDirect(rURL, post) {
  createHTTPObject();

  if (rURL.indexOf("?") == -1) {
    xhttp.open('post', rURL+"?ms="+new Date().getTime());
  } else {
    xhttp.open('post', rURL+"&ms="+new Date().getTime());
  }

  xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhttp.onreadystatechange = handleResponse;
  try { xhttp.send(post); } catch(err) { return; }
}

function handleResponse() {
  if (xhttp.readyState == 4) {
    var response = xhttp.responseText;
    var update = new Array();
    if (response.indexOf('|||') != -1) {
      update = response.split('|||');
      if (update[0] == 'redirect') {
        window.location = update[1];
      } else {
        if (!$x(update[0])) { return; }
               else {       $x(update[0]).innerHTML = update[1]; }
      }
    }
  }

}

function register_validate() {
  email = $x('r_email').value;
  username = $x('r_username').value;
  password = $x('r_password').value;
  confirm_password = $x('r_confirm_password').value;
  comment_text = $x('comment_text').value;
	
	// Prototype String method: str.blank() (detects if str is empty or contains only whitespace)
  if (comment_text.blank()) {
    alert("Your comment must be filled out");
    return false;
  } else if (email.blank()) {
    alert("Your email must be filled out.");
    return false;
  } else if (username.blank()) {
    alert("Your username must be filled out.");
    return false;
  } else if (password.blank()) {
    alert("Your password must be filled out.");
    return false;
  } else if (confirm_password == 0) {
    alert("You must re-enter your password.");
    return false;
  } else if (password != confirm_password) {
    alert("Your passwords must match.");
    return false;
  } else {
    return true;
  }
}

function signin_validate() {
  username = $x('r_username').value;
  comment_text = $x('comment_text').value;
  password = $x('r_password').value;
	
	// Prototype String method: str.blank() (detects if str is empty or contains only whitespace)
  if (comment_text.blank()) {
    alert("Your comment must be filled out.");
  } else if (username.blank()) {
    alert("Your username must be filled out.");
  } else if (password.blank()) {
    alert("Your password must be filled out.");
  } else {
    return true;
  }
  return false;
}

function comment_validate() {
  comment_text = $x('comment_text').value;

  if (comment_text.blank()) {
    alert("Your comment must be filled out.");
    return false;
  } else {
    return true;
  }
}

function comment() {

  var postString = "action=comment";
	
  postString 		+= "&comment_text="+		 escape( $x('comment_text').value );
  postString 		+= "&articleid="+				 $x('articleid').value;
	postString 		+= "&articleurl="+			 $x('articleurl').value;
	postString 		+= "&articletitle="+		 $x('articletitle').value;
	
	postString 		+= "&comment_reply_to="+ $x('commentReplyTo').value;
	
	postString += "&phone1="+escape($x('phone1').value);
  postString += "&phone2="+escape($x('phone2').value);
  postString += "&phone3="+escape($x('phone3').value);
	postString += "&full_name="+escape($x('full_name').value);
	
	postString += "&city="+escape($x('city').value);
	postString += "&state="+escape($x('state').value);

  sndPostDirect('/js/articles/users_comment.php', postString);

  return false;
}


function signin() {
  comment_text = $x('comment_text').value;
  username = $x('r_username').value;
  password = $x('r_password').value;
  articleid = $x('articleid').value;

  var postString = "action=signin";
  postString += "&username="+escape(username);
  postString += "&password="+escape(password);
  postString += "&comment_text="+escape(comment_text);
  postString += "&articleid="+escape(articleid);
	postString += "&articleurl="+			 escape($x('articleurl').value);
	postString += "&articletitle="+		 escape($x('articletitle').value);
  postString += "&comment_reply_to="+ escape($x('commentReplyTo').value);
	
  postString += "&phone1="+escape($x('phone1').value);
  postString += "&phone2="+escape($x('phone2').value);
  postString += "&phone3="+escape($x('phone3').value);
	postString += "&full_name="+escape($x('full_name').value);
	
	postString += "&city="+escape($x('city').value);
	postString += "&state="+escape($x('state').value);
	
  sndPostDirect('/js/articles/users_signin.php', postString);
}

function register_user() {
  gender = "";
  if ($x('gender_male')) {
    gender = "Male";
  } else if ($x('gender_female')) {
    gender = "Female";
  }
  email = $x('r_email').value;
  username = $x('r_username').value;
  password = $x('r_password').value;
  confirm_password = $x('r_confirm_password').value;
  comment_text = $x('comment_text').value;
  year_of_birth = $x('a_date').value;
  zip_code = $x('zip_code').value;
  phone1 = $x('phone1').value;
  phone2 = $x('phone2').value;
  phone3 = $x('phone3').value;

  articleid = $x('articleid').value;

  var postString = "action=register";
  postString += "&email="+escape(email);
  postString += "&username="+escape(username);
  postString += "&password="+escape(password);
  postString += "&confirm_password="+escape(confirm_password);
  postString += "&comment_text="+escape(comment_text);
  postString += "&gender="+escape(gender);
  postString += "&birthyear="+escape(year_of_birth);
  postString += "&zip="+escape(zip_code);
  postString += "&phone1="+escape(phone1);
  postString += "&phone2="+escape(phone2);
  postString += "&phone3="+escape(phone3);
	postString += "&full_name="+escape($x('full_name').value);
	
	postString += "&city="+escape($x('city').value);
	postString += "&state="+escape($x('state').value);
	
  postString += "&articleid="+escape(articleid);
	postString += "&articleurl="+			 escape($x('articleurl').value);
	postString += "&articletitle="+		 escape($x('articletitle').value);
	postString += "&comment_reply_to="+ escape($x('commentReplyTo').value);

  sndPostDirect('/js/articles/register.php', postString);
  window.location.hash="CommentsArea";
}

function addl_info() {
  income = $x('a_income')[$x('a_income').selectedIndex].value;
  subscriber = $x('a_subscriber')[$x('a_subscriber').selectedIndex].value;
  user_id = $x('user_id').value;
  var postString = "action=additional_info";
  postString += "&income="+escape(income);
  postString += "&subscriber="+escape(subscriber);
  postString += "&user_id="+escape(user_id);

  sndPostDirect('/js/articles/users_additional.php', postString);
  window.location.hash="CommentsArea";
  return false;
}

// Switches over the comment form from registration to sign_in
function sign_in() {
	
	var postString = 'articleid='+$x('articleid').value;
	postString 		+= "&articleurl="+			 escape($x('articleurl').value);
	postString 		+= "&articletitle="+		 escape($x('articletitle').value);
	postString 		+= "&comment_text="+		 escape($x('comment_text').value);
	
  sndPostDirect('/js/articles/users_signin.php', postString);
}
// Switches over the comment form from sign_in back to registration
function go_back_to_register() {
	
	var postString = 'articleid='+$x('articleid').value;
	postString 		+= "&articleurl="+			 escape($x('articleurl').value);
	postString 		+= "&articletitle="+		 escape($x('articletitle').value);
	postString 		+= "&comment_text="+		 escape($x('comment_text').value);
	
  sndPostDirect('/js/articles/go_back_to_register.php', postString);
}


