var lmCount = 0;
var rmCount = 0;
var contentIndex = 0;
var curStyle = '';
var ORANGE = 'Orange';
var WHITE = 'White';
var DEFAULT = 'Default';

// Banners
var topBanners = new Array();
var bottomBanners = new Array();
var successBanners = new Array();
function genTopBanner(target, image, width, height, newWindow, main, studyIds) {
		if(studyIds) target+="&studyIds="+studyIds;
	if(topBanners.length==0 && main == 1) {	//first banner
		document.write("<a href='" + target + "' "+(newWindow?"target='_blank'":"")+"><img id=imgTop border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=image&width=" + width + "&height=" + height + "'/></a>");
	}
	else if(bottomBanners.length==0 && main == 2) {
		document.write("<a href='" + target + "' "+(newWindow?"target='_blank'":"")+"><img id=imgBottom border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=image&width=" + width + "&height=" + height + "'/></a>");
	}
	else if(successBanners.length==0 && main == 3) {
		document.write("<a href='" + target + "' "+(newWindow?"target='_blank'":"")+"><img id=imgSuccess border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=image&width=" + width + "&height=" + height + "'/></a>");
	}
	var img = new Image(width,height);
	img.src = "Common/Handlers/GetFile.ashx?path=" + image + "&type=image&width=" + width + "&height=" + height;
	img.tag = target;
	if(main == 1)
		topBanners.push(img);
	if(main == 2)
		bottomBanners.push(img);
	if(main == 3)
		successBanners.push(img);
}

var topBannerIndex = 1;
var bottomBannerIndex = 1;
var successBannerIndex = 1;
function rotateBanners(timer,runOnce, main) {
	if(main == 1) 
	{
		var imgObj = document.getElementById('imgTop');
		if(timer==0 || !imgObj || topBanners.length==0 || (topBannerIndex==0 && runOnce)) return;
		var img = topBanners[topBannerIndex++];
		if(topBannerIndex==topBanners.length) topBannerIndex = 0;
		if(img) {
			imgObj.src = img.src;
			setTimeout("rotateBanners("+timer+","+runOnce+","+main+");",timer);
		}
	}
	if(main == 2) 
	{
		var imgObj = document.getElementById('imgBottom');
		if(timer==0 || !imgObj || bottomBanners.length==0 || (bottomBannerIndex==0 && runOnce)) return;
		var img = bottomBanners[bottomBannerIndex++];
		if(bottomBannerIndex==bottomBanners.length) bottomBannerIndex = 0;
		if(img) {
			imgObj.src = img.src;
			setTimeout("rotateBanners("+timer+","+runOnce+","+main+");",timer);
		}
	}
	if(main == 3) 
	{
		var imgObj = document.getElementById('imgSuccess');
		if(timer==0 || !imgObj || successBanners.length==0 || (successBannerIndex==0 && runOnce)) return;
		var img = successBanners[successBannerIndex++];
		if(successBannerIndex==successBanners.length) successBannerIndex = 0;
		if(img) {
			imgObj.src = img.src;
			imgObj.parentNode.href = img.tag;
			setTimeout("rotateBanners("+timer+","+runOnce+","+main+");",timer);
		}
	}
}

function genCentralRefBaner(target, image, width, height)
{
	document.write("<li><a href='" + target + "' target='_blank'><img  border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=centralbaner&width=" + width + "&height=" + height + "'/></a></li>");
}

function genCentralNonRefBaner(target, image, width, height)
{
	document.write("<li><a href='" + target + "'><img  border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=centralbaner&width=" + width + "&height=" + height + "'/></a></li>");
}

function genLeftMenuBaner(source, image, width, height, target)
{
	document.write("<li><a href='" + target + "'><img border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=leftbaner&width=" + width + "&height=" + height + "'/></a></li>");
}

function genRightMenuBaner(source, image, width, height, target)
{
	document.write("<li><a href='" + target + "'><img border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=rightbaner&width=" + width + "&height=" + height + "'/></a></li>");
}

function genImageLI(source, image, width, height, target)
{
	document.write("<li><a href='" + target + "'><img border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=rightbaner&width=" + width + "&height=" + height + "'/></a></li>");
}

