// **********************************************************************
// This function preloads the images of the maps
// **********************************************************************
function loadup() {
  Preloader.add('/images/map-africa.png');
  Preloader.add('/images/map-north-america.png');
  Preloader.add('/images/map-south-america.png');
  Preloader.add('/images/map-asia.png');
  Preloader.add('/images/map-australia.png');
  Preloader.add('/images/map-europe.png');

  Preloader.onFinish(function()
  {
    $('map').update('<br/><br/>Maps loaded, please select a region from the links above.');    
  }
  );

  Preloader.load();
}

/************************************************
Show the countries with retailers per continent
*************************************************/
function showmap(xid) {
  var map_img_file = '/images/map-';
  var links_text = '<ul>';

  if (xid =='af') {
    map_img_file = map_img_file + 'africa.png';
  }

  if (xid =='na') map_img_file = map_img_file + 'north-america.png';
  if (xid =='sa') map_img_file = map_img_file + 'south-america.png';
  if (xid =='eu') map_img_file = map_img_file + 'europe.png';
  if (xid =='au') map_img_file = map_img_file + 'australia.png';
  if (xid =='as') map_img_file = map_img_file + 'asia.png';

  new Effect.Fade('map', 
    { afterFinish: function (obj) { 
        var tmp = document.getElementById('map');
        tmp.innerHTML = '<img src="' + map_img_file + '" />';
        new Effect.Appear('map');
    }
  });

  var upd = new Ajax.Updater(
        'mapselector',
        '/en/get-region-links.php?dt=' + new Date(),
        {
          method: 'get',
          parameters: {id: xid}
        });

}

/************************************************
Show the retailers in a selected country
*************************************************/
function show_country_links(country) {
  var upd = new Ajax.Updater(
        'mapselector',
        '/en/get-country-links.php?dt=' + new Date(),
        {
          method: 'get',
          parameters: {id: country}
        });
}


/************************************************
Show the terms and conditions
*************************************************/
function show_tac() {
  var a = window.open('/en/tac.html','TAC','toolbar=0,menubar=0,scrollbars=1,width=400,height=500');
}

