var swipecount=0;

// ***********************************************************************
// Function to validate an airline member id 
//
function valAirMemberId(v_vendor,f){

var v_length = f.length;
var v_member_id = f;
var v_id_string = new String(v_member_id);
var chk_dig_msg = "Il numero inserito non e' corretto. Controlla il numero e prova di nuovo.";

// *************** START KLM AirWays and AIR FRANCE Airways AR0562 ***********************
if(v_vendor == 'FB')
{
	if ((v_id_string*1) < 0)
	{
		alert('Il numero identificativo per i Soci KLM e Air France deve avere 12 cifre.');
		return false;
	}
	if (v_length == 10)
	{
		f = '00' + v_member_id;
		v_member_id = f;
		v_length = f.length;
		v_id_string = v_member_id;
	}
	if (v_length!= 12)
	{
		alert('Il numero identificativo per i Soci KLM e Air France deve avere 12 cifre.');
		return false;
	}
	if ((v_id_string.substring(0,2))!='00')
	{
		alert('Il numero identificativo per i Soci KLM e Air France deve iniziare con 00.');
		return false;
	}
	if (v_length == 12)
	{
		var v_result;
		v_result = ((v_id_string.substring(0,11))*1)%7;
		var chk_digit;
		if (v_result > 0)
		{
			chk_digit = v_result - 1;
			if( (v_id_string.substring(11,1)*1) != chk_digit)
			{
				alert(chk_dig_msg);
				return false;
			}
		}
		else
		{
			chk_digit = 6;
			if( (v_id_string.substring(11,1)*1) != chk_digit)
			{
				alert(chk_dig_msg);
				return false;
			}
		}
	}
}
// *************** END KLM AirWays and AIR FRANCE Airways AR0562 ***********************

// *************** START SAS (Scandanavian Airlines) PROCESSING *************************************** 
if (v_vendor ==  'SK')
{
	if( !chkNum(f,"Il codice identificativo per i Soci SAS è solo numerico."))
  	{
  		return false;
  	}
     if (v_length != 9)
     {
       alert('Il codice identificativo per i Soci SAS ha 9 cifre.');
       return false
	}
	if(!chkCheckDigit(10,v_member_id))
	{
		alert(chk_dig_msg);
		return false;
	}
} //*****************END SAS PROCESSING

// *************** START CZECH AIRLINES PROCESSING ***************************************
if (v_vendor ==  'OK')
{
	if (v_length != 8)
	{
		alert('Il codice identificativo per i Soci Czech Airlines ha 8 cifre.');
		return false;
	}
	if( !chkNum(f,"Il codice identificativo per i Soci Czech Airlines è solo numerico."))
  	{
  		return false;
  	}
	// Member ID values start at 10000014
	if ( v_member_id < 10000014)
	{
		alert('Il numero identificativo per i Soci Czech Airlines deve iniziare con 10000014.');
		return false;
	}
	// Now do the check digit routine.
	// Pos 8 is check digit number. Algorithm is: (first 7 numbers mod 7) + 2 
	// Step 1: mod 7 digits 1-7 
	var v_mod_res = (v_id_string.substr(0,7)*1)%7;
	// Step 2: Remainder plus 2 must equal digit 8 of member id
	if ((v_id_string.substr(7,1)) != v_mod_res + 2)
	{
		alert(chk_dig_msg);
		return false;
	}
} //*****************END CZECH AIRLINES PROCESSING

// *************** START CHINA SOUTHERN PROCESSING ***************************************
if (v_vendor ==  'CZ')
{
	if (v_length != 12 && v_length != 16)
	{
		alert('Il codice identificativo per i Soci China Southern ha 12 oppure 16 cifre.');
		return false;
	}
	if( !chkNum(f,"Il codice identificativo per i Soci China Southern è solo numerico."))
	{
		return false;
	}

	if (v_length == 12)
	{
		// Mod 7 of sum of digits 2 thru 12 must equal 1st digit of member id
		// Step 1: Add digts 2 thru 12 to get dividend
		var v_result;
		v_result = (v_id_string.substr(1,1)*1)+(v_id_string.substr(2,1)*1)+(v_id_string.substr(3,1)*1);
		v_result = (v_result*1)+(v_id_string.substr(4,1)*1)+(v_id_string.substr(5,1)*1)+(v_id_string.substr(6,1)*1);
		v_result = (v_result*1)+(v_id_string.substr(7,1)*1)+(v_id_string.substr(8,1)*1)+(v_id_string.substr(9,1)*1);
		v_result = (v_result*1)+(v_id_string.substr(10,1)*1)+(v_id_string.substr(11,1)*1)+(v_id_string.substr(12,1)*1);

        //alert("debug v_result is " + v_result);

        // Step 2: mod 7 the sum of digits 2-12
        var v_mod_res = v_result%7;
        //alert("debug v_mod_res = " + v_mod_res + "1st dig = " + (v_id_string.substr(0,1)));

        // Step 3: Remainder must equal 1st digit of 12 digit member id
		if ( (v_id_string.substr(0,1)) != v_mod_res)
        {
			alert(chk_dig_msg);
			return false;
		}
	} //end if

	// if length of member id is 16, first 7 digits must be 4580658
	if (v_length == 16)
	{
		if(v_id_string.substr(0,7) != '4580658')
		{
			alert('Le prime 7 cifre del numero identificativo dei Soci Southern China devono essere 4580658.');
			return false;
		}
	} //end if
} //*****************END CHINA SOUTHERN PROCESSING

// *************** START AIR BERLIN PROCESSING ***************************************
if (v_vendor ==  'AB')
{
	if (v_length != 12 && v_length != 9 && v_length != 16)
	{
		alert('Il codice identificativo per i Soci Air Berlin ha 9, 12 oppure 16 cifre.');
		return false;
	}

	if( !chkNum(f,"Il codice identificativo per i Soci Air Berlin è solo numerico."))
	{
		return false;
	}

	if (v_length == 16)
	{
		if(v_id_string.substr(0,8) != '54995700')
		{
			alert('Le prime 8 cifre del numero identificativo dei Soci Air Berlin devono essere 54995700.');
			return false;
		}
	} //end if

	//must be within ranges 
	if (v_length == 12 && (!(( v_member_id > 110000000000 && v_member_id <= 139999999999) || ( v_member_id > 760000000000 && v_member_id <= 779999999999) || ( v_member_id > 990000000000 && v_member_id <= 999999999999))) )
	{
		alert('Il numero identificativo dei Soci Air Berlin deve rientrare nelle seguenti fasce: da 1100 0000 0001 a 1399 9999 9999 e da 9900 0000 0001 a 9999 9999 9999.');
		return false;
	}
} //*****************END AIR BERLIN PROCESSING

// *************** START CONTINENTAL AIRLINES PROCESSING ***************************************

if (v_vendor ==  'CO')
{
	if (v_length != 8)
	{
		alert('Il codice identificativo per i Soci Continental ha 8 cifre.');
		return false;
	}
	// Continental member id 1st 2 digits must both be alpha or both be numeric. Last 6 postitions must be numeric.
	
	// Make sure last 6 digits of member id are numeric 
	var   v_dig;
	var   count = 2;  //starting in pos3
	while (count < 8)				// check if last 6 digits are numeric
	{
		v_dig = v_id_string.substr(count,1);	// count start at pos3 go to pos8
		if (v_dig < '0' || v_dig > '9')
		{
			alert("Prego inserire un valore numerico per la sesta posizione del codice identificativo dei Soci Continental.");
			return false;
		}
		++count;
	} // end while 

	// Make sure first 2 positions of member id are either both alpha or both numeric
	v_dig1 = v_id_string.substr(0,1);	// start at pos 1 for a length of 1
	v_dig2 = v_id_string.substr(1,1);  // start at pos 2 for a length of 1

	if ((v_dig1 >= 'A') && (v_dig1 <= 'Z'))  //if v_dig1 alpha   
	{
		if ((v_dig2 >= '1') && (v_dig2 <= '9'))  //if v_dig2 numeric, not allowed
		{
			//alert("pos1 alpha, pos2 numeric, not allowed");
			alert("La prima e la seconda posizione del codice identificativo CO può contenere sia lettere che cifre."); 
			return false;
		}
	}
	else //v_dig1 is numeric            
	{
		if ((v_dig2 >= 'A') && (v_dig2 <= 'Z')) //v_dig1 is numeric, so v_dig2 must also be numeric  
		{
			//alert("pos1 numeric, pos2 alpha, not allowed");
			alert("La prima e la seconda posizione del codice identificativo CO può contenere sia lettere che cifre."); 
			return false;
		}
	} //end if
	//convert pos1 and pos2 of member id from alpha to numeric

	v_pos1 = v_id_string.substr(0,1);
	v_pos2 = v_id_string.substr(1,1);
	switch(v_pos1){
		case 'A':   v_pos1 = '1';     break;
		case 'B':   v_pos1 = '2';     break;
		case 'C':   v_pos1 = '3';     break;
		case 'D':   v_pos1 = '4';     break;
		case 'E':   v_pos1 = '5';     break;
		case 'F':   v_pos1 = '6';     break;
		case 'G':   v_pos1 = '7';     break;
		case 'H':   v_pos1 = '8';     break;
		case 'I':   v_pos1 = '9';     break;
		case 'J':   v_pos1 = '1';     break;
		case 'K':   v_pos1 = '2';     break;
		case 'L':   v_pos1 = '3';     break;
		case 'M':   v_pos1 = '4';     break;
		case 'N':   v_pos1 = '5';     break;
		case 'O':   v_pos1 = '6';     break;
		case 'P':   v_pos1 = '7';     break;
		case 'Q':   v_pos1 = '8';     break;
		case 'R':   v_pos1 = '9';     break;
		case 'S':   v_pos1 = '2';     break;
		case 'T':   v_pos1 = '3';     break;
		case 'U':   v_pos1 = '4';     break;
		case 'V':   v_pos1 = '5';     break;
		case 'W':   v_pos1 = '6';     break;
		case 'X':   v_pos1 = '7';     break;
		case 'Y':   v_pos1 = '8';     break;
		case 'Z':   v_pos1 = '9';     break;
		default:   v_pos1 = v_pos1;  break;
	} // end switch

	switch(v_pos2){
		case 'A':   v_pos2 = '1';     break;
		case 'B':   v_pos2 = '2';     break;
		case 'C':   v_pos2 = '3';     break;
		case 'D':   v_pos2 = '4';     break;
		case 'E':   v_pos2 = '5';     break;
		case 'F':   v_pos2 = '6';     break;
		case 'G':   v_pos2 = '7';     break;
		case 'H':   v_pos2 = '8';     break;
		case 'I':   v_pos2 = '9';     break;
		case 'J':   v_pos2 = '1';     break;
		case 'K':   v_pos2 = '2';     break;
		case 'L':   v_pos2 = '3';     break;
		case 'M':   v_pos2 = '4';     break;
		case 'N':   v_pos2 = '5';     break;
		case 'O':   v_pos2 = '6';     break;
		case 'P':   v_pos2 = '7';     break;
		case 'Q':   v_pos2 = '8';     break;
		case 'R':   v_pos2 = '9';     break;
		case 'S':   v_pos2 = '2';     break;
		case 'T':   v_pos2 = '3';     break;
		case 'U':   v_pos2 = '4';     break;
		case 'V':   v_pos2 = '5';     break;
		case 'W':   v_pos2 = '6';     break;
		case 'X':   v_pos2 = '7';     break;
		case 'Y':   v_pos2 = '8';     break;
		case 'Z':   v_pos2 = '9';     break;
		default:    v_pos2 = v_pos2;  break;
	} // end switch

	// Concatenate numeric pos 1 and pos 2 with numeric pos 3-8 of member id
	v_id_string = v_pos1 + v_pos2 + v_id_string.substr(2,6);
	//var v_message = "Continental member id has been converted to " + v_id_string;
	//alert(v_message);

	v_member_id = v_id_string; //  function chkCheckDigit() uses v_member_id

	if(!chkCheckDigit(10,v_member_id))  // did not pass mod 10 check digit
	{
		//alert("FAILED mod 10 check digit, will call function valContinentalID");
		if(!valContinentalID(v_member_id))
		{
			alert(chk_dig_msg);
			//alert(" FAILED both mod10 and valContinentalID");
			return false;
		}

//		else
//		{
//			alert("FAILED mod10; But passed valContinentalID");
//		}
//		//return false;
//      }
//      else 
//        {alert ("passed mod 10 check digit");
//         //return false;
	}
} //*****************END CONTINENTAL AIRLINES PROCESSING


if (v_vendor == 'NW')
{
	if (v_length != 9 && v_length != 12)
	{
		alert('Il codice identificativo per i Soci Northwest ha 9 oppure 12 cifre.');
		return false;
	}
	if( !chkNum(f,"Il codice identificativo per i Soci Northwest ha 9 oppure 12 cifre."))
	{
		return false;
	}
	//now check the check digit
	if(!chkCheckDigit(7,v_member_id))
	{
		alert(chk_dig_msg);
		return false;
	}
}

if (v_vendor ==  'AK')
{
	if (v_length != 8)
	{
		alert('Prego inserire un valore numerico di 8 cifre per il codice identificativo dei Soci Alaska.');
		return false;
	}
	if( !chkNum(f,"Prego inserire un valore numerico di 8 cifre per il codice identificativo dei Soci Alaska."))
	{
		return false;
	}
	if(!chkCheckDigit(7,v_member_id))
	{
		alert(chk_dig_msg);
		return false;
	}
}
if (v_vendor ==  'TW')
{
	if (v_length != 8 && v_length != 9)
	{
		alert('Prego inserire un valore numerico di 8 o 9 cifre per i Soci TWA.');
		return false;
	}
	if( !chkNum(f,"Prego inserire un valore numerico di 8 o 9 cifre per i Soci TWA."))
	{
		return false;
	}
	//check that check-digit isn't 7, 8 or 9

	v_dig = v_id_string.substr(v_length-1,1);
	if (v_dig == '7' || v_dig == '8' || v_dig == '9')
	{
		alert('Il numero identificativo dei Soci TWA non può terminare con 7, 8 o 9.');
		return false;
	}
	//check that 1st digit isn't 0
	v_dig = v_id_string.substr(0,1);
	if (v_dig == '0')
	{
		alert('Il numero identificativo dei Soci TWA inizia con 0.');
		return false;
	}
	if(!chkCheckDigit(7,v_member_id))
	{
		alert(chk_dig_msg);
		return false;
	}
}  
//TW validation ends here
//--- th011606   Lufthansa validation begins here
if (v_vendor ==  'LH')
{
	if (v_length != 9 && v_length != 15 && v_length != 16)
	{
		alert('Prego inserire un valore numerico di 9, 15 o 16 cifre per il codice identificativo dei Soci Lufthansa.');
		return false;
	}
	if (v_length == 16)
	{
		if(v_id_string.substr(0,6) != '401003' && v_id_string.substr(0,6) != '401001' && v_id_string.substr(0,6) != '401002' && v_id_string.substr(0,6) != '401000' && v_id_string.substr(0,6) != '403400' && v_id_string.substr(0,6) != '456848' && v_id_string.substr(0,6) != '479202')
		{
			alert('Le prime 6 cifre del numero identificativo Lufthansa non sono valide.');
			return false;
		}
	}

	if (v_length == 15)
	{
		if(v_id_string.substr(0,5) != '33303' && v_id_string.substr(0,5) != '22201' && v_id_string.substr(0,5) != '99222' && v_id_string.substr(0,5) != '99200' && v_id_string.substr(0,4) != '9999')
		{
			alert('Le prime 5 cifre del numero identificativo Lufthansa non sono valide.');
			return false;
		}
	}

	// Concatenate numeric 0 to member id if length = 16, must be odd number 
	var v_result;
	v_result = v_id_string;
	if (v_length == 16)
	{
		v_result = '0' + v_id_string.substr(0,16);
	}
          
	if(!chkCheckDigitlh(10,v_result ))
	{
		alert(chk_dig_msg);
		return false; 
	}
}  //--- th011606  LH validation ends here

if (v_vendor ==  'DE'){
	if (v_length != 10)
	{
		alert('Prego inserire un valore numerico di 10 cifre per i Soci Delta.');
		return false;
	}
	if( !chkNum(f,"Prego inserire un valore numerico di 10 cifre per i Soci Delta."))
	{
		return false;
	}

	if (( v_member_id >= 8704 && v_member_id <= 8999) || ( v_member_id >= 900000 && v_member_id <= 999999))
	{
		return true;
	} //no check digit check for these ranges
	if(!chkCheckDigit(10,v_member_id))
	{
		if (v_member_id >= 100000 && v_member_id <= 199999)
		{
			//--numbers from 0000100000-0000199999 go thru check digit twice,
     		//--once as is and if they fail subtract 100,000 from it and check again.
			v_member_id = v_member_id - 100000;
			if(!chkCheckDigit(10,v_member_id))
			{
				alert(chk_dig_msg);
				return false;
			}
		}
		else{
			alert(chk_dig_msg);
			return false;
		}
	}
} //Done with special Delta validation
//Code changed by Pradeep for merging the AW and US vendor Code
if (v_vendor ==  'AW')
{
	if (v_length > 11 || v_length < 2)
	{
		alert('Prego inserire un valore numerico da 2 a 11 cifre per i Soci America West.');
		return false;
		}
		if (v_length == 7)	// Checking for US member since this member can have any non numeric character   
		{
			if(!valAmericanID(v_id_string,v_vendor))	// This code is validating for AW member which hass length of 7
			{
				if( !chkNum(f,"Numero identificativo Socio non valido per America West."))
				{
					return false;
				}
				if(!chkCheckDigit(10,v_member_id))
				{
					alert(chk_dig_msg);
					return false;
				}
			}
		}
		if(v_length != 7)
		{
			if( !chkNum(f,"Prego inserire un valore numerico per il codice identificatico di America West."))
			{
				return false;
			}
			if(!chkCheckDigit(10,v_member_id))
			{
				if (v_length == 8 || v_length == 9 ||v_length == 10 ||v_length == 11)
				{
				if(!chkCheckDigit(7,v_member_id))
				{
					alert(chk_dig_msg);
     				return false;
     			}
			}
			else
			{
				alert(chk_dig_msg); 
				return false; 
			}
		}
	} //end of if to check member_id length not equals to 7
}//     --Done with special America West validation   

if (v_vendor == 'AC')
{
	if (v_length != 9 )
	{
		alert('Prego inserire un valore numerico di 9 cifre per i Soci Air Canada.');
		return false;
	}
	v_dig = v_id_string.substr(0,1);
	if (v_dig !='1' && v_dig != '3' && v_dig != '5' && v_dig != '7' && v_dig != '9')
	{
		alert('La prima cifra del codice identificativo dei Soci Air Canada deve essere dispari.');
		return false; 
	}
	if( !chkNum(f,"Prego inserire un valore numerico per i Soci Air Canada."))
	{
		return false;
	}
	if(!chkCheckDigit(10,v_member_id))
	{
		alert(chk_dig_msg);
		return false;
	}
} // Air Canada validation ends here

if (v_vendor == 'AA')
{
	if (v_length != 7)
	{
		alert('Prego inserire un valore numerico di 7 cifre per il codice identificativo Soci American Airlines.');
		return false;
	}
	if(!valAmericanID(v_id_string,v_vendor))
	{
		return false;
	}   
}  //American validation ends here

if(v_vendor == 'UA')
{
	if( !chkNum(f,"Prego inserire un valore numerico per i Soci United Airlines."))
	{
		return false;
	}
	if (v_length != 11)
	{
		alert('Prego inserire un valore numerico di 11 cifre per il codice identificativo Soci American Airlines.');
		return false;
	}  		
	if (v_vendor == 'AA')
	{
		if (v_length != 7)
		{
			alert('Prego inserire un valore numerico di 7 cifre per il codice identificativo Soci American Airlines.');
			return false;
		}
		if(!valAmericanID(v_id_string,v_vendor))
		{
			return false;
		}
	}  //American validation ends here
	var v_result;
	v_result = (v_id_string.substr(0,1) * 5)+(v_id_string.substr(1,1) * 4)+(v_id_string.substr(2,1) * 3);
	v_result = (v_result*1)+(v_id_string.substr(3,1) * 2)+(v_id_string.substr(4,1) * 7)+(v_id_string.substr(5,1) * 6);
	v_result = (v_result*1)+(v_id_string.substr(6,1) * 5)+(v_id_string.substr(7,1) * 4)+(v_id_string.substr(8,1) * 3)+(v_id_string.substr(9,1) * 2);
	var v_mod_res = v_result%11;
	var v_check_dig;
	if( v_mod_res !=0 )
	{
		v_check_dig = 11-v_mod_res;
	}
	else
	{
		v_check_dig = 0;
	}
	if( v_check_dig == 10 )
	{
		v_check_dig = 0;
	}
	if( v_check_dig != v_id_string.substr(10,1))
	{
	   alert(chk_dig_msg);
	   return false;
	}
}//UA validation ends here

if(v_vendor == 'AS')
{
	if (v_length != 9)
	{
		alert('Prego inserire un valore numerico di 9 cifre per il codice identificativo Soci Asiana Airlines.');
		return false;
	}
	//check if first digit numeric, if so rest of acct nbr must be number, if not convert 1st 3 alpha to number
	var   v_digs;
	var   v_dig3;
	var   v_dig; 
	var   count = 3;
	v_dig3 = v_id_string.substr(0,3);
	v_digs = v_id_string.substr(3,6);
	v_dig = v_id_string.substr(0,1);

	if (v_dig < '0' || v_dig > '9')
	{
		while (count < 9)				// check if last 6 digits are numeric
		{
			v_dig = v_id_string.substr(count,1);	// count start at 3 go to 9
			if (v_dig < '0' || v_dig > '9')
			{
				alert("Prego inserire un valore numerico di 6 cifre per il codice identificativo Soci Asiana Airlines.");
				return false;
			}
			++count;
		} // end while 
		switch(v_dig3){
			case 'ABA':   v_dig3 = '010';     break;
			case 'ABB':   v_dig3 = '011';     break;
			case 'ABC':   v_dig3 = '002';     break;
			case 'ABD':   v_dig3 = '003';     break;
			case 'ABE':   v_dig3 = '004';     break;
			case 'ABF':   v_dig3 = '005';     break;
			case 'ABG':   v_dig3 = '006';     break;
			case 'ABH':   v_dig3 = '012';     break;
			case 'ABI':   v_dig3 = '007';     break;
			case 'ABJ':   v_dig3 = '020';     break;
			case 'ABK':   v_dig3 = '001';     break;
			case 'ABL':   v_dig3 = '008';     break;
			case 'ABM':   v_dig3 = '009';     break;
			case 'ABT':   v_dig3 = '014';     break;
			case 'ABY':   v_dig3 = '000';     break;
			case 'ACA':   v_dig3 = '031';     break;
			case 'ACC':   v_dig3 = '030';     break;
			case 'AJA':   v_dig3 = '021';     break;
			case 'AJB':   v_dig3 = '022';     break;
			case 'AJC':   v_dig3 = '023';     break;
			case 'AJD':   v_dig3 = '024';     break;
			case 'AJE':   v_dig3 = '025';     break;
			case 'AJF':   v_dig3 = '026';     break;
			case 'AJG':   v_dig3 = '027';     break;
			case 'AJH':   v_dig3 = '028';     break;
			case 'ABS':   v_dig3 = '013';     break;
			default: 
			alert('Le prime tre cifre del codice identificativo Asiana Airlines non sono valide, prego controllare il numero e provare di nuovo.');
		return false;
		break;
	} // end switch
	v_id_string = v_dig3 + v_id_string.substr(3,6);} // end if
	else
	{
	if( !chkNum(f,"Prego inserire un valore numerico per il codice identificativo Asiana Airlines."))  // all numeric check
  	{
  		return false;
  	}  // end if
}   // end else

//    do check digit routine
	var v_result;
	v_result = (v_id_string.substr(0,1) + v_id_string.substr(1,1) + v_id_string.substr(2,1) + v_id_string.substr(3,1) + v_id_string.substr(4,1) + v_id_string.substr(5,1) + v_id_string.substr(6,1) + v_id_string.substr(7,1));
	var v_mod_res = v_result%9;
	if( v_mod_res != v_id_string.substr(8,1) )
	{
		alert(chk_dig_msg);
		return false;
	}
}//AS validation ends here


if(v_vendor == 'AZ')
{
	if (v_length != 10)
	{
		alert('Prego inserire un valore numerico di 10 cifre per il codice identificativo Soci Alitalia.');
		return false;
	}
// do not allow 10 spaces 
	if (v_member_id == '          ')
	{
		alert('Prego inserire un valore numerico di 10 cifre per il codice identificativo Soci Alitalia senza spazi.');
		return false;
	}
}//AZ validation ends here

if(v_vendor == 'LA')  // LanChile checked here
{
	if (v_length < 8 || v_length > 10)
	{
		alert('Il numero identificativo di LanChile Airways deve avere da 8 a 10 cifre.');
		return false;
	}
	var chk_len = (v_length - 1);
	var v_id_string = f.substr(0,chk_len);  // take off check digit
	//check if all but check digit numeric
	var   v_dig; 
	var   count = 1;

	while (count < chk_len)				// check if all but check digit are numeric
	{
		v_dig = v_id_string.substr(count,1);	// count start at 1 go to before check digit
		if (v_dig < '0' || v_dig > '9')
		{
			alert("Prego inserire in valore numerico per numero identificativo di LanChile Airlines.");
			return false;
   		}
		++count;
	} // end while 

	if(v_length == 9) {v_id_string = '0'+v_id_string;}
	if(v_length == 8) {v_id_string = '00'+v_id_string;}

	var v_result;
	v_result = (v_id_string.substr(0,1) * 4)+(v_id_string.substr(1,1) * 3)+(v_id_string.substr(2,1) * 2);
	v_result = (v_result*1)+(v_id_string.substr(3,1) * 7)+(v_id_string.substr(4,1) * 6)+(v_id_string.substr(5,1) * 5);
	v_result = (v_result*1)+(v_id_string.substr(6,1) * 4)+(v_id_string.substr(7,1) * 3)+(v_id_string.substr(8,1) * 2);
	var v_mod_res = v_result%11;
	var v_check_dig;
	v_check_dig = 11 - v_mod_res;
	var last_num = f.substr(chk_len,1);   
	if( v_check_dig == 10 ){v_check_dig = 'K';}
	if( v_check_dig == 11 ){v_check_dig = '0';}

	if( v_check_dig != last_num ){
	   alert(chk_dig_msg);
	   return false; }
}//LA validation ends here

if(v_vendor == 'KK'){  // TAM checked here
    
   if (v_length < 3 || v_length > 10){
       alert('Member ID for TAM Airways must be between 3 and 10 digits.');
       return false;
	   }
   if( !chkNum(f,"Prego inserire un valore numerico per il codice identificativo TAM.") )
      { return false; }

   var chk_len = (v_length - 1);
   var v_id_string = f.substr(0,chk_len);  // take off check digit

   if(v_id_string >= 1000000) {  // new rule


      if(v_length == 9) {v_id_string = '0'+v_id_string;}
      if(v_length == 8) {v_id_string = '00'+v_id_string;}
      if(v_length == 7) {v_id_string = '000'+v_id_string;}	

      var v_result;
      v_result =              (v_id_string.substr(0,1) * 2)+(v_id_string.substr(1,1) * 9)+(v_id_string.substr(2,1) * 8);
      v_result = (v_result*1)+(v_id_string.substr(3,1) * 7)+(v_id_string.substr(4,1) * 6)+(v_id_string.substr(5,1) * 5);
      v_result = (v_result*1)+(v_id_string.substr(6,1) * 4)+(v_id_string.substr(7,1) * 3)+(v_id_string.substr(8,1) * 2);

      var v_mod_res = v_result%10;
      var v_check_dig;
      if(v_mod_res =='0' || v_mod_res == '1') {
         v_check_dig = '0';}
      else {
	 v_check_dig = 10 - v_mod_res; }

      var last_num = f.substr(chk_len,1);   

      if( v_check_dig != last_num ){
	  alert(chk_dig_msg);
	  return false; }
    } // end of new rule

    else { // old run
      if(v_length == 7) {v_id_string = '0'+v_id_string;}
      if(v_length == 6) {v_id_string = '00'+v_id_string;}
      if(v_length == 5) {v_id_string = '000'+v_id_string;}
      if(v_length == 4) {v_id_string = '0000'+v_id_string;}
      if(v_length == 3) {v_id_string = '00000'+v_id_string;}
	
      var v_result;
      v_result =              (v_id_string.substr(0,1) * 1)+(v_id_string.substr(1,1) * 2)+(v_id_string.substr(2,1) * 3);
      v_result = (v_result*1)+(v_id_string.substr(3,1) * 4)+(v_id_string.substr(4,1) * 5)+(v_id_string.substr(5,1) * 6);
      v_result = (v_result*1)+(v_id_string.substr(6,1) * 7);
        
      var test = Math.floor(v_result/7);
      var v_mod_res = v_result - ((Math.floor(v_result/7)) * 7);
      var v_check_dig = v_mod_res;
      var last_num = f.substr(chk_len,1);   
        
      if( v_check_dig != last_num ){
	  alert(chk_dig_msg);
	  return false; }
    } // end of old rule
}//TAM validation ends here

if (v_vendor == 'TG'){
     if (v_length != 7){
         alert('Prego inserire un valore numerico di 7 cifre per il codice identificativo Soci Thai Airlines.');
         return false;
       }
     v_dig1 = v_id_string.substr(0,1);	// count start at 1 go to before check digit
     v_dig2 = v_id_string.substr(1,1);
     if ((v_dig1 < 'A' || v_dig1 > 'Z') || (v_dig2 < 'A' || v_dig2 > 'Z')) {                            
         alert("Prego inserire una lettera per le prime due posizioni del codice identificativo dei Soci Thai Airlines.");
         return false;}
     
     var   v_dig;
     var   count = 2;
    
     
     while (count < 7){				// check if last 5 digits are numeric
       v_dig = v_id_string.substr(count,1);	// count start at 2 go to 7
       if (v_dig < '0' || v_dig > '9'){ 
           alert("Prego inserire un valore numerico per le ultime 5 cifre del codice identificativo Thai Airlines.");
           return false;}
           ++count;
        } // end while 
         

}  //Thai validation ends here

return true;
} 

