function getRandomNum(lbound, ubound) {
  return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function getRandomChar(number, lower, upper, other, extra) {
  var numberChars = "0123456789";
  var lowerChars = "abcdefghijklmnopqrstuvwxyz";
  var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var otherChars = "~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
  var charSet = extra;
  if (number == true)
    charSet += numberChars;
  if (lower == true)
    charSet += lowerChars;
  if (upper == true)
    charSet += upperChars;
  if (other == true)
    charSet += otherChars;
  return charSet.charAt(getRandomNum(0, charSet.length));
}

function possible(number, lower, upper, other, extra, strLen, numCodes) {
  var numberChars = "0123456789";
  var lowerChars = "abcdefghijklmnopqrstuvwxyz";
  var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var otherChars = "~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
  
  var charSet = extra;
  if (number == true)
    charSet += numberChars;
  if (lower == true)
    charSet += lowerChars;
  if (upper == true)
    charSet += upperChars;
  if (other == true)
    charSet += otherChars;

  var pos = Math.pow(  charSet.length, strLen) ;
  
  if(numCodes <= pos){
	return true;
  }else{
	return false;
  }

}


function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther,
latterNumber, latterLower, latterUpper, latterOther) {
  var rc = "";
  if (length > 0)
    rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
  for (var idx = 1; idx < length; ++idx) {
    rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
  }
  return rc;
}

function doloop(counter) {
  tStr = "";

  var i = 0;
  while(i < counter ) {
  	
	
    var cpw = getPassword($('input[name="passwordLength"]').val(),
	$('input[name="extraChars"]').val(),
	$('input[name="firstNumber"]').get(0).checked,$('input[name="firstLower"]').get(0).checked,
	$('input[name="firstUpper"]').get(0).checked, $('input[name="firstOther"]').get(0).checked,
	$('input[name="latterNumber"]').get(0).checked, $('input[name="latterLower"]').get(0).checked,
	$('input[name="latterUpper"]').get(0).checked, $('input[name="latterOther"]').get(0).checked)+ "\n";
	
	if(tStr.indexOf(cpw) === -1 ){
		tStr = tStr + cpw ;
		i += 1;
	}
	
  }
  //document.pwdgen.password.value = '';
  $('textarea[name="password"]').val(tStr)
  
}

function StartCreator() {
	
	if(!possible(
		$('input[name="latterNumber"]').get(0).checked,
		$('input[name="latterLower"]').get(0).checked,
		$('input[name="latterUpper"]').get(0).checked,
		$('input[name="latterOther"]').get(0).checked,
		$('input[name="extraChars"]').val(),
		$('input[name="passwordLength"]').val(),
		$('input[name="passwordCount"]').val()
	
	
				 ) ){
		alert('Sorry! Für die gewünschte Anzahl Codes ist die Codelänge zu kurz.');
		return false;	
	}
	
  if ($('input[name="passwordLength"]').val() > 99)
   $('input[name="passwordLength"]').val(99);
  if ($('input[name="passwordCount"]').val() > 99999)
   $('input[name="passwordCount"]').val(99999) ;
   var numPw  =  $('input[name="passwordCount"]').val() ;
 
  doloop ( numPw );
}

