
function checkdigit(){
//var submitted_card = "9300861549";
var submitted_card = (document.form.cardnumber.value);
var submitted_checkdigit = submitted_card.substr(9,1);
var barcode = submitted_card.substr(0,9);
var sum = "0";
for (var i = 1; i <= 9; i++)
   {
	sum = (11-i) * barcode.substr((i-1),1) + parseInt(sum);
   }
sum = ((11-(sum % 11)) % 11);

if (submitted_card.substr(0,3) == "930" || submitted_card.substr(0,3) == "931")
   {
     if (sum == 10)
	{
	if (submitted_checkdigit == "+")
		{
			return true;
			//alert('Check Digit: ' + sum + '\nBarcode: ' + barcode + "+"); 
		}
	else
		{
			return false;
			//alert('Failed Check\nCheck Digit: ' + sum + '\nSubmitted Check Digit: ' + submitted_checkdigit); 
		}
	}
     else 
	{
	if (submitted_checkdigit == sum)
		{
			return true;
			//alert('Check Digit: ' + sum + '\nBarcode: ' + barcode + sum); 
		}
	else
		{
			return false;
			//alert('Failed Check\nCheck Digit: ' + sum + '\nSubmitted Check Digit: ' + submitted_checkdigit); 
		}
	}
   }
else 
   {
	return false;
	//alert('Failed: Not starting with 930'); 
   }
}







function isEmpty( str){
    strRE = new RegExp( );
    strRE.compile( '^[\s ]*$', 'gi' );
    return strRE.test( str.value );
} 

function isNumericNoPeriod(str){ 
var checkOK = "0123456789+";
var allValid = true; 
for (i = 0;  i < str.length;  i++) 
		{  ch = str.charAt(i); 
		   for (j = 0;  j < checkOK.length;  j++) 
		       if (ch == checkOK.charAt(j)) 
		       break; 
		       if (j == checkOK.length) 
			  { allValid = false; 
			    break; 
			  } 
		} 
		if (allValid) { return true } 
		else { return false } 
} 

function checkForm( form ){
    //lets check if Name text field is empty. This field has name "name".
    if( isEmpty( form.cardnumber ) ){
        alert( 'You must enter a card number!' );
        return false;
    }
    if (!(isNumericNoPeriod(document.form.cardnumber.value))) { 
        alert( 'Only numeric characters or a "+" sign are allowed in a card number.' ); 
        return false;
    }
    if (document.form.cardnumber.value.length != 10) {
	alert( 'You have entered an invalid length of card number.');
        return false;
    }
    if (!(checkdigit(document.form.cardnumber.value))) { 
        alert( 'The Card number you entered is not valid!\n Contact your local Scenic Regional Library if you need assistance.' ); 
        return false;
    }
    return true;
} 


