var SERVLET_STRING = '/!redirect/';
var windowAttributes = "500,250"; //width,height
var debug=false;

// Array that contains all interstitials link
var interstitialSitesArray = new Array(	                                        "http://anac.on.ca/",
										"http://www.diabeteseducator.org/",
										"http://www.diabetes.org/",
										"http://www.eatright.org/",
										"http://www.barbaradaviscenter.org/",
										"http://www.diabetes.ca/",
										"http://www.phac-aspc.gc.ca/",
										"http://phac-aspc.gc.ca/",
										"http://www.cwd.org/",
										"http://www.childrenwithdiabetes.org/",
										"http://www.childrenwithdiabetes.com/",
										"http://cihr-irsc.gc.ca/",
										"http://www.cwdfoundation.org/",
										"http://www.mendosa.com/",
										"http://www.diabetes123.com/",
										"http://www.diabetescamps.org/",
										"http://www.diabeteshealth.com/",
										"http://www.diabetescontrol.com/",
										"http://www.diabetesincontrol.com/",
										"http://www.diabetesnet.com/",
										"http://www.diabetesmonitor.com/",
										"http://www.diabetesselfmanagement.com/",
										"http://www.diabete.qc.ca/",
										"http://www.dieticians.ca/",
										"http://www.diabetes.niddk.nih.gov/",
										"http://www.insulin-pumpers.org/",
										"http://parknicollet.com/diabetes",
										"http://parknicollet.com/",
										"http://www.joslin.org/",
										"http://www.jdrf.org/",
										"http://members.aol.com/CamelsRFun/index.html",
										"http://www.diabetes.niddk.nih.gov/",
										"http://www.tcoyd.org/",
										"http://www.dtu.ox.ac.uk/",
										"http://www.aace.com/",
										"http://www.ncbi.nlm.nih.gov/pubmed/",
										"http://www.cdc.gov/diabetes/",
										"http://www.idf.org/",
										"http://www.diabeteseducator.org/DiabetesEducation/Find.html",
										"http://www.aace.com/resources/memsearch.php");


// Array that contains all interstitials sites type.
// IMPORTANT: This array has the same dimension than interstitials link array above.
//            Each site type position in the array match the interstitials link position
//            ex: At position 3, "N" is the site type for http://www.eatright.org/ at position 3 in the first array
var interstitialSitesTypeArray = new Array(     "N",
                                                "N",
						"F",
						"N",
						"N",
						"F",
						"N",
						"N",
						"F",
						"F",
						"F",
						"N",
						"N",
						"N",
						"F",
						"F",
						"F",
						"F",
						"F",
						"F",
						"N",
						"F",
						"N",
						"N",
						"N",
						"F",
						"N",
						"N",
						"F",
						"F",
						"N",
						"N",
						"F",
						"N",
						"F",
						"N",
						"N",
						"N",
						"F",
						"F");


	/* Only update the link if we're supposed tof
		1. It starts with http
		2. The CURRENT hostname is not dev.disetronic-usa.*
		3. The hostname does not contain *.disetronic-usa.*
	*/
function isThisAnUpdateCandidate( url ) {

	if (debug) alert("Starting isThisAnUpdateCandidate function");

	url = url.toLowerCase();

	if (
			(
				url.substring(0, 4) == 'http'
					&&
				location.href.toLowerCase().indexOf('dev.accu-chekinsulinpumps-ca.com') == -1
					&&
				url.indexOf('.accu-chekinsulinpumps-ca.') == -1
			)
			||
			debug
		)
		{
			return true;
		}
		
		return false;
}

function processLink(tag, bIsIframe)
{
	if (debug) alert("Starting processLink function");

	var queryString = tag.getAttribute('href');

	if (!queryString || queryString=="" || queryString.length==0) return;

	if ( isThisAnUpdateCandidate(queryString) )
		{		
			// Replace the HREF's :// with just a /, and stick on the redirect servlet
			tag.setAttribute('href', SERVLET_STRING + tag.getAttribute('href').replace("://", "/"));
			if (debug) alert('HREF->'+ tag.getAttribute('href'));

			// If Interstitial enabled site (ex: US site), then set the href to popup interstitial
			if(location.href.toLowerCase().indexOf('.accu-chekinsulinpumps-ca.com/dstrnc_ca/') != -1){
				var idx = getInterstitialSiteIndex(queryString);
				if(idx != -1) {
					// Set href for an Interstitial Site
          			var linkDest = tag.getAttribute('href');
					var siteType = interstitialSitesTypeArray[idx];
					
					var _openwin_jscript="javascript:openCustomWin";
					// for iframes resolve functions to parent: window.parent
					if(bIsIframe) _openwin_jscript="javascript:window.parent.openCustomWin";

					tag.setAttribute("href", _openwin_jscript+"('/dstrnc_ca/features/popup/interstitial.jsp?site_type=" + siteType + "&url=" + linkDest + "'," + windowAttributes + ")");
					tag.setAttribute("target", "");			
				}
			}
		}
		//else { alert(queryString.toLowerCase().substring(0, 3)); }

}



/*
	1. Iterate through every link AND form action on the page.
	2. If their HREF or ACTION does not contain "_pageLabel", add "_pageLabel=$CURRENT_PAGE"

	// Added _document & bIsIframe arguments to the function. If no argument is passed in default document is used.
	// Provision for looping through the child iframes. see util.js.startUp() function - Janardhan Babu Chinta
*/
function updateLinks(_document,bIsIframe) {
	 if(!_document) _document=document;

	 if (debug==true) alert("Starting updateLinks function");

	// Find any link tags and grab their content
	tgs = _document.getElementsByTagName('a');
	if (!tgs) return;
	for (var idx=0;idx<tgs.length;idx++) {
		processLink( tgs[idx], bIsIframe );
	}	

	tgs = _document.getElementsByTagName('area');
	if (!tgs) return;
	for (var idx=0;idx<tgs.length;idx++) {
//		alert(tgs[i].href);
		processLink( tgs[idx] );
	}	


	// Finally, if this is on development, update all of the image tags
	if (location.href.toLowerCase().indexOf('dev.disetronic-ca.com') != -1) {
	  tgs = _document.getElementsByTagName('img');
	  for (var idx=0;idx<tgs.length;idx++) {
	  	if (tgs[idx].src.indexOf('acs')==-1 ) {
//			alert(tgs[i].src);
	    	tgs[idx].src = ('http://wip.accu-chekinsulinpumps-ca.com'+tgs[idx].src.substring(24, tgs[idx].src.length) );
//	    	tgs[i].src = ('http://dev.disetronic-ca.com/dstrnc_ca'+tgs[i].src.substring(24, tgs[i].src.length) );
	    }
	  }
	}

  	if (debug) alert("Finished updateLinks function");
}

/*
	1. Loop through the InterstitialSite array for the requested link.
	2. If the link exist return the array link index else -1
*/

function getInterstitialSiteIndex(queryString) {

	for ( var idx = 0; idx < interstitialSitesArray.length; idx++) {
		if(interstitialSitesArray[idx].toLowerCase().indexOf(queryString.toLowerCase())!=-1) {
			return idx;
		}
	}
	
	return -1;
}

