//javascript

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function isDigit (c)
{
     return ((c >= "0") && (c <= "9"))
}

function msg_cd(m) {
  	
	var subMenu = document.getElementById('msg_cd');
  subMenu.innerHTML = m;
}

function msg_book(m) {
  	
	var subMenu = document.getElementById('msg_book');
  subMenu.innerHTML = m;
}

function msg_postage(m) {
  	
	 document.frmPayment.elements['shipping_cost'].value = m;
}


function reset_qty() {
	var e=0;
  for (e=0; e<=6; e++)
    {
    	n = 'q' + e;
    	document.frmPayment.elements[n].value = ''; 	
    }
    document.frmPayment.elements['total'].value = '';
    document.frmPayment.elements['tqty'].value = '';
    document.frmPayment.elements['shipping_cost'].value = '';
    
    msg_book('');
    msg_cd('');
    return true;
}



function PR(val){
  m = Math.round(val / .05) * .05 ;
  return m.toFixed(2);
} 


function quantity() {
	var cdarr = new Array(2);
	var bkarr = new Array(2);
	
	// postage
	var postage_cost_book = 0.00;
	var postage_cost_cd = 0.00;
	var total_shipping_cost = 0.00;
	var t = 0;
	var q = 0;
	
	cdarr = get_cd_cost();
	bkarr = get_book_cost();

	//alert(cdarr[0]);
	//alert(cdarr[1]);
	//alert(bkarr[0]);
	//alert(bkarr[1]);
	
	// check if shipping direct download or by post
	if (getCheckedValue(document.frmPayment.order_method) == 'transit') { // download
		postage_cost_book = parseFloat(get_postage_book(bkarr[1]));
		postage_cost_cd = parseFloat(get_postage_cd(cdarr[1]));
		total_shipping_cost = postage_cost_book + postage_cost_cd;
		document.frmPayment.shipping_cost.value = total_shipping_cost.toFixed(2);
	}	else {
		document.frmPayment.shipping_cost.value = '';
	}
	
	t = parseFloat(cdarr[0]) + parseFloat(bkarr[0]);
	q = parseInt(cdarr[1]) + parseInt(bkarr[1]);
	
	if (document.frmPayment.prom_code.value == 'LP2008') {
	    //alert('discounted');
	    tot = parseFloat(t.toFixed(2) - ((0.10) * t.toFixed(2))) + parseFloat(total_shipping_cost.toFixed(2));
	    document.frmPayment.total.value = tot.toFixed(2);
	} else {
	    //alert('not discounted');
	    tot = parseFloat(t.toFixed(2)) + parseFloat(total_shipping_cost);
	    document.frmPayment.total.value = tot.toFixed(2); 
	}
	document.frmPayment.tqty.value = q;
}




function get_cd_cost() {
	var cdarray = new Array(2);
	cdarray[0] = 0;
	cdarray[1] = 0;
	
  var msg = '';
  var debug = 0;
  var x = 0;
  var price_direct = 29.95;
  var price_transit = 45.00;

  if (debug==1)
    {
    for(i=0; i<document.frmPayment.elements.length; i++)
      {
      msg += "The field name is: " + document.frmPayment.elements[i].name + " and it’s value is: " + document.frmPayment.elements[i].value + ".\n";
      }
    alert(msg);
    }
    
	/*
  if (!is_product_selected())
  {
    alert('Please select at least one product.');
    reset_qty()
    return cdarray;;
  }  
	*/

	var t = 0;
	var qty = 0;
	var e=0;
	// work out the total q
  for (e=0; e<=6; e++)
    {
    	var n;
    	n = 'q' + e;
    	t = document.frmPayment.elements[n].value;
    	if (isDigit(t)) {
      	qty = parseInt(qty) + parseInt(t);
 			}
 			   		
    }
	
  /* alert('qty: '+qty);
  2 cd's or less bought */
  if (qty<=2 && qty > 0)
    {
    	if (getCheckedValue(document.frmPayment.order_method) == 'direct')
      {
      	total_cost = parseFloat(price_direct * qty);
      	cdarray[0] = PR(total_cost); 
      } else {
     		total_cost = parseFloat(price_transit * qty);
      	cdarray[0] = PR(total_cost);  
      }
      cdarray[1] = qty;
      msg_cd(qty + ' x Individual CD');
    	return cdarray;
    }

  /* 7 cd's bought */
  if (qty>=7)
    {
    if (getCheckedValue(document.frmPayment.order_method) == 'direct')
      {
      	p = price_direct * qty;
      	total_cost = parseFloat(p - (p * (20/100)));
      	cdarray[0] = PR(total_cost);  
      } else {
      	p = price_transit * qty;
      	total_cost = parseFloat(p - (p * (20/100)));
      	cdarray[0] = PR(total_cost); 
      }
    cdarray[1] = qty;
    msg_cd('Platinum Performance Package');
    return cdarray;
    
    }


  /* 3 cd's or more bought */
  if (qty>=3)
  {
	    if (getCheckedValue(document.frmPayment.order_method) == 'direct')
	    {
	      p = price_direct * qty;
	      total_cost = parseFloat(p - (p * (10/100)));
	      cdarray[0] = PR(total_cost); 
	    } else {
	      p = price_transit * qty;
	      total_cost = parseFloat(p - (p * (10/100)));
	      cdarray[0] = PR(total_cost); 
	    }
	    cdarray[1] = qty;
	    msg_cd('Gold Performance Package');
	    return cdarray;
	}
	return cdarray;
}
  


