var lmCount = 0;
var rmCount = 0;
var leftRef = "";
var centerRef = "";
var rightRef = "";
var contentIndex = 0;

function processContent(content) {
	return content.replace(/\[/g,'<').replace(/\]/g,'>').replace(/\[ul\]/g,"<ul class='content'>").replace(/\[\/ul\]/g,"</ul>").replace(/\[li\]/g,"<li>").replace(/\[\/li\]/g,"</li>").replace(/\[b\]/g,"<b>").replace(/\[\/b\]/g,"</b>").replace(/\[i\]/g,"<i>").replace(/\[\/i\]/g,"</i>").replace(/\[u\]/g,"<u>").replace(/\[\/u\]/g,"</u>");
}
var firstPricing = true;
function genPricing(title, subtitle, price, note, linkName, text, linkUrl) {
    text = processContent(text);
    document.write("<td class=pricingCell"+(firstPricing?"First":"")+">");
    document.write("<div class=pricingTitle>" + title + "</div>");
    document.write("<div class=pricingSubtitle>" + subtitle + "</div>");
    document.write("<div class=pricingPrice>" + price + "</div>");
    document.write("<div class=pricingNote>" + note + "</div>");
    document.write('<a href="' + linkUrl + '" class="un-underlined small-button r-corners pricing-btn small-button-last"><div class="corner tl"></div><div class="corner tr"></div><span>' + linkName + '</span><div class="corner bl"></div><div class="corner br"></div></a>');

    document.write("<div class=pricingText>" + text + "</div>");
    document.write("</td>");
    firstPricing = false;
}

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";
	}
}

// Banners
function GenHomeBanner(href, image)
{
    document.write("<img src=\"Common/Handlers/GetFile.ashx?path=" + image + "&type=image\" alt=\"\" />");
}

function genTopBanner(target, image, width, height, newWindow, main) {
	document.write("<a href='" + target + "' "+(newWindow?"target='_blank'":"")+"><img  border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=image&width=" + width + "&height=" + height + "'/></a>");
}

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 hideCentralBanners() {
	if(document.getElementById('centralBanners'))
		document.getElementById('centralBanners').style.display = "none";
}

//Page Content 
function genPageContent(title, content)
{
	document.write('<div class="page_content">');
	document.write('<h1>' + title + '</h1>');
	document.write('<p class="page_content">' + content + '</p>');
	document.write('</div>');
}

function genPageContent(title, content)
{
	content = processContent(content).replace("[end]","");
	var id = contentIndex++;
	document.write('<div class="page_content">');
	document.write('<h1>' + title + '</h1>');
	if(content.indexOf("[more]")==-1) {
		document.write('<p class="page_content">' + 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>');
		//document.write('<p id="prev'+id+'">' + preview + '&nbsp;&nbsp;<a href="javascript:toggleVis(\'prev'+id+'\');toggleVis(\'cont'+id+'\');">Learn more</a></p>');
		//document.write('<p id="cont'+id+'" style="display:none;">' + content + '&nbsp;&nbsp;<a href="javascript:toggleVis(\'cont'+id+'\');toggleVis(\'prev'+id+'\');">Hide</a></p>');
		
	}
	document.write('</div>');
}

function genRightMenuContent(tabid, id, title, content) {
	document.write('<div class="right_column_inner right_top">');
	document.write('<h1>' + title + '</h1>');
	document.write('<p>' + content + '</p>');
	document.write('</div>');
}

function genHomeSubContent(source, title, content, reference,width, height,target) {
	if(width != null && height != null && source != null)
	{
		document.write('<a href="'+target+'"><img src="Common/Handlers/GetFile.ashx?path=' + source + '&type=subtopicimage&width=' + width + '&height=' + height + '"/></a>');
	}
	document.write('<h3><a href="'+reference+'">' + title + '</a></h3>');
	document.write('<p>' + content + '</p>');
}

//HomePage
function genHomeMainContent(title, content, reference)
{
	content = processContent(content);
	if (title != '')
	{
		document.write('<h1>' + title + '</h1>');
	}
	
	var id = contentIndex++;
	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+'\');">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+'\');">Close</a></p></div>');
		//document.write('<p id="prev'+id+'">' + preview + '&nbsp;&nbsp;<a href="javascript:toggleVis(\'prev'+id+'\');toggleVis(\'cont'+id+'\');">Learn more</a></p>');
		//document.write('<p id="cont'+id+'" style="display:none;">' + content + '&nbsp;&nbsp;<a href="javascript:toggleVis(\'cont'+id+'\');toggleVis(\'prev'+id+'\');">Hide</a></p>');
	}
	if(reference != null)
	{
		document.write('<div class="furth-link"><a class="white-text" href="' + reference + '" target="_blank">Learn more</a></div>');
	}
}

