function createObject() {
     // Call this with something like:   var varname = createObject();
     var request_type;

     try{
          // Chrome, Opera, Firefox, Safari
          request_type = new XMLHttpRequest();
     }catch (e){
          // Internet Explorer Browsers
          try{
               request_type = new ActiveXObject("Msxml2.XMLHTTP");
          }catch (e){
               try{
                    request_type = new ActiveXObject("Microsoft.XMLHTTP");
               }catch (e){
                    // Something went wrong
                    alert("Unable to create ajax request. Please report this to the webmaster.");
                    return(false);
               }
          }
     }
     return request_type;
}

function LogoutUser(){
     var scriptURL = "script/logoutuser.php";

      // Create our ajax object
     var ajaxObject;
     if(ajaxObject = createObject()){
          // @TODO: See if they are logged in
  
          // This should alleviate the annoying cache thing in IE
          var nocache = Math.random();
          var postParam = "nocache="+nocache;

          ajaxObject.open("POST", scriptURL, true);

          //Send the proper header information along with the request
          ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          ajaxObject.setRequestHeader("Content-length", postParam.length);
          ajaxObject.setRequestHeader("Connection", "close");

          ajaxObject.onreadystatechange = function() {
               if(ajaxObject.readyState == 4 && ajaxObject.status == 200) {
                    var attemptReturn = ajaxObject.responseText.replace('\r\n','');
                    location.reload(true);
                    //alert(attemptReturn);
               }
          }
          ajaxObject.send(postParam);
     }
}

function LoginUser(){
     var scriptURL = "script/loginuser.php";

      // Create our ajax object
     var ajaxObject;
     if(ajaxObject = createObject()){
          // @TODO: See if they are logged in
          
          // Harvest form data
          var userid = document.getElementById('userid').value;
          var password = document.getElementById('userpass').value;
          
          // @TODO: Validate form          
  
          // This should alleviate the annoying cache thing in IE
          var nocache = Math.random();
          var postParam = "userid="+userid;
          postParam += "&userpass="+password;
          postParam += "&nocache="+nocache;

          ajaxObject.open("POST", scriptURL, true);

          //Send the proper header information along with the request
          ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          ajaxObject.setRequestHeader("Content-length", postParam.length);
          ajaxObject.setRequestHeader("Connection", "close");

          ajaxObject.onreadystatechange = function() {
               if(ajaxObject.readyState == 4 && ajaxObject.status == 200) {
                    var attemptReturn = ajaxObject.responseText.replace('\r\n','');
                     /* DEBUG: Show what is inside the response... 
                         alert(attemptReturn);
                    // End DEBUG */ 
                    if(attemptReturn == "true"){
                         //alert("Logged In!");
                         location.reload(true);
                    }else{
                         alert(attemptReturn);
                         document.getElementById("userid").focus();
                    }
               }
          }
          ajaxObject.send(postParam);
     }
}

function ResetNewUserForm(){
     document.getElementById('newuserid').value = null;
     document.getElementById('newuserpass').value = null;
     document.getElementById('newuserpass2').value = null;
     document.getElementById('newuserfname').value = null;
     document.getElementById('newuserlname').value = null;
     document.getElementById('newuseremail').value = null;
     document.getElementById('newusernotes').value = null;
}

function ResetBlogForm(){
     document.getElementById('title').value = null;
     document.getElementById('content').value = null;
}

function AddUser(){
     /*VALIDATIONS GO BELOW HERE */
     var uid = document.getElementById("newuserid").value;
     var pass = document.getElementById("newuserpass").value;
     var pass2 = document.getElementById("newuserpass2").value;
     var fname = document.getElementById("newuserfname").value;
     var lname = document.getElementById("newuserlname").value;
     var email = document.getElementById("newuseremail").value;
     if(!checkUserName(uid)){
          document.getElementById("newuserid").focus();
          return false;
     }
     if(!checkPwd(pass, pass2)){
          document.getElementById("newuserpass").value = "";
          document.getElementById("newuserpass2").value = "";
          document.getElementById("newuserpass").focus();
          return false;
     }
     if(!checkName(fname)){
          alert("Please enter a valid first name");
          document.getElementById("newuserfname").value = "";
          document.getElementById("newuserfname").focus();
          return false;
     }
     if(!checkName(lname)){
          alert("Please enter a valid last name");
          document.getElementById("newuserlname").value = "";
          document.getElementById("newuserlname").focus();
          return false;
     }
     if(!checkEmail(email)){
          alert("Please enter a valid email address");
          document.getElementById("newuseremail").value = "";
          document.getElementById("newuseremail").focus();
          return false;
     }
     /*VALIDATIONS GO ABOVE HERE */
     var scriptURL = "script/newuser.php";

     // Create our ajax object
     var ajaxObject;
     if(ajaxObject = createObject()){
          // @TODO: See if they are logged in
          
          // Harvest form data
          var userid = document.getElementById('newuserid').value;
          var password = document.getElementById('newuserpass').value;
          var password2 = document.getElementById('newuserpass2').value;
          var fname = document.getElementById('newuserfname').value;
          var lname = document.getElementById('newuserlname').value;
          var email = document.getElementById('newuseremail').value;
          var notes = document.getElementById('newusernotes').value;
          // @TODO: Validate form          
  
          // This should alleviate the annoying cache thing in IE
          var nocache = Math.random();
          var postParam = "newuserid="+userid;
          postParam += "&newuserpass="+password;
          postParam += "&newuserpass2="+password2;
          postParam += "&newuserfname="+fname;
          postParam += "&newuserlname="+lname;
          postParam += "&newuseremail="+email;
          postParam += "&newusernotes="+notes;     
          postParam += "&nocache="+nocache;

          ajaxObject.open("POST", scriptURL, true);

          //Send the proper header information along with the request
          ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          ajaxObject.setRequestHeader("Content-length", postParam.length);
          ajaxObject.setRequestHeader("Connection", "close");

          ajaxObject.onreadystatechange = function() {
               if(ajaxObject.readyState == 4 && ajaxObject.status == 200) {
                    var attemptReturn = ajaxObject.responseText.replace('\r\n','');
                     /* DEBUG: Show what is inside the response... 
                         alert(attemptReturn);
                    // End DEBUG */ 
                    if(attemptReturn == "true"){
                         alert("User Added!");
                         ResetNewUserForm();
                    }else{
                         alert("Add Failed! "+attemptReturn);
                         document.getElementById("newuserid").focus();
                    }
               }
          }
          ajaxObject.send(postParam);
     }
}