function is_product_selected() {
	for (e=0; e<=6; e++)
  {
    	var z=0;
    	var n;
    	n = 'q' + e;
    	t = document.frmPayment.elements[n].value;
    	if (t>0) {
    		return true;		
    	}
	}
	// check if the book qty is >0
	if (document.frmPayment.elements['q7'].value>0) {
		return true;
	}
	
	return false;
}
  

function get_book_cost() {
		var book_array = new Array(2);
		book_array[0] = 0;
		book_array[1] = 0.00;
		var price_direct_book = 14.95;
		var price_transit_book = 24.95;

		/* code to add book as well */
		
		/* get current cd total */

		var book_qty = parseInt(document.frmPayment.elements['q7'].value);
	 		
	 	if (book_qty > 0) {
			if (getCheckedValue(document.frmPayment.order_method) == 'direct') {
				book_total = book_qty * price_direct_book;
				book_array[0] = book_total.toFixed(2);
			} else {
				book_total = book_qty * price_transit_book;
				book_array[0] =  book_total.toFixed(2);
			}
			book_array[1] = book_qty;
			msg_book(book_qty + ' x Book Purchase');	
		} else {
			msg_book('');	
		}
		
		return book_array;
	
}


function get_postage_cd(q) {
	var cost = 0.00;
	// 1 -2 CD's  = $3AUD
	var cost_cd_au_1_2 = 3.00;
	
	// 3 -5 CD's = $5AUD
	var cost_cd_au_3_5 = 5.00;
	
	// 5+ CD's = $7AUD
	var cost_cd_au_6 = 7.00;
	
	// international pricing	
	var cost_cd_int = 12.00;
	
	var inoz = document.frmPayment.country.options[document.frmPayment.country.selectedIndex].value;
	if (inoz == "Australia") {
		// cost in oz
		if (q > 0 && q <= 2) {
			cost = cost_cd_au_1_2
			return parseFloat(cost);
		}
	
		if (q >= 3 && q <= 5) {
			cost = cost_cd_au_3_5
			return parseFloat(cost);
		}
		
		if (q >= 6) {
			cost = cost_cd_au_6
			return parseFloat(cost);
		}
		
	} else {
		
		if (q > 0) {
		// cost international
			cost = cost_cd_int;
			return parseFloat(cost);
		}
	}
	return cost;
}



function get_postage_book(q) {
	var cost = 0;
	var cost_per_book_au = 3.00;
	
	// international pricing	
	var cost_per_book_int = 5.00;
	
	var inoz = document.frmPayment.country.options[document.frmPayment.country.selectedIndex].value;
	if (inoz == "Australia") {

		// cost in oz
		if (q > 0) {
			cost = 	parseFloat(cost_per_book_au) * parseInt(q);
		}
	} else {
		
		// cost international
		if (q > 0) {
			cost = 	parseFloat(cost_per_book_int) * parseInt(q);
		}
	}
	return parseFloat(cost);
}

function other_state() {
	state = document.frmPayment.state.options[document.frmPayment.state.selectedIndex].value;
		
		if (state == "other") {
				document.getElementById('state_other').style.visibility = 'visible'; 
				document.frmPayment.state_other.focus(); 
		} else {
				document.getElementById('state_other').style.visibility = 'hidden'; 
		}
	
}