function doMyMedicaTemplate() {
  //return false;
  if (window.top != self) {
	  //alert("I'm within a frameset");
	  return true;
	} else {
	  return false;
	}
  
  //The check for parent.myMedicaContent throws a Permission Denied error in some IE 5.5 browsers
  /*
  if (window.top != self) {
	if (parent.myMedicaContent) {
	  //alert("I'm within a frameset, in a frame called 'myMedicaContent'");
	  return true;
	} else {
	  return false;
	}
  } else {
	//alert ("I'm the top dog in the window -- do the normal stuff.");
	return false;
  }
  */
}

function hideBranding() {
  if (doMyMedicaTemplate()) {
  
    if (document.getElementById('leftNav'))
		document.getElementById('leftNav').style.visibility = 'hidden';
	if (document.getElementById('sectionTop'))	
		document.getElementById('sectionTop').style.visibility = 'hidden';
	  
	// we need to reposition the div holding the main content, moving it up and to the left
	if (document.getElementById('sectionCenter')) {	
		var sectionCenter = document.getElementById('sectionCenter')
		sectionCenter.style.top = 50; //experiment to find the correct value
		sectionCenter.style.left = -150; //experiment to find the correct value	
	}
	
	if ( document.getElementById('myMedicaDiv') ) {
		document.getElementById('myMedicaDiv').style.left = 150; 
	}
	if ( document.getElementById('myMedicaLocator') ) {
		document.getElementById('myMedicaLocator').style.left = 0;
		document.getElementById('myMedicaLocator').style.width = '855px';
	}
	if ( document.getElementById('myMedicaLink') ) {
		document.getElementById('myMedicaLink').style.left = 0;
		document.getElementById('myMedicaLink').style.width = '855px';
	}
		
	//show the header for the child window
	if (document.getElementById('childWinSectionTop')) { 
		document.getElementById('childWinSectionTop').style.visibility = 'visible'; 
	}
  
  }  
}


function GetRelativeUrl() {
	var url = "" + document.location;	
	var indexOfCom = url.indexOf(".com/");
	var indexOfDefault = url.indexOf("default.aspx");
	var relPath = url.substring(indexOfCom + 5, indexOfDefault);
	return relPath;
}

function reWriteDocumentLinks() {
	//we want to open all documents within Sharepoint with the router application,
	//to allow logging and to avoid popups
	if (typeof reWriteDocLinks == "undefined") {
	  //alert("NOT re-writing doc links (reWriteDocLinks is null)");
	  return false;
	}
	if (reWriteDocLinks != 'Y') {
	  //alert("NOT re-writing doc links");
	  return false;
	} 
	
	var relativeUrl  = GetRelativeUrl()
		
	for (i=0; i < document.links.length; i++) {	
	  
		var hrefOrig = "" + document.links[i].href;	
		//alert("hrefOrig is: " + hrefOrig);		
		//all document links of interest will start with a reference to the area's document library
		var relPathIndx = hrefOrig.toLowerCase().indexOf(relativeUrl.toLowerCase() + "document")

		if (relPathIndx == -1) {
		   //compare current domain to domain in link, if same then send to router
	       var current_domain = "" + document.location;	
	       current_domain = current_domain.substring(0, current_domain.indexOf(".com/")+5)
	       var dest_domain = hrefOrig.substring(0, hrefOrig.indexOf(".com/")+5)
  			  if (current_domain == dest_domain && hrefOrig.toLowerCase().indexOf("document") > -1) {
		        relPathIndx = hrefOrig.toLowerCase().indexOf(".com/") + 5
		        if (hrefOrig.toLowerCase().indexOf("router/default.") > -1) {
							relPathIndx = hrefOrig.toLowerCase().indexOf("router/default.") + 24
		        }
			  }
		}
					
		//alert("is: " + hrefOrig.substring(relPathIndx, hrefOrig.length));		
		if ( relPathIndx > -1) {
			
			if (hrefOrig.toLowerCase().lastIndexOf("pdf") + 3 == hrefOrig.length) {
				document.links[i].href = "/router/default.pdf?doc=/" +  hrefOrig.substring(relPathIndx, hrefOrig.length)
			}
			if (hrefOrig.toLowerCase().lastIndexOf("doc") + 3 == hrefOrig.length) {
				document.links[i].href = "/router/default.doc?doc=/" +  hrefOrig.substring(relPathIndx, hrefOrig.length)
			}
			if (hrefOrig.toLowerCase().lastIndexOf("xls") + 3 == hrefOrig.length) {
				document.links[i].href = "/router/default.xls?doc=/" +  hrefOrig.substring(relPathIndx, hrefOrig.length)
			}
			if (hrefOrig.toLowerCase().lastIndexOf("ppt") + 3 == hrefOrig.length) {
				document.links[i].href = "/router/default.ppt?doc=/" +  hrefOrig.substring(relPathIndx, hrefOrig.length)
			}    	  
		}
	//alert("hrefOrig2 is: " + document.links[i].href);		
	}
	
}

