// VIN form validation
charValKey = "ABCDEFGHJKLMNPRSTUVWXYZ";
charVal = "12345678123457923456789";
vinPosVal = new Array(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);
numbers = "1234567890";

function isNumber (theChar) {
	if (numbers.indexOf(theChar) >= 0 ) {
    	return true;
	} else {
        return false;
    }
}

function findCodeValue(theChar) {
	valIdx = 0;
    returnVal = "-1";

    if (isNumber(theChar)) {
    	returnVal = theChar;
    } else {
        valIdx = charValKey.indexOf(theChar);
        if (valIdx >= 0) {
        	returnVal=charVal.charAt(valIdx);
        }
	}
	return returnVal;
}

function computeCheckDigit(theVIN) {
	rowSum = 0;
    checkDigit = 0;
    theVIN = theVIN.toUpperCase();

    // Compute the sum of all digits
    for (i=0; i < theVIN.length; i++) {
    	rowSum = rowSum + (findCodeValue(theVIN.charAt(i)) * vinPosVal[i]);
    }

    // Devide the sum by 11. The remainder is the check digit
    // unless it is 10. In that case, the check digit is X.
    checkDigit = rowSum % 11;
    if (checkDigit == 10) {
    	return "X";
    } else {
        return checkDigit;
    }
}

function validateVIN(theVIN) {
	if (theVIN.length != 17) {
		//var returnMessage = "VIN must be 17 characters. Please re-enter the VIN or enter an explanation to continue"; 
		//jQuery.showMessage({thisMessage:[returnMessage],className:'error',displayNavigation:false,autoClose:true});
        document.vin_form.vin.focus();
		return false;
    } else {
        checkDigit = computeCheckDigit(theVIN);
        if (checkDigit == theVIN.charAt(8)) {
        	return true;
        } else {
			var returnMessage = "VIN is not valid and must be 17 characters, please re-enter the VIN or enter an explanation to continue"; 
			jQuery.showMessage({thisMessage:[returnMessage],className:'error',displayNavigation:false,autoClose:true});
            document.vin_form.vin.focus();
			return false;
        }
	}
}

function noVINinfo(entered)
{
	var scam=document.vin_form.scam.value;
	
	if(scam=="yes"){
		//alert(scam)
		return false;
	}else{
		if(entered==null || entered==""){
			var returnMessage = "VIN is not valid and must be 17 characters, please re-enter the VIN or enter an explanation to continue"; 
			jQuery.showMessage({thisMessage:[returnMessage],className:'error',displayNavigation:false,autoClose:true});
			return false;
		}else{
			return true;
		}
	}
} 

// validates the form (return of false should not send form)
function vinFormvalidation(thisform)
{
	if(validateVIN(document.vin_form.vin.value)==false && noVINinfo(document.vin_form.novin.value)==false){return false;}
}
