function clickThru(aUrl, aWidth, aHeight) {
    if (aUrl == null) {
        return false;
    }
    if (aWidth == null) {
        aWidth = 450;
    }
    if (aHeight == null) {
        aHeight = 300;
    }
    window.open(aUrl, 'click_thru', 'height=' + aHeight + ',width=' + aWidth);
    return false;
}

function setCookie(aName, aValue) {
    document.cookie = aName + '=' + aValue;
}

function getUsername() {
    var cookie = new CH1Cookie();
    cookie.parseCookies();
    var pm = cookie.getCookie('PirtMember');
    pm = pm.replace(/^.*(%3b|;)data(%3d|=)/i, '');
    pm = decodeBase64(pm);
    var username = '';
    var i = 0;
    var chrCode = pm.charCodeAt(i);
    while ((chrCode < 32 || chrCode > 126) && i < pm.length) {
        i++;
        chrCode = pm.charCodeAt(i);
    }
    while (chrCode >= 32 && chrCode <= 126 && i < pm.length) {
        username += pm.charAt(i);
        i++;
        chrCode = pm.charCodeAt(i);
    }
    return username;
}

function writeUsername() {
    document.write(getUsername());
}

function decodeBase64(enc) {
    var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789+/=';
    var dec = '';
    var chr1, chr2, chr3 = '';
    var enc1, enc2, enc3, enc4 = '';
    var i = 0;

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    enc = enc.replace(/[^A-Za-z0-9\+\/\=]/g, '');
    do {
        enc1 = keyStr.indexOf(enc.charAt(i++));
        enc2 = keyStr.indexOf(enc.charAt(i++));
        enc3 = keyStr.indexOf(enc.charAt(i++));
        enc4 = keyStr.indexOf(enc.charAt(i++));

        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        dec += String.fromCharCode(chr1);
        if (enc3 != 64) {
            dec += String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            dec += String.fromCharCode(chr3);
        }
        chr1 = chr2 = chr3 = '';
        enc1 = enc2 = enc3 = enc4 = '';
    }
    while (i < enc.length);
    return dec;
}

function popitup(url) {
    newwindow = window.open(url, 'name', 'height=340,width=730');
    if (window.focus) {
        newwindow.focus();
    }
    return false;
}
function popitup2(url) {
    newwindow = window.open(url, 'name', 'height=480,width=730');
    if (window.focus) {
        newwindow.focus();
    }
    return false;
}
function actionOnChange(actionStr) {
    var actionStrArray = actionStr.split(" ");
    if (actionStrArray.length == 2) {
        var actionType = actionStrArray[0];
        var actionLink = actionStrArray[1];
        if (actionType == "page") {
            location = actionLink;
        }
        else if (actionType == "quiz") {
            window.open(actionLink, "pop", "status=1,height=340,width=730,resizable=0");
        }
        else if (actionType == "story") {
            window.open(actionLink, "pop", "status=1,height=480,width=730,resizable=0");
        }
        else {
            alert("Unkown type: " + actionType);
        }
    }
    return false;
}

function submitSearch(theForm) {
    var theaction = theForm.action;
    var thekeyword = theForm.elements[0].value;
    document.location.href = theaction + escape(thekeyword) + "/";
}

var idTimeRoot = null;

function getContent(iframe) {
    var content = '';
	/*if (location.host == "news.channelone.com") {
		document.domain = "www.channelone.com";
	} else if (location.host == "news.staging.channelone.com") {
		document.domain = "www.channelone.com";
	}*/
    if (iframe.contentDocument) {
        content = iframe.contentDocument.body.innerHTML;
    } else if (iframe.contentWindow) {
        content = iframe.contentWindow.document.body.innerHTML;
    } else if (iframe.document) {
        content = iframe.document.body.innerHTML;
    }
    return content;
}
function onLoadFrameRootDelay() {
    if (getContent(document.getElementById('hiddenRoot')) != '') {
		if ($$('.ad_med_square').length) {
			var ads = $$('.ad_med_square');
			ads.each(function(e) {
				e.innerHTML = getContent(document.getElementById('hiddenRoot'));
			});
		} else if ($('ad_skyscraper') != '') {
			$('ad_skyscraper').innerHTML = getContent(document.getElementById('hiddenRoot'));
		} else if ($('ad_med_square') != '') {
			$('ad_med_square').innerHTML = getContent(document.getElementById('hiddenRoot'));
		}
    }
}
function onLoadFrameRoot(){
	var timeRootDelay = Math.floor((40000-24999)*Math.random()) + 25000;
	setTimeout("onLoadFrameRootDelay()", timeRootDelay);
}
function setContentRoot(url) {
    frames['hiddenRoot'].location = url;
	var timeRoot = Math.floor((40000-24999)*Math.random()) + 25000;
    idTimeRoot = setTimeout("setContentRoot('/includes/banerRoot.html')", timeRoot);
}
function adGetWindowWidth() {
    if (typeof(window.innerWidth) == 'number') {
        //Non-IE
        return window.innerWidth;
    }
    if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        return document.documentElement.clientWidth;
    }
    if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        return document.body.clientWidth;
    }
    return 0;
}

function adGetWindowHeight() {
    if (typeof(window.innerWidth) == 'number') {
        //Non-IE
        return window.innerHeight;
    }
    if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        return document.documentElement.clientHeight;
    }
    if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        return document.body.clientHeight;
    }
    return 0;
}