/************************************************
Check the buy form
*************************************************/
function checkform() {

  filldetails();

  var errors = 0;
  var errstr = "A validation error occured:\n";

  var tmp = document.getElementById('chkreadtac');
  if (tmp != null && !tmp.checked) {
    errstr += "  - please read and accept the Terms and Conditions\n";
    errors++;
  }

  tmp = document.getElementById('first_name');
  if (tmp != null && tmp.value == "") {
    errstr += "  - please enter a First Name\n";
    errors++;
  }
  tmp = document.getElementById('last_name');
  if (tmp != null && tmp.value == "") {
    errstr += "  - please enter a Last Name\n";
    errors++;
  }
  tmp = document.getElementById('email_addr');
  if (tmp != null && tmp.value == "") {
    errstr += "  - please enter an Email Address\n";
    errors++;
  }
  var reg = new RegExp("[^@]+@[^\.]+\.(.*)");
  if (!reg.test(tmp.value)) {
    errstr += "  - please enter a valid email address\n";
    errors++;
  }
  tmp = document.getElementById('addr_1');
  if (tmp != null && tmp.value == "") {
    errstr += "  - please enter a Postal Address 1\n";
    errors++;
  }
  tmp = document.getElementById('city');
  if (tmp != null && tmp.value == "") {
    errstr += "  - please enter a City\n";
    errors++;
  }
  tmp = document.getElementById('postcode');
  if (tmp != null && tmp.value == "") {
    errstr += "  - please enter a Postal Code\n";
    errors++;
  }
  tmp = document.getElementById('phone');
  if (tmp != null && tmp.value == "") {
    errstr += "  - please enter a Daytime Phone Number\n";
    errors++;
  }
  if (isNaN(tmp.value) || tmp.value[0] == '+' || tmp.value[0] == '0') {
    errstr += "  - please enter a valid Daytime Phone Number (e.g. 27111234567)\n";
    errors++;
  }
  tmp = document.getElementById('birth_date');
  if (tmp != null && tmp.value == "") {
    errstr += "  - please enter a Birth/due Date\n";
    errors++;
  }
  tmp = document.getElementById('country_s');
  xi = tmp.selectedIndex;
  xval = tmp.options[xi];
  xvl2 = xval.value;
  if (xvl2 == "Please Select") {
    errstr += "  - please select a delivery country\n";
    errors++;
  }

  tmp = document.getElementById('province');
  xi = tmp.selectedIndex;
  xval = tmp.options[xi];
  xvl2 = xval.value;
  if (xvl2 == "Please Select") {
    errstr += "  - please select a delivery province\n";
    errors++;
  }

  var tmp_pay_cc = document.getElementById('pay_cc');
  var tmp_pay_eft = document.getElementById('pay_eft');
  var tmp_pay_off = document.getElementById('pay_off');
  if (!tmp_pay_cc.checked && !tmp_pay_eft.checked && !tmp_pay_off.checked) {
    errstr += "  - please select a valid payment type from the list\n";
    errors++;
  }

  var del_type = "";
  var tmp_deliver_dtd = document.getElementById('deliver_dtd');
  if (tmp_deliver_dtd.checked) {
    del_type = 'dtd';
  }
  var tmp_deliver_pobox = document.getElementById('deliver_pobox');
  if (tmp_deliver_pobox.checked) {
    del_type = 'pobox';
  }
  var tmp_deliver_collect = document.getElementById('deliver_collect');
  if (tmp_deliver_collect.checked) {
    del_type = 'collect';
  }
  if (!tmp_deliver_dtd.checked && !tmp_deliver_pobox.checked && !tmp_deliver_collect.checked) {
    errstr += "  - please select a valid delivery method from the list\n";
    errors++;
  }

  var res = false;
  if (errors > 0) {
    alert(errstr);
    res = false;
  }
  else {
    var tmp_d_t = document.getElementById("del_type");
    tmp_d_t.value = del_type;

    var tmp = document.getElementById('pay_cc');
    if (tmp.checked) {
      var tmp2 = document.getElementById("pay_type");
      tmp2.value = "cc";

      tmp2 = document.getElementById("buyform");
      tmp2.submit();
      res = false;
    }

    tmp = document.getElementById('pay_eft');
    if (tmp.checked) {
      var tmp_p_t = document.getElementById("pay_type");
      tmp_p_t.value = 'eft';

      var tmp_bf = document.getElementById("buyform");
      //tmp_bf.action = "paymentInt.php";
      tmp_bf.action = "integrator.php";
      tmp_bf.target = "";
      tmp_bf.submit();
      res = true;
    }

    tmp = document.getElementById('pay_off');
    if (tmp.checked) {
      var tmp2 = document.getElementById("pay_type");
      tmp2.value = "off";

      var tmp_bf = document.getElementById("buyform");
      tmp_bf.action = "integrator.php";
      tmp_bf.target = "";
      tmp_bf.submit();
      res = false;
    }
  }

  return res;
}


