/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
  return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
  while (textBox.value.length > 0 && isNaN(textBox.value)) {
    textBox.value = textBox.value.substring(0, textBox.value.length - 1)
  }
  
  textBox.value = trim(textBox.value);
  if (textBox.value.length != 0) textBox.value = parseInt(textBox.value);
/*  if (textBox.value.length == 0 || textBox.value == 0) {
    textBox.value = 0;
    textBox.select();
  } else {
    textBox.value = parseInt(textBox.value);
  }*/
}

/*
  Check if a form element is empty.
  If it is display an alert box and focus
  on the element
*/
function isEmpty(formElement, message) {
  formElement.value = trim(formElement.value);
  
  _isEmpty = false;
  if (formElement.value == '') {
    _isEmpty = true;
    alert(message);
    formElement.focus();
  }
  
  return _isEmpty;
}

function echeck(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false
  }
   if (str.indexOf(at,(lat+1))!=-1){
      return false
   }
   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
   }
   if (str.indexOf(dot,(lat+2))==-1){
      return false
   }
   if (str.indexOf(" ")!=-1){
      return false
   }

    return true          
  }

/*
  Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
  for (i=0; i < listElement.options.length; i++) {
    if (listElement.options[i].value == listValue)  {
      listElement.selectedIndex = i;
    }
  }  
}

function checkOrderInfo()
{
  with (window.document.frmOrder) {
    if (isEmpty(txtOrderName, 'Palun sisestage nimi')) {
      return false;
    } else if (isEmpty(txtOrderOrg, 'Palun sisestage organisatsiooni / firma / klubi nimi')) {
      return false;
    } else if (isEmpty(txtOrderEmail, 'Palun sisestage korrektne e-posti aadress')) {
      return false;
    } else if (echeck(txtOrderEmail.value)==false) {
      alert('Palun sisestage korrektne e-posti aadress');
      txtOrderEmail.focus();
      return false;
    } else if (isEmpty(txtOrderTel, 'Palun sisestage telefoninumber')) {
      return false;
    } else {
      return true;
    }
  }
}

function checkLength(countername, maxcount)
{
  var count = document.frmOrder.txtOrderComm.value.length;
  if (count > maxcount) {
    alert(maxcount+' tähemärki sai täis');
    document.frmOrder.txtOrderComm.value = document.frmOrder.txtOrderComm.value.substr(0,maxcount);
  } else document.getElementById(countername).innerHTML = count+' / '+maxcount;
}
