﻿
function formatCurrency(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;
	var s = new String(i);
	
	if(s.indexOf('.') < 0)
		{
		s += '.00';
		}
		
	if(s.indexOf('.') == (s.length - 2))
		{
		s += '0';
		}
	s = minus + s;
	return s;
	}

function countChecks( form )
	{
	var numChecked = 0;
	var i = 1;
		
	while (form['a'+i])
    	{
    	if (form['a'+i].checked == true)
    		{
    		numChecked ++;
    		}
    	i++;
	    }

	return numChecked;
	}

function countCheckBoxes( form )
	{
	var numCheckBoxes = 0;
	var i = 1;
		
	while (form['a'+i])
    	{
   		numCheckBoxes ++;
    	i++;
	    }

	return numCheckBoxes;
	}

/*
Example of discount and time extention

# of Segments purchased      Discount     Length of viewing time            

1 segment							0%				5 days
2 segments							5%				10 days
3 segments							10%				15 days
4 segments							15%				20 days
5 segments							20%				25 days

*/

function calculateAll( form )
	{
//	alert('aaa');
	var max_discount = form['max_discount'].value; 					// receive the maximum discount from the form
	
	var numChecked = countChecks( form );								// count the number of checked check boxes
	var numCheckBoxes = countCheckBoxes( form );					// count the number of check boxes
	var discount = max_discount * numChecked/numCheckBoxes;	// compute the discount
			
	if (numChecked == 1)
		{
		discount = 0;																// discount for more than one purchase
		}
		
	if (numChecked != numCheckBoxes)	
		{
		form.checkall.checked = false;
		}
	else
		{
		form.checkall.checked = true;		
		}
	
	var i = 1;
	var total_price = 0;
	var total_time = 0;

	for (i = 1; i <= numCheckBoxes; i++)
		{
		var a = 0;
		var b = 0;
		var c = 0;
		var t = 0;
							
		if(form['a'+i].checked)
			{			
			a = form['price' + i].value;
			b = a * discount;
			c = a-b;

			total_price = total_price + c;		// sum all the cost
			
	    	c = formatCurrency(c);					// now format 
			form['calcPrice' + i].value = c ;		// and show the calculated cost

	    	b = formatCurrency(b);					// now format
			form['calcSaved' + i].value = b;		// and show the amount saved
						
			t = form['rental_time' + i].value;				// receive the amount of time to be added for volume purchase
			total_time = total_time + t * 1;				// * 1 to keep it real
			form['calcTime' + i].value = total_time;	
			}
		else
			{	
			form['calcPrice' + i].value = form['price' + i].value ;
			form['calcTime' + i].value = form['rental_time' + i].value ;
			}
		}

		// now go through all the checkboxes again and this time show the days rental for each video selected -- else return to default

		for (i = 1; i <= numCheckBoxes; i++)		
			{						
			if(form['a'+i].checked)
				{			
				form['calcTime' + i].value = total_time;
				}
			else
				{
				form['calcTime' + i].value = form['rental_time' + i].value;
				form['calcPrice' + i].value = form['price' + i].value;
				form['calcSaved' + i].value = '0.00';		
				}
			}
 
		total_price = formatCurrency(total_price);
		form['total_price'].value = total_price;
		form['total_time'].value = total_time;
		}

	
//=============================


function checkAll( form )
    {
	var i = 1;
    
    while (form['a'+i])
    	{
	    form['a'+i].checked = form.checkall.checked;
	    i++;
	    }
	    
	 calculateAll( form );
    }

function toggleDiv($div)
	{
	if (document.getElementById($div).style.display == "block")
		{
		document.getElementById($div).style.display = "none";
		}
	else
		{
		document.getElementById($div).style.display = "block";
		}
	}    