/************************************************
Fill in the price details, etc.
*************************************************/
function filldetails() {
  // Get the Item price
  var tmp = document.getElementById('ITEM1_PRICE_S');
  price = tmp.value;

  // Set the item price
  tmp = document.getElementById('ITEM1_PRICE_CALC');
  tmp.value = price;
   
  // Get the item quantity
  tmp = document.getElementById('ITEM1_QUANTITY_S');
  quant = tmp.selectedIndex;
  quant = quant + 1;
   
  // Set the item quantity
  tmp = document.getElementById('ITEM1_QUANTITY_CALC');
  tmp.value = quant;
   
  // Get the delivery cost
  tmp = document.getElementById('ITEM2_PRICE');
  deliv_cost = tmp.value;

  // Set the delivery cost
  tmp = document.getElementById('ITEM2_PRICE_CALC');
  tmp.value = deliv_cost;
   
  tmp = document.getElementById('ITEM2_TOTAL_CALC');
  tmp.value = deliv_cost;
   
  // Set the item1 total
  tmp = document.getElementById('ITEM1_TOTAL_CALC');
  total_item = price * quant;
  tmp.value = total_item + '.00';

  tmp = document.getElementById('DELIVER_AMOUNT_CALC');
  tmp.value = deliv_cost * 1;

  tmp = document.getElementById('TOTAL_AMOUNT_CALC');
  y = deliv_cost * 1;
  total = total_item + y;
  tmp.value =  total +'.00';

  // Set title
  var tmp = document.getElementById('title');
  i = tmp.selectedIndex;
  t = tmp.options[i];
  v = t.value;
  tmp = document.getElementById('purch_title');
  tmp.value = v;

  // Set first name
  tmp = document.getElementById('first_name');
  x = tmp.value;
  tmp = document.getElementById('purch_first_name');
  tmp.value = x;

  // Set first name
  tmp = document.getElementById('last_name');
  x = tmp.value;
  tmp = document.getElementById('purch_last_name');
  tmp.value = x;

  // Set address 1
  tmp = document.getElementById('addr_1');
  x = tmp.value;
  tmp = document.getElementById('deliver_addr_1');
  tmp.value = x;

  // Set address 2
  tmp = document.getElementById('addr_2');
  x = tmp.value;
  tmp = document.getElementById('deliver_addr_2');
  tmp.value = x;

  // Set address 3
  tmp = document.getElementById('addr_3');
  x = tmp.value;
  tmp = document.getElementById('deliver_addr_3');
  tmp.value = x;

  // Set phone
  tmp = document.getElementById('phone');
  x = tmp.value;
  tmp = document.getElementById('purch_phone');
  tmp.value = x;

  // Set email address
  tmp = document.getElementById('email_addr');
  x = tmp.value;
  tmp = document.getElementById('purch_email');
  tmp.value = x;

  // Set postal code
  tmp = document.getElementById('postcode');
  x = tmp.value;
  tmp = document.getElementById('deliver_zip');
  tmp.value = x;

  // Set email address
  tmp = document.getElementById('city');
  x = tmp.value;
  tmp = document.getElementById('deliver_city');
  tmp.value = x;

  // Set country
  tmp = document.getElementById('country_s');
  xi = tmp.selectedIndex;
  xval = tmp.options[xi];
  xvl2 = xval.value;

  tmp = document.getElementById('deliver_country');
  tmp.value = xvl2;

  // Set province
  var tmp = document.getElementById('province');
  i = tmp.selectedIndex;
  t = tmp.options[i];
  v = t.value;


  tmp = document.getElementById('deliver_province');

  if (xvl2 == "South Africa") {
    tmp.value = v;
  }
  else {
    pptmp = document.getElementById('intl_province_t');
    x = pptmp.value;
    tmp.value = x;
  }

  tmp = document.getElementById('deliver_pobox');
  if (tmp.checked) {
    // Set address 1
    tmp = document.getElementById('deliver_addr_1');
    x = tmp.value;
    if (!isNaN(x)) {
      y = 'PO Box ' + x;
      tmp.value = y;
    }
  }
   
  tmp = document.getElementById('deliver_collect');
  if (tmp.checked) {
    // Set address 1
    tmp = document.getElementById('deliver_addr_1');
    x = tmp.value;
    y = x + ' Post Office';
    tmp.value = y;
  }
   
}

