
var PERCENTAGE=0;

/****************************************************************/

function getValue(str, name) {

  //alert('getValue');

  if(str.search(name) > 0)
  {
    startIndex = str.indexOf(name) + name.length+1;
    sub = str.substring(startIndex);

    if(sub.search("&") > 0)
    {
      stopIndex = sub.indexOf("&") + startIndex;
      if(startIndex == stopIndex)
      {
        return "";    
      } 

      return(str.substring(startIndex, stopIndex));
    }
    return(str.substring(startIndex));
  }
  return "";
}

/****************************************************************/

function checkInput(val) {
  if( val > 75) {
    window.alert("This value can't be greater than 75%");
    val = 75;
  }
  return val;
}

/****************************************************************/

function fillAmounts() {
  //alert('fillAmounts');

  var locate = window.location;
  document.stuff.url.value = locate;
  var text = document.stuff.url.value;
  var amount = getValue(text, "totalDollars");
  if ( amount == 0 || amount == "" ) {
    var amount = document.newDollars.myAmount.value;
  }
  var yearly = amount * 1.00;
  document.newDollars.myAmount.value = yearly.toFixed(2);
  var monthly = amount/12;
  document.newDollarsMonthly.textfield32.value = monthly.toFixed(2);

  totalVal.innerHTML = '$' + yearly.toFixed(2);
}

/****************************************************************/

function PrefillAmounts() {
  //alert('fillAmounts');

  var locate = window.location;
  document.stuff.url.value = locate;
  var text = document.stuff.url.value;
  var amount = getValue(text, "totalDollars");
  if ( amount == 0 || amount == "" ) {
    var amount = document.newDollars.myAmount.value;
  }
  var yearly = amount * 1.00;
  document.newDollars.myAmount.value = yearly.toFixed(2);
  var monthly = amount/12;
  document.newDollarsMonthly.textfield32.value = monthly.toFixed(2);
 
}

/****************************************************************/

function checkLogin(myForm) {
  if(myForm.username.value == "") {
    alert('Please enter your email address.');
    return false;
  }
  else if(myForm.password.value == "") {
    alert('Please enter your password.');
    return false;
  }

  return true;
}

/****************************************************************/

function checkChangePassword(myForm) {
  if(myForm.oldpassword.value == "") {
    alert('Please enter your old password.');
    return false;
  }
  else if(myForm.password.value == "") {
    alert('Please enter your new password.');
    return false;
  }
  else if(myForm.newnewpassword.value == "") {
    alert('Please re-type your new password.');
    return false;
  }

  return true;
}

/****************************************************************/

function checkPerc(where) {

  var msg="The following fields are not filled in:\n"; 
  var missingFields = 0;

  var myFields=new Array();
  myFields[0]="First Name ";
  myFields[1]="Last Name";
  myFields[2]="Street Name";
  myFields[3]="City";
  myFields[4]="State";
  myFields[5]="Zip Code";
  myFields[6]="Email Address";

  var hasValue=new Array();
  hasValue[0]=0;
  hasValue[1]=0;
  hasValue[2]=0;
  hasValue[3]=0;
  hasValue[4]=0;
  hasValue[5]=0;
  hasValue[6]=0;
  
  
//if on info page check this
if(where == 1){
	  if(document.donationInfo.firstname.value == "") {
		hasValue[0]=1;
	  }
	  if(document.donationInfo.lastname.value == "") {
		hasValue[1]=1;
	  }
	  if(document.donationInfo.streetname.value == "") {
		hasValue[2]=1;
	  }
	  if(document.donationInfo.city.value == "") {
		hasValue[3]=1;
	  }
	  if(document.donationInfo.state.value == "") {
		hasValue[4]=1;
	  }
	  if(document.donationInfo.zipcode.value == "") {
		hasValue[5]=1;
	  }
	  if(document.donationInfo.email.value == "") {
		hasValue[6]=1;
	  }
	
	  for (var x = 0; x <= 6; x++)
	  {
		if(hasValue[x] == 1)
		{
		  missingFields = 1;
		  msg += myFields[x] + "\n";
		}
	  } 
  
	 if(missingFields == 1) {
		alert(msg);
		return false;
	  }
	  if(document.donationInfo.opinion[1].checked) {
		alert('You must agree to the Terms of Use\nand Conditions of Donations before continuing');
		return false;
	  }
}

  
//if pie chart do this
if(where == 0){
	  if(PERCENTAGE > 100) {
		alert('The total percentage is ' + PERCENTAGE + '%,\n it can\'t be more than 100%.\n Please correct this before submitting.');
		return false;
	  }
	  if(PERCENTAGE < 100){
		alert('The total percentage is ' + PERCENTAGE + '%,\n it can\'t be less than 100%.\n Please correct this before submitting.');
		return false;
	  }
}

  return true;
}

/****************************************************************/

