function toggleDisplayMode(ID)
{
	var obj = document.getElementById(ID);
	if (obj.style.display == "none")
	{
		obj.style.display = "block";
	}
	else
	{
		obj.style.display = "none";
	}
}

function toggleShowHide(prefix)
{
	var obj = document.getElementById(prefix+'Target');
	var text = document.getElementById(prefix+'Action');
	if (obj.style.display == "none")
	{
		obj.style.display = "block";
		document.getElementById('widebody').style.overflow = "visible";
		text.innerHTML = text.innerHTML.replace(/^Show /g,"Hide ");
	}
	else
	{
		obj.style.display = "none";
		document.getElementById('widebody').style.overflow = "hidden";
		text.innerHTML = text.innerHTML.replace(/^Hide /g,"Show ");
	}
}

function openThumbnailPopup(imagePath,description)
{
	var newWindow;
	newWindow = window.open("","imageWindow",'height=480,width=480');
	newWindow.document.write("<center><img src=\""+imagePath+"\" /></center>");
	newWindow.document.close()
}

function changeLetterSection(section)
{
	for (var i = 65; i < 91; i++)
	{
		if (i == section)
		{
			document.getElementById('letterBlock'+i).style.display = "block";
		}
		else
		{
			document.getElementById('letterBlock'+i).style.display = "none";
		}
	}
}

function mouseOverSortHeaderImage(section,sort,order)
{
	var imgElement = document.getElementById(section+'ImgSort');
	if ((section == sort && order=='desc') || (section != sort))
	{
		imgElement.src = '/art/page-showchildren-on-up.gif';
	}
	else
	{
		imgElement.src = '/art/page-showchildren-on-down.gif';
	}
}
function mouseOutSortHeaderImage(section,sort,order)
{
	var imgElement = document.getElementById(section+'ImgSort');
	if (section == sort && order=='desc')
	{
		imgElement.src = '/art/page-showchildren-on-down.gif';
	}
	else if (section == sort)
	{
		imgElement.src = '/art/page-showchildren-on-up.gif';
	}
	else
	{
		imgElement.src = '/art/page-showchildren-off.gif';
	}
}

function Init()
{
}

function copyBilling()
{
	document.CartBillingInfo.firstName.value = document.CartBillingInfo.billFirstName.value;
	document.CartBillingInfo.lastName.value = document.CartBillingInfo.billLastName.value;
	document.CartBillingInfo.address.value = document.CartBillingInfo.billAddress.value;
	document.CartBillingInfo.city.value = document.CartBillingInfo.billCity.value;
	document.CartBillingInfo.state.value = document.CartBillingInfo.billState.value;
	document.CartBillingInfo.zip.value = document.CartBillingInfo.billZip.value;
	document.CartBillingInfo.country.selectedIndex = document.CartBillingInfo.billCountryIndex.value;
}