function genImage(source, image, width, height, target) {
	document.write('<p class="download-link">');
	document.write('<a href="' + target + '"><img src="Common/Handlers/GetFile.ashx?path=' + image + '&type=rightbaner&width=' + width + '&height=' + height + '" alt="' + source + '" /></a>');
	document.write('<a href="' + target + '"><span><strong>' + source + '</strong></span></a>');
	document.write('</p>');
}

function hideCentralBanners() {
	if(document.getElementById('centralBanners'))
		document.getElementById('centralBanners').style.display = "none";
}

//Page Content 
function toggleVis(id) {
	var controls = document.getElementsByName(id);
	for(var i=0; i < controls.length; i++)
	{
		if(controls[i].style.display=="none")
			controls[i].style.display="block";
		else 
			controls[i].style.display="none";
	}
}

function genPageContent(title, content)
{
	content = processContent(content).replace("[end]","");
	var id = contentIndex++;
	if(title) document.write('<h3 class="content-title">' + title + '</h3>');
	if(content.indexOf("[more]")==-1) {
		document.write('<p>' + content + '</p>');
	} else {
		var idx = content.indexOf("[more]");
		var preview = content.substring(0,idx);
		content = content.replace("[more]","");
		if(preview.indexOf("<ul>")>-1) preview += "</ul>";
		document.write('<div id="prev'+id+'"><p>' + preview + '&nbsp;&nbsp;<a href="javascript:toggleVis(\'prev'+id+'\');toggleVis(\'cont'+id+'\');" style="color:#06c;font-size:8pt;">Learn more</a></p></div>');
		document.write('<div id="cont'+id+'" style="display:none;"><p>' + content + '&nbsp;&nbsp;<a href="javascript:toggleVis(\'cont'+id+'\');toggleVis(\'prev'+id+'\');" style="color:#06c;font-size:8pt;">Close</a></p></div>');
	}
}

function genRightMenuContent(tabid, id, title, content, reference) {
	content = processContent(content);
	document.write('<h5>' + title + '</h5>');
	document.write('<p>' + content + '</p>');
	if(reference != null)
	{
		document.write('<div class="furth-link"><a class="green_text" href="' + reference + '" target="_blank">Learn more</a></div>');
		document.write('<div class="content-hor-strip"></div>');
	}
}

function genHomeSubContent(source, title, content, reference, width, height, target) {
	content = processContent(content);
	title = processContent(title);
    document.write('<div class="white-block-small-b r-corners b-shadow-2px ver-gap-10">');
	document.write('<div class="corner tl"></div>');
	document.write('<div class="corner tr"></div>');
	document.write('<div class="block-inner-wrap">');
	document.write('<div class="block-inner">');
	
	if (width != null && height != null && source != null)
	{
		document.write('<img src="Common/Handlers/GetFile.ashx?path=' + source + '&type=subtopicimage&width=' + width + '&height=' + height + '" alt="'+title+'" alt="' + title + '" class="sub-content-article" />');
	}
	document.write('<h2 class="sub-content-article">' + title + '</h2>');
	
	
	document.write('<p class="sub-content-article">' + content + '</p>');
	if (reference) {
		document.write('<div class="learnmore-panel">');
		document.write('<a href="' + reference + '" class="link-btn bordered r-corners"><div class="corner tl"></div><div class="corner tr"></div><span>Learn More</span><div class="corner bl"></div><div class="corner br"></div></a>');
		document.write('</div>');
	}
	document.write('</div>');
	document.write('</div>');
	document.write('<div class="corner bl"></div>');
	document.write('<div class="corner br"></div>');
	document.write('</div>');
}

//HomePage
function genHomeMainContent(title, content, reference)
{
	document.write('<br>');
	return;
	content = processContent(content);
	var sub_content = content != '' ? ' <small>' + content + '</small>' : '';
	if (title != '')
	{
		document.write('<h1 class="ic-success-platform-title image-title big-block-title">' + title + sub_content + '<span></span></h1>');
	}
}

var sectionCount =0;
function genHomeContent(title, content, reference) {
	content = processContent(content);
	if (title != '')
	{
		document.write('<h1>' + title + '</h1>');
	}
	
	if(content != '')
	{
		document.write('<p>' + content + '</p>');
	}
	
	if(reference != null)
	{
		if(sectionCount == 0) 
			document.write('<p class="furth-link"><a href="' + reference + '" '+getLinkTarget(reference)+'>Request a demo</a></p>');
		else
			document.write('<p class="furth-link"><a href="' + reference + '" '+getLinkTarget(reference)+'>Learn more</a></p>');
	}
	sectionCount++;
	document.write('<br />');
}

