function checkForm(theForm) {
 var err = checkElements(theForm);
 if (err != "") {
  alert(err);
  return false;
 }
 return true;
}

function isEmpty(name, value) {
 var err = "";
 if (value.length == 0) {
  err = "The mandatory " + name + " has not been filled in.\n"
 }
 return err;
}

function checkCombo(name, choice) {
 var err = "";
 if (choice == 0) {
  err = "You didn't choose an option from the " + name + " drop-down list.\n";
 }    
 return err;
}    

function checkRadio(name, radio) {
 for (var i = 0; i < radio.length; i++) {
  if (radio[i].checked) {
   return "";
  }
 }
 return "You didn't choose an option from the " + name + " radio group.\n";
}

function checkSearch() {
 if (document.search.lookfor.value.length == 0 && document.search.company.value.length == 0) {
  alert('The mandatory \'looking for\' or \'company name\' field has not been filled in.\n');
  return true;
 }
 if (document.search.postcode.value.length == 0 && document.search.company.value.length == 0) {
  alert('The mandatory \'postcode\' or \'company name\' field has not been filled in.\n');
  return true;
 }
 return false;
}

function setTrade() {
 var index = document.register.tl.selectedIndex;
 if (index < 0) return;
 var val = document.register.tl.options[index].text;
 document.register.trade.value=val;
}

function editTrade() {
 var index = document.trade.tl.selectedIndex;
 if (index < 0) return;
 var val = document.trade.tl.options[index].text;
 document.trade.trade_val.value=val;
}

function editTrade2() {
 var index = document.trade.tr.selectedIndex;
 if (index < 0) return;
 var val = document.trade.tr.options[index].text;
 document.trade.trade_val2.value=val;
}

function add_trade() {
 var index = document.trades.tl.selectedIndex;
 if (index < 0) return;
 add_trade_i(index);
}

function add_trade_id(index) {
 var len = document.trades.tl.length;
 for (k = 0; k < len; k++) {
  if (index == document.trades.tl.options[k].value) add_trade_i(k);
 }
}

function add_trade_i(index) {
 var val = document.trades.tl.options[index].text;
 len = document.trades.tr.length;
 for (i = 0; i < len; i++) {
  if (val == document.trades.tr.options[i].text) return;
  if (val < document.trades.tr.options[i].text) break;
 }
 for (j = len; j > i; j--) {
  document.trades.tr.options[j] = new Option(document.trades.tr.options[j - 1].text, document.trades.tr.options[j - 1].value);
 }
 document.trades.tr.options[i] = new Option(document.trades.tl.options[index].text, document.trades.tl.options[index].value);
}

function del_trade() {
 var index = document.trades.tr.selectedIndex;
 if (index < 0) return;
 document.trades.tr.options[index] = null;
}

function del_all_trade() {
 len = document.trades.tr.length;
 for (i = len - 1; i >= 0; i--) {
  document.trades.tr.options[i] = null;
 }
}

function add_all_trade() {
 len = document.trades.tl.length;
 for (i = 0; i < len; i++) {
  document.trades.tr.options[i] = new Option(document.trades.tl.options[i].text, document.trades.tl.options[i].value);
 }
}

function submit_trades(check) {
 len = document.trades.tr.length;
 if (len < 1) {
  if (check) alert("At least one category should be added");
  return !check;
 }
 document.trades.trades.value = document.trades.tr.options[0].value;
 for (i = 1; i < len; i++) {
  document.trades.trades.value = document.trades.trades.value + ',' + document.trades.tr.options[i].value;
 }
 len = document.trades.tl.length;
 for (i = 0; i < len; i++) {
  document.trades.tl.options[i].selected = false;
 }
 len = document.trades.tr.length;
 for (i = 0; i < len; i++) {
  document.trades.tr.options[i].selected = false;
 }
 return true;
}


function changeLocation(v) {
  document.search.location.value=v;
  if (document.search.lookfor.value.length > 0 && document.search.postcode.value.length > 0) {
	  document.search.submit();
  }
}

function generateUrl(url) {
	return url + '&company=' + document.search.company.value + '&location=' + document.search.location.value + '&postcode=' + document.search.postcode.value;
}

function textLimit(field, countfield, maxlen) {
  if (field.value.length > maxlen)
    field.value = field.value.substring(0, maxlen);
  else
	document.getElementById(countfield).innerHTML = maxlen - field.value.length;
}

function calcBullets() {
  var pb = 0.5 * document.register.bullets.value;
  document.getElementById('bullets_price').innerHTML = pb.toFixed(2);
}

function toggleBullets(isGold) {
  document.register.bullets.selectedIndex = 0;
  calcBullets();
  document.register.bullets.disabled = !isGold;
}

function calcBulletsUpgrade() {
  var pb = 0.5 * document.upgrade.p_new_bullets.value;
  document.getElementById('bullets_price').innerHTML = pb.toFixed(2);
}

function toggleBulletsUpgrade(isGold) {
  document.upgrade.p_new_bullets.selectedIndex = 0;
  calcBulletsUpgrade();
  document.upgrade.p_new_bullets.disabled = !isGold;
}

var price = 100;
function toggleTmpltPkg(chk) {
  if (chk.checked) price += 100;
  else price -= 100;
  document.getElementById('tmplt_price').innerHTML = price;
}
