/**
 * @author Walter
 */

function openWin(url)
{
	/* The popup window is 60% x 60% of the screen resolution.
	Minimum width is 600. The popup window is centered on the screen */

	clientWidth = screen.width * 0.6;
	clientHeight = screen.height * 0.6;
	if (clientWidth < 600) clientWidth = 600;
	posX = (screen.width - clientWidth) * 0.5;
	posY = (screen.height - clientHeight) * 0.5;

	/* The width and height of the popup window applies to the client area!
	Because a popup window also has a title bar and a frame the total
	width and total height is bigger than the client area! Therefor we
	assume the total hight being about 40 pixel more than the client height.
	When we center the popup on the screen we get a better result. */
	posY -= 20;
	if (posX < 6) posX = 6;
	if (posY < 6) posY = 6;

	/* debug
	document.write("clientWidth=" + clientWidth + " clientHeight=" + clientHeight + "<br>");
	document.write("screen.width=" + screen.width + " posX=" + posX + "<br>");
	document.write("screen.height=" + screen.height + " posY=" + posY + "<br>");
	*/

	winProps='width='+clientWidth+', height='+clientHeight+', left='+posX+', screenX='+posX+', top='+posY+', screenY='+posY+', toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1';
	win1 = window.open(url, 'info_popup', winProps);
	win1.focus();
}

function openChildWin(url)
{
	/* This popup shall be called from another popup. While the above 
	openWin() popup  is centered on the screen, this openChildWin()
	popup is located in the top left corner of the screen. Example:
	While the openWin() popup may contain a form, the openChildWin()
	popup may be used to display a help text. */

	clientWidth = 600;
	clientHeight = 400;
	posX = 20;
	posY = 20;

	winProps = 'width='+clientWidth+', height='+clientHeight+', left='+posX+', screenX='+posX+', top='+posY+', screenY='+posY+', location=0, status=0, toolbar=0, menubar=0, scrollbars=1, resizable=1';
	win = window.open(url, 'info_child_popup', winProps);
	win.focus();
}

/**
 * Checks the validity of a credit card number
 * 
 * @param {Object} number
 */
function luhn_check(number) {
  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  var number=number.replace(/\D/g, '');

  // Set the string length and parity
  var number_length=number.length;
  var parity=number_length % 2;

  // Loop through each digit and do the maths
  var total=0;
  for (i=0; i < number_length; i++) {
    var digit=number.charAt(i);
    // Multiply alternate digits by two
    if (i % 2 == parity) {
      digit=digit * 2;
      // If the sum is two digits, add them together (in effect)
      if (digit > 9) {
        digit=digit - 9;
      }
    }
    // Total up the digits
    total = total + parseInt(digit);
  }

  // If the total mod 10 equals 0, the number is valid
  if (total % 10 == 0) {
    return true;
  } else {
    return false;
  }

}

function showBonus(country){
	openChildWin('/' + country + '/payment/special');
}