function ensureSSL(){
  var loc = "" + document.location;
  
  if (loc.indexOf("https://") == -1) {
	return false;
  } else {
	 for (i=0; i < document.links.length; i++) {
	  var hrefOrig = "" + document.links[i].href;	    
	  	if (hrefOrig.indexOf("http://") == 0) {
		 if (hrefOrig.toLowerCase().indexOf("http://www.medica.com") == 0)
			  document.links[i].href = "https://" + hrefOrig.substring(7)
		  if (hrefOrig.toLowerCase().indexOf("http://providerdev1.medicadev.com") == 0) 
			  document.links[i].href = "https://" + hrefOrig.substring(7)
		  if (hrefOrig.toLowerCase().indexOf("http://member.medica.com") == 0)
			  document.links[i].href = "https://" + hrefOrig.substring(7)
		  if (hrefOrig.toLowerCase().indexOf("http://broker.medica.com") == 0)
			  document.links[i].href = "https://" + hrefOrig.substring(7)
		  if (hrefOrig.toLowerCase().indexOf("http://employer.medica.com") == 0)
			  document.links[i].href = "https://" + hrefOrig.substring(7)
		} //end if
	  }	//end for
   }//end else  
}//end function

function reWriteExtLinks(){
	//for event logging and security, we open documents via the router site
	reWriteDocumentLinks()
	//if we are in a child window, external links must be openned in their own child window.
	if (doMyMedicaTemplate()) {
		var isExtSite;
		for (i=0; i < document.links.length; i++) {	
				
			hrefOrig = document.links[i].href;
						
			isExtSite = false;
			if (hrefOrig.indexOf("http://") != -1 || hrefOrig.indexOf("https://") != -1) {
				isExtSite = true;
				if (hrefOrig.toLowerCase().indexOf("http://www.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("http://provider.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("http://member.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("http://broker.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("http://employer.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("http://visitor.medica.com") == 0)
					isExtSite = false;	
				if (hrefOrig.toLowerCase().indexOf("http://medica2.medica.com") == 0)
					isExtSite = false;
					
				if (hrefOrig.toLowerCase().indexOf("https://www.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("https://provider.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("https://member.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("https://broker.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("https://employer.medica.com") == 0)
					isExtSite = false;
				if (hrefOrig.toLowerCase().indexOf("https://visitor.medica.com") == 0)
					isExtSite = false;		
				if (hrefOrig.toLowerCase().indexOf("https://medica2.medica.com") == 0)
					isExtSite = false;				
					
				if (hrefOrig.toLowerCase().indexOf("medicadev.com") != -1)
					isExtSite = false;	
					
				//treat document links, even on our sites, as external sites, so they open in a new window	
				if ( (hrefOrig.toLowerCase().lastIndexOf("pdf") + 3 == hrefOrig.length)
				     || (hrefOrig.toLowerCase().lastIndexOf("doc") + 3 == hrefOrig.length)
				     || (hrefOrig.toLowerCase().lastIndexOf("xls") + 3 == hrefOrig.length)
				     || (hrefOrig.toLowerCase().lastIndexOf("ppt") + 3 == hrefOrig.length)
				  ){
					//alert("setting document link to open in child window");
					isExtSite = true;
				} else {
					//alert("for " + hrefOrig + ", lastIndex of pdf + 3 is: " + (hrefOrig.toLowerCase().lastIndexOf("pdf") + 3) + " and length is " + hrefOrig.length);
				}
								
			} 
			
			//check for re-written document links, to be opened on router site
			if (hrefOrig.toLowerCase().indexOf("?doc=") > -1) {
			  isExtSite = true;
			}
			
			if (isExtSite) {
			  document.links[i].href = "javascript:openExtSiteChildWin('" + hrefOrig + "');"
			  document.links[i].target = "";
			} 
			
		} // end for
	} // end outermost if					
} // end function

function openExtSiteChildWin(url) {
	window.open(url);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(hideBranding);
addLoadEvent(reWriteExtLinks);
addLoadEvent(ensureSSL);

//Used for the Provider Quick Search and Referral Search.
//When the Chiro product is selected, need to display the correct Specialty drop-down list.
function manageSpecialty(ddlProduct) {
	
	var allTableData = document.all.tags("td");
	var ddlSpecialty;
	var tdSpecialty;
	var tdChiroSpecialty;

	//find the specialty drop-down list 
	for (var i = 0 ; i < document.forms[0].elements.length; i++) { 
		if (document.forms[0].elements[i].id.indexOf('_ddlspecialty') != -1) { 
			ddlSpecialty = document.forms[0].elements[i]; 
			break; 
		}//end if 
	} //end for
	
	//find the table cells that display the drop-down list
	for (i = 0 ; i < allTableData.length; i++) {
		if ( allTableData[i].id=="td_specialty" )	{
			tdSpecialty = allTableData[i];
			for (i=i-1 ; i<allTableData.length; i++) {
					if ( allTableData[i].id=="td_chiro_specialty" )	{
						tdChiroSpecialty = allTableData[i];
						break;
					}//end if 
			}//end for
			break;
		}//end if 
	}//end for 

	//hide and show the correct drop-down list		
	if ((ddlProduct.selectedIndex == 13)||(ddlProduct.selectedIndex == 15)) {
		//default the specialty to "Chiropractic Medicine" 
		ddlSpecialty.selectedIndex = 7;
		tdSpecialty.style.display="none";
		tdChiroSpecialty.style.display="inline";
	} else {
		//reset index
		tdChiroSpecialty.style.display="none";
		tdSpecialty.style.display="inline";
		ddlSpecialty.selectedIndex = 0;
	}//end if 

}//end function manageSpecialty()
