// AIM
function track(user,version) {
  var iXz = new Image();
  iXz.src = '/track.gif?v='+user+':'+version;
}

function error(pfx, msg) {
	var err = document.getElementById(pfx+'_error');
	err.innerHTML = msg;
}

function claim() {
	openwin('ebuddyid',723,639);
	return true;
}
var EBUDDY_WINDOW = null;

function openwin(pfx,w,h,user) {
    var loc = 'no';
    if (pfx == 'ebuddyid' || pfx == 'aim' || pfx == 'yahoo' || pfx == 'gtalk' || pfx == 'myspace' || pfx == 'msn' || pfx == 'icq') loc = 'yes';

    EBUDDY_WINDOW = window.open('', pfx, 'width='+w+', height='+h+', menubar=no,  toolbar = no, scrollbars=no, resizable=yes, location='+loc, true);

    if (EBUDDY_WINDOW == null) {
        error(pfx, 'A popup blocker prevented the eBuddy window from opening');
        return false;
    }
    
    return true;
}

function isEmailAddr(str) {
	return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
}
function isID(str) {
	return (str.indexOf('@')<0);
}
function clickSecure(type){
  //var frm = document.getElementById(type+'_form');
  var frm = document.getElementById(type);
  var s = document.getElementById(type+'_secure');
  var a = frm.action;
  if (s.checked)
    frm.action = a.replace('http://', 'https://');
  else
    frm.action = a.replace('https://', 'http://');
}
window.getSize = function (win) {
    if (win == null)
        win = window;
     if (win.innerHeight != null)
         return {width: win.innerWidth, height: win.innerHeight};
    if (win.document.documentElement && win.document.documentElement.clientHeight)
        return Element.getDimensions(win.document.documentElement);
    if (win.clientHeight != null)
        return {width: win.clientWidth, height: win.clientHeight};
    return Element.getDimensions(win.document.body);
};

var EBUDDY_MSG_PASSENC = null;
var EBUDDY_SEL_NETWORK = null;
var EBUDDY_WINDIMS     = [];
var EBUDDY_ENCRYPTED   = false;
var EBUDDY_LOADER      = null;
var EBUDDY_RSA_E       = null;
var EBUDDY_RSA_M       = null;

var EBUDDY_RSA_TAGS    = [];

var Encryption = {
    keyPair: null,
    timer  : null,
    text   : "",
   
    start: function(network, width, height) {
	
        Encryption.reset();
        
        if (typeof window.stop == "function")
            window.stop();
        else if (document.execCommand)
            document.execCommand('Stop');

        EBUDDY_SEL_NETWORK = network;
        EBUDDY_WINDIMS     = [width, height];
        
        this.toggleProgress(true);
        //keysize = 512, thus 77 precision
        Crypt.BigInt.setMaxDigits(77);
        
        var oHead     = document.getElementsByTagName('head')[0];
        var oForm     = document.getElementById(EBUDDY_SEL_NETWORK);
		var redirect  = oForm.e_redirect.value;
        var encTarget = redirect.replace(/main\.html/, 'secure_js.jsp?').appendRandomNumber(9);
		
        //var encTarget = String("http://delta.ebuddy.com/oberon_latest/secure_js.jsp?").appendRandomNumber(9); //TESTING
        //first remove the OLD scripttags from the HEAD
        for (var i = 0; i < EBUDDY_RSA_TAGS.length; i++) {
            if (EBUDDY_RSA_TAGS[i]) {
                EBUDDY_RSA_TAGS[i].parentNode.removeChild(EBUDDY_RSA_TAGS[i]);
                EBUDDY_RSA_TAGS[i] = null;
            }
        }
        
        var encScript    = oHead.appendChild(document.createElement('script'));
        encScript.src    = encTarget;
        encScript.type   = "text/javascript";
        EBUDDY_RSA_TAGS.push(encScript);
        
		
        Encryption.fetchKeys(window);
    },
    end: function() {
        this.toggleProgress(false);
        EBUDDY_ENCRYPTED = true;
    },
    fetchKeys: function(win) {
        if (this.timer) clearTimeout(this.timer);
        if (!EBUDDY_RSA_E | !EBUDDY_RSA_M) {
            this.timer = window.setTimeout(function() {
                Encryption.fetchKeys(win);
            }, 50);
            return;
        }
        
        var oForm = document.getElementById(EBUDDY_SEL_NETWORK);
        this.keyPair = new Crypt.RSA.getKeyPair(
          win.EBUDDY_RSA_E, '',
          win.EBUDDY_RSA_M
        );
        
        var s = Crypt.RSA.encrypt(this.keyPair, oForm.elements['pwd'].value);
        Encryption.text = oForm.elements['e_password'].value = s;
        
        Encryption.end();
        //oForm.action = "http://delta.ebuddy.com/oberon_latest/login.jsp";//TESTING
        doLogin(EBUDDY_SEL_NETWORK, EBUDDY_WINDIMS[0], EBUDDY_WINDIMS[1]);
    },
    toggleProgress: function(visible) {
        if (typeof visible == "undefined") visible = true;
        
        if (!EBUDDY_LOADER) {
            //tooltip to show password-encryption status:
            var oDims = window.getSize();
            EBUDDY_LOADER = new Tooltip('tooltip_encryption', null, {
                classTopLeft     : 'tooltip_silver_topleft',
                classBottomLeft  : 'tooltip_silver_bottomleft',
                classContent     : 'tooltip_silver_content',
                classTopRight    : 'tooltip_silver_topright',
                classBottomRight : 'tooltip_silver_bottomright',
                classBottomMiddle: 'tooltip_silver_bottommiddle',
                classDisabled    : 'tooltip_silver_disabled',
                classArrow       : 'tooltip_silver_arrow',
                pathToContent    : EBUDDY_CONTENTPATH + "/_images/",
                offsetX          : (oDims.width / 2) - 200,
                offsetY          : 292,
                width            : 'auto',
                height           : 61,
                showCloseButton  : false,
                animate          : false
            });
        }
        
        if (visible)
            EBUDDY_LOADER.show(null, '<img src="' + EBUDDY_CONTENTPATH
              + '/_images/tooltip/tooltip_padlock.gif" border="" alt="" />'
              + EBUDDY_MSG_ENCRYPTING, '');
        else
            EBUDDY_LOADER.hide();
    },
    reset: function() {
        Encryption.text    = "";
        Encryption.keyPair = EBUDDY_SEL_NETWORK = EBUDDY_LOADER = EBUDDY_RSA_E = EBUDDY_RSA_M = null;
        EBUDDY_WINDIMS     = [];
        EBUDDY_ENCRYPTED   = false;
    }
};