////////////////////////////////////////////////////////////////////////////
//
// This script will read and write to a checkbox and based on the value
// of cookie. 'True' will open all links to *external sites* in new
// windows, 'False' will open all links in the current window.
// 'True' will be set as the default if no cookie exists.
//
// To work properly, the client must have JavaScript and Cookies enabled.
//
// This is a combination of two scripts that have been adapted, and somewhat
// mangled, to do my bidding. The cookie code is from Javascripts.com and
// the link targeting is from a good Bookmarklet collection at...
// http://www.zdnet.com/devhead/stories/articles/0,4413,2348867,00.html
//
// This script is provided, as is, and without warranty.
//
// If you like it, feel free to
// shower me with money via PalPal (paypal@inmyexperience.com). 
// Suggested donation is a buck. Just one dollar!
//
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
function setLink() {

var lcElement = document.getElementById("linkController");

var myCookieName = 'imxLinkName';

// Try to get your cookie
imxLink = GetCookie(myCookieName);

// No cookie? then make a cookie, and give it a value of true.
if (imxLink == null) {

	imxLink = true;
	lcElement.checked = true;
	pathname = location.pathname;
	myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';

	// set expiry date to 1 year from now.
	var largeExpDate = new Date ();
	largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
	SetCookie(myCookieName,imxLink,largeExpDate,myDomain);
	}
	
// By now we have a cookie for one reason or another, read it and use it
if (imxLink == "true") {
	lcElement.checked = true;
	}
	
// Not true? then it's gotta be false.
if (imxLink == "false") {
	lcElement.checked = false;
	}
	
// Done with everything? Good, now save it.
	targetNew(top)
}

/////////////////////////////////////////////////////////////////////////////
function targetNew(w) { 

	var i,k,T,L,D;
	var myCookieName = 'imxLinkName';
	var myDomain = location.hostname;
	var lcElement = document.getElementById("linkController");

	// If the checkbox is checked, then set all the link and save the state
	if(lcElement.checked) {
		
	for(i=0;i<w.length;i++)
		targetNew(w.frames[i]);
		L=w.document.links; // returns [object LinkArray]
		
	for(k=0;k<L.length;k++){
		T=L[k].target;
		D=L[k].hostname;
//		alert(D + " : " + T);
		if(!T||T=='_self'||T=='_top'||T==w.name||T=='Special') {

		// if the domain is not our domain, make the target a blank window
		// note: if you want all links to open to the same new window,
		// change '_blank' to a unique name, but don't use '_top', '_self' or '_parent'

			if (D != "www.inmyexperience.com" && D != "inmyexperience.com") {
				L[k].target='_blank';
			}
		}
	}
	imxLink = true;
	pathname = location.pathname;
	myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';

	// set expiry date to 1 year from now.
	var largeExpDate = new Date ();
	largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
	SetCookie(myCookieName,imxLink,largeExpDate,myDomain);
	} // end the if statement
	
	// If not checked, then save the state...
	else {
	imxLink = false;
	pathname = location.pathname;
	myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';

	// set expiry date to 1 year from now.
	var largeExpDate = new Date ();
	largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
	SetCookie(myCookieName,imxLink,largeExpDate,myDomain);

	// and then target the same window with a function call
	targetSelf(top);
	}
}

/////////////////////////////////////////////////////////////////////////////
function targetSelf(w) { 
	var i,k,T,L;
	for(i=0;i<w.length;i++)
		targetSelf(w.frames[i]);
		L=w.document.links;

for(k=0;k<L.length;k++){

	T=L[k].target;
	if(T=='_blank'||T=='Special')
	L[k].target='_self'
	}
};

/////////////////////////////////////////////////////////////////////////////
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                        return getCookieVal (j);
                i = document.cookie.indexOf(" ", i) + 1;
                        if (i == 0)
                                break;
                }
	return null;
}

/////////////////////////////////////////////////////////////////////////////
function SetCookie (name, value) {
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? "/" : null;
        var domain = (argc > 4) ? ".inmyexperience.com" : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape (value) +
                ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
                ((path == null) ? "" : ("; path=" + path)) +
                ((domain == null) ? "" : ("; domain=" + domain)) +
                ((secure == true) ? "; secure" : "");
}