/************************************************
Update the cost of the monitors
*************************************************/
function updatecost(ss_unit_price, dtd_price, pobox_price, counter_price) {
  var tmp = document.getElementById('ITEM1_QUANTITY_S');
  var i = tmp.selectedIndex;
  i = i + 1;
  v = i * ss_unit_price;

  tmp = document.getElementById('order_cost_info');
  tmp.innerHTML = '<b>Subtotal: R' + v + '</b>';
  
  tmp = document.getElementById('order_cost_info');
  tmp.innerHTML = '<b>Subtotal: R' + v + '</b>';

  updatetotal(ss_unit_price, dtd_price, pobox_price, counter_price);
}

/************************************************
Update the total order amount
*************************************************/
function updatetotal(ss_unit_price, dtd_price, pobox_price, counter_price) {
  var price = ss_unit_price;

  tmp = document.getElementById('ITEM1_QUANTITY_S');
  var quant = tmp.selectedIndex;
  quant = quant + 1;
  var deli = 0;

  // Get delivery cost
  tmp = document.getElementById('deliver_dtd');
  if (tmp.checked) {
    deli = dtd_price;
  }
  else {
    tmp = document.getElementById('deliver_pobox');
    if (tmp.checked) {
      deli = pobox_price;
    }
    else {
      tmp = document.getElementById('deliver_collect');
      if (tmp.checked) {
        deli = counter_price;
      }
      else {
      }
    }
  }
   
  // alert('v = (' + price + ' * ' + quant + ') + ' + deli);
  v = (price * quant) + deli;
  tmp = document.getElementById('total_cost_info');
  tmp.innerHTML = '<b>Total Cost: R' + v + '</b>';

}

/************************************************
Reset the buy form
*************************************************/
function clearform() {
  var tmp = document.getElementById('pay_cc');
  tmp.checked = false;
  tmp = document.getElementById('pay_eft');
  tmp.checked = false;
  tmp = document.getElementById('pay_off');
  tmp.checked = false;
  tmp = document.getElementById('deliver_dtd');
  tmp.checked = false; 
  tmp = document.getElementById('deliver_collect');
  tmp.checked = false;
  tmp = document.getElementById('deliver_pobox');
  tmp.checked = false;
  tmp = document.getElementById('ITEM1_QUANTITY_S');
  tmp.selectedIndex = 0;
  tmp = document.getElementById('country_s');
  tmp.selectedIndex = 0;
                        
  tmp = document.getElementById('deliver_dtd');
  tmp.checked = false;  
  tmp = document.getElementById('deliver_pobox');
  tmp.checked = false;  
  tmp = document.getElementById('deliver_collect');
  tmp.checked = false;  
                        
  tmp = document.getElementById('pay_cc');
  tmp.checked = false;  
  tmp = document.getElementById('pay_eft');
  tmp.checked = false;
  tmp = document.getElementById('pay_off');
  tmp.checked = false;

  tmp = document.getElementById('addr_1_txt');
  if (tmp != null) {
    tmp.innerHTML = 'Delivery Address 1';
  }
  tmp = document.getElementById('addr_2_txt');
  if (tmp != null) {
    tmp.innerHTML = 'Delivery Address 2';
  }
  tmp = document.getElementById('addr_3_txt');
  if (tmp != null) {
    tmp.innerHTML = 'Delivery Address 3';
  }

  tmp = document.getElementById('addr_1');
  if (tmp != null) {
    tmp.disabled = false;
    tmp.value = '';
  }
  tmp = document.getElementById('addr_2');
  if (tmp != null) {
    tmp.disabled = false;
    tmp.value = '';
  }
  tmp = document.getElementById('addr_3');
  if (tmp != null) {
    tmp.disabled = false;
    tmp.value = '';
  }
  tmp = document.getElementById('city');
  tmp.value = '';
  tmp = document.getElementById('postcode');
  tmp.value = '';
  tmp = document.getElementById('postcode');
  tmp.value = '';
  tmp = document.getElementById('first_name');
  tmp.value = '';
  tmp = document.getElementById('last_name');
  tmp.value = '';
  tmp = document.getElementById('phone');
  tmp.value = '';
  tmp = document.getElementById('email_addr');
  tmp.value = '';

}

