var arrSelections = new Array();
indexLastSelection = 0;

function ucwords(str) {
  return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
    return $1.toUpperCase();
  });
}

function check(objForm) {
  objForm.votes.value = '';
  var len = objForm.lstDest.options.length;
  if (len < 10) {
    alert('Uw selectielijst moet uit tenminste 10 platen bestaan!');
    return false;
  }
  if (len > 30) {
    alert('U hebt ' + len + ' platen geselecteerd.\nUw selectielijst mag hoogstens 30 platen bevatten!');
    return false;
  }

  if (objForm.txtName.value == '') {
    alert('Geen naam ingevuld');
    return false;
  }
  var emailPattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if (emailPattern.test(objForm.txtEmail.value) == false) {
    alert('Ongeldig email adres ingevuld');
    return false;
  }
  for (var i = 0; i < len; i++) {
    if (i < (len - 1)) {
      objForm.votes.value += objForm.lstDest.options[i].value + ',';
    }
    else {
      objForm.votes.value += objForm.lstDest.options[i].value;
    }
  }
  objForm.submit();
}

function clearSelection(objForm) {
  while (arrSelections.length > 0) {
    objForm.lstSource.options[arrSelections.pop()].selected = false;
  }
  indexLastSelection = 0;
}

// Voeg geselecteerde items toe aan de selectielijst
function addSrcToDestList(objForm) {
  var len = objForm.lstDest.options.length;
  if (len == 30) {
    alert('Er staan al 30 platen in uw selectielijst!');
    return;
  }
  for (var i = 0; i < objForm.lstSource.length; i++) {
    if ((objForm.lstSource.options[i] != null) && (objForm.lstSource.options[i].selected)) {
      //Check if this value already exist in the destList or not
      //if not then add it otherwise do not add it.
      var found = false;
      for (var count = 0; count < len; count++) {
        if (objForm.lstDest.options[count] != null) {
          if (objForm.lstSource.options[i].text == objForm.lstDest.options[count].text) {
            found = true;
            break;
          }
        }
      }
      if (found != true) {
        objForm.lstDest.options[len] = new Option(objForm.lstSource.options[i].text, objForm.lstSource.options[i].value);
        len++;
      }
    }
  }
  return false;
}

// Verwijder items van de selectielijst
function deleteFromDestList(objForm) {
  var len = objForm.lstDest.options.length;
  for (var i = (len - 1); i >= 0; i--) {
    if ((objForm.lstDest.options[i] != null) && (objForm.lstDest.options[i].selected == true)) {
      objForm.lstDest.options[i] = null;
    }
  }
  return false;
}

// Selecteer alle items in de selectielijst om ze zo door te geven aan de volgende pagina
function selectAllDestList(objForm) {
  for (i = 0; i < objForm.lstDest.options.length; i++) {
    objForm.lstDest.options[i].selected = true;
  }
}

function getSubsetSelectielijst(p_letter, p_artist, p_title) {
	var strData = ''
	if (p_letter != '') {
		strData = "letter=" + p_letter;
	}
	if (p_artist != '') {
		strData = "artiest=" + p_artist;
	}
	if (p_title != '') {
		strData = "titel=" + p_title;
	}
  $.ajax({
    type:'POST',
    url:'ajax_stempagina_searchFilter.php',
    //data:{letter: p_letter, artiest: p_artiest, titel: p_titel},
    data: strData,
    dataType:'xml',
    error: function(){
      $('.error').text('Er is een fout opgetreden.');
    },
    success: function(xmlDoc, textStatus){
      $lstSource = $('div[id="divSource"]').find('select[id="lstSource"]');
      $lstSource.html('');
      // Doorloop het XML-document
      var titles = xmlDoc.getElementsByTagName('selectie');
      for (var i = 0; i < titles.length; i++) {
        strId = '';
        strArtiestnaam = '';
        strTitelnaam = '';
        for (el = 0; el < titles[i].childNodes.length; el++) {
          if (titles[i].childNodes[el].nodeType == 1) {
            switch (titles[i].childNodes[el].nodeName) {
              case 'id' : strId = unescape(titles[i].childNodes[el].firstChild.nodeValue); break;
              case 'artiestnaam' : strArtiestnaam = ucwords(unescape(titles[i].childNodes[el].firstChild.nodeValue)); break;
              case 'titelnaam' : strTitelnaam = ucwords(unescape(titles[i].childNodes[el].firstChild.nodeValue)); break;
              default : break;
            }
          }
        }
        $('<option/>', {value: strId, text: strArtiestnaam + ' - ' + strTitelnaam}).appendTo($lstSource);
      }
    }
  });
  return false;
}

// Hoofdfunctie
$(document).ready(function() {
	// Initieel vullen met artiesten met beginletter A
  getSubsetSelectielijst('A', '', '');

  // Functionaliteit voor het klikken op de letters
  $('div.vote_letters ul li a').bind('click', function() {
    var letter = $(this).text();
    //alert(letter);
    getSubsetSelectielijst(letter, '', '');
    return false;
  });
  
  // Functionaliteit voor het zoeken op artiestnaam
  $(':button[id="btnSearchArtist"]').bind('click', function() {
  	var arg = $(this).parent().find(':text[id="txtSearchArtist"]').val();
  	if (arg == '') {
  		alert('Er is geen zoekargument opgegeven!');
  	}
  	getSubsetSelectielijst('', arg, '');
  });
  
  // Functionaliteit voor het zoeken op titelnaam
  $(':button[id="btnSearchTitle"]').bind('click', function() {
  	var arg = $(this).parent().find(':text[id="txtSearchTitle"]').val();
  	if (arg == '') {
  		alert('Er is geen zoekargument opgegeven!');
  		return false;
  	}
  	getSubsetSelectielijst('', '', arg);
  });
  
  // Functionaliteit voor de pijlen
  /*$('a[id="addSrcToDestList"]').bind('click', function() {
  	var frmSelectie = $('form[name="frmSelectie"]').get();
  	addSrcToDestList(frmSelectie);
  	return false;
  });
  $('a[id="deleteFromDestList"]').bind('click', function() {
  	var frmSelectie = $('form[name="frmSelectie"]').get();
    deleteFromDestList(frmSelectie);
    return false;
  });*/
  
});

