$(document).ready(function(){
	$(document.body).on ('click', '#testVin', function(){
  if( $("#vin").val() ) {
		vinVal();
    $(this).next("#error-vin").show();	
  }else{	
    $("#error-vin").hide();
  }
	 	return false;
	 });
	 $(document.body).on ('click', '.hide-vin', function(){
	 $(this).parent("#error-vin").hide();
	 return false;
	 });

	 function vinVal(){   
		 $( "#error-vin").empty(); 
	     
        var hasErrors = false;
        var errors = {
        };
        var VIN_PATTERN = new RegExp("^[A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][0-9X][A-HJ-NPR-TV-Y1-9][A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][A-HJ-NPR-Z0-9][0-9][0-9][0-9][0-9]$","i");
        var vinValue = $.trim($('#vin').val().toUpperCase());
     
        var VIN_PATTERN_ARRAY = [
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[0-9X]",
                "[A-HJ-NPR-TV-Y1-9]",
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[A-HJ-NPR-Z0-9]",
                "[0-9]",
                "[0-9]",
                "[0-9]",
                "[0-9]"];
        var errMsg="<ul>";
     
        if ( vinValue.length== 17 ) {
        	if (VIN_PATTERN.test(vinValue)) {
          if(!checkDigit(vinValue)){
          	errMsg +="<li>Invalid VIN</li>";
          }else{
            errMsg +='<li class="success-icon">Valid VIN</li>';
          }
             
           }else{//end VIN_PATTERN.test(vinValue)) 
        	   for (var  j=0; j<vinValue.length; j++ ) {
        		   var regExpChar =  new RegExp(VIN_PATTERN_ARRAY[j],"i");
        			if(!regExpChar.test(vinValue.charAt(j))){
        				errMsg +=getErrorMsg(j);
                  	  hasErrors = true
                  }
              }
           }	
        	
        }else{//end vinValue.length == 17
        	 errMsg=errMsg +"The VIN must be 17 characters long."
        }
        errMsg +="</ul>"
        //alert(errMsg);
      //  $("#error-vin").show();
        html = $.parseHTML( errMsg )
       
        $( "#error-vin").append( html );
   	  
      
    };	
  
  
    function populateDecodeArray(deCodeCharArray){
      deCodeCharArray["A"]=1;
      deCodeCharArray["B"]=2;
      deCodeCharArray["C"]=3;
      deCodeCharArray["D"]=4;
      deCodeCharArray["E"]=5;
      deCodeCharArray["F"]=6;
      deCodeCharArray["G"]=7;
      deCodeCharArray["H"]=8;
      deCodeCharArray["J"]=1;
      deCodeCharArray["K"]=2;
      deCodeCharArray["L"]=3;
      deCodeCharArray["M"]=4;
      deCodeCharArray["N"]=5;
      deCodeCharArray["P"]=7;
      deCodeCharArray["R"]=9;
      deCodeCharArray["S"]=2;
      deCodeCharArray["T"]=3;
      deCodeCharArray["U"]=4;
      deCodeCharArray["V"]=5;
      deCodeCharArray["W"]=6;
      deCodeCharArray["X"]=7;
      deCodeCharArray["Y"]=8;
      deCodeCharArray["Z"]=9;  
    }
	  /**
  * Checks for the input string to calculate the digit by
  * considering all the decode and weightfactor.
  * @author  	Gopal Rajanala
  * @version 	1.0 02/10/01
  */
  function calculateDigit(paramVIN){
    var VIN = new String(paramVIN);
	var deCodeCharArray = 	new Array();
  								//    1, 2, 3, 4, 5, 6, 7, 8,  9, 10, 11, 12, 13, 14, 15, 16, 17
  	var weightFactorArray = new Array(8, 7, 6, 5, 4, 3, 2, 10, 0,  9,  8,  7,  6,  5,  4,  3,  2);
	populateDecodeArray(deCodeCharArray);
	var sum = 0;
	for(i=0; i<VIN.length;i++){
		var deCodeChar = VIN.charAt(i);
		if(isNaN(VIN.charAt(i))){
			deCodeChar = deCodeCharArray[VIN.charAt(i)];
		}		
		var weightFacor = weightFactorArray[i];
		sum = sum + (deCodeChar*weightFacor);
	}
	var digit = sum % 11;
	if(digit == 10){
		return 'X';
	}
	return digit;
  } 
  
function checkDigit(paramVIN){
 	var digit = calculateDigit(paramVIN);
	var VIN = new String(paramVIN);
	var ninethChar = VIN.charAt(8);
	if(digit==ninethChar){
		return true;
	}
  	return false;
  }
	     function getErrorMsg(paramCharPosition){
		     	switch(paramCharPosition){
		   		case 0:
		   			return "<li>Position 1 of the VIN cannot contain the characters I, O, and Q.</li>";
		   		case 1:
		   			
		   			return "<li>Position 2 of the VIN cannot contain the characters I, O, and Q.</li>";
		   			
		   		case 2:
		   			
		   			return "<li>Position 3 of the VIN cannot contain the characters I, O, and Q.</li>";
		   			
		   		case 3:
		   			
		   			return "<li>Position 4 of the VIN cannot contain the characters I, O, and Q.</li>";
		   			
		   		case 4:
		   			
		   			return "<li>Position 5 of the VIN cannot contain the characters I, O, and Q.</li>";
		   			
		   		case 5:
		   			
		   			return "<li>Position 6 of the VIN cannot contain the characters I, O, and Q.</li>";
		   			
		   		case 6:
		   			
		   			return "<li>Position 7 of the VIN cannot contain the characters I, O, and Q.</li>";
		   			
		   		case 7:
		   			
		   			return "<li>Position 8 of the VIN cannot contain the characters I, O, and Q.</li>";
		   		case 8:
		   			
		   			return "The check character (position 9) of the VIN is invalid. Valid numbers are 0-9. The valid character is X.</li>";
		   			
		   		case 9:
		   			
		   			return "<li>Position 10 of the VIN cannot contain the characters I, O, Q, U, Z or the number 0.</li>";
		   			
		   		case 10:
		   			
		   			return "<li>Position 11 of the VIN cannot contain the characters  I, O, and Q.</li>";
		   				
		   		case 11:
		   			
		   			return "<li>Position 12 of the VIN cannot contain the characters I, O, and Q.</li>";
		   				
		   		case 12:
		   			
		   			return "<li>Position 13 of the VIN cannot contain the characters I, O, and Q.</li>";
		   				
		   		case 13:
		   			
		   			return "<li>Position 14 of the VIN must be numeric.</li>";
		   				
		   		case 14:
		   			
		   			return "<li>Position 15 of the VIN must be numeric.</li>";
		   				
		   		case 15:
		   			
		   			return "<li>Position 16 of the VIN must be numeric.</li>";
		   				
		   		case 16:
		   			
		   			return "<li>Position 17 of the VIN must be numeric.</li>";	
		   	
		   	}
		     
		     }
		      
	   
	   
	 
	    
	  
});
