IsMSIE = navigator.appVersion.indexOf('MSIE') != -1;

// Sets the cookie for the RX affiliate referrer
function setRxAffiliateCookie() {
	CookieData = unescape(String(fetchGETVar('rxa')).replace(/[^a-z%20\.]/gi, ''));
	
	if (CookieData != 'null')	
		document.cookie = "ReferringAffiliate=" + CookieData + "; expires=01/01/2100 00:00:00; path=/; domain=www.cantrustrx.com";
}

function readAffiliateCookieIntoMenu() {		
	if (fetchGETVar('type') == 'new_patient' && fetchGETVar('step') == 'OnlineQuestionnaireForm') { 
		CookieDataArray = document.cookie.split(';');
		for (var i = 0; i < CookieDataArray.length; i++) {							
			ThisCookie = CookieDataArray[i].split('=');
			if (trim(ThisCookie[0]) == 'ReferringAffiliate') {
				document.getElementById('Please_Explain_ID').value = 'I was referred by ' +
				((ThisCookie[1].length > 15)?ThisCookie[1].substr(0, 15) + '...':ThisCookie[1]);
				break;
			}
		}
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

// Javascript for CanTrustRX
function enlargeAddress() {
	with (document.getElementById('SidebarAddressEnlarged'))
		style.display = 'block';
}

function hideEnlargedAddress() {
	with (document.getElementById('SidebarAddressEnlarged'))
		style.display = 'none';
}

function setNavArrow() {
	
	URLPart  = document.location.href.substr(document.location.href.indexOf('/')+2);
	ThisPage = URLPart.substr(URLPart.indexOf('/'));
	ThisPage = (ThisPage.lastIndexOf('/') > 0)?ThisPage.substr(0, ThisPage.lastIndexOf('/')):ThisPage;

	// Get child elements of main nav container
	ChildNavDivs = document.getElementById('MainNavContainer').getElementsByTagName('div');
	for (var i = 0; i < ChildNavDivs.length; i++) {
		Links = ChildNavDivs[i].getElementsByTagName('a');
		Imgs  = ChildNavDivs[i].getElementsByTagName('img');
		
		CurrentHREF = Links[0].getAttribute('href');
		
		// IE Jscript stupidly converts URLs to include their full path, including domain, protocol etc.
		if (IsMSIE)
			CurrentHREF = CurrentHREF.substr(document.location.href.indexOf('/')+2);
		
		CurrentHREF = CurrentHREF.substr(CurrentHREF.indexOf('/'));
		CurrentHREF = (CurrentHREF.lastIndexOf('/') > 0)?CurrentHREF.substr(0, CurrentHREF.lastIndexOf('/')):CurrentHREF;
		
		ThisPageArr = String(ThisPage).split(/\?|#/);
		
		if ((i == 0 && (ThisPageArr[0] == '' || ThisPageArr[0] == '/')) || CurrentHREF == ThisPageArr[0]) {
			Imgs[0].src = '/images/nav_arrow.gif';
			return;
		}
	}
}

// GENERATES EMAIL ADDRESS "SAFE" FROM HARVESTERS
function generateAntiSpiderEmail(Username, DomainName, TLD, OutputText, NoLink) {
	EmailAddress = Username + "&#64;" + DomainName + "&#46;" + TLD;
	OutputText   = ((OutputText)?OutputText:EmailAddress);
	
	document.write((NoLink)?OutputText:"<a href=\"ma" + "il" + "to:" + EmailAddress + "\">" + OutputText + "</a>");
}

// Toggles the CSS "display" property of an HTML element
function toggleCSSDisplay(Obj) {
	CurrentDisplay = Obj.style.display;
	Obj.style.display = (CurrentDisplay == 'none' || !CurrentDisplay)?'block':'none';
}

// OPENS A WINDOW ACCORDING TO PASSED ARGUMENTS
function openGenWindow(FilePath, WindowOptions) {	
	window.open(FilePath, String(Math.random()).replace(/\./, ""), WindowOptions + getCenterForPopupWindow(WindowOptions));
}

// GET HORIZONTAL CENTER OF WINDOW
function getWindowCenterX(w) {
	w = (w)?w:document.body.clientWidth;
	return ((screen.width-w)/2);
}

// GET VERTICAL CENTER OF WINDOW
function getWindowCenterY(h) {
	h = (h || navigator.appName != "Microsoft Internet Explorer")?h:document.body.clientHeight;
	return ((screen.height-h)/2);
}

// GET CENTER FOR WINDOW
function getCenterForPopupWindow(WindowOptions) {
	return ",left=" + getWindowCenterX(getVarsFromList(WindowOptions, "width")) + ",top=" + getWindowCenterY(getVarsFromList(WindowOptions, "height"));
}

// GETS CERTAIN VARS FROM A LIST (seperated by commas with no comma at the end -- useful for grabbing vars from window.open options)
function getVarsFromList(VarList, GetVar) {

	IndexOfVar    = VarList.indexOf(GetVar + '=') + (GetVar.length + 1);
	CommaAfterVar = VarList.substr(IndexOfVar).indexOf(",");
	return (CommaAfterVar > 0)?VarList.substr(IndexOfVar, CommaAfterVar):VarList.substr(IndexOfVar);
}

// ASSIGNS A JAVASCRIPT EVENT TO A SPECIFIED OBJECT ACCORDING TO THE BROWSER
function setEvent(Obj, Event, Action) {
	
	// For IE, use attachEvent
	if (IsMSIE) {
		eval("Obj.attachEvent('" + Event.toLowerCase() + "', function() { " + Action + " });");
	}
	
	// For proper browsers, simply use setAttribute
	else
		Obj.setAttribute(Event, Action);
}

// GETS A SPECIFIED GET VARIABLE FROM A GIVEN OR CURRENT URL
function fetchGETVar(GETVar, OverrideURL) {
	
	// Figure out what URL to use and then parse out the unnecessary parts
	UseURL = (OverrideURL)?OverrideURL:document.location.href;
	if (UseURL.indexOf('?') != -1 && UseURL.indexOf(GETVar) != -1) {
		URLArray      = UseURL.split('?');
		QueryStrArray = URLArray[1].split('&');
		
		// Set array of get vars
		GETVarArray = new Array(QueryStrArray.length);
		
		// Set the array values
		for (i in QueryStrArray) {
			CurrentVarSplit = QueryStrArray[i].split('=');
			CurrentVarName  = CurrentVarSplit[0];
			CurrentVarValue = CurrentVarSplit[1];
			eval('GETVarArray[\'' + CurrentVarName + '\'] = CurrentVarValue;');
		}
		
		if (GETVar)
			return GETVarArray[GETVar];
	}
	else
		return null;
}