var oXmlHttp

function GetHttpObject(handler) {
  try {
    var oRequester = new XMLHttpRequest();
    oRequester.onload=handler
    oRequester.onerror=handler
    oRequester.onreadystatechange=handler
    return oRequester
  }
  catch (error) {
    try {
      var oRequester = new ActiveXObject("Microsoft.XMLHTTP");
      oRequester.onreadystatechange=handler
      return oRequester
    }
    catch (error) {
      return false;
    }
  }
}

sfHover = function() {
	if(document.getElementById("topmenu")) {
		var sfEls = document.getElementById("topmenu").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function popup(url, name, height, width) {
	window.open(url, name, 'height='+height+',width='+width+',scrollbars=yes');
}

function toggleDisplay(id) {
	style = document.getElementById(id).style;
	(style.display == 'none') ? style.display = '' : style.display = 'none';
}	

/* Savings Calculator Stuff */

STARBOX_2200_PRICE = 999;
STARBOX_5500_PRICE = 1495;
PORT_PRICE = 49;

POLYCOM_PRICE = 215;
GRANDSTREAM_PRICE = 115;
COUNTERPATH_PRICE = 5;
FXS_PRICE = 80;
FXO_PRICE = 120;

LINE_PRICE = 39.95;
STARWATCH_PRICE = 9.95;
DID_PRICE = 4.95;

STARBOX_INSTALLATION = 100;
PORT_INSTALLATION = 20;
POLYCOM_INSTALLATION = 20;
GRANDSTREAM_INSTALLATION = 20;
FXO_INSTALLATION = 20;
FXS_INSTALLATION = 20;

three_year_monthly = 0;
four_year_monthly = 0;
min_monthly_payment = 0;

function loadSavings() {
	calcService();
	calcEquipment();
	calcTotal();
}

function calcService() {

	var content = "";

	if(!isFinite(document.getElementById('phone_lines').value) || (document.getElementById('phone_lines').value <= 0)) {
		content = '<p>Please order at least one phone line.</p>';
	}

	if(!isFinite(document.getElementById('current_phone_bill').value) || (document.getElementById('current_phone_bill').value <= 0)) {
		content += '<p>Please enter the amount of your monthly service bill.</p>';
	}

	if(content == "") {
		var num_lines = document.getElementById('phone_lines').value;
		var phone_numbers = Number(document.getElementById('phone_numbers').value);
		var current_bill = document.getElementById('current_phone_bill').value;
		var s2s_monthly_bill = num_lines * (LINE_PRICE + STARWATCH_PRICE);
		s2s_monthly_bill += (phone_numbers * DID_PRICE);

		var annual_savings = (current_bill - s2s_monthly_bill) * 12;
		var annual_savings_percentage = (1 - (s2s_monthly_bill / current_bill)) * 100;

		var fifty_percent_savings = (current_bill * 12) / 2;

		if(annual_savings_percentage < 50) {
			min_monthly_payment = current_bill / 2;
		} else {
			min_monthly_payment = s2s_monthly_bill;
		}

		if(current_bill < s2s_monthly_bill) {
			content += "<p>Sorry, Star2Star will not be able to reduce your monthly phone bill.</p>";
		} else {
			content += "<p>We estimate your new monthly phone service bill at <span class=\"boldorange\">$"+s2s_monthly_bill.toFixed(2)+"</span>";
			if(annual_savings > 0) {
				content += " which is an annual savings of <span class=\"boldorange\">$"+annual_savings.toFixed(2)+"</span>.</p>";
				if(annual_savings_percentage < 50) {
					content += "<p>This <span class=\"boldorange\">"+annual_savings_percentage.toFixed(0)+"%</span> savings can be increased to";
					content += " <span class=\"boldorange\">50%</span> off, saving you <span class=\"boldorange\">$"+fifty_percent_savings.toFixed(0)+"</span>.";
				} else {
					content += "<p>This is a savings of <span class=\"boldorange\">"+annual_savings_percentage.toFixed(0)+"%</span>.";
				}
			} else {
				content += ".</p>";
				content += "<p>We can cut your current bill in half, saving you <span class=\"boldorange\">$"+fifty_percent_savings.toFixed(0)+"</span>.";
			}
		}
	}

	document.getElementById('monthly_service_div').innerHTML = content;

	calcTotal();	

}

function calcEquipment() {

	var content = "";

	if(!isFinite(document.getElementById('analog_lines').value) || (document.getElementById('analog_lines').value <= 0)) {
		content += '<p>You must purchase at least one analog line.</p>';
	}
	if((!isFinite(document.getElementById('polycom_phones').value) || (document.getElementById('polycom_phones').value <= 0)) && 
	(!isFinite(document.getElementById('grandstream_phones').value) || (document.getElementById('grandstream_phones').value <= 0)) &&
	(!isFinite(document.getElementById('counterpath_phones').value) || (document.getElementById('counterpath_phones').value <= 0))) {
		content += '<p>You must purchase at least one phone.</p>';
	}

	if(content == "") {
		if(document.getElementById('sbox2200').checked) {
			var total = STARBOX_2200_PRICE;
		} else {
			var total = STARBOX_5500_PRICE;
		}

		total += Number(document.getElementById('polycom_phones').value) * (POLYCOM_PRICE + PORT_PRICE);
		total += Number(document.getElementById('grandstream_phones').value) * (GRANDSTREAM_PRICE + PORT_PRICE);
		total += Number(document.getElementById('analog_lines').value) * (FXS_PRICE);
		total += Number(document.getElementById('counterpath_phones').value) * (COUNTERPATH_PRICE + PORT_PRICE);

		var hardware_total = total;
		var maintenance = total * .15;

		if(document.getElementById('equipment_discount_3_years').checked) {
			total -= (hardware_total * .1);
			hardware_total -= (hardware_total * .1);
		}

		total += STARBOX_INSTALLATION;
		total += Number(document.getElementById('polycom_phones').value) * (POLYCOM_INSTALLATION + PORT_INSTALLATION);
		total += Number(document.getElementById('grandstream_phones').value) * (GRANDSTREAM_INSTALLATION + PORT_INSTALLATION);
		total += Number(document.getElementById('analog_lines').value) * (FXS_INSTALLATION);
		total += Number(document.getElementById('counterpath_phones').value) * (PORT_INSTALLATION);

		var items = Number(document.getElementById('polycom_phones').value) + Number(document.getElementById('grandstream_phones').value) + Number(document.getElementById('analog_lines').value) + 1;

		var shipping = items * 5;

		total += shipping;

		var tax = (hardware_total + shipping) * .07;

		total += tax;

		if(document.getElementById('equipment_discount_month_to_month').checked) {
			total += maintenance;
		}

		if(document.getElementById('equipment_discount_2_years').checked) {
			total += (maintenance * 2);
		}

		if(document.getElementById('equipment_discount_3_years').checked) {
			total += (maintenance * 3);
		}

		three_year_monthly = (total * 1.15) / 36;
		four_year_monthly = (total * 1.21) / 48;

		content += "<p>Amounts shown include professional installation.</p>";
		content += "<p><span class=\"boldorange\">36</span> month lease/purchase is <span class=\"boldorange\">$"+three_year_monthly.toFixed(2)+"</span></p>";
		content += "<p><span class=\"boldorange\">48</span> month lease/purchase is <span class=\"boldorange\">$"+four_year_monthly.toFixed(2)+"</span></p>";
	}

	document.getElementById("equipment_div").innerHTML = content;

	calcTotal();	


}

function calcTotal() {

	var content = "";

	if(!isFinite(document.getElementById('phone_lines').value) || (document.getElementById('phone_lines').value <= 0)) {
		content += '<p>Please order at least one phone line.</p>';
	}

	if(!isFinite(document.getElementById('current_phone_bill').value) || (document.getElementById('current_phone_bill').value <= 0)) {
		content += '<p>Please enter the amount of your monthly service bill.</p>';
	}

	if(document.getElementById('analog_lines').value <= 0) {
		content += '<p>You must purchase at least one analog line</p>';
	}

	if((!isFinite(document.getElementById('polycom_phones').value) || (document.getElementById('polycom_phones').value <= 0)) && 
	(!isFinite(document.getElementById('grandstream_phones').value) || (document.getElementById('grandstream_phones').value <= 0)) &&
	(!isFinite(document.getElementById('counterpath_phones').value) || (document.getElementById('counterpath_phones').value <= 0))) {
		content += '<p>You must purchase at least one phone.</p>';
	}

	if(content == "") {

		var customer_bill = Number(document.getElementById('current_phone_bill').value) + Number(document.getElementById('current_maintenance_bill').value);

		var three_year_monthly_total = three_year_monthly + min_monthly_payment;
		var three_year_monthly_savings = customer_bill - three_year_monthly_total;
		var three_year_total_savings = three_year_monthly_savings * 12;

		var four_year_monthly_total = four_year_monthly + min_monthly_payment;
		var four_year_monthly_savings = customer_bill - four_year_monthly_total;
		var four_year_total_savings = four_year_monthly_savings * 12;

		content += "<p>The phone service cost and the <span class=\"boldorange\">36</span> month lease/purchase of the equipment is <span class=\"boldorange\">$";
		content += three_year_monthly_total.toFixed(2)+"</span> per month.<br>";
		content += "This is a <span class=\"boldorange\">$"+three_year_monthly_savings.toFixed(2)+"</span> monthly savings and";
		content += " <span class=\"boldorange\">$"+three_year_total_savings.toFixed(2)+"</span> annual savings over your current costs.</p>";

		content += "<p>The phone service cost and the <span class=\"boldorange\">48</span> month lease/purchase of the equipment is <span class=\"boldorange\">$";
		content += four_year_monthly_total.toFixed(2)+"</span> per month.<br>";
		content += "This is a <span class=\"boldorange\">$"+four_year_monthly_savings.toFixed(2)+"</span> monthly savings and";
		content += " <span class=\"boldorange\">$"+four_year_total_savings.toFixed(2)+"</span> annual savings over your current costs.</p>";

	}

	document.getElementById("total_div").innerHTML = content;

	saveToSession();

}

function saveToSession() {

	var phone_lines = escape(document.getElementById('phone_lines').value);
	var phone_numbers = escape(document.getElementById('phone_numbers').value);
	if(document.getElementById('sbox5500').checked) {
		var starbox_type = "5500";
	} else {
		var starbox_type = "2200";
	}
	var polycom_phones = escape(document.getElementById('polycom_phones').value);
	var grandstream_phones = escape(document.getElementById('grandstream_phones').value);
	var counterpath_phones = escape(document.getElementById('counterpath_phones').value);
	var analog_lines = escape(document.getElementById('analog_lines').value);
	if(document.getElementById('equipment_discount_month_to_month').checked) {
		var equipment_discount = "month_to_month";
	} else if(document.getElementById('equipment_discount_2_years').checked) {
		var equipment_discount = "2_years";
	} else {
		var equipment_discount = "3_years";
	}
	var current_phone_bill = escape(document.getElementById('current_phone_bill').value);
	var current_maintenance_bill = escape(document.getElementById('current_maintenance_bill').value);

  var url="/ajax.php?";

	url += "phone_lines="+phone_lines;
	url += "&phone_numbers="+phone_numbers;
	url += "&starbox_type="+starbox_type;
	url += "&polycom_phones="+polycom_phones;
	url += "&grandstream_phones="+grandstream_phones;
	url += "&counterpath_phones="+counterpath_phones;
	url += "&analog_lines="+analog_lines;
	url += "&equipment_discount="+equipment_discount;
	url += "&current_phone_bill="+current_phone_bill;
	url += "&current_maintenance_bill="+current_maintenance_bill;

  oXmlHttp=GetHttpObject(ajax_handler)
  oXmlHttp.open("GET", url , true)
  oXmlHttp.send(null)

}

function ajax_handler() {
  if (oXmlHttp.readyState==4 || oXmlHttp.readyState=="complete") {
  }
}

function alternateRows(id){
 if(document.getElementsByTagName){
   var table = document.getElementById(id);
   var rows = table.getElementsByTagName("tr");
   for(i = 0; i < rows.length; i++){
     if(i % 2 != 0){
       rows[i].className = "alt";
     }
   }
 }
}

