var lmCount = 0;
var rmCount = 0;
var leftRef = "";
var centerRef = "";
var rightRef = "";
var contentIndex = 0;
var existsFloatForm = false; //ugly hack (even uglier than the rest of this project)

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;
var border = true;
function genPricing(title, subtitle, price, note, linkName, text, linkUrl, isBestPrice) {
    text = processContent(text);
    document.write("<td style='width:225px;'><table style='width:100%;heigth:100%' cellspacing='0' cellpadding='0'><tr><td style='font-size:1px;'>&nbsp;</td></tr><tr>")

    document.write("<td class='pricingCell" + (firstPricing ? "First" : "") + (isBestPrice ? "Best" : (border ? " leftBorder" : "")) + "'>");
    if (isBestPrice)
    { 
        document.write("<table style='width:100%; height:12px' cellspacing='0' cellpadding='0'><tr>"); 
        document.write("<td class='cornerTopLeft'>&nbsp;</td>"); //left top
        document.write("<td class='cornerTitle'>&nbsp;</td>")
        document.write("<td class='cornerTopRight'>&nbsp;</td>"); //right top
        document.write("</tr><tr><td class='bestPriceCellLeft'></td>");
        document.write("<td class='bestPriceLabel' style=''>Best Value!</td>");
        document.write("<td class='bestPriceCellRight'></td></tr></table>");
        
        document.write("<table style='width:100%;heigth:100%' cellspacing='0' cellpadding='0'><tr><td style='border-right: solid 3px #59acd7; border-left: solid 3px #59acd7;width:100%;heigth:100%'>")
    }
    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>");
    if (isBestPrice) 
    { 
        document.write("</td></tr></table>");
        
        document.write("<table style='width:100%; height:12px' cellspacing='0' cellpadding='0'><tr>"); 
        document.write("<td class='cornerBottomLeft'>&nbsp;</td>"); //left top
        document.write("<td class='cornerBottom'>&nbsp;</td>")
        document.write("<td class='cornerBottomRight'>&nbsp;</td>"); //right top
        document.write("</tr></table>");        
        
    }
    document.write("</td>");
    
    document.write("</tr></table></td>");
    firstPricing = false;
    if (!border) border = true;
    if (isBestPrice)
        border = 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');
	};
}
function changeParent(enable, obj) {
    if (!obj) obj = parent;
    else obj = obj.parent;

    var div = obj.document.getElementById("__HidingParent");
    if (div == null) {
        div = obj.document.createElement("div");
        div.id = "__HidingParent";
        div.style.position = "absolute";
        div.style.top = "0px";
        div.style.left = "0px";
        div.style.height = obj.document.body.scrollHeight;
        div.style.width = "100%";
        div.style.zIndex = "97";
        div.style.filter = "alpha(opacity=30)";
        div.style.background = "#333";
        obj.document.body.appendChild(div);
    }
    // PA, 03-13-2009: An iframe just beneath the div hides all windowed controls (selects and objects)
    // if you have a few minutes try to make it work with the iframe only (without the div) - i couldn't get it to have a gray background
    var ifr = obj.document.getElementById("__HidingParentIframe");
    if (ifr == null) {
        ifr = obj.document.createElement("iframe");
        ifr.id = "__HidingParentIframe";
        ifr.src = "javascript:false";
        ifr.style.position = "absolute";
        ifr.style.top = "0px";
        ifr.style.left = "0px";
        ifr.style.height = obj.document.body.scrollHeight;
        ifr.style.width = "100%";
        ifr.style.zIndex = "96";
        ifr.style.filter = "alpha(opacity=0)";
        obj.document.body.appendChild(ifr);
    }
    ifr.style.visibility = enable ? "visible" : "hidden";
    div.style.visibility = enable ? "visible" : "hidden";
}
function OpenPopup(url) {
    changeParent(true);
    var ifr = document.getElementById('ifr');
    if (!ifr) {
        location.replace(url);    
        return;
    }
    var w = 1000;
    var h = 720;
    var maxW = document.body.clientWidth;
    var maxH = document.body.clientHeight;
    ifr.style.width = w;
    ifr.style.height = h;
    ifr.style.left = (maxW - w) / 2;
    ifr.style.top = (maxH - h) / 2 - 50;
    ifr.src = url + "&mode=popup";
    ifr.style.display = "block";

    var img = document.getElementById("closeIcon");
    if (!img) return;
    img.style.position = "absolute";
    img.style.top = (maxH - h) / 2 - 40;
    img.style.left = (maxW - w) / 2 + w - 30;
    img.style.display = "block";
}

function ClosePopup() {
    var ifr = document.getElementById('ifr');
    if (ifr) {
        ifr.style.display = "none";
        ifr.src = "img/transparent.gif";
    }
    var img = document.getElementById('closeIcon');
    if (img) {
        img.style.display = "none";
    }
    changeParent(false);
}

function SetFloatMenuPosition() {
    try {
        var menu = document.getElementById('FloatMenu');
        var divBanner = document.getElementById('divBanner');
        if (menu) {
            if (divBanner && divBanner.offsetLeft > 0)
                menu.style.left = (divBanner.offsetLeft-10) + 'px';
            menu.style.display = "block";
        }
    }
    catch(e) {}
}

function SetContentSize() {
    var div = document.getElementById("divContent");
    if (!div) return;
    if (existsFloatForm)
        div.style.width = "500px";
}
function OffsetLeft(obj) {
    var eL = 0;
    for (var p = obj; p && p.tagName != 'BODY'; p = p.offsetParent) {
        eL += p.offsetLeft;
    }
    return eL;
}
function OffsetTop(obj) {
    var eL = 0;
    for (var p = obj; p && p.tagName != 'BODY'; p = p.offsetParent) {
        eL += p.offsetTop;
    }
    return eL;
}
