// # is replaced dynamically to grab the appropriate number image
var rebateSectionNumberImgPath = "/opencms/opencms/system/modules/com.covad/resources/image/#.gif";

// ************* helper functions ************ //
function updateDiv(divName, theHTML){
	document.getElementById(divName).innerHTML = theHTML;
	return;
}

function showDiv(divName){
	with(document.getElementById(divName).style){
		visibility = "visible";
		display = "block";
	}
	return;
}

function hideDiv(divName){
	with(document.getElementById(divName).style){
		visibility = "hidden";
		display = "none";
	}
	return;
}

function appendHTMLStr(origStrName, strToAppend){
	eval(origStrName + " += \"" + strToAppend + "\"");
}

function clearHTMLStr(origStrName){
	eval(origStrName + " = '';");
}

function okToShowRebate(dateRangeIndex, productIndex, rebateType){
	switch(rebateType){
		case "covad":
			return rebatesShowCovad[dateRangeIndex][productIndex];
			break;
		case "equip":
			return rebatesShowEquip[dateRangeIndex][productIndex];
			break;
	}
}

function beginTable(rebateSectionNumber){
	var theSectionImg = rebateSectionNumberImgPath.replace("#", rebateSectionNumber);
	return "<table cellpadding=0 cellspacing=0 border=0><tr><td width=40 valign=top><img class='sectionNumberImg' src='" + theSectionImg + "'></td><td valign=top>";
}

function endTable(){
	return "</td></tr></table>";
}

function makeLink(hrefValue, onClickValue, linkId, linkText, linkTarget){
	//alert(linkId);
	if(onClickValue != ""){
		onClickValue = onClickValue + "; ";
	}
	
	var strLink = "<a href='" + hrefValue + "' onClick='" + onClickValue + "noteLink(this.id);' id='" + linkId + "'";
	
	if(linkTarget != ""){
		strLink += " target='" + linkTarget + "'";
	}
	
	strLink += ">" + linkText + "</a><br>";
	
	return strLink;
}

function noteLink(linkId){
	var containerElementId, containerElement, childElements, clickedLink, thisLinkId, i;
	
	// first unbold all the links in this linkId's container
	// container element = linkId.length - 1 (cut off the integer at the end)
	containerElementId = linkId.substring(0, linkId.length-1);
	containerElement = document.getElementById(containerElementId);
	
	// now get the obj array of all A elements within the container
	childElements = containerElement.getElementsByTagName("a");
	clickedLink = document.getElementById(linkId);
	
	// loop through container element/child elements and unbold
	for(i=0; i<childElements.length; i++){
		with(childElements[containerElementId + i]){
			innerHTML = innerHTML.replace(selectedLinkAnnotation, "");
			style.fontWeight = "normal";
			style.color = unselectedLinkColor;
		}
	}
	
	// set the clicked link to be bold + raquo 
	with(clickedLink){
		innerHTML = selectedLinkAnnotation + innerHTML;
		style.fontWeight = (selectedLinkIsBold ? "bold" : "normal");
		style.color = selectedLinkColor;
	}
}

// ************* the following are for displaying built content boxes *********** //

function displayProducts(dateRangeIndex){
	// build static content
	buildProducts(dateRangeIndex);
	
	// update divs
	updateDiv("products", productStr);
	
	// show products
	showDiv("products");
	
	// hide all following divs after this one
	hideDiv("noRebates");
	hideDiv("covadRebate");
	hideDiv("manufacturers");
	hideDiv("manufacturerRebate");
}

function displayRebates(dateRangeIndex, productIndex){
	// reset the flag which throws up the "no rebates" copy
	noRebatesAvailable = 0;
	
	var okToShowCovadRebate = okToShowRebate(dateRangeIndex, productIndex, "covad");
	var okToShowEquipRebate = okToShowRebate(dateRangeIndex, productIndex, "equip");
	
	noRebatesAvailable = (!okToShowCovadRebate && !okToShowEquipRebate);
	
	if(noRebatesAvailable){
		// hide now irrelevant divs
		hideDiv("covadRebate");
		hideDiv("manufacturers");
		hideDiv("manufacturerRebate");
		
		// show "no rebates" copy
		updateDiv("noRebates", noRebatesStr);
		showDiv("noRebates");
	}else{
		// hide "no rebates" copy
		hideDiv("noRebates");
		
		if(okToShowCovadRebate){
			// build static content
			buildCovadRebate(dateRangeIndex);
			
			// update divs
			updateDiv("covadRebate", covadRebateStr);
			
			// show covad rebate
			showDiv("covadRebate");
		}else{
			// hide this rebate block
			hideDiv("covadRebate");
		}
		
		if(okToShowEquipRebate){
			// build static content
			buildManufacturers(dateRangeIndex, productIndex);
			
			// update divs
			updateDiv("manufacturers", manufacturerStr);
			
			// show covad rebate
			showDiv("manufacturers");
		}else{
			// hide this rebate block
			hideDiv("manufacturers");
		}
	}
	
	// hide all following divs after this one
	hideDiv("manufacturerRebate");
}