function changePie(myForm) {
  //alert('changePie');

  //PrefillAmounts();

//  var totalDonation = document.newDollars.myAmount.value;
  var totalDonation = 300; //temp
  var countDollars = 0; 
  var countPerc = 0;
  var outreachWarning = 0;
  var nonProfitWarning = 0;
  var moneyAmt;

  pieCanvas.innerHTML = '';

  myForm.wind.value = checkInput(myForm.wind.value);
  var newWind = myForm.wind.value;
  myForm.windVal.value = newWind * totalDonation * .01;
  //moneyAmt = parseFloat(myForm.windVal.value);
  //moneyAmt = moneyAmt.toFixed(2);
  //theWind.innerHTML = '$' + moneyAmt;

  myForm.solar.value = checkInput(myForm.solar.value);
  var newSolar = myForm.solar.value;
  myForm.solarVal.value = newSolar * totalDonation * .01;
  //moneyAmt = parseFloat(myForm.solarVal.value);
  //moneyAmt = moneyAmt.toFixed(2);
  //theSolar.innerHTML = '$' + moneyAmt;

  myForm.geothermal.value = checkInput(myForm.geothermal.value);
  var newGeothermal = myForm.geothermal.value;
  myForm.geothermalVal.value = newGeothermal * totalDonation * .01;
  //moneyAmt = parseFloat(myForm.geothermalVal.value);
  //moneyAmt = moneyAmt.toFixed(2);
  //theCarbonSeq.innerHTML = '$' + moneyAmt;

  myForm.recycling.value = checkInput(myForm.recycling.value);
  var newRecycling = myForm.recycling.value; 
  myForm.recyclingVal.value = newRecycling * totalDonation * .01;
  //moneyAmt = parseFloat(myForm.recyclingVal.value);
  //moneyAmt = moneyAmt.toFixed(2);
  //theBioFuels.innerHTML = '$' + moneyAmt;

  myForm.bioMass.value = checkInput(myForm.bioMass.value);
  var newBioMass = myForm.bioMass.value;
  myForm.bioMassVal.value = newBioMass * totalDonation * .01;
  //moneyAmt = parseFloat(myForm.bioMassVal.value);
  //moneyAmt = moneyAmt.toFixed(2);
  //theResearch.innerHTML = '$' + moneyAmt;

  myForm.emergingTech.value = checkInput(myForm.emergingTech.value);
  var newEmergingTech = myForm.emergingTech.value;
  myForm.emergingTechVal.value = newEmergingTech * totalDonation * .01;
  //moneyAmt = parseFloat(myForm.emergingTechVal.value);
  //moneyAmt = moneyAmt.toFixed(2);
  //theNonProfits.innerHTML = '$' + moneyAmt;

  myForm.nonProfit.value = checkInput(myForm.nonProfit.value);
  if( myForm.nonProfit.value < 10) {
    nonProfitWarning = 1;
    myForm.nonProfit.value = 10;
  }

  var newNonProfit = myForm.nonProfit.value;
  myForm.nonProfitVal.value = newNonProfit * totalDonation * .01;
  //moneyAmt = parseFloat(myForm.nonProfitVal.value);
  //moneyAmt = moneyAmt.toFixed(2);
  //theRadio.innerHTML = '$' + moneyAmt;

  myForm.outreach.value = checkInput(myForm.outreach.value);
  if( myForm.outreach.value < 5) {
    outreachWarning = 1;
    myForm.outreach.value = 5;
  }

  var newOutreach = myForm.outreach.value;
  myForm.outreachVal.value = newOutreach * totalDonation * .01;
  //moneyAmt = parseFloat(myForm.outreachVal.value);
  //moneyAmt = moneyAmt.toFixed(2);
  //thePromotion.innerHTML = '$' + moneyAmt;


var p = new pie();
if(newWind > 0) {
  p.add('Wind',newWind);
}
if(newSolar > 0) {
  p.add('Solar',newSolar);
}
if(newGeothermal > 0) {
  p.add('Geothermal',newGeothermal);
}
if(newRecycling > 0) {
  p.add('Recycling',newRecycling);
}
if(newBioMass > 0) {
  p.add('Bio-Mass',newBioMass);
}
if(newEmergingTech > 0) {
  p.add('Emerging Tech.',newEmergingTech);
}
if(newNonProfit > 0) {
  p.add('Non-Profit(s)',newNonProfit);
}
if(newOutreach > 0) {
  p.add('Outreach',newOutreach);
}
p.render("pieCanvas", "Pie Graph");

if( nonProfitWarning )
  window.alert("Non-Profit(s) can't be less than 10%");

if( outreachWarning )
  window.alert("Public Awareness & Outreach can't be less than 5%");


countPerc = parseInt(newWind);
countPerc += parseInt(newSolar);
countPerc += parseInt(newGeothermal);
countPerc += parseInt(newRecycling);
countPerc += parseInt(newBioMass);
countPerc += parseInt(newEmergingTech);
countPerc += parseInt(newNonProfit);
countPerc += parseInt(newOutreach);
countPerc = countPerc.toFixed(0);
PERCENTAGE = parseInt(countPerc);
totalPercent.innerHTML = countPerc + '%';

//countDollars = parseInt(myForm.windVal.value);
//countDollars += parseInt(myForm.solarVal.value);
//countDollars += parseInt(myForm.geothermalVal.value);
//countDollars += parseInt(myForm.recyclingVal.value);
//countDollars += parseInt(myForm.bioMassVal.value);
//countDollars += parseInt(myForm.emergingTechVal.value);
//countDollars += parseInt(myForm.nonProfitVal.value);
//countDollars += parseInt(myForm.outreachVal.value);
//countDollars = countDollars.toFixed(2);
//totalVal.innerHTML = '$' + countDollars
//totalDonation = totalVal.toFixed(2);
//var totalD = parseInt(totalDonation);
//totalD = totalD.toFixed(2);
//totalVal.innerHTML = '$' + totalD;


}


