
// overwritten in a page in case of special init handling
function PreInit() {};
function PostInit() {};

function Init()
{
	PreInit();
	InitMenu();
	FixParentHeight("mainContent");
	PostInit();
}

function InitMenu()
{
	FixDropdownForIE("menuList");
}

function FixDropdownForIE(ulTagID)
{
	if (document.all && document.getElementById)
	{
		navRoot = document.getElementById(ulTagID);
		if (navRoot != null)
		{
			for (i=0; i < navRoot.childNodes.length; i++)
			{
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI")
				{
					node.onmouseover=function()
					{
						this.className+=" over";
						var obj = document.mainform.elements;
						for (var j=0; j< obj.length; j++)
						 {
						 	if ((obj[j].type =="select-one") && (obj[j].name != "CCOWebBody:CCOWebHeader:ddlTopics"))
						 	{
						 		obj[j].style.visibility = "hidden";
						 	}
						 }
					};
					node.onmouseout=function()
					{
						this.className=this.className.replace(" over", "");
						var obj = document.mainform.elements;
						for (var j=0; j< obj.length; j++)
						 {
						 	if (obj[j].type =="select-one")
						 	{
						 		obj[j].style.visibility = "visible";
						 	}
						 }
					};
				}
			}
		}
	}
}

function FixParentHeight(parentID)
{

	if (document.getElementById) 
	{
		var node = document.getElementById(parentID);
		var hgt = 0;
		for (var i=0; i < node.childNodes.length; i++) {
			var child = node.childNodes[i];
			if (child.nodeName=="DIV") 
			{
				if ((child.offsetHeight+child.offsetTop) > hgt) hgt = (child.offsetHeight + child.offsetTop);
			}
		}
		node.style.height = hgt + "px";
		return true;
	}
}

function openExternalLink(url)
{
	alert("You are now leaving this CME activity and visiting an external site. The faculty, the accrediting body, and Clinical Care Options, LLC are not responsible for the accuracy or objectivity of the content of the site you are about to visit. Click 'OK' to continue to the external site.");
	CCOWindowOpen(url,"external");
}

function OpenForgotPasswordDialog()
{
	CCOWindowOpen("/x/Forgot%20Password.aspx","forgotpassword","width=300,height=300");
}

function PopCertificate(guid,type)
{
    var url = "/x/Certificate.aspx?ID="+guid;
    if(type != null) url += "&type="+type;

	CCOWindowOpen(url,"certificate","height=680,width=650,status,scrollbars,toolbar",true);
}

//function openPrintVersion()
//{
//	var url = window.location.href;
//	if (url.indexOf("?") > -1)
//	{
//		url += "&lg=print";
//	}
//	else
//	{
//		url += "?lg=print";
//	}
//	CCOWindowOpen(url,"print","width=600, height=575, resizable, scrollbars");
//}

function showMakeHomepageMsg(idx)
{
	var msg = "";
	switch(idx)
	{
		case "ok":
			msg = "You have successfully made this your default home page.";
			break;
		case "anon":
			msg = "You must be logged in to take advantage of this feature.";
			break;
		case "error":
			msg = "An error occurred when trying to save your default home page.";
			break;
	}
	
	alert(msg);
}

function popPage(path)
{
	popWindow(path, 600, 530);
}

function calcHeightSurvey(iframeTitle, divTitle, show) {
    //find the height of the internal page
    var iframe = document.getElementById(iframeTitle);
    var the_height = 0;
    if($.browser.firefox) the_height = document.getElementById(iframeTitle).contentWindow.document.body.scrollHeight;
    else the_height = window.frames[iframeTitle].document.documentElement.scrollHeight;
    iframe.height = the_height;
}



function showHide(divTitle, iframeTitle, show, offset) {
    var emaildiv = window.parent.document.getElementById(divTitle);
    var iframe = window.parent.document.getElementById(iframeTitle)
    if (show) {
          emaildiv.style.display = "block";
          if ($(emaildiv).children('.inlineIframe').length > 0) {
              $(emaildiv).children('.inlineIframe')[0].contentWindow.location.reload();  //reload child to force resize of any child questions
          }
    } 
    else {
        emaildiv.style.display = "none";
        var the_height = 0;
        the_height = iframe.contentWindow.document.body.scrollHeight;
        iframe.height = the_height;
    }
}


//function for popping NeuroAIDS animations/videos
function popNeuroAIDSAnimation(path)
{
	popWindow(path, 390, 320);
}

function popWindow(path, width, height)
{
	var nURL;

	// Previously, nURL = escape(path). This would escape the querystring parameters.
	// Now, we only escape the non-querystring parameter portion of the URL
	var i = path.indexOf("?");
	if (i > 0)
	{
		// Escape the URL, but not the parameters
		nURL = escape(path.substring(0, i));
		nURL += path.substring(i, path.length);
	}
  	else
	{
		nURL = escape(path);
	}

	if (path.indexOf(".aspx") == -1)
	{
		nURL = "/layouts/CCO.Print.aspx?path=" + nURL;
		nURL += "&p=1";
	}
	
	var d = new Date();
	var millis = Math.floor(d.getTime());
	var w;
	
	var title = "print" + millis;
	w = window.open(nURL, title, "width=" + width + ", height=" + height + ", resizable, scrollbars=1");

	if (w)
		w.focus();
	else // blocked!
		alert('Message from Clinical Care Options:\n' +
				'\n' +
				'The item you clicked on cannot be displayed - most likely\n' +
				'because you are blocking popup windows for this site.\n' +
				'\n' +
				'Try clicking on the item again. If that doesn\'t work, you\n' +
				'will need to disable popup blocking to see the item.\n');
}

