//previously in ajax2.js
function xmlhttpPost(strURL, strSubmit, strResultFunc) {
   var xmlHttpReq = false;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
                //IE 7
                if (xmlHttpReq.overrideMimeType) {
                	xmlHttpReq.overrideMimeType('text/xml');
                }
        }
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }

	xmlHttpReq.open('POST', strURL, true);
	xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
	if (xmlHttpReq.readyState == 4) {
           strResponse = xmlHttpReq.responseText;
           switch (xmlHttpReq.status) {
                   // Page-not-found error
                   case 404:
                           alert('Error: Not Found. The requested URL ' + 
                                   strURL + ' could not be found.');
                           break;
                   // Display results in a full window for server-side errors
                   case 500:
                           handleErrFullPage(strResponse);
                           break;
                   default:
                           // Call JS alert for custom error or debug messages
                           if (strResponse.indexOf('Error:') > -1 || 
                                   strResponse.indexOf('Debug:') > -1) {
                                   alert(strResponse);
                           }
                           // Call the desired result function
                           else {
                                   eval(strResultFunc + '(strResponse);');
                           }
                           break;
           }
   }
 }	

 xmlHttpReq.send(strSubmit);
}

function handleErrFullPage(strIn) {

        var errorWin;

        // Create new window and display error
        try {
                errorWin = window.open('', 'errorWin');
                errorWin.document.body.innerHTML = strIn;
        }
        // If pop-up gets blocked, inform user
        catch(e) {
                alert('An error occurred, but the error message cannot be' +
                        ' displayed because of your browser\'s pop-up blocker.\n' +
                        'Please allow pop-ups from this Web site.');
        }
}
//end ajax2.js

//previously inline in subscribe.php
var minForDistrict = 450;

function getMinForDistrict() {
	return minForDistrict;
}

function alertNotMin() {
		var tempNumStudent = removeCommas(document.form1.numStudent.value);

		if(tempNumStudent=="")	{
			tempNumStudent="0";
		}

		if(isNaN(tempNumStudent)){
			tempNumStudent="0";
		}

		if (tempNumStudent < getMinForDistrict()) {
			alert("Number of students must be "+minForDistrict+" or greater for District pricing.");
		}
}

function createPOForm() {
	//validate
	var errorstr = '';
	var msgstr = "Sorry, we cannot complete your request.\nPlease provide the following missing or incorrect information:\n\n";
	if(document.form1.access.selectedIndex==0) { //noop
		//if (document.form1.numsStudents.value=='' || document.form1.numsStudents.value=='0' || parseFloat(document.form1.numsTotal.value)==0.0) { errorstr += "- Please enter the total number of 3rd, 4th, 5th, and 6th grade students.\n";}
		errorstr += "- Please select a license option.\n";
	}

	else if(document.form1.access.selectedIndex==1) { //classroom
		if (document.form1.numsClass.value==0) { errorstr += "- Please enter the number of classrooms.\n";}
	}

	else if(document.form1.access.selectedIndex==2 || document.form1.access.selectedIndex==3) { //school or parent resource
		if (document.form1.numsSchool.value==0) { errorstr += "- Please enter the number of school buildings.\n";}
	}

	if (errorstr != '') {
		msgstr = msgstr + errorstr;
		alert(msgstr);
		return false;
	}

	else {
		//ensure product id is set
		calculateTotal();
		if (document.form1.product_id.value=="") {
			alert("An error occurred determining price.  Please contact customer support.");
		}
		else {
			document.form1.submit();
		}
	}
}