function genHomeContent(title, content, reference) {
	content = processContent(content);
	var id = contentIndex++;
	if (title != '') {
		document.write('<h1>' + title + '</h1>');
	}
	
	if(content != '') {
		document.write('<p>' + content + '</p>');
	}
	
	if (leftRef == "" && leftRef != null) {
		leftRef = reference;
	} else if (centerRef == "" && centerRef != null) {
		centerRef = reference;
	} else if (rightRef == "" && rightRef != null) {
		rightRef = reference;
	}

/*	if(reference != null) {
		document.write('<div class="furth_link">');
		document.write('<a href="' + reference + '" class="add_link">View Products&nbsp;&nbsp;<img src="img/icertainty/furth.gif" width="18" height="18" alt="" /></a>');
		document.write('<a href="' + reference + '" ' + getLinkTarget(reference )+ '><img src="img/icertainty/learnmore.gif" width="112" height="31" alt="Learn More" /></a>');
		document.write('</div>');
	}*/
}

function writeRef(reference) {
	if(reference != null) {
		document.write('<div class="furth_link">');
		document.write('<a href="' + reference + '" class="add_link">View Products&nbsp;&nbsp;<img src="img/icertainty/furth.gif" width="18" height="18" alt="" /></a>');
		document.write('<a href="' + reference + '" ' + getLinkTarget(reference )+ '><img src="img/icertainty/learnmore.gif" width="112" height="31" alt="Learn More" /></a>');
		document.write('</div>');
	}
}

// Menus
function genRefMenu(reference, name) {
	document.write("<LI><a href=" + reference + ">"+name+"</a></LI>");
}

function genLeftMenu(tabid, id, name,selected) {
	document.write("<LI><a "+(selected?"class=selected":"")+" href=?id=" + tabid + "&" + "menu="+id+">"+name+"</a></LI>");
}

function genRightMenu(tabid, id, name,selected) {
	document.write("<LI><a "+(selected?"class=selected":"")+" href=?id=" + tabid + "&" + "menu="+id+">"+name+"</a></LI>");
}

function genContainerLeftMenu(tabid, id, name,selected) {
	document.write("<LI><a style='cursor:hand;' onclick=genSubMenu('" + id + "')>" + name + "</a></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>");
}

function genLeftMenuHeader(name)
{
	document.write('<ul id="leftMenu_' + (lmCount++) + '" class="left_menu">' + (name ? '<h4>' + name + '</h4>' : ''));
}

function genRightMenuHeader(name)
{
	document.write('<ul id="rightMenu_' + (rmCount++) + '">' + (name ? '<h4>' + name + '</h4>' : ''));
}

function genCloseUL() {
	document.write('</ul>');
}

function genStartImages() {
	document.write('<ul id=imageMenu >');
}

function genEndImages() {
	document.write('</ul>');
}

//submenus
function genSubMenu(id)
{
	var menulinkdiv = document.getElementById(id);
	if(menulinkdiv == null)
	{
		return false;
	}
	if(menulinkdiv.style.display == 'block')
		menulinkdiv.style.display = 'none'
	else
		menulinkdiv.style.display = 'block';
	
}