// ***********************************************************************
// Function to check a check digit on a member id 
// using either a mod7 or mod10 routine
//

function chkCheckDigit(v_mod, v_member_id)
{
	//mod7 routine
	if (v_mod == 7)
	{
		//step 1 separate check digit from member number
		var v_numb = v_member_id/10;
		var w_member_id = Math.floor(v_numb);
		var check_digit = (v_numb - w_member_id) * 10;
		check_digit = Math.round(check_digit);
		//step 2 divide by 7 and trunc any decimals
		//  step 3 multiply by 7
		// step 4 subtract from member_id*/
		v_numb = w_member_id - (Math.floor(w_member_id/7) * 7);
		if (v_numb != check_digit){return false;}
	}
	//mod10 routine
	if (v_mod == 10)
	{
		var vlen = v_member_id.length;  
		var vskip = vlen % 2;
		//alert('v_len/vskip = ' + vlen + '/' + vskip);
		var mem_string = new String(v_member_id);
		var vtotal = new Number(0);
		var v_digit = new Number(0);
		var i = new Number(1);
		do
		{
			vdigit = Math.round(mem_string.substr(i-1,1));
			if (i % 2 == vskip)
			{
				vtotal = Math.round(vtotal + vdigit);
			}
			else
			{
				vtotal = Math.round(vtotal) + Math.floor(2*vdigit/10) + Math.round((2*vdigit) % 10);
			}
			i++;
		}
		while (i <= vlen)
		if ( vtotal % 10 != 0 ){return false;}
		}
		if (v_mod != 7 && v_mod != 10){return false;}
	return true;
}  
// *********************************************************************** th011606 whole routine
// Function to check a check digit on a member id for Lufthansa       
// using mod10 routine
//
  
