var formIsGood = true;
// used to check for better value in Publisher/CPlus
var cpmHost = 50;
var cpmRate = 1.50;
var cpmMargin=50;
var prolite=false;
var formchecked=false;
var kb_ratio_pro=1;
var kb_ratio_prolite=0.5;
var prolite_kb=30;
var pro_kb=300;
var minimum_kb_charge=5;

function checkFormSetup(whichform) {
  for (i=0; i<whichform.elements.length; i++){
    words=whichform.elements[i].name.split("-");
	if (words[0] == "price") {
      if (parseInt(words[1])<1) {
	    formIsGood = false;
      }
	  if ((parseInt(words[2]) != pro_kb) && (parseInt(words[2]) != prolite_kb)) {
	    alert("Invalid form setup: No license found with "+words[2]+" KB. Please contact Savicom webmaster.");
		formIsGood = false;
	  }
	  if (parseInt(words[2]) == prolite_kb) {
	    prolite=true;
	  }
    }
  }
  formchecked=true;
  return(formIsGood);
}

function doPriceCalc(whichform) {
  var rate=0;
  var r=whichform.recipients.value;
  var f=whichform.frequency.value;
  var k=whichform.size.value;
  var overagefee=0;
  var kb_used=0;
  var cpmPrice=0;
  
  // Check data
  if (! formchecked) {
    checkFormSetup(whichform);
  }
  if ((r != parseInt(r)) || (parseInt(r)<1)){
    alert("Please enter a valid number of total recipients on your list");
	whichform.recipients.value='0';
	whichform.recipients.focus();
	return(false);
  } else r=parseInt(r);

  if (f != parseInt(f)) {
    alert("Please enter a valid sending frequency");
	whichform.frequency.value='0';
	whichform.frequency.focus();
	return(false);
  } else f=parseInt(f);
  
  if (k != parseFloat(k)) {
    alert("Please enter the average size of your messages in KB as a decimal number");
	whichform.size.value='0';
	whichform.size.focus();
	return(false);
  } else k=parseFloat(k);

  m = f*r;
  whichform.messages.value=m;
  kbusage=k*m/r;
  whichform.kbusage.value=parseInt(kbusage);
  
  var license=99999999;
  var thislicense=0;
  var rate=99999999;
  var thisrate=0.00;
  var kblimit=0;
  var thiskblimit=0;
  var thisoveragefee=0.00;
  var ratefound = false;
  
  for (i=0; i<whichform.elements.length; i++){
    words=whichform.elements[i].name.split("-");
	if (words[0] == "price") {
	  thislicense=parseInt(words[1]);
      if (thislicense >= r) {
	    // found a license that applies now see how much it will cost
		thisrate=parseFloat(whichform.elements[i].value);
		thiskblimit=parseInt(words[2]);
		if (kbusage > thiskblimit) {
		  if (thiskblimit == pro_kb) {
  		    thisoveragefee=(kbusage-thiskblimit)*thisrate/thiskblimit*kb_ratio_pro;
		  } else if (thiskblimit == prolite_kb) {
		    thisoveragefee=(kbusage-thiskblimit)*thisrate/thiskblimit*kb_ratio_prolite;
          } else {
		    alert("Invalid form setup: No license found with "+thiskblimit+" KB. Please contact Savicom webmaster.");
			return(false);
		  }
		}
	    ratefound = true;
//		alert ("Applicable license found: "+thislicense+" recipients, "+thiskblimit+" KB, $"+thisrate+" base fee, $"+thisoveragefee+" addl KB.");
	    if ((thisrate + thisoveragefee) < (rate + overagefee)) {
		  rate=thisrate;
		  license=thislicense;
		  kblimit=thiskblimit;
		  overagefee=thisoveragefee;
		}
      }
	}
  }
  if (ratefound == false) {
    whichform.licensed_recipients.value="contact us";
    whichform.basefeedisplay.value="contact us";
    whichform.kbfeedisplay.value="-";
    whichform.totalfeedisplay.value="contact us";
  } else {
    whichform.licensed_recipients.value=license;
    whichform.licensed_kb.value=kblimit;
    whichform.basefeedisplay.value=formatAmount(rate);
	if (prolite) {
	  if (kblimit == prolite_kb) {
	    whichform.license_name.value="Pro Lite";
	  } else if (kblimit == pro_kb) {
	    whichform.license_name.value="Pro";
	  }
	}
//    if (kbusage > kblimit) {
//      overagefee=(kbusage-kblimit)*rate/kblimit;
//    }
    whichform.kbfeedisplay.value=formatAmount(overagefee);
    rate+=overagefee;
    whichform.totalfeedisplay.value=formatAmount(rate);
	cpmPrice=cpmHost+cpmRate*m/1000;
	if (cpmPrice+cpmMargin < rate) {
	  alertMsg="For your recipient level and sending rate, one of our CPM-priced\n";
	  alertMsg+="solutions like Publisher or CampaignPlus may be a better value.\n";
	  alertMsg+="Contact Savicom sales for detailed pricing on these products.";
	  alert(alertMsg);
	}
  }
}

function formatAmount(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function recalcMessages(whichform) {
  var r=whichform.recipients.value;
  var f=whichform.frequency.value;
  var k=whichform.size.value;

  // Check data
  if ((r != parseInt(r)) || (parseInt(r)<1)){
    alert("Please enter a valid number of total recipients on your list");
	whichform.recipients.value='0';
	whichform.recipients.focus();
	return(false);
  } else r=parseInt(r);

  if (f != parseInt(f)) {
    alert("Please enter a valid sending frequency");
	whichform.frequency.value='0';
	whichform.frequency.focus();
	return(false);
  } else f=parseInt(f);
  
  if (k != parseFloat(k)) {
    alert("Please enter the average size of your messages in KB as a decimal number");
	whichform.size.value='0';
	whichform.size.focus();
	return(false);
  } else k=parseFloat(k);
  if (f >=1000) {
    alert("Sending frequency per month is the number of times you send messages to your\nentire list over the course of one month.\nDo not enter the total number of messages sent per month.");
	whichform.frequency.value='0';
	whichform.frequency.focus();
	return(false);
  }
  if ((k < minimum_kb_charge) && (k > 0)) {
    alert("The minimum charge per message is "+minimum_kb_charge+" KB.");
	whichform.size.value='5';
	k=5;
  }
  m = f*r;
  whichform.messages.value=m;
  kbusage=k*m/r;
  whichform.kbusage.value=parseInt(kbusage);
}