function insertDetailedViewGotickets(id,pars,type)
{
	//type = 0 is inbound
	//type = 1 is local
    var mainTable = $('TermListing');

    var parentRowID = 'reportTableRow-'+id;
    var firstCellInRowID = 'firstCellInRow-'+id
    var tickDownImageID = 'tickDownForRow-'+id;

    document[tickDownImageID].src = '/images/cantip.png';

    var parentRow = $(parentRowID);

    var tempRow = document.createElement('TR');
    tempRow.setAttribute("id","placeholder"+id);

    var tempCell = document.createElement('TD');
    tempCell.setAttribute("colspan","8");
    tempCell.appendChild(document.createTextNode('Loading...'));
    tempRow.appendChild(tempCell);

    //new Insertion.After(parentRow,tempRow.innerHTML);

    parentRow.parentNode.insertBefore(tempRow, parentRow.nextSibling);

	var url = '/xml/index.php';
	//var pars = 'func=' + func + idArg + id;

    try
    {
	    var myAjax = new Ajax.Request(
    		url,
    		{
    			method: 'get',
    			parameters: pars,
    			onComplete: function(originalRequest)
                {
                    var response = originalRequest.responseXML;

                    var searches = $A(response.getElementsByTagName('search'));

                    var newRows = new Array();
					if (searches.length < 1)
					{
						var newRow = Builder.node('tr',{id:'detailRow'+key, title:'detailRow'+id,className:"insightDetailRow"});
						var newCell = document.createElement('TD');
						newCell.setAttribute("colspan","5");
						newCell.appendChild(document.createTextNode('No results found.'));
						newRow.appendChild(newCell);
						newRows[newRows.length] = newRow;
					}
					else
					{
					try
                    {
                        var i = 0;
                        searches.each( function(search)
                        {
                            var key = search.getElementsByTagName('key')[0].firstChild.nodeValue;
							var newRow = Builder.node('tr',{id:'detailRow'+key, title:'detailRow'+id,className:"insightDetailRow"});
                            if(type == 0)
                            {
								var newCheckbox = Builder.node('input',{className:'ctf-checkbox searchGroupCheckbox',type:"checkbox",id:'selectQuery'+key,value:"1"});
                            }
                            var phrase = search.getElementsByTagName('phrase')[0].firstChild.nodeValue;
                            var firstCell = Builder.node('td',{className:"insightFirstCell"});

                            if(type == 0)
                            {
                            	firstCell.appendChild(newCheckbox);
                            	firstCell.appendChild(document.createTextNode(phrase));
                            }
                            if(type == 1)
                            {
                            	var newSearchLink = Builder.node('a',{href:'/search-results.php?q='+phrase,target:"_blank"});
                            	newSearchLink.appendChild(document.createTextNode(phrase));
                            	firstCell.appendChild(newSearchLink);
                            }

                            if (search.getAttribute('custom') == 1)
                            {
								var removeLink = Builder.node('span',{'onclick':"removeSearchFromGroup("+key+","+search.getAttribute('groupID')+");",className:'action'});
								removeLink.appendChild(document.createTextNode('  [Remove from Group]'));
								firstCell.appendChild(removeLink);
							}
							newRow.appendChild(firstCell);

                            var counts = $A(search.getElementsByTagName('count'));
                            var  countCellCount = 0;
                            counts.each( function(count)
                            {
                                if(countCellCount != 6 && countCellCount != 3)
								{
									var newCell = Builder.node('td');
									countCellCount++;
								}
								else
								{
									var newCell = Builder.node('td',{className:'insightTotalCell'});
									if(countCellCount == 6)
									{
										countCellCount = 0;
									}
									else
									{
										countCellCount++;
									}
								}

                                var countValue = count.firstChild.nodeValue;

                                if(type == 0)
                                {
                                	if(count.getAttribute('source') == 'Signups' || count.getAttribute('source') == 'Revenue' || count.getAttribute('source') == 'Score')
                                	{
		                            	newCell.appendChild(document.createTextNode(countValue));
                                	}
                                	else if(count.getAttribute('source') == 'Google')
                                	{
		                            	var outgoingLink = Builder.node('a',{href:'http://www.google.com/search?q='+phrase.replace(/"/g,"%22"),target:"_blank"});
		                            	outgoingLink.appendChild(document.createTextNode(countValue));
		                            	newCell.appendChild(outgoingLink);
                                	}
                                	else if(count.getAttribute('source') == 'MSN')
                                	{
		                            	var outgoingLink = Builder.node('a',{href:'http://search.msn.com/results.aspx?q='+phrase.replace(/"/g,"%22"),target:"_blank"});
		                            	outgoingLink.appendChild(document.createTextNode(countValue));
		                            	newCell.appendChild(outgoingLink);
                                	}
                                	else if(count.getAttribute('source') == 'Yahoo')
                                	{
		                            	var outgoingLink = Builder.node('a',{href:'http://search.yahoo.com/search?p='+phrase.replace(/"/g,"%22"),target:"_blank"});
		                            	outgoingLink.appendChild(document.createTextNode(countValue));
		                            	newCell.appendChild(outgoingLink);
                                	}
                                	else
                                	{
                                		newCell.appendChild(document.createTextNode(countValue));
                                	}
                                }
                                if(type == 1)
                                {
                                	newCell.appendChild(document.createTextNode(countValue));
                                }

                                newRow.appendChild(newCell);
                            });

                            newRows[newRows.length] = newRow;
                            i = i+1;
                		});
	                	var newRow = Builder.node('tr',{id:'detailRow',title:'detailRow'+id,className:"insightDetailRow"});
	                    var newCell = Builder.node('td',{className:"insightFirstCell"});
	                	var newSearchLink = Builder.node('a',{href:'/admin/index.php?func=CoalInsight.inboundSearches&InboundSearchTerm_id='+id});
	                	newSearchLink.appendChild(document.createTextNode("See all phrases"));
	                	newCell.appendChild(newSearchLink);
	                	newRow.appendChild(newCell);
	                	newCell = Builder.node('td');
	                	newCell.appendChild(document.createTextNode("..."));
	                	newRow.appendChild(newCell);
	                	newCell = Builder.node('td');
	                	newCell.appendChild(document.createTextNode("..."));
	                	newRow.appendChild(newCell);
	                	newCell = Builder.node('td');
	                	newCell.appendChild(document.createTextNode("..."));
	                	newRow.appendChild(newCell);
	                	newCell = Builder.node('td',{className:'insightTotalCell'});
	                	newCell.appendChild(document.createTextNode("..."));
	                	newRow.appendChild(newCell);
	                	newCell = Builder.node('td');
	                	newCell.appendChild(document.createTextNode("..."));
	                	newRow.appendChild(newCell);
	                	newCell = Builder.node('td');
	                	newCell.appendChild(document.createTextNode("..."));
	                	newRow.appendChild(newCell);
	                	newCell = Builder.node('td',{className:'insightTotalCell'});
	                	newCell.appendChild(document.createTextNode("..."));
	                	newRow.appendChild(newCell);
	                	newRows[newRows.length] = newRow;
                    }
                    catch(e) { alert(e); }
					}
                    try
                    {
                    var oldRow = $('placeholder'+id);

                    oldRow.parentNode.removeChild(oldRow);

                    newRows = newRows.reverse();

                    newRows.each( function(row){
                                parentRow.parentNode.insertBefore(row, parentRow.nextSibling);
                            });
                    }
                    catch(e) { alert(e); }


                }
    		});
    }
    catch(e) { alert(e); }
}

function unsubscribeAllCheck()
{
	try {
		if (document.getElementById('subscription_emailunsubAllForm').value == '')
		{
			alert('Please specify an email address to unsubscribe');
			return false;
		}
		return window.confirm('Are you sure you want to unsubscribe ' + document.getElementById('subscription_emailunsubAllForm').value + 
		  ' from all your event newsletters? Once you unsubscribe from "all" you can no longer use this email address to receive newsletters from us.');
	} catch (e) {return true;}
}

function checkSubscriptionForm()
{
	try {
		if (document.getElementById('nameSubscriptionForm').value == '' ||
			document.getElementById('subscription_emailSubscriptionForm').value == '' ||
			document.getElementById('zipSubscriptionForm').value == '')
		{
			alert('Please enter your first name, email address, and postal code');
			return false; 
		}
	} catch (e) {}
	return true;
}

function clearNewSubscriptions()
{
	try {
		var theForm = document.getElementById('SubscriptionForm');
		for (i=0;i<theForm.length;i++)
		{
			if( theForm[i].type.toLowerCase() == "checkbox" && theForm[i].checked && theForm[i].name.indexOf("unsub") < 0)
			{
				theForm[i].checked = false;
			}
		}
	} catch (e) {}
}

function toggleSubscriptionColumn(column,list)
{
	var cellDiv = document.getElementById('subscriptionCell'+list);
	var listDiv = document.getElementById('listCell'+list);
	
	if (cellDiv.innerHTML == "") //fetch via AJAX
	{
		cellDiv.initialize=function()
		{
			//cellDiv.hide();
			document.getElementById('subscriptionCell'+list).innerHTML="Loading...";
			//cellDiv.show();
		}
		cellDiv.finalizeRenderable=function(results,script)
		{
			//cellDiv.hide();
			document.getElementById('subscriptionCell'+list).innerHTML=results;
			if(typeof(script) == "string" && script.length > 0)
			{
				if (window.execScript) {
					window.execScript( script );
				} else {
					setTimeout( script , 1 );
				}
			}
			//cellDiv.show();
		}
		
		var ajax = new CMAjax(
			"get",
			"/xml/ajax.php?func=Distro.renderNewsletterLists&parentListID="+list+"&column="+column,
			""
		);
		ajax.data = "func=Distro.renderNewsletterLists&parentListID="+list+"&column="+column;
		ajax.control = cellDiv;
		ajax.execute(true);
	}
	
	var tempColumn = column - 1;
	while( tempColumn < 4 ) //clear out old active ones on current level or deeper
	{
		var tempColumnWrapper = document.getElementById('subscriptionColumn'+tempColumn);
		var tempColumnParts = tempColumnWrapper.getElementsByTagName("div");
		for (i=0;i<tempColumnParts.length;i++)
		{
			var lists = tempColumnParts[i].getElementsByTagName("div");
			for (j=0;j<lists.length;j++)
			{
				lists[j].className = lists[j].className.replace("parentListActive","parentListInactive");
			}
		}
		tempColumn++;
	}
	
	if( subscriptionColumns[column] != list )
	{
		var oldDiv = document.getElementById('subscriptionCell'+subscriptionColumns[column]);
		if( oldDiv )
		{
			oldDiv.style.display = 'none';
		}
		cellDiv.style.display = 'block';
		listDiv.className = listDiv.className.replace("parentListInactive","parentListActive");
		subscriptionColumns[column] = list;
	}
	else //collapse
	{
		cellDiv.style.display = 'none';
		listDiv.className = listDiv.className.replace("parentListActive","parentListInactive");
		subscriptionColumns[column] = -1;
	}
	
	while( column < 6 )
	{
		column++;
		var oldDiv = document.getElementById('subscriptionCell'+subscriptionColumns[column]);
		if( oldDiv )
		{
			oldDiv.style.display = 'none';
		}
		subscriptionColumns[column] = -1;
	}
}

function buy_tickets( element, url, actual )
{
	if( typeof(element.id) == "undefined" )
	{
		document.getElementById(actual).onclick = null;
		return false;
	}
	document.location.href=url;
}


function event_date_filter(params, target) {
 	if ( ! document.getElementById) {
  		return false;
 	}
    var url="/index.php?func=Distro.renderSchedule" + params;
    
  	document.getElementById(target).innerHTML = "Loading...";
 
	
 	if (window.ActiveXObject) {
  		link = new ActiveXObject("Microsoft.XMLHTTP");
 	} else if (window.XMLHttpRequest) {
  		link = new XMLHttpRequest();
 	}

 	if (link == undefined) {
  		return false;
 	}
 	link.onreadystatechange = function() { event_date_filter_response(url, target); }
 	link.open("GET", url, true);
 	link.send(null);
}

function event_date_filter_response(url, target) {
 	if (link.readyState == 4) {
	 	document.getElementById(target).innerHTML = (link.status == 200) ? link.responseText : "link status: " + link.status;
	}
}

function toggleTabBlockHelper(CEElement_id,blocknum,prefix) {
  var wrapperdiv = document.getElementById(prefix+'s'+CEElement_id);
  var matchChildId = prefix+CEElement_id+'-'+blocknum;
  var arChildren = wrapperdiv.getElementsByTagName('div');
  var i;
  for(i=0;i<arChildren.length;i++) {
    var obj = arChildren[i];
    var replaceFind;
    var replaceWith;
    if (obj.id == matchChildId)
    {
      replaceFind = prefix+'Off';
      replaceWith = prefix+'On';
    }
    else
    {
      replaceFind = prefix+'On';
      replaceWith = prefix+'Off';    
    }
    obj.className = obj.className.replace( replaceFind, replaceWith);
  }
}

function toggleTabBlock(CEElement_id,blocknum) {
  toggleTabBlockHelper(CEElement_id,blocknum,'tabBlock');
  toggleTabBlockHelper(CEElement_id,blocknum,'dataBlock');
}