function chkCheckDigitlh(v_modlh, v_result){
	//mod10 routine
	if (v_modlh == 10)
	{
		var vlen = v_result.length;  
		var vskip = vlen % 2;
		//alert('v_len/vskip = ' + vlen + '/' + vskip);
		var mem_string = new String(v_result);
		var vtotal = new Number(0);
		var v_digit = new Number(0);
		var i = new Number(1);
		do
		{
			vdigit = Math.round(mem_string.substr(i-1,1));
			if (i % 2 == vskip)
			{
				vtotal = Math.round(vtotal + vdigit);
			}
			else
			{
				vtotal = Math.round(vtotal) + Math.floor(2*vdigit/10) + Math.round((2*vdigit) % 10);
			}
			i++;
		}
		while (i <= vlen)
		if ( vtotal % 10 != 0 ){return false;}
		}
		if (v_modlh != 10){return false;}
	return true;
}

// end of chkCheckDigitlh *******************************************

// ***********************************************************************
// Function to do special validation for American Airlines also US Aire
//

function valAmericanID(v_id_string,v_vendor){
	var v_vendor_desc;
	if (v_vendor=='AA'){v_vendor_desc = 'American Airlines';}
	if (v_vendor=='US'){v_vendor_desc = 'US Airways';}

	//make sure I, O, Q and Z arent in the id anywhere
	var i =0;
	var v_specl_char = 'IOQZ';
	for(i; i < 4; i++)
	{
		if( v_id_string.indexOf(v_specl_char.substr(i,1)) >= 0 )
		{
			alert('Le lettere I, O, Q e Z non sono valide per '+v_vendor_desc+' numero di tessera..');
			return false;
		}
	}
	//make sure G or S arent in position 2-7
	var v_id_short = new String(v_id_string.substr(1,6));
	if (v_id_short.indexOf('G') >=0)
	{
		alert('La lettera G non puo essere tra la posizione 2 e 7.');
		return false;
	}
	if (v_id_short.indexOf('S') >=0){
	 	alert('La lettera S non puo essere tra la posizione 2 e 7.');
		return false;
	}
	//check digit is an even number
	var chk_dig = v_id_string.substr(6,1)
	var even_chk = new String('02468');
	if (even_chk.indexOf(chk_dig)<0)
	{
		alert('Le cifre digitate devono essere pari.');
		return false;
	}
	var v_A1 = new Number(convertAAchar(v_id_string.substr(0,1)));
	var v_A3 = new Number(convertAAchar(v_id_string.substr(2,1)));
	var v_A4 = new Number(convertAAchar(v_id_string.substr(3,1)));
	var v_A5 = new Number(convertAAchar(v_id_string.substr(4,1)));
	var v_A6 = new Number(convertAAchar(v_id_string.substr(5,1)));
	var v_calc = new Number((v_A1+v_A3+v_A3+v_A4+v_A5+v_A6)/5);
	v_calc = Math.floor(v_calc*10);
	var v_rem = v_calc.toString();
	var v_len = v_rem.length;
	if (chk_dig != v_rem.substr(v_len-1,1))
	{
		alert("Il codice identificativo non e' stato validato.");
		return false;
	}
return true;
}

