// /////////////////////////////////////////////////////////////////////////////////
// JavaScript Magstripe (track 1, track2) data parser object
//
// Mar-22-2005	Modified by Wayne Walrath,
//				Acme Technologies http://www.acmetech.com
//			based on demo source code from www.skipjack.com
//
// USAGE:
// var p = new SwipeParserObj();
// p.dump();  -- returns parsed field values and meta info.
//	-- or --
// get individual field names (see member variables at top of object)
//
// if( p.hasTrack1 ){
//		p.surname;
//		p.firstname;
//		p.account;
//		p.exp_month + "/" + p.exp_year;
//	}
///////////////////////////////////////////////////////////////////////////////////

function SwipeParserObj(strParse)
{
	///////////////////////////////////////////////////////////////
	if (strParse.substring(0,1)=='~') {
		strParse = strParse.substring(1);
	}
	
	///////////////////// member variables ////////////////////////
	this.input_trackdata_str = strParse;
	this.account_name = null;
	this.surname = null;
	this.firstname = null;
	this.acccount = null;
	this.exp_month = null;
	this.exp_year = null;
	this.track1 = null;
	this.track2 = null;
	this.track3 = null;
	this.hasTrack1 = false;
	this.hasTrack2 = false;
	this.hasTrack3 = false;
	/////////////////////////// end member fields /////////////////

  this.errorRead = function() {
	  this.hasTrack1 =  false;
    this.hasTrack2  = false;
  	this.hasTrack3 = false;
	}
		
	function valid_name(sInput){
		if (sInput == null) return false;
		clean = sInput.replace(/[^a-zA-Z\'\-\/ ]/,'');
		return !(clean != sInput);
	}
	
	sTrackData = this.input_trackdata_str;     //--- Get the track data
	
  //-- Example: Track 1 & 2 Data
  //-- %B1234123412341234^CardUser/John^030510100000019301000000877000000?;1234123412341234=0305101193010877?
  //-- Key off of the presence of "^" and "="

  //-- Example: Track 1 Data Only
  //-- B1234123412341234^CardUser/John^030510100000019301000000877000000?
  //-- Key off of the presence of "^" but not "="

  //-- Example: Track 2 Data Only
  //-- 1234123412341234=0305101193010877?
  //-- Key off of the presence of "=" but not "^"

  if ( strParse != '' )
  {
    // alert(strParse);

    //--- Determine the presence of special characters
    nHasTrack1 = strParse.indexOf("^");
    nHasTrack2 = strParse.indexOf("=");
    nCountEndSentinal = strParse.split("?").length - 1;

    //--- Set boolean values based off of character presence
    this.hasTrack1 = bHasTrack1 = false;
    this.hasTrack2 = bHasTrack2 = false;
  	this.hasTrack3 = bHasTrack3 = false;
    if (nHasTrack1 > 0) { this.hasTrack1 = bHasTrack1 = true; }
    if (nHasTrack2 > 0) { this.hasTrack2 = bHasTrack2 = true; }
    if (nCountEndSentinal == 3) {this.hasTrack3 = bHasTrack3 = true;}
		
    //--- Initialize
    bTrack1_2  	= false;
    bTrack1_2_3 = false;
    bTrack1    	= false;
    bTrack2    	= false;
		
    //--- Determine tracks present

		if (( bHasTrack3)) { bTrack1_2_3 = true; }
    if (( bHasTrack1) && ( bHasTrack2) && (!bHasTrack3)) { bTrack1_2 = true; }
    if (( bHasTrack1) && (!bHasTrack2) && (!bHasTrack3)) { bTrack1   = true; }
    if ((!bHasTrack1) && ( bHasTrack2) && (!bHasTrack3)) { bTrack2   = true; }

    //--- Initialize alert message on error
    bShowAlert = false;
    
    //-----------------------------------------------------------------------------    
    //--- Track 1 & 2 cards
    //--- Ex: B1234123412341234^CardUser/John^030510100000019301000000877000000?;1234123412341234=0305101193010877?
    //-----------------------------------------------------------------------------    
    if (bTrack1_2)
    { 

      strCutUpSwipe = '' + strParse + ' ';
      arrayStrSwipe = new Array(4);
      arrayStrSwipe = strCutUpSwipe.split("^");
  
      var sAccountNumber, sName, sShipToName, sMonth, sYear;
  
      if ( arrayStrSwipe.length > 2 )
      {
      	this.account = stripAlpha( arrayStrSwipe[0].substring(1,arrayStrSwipe[0].length) );
        this.account_name          = arrayStrSwipe[1];
        this.exp_month         = arrayStrSwipe[2].substring(2,4);
        this.exp_year          = arrayStrSwipe[2].substring(0,2); 
        				
       	var track2sentinel = sTrackData.indexOf(";");
       	if( track2sentinel != -1 ){
       		this.track1 = sTrackData.substring(0, track2sentinel);
       		this.track2 = sTrackData.substring(track2sentinel);
       	} else
					{
						this.errorRead();
					}
				if (valid_name(this.account_name)) {
					//--- parse name field into first/last names
					var nameDelim = this.account_name.indexOf("/");
					if( nameDelim != -1 ){
						this.surname = this.account_name.substring(0, nameDelim);
						this.firstname = this.account_name.substring(nameDelim+1);
					} else {
						this.firstname = this.account_name;
						this.surname = "";
					}
				}
				else
					{
						this.errorRead();
					}
    }
      else  //--- for "if ( arrayStrSwipe.length > 2 )"
      { 
        bShowAlert = true;  //--- Error -- show alert message
				this.errorRead();
      }
    }
		if (bTrack1_2_3)
		{ 
			strCutUpSwipe = '' + strParse + ' ';
			arrayStrSwipe = new Array(4);
			arrayStrSwipe = strCutUpSwipe.split("^");

			var sAccountNumber, sName, sShipToName, sMonth, sYear;

			if ( arrayStrSwipe.length > 2 )
			{
				this.account = stripAlpha( arrayStrSwipe[0].substring(1,arrayStrSwipe[0].length) );
				this.account_name          = arrayStrSwipe[1];
				this.exp_month         = arrayStrSwipe[2].substring(2,4);
				this.exp_year          = arrayStrSwipe[2].substring(0,2); 

				var track2sentinel = sTrackData.indexOf(";");
				
				if( track2sentinel != -1 )
				{
					this.track1 = sTrackData.substring(0, track2sentinel);
					this.track2 = sTrackData.substring(track2sentinel);
				} 
				else 
				{
					this.errorRead();
				}
				var track2endsentinel =this.track2.indexOf("?");
				if (track2endsentinel != -1)
				{
					this.track3 = this.track2.substring(track2endsentinel+1);
					this.track2 = this.track2.substring(0,track2endsentinel+1);	
				} 
				else
				{ 
					this.errorRead();
				}

				//--- parse name field into first/last names
				if (valid_name(this.account_name)) 
				{
					var nameDelim = this.account_name.indexOf("/");
					if( nameDelim != -1 ) 
					{
						this.surname = this.account_name.substring(0, nameDelim);
						this.firstname = this.account_name.substring(nameDelim+1);
					} 
					else 
					{
						this.firstname = this.account_name;
						this.surname = "";
					}
				} 
				else
				{
					this.errorRead();
				}
			}
			else  //--- for "if ( arrayStrSwipe.length > 2 )"
			{ 
				bShowAlert = true;  //--- Error -- show alert message
				this.errorRead();
			}
		}

    //-----------------------------------------------------------------------------
    //--- Track 1 only cards
    //--- Ex: B1234123412341234^CardUser/John^030510100000019301000000877000000?
    //-----------------------------------------------------------------------------    
    if (bTrack1)
    {
      strCutUpSwipe = '' + strParse + ' ';
      arrayStrSwipe = new Array(4);
      arrayStrSwipe = strCutUpSwipe.split("^");
  
      var sAccountNumber, sName, sShipToName, sMonth, sYear;
  
      if ( arrayStrSwipe.length > 2 )
      {
        this.account = sAccountNumber = stripAlpha( arrayStrSwipe[0].substring( 1,arrayStrSwipe[0].length) );
        this.account_name = sName	= arrayStrSwipe[1];
        this.exp_month = sMonth	= arrayStrSwipe[2].substring(2,4);
        this.exp_year = sYear	= arrayStrSwipe[2].substring(0,2); 
      	this.track1 = sTrackData;
				//--- parse name field into first/last names
				if (valid_name(this.account_name)) 
				{
					var nameDelim = this.account_name.indexOf("/");
					if( nameDelim != -1 )
					{
						this.surname = this.account_name.substring(0, nameDelim);
						this.firstname = this.account_name.substring(nameDelim+1);
					} 
					else 
				 	{
						this.firstname = this.account_name;
						this.surname = "";
					} 
				}
				else
				{
					this.errorRead();
				}
      }
      else  //--- for "if ( arrayStrSwipe.length > 2 )"
      { 
        bShowAlert = true;  //--- Error -- show alert message
					this.errorRead();
      }
    }
    
    //-----------------------------------------------------------------------------
    //--- Track 2 only cards
    //--- Ex: 1234123412341234=0305101193010877?
    //-----------------------------------------------------------------------------    
    if (bTrack2)
    {
      nSeperator  = strParse.indexOf("=");
      sCardNumber = strParse.substring(1,nSeperator);
      sYear       = strParse.substr(nSeperator+1,2);
      sMonth      = strParse.substr(nSeperator+3,2);
      this.account = sAccountNumber = stripAlpha(sCardNumber);
      this.exp_month = sMonth		= sMonth;
      this.exp_year = sYear			= sYear; 
  		this.track2 = sTrackData;
			this.surname = ""
			this.firstname = ""
			if (this.account.length < 13) this.errorRead();
    }
	} //--- end "if ( strParse != '' )"


	this.dump = function(){
		var s = "";
		var sep = "\n"; // line separator
		s += "Name: " + this.account_name + sep;
		s += "Surname: " + this.surname + sep;
		s += "first name: " + this.firstname + sep;
		s += "account: " + this.account + sep;
		s += "exp_month: " + this.exp_month + sep;
		s += "exp_year: " + this.exp_year + sep;
		s += "has track1: " + this.hasTrack1 + sep;
		s += "has track2: " + this.hasTrack2 + sep;
		s += "has track3: " + this.hasTrack3 + sep;
		s += "TRACK 1: " + this.track1 + sep;
		s += "TRACK 2: " + this.track2 + sep;
		s += "TRACK 3: " + this.track3 + sep;
		s += "Raw Input Str: " + this.input_trackdata_str + sep;
		
		return s;
	}

	function stripAlpha(sInput){
		if( sInput == null )	return '';
		return sInput.replace(/[^0-9]/g, '');
	}


}