DOM2Event.initRegistration(window);
window.addEventListener("load", tandc_init, false);

function tandc_init(event) {
	event = new DOM2Event(event, window.event, this);
    if (!document.getElementsByTagName) return;
	
	var sitecheck = true;
    var body = document.getElementsByTagName("body");
    for (i=0; i<body.length; i++) {
		if (body[i].getAttribute("tandc_sitecheck") && (body[i].getAttribute("tandc_sitecheck").toLowerCase() == "disabled")) {
			sitecheck = false;
		}
    }

	if ( sitecheck) tandc_sitecheck();	

    var anchors = document.getElementsByTagName("a");
    for (i=0; i<anchors.length; i++) {
		if (anchors[i].className.toLowerCase() == "externallink") {
			tandc_externallinkcheck(anchors[i]);
		}
    }

	if (!cookie_check()) {
	    var divs = document.getElementsByTagName("div");
		for (i=0; i<divs.length; i++) {
			if (divs[i].className.toLowerCase() == "cookiesdisabled") {
				divs[i].innerHTML = "Please note that you are preventing a cookie being stored on your machine that confirms your acceptance of these conditions. As a result you will be prompted on every page.";
			}
		}
	}
}

function tandc_check(redirect) {
	var argv = arguments;
	var argc = arguments.length;
	var cookie_name = (argc > 1) ? argv[1] : null;
	var agreed = false;
	var query = new QueryString(window.location.search);
	if (cookie_name) {
		if (cookie_get(cookie_name) == "agreed") {
			agreed = true;
		} else {
			if (query.getValue(cookie_name) == "agreed")
				agreed = true;
		}
	}

        if (!agreed)
        {
                var ref = location.href;
                if( ref == "http://intranet.matrixgroup.co.uk/" )
                {
                        ref = "http://intranet.matrixgroup.co.uk/intranet_home";
                }
                return (redirect + "ref=" + escape(ref));
        }
}

function tandc_agree() {
	var argv = arguments;
	var argc = arguments.length;
	var query = new QueryString(window.location.search);
	var cookie_name = (argc > 0) ? argv[0] : null;
	var ref = query.getValue("ref");
	ref = (ref) ? unescape(ref) : "";

	if (cookie_name) {
		var expires_in_days = (argc > 1) ? argv[1] : null;

		if (expires_in_days)
		{
			var regexp = new RegExp( "^https?:\/\/*(www\.)?([^\/]+)\/", "ig" );
			var domain_matches = regexp.exec( window.location.href );
			var cookie_domain = domain_matches[2];
			cookie_set("tandc", "agreed", new Date((new Date()).getTime() + 1000 * 60 * 60 * 24 * expires_in_days), "/", "." + cookie_domain, false);
			//cookie_set("tandc", "agreed", new Date((new Date()).getTime() + 1000 * 60 * 60 * 24 * expires_in_days), "/", null, true);
		}
		else
		{
			cookie_set("tandc", "agreed", new Date((new Date()).getTime() + 1000 * 60 * 20), "/"); // 20 minutes
		}
		if (ref.indexOf("?") >= 0) 
			ref = ref + "&";
		else
			ref = ref + "?";

		ref = ref + cookie_name + "=agreed";
	}

	// location.href = document.referrer; // IE stops this from working. http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/referrer.asp?frame=true
	location.href = ref;
}

function tandc_sitecheck() {
    /*
       We use an absolute URL here to avoid eZ Publish messing with our parameters.
       If eZ Publish triggers an SSL/Non-SSL redirect it will strip all parameters.

       matrixSiteURL comes from the [SiteSettings] SiteURL settings and is set in the pagelayout.tpl.
    */
	var redirect = tandc_check("http://" + matrixSiteUrl + "/terms_and_conditions?", "tandc");
	if (redirect) location.href = redirect;
}

function tandc_siteagree() {
	tandc_agree("tandc", 30); // expire in 30 days
}

function tandc_externallinkcheck(anchor) {
	// event = new DOM2Event(event, window.event, this);
	// var anchor = event.target;
	anchor.href = "/external_link_disclaimer?ref=" + escape(anchor.href);
}

function tandc_externallinkagree() {
	tandc_agree();
}
