﻿/*******************************************************************************************
SUMMARY: 
    This file contains server-side JavaScript.
NOTES:
    The first two lines on .asp page to include this file should look something like this:
        @ language="javascript" (Actually an ASP Processing Directive)
        #INCLUDE VIRTUAL = "/c/siteSSJS.asp" (Actually a Server Side Include directive)
TOC:
    gob Initializing
    gob Database Stuff
    gob String Stuff
    gob Archives Stuff
    gob Miscellany
    Non-gob
MOD LOG: 
    20080608 1356 Protoyped String with .apose() which escapes apostrophes.  -gh@georgehernandez.com
*******************************************************************************************/

////gob Initializing
var gob = { };
gob.TestVariable = 'bar';
gob.TestFunction = function() {
    Response.Write('<p>foo'+gob.TestVariable+'</p>');
}

////gob Database Stuff
gob.Cnn;
//Open the connection.
gob.OpenConnection = function() {
    //var strConnection = ""; //DEV ONLY
    var strConnection = "[Not for public viewing]";
    //Response.Write("<p>strConnection: "+strConnection+"</p>"); Response.End(); //DEV ONLY
    gob.Cnn = Server.CreateObject("ADODB.Connection");
    gob.Cnn.CommandTimeout = 180;
    gob.Cnn.Open(strConnection);
}

////gob String Stuff
//Pad ID (str or int) with zeros on its left.
gob.Pad = function (ID, intPlaces) {
    //Assumes that places >= length
    var str = String(ID);
    var intLength = str.length;
    var intDifference = intPlaces-intLength;
    while (intDifference>0) {
        str = "0"+str;
        intDifference--;
    }
    return str;
}

////gob Archives
gob.Comment_GetCnt = function (commentedID) {
    var ReturnValue = "";
    var cmd = Server.CreateObject("ADODB.Command");
    cmd.ActiveConnection = gob.Cnn;
    cmd.CommandText = "Comment_GetCnt";
    cmd.CommandType = 4;

    var parIn = cmd.CreateParameter("commentedID", 3, 1, 4, commentedID);
    cmd.Parameters.Append(parIn);
    //Response.Write("<p>commentedID: "+parIn.Value+"</p>"); //DEV ONLY

    var rst = Server.CreateObject("ADODB.Recordset");
    rst = cmd.Execute();
    if (!rst.EOF) {
        ReturnValue = String(rst("cntComments"));
        if (ReturnValue == "null") ReturnValue = "";
    }
    rst.Close(); rst = null;
    return ReturnValue;
}
gob.Member_GetName = function (memberID) {
    var ReturnValue = "";
    var cmd = Server.CreateObject("ADODB.Command");
    cmd.ActiveConnection = gob.Cnn;
    cmd.CommandText = "Member_GetName";
    cmd.CommandType = 4;

    var parIn = cmd.CreateParameter("memberID", 3, 1, 4, memberID);
    cmd.Parameters.Append(parIn);
    //Response.Write("<p>memberID: "+parIn.Value+"</p>"); //DEV ONLY

    var rst = Server.CreateObject("ADODB.Recordset");
    rst = cmd.Execute();
    if (!rst.EOF) {
        ReturnValue = String(rst("displayedName"));
        if (ReturnValue == "null") ReturnValue = "";
    }
    rst.Close(); rst = null;
    return ReturnValue;
}
gob.Post_GetCnt = function (PostYear, PostMonth, tag, posterID) {
    var ReturnValue = "";
    
    //PostYear = Number(PostYear);
    //PostMonth = Number(PostMonth);
    if (tag==0) tag=null;
    if (posterID==0) posterID=null;
    
    var cmd = Server.CreateObject("ADODB.Command");
    cmd.ActiveConnection = gob.Cnn;
    cmd.CommandText = "Post_GetCnt";
    cmd.CommandType = 4;

    var parIn = cmd.CreateParameter("PostYear", 3, 1, 4, PostYear);
    cmd.Parameters.Append(parIn);
    //Response.Write("<p>PostYear: "+parIn.Value+"</p>"); //DEV ONLY
    
    var parIn2 = cmd.CreateParameter("PostMonth", 3, 1, 4, PostMonth);
    cmd.Parameters.Append(parIn2);
    //Response.Write("<p>PostMonth: "+parIn2.Value+"</p>"); //DEV ONLY
    
    var parIn3 = cmd.CreateParameter("tag", 200, 1, 64, tag);
    cmd.Parameters.Append(parIn3);
    //Response.Write("<p>tag: "+parIn3.Value+"</p>"); //DEV ONLY
    
    var parIn4 = cmd.CreateParameter("posterID", 3, 1, 4, posterID);
    cmd.Parameters.Append(parIn4);
    //Response.Write("<p>posterID: "+parIn4.Value+"</p>"); //DEV ONLY

    var rst = Server.CreateObject("ADODB.Recordset");
    rst = cmd.Execute();
    if (!rst.EOF) {
        ReturnValue = String(rst("cnt"));
        if (ReturnValue == "null" || ReturnValue == "0") ReturnValue = "";
    }
    rst.Close(); rst = null;
    return ReturnValue;
}
gob.Vote_GetAvg = function (votedID) {
    var ReturnValue = "";
    var cmd = Server.CreateObject("ADODB.Command");
    cmd.ActiveConnection = gob.Cnn;
    cmd.CommandText = "Vote_GetAvg";
    cmd.CommandType = 4;

    var parIn = cmd.CreateParameter("votedID", 3, 1, 4, votedID);
    cmd.Parameters.Append(parIn);
    //Response.Write("<p>votedID: "+parIn.Value+"</p>"); //DEV ONLY

    var rst = Server.CreateObject("ADODB.Recordset");
    rst = cmd.Execute();
    if (!rst.EOF) {
        ReturnValue = String(rst("avgVote"));
        if (ReturnValue == "null") ReturnValue = "";
    }
    rst.Close(); rst = null;
    return ReturnValue;
}


