
/**
 * @author Justin Pfifer
 * @namespace com.monicasarli
 */
if (typeof(com) === "undefined") {
    com = {};
}
if (typeof(com.monicasarli) === "undefined") {
    com.monicasarli = {};
}

/***
 * Creates a CookieCheck object and sets the name of the cookie
 * @param {String} cookieName The name of the cookie to set
 * @constructor
 * @classDescription Used to support checking the cookie to make sure the user has viewed the warning page
 */
com.monicasarli.CookieCheck = function(cookieName, defaultUrl){
    this.cookieName = cookieName;
    if (typeof(defaultUrl) === "undefined") {
        this.defaultUrl = this.getQueryStringValue("Default");
    }
    else {
        this.defaultUrl = defaultUrl;
    }
}

/***
 * @memberOf {com.monicasarli.CookieCheck}
 * @private
 */
com.monicasarli.CookieCheck.prototype.verifyDependencies = function(){
    if (typeof($) === "undefined") {
        //
        // Can't handle jQuery not being present
        //
        throw "jQuery not found.  Please ensure that jQuery is referenced before this function is called";
    }
    if (typeof($.cookie) === "undefined") {
        //
        // Need the cookie plugin too
        //
        throw "jQuery cooking plugin not found.  Please ensure that the jQuery cookie plugin is referenced before this function is called"
    }
}

/***
 * Appends the returning URL to
 * @param {String} urlToRedirectTo
 * @private
 * @memberOf {com.monicasarli.CookieCheck}
 * @return {String} A new URL with the return path present;
 */
com.monicasarli.CookieCheck.prototype.appendReturnInfo = function(urlToRedirectTo){
    var finalUrl = urlToRedirectTo;
    
    finalUrl += "?Default=" + escape(this.defaultUrl) + "&ReturnTo=" +
    escape(window.location.pathname +
    (window.location.search ? window.location.search : "") +
    (window.location.hash ? window.location.hash : ""));
    
    
    return finalUrl;
}


/***
 * Creates a full url from the path portion for IE
 * @param {String} pathUrl A URL that is the path + search + hash
 * @return {String} A full URL that can be used with HRef
 */
com.monicasarli.CookieCheck.prototype.createFullUrl = function(pathUrl){
    return window.location.protocol + "//" + window.location.hostname + pathUrl;
}

/***
 * Checks the cookie and if it's not set will redirect the user to a page to set it.
 * @param {String} urlToRedirectTo The URL to redirect to if the cookie is not set.
 * @memberOf {com.monicasarli.CookieCheck}
 *
 */
com.monicasarli.CookieCheck.prototype.checkCookieAndRedirect = function(urlToRedirectTo){
    this.verifyDependencies();
    var finalUrl = this.appendReturnInfo(urlToRedirectTo);
	finalUrl = this.createFullUrl(finalUrl);
    var hasVisitedWarningPage = $.cookie(this.cookieName);
    if (hasVisitedWarningPage === null || hasVisitedWarningPage.toLowerCase() === "no") {
        window.location.href = finalUrl;
    }
}

/***
 * Redirects if the user agrees to the terms
 * @param {Boolean} agreedToTerms Whether the user agreed or not
 */
com.monicasarli.CookieCheck.prototype.setCookie = function(agreedToTerms){
    this.verifyDependencies();
    if (agreedToTerms) {
    
        var urlToReturnTo = this.getQueryStringValue("ReturnTo")
        if (urlToReturnTo === null) {
            urlToReturnTo = this.defalutUrl;
        }
        
        $.cookie(this.cookieName, "yes", {
            expires: 1000
        });
        
        window.location.href = this.createFullUrl(urlToReturnTo);
    }
    
}

/***
 * Extracts the value reference by name if it's present
 * @param {String} name The Name to extract from the Query String
 * @return {String} The value from the query string or null if a matching name was not found
 * @private
 * @memberOf {com.monicasarli.CookieCheck}
 */
com.monicasarli.CookieCheck.prototype.getQueryStringValue = function(name){
    var queryString = window.location.search.toString();
    if (queryString.indexOf("?") === 0) {
        queryString = queryString.substr(1, queryString.length - 1);
    }
    var queryParts = queryString.split("&");
    
    for (var i = 0; i < queryParts.length; i++) {
        var nameValuePair = queryParts[i].split("=");
        if (nameValuePair[0] === name) {
            return unescape(nameValuePair[1]);
        }
    }
}




