// JavaScript Document
//product
var error_string;
var total_dvds = 6;
var total_books = 1;

function place_order() {
	// show loader graphic
	$('loader').style.display = 'block';
	// clear error block
	$('error_message').innerHTML = '';
	setTimeout( "place_order_redirect();", 500);
}

function reset_the_form() {
	$('name').value = '';
	$('address').value = '';
	$('postcode').value = '';
	$('state').value = '';
	$('country').value = '';
	$('telephone').value = '';
	$('shipping').value = '';
	$('readTermsAndConditions').checked = false;
	$('num_books').value = '0';
	$('num_dvds').value = '0';
	$('delivery_method').value = 'Australian Post';
	$('shipping').value = '0';
	$('subtotal').value = '0';
	
	for (var i = 1; i <= total_dvds; i++) {
		$('qty_'+i).value = '0';
		$('total_'+i).innerHTML = '$0.00';
	}
	for (var i = 200; i < (200+total_books); i++) {
		$('qty_'+i).value = '0';
		$('total_'+i).innerHTML = '$0.00';
	}

    $('order_total').innerHTML = '$0.00';
	$('freight').innerHTML = '$0.00';
}

function place_order_redirect() {
	// validate fields
	var validate_response = validate_order();
	
	if (validate_response) {
		// hide loader
		$('loader').style.display = 'none';
		// build the url to redirect to.
		var name = $('name').value;
		var address = $('address').value;
		var suburb = $('suburb').value;
		var postcode = $('postcode').value;
		var state = $('state').value;
		var country = $('country').value;
		var full_address = address+', '+suburb+', '+postcode+', '+state+', '+country;
		var telephone = $('telephone').value;
		var freight_cost = $('shipping').value;
		var freight_type; 
		var radios = document.getElementsByTagName('input');
		for (var i = 0; i < radios.length; i++) {
			if (radios[i].type === 'radio' && radios[i].checked && radios[i].name == 'postMode') {
				// get value, set checked flag or do whatever you need to
				freight_type = radios[i].value; 
			}
		}
		var dvd_format; 
		var radios_format = document.getElementsByTagName('input');
		for (var i = 0; i < radios.length; i++) {
			if (radios_format[i].type === 'radio' && radios_format[i].checked && radios_format[i].name == 'dvd_format') {
				// get value, set checked flag or do whatever you need to
				dvd_format = radios_format[i].value; 
			}
		}

		var base_url = 'https://vault.safepay.com.au/cgi-bin/make_payment.pl?vendor_name=fromagent&print_zero_qty=false&';
		var product_order_string = '';
		
		// build the DVD section
		for (var i = 1; i <= total_dvds; i++) {
			// look to see if there are any ordered & add it to the list
			if (parseFloat($('qty_'+i).value) > 0) {
				if (i == 4) {
					product_order_string = product_order_string + '&dvd_'+i+'='+$('qty_'+i).value+'&dvd_'+i+'=28';
				} else if (i == 6) {
					product_order_string = product_order_string + '&the_collection='+$('qty_'+i).value+'&the_collection=48.99';
				} else {
					product_order_string = product_order_string + '&dvd_'+i+'='+$('qty_'+i).value+'&dvd_'+i+'=29.95';
				}
			}			
		}
		
		// build the DVD section
		for (var i = 200; i < (200+total_books); i++) {
			// look to see if there are any ordered & add it to the list
			if (parseFloat($('qty_'+i).value) > 0) {
				product_order_string = product_order_string + '&book='+$('qty_'+i).value+'&book=75';
			}			
		}
		
		var full_url = base_url + 'information_fields=Name&Name='+escape(name);
		full_url = full_url + '&information_fields=Address&Address='+escape(full_address);
		full_url = full_url + '&information_fields=Phone&Phone='+escape(telephone);
		full_url = full_url + product_order_string;
		full_url = full_url + '&information_fields=Dvd_Format&Dvd_Format='+escape(dvd_format);
		full_url = full_url + '&information_fields=Delivery_Method&Delivery_Method='+escape(freight_type)+'&shipping='+escape(freight_cost);
		
		// do the redirect
		window.location = full_url;
	} else {
		$('error_message').innerHTML = error_string;
		// hide loader
		$('loader').style.display = 'none';
	}
}



function validate_order () {
	var name = $('name').value;
	var address = $('address').value;
	var postcode = $('postcode').value;
	var suburb = $('suburb').value;
	var state = $('state').value;
	var country = $('country').value;
	var telephone = $('telephone').value;
	var postMode = $('postMode').value;
	var readTermsAndConditions = $('readTermsAndConditions').checked;
	var num_books = parseFloat($('num_books').value);
	var num_dvds = parseFloat($('num_dvds').value);
	
	// country & shipping check
	var freight_type; 
	var radios = document.getElementsByTagName('input');
	var country_selected;
	for (var i = 0; i < radios.length; i++) {
		if (radios[i].type === 'radio' && radios[i].checked && radios[i].name == 'postMode') {
			// get value, set checked flag or do whatever you need to
			freight_type = radios[i].value; 
		}
	}
	country_selected = $('country').value;
	if (country_selected != 'Australia' && freight_type != 'International Post') {
	  // check to see that international freight is selected, if not, force its selection.
	  for (var i = 0; i < radios.length; i++) {
			radios[i].checked = false;
			if(radios[i].value == 'International Post') {
				radios[i].checked = true;
			}
	  }
	  change_freight();
	}
	
	if (num_books == 0 && num_dvds == 0) {
		error_string = 'Please add products to your order';
		return false;
	}
	if (readTermsAndConditions == false) {
		error_string = 'Please ensure you have read the terms &amp; conditions and agree.';
		return false;
	}
	if (name == '' || address == '' || postcode == '' || state == '' || country == '' || telephone == '' || suburb == '') {
		error_string = 'Please ensure all fields marked with a * are filled in.';
		return false;
	}
	// not required
	return true;
}