/************************************************
Post the buy form
*************************************************/
function postform() {
  filldetails();
  return checkform();
}

/************************************************
Update the delivery cost based on the selection of a delivery type
*************************************************/
function do_deliv_select(i, ss_unit_price, dtd_price, pobox_price, counter_price, intl_price) {
  if (i == 'dtd') {
    var tmp = document.getElementById('deliver_msg');
    if (tmp != null) {
      tmp.innerHTML = ' - Someone should be available at the street address you give from 8AM to 5PM<br/> weekdays to take delivery.';
    }

    tmp = document.getElementById('addr_1_txt');
    if (tmp != null) {
      tmp.innerHTML = 'Street Address 1';
    }
    tmp = document.getElementById('addr_2_txt');
    if (tmp != null) {
      tmp.innerHTML = 'Street Address 2';
    }
    tmp = document.getElementById('addr_3_txt');
    if (tmp != null) {
      tmp.innerHTML = 'Street Address 3';
    }

    tmp = document.getElementById('ITEM2_PRICE');
    if (tmp != null) {
      tmp.value = dtd_price + '.00';
    }
    tmp = document.getElementById('addr_2');
    if (tmp != null) {
      tmp.disabled = false;
    }
    tmp = document.getElementById('addr_3');
    if (tmp != null) {
      tmp.disabled = false;
    }

    tmp = document.getElementById('deliv_cost_info');
    tmp.innerHTML = '<b>Delivery Cost: R' + dtd_price + '</b>';
  }
  else if (i == 'pobox') {
    var tmp = document.getElementById('deliver_msg');
    if (tmp != null) {
      tmp.innerHTML = ' - Please ensure you enter a valid SA Post Office Box address';
    }
    tmp = document.getElementById('addr_1_txt');
    if (tmp != null) {
      tmp.innerHTML = 'PO Box Number';
    }
    tmp = document.getElementById('addr_2_txt');
    if (tmp != null) {
      tmp.innerHTML = '';
    }
    tmp = document.getElementById('addr_3_txt');
    if (tmp != null) {
      tmp.innerHTML = '';
    }
    tmp = document.getElementById('addr_2');
    if (tmp != null) {
      tmp.disabled = true;
    }
    tmp = document.getElementById('addr_3');
    if (tmp != null) {
      tmp.disabled = true;
    }
    tmp = document.getElementById('ITEM2_PRICE');
    if (tmp != null) {
      tmp.value = pobox_price + '.00';
    }
    tmp = document.getElementById('deliv_cost_info');
    tmp.innerHTML = '<b>Delivery Cost: R' + pobox_price + '</b>';
  }
  else if (i == 'counter') {
    var tmp = document.getElementById('deliver_msg');
    if (tmp != null) {
      var str = " - Please ensure you enter a mobile number so we can send you your parcel tracking number<br/>";
      str += " - You will need to collect the parcel from the Post Office counter, using the tracking number <br/>we send you.";
      tmp.innerHTML = str;
    }
    tmp = document.getElementById('addr_1_txt');
    if (tmp != null) {
      tmp.innerHTML = 'Post Office Name';
    }
    tmp = document.getElementById('addr_2_txt');
    if (tmp != null) {
      tmp.innerHTML = '';
    }
    tmp = document.getElementById('addr_3_txt');
    if (tmp != null) {
      tmp.innerHTML = '';
    }
    tmp = document.getElementById('addr_2');
    if (tmp != null) {
      tmp.disabled = true;
    }
    tmp = document.getElementById('addr_3');
    if (tmp != null) {
      tmp.disabled = true;
    }
    tmp = document.getElementById('ITEM2_PRICE');
    if (tmp != null) {
      tmp.value = counter_price + '.00';
    }
    tmp = document.getElementById('deliv_cost_info');
    tmp.innerHTML = '<b>Delivery Cost: R' + counter_price + '</b>';
  }
  else if (i == 'intl') {
    var tmp = document.getElementById('deliver_msg');
    if (tmp != null) {
      tmp.innerHTML = ' - Someone should be available at the street address you give from 8AM to 5PM<br/> weekdays to take delivery.<br/> - Please allow 3 weeks for international delivery.';
    }

    tmp = document.getElementById('addr_1_txt');
    if (tmp != null) {
      tmp.innerHTML = 'Delivery Address 1';
    }
    tmp = document.getElementById('addr_2_txt');
    if (tmp != null) {
      tmp.innerHTML = 'Delivery Address 2';
    }
    tmp = document.getElementById('addr_3_txt');
    if (tmp != null) {
      tmp.innerHTML = 'Delivery Address 3';
    }

    tmp = document.getElementById('ITEM2_PRICE');
    if (tmp != null) {
      tmp.value = intl_price + '.00';
    }
    tmp = document.getElementById('addr_1');
    if (tmp != null) {
      tmp.disabled = false;
    }
    tmp = document.getElementById('addr_2');
    if (tmp != null) {
      tmp.disabled = false;
    }
    tmp = document.getElementById('addr_3');
    if (tmp != null) {
      tmp.disabled = false;
    }

    tmp = document.getElementById('deliv_cost_info');
    tmp.innerHTML = '<b>Delivery Cost: R' + intl_price + '</b>';

  }

  updatetotal(ss_unit_price, dtd_price, pobox_price, counter_price);
}