function genSubLink(tabid, id, name,selected)
{
	document.write("<li><a "+(selected?"class=selected":"")+" href=?id=" + tabid + "&" + "menu="+id+">"+name+"</a></li>");
}

function genSubLinksEnd()
{
	document.write("</UL>");
}

function genSubLinksBegin(id)
{
	document.write("<UL class=subLink id='" + id + "' style='display:none'>");
}

function genSubLinksBeginBlock(id)
{
	document.write("<UL class=subLink id='" + id + "' style='display:block'>");
}

function genTab(id,name,selected) {
	document.write("<li>");
	if (selected) {
		document.write("<span>" + name + "</span>");
	} else {
		document.write("<a href='Default.aspx?id=" + id + "'>" + name + "</a>");
	}
	document.write("</li>");
}

function genTab(id, name, selected, isTop, isLast) {
	if (selected) {
		document.write('<li class="selected">');
		//document.write('<span>' + name + '</span>');
		document.write('<span><a href="Default.aspx?id=' + id + '" style="margin-right:0">' + name + '</a></span><span class="selected_right">&nbsp;</span>');
		//document.write('<span class="selected_right">&nbsp;</span>');
	} else {
		document.write('<li>');
		document.write('<a href="Default.aspx?id=' + id + '">' + name + '</a>');
	}
	document.write('</li>');
}

function genRefTab(reference, name,selected) {
	document.write("<li><a "+(selected?"class=selected":"")+" href='" + reference + "' "+getLinkTarget(reference)+">"+name+"</a></li>");
	document.write("<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>");
}

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 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("<a href='" + target + "'><img border=0 src='Common/Handlers/GetFile.ashx?path=" + image + "&type=rightbaner&width=" + width + "&height=" + height + "'/></a>");
}

var helphash;
var mainframe;
var playertable;
function loadHelp(id, title, isPath) {

    var video = isPath ? id : 'Common/Handlers/GetFile.ashx?id=' + id;
    helphash = new Hashtable();
    mainframe = document.getElementById('mainframe');
    playertable = document.getElementById('player_table');
    if (!mainframe || !helphash || !playertable) return;
	mainframe.style.width = '100%';
	mainframe.style.height = window.document.body.scrollHeight;
	mainframe.style.filter = "alpha(opacity=90)";
	mainframe.style.background = "#333";
	if (swfobject.hasFlashPlayerVersion("6")) 
	{
		var c = document.getElementById("player");
		if (!c) {
			var d = document.createElement("div");
			d.setAttribute("id", "player");
			d.setAttribute("style", "position:absolute;z-index:100;");
			document.getElementById("player_table").appendChild(d);
		}
		
		var att = { data:"Controls/uflvplayer.swf", width:"500", height:"300" };
		var par = { flashvars: "comment=Video&nbsp;Help&autoplay=1&skin=white&noembed=1&way=" + video};
		var swfid = "player";
		swfobject.createSWF(att, par, swfid);
	}

	playertable.style.left = document.body.clientWidth / 2 - 250;
	playertable.style.top = document.body.scrollHeight - document.body.clientHeight + 200 + 30;
	playertable.style.width = '500px';
	playertable.style.height = '336px';
	playertable.style.display = 'block';
		
	//var main = document.getElementById('mainpage');
	//main.style.filter = "alpha(opacity=60)";
	
	var playerHeader = document.getElementById('player_header');
	playerHeader.innerHTML = title ? title : (helphash.containsKey(id.toString()) ? helphash.get(id.toString()) : "Video Player");

	var imgClose = document.getElementById('close_button');
	imgClose.onclick = function () {
				mainframe.style.background = "#FFF";
				mainframe.style.width = '0';
				mainframe.style.height = '0';
				//main.style.filter = "";
				playertable.style.display = 'none';
				swfobject.removeSWF('player');
	};
}

