var boxLimit = 20; var roomLimit = 7; var monthLimit = 12; var monthQty = 1; var beginMonth = 1278000000; var studentQty = 1; var roomQty = 1; var boxOptions = new Array(1); for (var i = 1;i < roomLimit;i++){ boxOptions[i] = 0; } function calcCurriculumPrice() { productCalc(monthQty,boxOptions,0); } function clearQtys() { for(var i = 0; i < boxOptions.length; i++){ boxOptions[i] = 0; setElementById('boxOption_'+i,0); } boxOptions[0] = 1; } function sortMonths(total) { monthQty = total; monthQty = monthQty * 1; if(monthQty > monthLimit){ monthQty = monthLimit; setElementById('monthQty',monthLimit); } clearQtys(); setQtyValues(); } function sortStudents(total) { studentQty = total; studentQty = studentQty * 1; roomQty = 1; //reset the room qty clearQtys(); //reset the students in each room setQtyValues(); } function sortRooms(total) { roomQty = total; studentQty = document.getElementById('studentQty').value * 1; roomQty = roomQty * 1; if(studentQty < roomQty){ //make sure we have at least one student in each room studentQty = roomQty; setElementById('studentQty',studentQty); } clearQtys(); setQtyValues(); } function setElementById(el,val) { if(document.getElementById(el)){ document.getElementById(el).value = val; } } function getElementValueById(el) { if(document.getElementById(el)){ return document.getElementById(el).value; } else { return 0; } } function setQtyValues() { var rooms = 0; var i = 0; var fullBoxes = 0; var otherBox = 0; var studentSub = studentQty; if(studentQty > boxLimit){ fullBoxes = Math.floor(studentQty / boxLimit); otherBox = studentQty % boxLimit; rooms = (otherBox != 0)? fullBoxes + 1:fullBoxes; if(roomQty <= rooms){ roomQty = rooms; roomLimit = rooms; messWithTestBox(rooms); setElementById('roomQty',rooms); splitByBoxes(fullBoxes,otherBox,rooms); countStudents(); // calcCurriculumPrice() is included in this function } else { messWithTestBox(roomQty); splitByRooms(); calcCurriculumPrice(); } } else if(roomQty == 1) { messWithTestBox(roomQty); boxOptions[0] = studentQty; setElementById('boxOption_0',studentQty); setElementById('roomQty',roomQty); calcCurriculumPrice(); } else { messWithTestBox(roomQty); splitByRooms(); calcCurriculumPrice(); } //toggleRoomView() } function messWithTestBox(rooms) { var box = document.getElementById("testBox"); var inputs = ""; var i = 0; while(i < rooms){ inputs = inputs + ''; i++; } box.innerHTML = inputs; } function splitByBoxes(boxes,lastBox,rooms) { boxLimit = boxLimit * 1; halfLimit = boxLimit / 2; //handle all the boxes leading up to the last one for(var i = 0; i < boxes;i++){ if(i == (boxes - 1)){ if(lastBox < halfLimit && lastBox != 0){ boxOptions[i] = lastBox + halfLimit; setElementById('boxOption_'+i, lastBox +halfLimit); } else { boxOptions[i] = boxLimit; setElementById('boxOption_'+i,boxLimit); } } else { boxOptions[i] = boxLimit; setElementById('boxOption_'+i,boxLimit); } } // last box if(lastBox != 0){ if(lastBox < halfLimit){ //since we've already added the lastBox value to the previous box, use the halfLimit boxOptions[boxes] = halfLimit; setElementById('boxOption_'+boxes,halfLimit); } else { //if we didn't use the lastBox value yet, use it here boxOptions[boxes] = lastBox; setElementById('boxOption_'+boxes,lastBox); } } else if(boxes != roomLimit) { // if the limit divides into the total evenly then there is no remainder boxOptions[boxes] = 0; setElementById('boxOption_'+boxes,0); } for(i = rooms;i < boxOptions.length;i++){ boxOptions[i] = 0; setElementById('boxOption_'+i,0); } } function splitByRooms() { var boxes = Math.floor(studentQty / roomQty); var leftOver = studentQty % roomQty; for(var i = 0; i < roomQty;i++){ boxOptions[i] = boxes; setElementById('boxOption_'+i,boxes); } if(leftOver != 0){ boxOptions[0] = leftOver + boxes; setElementById('boxOption_0',leftOver + boxes); } for(i = roomQty;i < boxOptions.length;i++){ boxOptions[i] = 0; setElementById('boxOption_'+i,0); } } function compareCounts(boxes,studentQty) { if(boxes != studentQty){ alert("The total number of students must match the quantity entered in the rooms."); return false; } return true; } function countStudents() { studentQty = 0; roomQty = 0; for(var i = 0;i < boxOptions.length; i++){ boxOptions[i] = Number(getElementValueById('boxOption_'+i)); if(boxOptions[i] > boxLimit){ boxOptions[i] = boxLimit; setElementById('boxOption_'+i,boxLimit); toggleQtyWarning(); } studentQty += boxOptions[i]; if(boxOptions[i] > 0){ roomQty += 1; } } setElementById('roomQty',roomQty); setElementById('studentQty',studentQty); calcCurriculumPrice(); } function toggleRoomView() { if(roomQty > 1){ document.getElementById('roomBoxes').style.display = "block"; document.getElementById('roomBoxesAlt').style.display = "none"; } else { document.getElementById('roomBoxes').style.display = "none"; document.getElementById('roomBoxesAlt').style.display = "block"; } } function fadeDown(obId,value) { var ob = document.getElementById(obId); value = value -1; if(value >= 0){ ob.style.opacity = value/10; ob.style.filter = 'alpha(opacity=' + value*10 + ')'; setTimeout('fadeDown("'+obId+'",'+value+')',100); } } function toggleQtyWarning() { var box = document.getElementById('qtyWarning'); if(box.style.opacity == 10){ fadeDown('qtyWarning',10); } else { box.style.display = "block"; box.style.opacity = 10; setTimeout('toggleQtyWarning()',5000); } }