function changeDiv() {

	if(document.form1.access.selectedIndex==0) { //noop
		document.getElementById("div1").style.display="none"; //school building
		document.getElementById("div2").style.display="none"; //students
		document.getElementById("div3").style.display="none"; //years
		document.getElementById("div4").style.display="none"; //calculator
		document.getElementById("div5").style.display="none"; //plus
		document.getElementById("div6").style.display="none"; //classrooms
		document.getElementById("div7").style.display="none"; //total
		document.form1.plusBox.checked=true;
		document.form1.plusBox.disabled=true;
		document.getElementById("div8").style.display="none"; //submit
	}


	else if(document.form1.access.selectedIndex==1) { //classroom
		document.getElementById("div1").style.display="none"; //school building
		document.getElementById("div2").style.display="none"; //students
		document.getElementById("div3").style.display="none"; //years
		document.getElementById("div4").style.display="none"; //calculator
		document.getElementById("div5").style.display=""; //plus
		document.getElementById("div6").style.display=""; //classrooms
		document.getElementById("div7").style.display=""; //total
		document.form1.plusBox.disabled=false;
		document.form1.numsStudents.value=1;  //if this is not 1, payment will be $0.00 in signup.php
		document.getElementById("div8").style.display=""; //submit
	}

	else if(document.form1.access.selectedIndex==2) { //school
		document.getElementById("div1").style.display=""; //school building
		document.getElementById("div2").style.display="none"; //students
		document.getElementById("div3").style.display="none"; //years
		document.getElementById("div4").style.display="none"; //calculator
		document.getElementById("div5").style.display=""; //plus
		document.getElementById("div6").style.display="none"; //classrooms
		document.getElementById("div7").style.display=""; //total
		document.form1.plusBox.disabled=false;
		document.form1.numsStudents.value=1;  //if this is not 1, payment will be $0.00 in signup.php
		document.getElementById("div8").style.display=""; //submit
	}

	else if(document.form1.access.selectedIndex==3) { //parent
		document.getElementById("div1").style.display=""; //school building
		document.getElementById("div2").style.display="none"; //students
		document.getElementById("div3").style.display="none"; //years
		document.getElementById("div4").style.display="none"; //calculator
		document.getElementById("div5").style.display="none"; //plus
		document.getElementById("div6").style.display="none"; //classrooms
		document.getElementById("div7").style.display=""; //total
		document.form1.plusBox.disabled=false;
		document.form1.numsStudents.value=1;  //if this is not 1, payment will be $0.00 in signup.php
		document.getElementById("div8").style.display=""; //submit
	}

	else if(document.form1.access.selectedIndex==4) { // family
		document.getElementById("div1").style.display="none"; //school building
		document.getElementById("div2").style.display="none"; //students
		document.getElementById("div3").style.display="none"; //years
		document.getElementById("div4").style.display="none"; //calculator
		document.getElementById("div5").style.display=""; //plus
		document.getElementById("div6").style.display="none"; //classrooms
		document.getElementById("div7").style.display=""; //total
		document.form1.plusBox.checked=true;
		document.form1.plusBox.disabled=true;
		document.form1.numsStudents.value=1;  //if this is not 1, payment will be $0.00 in signup.php
		document.getElementById("div8").style.display=""; //submit
	}

	//displays table for classroom/school/parent/family
	calculateTotal();

}

function resetDiv() {
	//this happens when product type is changed.
	//seperated from changeDiv, as don't want to reset on page load
	//("Back" button was pressed)

	//reset fields
	document.form1.numClass.value=0;
	document.form1.numStudent.value=0;
	document.form1.numSchool.value=0;
	document.form1.numYears.selectedIndex=0;
	document.form1.product_id.value=0;
	document.form1.plusBox.checked=false;

	changeDiv();
}

