/*
 * Title:        Etrigue Link Activator (Replaces cookies.js)
 * Description:  Insert into pages containing resource links
 * Usage:        <a href="[link to resource]" class="etrigue">[Title]</a>
 *
 * CONFIGURATION VARIABLES */
	var et_InfoCenterURL = "/infocenter/";
	var et_RegistrationURL = "/infocenter/registration.html";
	var et_RegistrationThanksURL = "/infocenter/registration_thankyou.html";
	var et_ShowThankYouPage = true;

/*
 * END CONFIGURATION VARIABLES. - You do not need to modify below this line!
 */
	var RegistrationCookieValue = getCookie("Registration");
	var DocRequestCookie = getCookie("DocRequest");
	var user_email_cookie = getCookie("email_cookie");
	var eTrigue_moid = getCookie("moid");

	var etDate = new Date();
	etDate.setTime(etDate.getTime() + 10*365 * 24 * 60 * 60 * 1000);
	
function et_activateLink(){
	if(!document.getElementById || !document.createElement)	return(false);
	//set elements for tag <a>
	var elements=document.getElementsByTagName("a");
	//go through all <a> tags
	for ( i=0; i < elements.length; i++){
		var ele=elements[i];
		//when class=etrigue found in a tag <>
		if (ele.className.indexOf('etrigue')>-1){
			// Attach the event function.
			et_AttachEvent(ele, 'click', function(evt){
				var srcElement = evt.target || evt.srcElement;
				
				
				//var docHref = srcElement.href;
				//document.write(docHref);
				/*
				 * Get Document URL
				*/ 
				 var cSpans = srcElement.getElementsByTagName("span");
				 for (ci=0; ci < cSpans.length; ci++){
					var cSpan = cSpans[ci];
					if (cSpan.className=='DocRequest'){
						var docHref=cSpan.innerHTML;
					};
				 //document.write(docHref);
				 };
				 
				 var img = srcElement.getElementsByTagName("img");
				 for (ci=0; ci < img.length; ci++){
					var img = img[ci];
					if (img.className=='DocRequest'){
						var docHref=a.innerHTML;
					};
				 //document.write(docHref);
				 };
				
				
				// Mask link to prevent it from launching
				srcElement.href='javascript:;';

				// Set DocRequest cookie and variable
				setCookie ("DocRequest", docHref, etDate, "/");
				DocRequestCookie = docHref;
				
				/* To-Do: Add registration check and bypass registration */
				if (RegistrationCookieValue==null){
					document.location.href=et_RegistrationURL;
				} else if (et_ShowThankYouPage == false) {
					document.location.href=docHref;
				} else {
					document.location.href=et_RegistrationURL; //et_RegistrationThanksURL;
				};

				return false;
			}, true);
			
			/*
			 *  Add a hidden div containing the path to PDF
 			 */
			var divRef = document.createElement('span');
			divRef.id = "etlink_"+i;
			divRef.className = "DocRequest";
			divRef.style.display = "none";
			divRef.innerHTML = ele.href;
			ele.appendChild(divRef);
			
			ele.href=et_RegistrationURL;//---hidden document path :: +'?DocRequest='+escape(ele.href);
		};
	};
	/*
	 * Check Document Link If on the register or registration page
	 */
	var currentPageURL = document.location.href;
	if (currentPageURL.indexOf(et_RegistrationURL) > -1) {
		// We are on the registration page.
		// Check if the document cookie is set. (User clicked a link to get here)
		// Also checks if 'moid' cookie is set and bypasses registration page.
		et_checkDocumentCookie(false);
	}
	if (currentPageURL.indexOf(et_RegistrationThanksURL) > -1){
		// We are on the thank you page.
		// Check if document cookie is set and set the Registration cookie.
		et_checkDocumentCookie(true);
	};
};
function et_getParam(sKey){
	var sURL = window.location.href;
	var pRet = false;
	if (sURL.indexOf("?")>-1){
			var sParams = sURL.substr(sURL.indexOf("?")+1, sURL.length);
			if (sParams.indexOf('&')>-1){
				var aryParams = sParams.split('&');
			} else {
				var aryParams = Array(sParams);
			}
			for (i=0;i<aryParams.length;i++){
					if (aryParams[i].indexOf(sKey+"=")>-1){
						pRet = unescape(aryParams[i].substr(aryParams[i].indexOf('=')+1, aryParams[i].length));
					}
			}
	}
	return pRet;
}	