// ***********************************************************************
// Function to convert an alpha numeric to the ascii value of it's ebcdic value on a member id 
//
function convertAAchar(v_char)
{
  switch(v_char){
	case "0": 
		return '240';
		break;
	case "1": 
		return '241';
		break;
	case "2": 
		return '242';
		break;
	case "3": 
		return '243';
		break;		   
	case "4": 
		return '244';
		break;
	case "5": 
		return '245';
		break;
	case "6": 
		return '246';
		break;
	case "7": 
		return '247';
		break;		
	case "8": 
		return '248';
		break;
	case "9": 
		return '249';
		break;
	case "A": 
		return '193';
		break;
	case "B": 
		return '194';
		break;		   
	case "C": 
		return '195';
		break;
	case "D": 
		return '196';
		break;
	case "E": 
		return '197';
		break;
	case "F": 
		return '198';
		break;		
	case "G": 
		return '199';
		break;
	case "H": 
		return '200';
		break;
	case "I": 
		return '201';
		break;
	case "J": 
		return '209';
		break;		   
	case "K": 
		return '210';
		break;
	case "L": 
		return '211';
		break;
	case "M": 
		return '212';
		break;
	case "N": 
		return '213';
		break;		
	case "O": 
		return '214';
		break;
	case "P": 
		return '215';
		break;
	case "Q": 
		return '216';
		break;
	case "R": 
		return '217';
		break;		   
	case "S": 
		return '226';
		break;
	case "T": 
		return '227';
		break;
	case "U": 
		return '228';
		break;
	case "V": 
		return '229';
		break;		
	case "W": 
		return '230';
		break;
	case "X": 
		return '231';
		break;
	case "Y": 
		return '232';
		break;
	case "Z": 
		return '233';
		break;
	default :
		return '0';
		break;		   	   		   		   		   	   
  }
}

