
var SERVLET_STRING = '/!redirect/';
var windowAttributes = "500,250"; //width,height
var debug=false;

// Array that contains all interstitials link
var interstitialSitesArray = new Array(	
"http://www.accu-chek.com/",
"http://www.accu-chek.com/us/rewrite/content/en_US/2.4.1:10/article/ACCM_general_article_2550.htm",
"http://www.mylabonline.com/",
"http://www.diabetes.org/",
"http://www.accp.com/",
"http://www.bloodclot.org/",
"http://www.acforum.org/",
"http://www.chestjournal.org/",
"http://www.coumadin.com/",
"http://www.outcomes-umassmed.org/",
"http://www.nattinfo.org/",
"http://www.thrombosisclinic.com/",
"http://www.natfonline.org/",
"http://heart.health.ivillage.com/index.cfm",
"http://www.hsforum.com/",
"http://www.labtestsonline.org/",
"http://www.stroke.org/site/PageNavigator/HOME",
"http://www.clotcare.com/",
"http://www.aace.com/meetings/consensus/IIDC/",
"http://www.sccm.org/SCCM/Professional+Development/",
"http://www.hospitalmedicine.org/AM/Template.cfm?Section=Quality_Improvement_Resource_Rooms1&Template=/CM/HTMLDisplay.cfm&ContentID=4337",
"http://www.diabetes.org/for-health-professionals-and-scientists/insulin-grand-rounds.jsp",
"http://www.ahrq.gov/clinic/ptsafety/",
"http://www.premierinc.com/all/advocacy/issues/medicare/pay-for-performance-improved-quality-0505.jsp",
"http://www.mha.org/mha/keystone/icu/projectoverview.jsp",
"http://www.ihi.org/IHI/Topics/CriticalCare/IntensiveCare/Measures/",
"http://www.ihi.org/IHI/Topics/CriticalCare/Sepsis/Changes/IndividualChanges/MaintainAdequateGlycemicControl.htm",
"http://www.jointcommission.org/CertificationPrograms/Inpatient+Diabetes/",
"http://www.medqic.org/dcs/ContentServer?cid=1136495755695&pagename=Medqic%2FOtherResource%2FOtherResourcesTemplate&c=OtherResource",
"http://cme.medicine.iu.edu/speakersbureau/",
"http://cme.medicine.iu.edu/expertnetwork/",
"https://www.usi.edu/health/cc_acc_roche.html",
"https://www.usi.edu/health/cc_lmc_roche.html",
"https://www.usi.edu/health/cc_chf_roche.html",
"https://www.usi.edu/health/cc_bill_roche.html",
"http://www.patientsafetywebcasts.com",
"http://www.hida.org/",
"http://www.asrm.org/",
"http://www.chestnet.org/",
"http://www.aafp.org/",
"http://www.pri-med.com/",
"http://www.scientificsessions.org/",
"http://www.tapestrymedical.com",
"http://www.coumadintest.com",
"http://www.medscape.com/infosite/poc",
"http://www.cms.hhs.gov/",
"http://www.cms.hhs.gov/center/coverage.asp",
"http://www.cms.hhs.gov/mcd/index_list.asp?list_type=ncd",
"http://www.cms.hhs.gov/CoverageGenInfo/04_LabNCDs.asp",
"http://www.cms.hhs.gov/FeeScheduleGenInfo/",
"http://www.cms.hhs.gov/Manuals/",
"http://www.cms.hhs.gov/clia/",
"http://www.cms.hhs.gov/MedicareContractingReform/",
"http://www.cms.hhs.gov/PQRI/01_Overview.asp",
"http://www.cms.hhs.gov/NationalProvIdentStand/01_Overview.asp",
"http://www.cms.hhs.gov/MLNEdWebGuide/25_EMDOC.asp",
"http://www.cigna.com/customer_care/healthcare_professional/coverage_positions/medical/mm_0109_coveragepositioncriteria_prothrombin_time_home_testing.pdf",
"http://www.regence.com/trgmedpol/dme/dme44.html",
"https://www.bcbsal.org/providers/policies/final/078.pdf",
"http://www.bcbsnc.com/services/medical-policy/pdf/prothrombin_time_monitoring_in_the_home.pdf",
"https://provider.bcbsga.com/provider/medpolicy/policies/guidelines/DME/prothrombin.html",
"http://www.empireblue.com/provider/noapplication/f2/s5/t9/pw_ad086476.pdf",
"https://www.oxhp.com/secure/policy/home_prothrombin_time_monitoring9.html",
"http://www.patientsafetywebcasts.com/",
"http://www.pri-med.org",
"javascript:openCustomWin('/en_US/multimedia/anticoag_member.html',640, 620);",
"javascript:openCustomWin('/en_US/multimedia/anticoag_member.html',640,620);",
"javascript:openWin('http://www.cms.hhs.gov/CLIA/06_How_to_Apply_for_a_CLIA_Certificate,_Including_Foreign_Laboratories.asp',600,800);urchinTracker('http://www.cms.hhs.gov/CLIA/06_How_to_Apply_for_a_CLIA_Certificate,_Including_Foreign_Laboratories.asp');"
);								