/*
 * Attach event to activate Etrigue links.
 */
	if(window.attachEvent){
		window.attachEvent("onload", et_activateLink);
	} else {
		if(typeof window.onload == "function"){
			var et_fOld = window.onload;
			window.onload = function(){ et_fOld(); et_activateLink(); };
		} else {
			window.onload = et_activateLink;
		};
	};


/* Updated document click check.  To enable set var infoCenterRegPages = true before the onLoad event.
 * Detect "document" cookie send user back to infocenter if user did not click on the protected document.
 */
var etCdc;
function et_checkDocumentCookie(blnThankYouPage){
		if (etCdc==true) return;
		// see if we have the location to set the cookie right now.
		var paramCheck = et_getParam('DocRequest');
		if (! paramCheck == false) {
			setCookie("DocRequest", paramCheck, etDate, "/");
			DocRequestCookie = paramCheck;
		}
			
		if (DocRequestCookie == null) {
			// document cookie not set, redirect to infoCenter.
	    	document.location.href = et_InfoCenterURL;
		} else {
			if (blnThankYouPage == true) {
				// set Registration cookie if on the thank you page.
				setCookie("Registration", "myRegistration", etDate, "/");
			} else {
				if (RegistrationCookieValue!=null || eTrigue_moid!=null) document.location.href = et_RegistrationThanksURL +"?docURL="+DocRequestCookie;
				
			};
		};
		etCdc==true;
};

/*
 * Author Note: Imported from cookies.js
 * Use the following commented code to be compatible with old Etrigue link style. */
var now = new Date();
now.setTime(now.getTime() + 10*365 * 24 * 60 * 60 * 1000);

 
	function setCookie(name, value, expires, path, domain, secure){
		document.cookie= name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	}
	function getCookie(name){
		var dc = document.cookie; 
		var prefix = name + "="; 
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1){
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		} else {
			begin += 2;
		};
		var end = document.cookie.indexOf(";", begin);
		if (end == -1){
			end = dc.length;
		};
		return unescape(dc.substring(begin + prefix.length, end));
	};
	function deleteCookie(name, path, domain){
		if (getCookie(name)){
			document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
		};
	};

/*
 * Author Note: AttachEvent Functions below renamed for compatibility.
 */
 
	//*** AttachEvent functions are copyright 2003 by Gavin Kistner, gavin@refinery.com
	//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
	//*** Reuse or modification is free provided you abide by the terms of that license.
	//*** (Including the first two lines above in your source code satisfies the conditions.)
	
	//***Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted
	//***e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);

	function et_AttachEvent(obj,evt,fnc,useCapture){
		if (!useCapture) useCapture=false;
		if (obj.addEventListener){
			obj.addEventListener(evt,fnc,useCapture);
			return true;
		} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
		else{
			MyAttachEvent(obj,evt,fnc);
			obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
		};
	};
	//The following are for browsers like NS4 or IE5Mac which don't support either
	//attachEvent or addEventListener
	function et_MyAttachEvent(obj,evt,fnc){
		if (!obj.myEvents) obj.myEvents={};
		if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
		var evts = obj.myEvents[evt];
		evts[evts.length]=fnc;
	};
	function et_MyFireEvent(obj,evt){
		if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
		var evts = obj.myEvents[evt];
		for (var i=0,len=evts.length;i<len;i++) evts[i]();
	};