// ***********************************************************************
// Function to do a special validation for Continental if ID 
// did not pass standard mod10 check digit
//
  
function valContinentalID(v_member_id)
{
	var vlen = v_member_id.length;
	var vskip = vlen % 2;
	//alert('v_len/vskip = ' + vlen + '/' + vskip);
	var mem_string = new String(v_member_id);
	var vtotal = new Number(0);
	var v_digit = new Number(0);
	var i = new Number(1);
	do
	{
		vdigit = Math.round(mem_string.substr(i-1,1));
		if (i % 2 == vskip)              //DoneForPos 2,4,6,8
	    
		if (i == 8)                     //compare low order digit of member id to end_plus_1 calculated in i=7
		{
			//alert(" In i=8 , vdigit = " + vdigit);
			//alert(" In i-8,  end_plus_1 = " + end_plus_1);
			if (vdigit != end_plus_1)
			{
				//alert("did not pass valContinentalID");
				return false;
			}
		}
		else
		{
			vtotal = Math.round(vtotal + vdigit);         //Done for pos 2,4,6 
		}
		else
		{
			vtotal = Math.round(vtotal) + Math.floor(2*vdigit/10) + Math.round((2*vdigit) % 10); // DoneForPos 1,3,5,7 
			if (i == 7)
			{
				low_order_digit         = (vtotal % 10); 
				//alert("low_order_digit is " + low_order_digit);
				subtract_from_10_result = (10 - low_order_digit);
				//alert("subtract_from_10_result is " + subtract_from_10_result);
				end_plus_1              = (subtract_from_10_result + 1);
				//alert("end_plus_1 " + end_plus_1);

			if (end_plus_1 > 9) 
			{
				end_plus_1 = 1;       //if end result is > 9, use 1
				//alert("end_plus_1 changed to " + end_plus_1);
			}
		}			
	}				// end outer if	
	//alert ("i = " + i);
	//alert ("vdigit = " + vdigit);
	//alert ("vtotal = " + vtotal);
	i++;
	}
	while (i <= vlen);
	return true;
} //end of function  
// end of valContinentalID *******************************************