// Menus
function genRightMenu(tabid, id, name, selected, image, reference) {
	document.write('<li>');
	var href = reference ? reference : '?id=' + tabid + '&' + 'menu=' + id;
	if (curStyle == ORANGE) {
		document.write('<h3 class="big-link-btn">');
        document.write('<a href="'+href+'" class="orange-block-btn r-corners w-shadow-3px"><div class="corner tl"></div><div class="corner tr"></div><div class="block-inner-wrap"><div class="block-inner">' + name + '<span>&raquo;</span></div></div><div class="corner bl"></div><div class="corner br"></div></a>');
        document.write('</h3>');
	} else if (curStyle == WHITE) {
		if (image) document.write('<img src="Common/Handlers/GetFile.ashx?path=' + image + '" alt="" />');
		document.write('<a href="'+href+'">' + name + '</a>');
	}
	document.write('</li>');
}

function genContainerRightMenu(tabid, id, name,selected) {
	document.write("<LI><a style='cursor:hand;' "+
	(selected?"":"onmouseover=\"this.className='selected';\" onmouseout=\"this.className='';\" ")+ " onclick=genSubMenu('" + id + "')>" + name + "</a></LI>");
}

// Left Menu Rendering
function genLeftMenuHeader(name) {
	if (name) {
		document.write('<div class="menu-title-inner-wrap">');
		document.write('<h5>' + name + '</h5>');
		document.write('</div>');
	}
	document.write('<ul id="leftMenu_' + (lmCount++) + '" class="block-inner left-menu-items">');
}

function genLeftMenu(tabid, id, name, selected, image, reference) {
	var href = reference ? reference : '?id=' + tabid + '&' + 'menu=' + id;
	document.write('<li>');
	if (selected) {
		document.write('<div class="selected divided"><strong>' + name + '</strong></div>');
	} else {
		document.write('<a href="'+href+'" class="divided un-underlined">' + name + '</a>');
	}
	document.write('</li>');
}


function genSubLinksBegin(id) {
	document.write('<ul id="' + id + '" style="display:none">');
}

function genSubLink(tabid, id, name, selected) {
	document.write('<li>');
	if (selected) {
		document.write('<div class="selected"><strong>' + name + '</strong></div>');
	} else {
		document.write('<a href="?id=' + tabid + '&' + 'menu=' + id + '" class="un-underlined">' + name + '</a>');
	}
	document.write('</li>');
}

function genSubLinksEnd() {
	document.write("</ul></li>");
}

function genSubLinksBeginBlock(id)
{
	document.write('<ul class="subLink" id="' + id + '" style="display:block">');
}

// Left Menu Rendering
function genSubMenu(id, inner)
{
	var menulinkdiv = document.getElementById(id);
	var menulink_li = document.getElementById("li_" + id);
	if (menulinkdiv == null) {
		return false;
	}
	if (menulinkdiv.style.display == 'block') {
		menulinkdiv.style.display = 'none';
		menulink_li.setAttribute("class", "");
		menulink_li.setAttribute("className", "");
		if(inner) {	
			inner.setAttribute("class", "innerexpandable divided un-underlined");
			inner.setAttribute("className", "innerexpandable divided un-underlined");
			}
		} else {
		menulinkdiv.style.display = 'block';
		menulink_li.setAttribute("class", "submenu-header expanded divided");
		menulink_li.setAttribute("className", "submenu-header expanded divided");
		if(inner) {	
			inner.setAttribute("class", "expandable divided un-underlined");
			inner.setAttribute("className", "expandable divided un-underlined");
		}
	}
}

function genRightMenuHeader(name, style)
{
	curStyle = style;
	if (curStyle == WHITE) {
		document.write('<div class="ver-gap-10">');
        document.write('<div class="side-menu white-block r-corners w-shadow-3px">');
        document.write('<div class="corner tl"></div>');
        document.write('<div class="corner tr"></div>');
        document.write('<div class="block-inner-wrap">');
        document.write('<div class="block-inner">');
	}
	
	document.write('<ul id="rightMenu_' + (rmCount++) + '">');
	if (name) {
		document.write('<h5>' + name + '</h5>');
	}
}

