/// <reference path="jquery-1.4.1.js" />
/// <reference path="jquery-1.4.1-vsdoc.js" />

var J = jQuery.noConflict();
var mydomain = '';
var host = '';
var onlineBanking = "https://tularefcu.online-cu.com";

J(document).ready(function () {
    mydomain = window.document.domain;
    host = window.location.host;
    J('.wrapper a').each(function () {
        var link = J(this).attr('href');
        
        var reWith = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im'); 
        var reOut = new RegExp('^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$','im')       
        
        if ((link != "http://www.tularefcu.org/auto_loan_calc.html")
            && (link != "http://www.tularefcu.org/home_mortgage_calc.html") 
            && (link != "http://www.tularefcu.org/retirement_calc.html")
            && (link != "https://www.loanliner.com/ICE/presenter/default.asp?CuIdNr=10404900&ProtPkgIdNr=8757649009139114540&ORIGIN_CHANNEL_ID=1503251609139163808&ORIGIN_LOCATION_ID=9796411209139163744")) {
		if (link.match(reWith)){
		    var currDomain =  get_hostname_from_url(link.toString());
		    if(link.indexOf(onlineBanking) == -1){
			if(mydomain != currDomain){
			resetAnchor(link,this);                       
		      }                 
		    }                
		}
		else{
		    if(link.match(reOut)){
			link = "http://" + link;
			var currDomain =  get_hostname_from_url(link.toString());
			if(mydomain != currDomain){
			    resetAnchor(link,this);                       
			}                
		    }
		}
        }

    });
});

function resetAnchor(_link, anchor){
    J(anchor).removeAttr('href');
    J(anchor).removeAttr('target');
    J(anchor).attr('href', 'box/box-external-redirect.aspx?u=' + _link.toString());
    J(anchor).attr('rel', 'moodalbox 550 450');
    J(anchor).attr('title', 'external link');
}

function get_hostname_from_url(url) {
    return url.match(/:\/\/(.[^/]+)/)[1];
}

function OpenModalFromAnother(url, title, sizes) {
    MOOdalBox.open( // case matters
        url, // the link URL
        title, // the caption (link's title) - can be blank
        sizes // width and height of the box - can be left blank
        );
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