// ***********************************************************************
function chkNum(f,vText)
{
	f = trim(f);
	if( !valNumber(f) || f =="" )
	{
		alert(vText); 
		f.focus();
		return false;
	}
	else
	{
		return true;
	}
}
// end of chkNum *********************************************************

// ***********************************************************************
// Function to validate field for integer data only
function valNumber(obj)
{
	var myval = obj;
	var numStr = "012345556789";
	var charStr;
	var counter = 0;
	var i =0;
	var error = "Prego inserire un numero intero valido."; 

	for(i; i < myval.length; i++)
	{
		charStr = myval.substring(i, i+1);
		if(numStr.indexOf(charStr) !=-1)
					counter ++;
	}
	if ( (counter !=myval.length) )
	{
		return false;
	}
	return true;
}
// end of valNumber ******************************************************

// ***********************************************************************
// Function to trim trailing spaces
function trim(str)
{
	var start = 0;
	var len = str.length;
	var end = len-1;
	for(var i = 0; i > len; i++)
		if(str.charCodeAt(i) == 32 || str.charCodeAt(i) == 9)
				start++;
		else
			break;
	for(var i=end;i>=0;i--)
			if(str.charCodeAt(i) == 32 || str.charCodeAt(i) == 9)
					end--;
			else
					break;
	if (start <= end)
	return(str.substring(start,end+1));
	else return "";
}
// end of trim ***********************************************************