////gob Miscellany
//Send email
gob.SendEmail = function (Email) {
    //Using CDOSYS sending email thru a remote SMTP server:
    var objMessage=Server.CreateObject("CDO.Message");
    objMessage.Subject=Email.strSubject;
    objMessage.From=Email.strFrom;
    objMessage.To=Email.strTo;  //EG: "georgelhernandez@fake.com;smith@fake.com" 
    if ('strCc' in Email) objMessage.Cc=Email.strCc;
    if ('strBcc' in Email) objMessage.Bcc=Email.strBcc;
    if (Email.blnIsHtml) objMessage.HtmlBody=Email.strBody; //EG: "<html><body><p>Hello</p></body></html>"
    else objMessage.TextBody=Email.strBody; //EG: "world!";
    if ('strAttachment' in Email) objMessage.AddAttachment(Email.strAttachment); //EGs: "http://www.georgehernandez.com/h/gh.jpg" or "c:\secret.pdf"
    objMessage.Configuration.Fields.Item("http:/"+"/schemas.microsoft.com/cdo/configuration/sendusing")="[Not for public viewing]";
    objMessage.Configuration.Fields.Item("http:/"+"/schemas.microsoft.com/cdo/configuration/smtpserver")="[Not for public viewing]";
    objMessage.Configuration.Fields.Item("http:/"+"/schemas.microsoft.com/cdo/configuration/smtpserverport")="[Not for public viewing]";
    objMessage.Configuration.Fields.Item("http:/"+"/schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")="[Not for public viewing]";
    objMessage.Configuration.Fields.Item("http:/"+"/schemas.microsoft.com/cdo/configuration/smtpusessl")="[Not for public viewing]";
    objMessage.Configuration.Fields.Item("http:/"+"/schemas.microsoft.com/cdo/configuration/smtpauthenticate")="[Not for public viewing]";
    objMessage.Configuration.Fields.Item("http:/"+"/schemas.microsoft.com/cdo/configuration/sendusername")="[Not for public viewing]";
    objMessage.Configuration.Fields.Item("http:/"+"/schemas.microsoft.com/cdo/configuration/sendpassword")="[Not for public viewing]";
    objMessage.Configuration.Fields.Update();
    objMessage.Send();
}
//Display session variables for DEV ONLY
gob.CheckSessionVariables = function () {
    Response.Write("<p>"+Session.Contents.Count + " items in Session.Contents:<br />\r\n");
    Response.Write("Session(\"circle\"): "+Session("circle")+"<br />\r\n");
    Response.Write("Session(\"circleName\"): "+Session("circleName")+"<br />\r\n");
    Response.Write("Session(\"isGuilder\"): "+Session("isGuilder")+"<br />\r\n");
    Response.Write("Session(\"displayedName\"): "+Session("displayedName")+"<br />\r\n");
    Response.Write("Session(\"memberID\"): "+Session("memberID")+"<br />\r\n");
    Response.Write("</p>");
}
//Some date related arrays
gob.DAYNAME = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
gob.DAYNAME3 = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
gob.MONTHNAME = ["January","February","March","April","May","June","July","August","September","October","November","December"];
gob.MONTHNAME3 = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];


////Non-gob
//Add trim methods to String object
String.prototype.trim = function() {
    //return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
   return this.replace(/^\s+/g,"");
}
String.prototype.rtrim = function() {
   return this.replace(/\s+$/g,"");
}
String.prototype.apose = function() {
   return this.replace(/'/g,"''");
}