function calculateTotal() {

	if (document.form1.access.selectedIndex==0) { //district
//now this is a noop
		//fix bad values
		//we don't want to reset the actual field to '0' because we're doing this on KeyUp
//		document.form1.numsStudents.value = removeCommas(document.form1.numStudent.value);
//
//		if(document.form1.numsStudents.value=="")	{
//			document.form1.numsStudents.value="0";
//		}
//
//		if(isNaN(document.form1.numsStudents.value)){
//			document.form1.numsStudents.value="0";
//		}
//
//		if (parseFloat(document.form1.numsStudents.value) < parseInt(getMinForDistrict())) {
//			processDistrict2();
//			return;
//		}
//
//		document.form1.numsYears.value=document.form1.numYears[document.form1.numYears.selectedIndex].text;
//		document.form1.numsStudentsDisplay.value=insertCommas(document.form1.numsStudents.value);;
//
//		if(document.form1.numsYears.value!="0" && document.form1.numsStudents.value!="0") {
//			strSubmit = "yearID="+document.form1.numsYears.value+"&numStudents="+document.form1.numsStudents.value;
//			var strResultFunc = "processDistrict1";
//
//			processAll(strSubmit, strResultFunc);
//		}
//
//		else {
//			processDistrict2();
//		}
	}

	else {

		if (document.form1.access.selectedIndex==1) { //classroom

			//determine product type
			if (document.form1.plusBox.checked==true) { //progress accounts
				document.form1.product_id.value="204";
			}
			else {
				document.form1.product_id.value="203";
			}
		}

		else if (document.form1.access.selectedIndex==2) { //school

			//determine product type
			if (document.form1.plusBox.checked==true) { //progress accounts
				document.form1.product_id.value="206";
			}
			else {
				document.form1.product_id.value="205";
			}
		}

		else if (document.form1.access.selectedIndex==3) { //parent
			document.form1.product_id.value="207";
		}

		else if (document.form1.access.selectedIndex==4) { //family
			document.form1.product_id.value="2";
		}

		strSubmit = "knownProduct="+true+"&productID="+document.form1.product_id.value;
		var strResultFunc = "processNonDistrict";

		processAll(strSubmit, strResultFunc);


	}
}



function processAll(strSubmit, strResultFunc) {
	//alert("function called: "+strSubmit);
	var strURL = "/innercode2.php";
	xmlhttpPost(strURL, strSubmit, strResultFunc);
}



function processDistrict1(strIn) {
	//alert(strIn);
	returnArray =strIn.split(",");
	document.form1.numsRate.value=returnArray[0];
	document.form1.numsRateDisplay.value=formatMoney(returnArray[0]);

	document.form1.numsTotal.value=returnArray[1];
	document.form1.numsTotalDisplay.value=formatMoney(returnArray[1]);

	document.form1.product_id.value=returnArray[2];

	//document.form1.numsDiscount.value=(parseFloat(document.form1.numsRate.value)-parseFloat(document.form1.numsTotal.value)).toFixed(2);
	document.form1.numsDiscount.value=parseFloat(returnArray[0])-parseFloat(returnArray[1]);
	document.form1.numsDiscountDisplay.value=formatMoney((parseFloat(returnArray[0])-parseFloat(returnArray[1])));
}

function processDistrict2(strIn) {
	document.form1.numsRate.value="0";
	document.form1.numsRateDisplay.value=formatMoney("0");

	document.form1.numsDiscount.value="0";
	document.form1.numsDiscountDisplay.value=formatMoney("0");

	document.form1.numsTotal.value="0";
	document.form1.numsTotalDisplay.value=formatMoney("0");

	document.form1.numsStudents.value="0";
	document.form1.numsStudentsDisplay.value="0";
}

function processNonDistrict(strIn) {
	//alert(strIn);
	if (strIn==0) {
		document.form1.product_id.value="";
		alert("An error occurred determining price.  Please contact customer support.");
	}
	else {
		document.getElementById("div7").innerHTML = showTotal(strIn);
	}
}