function displayManufRebate(dateRangeIndex, productIndex, manufacturerIndex){
	// build static content
	buildManufRebate(dateRangeIndex, productIndex, manufacturerIndex);
	
	// update divs
	updateDiv("manufacturerRebate", manufacturerRebateStr);
	
	// show equip rebate
	showDiv("manufacturerRebate");
}


// ************* the following are the functions for building the various content boxes *********** //

function buildDateRanges(){
	var i, dateRangeIndex, dateRangeValue;
	var thisHTMLStr = "dateRangeStr";
	var rebateSectionNumber = 1;
	
	clearHTMLStr(thisHTMLStr);
	
	appendHTMLStr(thisHTMLStr, beginTable(rebateSectionNumber));
	
	appendHTMLStr(thisHTMLStr, "<b>When did you order your Covad service?</b>");
	appendHTMLStr(thisHTMLStr, "<br>");
	
	for(i=0; i<rebateDateRanges.length; i++){
		dateRangeIndex = i;
		dateRangeValue = rebateDateRanges[i];

		appendHTMLStr(thisHTMLStr, makeLink("javascript:void(0);", "displayProducts(" + dateRangeIndex + ")", "dateRanges" + i, dateRangeValue, ""));
	}
	
	appendHTMLStr(thisHTMLStr, endTable());
	
	return;
}

function buildProducts(dateRangeIndex){
	// dateRangeIndex is carried over to product selection links
	var i, productIndex, productValue;
	var thisHTMLStr = "productStr";
	var rebateSectionNumber = 2;
	
	clearHTMLStr(thisHTMLStr);
	
	appendHTMLStr(thisHTMLStr, beginTable(rebateSectionNumber));
	
	appendHTMLStr(thisHTMLStr, "<b>What Covad service did you order?</b>");
	appendHTMLStr(thisHTMLStr, "<br>");
	
	for(i=0; i<rebateProducts.length; i++){
		productIndex = i;
		productValue = rebateProducts[i];
		
		appendHTMLStr(thisHTMLStr, makeLink("javascript:void(0);", "displayRebates(" + dateRangeIndex + ", " + productIndex + ")", "products" + i, productValue, ""));
	}
	
	appendHTMLStr(thisHTMLStr, endTable());
	
	return;
}

function buildCovadRebate(dateRangeIndex){
	var covadRebateValue = rebateCovadLinks[dateRangeIndex];
	var thisHTMLStr = "covadRebateStr";
	
	clearHTMLStr(thisHTMLStr);
	
	appendHTMLStr(thisHTMLStr, "<b>Covad Rebates</b>");
	appendHTMLStr(thisHTMLStr, "<br>");	
	appendHTMLStr(thisHTMLStr, makeLink(covadRebateValue, "", "covadRebate0", selectedLinkAnnotation + "Click here to claim your Covad rebate.", "_blank"));
	
	return;
}

function buildManufacturers(dateRangeIndex, productIndex) {
	var i, manufacturerIndex, manufacturerValue;
	var thisHTMLStr = "manufacturerStr";
	var rebateSectionNumber = 3;
	
	clearHTMLStr(thisHTMLStr);
	
	appendHTMLStr(thisHTMLStr, beginTable(rebateSectionNumber));
	
	appendHTMLStr(thisHTMLStr, "<b>Select your equipment manufacturer:</b>");
	appendHTMLStr(thisHTMLStr, "<br>");
	
	for(i=0; i<rebateEquipManufs.length; i++){
		manufacturerIndex = i;
		manufacturerValue = rebateEquipManufs[i];
				
		appendHTMLStr(thisHTMLStr, makeLink("javascript:void(0);", "displayManufRebate(" + dateRangeIndex + ", " + productIndex + ", " + manufacturerIndex + ")", "manufacturers" + i, manufacturerValue, ""));
	}
	
	appendHTMLStr(thisHTMLStr, endTable());
	
	return;
}

function buildManufRebate(dateRangeIndex, productIndex, manufacturerIndex){
	var manufacturerValue, manufRebateValue;
	var thisHTMLStr = "manufacturerRebateStr";
	
	manufacturerValue = rebateEquipManufs[manufacturerIndex];
	manufRebateValue = rebatesManufacturers[dateRangeIndex][productIndex][manufacturerIndex];
	
	clearHTMLStr(thisHTMLStr);
	
	appendHTMLStr(thisHTMLStr, "<b>Manufacturer Rebate</b>");
	appendHTMLStr(thisHTMLStr, "<br>");
	appendHTMLStr(thisHTMLStr, makeLink(manufRebateValue, "", "manufacturerRebate0", selectedLinkAnnotation + "Click here to claim your " + manufacturerValue + " equipment rebate.", "_blank"));
		
	return;
}


// *********** startup function ************ //
function init(){
	// build static content
	buildDateRanges();
	
	// update divs
	updateDiv("dateRanges", dateRangeStr);
	
	// show dates
	showDiv("dateRanges");
	
	// hide other divs
	hideDiv("products");
	hideDiv("covadRebate");
	hideDiv("manufacturers");
	hideDiv("manufacturerRebate");
}