﻿function setCookie(c_name, value, expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+parseFloat(expiredays));
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1)
        {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";",c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz,";
	var string_length = 50;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
function checkCookie()
{
    interstitial = getCookie(cookieName);
    if (interstitial != null && interstitial != "")
    {
    }
    else
    {
        setCookie(cookieName, window.location, cookieLength);
        
        // Category specific interstitial
        if (adCategory != null && adCategory != "")
        {
            window.location = "http://au.fourfourtwo.com/promos/interstitial/interstitial.html?l=" + adLength + ";" + adCategory + ";" + randomString() + ";" + cookieName;
        }
        else
        {
            window.location = "http://au.fourfourtwo.com/promos/interstitial/interstitial.html?l=" + adLength + ";" + randomString() + ";" + cookieName;
        }
    }
}

var cookieName = "";
var cookieLength = 7; //Default
var adLength = 20; //Default
var adCategory = "";
if (document.getElementById("interstitial_name") != null) cookieName = document.getElementById("interstitial_name").value;
if (document.getElementById("interstitial_cookie") != null) cookieLength = document.getElementById("interstitial_cookie").value;
if (document.getElementById("interstitial_adlength") != null) adLength = document.getElementById("interstitial_adlength").value;
if (document.getElementById("interstitial_category") != null) adCategory = document.getElementById("interstitial_category").value;

if (cookieName != "") checkCookie(); // Do logic