function add_product(TID,QTY,PRICE) {
	var current_qty = parseFloat($('qty_'+TID).value);
	var current_subtotal = parseFloat($('subtotal').value);
	
	if (QTY == '-1' && current_qty == 0) {
		var new_quantity = 0;
		$('qty_'+TID).value = parseFloat(new_quantity);
	} else {
		var new_quantity = current_qty + parseFloat(QTY);
		$('qty_'+TID).value = parseFloat(new_quantity);
		
		var total_price = new_quantity * parseFloat(PRICE);
		total_price = total_price.toFixed(2);
		$('total_'+TID).innerHTML = '$'+total_price;
		
		// subtotal update
		var new_sub_total = current_subtotal + (parseFloat(QTY) * parseFloat(PRICE));
		$('subtotal').value = new_sub_total;
		
		// show total
		var shipping = parseFloat($('shipping').value);
		var order_total = new_sub_total + shipping;
		order_total = order_total.toFixed(2);
		$('order_total').innerHTML = '$'+order_total;
	}
	// update qtys based on type
	if (parseFloat(TID) < 200) {
		var current_num_dvds = parseFloat($('num_dvds').value);
		if (current_num_dvds == 0 && QTY == '-1') {
			$('num_dvds').value = 0;
		} else {
			$('num_dvds').value = current_num_dvds + parseFloat(QTY);
		}
	} else {
		var current_num_books = parseFloat($('num_books').value);
		if (current_num_books == 0 && QTY == '-1') {
			$('num_books').value = 0;
		} else {
			$('num_books').value = current_num_books + parseFloat(QTY);
		}
	}
	
	change_freight();
}

//freight
function change_freight() {
	var order_sub_total = parseFloat($('subtotal').value);
	var freight_total = 0;
	var num_books = parseFloat($('num_books').value);
	var num_dvds = parseFloat($('num_dvds').value);
	var freight_type; 
	var radios = document.getElementsByTagName('input');
	for (var i = 0; i < radios.length; i++) {
		if (radios[i].type === 'radio' && radios[i].checked && radios[i].name == 'postMode') {
			// get value, set checked flag or do whatever you need to
			freight_type = radios[i].value; 
		}
	}

	if (freight_type == 'Australia Post') {
		// dvds $5.50 for each block of 4
		// books $9.50 for each book.
		if (num_dvds > 0 && num_books == 0) {
			var number_packages = Math.ceil(num_dvds/4);
			freight_total = number_packages * 5.50;
		} else if (num_books > 0 && num_dvds == 0) {
			freight_total = freight_total + (9.50*num_books);
		} else if (num_dvds > 0 && num_books > 0) {
			var books_dvd_difference = num_dvds - (num_books * 4);
			if (books_dvd_difference <= 0) {
				freight_total = num_books * 9.50;
			} else {
				freight_total = (num_books * 9.50) + (Math.ceil(books_dvd_difference/4) * 5.50);
			}
		} 
	} else if (freight_type == 'Australian Air') {
		if (num_dvds > 0 && num_books == 0) {
			var number_packages = Math.ceil(num_dvds/4);
			freight_total = number_packages * 7.50;
		} else if (num_books > 0 && num_dvds == 0) {
			freight_total = freight_total + (12*num_books);
		} else if (num_dvds > 0 && num_books > 0) {
			var books_dvd_difference = num_dvds - (num_books * 4);
			if (books_dvd_difference <= 0) {
				freight_total = num_books * 12;
			} else {
				freight_total = (num_books * 12) + (Math.ceil(books_dvd_difference/4) * 7.50);
			}
		} 
	} else if (freight_type == 'International Post') {
		if (num_dvds > 0 && num_books == 0) {
			var number_packages = Math.ceil(num_dvds/2);
			freight_total = number_packages * 11;
		} else if (num_books > 0 && num_dvds == 0) {
			freight_total = freight_total + (75*num_books);
		} else if (num_dvds > 0 && num_books > 0) {
			var books_dvd_difference = num_dvds - (num_books * 4);
			if (books_dvd_difference <= 0) {
				freight_total = num_books * 75;
			} else {
				freight_total = (num_books * 75) + (Math.ceil(books_dvd_difference/4) * 11);
			}
		} 
	} else {
		freight_total = 0;
	}
	
	$('freight').innerHTML = '$'+freight_total.toFixed(2);
	$('delivery_method').value = freight_type;
	$('shipping').value = freight_total;
	
	// order total
	var order_total = order_sub_total + freight_total;
	order_total = order_total.toFixed(2);
	$('order_total').innerHTML = '$'+order_total;
}

function isInteger (s) {
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
	 var c = s.charAt(i);

	 if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s) {
  return ((s == null) || (s.length == 0))
}

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