function showTotal(strIn) {

	document.form1.numsUnitPriceDisplay.value = formatMoney(strIn);

	document.form1.numsClass.value = removeCommas(document.form1.numClass.value);
	document.form1.numsSchool.value = removeCommas(document.form1.numSchool.value);

	//fix bad values
	if(document.form1.numsClass.value=="")	{
		document.form1.numsClass.value="0";
	}

	if(isNaN(document.form1.numsClass.value)){
		document.form1.numsClass.value="0";
	}

	if(document.form1.numsSchool.value=="")	{
		document.form1.numsSchool.value="0";
	}

	if(isNaN(document.form1.numsSchool.value)){
		document.form1.numsSchool.value="0";
	}

	document.form1.numsClassDisplay.value=insertCommas(document.form1.numsClass.value);
	document.form1.numsSchoolDisplay.value=insertCommas(document.form1.numsSchool.value);

	if (document.form1.product_id.value==2) {
		document.form1.numsTotal.value=strIn;
		document.form1.numsTotalDisplay.value=formatMoney(document.form1.numsTotal.value);
		var newHTML = "<table cellpadding='2' cellspacing='1' width='100%' border='1'>";
		newHTML += "<tr><td align='right' width='70%'>Family License:</td><td align='right' bgcolor='#00CCFF' NOWRAP>"+document.form1.numsTotalDisplay.value+"</td></tr>";
		newHTML += "</table>";
		return newHTML;
	}
	else {
		var productString = "";
		var unitString = ""
		var units = ""
		var unitsDisplay = "";

		if (document.form1.product_id.value==203) {
			productString = "Classroom License";
			unitString = "Classrooms";
			units = document.form1.numsClass.value;
			unitsDisplay = document.form1.numsClassDisplay.value
		}
		else if (document.form1.product_id.value==204) {
			productString = "Classroom Plus License";
			unitString = "Classrooms";
			units = document.form1.numsClass.value;
			unitsDisplay = document.form1.numsClassDisplay.value
		}
		else if (document.form1.product_id.value==205) {
			productString = "School License"
			unitString= "Buildings";
			units = document.form1.numsSchool.value;
			unitsDisplay = document.form1.numsSchoolDisplay.value
		}

		else if (document.form1.product_id.value==206) {
			productString = "School Plus License"
			unitString= "Buildings";
			units = document.form1.numsSchool.value;
			unitsDisplay = document.form1.numsSchoolDisplay.value
		}
		else if (document.form1.product_id.value==207) {
			productString = "After-School License"
			unitString= "Buildings";
			units = document.form1.numsSchool.value;
			unitsDisplay = document.form1.numsSchoolDisplay.value
		}

		document.form1.numsTotal.value=strIn*units;
		document.form1.numsTotalDisplay.value=formatMoney(document.form1.numsTotal.value);

		return printCommonTable(productString,
								unitString,
								units,
								document.form1.numsUnitPriceDisplay.value,
								document.form1.numsTotalDisplay.value);

	}

	return "";
}

function printCommonTable(productString, unitString, units, price, total) {

	var newHTML = "<table cellpadding='2' cellspacing='1' width='100%' border='1'>";
	newHTML += "<tr><td align='right' width='70%'>"+productString+":</td><td align='right'>"+price+"</td></tr>";
	newHTML += "<tr><td align='right'>"+unitString+":</td><td align='right'>"+units+"</td></tr>";
	newHTML += "<tr><td align='right'>Total:</td><td align='right' bgcolor='#00CCFF' NOWRAP>"+total+"</td></tr>";
	newHTML += "</table>";
	return newHTML;

}

//reset needs to call changeDiv, after the reset, so handlers won't work
function fullReset() {
	document.form1.access.selectedIndex=0;
	resetDiv();
}

function removeCommas(str) {
    return str.split(",").join("");
}

function insertCommas(str) {
    str += '';
	arr = str.split('.');
	a1 = arr[0];
	a2 = arr.length > 1 ? '.' + arr[1] : '';
	var exp = /(\d+)(\d{3})/;
	while (exp.test(a1)) {
		a1 = a1.replace(exp, '$1' + ',' + '$2');
	}
	return a1 + a2;
}

function formatMoney(num) {
	return "$ "+insertCommas(parseFloat(num).toFixed(2));
}
//end inline in subscribe.php