/* 
  ProxEasy.js
  Copyright (C) 2008 Bine Consulting Corp.
*/


/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function setCookie(c_name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

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 "";
}


var hideTimer = null;
var moveToRight = false;
var oldresize, oldonload;

function __ProxEasyResize() {
	if (oldresize) oldresize();
	__DoHideProxEasy();
}

function __ShowProxEasy() {
	if (hideTimer != null) clearTimeout(hideTimer);
	document.getElementById('__ProxEasyPopup').style.display = "block";
	document.getElementById('__ProxEasy').style.width = document.getElementById('__ProxEasyPopup').style.width;

	if (moveToRight) document.getElementById('__ProxEasy').style.left = document.body.clientWidth - document.getElementById('__ProxEasy').offsetWidth + "px";
	else document.getElementById('__ProxEasy').style.left = "0px";
}

function __HideProxEasy() {
	hideTimer = setTimeout("__DoHideProxEasy()", 500);
}

function __DoHideProxEasy() {
	document.getElementById('__ProxEasyPopup').style.display = 'none';
	document.getElementById('__ProxEasy').style.width = document.getElementById('__ProxEasyFloat').style.width;

	if (moveToRight) document.getElementById('__ProxEasy').style.left = document.body.clientWidth - document.getElementById('__ProxEasy').offsetWidth + "px";
	else document.getElementById('__ProxEasy').style.left = "0px";
}

function __ProxEasyGotoURL() {
	document.location.href = '/webclient.aspx?__ProxEasyURL='
	+ escape(Base64.encode(document.getElementById('__ProxEasyURLd').value))
	+ '&__ProxEasyFlags='
	+ (document.getElementById('__ProxEasyRemoveScripts').checked ? "1" : "0")
	+ (document.getElementById('__ProxEasyRemoveTitle').checked ? "1" : "0")
	+ (document.getElementById('__ProxEasyRemoveCookies').checked ? "1" : "0")
	+ (document.getElementById('__ProxEasyRemoveUserAgent').checked ? "1" : "0")
	+ (document.getElementById('__ProxEasyRemoveReferer').checked ? "1" : "0")
	+ (document.getElementById('__ProxEasyRemoveImages').checked ? "1" : "0")
	+ "0";
}

function __MoveProxEasyToRight() {
	moveToRight = true;
	setCookie("__ProxEasyPosition", "right", null);
	document.getElementById('__ProxEasy').style.textAlign = "right";
	document.getElementById('__ProxEasyFloat').style.marginLeft = "auto";
	document.getElementById('__ProxEasyMoveRight').style.display = 'none';
	document.getElementById('__ProxEasyMoveLeft').style.display = 'inline';
	__DoHideProxEasy();
}

function __MoveProxEasyToLeft() {
	moveToRight = false;
	setCookie("__ProxEasyPosition", "left", null);
	document.getElementById('__ProxEasy').style.textAlign = "left";
	document.getElementById('__ProxEasyFloat').style.marginLeft = "0";
	document.getElementById('__ProxEasyMoveRight').style.display = 'inline';
	document.getElementById('__ProxEasyMoveLeft').style.display = 'none';
	__DoHideProxEasy();
}

function __ProxEasySubmitOnEnter(evt) {
	if ((evt.which || evt.keyCode) == 13) {
		__ProxEasyGotoURL();
		return false;
	} else {
		return true;
	}
}

function __ShowProxEasyPopup(url, removeCookies, removeScripts, removeTitle, removeUserAgent, removeImages, removeReferer) {
	if (!document.getElementById('__ProxEasy')) {
		document.write("<link rel='stylesheet' media='all' type='text/css' href='/popup.css' />"
		+ "<form><div id='__ProxEasy' onmouseover='__ShowProxEasy();' onmouseout='__HideProxEasy();'>"
		+ "<div id='__ProxEasyFloat'>"
		+ "<a id='__ProxEasyMoveLeft' href='javascript:__MoveProxEasyToLeft()' style='display:none'><img src='/images/leftarrow.gif' title='Move to left' alt='Move to left' border='0'></a><a href='/webclient.aspx' style='color:#0000ff;text-decoration:none;'><img src='/images/proxeasy32x32.png' alt='ProxEasy is protecting you' title='ProxEasy is protecting you' border='0'></a><a id='__ProxEasyMoveRight' href='javascript:__MoveProxEasyToRight()'><img src='/images/rightarrow.gif' alt='Move to right' title='Move to right' border='0'></a>"
		+ "</div>"
		+ "<div id='__ProxEasyPopup'>"
		+ "<table cellspacing='0'><tr><td><label for='__ProxEasyURLd' style='color:#000080'>URL: "
		+ "<input type='text' value='" + url + "' id='__ProxEasyURLd' onkeypress='return __ProxEasySubmitOnEnter(event)' style='width:600px;background-color:#ffffff;color:#000000;border:1px groove #cccccc;'></label> "
		+ "<input style='background-color:#cccccc;color:#000000;height:22px;outline:1px outset #777777;' type='button' onclick='__ProxEasyGotoURL()' value='Go'>"
		+ "<div style='margin-top:5px;font-size:14px;color:#000080;text-align:center;line-height:24px;'>"
		+ "<label for='__ProxEasyRemoveCookies'><input type='checkbox' id='__ProxEasyRemoveCookies' " + removeCookies + " /> No Cookies</label> &nbsp; "
		+ "<label for='__ProxEasyRemoveScripts'><input type='checkbox' id='__ProxEasyRemoveScripts' " + removeScripts + " /> No Scripts</label> &nbsp; "
		+ "<label for='__ProxEasyRemoveTitle'><input type='checkbox' id='__ProxEasyRemoveTitle' " + removeTitle + " /> No Title</label> &nbsp; "
		+ "<label for='__ProxEasyRemoveUserAgent'><input type='checkbox' id='__ProxEasyRemoveUserAgent' " + removeUserAgent + " /> No UserAgent</label> &nbsp; "
		+ "<label for='__ProxEasyRemoveImages'><input type='checkbox' id='__ProxEasyRemoveImages' " + removeImages + " /> No Images</label> &nbsp; "
		+ "<label for='__ProxEasyRemoveReferer'><input type='checkbox' id='__ProxEasyRemoveReferer' " + removeReferer + " /> No Referer</label> "
		+ "</div>"
		+ "<div style='font-size:12px;text-align:center;line-height:24px;'>"
		+ "<a style='color:#0000ff' href='/membership.aspx'>Membership Options</a>"
		+ " &nbsp;&nbsp;&nbsp; "
		+ "<a style='color:#0000ff' href='mailto:proxy@bine.ca?Subject=ProxEasy Problem Report&Body=URL:" + document.location.href + "%0A%0AEnter the problem details here:%0A'>Report a problem with this page</a>"
		+ "</div>"
		+ "<div style='font-size:10px;color:#000000;text-align:center;line-height:24px;'>Copyright &copy; 2008 <a style='color:#4080ff' href='http://bine.ca'>Bine Consulting Corp.</a></div>"
		+ "</td></tr></table></div>"
		+ "</div></form>");

		oldresize = window.onresize;
		window.onresize = __ProxEasyResize;
		
		oldonload = window.onload;
		window.onload = __ProxEasyOnLoad;
	}
}
	
function __ProxEasyOnLoad() {
	if (oldonload) oldonload();
	if (getCookie("__ProxEasyPosition") == "right") __MoveProxEasyToRight();
}

