

function createMember() {
	//Get all form data and saves it in a array
	var data = "";
	var name = "";
	$("input[type=text], input[type=submit], input[type=checkbox], select").each(function (i) {
		if($(this).attr("type") == "checkbox") {
			data += $(this).attr("name") + "=" + $(this).is(':checked') + "&";
		} else {
			data += $(this).attr("name") + "=" + $(this).val() + "&";
		}
	});
	
	jQuery.ajax({
		type: "POST",
		url: "/welcome/createNewMember/1",
		data: data,
		dataType: "text",
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("XMLHttpRequest: " + XMLHttpRequest + "\ntextStatus: " + textStatus + "\nerrorThrown: " + errorThrown);
		},
		success: function (data, textStatus) {
			  $("#ajax").css("display", "none");
			  data == "1" ? alertUser("success") : alertUser("fail");
		},
		beforeSend: function () {
			$("#message").hide();
			$("#ajax").css("display", "inline");
		}
	});
}

function alertUser(mode) {	

		if(mode == "success") {
			$("label.valid").remove();
			document.getElementById('frm').reset();
		}
		$("#message").removeClass().addClass("sprite").addClass(mode).fadeIn(300);
}

function validatePNum(sPNum)
{
  var numbers = sPNum.match(/^(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)$/);
  var checkSum = 0;

  var d = new Date();
  if (!isDate(sPNum.substring(0,4),sPNum.substring(4,6),sPNum.substring(6,8))) {
    return false;
  }

  if (numbers == null) { return false; }

  var n;
  for (var i = 3; i <= 12; i++)
  {
    n=parseInt(numbers[i]);
    if (i % 2 == 0) {
      checkSum+=n;
    } else {
      checkSum+=(n*2)%9+Math.floor(n/9)*9 
    }
  }

  if (checkSum%10==0) { return true;}
  return false;
}


function getYear(y) { return (y < 1000) ? y + 1900 : y; }

function isDate(year, month, day) 
{
  month = month - 1; // 0-11 in JavaScript
  var tmpDate = new Date(year,month,day);
  if ( (getYear(tmpDate.getYear()) == year) &&
  (month == tmpDate.getMonth()) &&
  (day == tmpDate.getDate()) )
    return true;
  else
    return false;
}


