﻿var MONTH_ARRAY = ["January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December"];
var DAYS_ARRAY = ["Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday"];
var DAYSUFFIX_ARRAY =[
	"" , "st" , "nd" , "rd" , "th" , "th" , "th" , "th" , "th" , "th" , "th", "th" , "th" , "th" , "th" , "th" , "th" , "th" , "th" , "th" , "th" , "st" , "nd" , "rd" , "th" , "th" , "th" , "th" , "th" , "th" , "th" , "st"
];
var SEARCHQUERYSTRING = ["q", "query", "p", "keywords"];

function writeToday() {
    var thisDateObject = new Date();
    var thisDate = thisDateObject.getDate();
    var thisDayIndex = thisDateObject.getDay();
    var thisDay = DAYS_ARRAY[ thisDayIndex ];
    var thisMonth = MONTH_ARRAY[ thisDateObject.getMonth() ];
    var thisDateSuffix = DAYSUFFIX_ARRAY[ thisDate ];
 
    var dateString = "<div>"+thisDay + ", " + thisMonth + " " + thisDate + thisDateSuffix+"</div>";

    document.write( dateString );
}

function ReferCookie() {
    this.createCookie = function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    this.readCookie = function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    this.eraseCookie = function (name) {
        createCookie(name, "", -1);
    }

    this.getParameterByName = function (url, name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS, "i");
        var results = regex.exec(url);
        if (results == null)
            return null;
        else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    this.calculateSearchPhrase = function (url) {
        var len = SEARCHQUERYSTRING.length;
        for (var i = 0; i < len; i++) {
            var value = SEARCHQUERYSTRING[i];
            var searchquery = this.getParameterByName(url, value);
            if (searchquery) {
                return searchquery;
            }
        }
        return null;
    }

    this.getReferCookieValue = function () {
        var referrer = document.referrer;
        if (!referrer || referrer.length == 0) {
            return null;
        }
        var searchphrase = this.calculateSearchPhrase(referrer);
        var host = referrer.split('/')[2];
        if(this.getParameterByName(window.location.href,'gclid')) {
            host = "google (cpc)";
        } else if (this.getParameterByName(window.location.href,'ysmwa')) {
            host = "yahoo (cpc)";
        }
        if (searchphrase) {
            return host + "#" + searchphrase;
        }
        else {
            return host;
        }
    }

    this.saveReferCookie = function () {
        var refcook = this.readCookie("VISITREFER");
        if (!refcook)
        {
            // calculate refercookie
            var refercookievalue = this.getReferCookieValue();
            if (!refercookievalue)
                refercookievalue = "direct";
            this.createCookie("VISITREFER", refercookievalue, 90);
        }
    }
}

window.onload = function () {
    var cook = new ReferCookie();
    cook.saveReferCookie();

    var text_input = document.getElementById('loginusername');
    text_input.focus();
}
