/**
 * Page initialize
 */
function init()
{
  $('#registration_form').submit(registerSubmit);
  $('#info_form').submit(infoSubmit);
  
  if (typeof(p_url) == 'string' && p_url.length > 0) {
    window.onunload = RorC;
  }
}

/**
 * Registration form submit validation
 */
function registerSubmit()
{
/*
  if (! $('#agreement').attr('checked')) {
    alert('You must agree our privacy and promotion term to continue');
    return false;
  }

  if ($('#email').val().length == 0 || $('#password').val().length == 0) {
    alert('Email address and password are required');
    return false;
  }
*/
  disablePopup();
  return true;
}

/**
 * Info form submit validation
 */
function infoSubmit()
{
  /*
  if ($('#forename').val().length == 0) {
    alert('Forename is required');
    return false;
  }

  if ($('#surname').val().length == 0) {
    alert('Surname is required');
    return false;
  }

  var postal_code_pattern = /^[A-Za-z]{1,2}\d[0-9A-Za-z]\ ?\d[A-Za-z]{2}$/;
  if (! postal_code_pattern.test($('#postal_code').val())) {
    alert('Postal code invalid');
    return false;
  }

  if ($('#address_1').val().length == 0) {
    alert('Home address is required');
    return false;
  }

  if ($('#city_town').val().length == 0) {
    alert('Town is required');
    return false;
  }

  if (! checkAge($('#month').val(), $('#day').val(), $('#year').val())) {
    alert("You must be at least 13 years old.");
    return false;
  }

  var phone_pattern = /^0?\d{10}$/;
  var phone = $('#phone_areacode').val() + $('#phone_number').val();
  if (! phone_pattern.test(phone)) {
    alert('Primary phone invalid');
    return false;
  }
	*/
  disablePopup();
  return true;
}

function checkAge(m, d, y)
{
  var today = new Date();
  var age13Time = new Date(); 
  age13Time.setFullYear(parseInt(y)+13, parseInt(m)-1, parseInt(d));

  if (age13Time > today) {
    return false;
  }
  return true;
}

/**
 * Onunload popup
 */
function RorC()
{ 
  var features = '';

  if ((p_height > 0) && (p_width > 0)) {
    features = features + 'height='+p_height+',';
    features = features + 'width='+p_width+',';
    features = features + 'toolbar=no,menubar=no,location=no,directories=no,status=no,scrollbars=yes';
  }
  else{
	features = features + 'toolbar=yes,menubar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,resizable=yes';
  }
  if (typeof(p_link) == 'string' && p_link.length > 0) {
    var location = p_link+'?url='+encodeURIComponent(p_url);
  }
  else {
    var location = p_url;
  }
  var nzzz = window.open(location,'nzzz',features);
  if (nzzz) {
	nzzz.focus();
  }  
}

/**
 * Remove onunload popup
 */
function disablePopup()
{
  document.body.onunload = "";
  document.body.onbeforeunload = "";
  window.onunload = "";
  window.onbeforeunload = "";
}

function numOnly(e, allowChars){
	pressed = (window.event) ? event.keyCode : e.which;

	c = false;
	if(allowChars)
		c = checkChars(pressed, allowChars);

	//allow common key such as back space, tab
	if(pressed >= 48 && pressed <= 57 || c || pressed == 8 || pressed == 0)
		return true;
	return false;
}

function checkChars(c, list){
	found = false;
	for(i = 0; i < list.length; i++){
		if(c == asciiValue(list[i]))
			found = true;
	}
	return found;
}

function asciiValue (c){
	c = c . charAt (0);

	var i;
	for (i = 0; i < 256; ++ i){
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;
		h = "%" + h;
		h = unescape (h);
		if (h == c)
			break;
	}
	return i;
}