var GA_4_PRICE = 85;
var GA_3_PRICE = 65;
var SR_4_PRICE = 70;
var SR_3_PRICE = 55;

function isCurrentPage(hrefName)
{
    var location = window.location.href;
    var pageName = location.substring(location.lastIndexOf('/')+1);
	
	if(hrefName.lastIndexOf(':') > 0)
	{
		hrefName = hrefName.substring(hrefName.lastIndexOf('/')+1);
	}
    
    return pageName == hrefName ? true : false;
}

function isConcertPage()
{
	/*var concertPages = new Array("poemas_de_amor.html", "a_choral_arts_christmas.html",
							"pierced_to_the_heart.html","mornings_like_this.html"); */
							
	var concertPages = new Array("singet_dem_hern.html", "a_choral_arts_christmas_2009.html",
									"frank_ferkos_stabat_mater.html", "american_hallelujah.html");
	
	for (var i = 0; i < concertPages.length; i++)
	{
		if(isCurrentPage(concertPages[i]))
		{
			return true;
		}
	}
	
	return false;
}

function highlightCurrentPage()
{
    var links = document.getElementById("NavBar").getElementsByTagName("a");
    
    for (var i = 0; i < links.length; i++)
    {
        if (isCurrentPage(links.item(i).attributes.getNamedItem("href").nodeValue))
        {
            if (links.item(i).hasChildNodes())
            {
                doHighlight(links.item(i));                
                return;
            }
        }
    }
	
	if (isConcertPage())
	{
		// highlight a concert page
		doHighlight(links.item(2));
	}
	else
	{
		// highlight home if the above code fails
		doHighlight(links.item(0));
	}
}

function doHighlight(navItem)
{
    var img = navItem.firstChild;
    var src = img.attributes.getNamedItem("src").nodeValue;
    var beforeExtension = src.substring(0, src.lastIndexOf('.'));
    var newSrc = beforeExtension + "_selected" + src.substring(src.lastIndexOf('.'), src.length);                
    img.attributes.getNamedItem("src").nodeValue = newSrc;
}

function toggleConcertDetails(show)
{
    var selection = document.getElementById("concertSelection");

	if(show)
	{
		selection.style.display = "block";
		updateLabels("GAPriceSpan", GA_3_PRICE);
		updateLabels("SeniorPriceSpan", SR_3_PRICE);
	}
	else
	{
		selection.style.display = "none";
		updateLabels("GAPriceSpan", GA_4_PRICE);
		updateLabels("SeniorPriceSpan", SR_4_PRICE);
	}
}

function updateLabels(element, price)
{
	document.getElementById(element).textContent = price;
    document.getElementById(element).innerText = price;
}

function updateTotal()
{
    document.getElementById("TotalSpan").textContent = getTotal();
    document.getElementById("TotalSpan").innerText = getTotal();
}

function getTotal()
{
    var is4 = document.forms[0].SubscriptionType[0].checked;
    var numGA = document.getElementById("GAQuantity").value;
    var numSenior = document.getElementById("SeniorQuantity").value;

    if (is4)
    {
        return (numGA * GA_4_PRICE) + (numSenior * SR_4_PRICE);
    }
    else
    {
        return (numGA * GA_3_PRICE) + (numSenior * SR_3_PRICE);
    }
}

function validateForm()
{    
    if (selectionsValid())
    {
        addPayPalValues();
    }
    else
    {
        alert("Please check your selections and try again.");
        return false;
    }
}

function selectionsValid()
{
    var is3 = document.forms[0].SubscriptionType[1].checked;
    var numGA = document.getElementById("GAQuantity").value;
    var numSenior = document.getElementById("SeniorQuantity").value;

	if ((numGA > 0) || (numSenior > 0))
	{
		if (!is3)
		{
			return true;
		}
		else if (is3 && exactly3Selected())
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function addPayPalValues()
{
	var ppForm = document.getElementById("dynamicInputs");
    var is4 = document.forms[0].SubscriptionType[0].checked;
    var numGA = document.getElementById("GAQuantity").value;
    var numSenior = document.getElementById("SeniorQuantity").value;
	
	ppForm.textContent = "";
	ppForm.innerText = "";
	
	var itemNumber = 1;
	
	if (numGA > 0)
	{
    	ppForm.appendChild(addFormElement("item_name_" + itemNumber, "General Admission Season Subscription"));
    	ppForm.appendChild(addFormElement("quantity_" + itemNumber, numGA));

        if(is4)
        {
            ppForm.appendChild(addFormElement("amount_" + itemNumber, GA_4_PRICE));
        }
        else
        {
            ppForm.appendChild(addFormElement("amount_" + itemNumber, GA_3_PRICE));
            ppForm.appendChild(addFormElement("on0_" + itemNumber, "Performances"));
            ppForm.appendChild(addFormElement("os0_" + itemNumber, getSelectedConcerts()));           
        }
        
        itemNumber++;
	}
	
	if (numSenior > 0)
	{
	    ppForm.appendChild(addFormElement("item_name_" + itemNumber, "Senior Season Subscription"));
    	ppForm.appendChild(addFormElement("quantity_" + itemNumber, numSenior));

        if(is4)
        {
            ppForm.appendChild(addFormElement("amount_" + itemNumber, SR_4_PRICE));
        }
        else
        {
            ppForm.appendChild(addFormElement("amount_" + itemNumber, SR_3_PRICE));
            ppForm.appendChild(addFormElement("on0_" + itemNumber, "Performances"));
            ppForm.appendChild(addFormElement("os0_" + itemNumber, getSelectedConcerts()));                      
        }
	}	
}

function addFormElement(name, value)
{
    var myInput = document.createElement("input");
    myInput.setAttribute("type", "hidden");
    myInput.setAttribute("name", name);
    myInput.setAttribute("value", value);

    return myInput;
}

function getSelectedConcerts()
{
    var concerts = new Array(document.forms[0].Concert1,
                            document.forms[0].Concert2,
                            document.forms[0].Concert3,
                            document.forms[0].Concert4);
    var selectedConcerts = "";
    
    for (var i = 0; i < concerts.length; i++)
    {
        if (concerts[i].checked)
        {
            selectedConcerts += concerts[i].value + ", ";
        }
    }        
    
    return selectedConcerts.substring(0, selectedConcerts.length-2);
}

function exactly3Selected()
{
    var concerts = new Array(document.forms[0].Concert1,
                            document.forms[0].Concert2,
                            document.forms[0].Concert3,
                            document.forms[0].Concert4);
    var numSelected = 0;
    
    for (var i = 0; i < concerts.length; i++)
    {
        if (concerts[i].checked)
        {
            numSelected++;
        }
    }
    
    return numSelected == 3;
}

function checkDonation()
{
	if(document.forms[0].amount.value > 0)
	{
		return true;
	}
	else
	{
		alert("Please check your donation amount. It must be a number greater than 0.");
		return false;
	}
}