// JavaScript Document
function validateSIN (sin) {
	// function to validate a Canadian Social Insurance Number (SIN)

	// initialize validation variable to true
	var valid = true;
	var msg = "";

	// trim whitespace at beginning and end of string
	//sin = sin.trim();

	// trim extra whitespace from string
	// Note: this function will trim ALL whitespace from string vs prev function which only trimmed 
	// from beginning and end of string.
	// Requires trim() function in genfunc.js to be included as external script file
	sin = trim(sin);

	// Remove non-numeric characters.
	// Note: Only use this if we want to fix input errors without alerting user to the error.  Probably not a useful thing
	// to do since the string probably won't pass the required test for 9 characters or the checkdigit - better to alert the
	// user to the fact that they input non-numeric data.
	//sin = numericOnly(sin);	// function (will work in v3 browsers)
	//sin = sin.replace(/(\D)+/g,"");	// (will not work in v3 browsers)
	
	// must be 9 characters (digits)
	digits = sin.length;
	if (digits > 9) 
	{
		valid = false;
		msg = "Invalid: " + sin + " has more than maximum 9 digits.";
	} 
	else if (digits < 9)
	{
		valid = false;
		if (digits == 0)
		{
			msg = "Please enter a 9 digit Social Insurance Number.";
		} else {
			msg = "Invalid: " + sin + " has less than the required 9 digits.";
		}
	} 
	else if (!sin.match(/^\d+$/)) 	// must contain ONLY digits 0-9
	{
		valid = false;
		msg = "Invalid: " + sin + " contains invalid non-numeric characters";
	} 
	else if (sin=="000000000")		// for use when unknown SIN only
	{
		msg = "000000000 may be used only when SIN is unknown - please revalidate when SIN is available";
	}
	else 
	{	// perform the checkdigit validation routine

		// last (9th) digit is the check digit
		var checkdigit = sin.substr(8,1);
	
		// Double the even-numbered position digits (pos 2,4,6 & 8)
		var double2 = parseInt(sin.substr(1,1)) * 2;
		var double4 = parseInt(sin.substr(3,1)) * 2;
		var double6 = parseInt(sin.substr(5,1)) * 2;
		var double8 = parseInt(sin.substr(7,1)) * 2;
	
		// concatenate the doubles into one number string
		var num1 = double2.toString() + double4.toString() + double6.toString() + double8.toString();

		// Extract the odd-numbered position digits
		var digit1 = sin.substr(0,1);
		var digit3 = sin.substr(2,1);
		var digit5 = sin.substr(4,1);
		var digit7 = sin.substr(6,1);

		// concatenate the digits into one number string
		var num2 = digit1 + digit3 + digit5 + digit7;
			
		// sum the digits in num1
		var crossadd1 = 0;
		var position = 0;
		for (position = 0; position < num1.length; position ++) {
			crossadd1 = crossadd1 + parseInt(num1.substring(position, position+1));	
		}

		// sum the digits in num2
		var crossadd2 = 0;
		for (position = 0; position < num2.length; position ++) {
			crossadd2 = crossadd2 + parseInt(num2.substring(position, position+1));
		}
		
		// add the two sums
		var checksum1 = crossadd1 + crossadd2;
		var checksum2;
		var checkdigitX;
		
		if (checksum1.toString().substring(checksum1.toString().length-1)=="0") {
			checksum2 = checksum1;
			checkdigitX = '0';
		} else {
			checksum2 = (Math.ceil(checksum1 / 10.0) * 10.0);
			checkdigitX = parseFloat(checksum2 - checksum1).toString();
		}

		if (checkdigitX == checkdigit) {
			valid = true;
			msg = "Valid: " + sin + " is a valid SIN.";
		} else {
			valid = false;
			msg = "Invalid: " + sin + " - check digit does not pass validation test.";
		}
	}
	/*else {
		msg = "Testing only - SIN validates";
	}*/

	alert(msg);
	return valid;
} 	// end function validateSIN(sin)

function validateBN (bn) {
	// function to validate a Canadian CRA Busines Number (BN)
	// Note: same checkdigit formula as SIN validation applies to BN

	// initialize validation variable to true
	var valid = true;
	var msg = "";

	// trim whitespace at beginning and end of string
	//bn = bn.trim();

	// trim extra whitespace from string
	// Note: this function will trim ALL whitespace from string vs prev function which only trimmed 
	// from beginning and end of string.
	// Requires trim() function in genfunc.js to be included as external script file
	bn = trim(bn);

	// Remove non-numeric characters.
	// Note: Only use this if we want to fix input errors without alerting user to the error.  Probably not a useful thing
	// to do since the string probably won't pass the required test for 9 characters or the checkdigit - better to alert the
	// user to the fact that they input non-numeric data.
	// bn = numericOnly(bn);	// function (will work in v3 browsers)
	// bn = bn.replace(/(\D)+/g,"");	// (will not work in v3 browsers)
	
	// must be 9 characters (digits)
	digits = bn.length;
	if (digits != 9) 
	{
		valid = false;
		msg = "Submitted BN must have 9 digits";
	} 
	else if (!bn.match(/^\d+$/)) 	// must contain ONLY digits 0-9
	{
		valid = false;
		msg = "BN contains invalid non-numeric characters";
	} 
	else if (bn=="000000000")		// for use when unknown SIN only
	{
		msg = "000000000 may be used only when BN is unknown - please revalidate when BN is available";
	}
	else 
	{	// perform the checkdigit validation routine

		// last (9th) digit is the check digit
		var checkdigit = bn.substr(8,1);
	
		// Double the even-numbered position digits (pos 2,4,6 & 8)
		var double2 = parseInt(bn.substr(1,1)) * 2;
		var double4 = parseInt(bn.substr(3,1)) * 2;
		var double6 = parseInt(bn.substr(5,1)) * 2;
		var double8 = parseInt(bn.substr(7,1)) * 2;
	
		// concatenate the doubles into one number string
		var num1 = double2.toString() + double4.toString() + double6.toString() + double8.toString();

		// Extract the odd-numbered position digits
		var digit1 = bn.substr(0,1);
		var digit3 = bn.substr(2,1);
		var digit5 = bn.substr(4,1);
		var digit7 = bn.substr(6,1);

		// concatenate the digits into one number string
		var num2 = digit1 + digit3 + digit5 + digit7;
			
		// sum the digits in num1
		var crossadd1 = 0;
		var position = 0;
		for (position = 0; position < num1.length; position ++) {
			crossadd1 = crossadd1 + parseInt(num1.substring(position, position+1));	
		}

		// sum the digits in num2
		var crossadd2 = 0;
		for (position = 0; position < num2.length; position ++) {
			crossadd2 = crossadd2 + parseInt(num2.substring(position, position+1));
		}
		
		// add the two sums
		var checksum1 = crossadd1 + crossadd2;
		var checksum2;
		var checkdigitX;
		
		if (checksum1.toString().substring(checksum1.toString().length-1)=="0") {
			checksum2 = checksum1;
			checkdigitX = '0';
		} else {
			checksum2 = (Math.ceil(checksum1 / 10.0) * 10.0);
			checkdigitX = parseFloat(checksum2 - checksum1).toString();
		}

		if (checkdigitX == checkdigit) {
			valid = true;
			msg = "BN is a valid BN.";
		} else {
			valid = false;
			msg = "Invalid BN - check digit does not pass validation test.";
		}
	}
	/*else {
		msg = "Testing only - BN validates";
	}*/

	alert(msg);
	return valid;
} 	// end function validateBN(bn)