// 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("R","R", "R","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N","N");


	/* Only update the link if we're supposed tof
		1. It starts with http
		2. The CURRENT hostname is not dev.accu-chek.*
		3. The hostname does not contain *.accu-chek.*
	*/
function isThisAnUpdateCandidate( url ) {

	//if (debug) alert("Starting isThisAnUpdateCandidate function");

	url = url.toLowerCase();

	//if (debug) alert("isThisAnUpdateCandidate function URL = " + url);

	
	if ( ( url.substring(0, 4) == 'http' || url.substring(0, 5) == 'https' || url.substring(0, 11) == 'javascript:') &&
	     url.indexOf('.poc.roche.') == -1)
		{
			//if (debug) alert("isThisAnUpdateCandidate? yes = " + url);
			return true;
		}
		
	return false;
}

function processLink(tag, bIsIframe)
{
	//if (debug) alert("Starting processLink function");

	var queryString = tag.getAttribute('href');

	//if (debug) alert("processLink function = " + queryString);

	if (!queryString || queryString=="" || queryString.length==0) return;

	if (debug) alert("processLink function: test isThisAnUpdatecandidate = " + isThisAnUpdateCandidate(queryString));

	if ( isThisAnUpdateCandidate(queryString) )
	{
		if(queryString.substring(0, 11) == 'javascript:')
		{
			//if (debug) alert("processLink function: javascript " );
			if (debug) alert("processLink function: pull out url from JS  " + queryString );
			
			//pull out the link from the href javascript call and insert it into the new javascript popup call
				if (debug) alert("processLink function:  IS  openCustomWin" );
				sq =  queryString.indexOf("'");
				jsUrl = queryString.substring(sq+1);
				sq =  jsUrl.indexOf("'");
				jsUrl = jsUrl.substring(0, sq);
				if (debug) alert("processLink function:  cat string =" + jsUrl);
				
				// If Interstitial enabled site (ex: US site), then set the href to popup interstitial
				if(location.href.toLowerCase().indexOf('.poc.roche.com/') != -1){
					
					var idx = getInterstitialSiteIndex(queryString);
					if (debug) alert("processLink function: IDX =" + idx);
					if(idx != -1) {
						// Set href for an Interstitial Site
	          			var linkDest = tag.getAttribute('href');
						//var siteType = interstitialSitesTypeArray[idx];
						var siteType = "N";
							
						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+"('/poc/features/popup/interstitial.jsp?site_type=" + siteType + "&url=" + jsUrl + "'," + windowAttributes + ")");
						tag.setAttribute("target", "");
					}	
				}
			
		}else{
			if (debug) alert("processLink function not javascript " );
		
			// 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('.poc.roche.com/') != -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+"('/poc/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 i=0;i<tgs.length;i++) {
		
		// Replace any link that reference the POC site (/poc) by Coaguchek site (/coaguchek) 
		if( (tgs[i].href.substring(0, 4) == 'http') &&
                    (tgs[i].href.indexOf('poc.roche.com/poc') != -1) &&
		    (location.href.indexOf('poc.roche.com/coaguchek/') != -1) )
		{

			tgs[i].href = tgs[i].href.replace("poc.roche.com/poc/","poc.roche.com/coaguchek/");
			//alert(tgs[i].href);
		}
		
		processLink( tgs[i], bIsIframe );
	}	

	tgs = _document.getElementsByTagName('area');
	if (!tgs) return;
	for (var i=0;i<tgs.length;i++) {
		//alert(tgs[i].href);
		processLink( tgs[i] );
	}	

	// Finally, if this is on development, update all of the image tags
	if (location.href.toLowerCase().indexOf('/dev.poc.roche.com') != -1) {
	  tgs = _document.getElementsByTagName('img');
	  for (var i=0;i<tgs.length;i++) {


		if ( (location.href.toLowerCase().indexOf('/dev.poc.roche.com/poc/') != -1) &&
		     (tgs[i].src.indexOf('/poc') == -1) )
		{

		    tgs[i].src = tgs[i].src.replace("poc.roche.com/","poc.roche.com/poc/");		    

		}

		else if ( (location.href.toLowerCase().indexOf('/dev.poc.roche.com/coaguchek/') != -1) &&
		          (tgs[i].src.indexOf('/coaguchek') == -1) )
		{

		    tgs[i].src = tgs[i].src.replace("poc.roche.com/","poc.roche.com/coaguchek/");

		}

	  }
	}

  	//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 ( i = 0; i < interstitialSitesArray.length; i++) {
		if(interstitialSitesArray[i] == queryString) {
			return i;
		}
	}
	
	return -1;
}