function genCloseUL() {
	if (curStyle == WHITE) {
		document.write('</div>');
        document.write('</div>');
        document.write('<div class="corner bl"></div>');
        document.write('<div class="corner br"></div>');
        document.write('</div>');
        document.write('</div>');
	}
	document.write('</ul>');
}

function genStartImages() {
	document.write('<ul id=imageMenu >');
}

function genEndImages() {
	document.write('</ul>');
}

function genTab(id, name, selected, isTop, isLast) {
	if (selected) {
		document.write('<li class="selected">');
		document.write('<strong>' + name + '</strong>');
	} else {
		document.write('<li>');
		document.write('<a href="Default.aspx?id=' + id + '" class="un-underlined">' + name + '</a>');
	}
	document.write('</li>');
}

function genRefTab(reference, name, selected, isTop, isLast) {
	if (isTop) {
		var itemStyle = 'un-underlined small-button r-corners ' + ((isLast) ? 'blue-btn small-button-last' : 'orange-btn');
		//document.write("<li><a href='" + reference + "' " + getLinkTarget(reference) + ">&gt;&nbsp;" + name + "</a></li>");
		
		document.write('<a href="' + reference + '" class="' + itemStyle + '"><div class="corner tl"></div><div class="corner tr"></div><span>' + name + '</span><div class="corner bl"></div><div class="corner br"></div></a>');
	}
}

function genBottomMenuLink(tabid, id, name,selected) {
	document.write("<li><a "+(selected?"class=selected":"")+" href='Default.aspx?id=" + tabid + "&menu=" + id + "'>"+name+"</a></li>");
}

function genRefBottomMenuLink(reference, name) {
	document.write("<li><a href='" + reference + "' "+getLinkTarget(reference)+">"+name+"</a></li>");
}

function getLinkTarget(reference) {
	if(reference.indexOf("http")==0 && reference.indexOf(location.hostname)==-1)
		return " target=_blank ";
	return "";
}

function IDM1LogIn(l, p, page) {
	if(l=='' || p=='') {
		return;
	}
	var form = document.createElement("<form method='post' target='_self' action='http://localhost/IDM1/LogMeIn.aspx'>");
	var login = document.createElement("<input type='hidden' name='login' value='"+l+"'>");
	var pass = document.createElement("<input type='hidden' name='pass' value='"+p+"'>");
	var redir = document.createElement("<input type='hidden' name='redirect' value='" + page + "'>");
	form.insertBefore(login);
	form.insertBefore(pass);
	form.insertBefore(redir);
	document.body.insertBefore(form);
	form.submit();
}

function genSubmenuLink(tabid, id, name,selected) {
	document.write("<li>"+(selected?"<span>":"")+"<a href=?id=" + tabid + "&" + "menu=" + id + ">" + name + "</a>"+(selected?"</span>":"")+"</li>");
}

function search(e)
{
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) 
	{
		keynum = e.keyCode;
	}
	else if(e.which)
	{
		keynum = e.which;
	}

	if(keynum == 13)
	{
		doSearch();
	}
}

function doSearch() {
	var searcher = document.getElementById('searchText');
	var sURL = unescape(window.location);
	if(sURL.indexOf('?') > 0)
	{
		if(sURL.indexOf('search') > 0)
		{
			window.location.href = window.location.href.substring(0, window.location.href.indexOf('search') - 1) + '&search=' + searcher.value;
		}
		else
		{
			window.location.href += '&search=' + searcher.value;
		}
	}
	else
	{
		if(sURL.indexOf('search') > 0)
		{
			window.location.href = window.location.href.substring(0, window.location.href.indexOf('search') - 1) + '&search=' + searcher.value;
		}
		else
		{
			window.location.href += '?search=' + searcher.value;
		}
	}
}

function genInnerTab(idx, clientId, name) {
    document.write('<li id="' + clientId + '_tab_' + idx + '" class="tab r-corners' + (idx == 1 ? ' selected' : '') + '" onClick="switchTab(\'' + clientId + '\', this)">');
	document.write('<div class="corner tl"></div>');
    document.write('<div class="corner tr"></div>');
    document.write('<div class="tab-inner"><strong>' + name + '</strong></div>');
	document.write('</li>');
}

function genBottomInnerTab(idx, clientId, name) {
	document.write('<li id="' + clientId + '_slider_' + idx + '" class="' + (idx == 1 ? 'slider-pointer' : '')+ '">' + name + '</li>');
}