var theform = document.banditform;

function checkForm(TheForm) {
	if (!isValidEmailAddress(TheForm.email.value)) {
		alert("Please enter your email address before submitting.");
		TheForm.email.focus();
		return false;
	} else {
		return true;
	}
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function banditcalculate() {

	var budgetX = budget[theform.budget.selectedIndex];
	var complexityX = complexity[theform.complexity.selectedIndex];
	var transactionsX = theform.transactions.selectedIndex;


	if (theform.budget.selectedIndex >= 6) {
		document.getElementById('overagedisclaimer').style.display = "block";
	} else {
		document.getElementById('overagedisclaimer').style.display = "none";
	}

	//Startup Display Note
	if (theform.startup_books[1].checked || theform.startup_conversion[0].checked) {
		document.getElementById('startup_contact').style.display = "block";
	} else {
		document.getElementById('startup_contact').style.display = "none";
	}
	
	

	var hourcount = 0;
	if (theform.accountnum.value && isNumeric(theform.accountnum.value)) hourcount += (scope[transactionsX][0] * parseInt(theform.accountnum.value));
	hourcount += scope[transactionsX][1];
	
	//Coordinate Payroll
	hourcount += scope[transactionsX][2] * parseFloat(getSelectedValue(theform.coordinatepayroll));
	
	//Pay Bills
	hourcount += scope[transactionsX][3] * parseFloat(getSelectedValue(theform.paybills));
	
	if (getCheckedValue(theform['ccexpenses']) == 1) hourcount += scope[transactionsX][4];
	//if (theform['cash_checks'].checked) { alert(hourcount); hourcount += scope[transactionsX][5]; alert(hourcount); }
	if (theform['cash_checks'].checked) { hourcount += scope[transactionsX][5]; }
	if (theform['cc_paypal'].checked) hourcount += (scope[transactionsX][6] * 0.6);
	if (theform['cc_terminal'].checked) hourcount += (scope[transactionsX][6] * 0.6);
	if (getCheckedValue(theform['recorddonordetail']) == 1) hourcount += (scope[transactionsX][6] * 0.6);
	if (getCheckedValue(theform['donationreceipts']) == 1) hourcount += scope[transactionsX][7];
	if (getCheckedValue(theform['deposits']) == 1) hourcount += scope[transactionsX][8];

	totalHours = roundNumber(budgetX * complexityX * hourcount,1);
	theform.hours.value = roundNumber(totalHours, 1);
	
	//Find Band With Hours
	var suggestedBand = -1;
	var maxband = band.length - 1;
	var specialBands = 10;
	for (i=0; i<=maxband; i++) {
		minHours = parseFloat(band[i][0]);
		if (i==0) minHours = 0;
		maxHours = parseFloat(band[i][1]);
		totalHours = parseFloat(totalHours);
		if (minHours <= totalHours && maxHours >= totalHours) suggestedBand = i;
		//alert(minHours+"...."+totalHours+"......."+maxHours+"==="+suggestedBand);
	}
	if (suggestedBand < 0) suggestedBand = maxband;
	if (totalHours < 1) suggestedBand = 0;
	var bandBelow = suggestedBand - 1;
	var bandAbove = suggestedBand + 1;
	if (bandAbove > maxband) bandAbove = maxband;
	if (bandBelow < 0) bandBelow = 0;
	theform.suggestedband.value = suggestedBand + 100;
	
	maxHours1 = band[bandBelow][1];
	maxHours2 = band[suggestedBand][1];
	maxHours3 = band[bandAbove][1];
	
	if (bandBelow >= specialBands) maxHours1 = "n/a";
	if (suggestedBand >= specialBands) maxHours2 = "n/a";
	if (bandAbove >= specialBands) maxHours3 = "n/a";
	

	if (suggestedBand == 0) {
		document.getElementById('minimumdisclaimer').style.display = "block";
	} else {
		document.getElementById('minimumdisclaimer').style.display = "none";
	}


	// Write Suggested Bands
	document.getElementById('b0-num').innerHTML = band[bandBelow][3];
	document.getElementById('b1-num').innerHTML = band[suggestedBand][3];
	document.getElementById('b2-num').innerHTML = band[bandAbove][3];

	// Write Band Minimum
	document.getElementById('b0-min').innerHTML = band[bandBelow][0];
	document.getElementById('b1-min').innerHTML = band[suggestedBand][0];
	document.getElementById('b2-min').innerHTML = band[bandAbove][0];

	//Reset to 0 Min for first band
	if (bandBelow == 0) document.getElementById('b0-min').innerHTML = 0;
	if (suggestedBand == 0) document.getElementById('b1-min').innerHTML = 0;


	// Write Band Average
	document.getElementById('b0-avg').innerHTML = average(band[bandBelow][0],maxHours1);
	document.getElementById('b1-avg').innerHTML = average(band[suggestedBand][0],maxHours2);
	document.getElementById('b2-avg').innerHTML = average(band[bandAbove][0],maxHours3);

	// Write Band Maximum
	document.getElementById('b0-max').innerHTML = maxHours1;
	document.getElementById('b1-max').innerHTML = maxHours2;
	document.getElementById('b2-max').innerHTML = maxHours3;

	
	// Write Band Average Cost
	
	var cost1 = 0;
	var cost2 = 0;
	var cost3 = 0;
	
	if (bandBelow >= specialBands) { cost1 = "$"+roundNumber(band[bandBelow][0] * band[bandBelow][2], 0);
	} else { cost1 = "$"+roundNumber(average(band[bandBelow][0],band[bandBelow][1]) * band[bandBelow][2], 0); }

	if (suggestedBand >= specialBands) { cost2 = "$"+roundNumber(band[suggestedBand][0] * band[suggestedBand][2], 0);
	} else { cost2 = "$"+roundNumber(average(band[suggestedBand][0],band[suggestedBand][1]) * band[suggestedBand][2], 0); }

	if (bandAbove >= specialBands) { cost3 = "$"+roundNumber(band[bandAbove][0] * band[bandAbove][2], 0);
	} else { cost3 = "$"+roundNumber(average(band[bandAbove][0],band[bandAbove][1]) * band[bandAbove][2], 0); }
	
	document.getElementById('b0-cost').innerHTML = cost1;
	document.getElementById('b1-cost').innerHTML = cost2;
	document.getElementById('b2-cost').innerHTML = cost3;
	theform.cost1.value = cost1;
	theform.cost2.value = cost2;
	theform.cost3.value = cost3;

	//Set Overage Cost
	document.getElementById('b0-overage').innerHTML = "$" + band[bandBelow][2];
	document.getElementById('b1-overage').innerHTML = "$" + band[suggestedBand][2];
	document.getElementById('b2-overage').innerHTML = "$" + band[bandAbove][2];
}

function setComplexity(val) {
	maxComplexity = 0;
	for (i=1;i<=8;i++) {
		if (theform['r'+i].checked && maxComplexity < parseFloat(theform['r'+i].getAttribute("rel"))) maxComplexity = parseFloat(theform['r'+i].getAttribute("rel"));
	}
	theform.complexity.selectedIndex = maxComplexity;
	banditcalculate();
}


function showbankaccountnum() {
	if (getCheckedValue(theform.bankrec) == 1) {
		document.getElementById('bankaccountnumdiv').style.display = "block";
	} else {
		document.getElementById('bankaccountnumdiv').style.display = "none";
		theform.accountnum.value = "";
	}
}


function getSelectedValue(el) {
	return el.options[el.selectedIndex].value;
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function roundNumber(rnum, rlength) {
	var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}

function isNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) IsNumber = false;
	}
	return IsNumber;
}

function average(num1,num2) {
	if (isNumeric(num1) && isNumeric(num2)) {
		return roundNumber(num1 + ((num2 - num1) / 2), 1);
	} else {
		return "n/a";
	}
}

function selectRadio(el,sel) { el[sel].checked = true; }
function onlyNumbers(pstrSource) { var m_strOut = new String(pstrSource); m_strOut = m_strOut.replace(/[^0-9]/g, ''); return m_strOut; }


showbankaccountnum();
banditcalculate();

