/**
* add commas to a dollar amount
*/
function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

/**
* functions for checking phone number input
*/
function phoneCheck1() {
   var crawl=document.getElementById('artCrawl');
	var letters = crawl.phone1.value.length +1;
	if (letters <= 3) {
		crawl.phone1.focus();
	}
	else {
		crawl.phone2.focus();
	}
}

function phoneCheck2() {
   var crawl=document.getElementById('artCrawl');
	var letters2 = crawl.phone2.value.length +1;
	if (letters2 <= 3) {
		crawl.phone2.focus();
	}
	else {
		crawl.phone3.focus();
	}
}

function phoneCheck3() {
   var crawl=document.getElementById('artCrawl');
	var letters3 = crawl.phone3.value.length +1;
	if (letters3 <= 4) {
		crawl.phone3.focus();
	}
	else {
	   crawl.email.focus();
	}
}


function displayPrice(amount, spanId) {
	var priceSection = document.getElementById(spanId);
   priceComma = addCommas(amount);
 	priceSection.innerText="$"+priceComma;
	priceSection.innerHTML="$"+priceComma;	
}



/**
* populates the total price information
*/
function calculateTotal() {
   crawl = document.getElementById("artCrawl");
   
   // number of art crawl tickets
   artCrawlTickets = Number(crawl.crawl1.value);
   
   // total price of art crawl tickets
   artCrawlPrice = artCrawlTickets * 75;

   // number of art crawl tickets + wine
   artCrawlTickets2 = Number(crawl.crawl2.value);
   
   // total price of art crawl tickets + wine
   artCrawlPrice2 = artCrawlTickets2 * 100;
   
   //donation total
   donationAmount = Number(crawl.donation.value);

   totalAmount = artCrawlPrice + artCrawlPrice2 + donationAmount;
   
   // write the prices to the corresponding sections
   displayPrice(artCrawlPrice, "crawl1Total");
   displayPrice(artCrawlPrice2, "crawl2Total");
   displayPrice(donationAmount, "donationTotal");
   displayPrice(totalAmount,"totalPrice");
}


