// JavaScript Document
function Level() {
	var sponsorship_level = document.getElementsByName('customer_level');
	var sponsorship_level_number = 0;
	if(sponsorship_level!=null){
		var i;
		for(i=0;i<sponsorship_level.length;i++){
			if(sponsorship_level[i].checked){
				sponsorship_level_number += parseFloat(sponsorship_level[i].value);
			}
		}
	}
	document.getElementById('sponsorship_level_number').value = sponsorship_level_number;
	Calculator('quantity_8',document.getElementById('sponsorship_level_number').value,'sub_total_8');
}

function Calculator(QuantityID,Price,TotalID) {
	var quantity = document.getElementById(QuantityID).value;	
	var re = /^\d+(?=\.{0,1}\d+$|$)/;
	var number_re=/^[\d]+[\.]{0,1}[\d]*$/; 
	
	if (quantity.length <= 0) quantity = 0;
	if (Price <= 0) Price = 0;
	if (!number_re.test(Price)) {
			alert('Invalid Price, please try again.');
			Price = 0;
			document.getElementById('sponsorship_level_number').value = '';
			document.getElementById('sponsorship_level_number').focus();
	}
	
	if (re.test(quantity)) {
		var total = 0;
		var price = parseFloat(Price);
		total = price * quantity;
		if (total == 0) {
			document.getElementById(TotalID).value = '';
		} else {
			document.getElementById(TotalID).value = Float_Number(total,2);
		}
		
		sum_total = 0; 
		for (i=1;i<=10;i++) {
			if(i==10)
				sum_total = sum_total - Number(document.getElementById('sub_total_' + i).value);
				
			else
				sum_total = sum_total + Number(document.getElementById('sub_total_' + i).value);
		}
		
		if (sum_total <= 0) {
			document.getElementById('total').value = '';
			alert('Invalid quantity, please try again.');
		} else {
			document.getElementById('total').value = Float_Number(sum_total,2);
		}
	} else {
		alert('Invalid quantity, please try again.');
		document.getElementById(QuantityID).value = '1';
		document.getElementById(TotalID).value = Float_Number(Price,2);
		document.getElementById(QuantityID).focus();
		sum_total = 0; 
		for (i=1;i<=10;i++) {
			sum_total = sum_total + Number(document.getElementById('sub_total_' + i).value);
		}
		document.getElementById('total').value = Float_Number(sum_total,2);
		return false;
	}
}


function Float_Number(f,s) {  
	var re=/^[\d]+[\.]{0,1}[\d]*$/;  
  	if (!re.test(f)){return   false;}  
  	var t=Math.pow(10,s);  
  	var val=Math.round(f*t);  
	var dort=val-Math.floor(val/t)*t;  
	for (i=s;i>1;i--) {  
		if (dort<Math.pow(10,i-1)) {  
			dort='0'+dort;
		}  
	}  
	return ((s>0)?(Math.floor(val/t)+'.'+dort):(Math.floor(val/t)));    
} 