function SubmitPost(){

     /*VALIDATIONS GO BELOW HERE */
     var title = document.getElementById("title").value;
     var content = document.getElementById("content").value;
     
     if(!checkBlank(title)){
          alert("Hey buddy you forgot to enter a title.");
          document.getElementById("title").focus();
          return false;
     }
     if(!checkBlank(content)){
          alert("Hey buddy you might want to actually write something.");
          document.getElementById("content").focus();
          return false;
     }
     /*VALIDATIONS GO ABOVE HERE*/
     var scriptURL = "script/addpost.php";

     // Create our ajax object
     var ajaxObject;
     if(ajaxObject = createObject()){
          // @TODO: See if they are logged in
          
          // Harvest form data
          var blogtitle = document.getElementById('title').value;
          var blogcontent = document.getElementById('content').value;
          // @TODO: Validate form          
  
          // This should alleviate the annoying cache thing in IE
          var nocache = Math.random();
          var postParam = "title="+blogtitle;
          postParam += "&content="+blogcontent;
          postParam += "&nocache="+nocache;

          ajaxObject.open("POST", scriptURL, true);

          //Send the proper header information along with the request
          ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          ajaxObject.setRequestHeader("Content-length", postParam.length);
          ajaxObject.setRequestHeader("Connection", "close");

          ajaxObject.onreadystatechange = function() {
               if(ajaxObject.readyState == 4 && ajaxObject.status == 200) {
                    var attemptReturn = ajaxObject.responseText.replace('\r\n','');
                     /* DEBUG: Show what is inside the response... 
                         alert(attemptReturn);
                    // End DEBUG */ 
                    if(attemptReturn == "true"){
                         alert("Posted!");
                         ResetBlogForm();
                    }else{
                         alert("Post Failed! "+attemptReturn);
                         document.getElementById("title").focus();
                    }
               }
          }
          ajaxObject.send(postParam);
     }
}

// @TODO: Fix this or make it call a new page instead.
function DeletePost(postID){
      var scriptURL = "script/deletepost.php";

     // Create our ajax object
     var ajaxObject;
     if(ajaxObject = createObject()){
          // This should alleviate the annoying cache thing in IE
          var nocache = Math.random();
          var postParam = "websafetitle="+postID;
          postParam += "&nocache="+nocache;

          ajaxObject.open("POST", scriptURL, true);

          //Send the proper header information along with the request
          ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          ajaxObject.setRequestHeader("Content-length", postParam.length);
          ajaxObject.setRequestHeader("Connection", "close");

          ajaxObject.onreadystatechange = function() {
               if(ajaxObject.readyState == 4 && ajaxObject.status == 200) {
                    var attemptReturn = ajaxObject.responseText.replace('\r\n','');
                    // /* DEBUG: Show what is inside the response... 
                         alert(attemptReturn);
                    // End DEBUG */ 
                    if(attemptReturn == "true"){
                         alert("Deleted");
                         location.href="index.php?m=blog";
                    }else{
                         alert("Deletion Failed! "+attemptReturn);
                    }
               }
               if(ajaxObject.readyState == 4){
                    alert(ajaxObject.status);
               }
          }
          ajaxObject.send(postParam);
     }

}

function UploadPhoto(){
     var title = document.getElementById("comictitle").value;
     if(!checkBlank(title)){
          alert("Hey there big guy, you need to enter a title for your comic");
          document.getElementById("comictitle").focus();
          return false;
     }
     
     return true;
}

function checkEmail(email){
     apos=email.indexOf("@");
     dotpos=email.lastIndexOf(".");
  
     if (apos<1||dotpos-apos<2){
          //alert("Please enter a valid email address");
          //document.getElementById("e-mail").value = "";
          //form.email.focus();
          return false;
     } else return true;
}

function checkBlank(input){
     if(input==""){
          return false;
     }else{
          return true;
     }
}

function checkPwd(password, password2){
     if(password != password2){
          alert("Passwords not equal, try again");
          return false;
     }
     if(password.length < 6){
          alert("Password needs to be at least 6 characters");
          return false;
     }
     return true;
}

function checkUserName(username){
     var usernameExp = /^[A-z0-9_@-]{1,}$/;
     if(username.search(usernameExp)==-1){
          alert("Username not valid.")
          return false;
     } else return true;
}

function checkName(nme){
     var nameExp =  /^[A-z']{1,50}$/;
     if(nme.search(nameExp) ==-1){
          //alert("A name must be between 1 and 50 characters, letters only.");
          return false;
     } else return true;
}