function CCOWindowOpen(url, title, extras,suppressLG)
{
	var date = new Date();
	var millis = Math.floor(date.getTime());
	var openedWindow;

	var internalLink = true;
	
	if (title == "external")
	{
		internalLink = false;
	}
	title = title.split(" ").join("") + millis;
	
	// what is the following for?
	
	// "normalize" URL
//	if ((url.substr(0, 4).toLowerCase() != "http") && (url.indexOf('/') > 0 || url.indexOf('/') < 0))
//	{
//		url = Url.substr(0, Url.lastIndexOf('/') + 1) + url;
//	}
	// add the print layout unless external url
	if (internalLink && !suppressLG)
	{
		// check to see if a layout is already specified
		if (url.toLowerCase().indexOf("lg=") < 0)
		{
			if (url.indexOf("?") > 0)
			{
				url += "&lg=print";
			}
			else
			{
				url += "?lg=print";
			}
		}
	}
	if (extras)
	{
        if (extras.toLowerCase().indexOf("scrollbars") < 0)
	    {
		    // Add "scrollbars=1"
		    if (extras.length == 0)
		    {
			    extras = "scrollbars=1";
		    }
		    else
		    {
			    extras += ",scrollbars=1";
		    }
	    }		
	    openedWindow = window.open(url, title, extras);
	}
	else
	{
		openedWindow = window.open(url, title);
	}
	if (openedWindow)
	{
		openedWindow.focus();
	}
	else // blocked!
	{
		alert('Message from Clinical Care Options:\n' +
				'\n' +
				'The item you clicked on cannot be displayed - most likely\n' +
				'because you are blocking popup windows for this site.\n' +
				'\n' +
				'Try clicking on the item again. If that doesn\'t work, you\n' +
				'will need to disable popup blocking to see the item.\n');
	}
}

function openWindowCentered (url, title, params, height, width, suppressLG)
{
	var left = (screen.width - width) / 2;
	var top = (screen.height - height)/2;

	CCOWindowOpen(url, title, (params + ", width=" + width + ", height=" + height + ", top=" + top + ", left=" + left),suppressLG);
}

function ValidateRadioButtonGroup(radioButtonName)
{
	var isSelected = false;

	var radioButton = eval("document.forms[0]." + radioButtonName);
	
	for (counter = 0; counter < radioButton.length; counter++)
	{
		if (radioButton[counter].checked)
		{
			isSelected = true;
        }
	}
	return isSelected;
}


//Needed to fix groupName in radiobutton within repeaters
function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

/**
 * Wrapper for Ext's Ajax request.
 * It assumes the service is returning a JSON object created by our .Net ServiceResponse
 * The config object has the following parameters:
 * @param {string} service The name of the asmx (without the extension. ex. "ProgramService")
 * @param {string} methodName The name of the method to be called on the web service
 * @param {object} params (optional) The parameters to be passed to the Ajax request
 * @param {function} callback (optional) The callback function to be called, will be called with 4 parameters:
		@param {object} json The json object returned from the server
		@param {object} options The options object from the Ajax request
		@param {boolean} success Whether the request was successful
		@param {object} response The raw response from the Ajax call
 * @param {object} scope (optional) The scope in which the callback should be called
 * @param {boolean} showErrorMessage (optional) defaults to true. Sets whether or not to display an Ext alert on errors
 * @return {void}
 */
function ServiceRequest(obj)
{
	var cb = obj.callback;
	var scope = obj.scope;
	var showErrorMessage = !(obj.showErrorMessage === false); 
	var url = "/WebServices/" + obj.service + ".asmx/" + obj.methodName;
	Ext.Ajax.request({
		url: url,
		params: obj.params,
		callback: function(options,success,response)
		{	
			var message,json,header;
			if(!success)
			{	
				json = {
					success: false,
					header: "Error",
					message: "There was an error processing that request."
				};
			}
			else
			{
				json = eval("("+response.responseText+")");
			}
			
			if(!json.success && showErrorMessage)
			{
				Ext.MessageBox.alert(json.header,json.message);
			}
			
			if (cb != null)
			{
				if (!scope)
				{
					cb(json,options,response);
				}
				else
				{
					cb.apply(scope,[json,options,response]);
				}
			}
		}
	});
};
function makevisible(cur,which){
strength=(which==0)? 1 : 0.60;

if (cur.style.MozOpacity)
cur.style.MozOpacity=strength;
else if (cur.filters)
cur.filters.alpha.opacity=strength*100;
}
function switchHighlightDesc(desc,vID) {
		var thediv = document.getElementById('videoHighlight');
		thediv.innerHTML = desc;
		document.getElementById(currentVideo).className="highlightsNav";
		document.getElementById(vID).className="highlightsNavCurrent";
		currentVideo=vID;

	}
function switchVideoDesc(desc) {
		var thediv = document.getElementById('nowplaying');
		thediv.innerHTML = "<p>" + desc + "</p>";	
	}
function blankVideo(desc) {
		var thediv = document.getElementById('flashcontent');
		thediv.innerHTML = "<img src='" + desc + "'>";
	}
	function switchVideoPreviewDesc(desc) {
		var thediv = document.getElementById('previewdesc');
		if (desc == 'reset')
		{
			desc = 'Click an image above for more.';
		}
		thediv.innerHTML = "<p><b>Also Available:</b> " + desc + "</p>";
}

function showEula() { }