/************************************************
Update the order based on payment type
*************************************************/
function do_pay_select(i) {
  if (i == 'eft') {
    var tmp = document.getElementById('pay_msg');
    tmp.innerHTML = ' - Please allow 48 hours for delivery.<br/> - Please set up your EFT payment to send confirmation (if possible) to<br/> singlesales@infantrust.com';
  }
  else if (i == 'cc') {
    var tmp = document.getElementById('pay_msg');
    tmp.innerHTML = ' - Please allow 48 hours for delivery.<br/> - We accept MasterCard and Visa. <br /> - Delivery only during working days<br/> - Credit card transactions are processed by <a href="http://www.paygate.co.za/">PayGate</a>.<br /><img src="../images/cust-cards-pg.png" border="0" style="padding-top: 0px;" alt="We accept Mastercard and Visa using the PayGate system" title="We accept Mastercard and Visa using the PayGate system" />';
  }
  else {
    var tmp = document.getElementById('pay_msg');
    tmp.innerHTML = ' - Please allow 48 hours for delivery during working days.';
  }
}

/************************************************
Check the form for the Distributor Payment page
*************************************************/
function checkpayment() {
	var errors = 0;
	var errstr = "A validation error occured:\n";
	
	var tmp = document.getElementById('chkreadtac');
	if (tmp != null && !tmp.checked) {
		errstr += "  - please read and accept the Terms and Conditions\n";
		errors++;
 	}

	tmp = document.getElementById('tcomments');
 	if (tmp != null && tmp.value.length == 0) {
		errstr += "  - Please supply an invoice number\n";
		errors++;
	}

	tmp = document.getElementById('tTOTAL_AMOUNT');
	if (tmp != null && tmp.value.length == 0) {
		errstr += "  - Please supply an amount to pay\n";
		errors++;
	}
	else {
		if (isNaN(tmp.value)) {
			errstr += "  - Please supply a valid amount to pay\n";
			errors++;
		}
	}

	var res = false;
	if (errors > 0) {
		alert(errstr);
		res = false;
	}
	else {
		item1_total = tmp.value;
		tmp = document.getElementById('ITEM1_TOTAL_CALC');
		tmp.value = item1_total;
		tmp = document.getElementById('ITEM1_PRICE_CALC');
		tmp.value = item1_total;
		res = true;
	}
	
	return res;
}

