(*
 Additional types and functions that can be used in scripts:
 
 Please see 'OPTIONS' in the body of the script.

 This version of script is for use with PVD versions 0.9.9.16 and above!!!
 ---------------------------------------------

 SCRIPT:		EGAFD_Movie
 CO-AUTHORS:	pra15 and Ivek23
 VERSION:		0.1.1.0
 DATE:			13/01/2012

 ---------------------------------------------

CHANGE LOG:

v 0.1.1.0		Ivek23: initial "public" release to auto-update system
			only change is in script name (was final - posted at topic reply:
			http://www.videodb.info/forum_en/index.php/topic,3021.msg14853.html#msg14853)
			previous versions posted at forum topic:
			http://www.videodb.info/forum_en/index.php/topic,3021.0.html

---------------------------------------------

 CUSTOM FIELD USE:

Field names in comments are enclosed in "~" so they are easier to find in the script.
Use with your editor's search function to find applicable code sections.

 EGAFD data:		 PVD field:		    	       Type:
-------------	   --------------			  -----------------

EGAFD Version	    ~EGAFD Version~				short text
EGAFD ID		    ~EGAFD ID~					short text
Director			~EGAFD Director~			memo
AKA					~EGAFD FullAkas~			memo

 Footnotes:
 ----------

1. Control the use of standard fields using the "Overwrite Fields" plugin 
preferences in the main program.

Some of these optional custom fields are named the same as their standard counterparts.
To avoid confusion, you may wish to rename them (i.e., in the script, in PVD and your skin). 
For example, change "Year" to " EGAFD Year"—so as to not be confused 
with the standard field used by another source.

2. 'Options' are set in the body of the script below. 
Recommended: Leave them all set to 'True' or 'False' and use  
Preferences > Plugins > EGAFD_Movie > Overwrite fields 
to control how these items are to be updated.

 ---------------------------------------------

  //Types
  TWIDEARRAY : array of String

  //Field functions
  procedure AddSearchResult(Title1, Title2, Year, URL, PreviewURL : String)
  procedure AddFieldValue(AField: Integer; AValue : String)
  procedure AddMoviePerson(Name, TransName, Role, URL : String; AType : Byte)
  procedure AddPersonMovie(Title, OrigTitle, Role, Year, URL : String; AType : Byte)
  procedure AddAward(Event, Award, Category, Recipient, Year: String; const Won : Boolean)
  procedure AddConnection(Title, OrigTitle, Category, URL, Year: String)
  procedure AddEpisode(Title, OrigTitle, Description, URL, Year, Season, Episode : String)
 
  //String functions
  function Pos(Substr : String; Str: String): Integer
  function PosFrom(const SubStr, Str : String; FromIndex : Integer) : Integer
  function LastPos(const SubStr, Str : String) : Integer
  function PrevPos(const SubStr, Str : String; APos : Integer) : Integer
  function RemoveTags(AText : String; doLineBreaks : Boolean) : String
  function ExplodeString(AText : String; var Items : TWideArray; Delimiters : String) : Integer
  function Copy(S: String; Index, Count: Integer): String
  procedure Delete(var S: String; Index, Count: Integer)
  procedure Insert(Source: String; var Dest: String; Index: Integer)
  function Length(S: String): Integer
  function Trim(S: String): String
  function CompareText(S1, S2: String): Integer
  function CompareStr(S1, S2: String): Integer
  function UpperCase(S: String): String
  function LowerCase(S: String): String
  function StringReplace(S, OldPattern, NewPattern: String; ReplaceAll : Boolean; IgnoreCase : Boolean; WholeWord: Boolean): String
  function StrToInt(const S: String): Integer
  function IntToStr(const Value: Integer): String
  function StrToFloat(const S: String): Extended
  function FloatToStr(const Value: Extended): String

  function HTMLValues(const HTML : String; ABegin, AEnd, ItemBegin, ItemEnd : String; ValDelim : String; var Pos : Integer) : String
  function HTMLValues2(const HTML : String; ABegin, AEnd, ItemBegin, ItemEnd : String; ValDelim : String; var Pos : Integer) : String
  function TextBetween(const HTML : String; ABegin, AEnd : String; doLineBreaks : Boolean; var Pos : Integer) : String
  function HTMLToText(const HTML : String) : String

  procedure ShowMessage(const Msg, Head : String)
*)

const
 pauseBeforeLoad = 1000; // Pause before loading (in millisecond)

//Some useful constants
const
 //Script types
 stMovies = 0;
 stPeople = 1;
 stPoster = 2;
 
 //Script modes
 smSearch = 0;
 smNormal = 1;
 smPoster = 2;
 
 //Parse results
 prError     = 0;
 prFinished  = 1;
 prList      = 2;
 prListImage = 3;
 prDownload  = 4;
 
 //Movie fields
 mfURL         = 0;
 mfTitle       = 1;
 mfOrigTitle   = 2;
 mfAka         = 3;
 mfYear        = 4;
 mfGenre       = 5;
 mfCategory    = 6;
 mfCountry     = 7;
 mfStudio      = 8;
 mfMPAA        = 9;
 mfRating      = 10;
 mfTags        = 11;
 mfTagline     = 12;
 mfDescription = 13;
 mfDuration    = 14;
 mfFeatures    = 15;

 
 //People fields
 pfURL        = 0;
 pfName       = 1;
 pfTransName  = 2;
 pfAltNames   = 3;
 pfBirthday   = 4;
 pfBirthplace = 5;
 pfGenre      = 6;
 pfBio        = 7;
 pfDeathDate  = 8;
 
 //Credits types
 ctActors    = 0;
 ctDirectors = 1;
 ctWriters   = 2;
 ctComposers = 3;
 ctProducers = 4;
 
 //Script data

 SCRIPT_VERSION = '0.1.1.0';
 SCRIPT_NAME    = 'EGAFD_Movie';
 SCRIPT_DESC    = '[EN] Get movie information from EGAFD.com';
 SCRIPT_LANG    = $09; //English
 SCRIPT_TYPE    = stMovies;
 
 BASE_URL    = 'http://www.egafd.com';
 RATING_NAME = 'ADE';

 SEARCH_STR  = 'http://www.egafd.com';
 
 CODE_PAGE   = 0; //Use 0 for Autodetect

 
 //OPTIONS
 //For Example, Not Work:
 GET_THEMES  = True; //Set to False if you do not want Themes in Category field 
                    //For Example, Not Work
 GET_REVIEW  = True; //For Example, Not Work
 GET_CREDITS = True; //For Example, Not Work
 
 //Work:
 GET_DIRECTOR = True; //Set to True if you do want to DIRECTOR in Comment field OR 
                    //False if you do not want to DIRECTOR in Comment field 
 GET_AKAS = True; //Set to True if you do want to FullAKAS information in Comment field OR 
                    //False if you do not want to FullAKAS information in Comment field 
 GET_EGAFD = True; //Set to True if you do want to EGAFD in Comment field OR 
                    //False if you do not want to EGAFD in Comment field 
 GET_EGAFD_ID = False; //Set to True if you do want to EGAFD_ID in Comment field OR 
                    //False if you do not want to EGAFD_ID in Comment field 
 GET_COMMENT = True; //Set to True if you do want to COMMENT in Comment field OR 
                    //False if you do not want to COMMENT in Comment field 
 
//------------------------ Global variables ---------------------//

//Global variables
var
 Mode : Byte;
 PosterURL, URLSearch, firstStr, nMovie : String;
 tabXpage, tabSeenXPage : TwideArray;
 A, I : integer;

//----------------------------- Functions ------------------------//
	
//Functions
function GetScriptVersion : String;
begin
 Result := SCRIPT_VERSION;
end;
 
function GetScriptName : String;
begin
 Result :=  SCRIPT_NAME;
end;

function GetScriptDesc : String;
begin
 Result := SCRIPT_DESC;
end;

function GetRatingName : String;
begin
 Result := RATING_NAME;
end;

function GetScriptLang: Cardinal;
begin
 Result := SCRIPT_LANG;
end;

function GetCodePage : Cardinal;
begin
 Result := CODE_PAGE;
end;

function GetBaseURL : AnsiString;
begin
 Result := BASE_URL;
end;

function GetDownloadURL : AnsiString;
var Coun : integer;
    ifLetter : string;

begin
    // Initialisation Variables Tab
		TabXPage :=     ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'
                    ,'p','q','r','s','t','u','v','w','x','y','z','0'];
    TabSeenXPage := ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'
                    ,'0','0','0','0','0','0','0','0','0','0','0','0'];
                    
    nMovie := GetFieldValue(2);
		firstStr := LowerCase(Copy(nMovie, 1, (Length(nMovie)-(Length(nMovie)-1))));
		logMessage('1 ere LETTRE : ' + firstStr);
		
		For Coun := 0 to 25 do
		  Begin
		  If firstStr = TabXPage[Coun] then ifLetter := 'OK';
		  end;
		If ifLetter <> 'OK' then begin
		firstStr := '0';
		ifLetter := '';
		end;
		  
		URLSearch := 'http://www.egafd.com/films/index.php/index/' + lowercase(firstStr);
		LogMessage('URL page recherche : '+ URLSearch);
		
		
		Result := URLSearch;
		
end;

function GetScriptType : Byte;
begin
 Result := SCRIPT_TYPE;
end;

function GetCurrentMode : Byte;
begin
 Result := Mode;
end;

//---------- Procedure actuaTabSeenXPage ----------//
procedure ActuaTabSeenXPage (Letter : String);
Begin
  For A :=  Low(TabXPage) to High(TabXPage) do
    Begin
      If TabXPage[A] = Letter then TabSeenXPage[A] := '1';
    end;
  A := 0;
end;

//---------- Function RemoveTagsEx ---------------//
function RemoveTagsEx(AText : String) : String;
var
	B, E : Integer;
begin
	Result	:= AText;
	B		:= PosFrom('<', Result, 1);
	E		:= PosFrom('>', Result, B);

	while (B > 0) AND (B < E) do begin
		Delete(Result, B, E - B + 1);
		B	:= Pos('<', Result);
		E	:= Pos('>', Result);
	end;
end;

//---------- procedure FindPoster ---------------//
procedure FindPoster(HTML : String);

var
 curPos, EndPos : Integer;
begin
LogMessage('procedure Findposter en cours')
// curPos := Pos('<span class="Item_ViewTitle">Box Cover</span>', HTML);
// curPos := PosFrom('<img src="', HTML, curPos);
// curPos := curPos + Length('<img src="');
// EndPos := PosFrom('"', HTML, curPos);
 
// PosterURL := Copy(HTML, curPos, EndPos - curPos);
end;
 
//---------- Procedure ParseMovie ----------//

//  - after searchlist selection this function is called with the new html content

procedure ParseMovie(MovieURL : String; HTML : String);

var
 curPos, EndPos, PosStart, PosEnd : Integer;
 actPosStart, actPosEnd, UrlPosStart,UrlPosEnd,debug_pos1:Integer;
 URL, Studio, Name, Role, Lien, Lien1, Lien2, Countries, dbgstrg, 
 dbgstrg1, dbgstrg2, fullinfo, endListStr : String;

begin
    
	AddCustomFieldValueByName('EGAFD Version', SCRIPT_NAME + ' ' + SCRIPT_VERSION);
    AddFieldValue(mfURL, MovieURL);
	dbgstrg:= StringReplace( MovieURL, BASE_URL + '/films/details.php/id/', '', false, false, false );
    AddCustomFieldValueByName('EGAFD ID',dbgstrg);
    LogMessage('EGAFD ID: ' + dbgstrg);
	if GET_EGAFD_ID then
	fullinfo := fullinfo + 'EGAFD ID: '+ dbgstrg + '    ';
	LogMessage(fullinfo);
    LogMessage('Page parsing started');
    EndPos := 1;
	
	
    //Check for title. No orig. title info present, so duplicate..
   dbgstrg:= TextBetween(HTML, '<title>', '</title>', False, EndPos);
   LogMessage('Title: ' + dbgstrg);

	If dbgstrg <> '' then begin
   		AddFieldValue(mfOrigTitle,dbgstrg);
   		AddFieldValue(mfTitle,dbgstrg);
		end;
		
   ///// Internal Part <Titles>
	//DVD - Dvds...
	//pull ~DVD~ from 'Titles'
   dbgstrg := '';
   LogMessage('getting dvd studios')
   Curpos := Pos('<h1><a href="/films/index.php/index/' ,HTML);
   EndPos := curPos;
	while (curPos > 0) AND (curPos < PosFrom('<th>Details</th>', HTML, EndPos)) do begin
		EndPos := curPos;
        actPosStart := PosFrom('<img src="/images/dvd.gif" width="25" height="9" alt="', HTML, EndPos);
        actPosEnd := PosFrom('available"></h1>', HTML, actPosStart);
        dbgstrg := TextBetween(HTML, '<img src="/images/dvd.gif" width="25" height="9" alt="', 'available"></h1>', true, actPosStart);
		LogMessage('DVD: ' + dbgstrg); 
		debug_pos1:=Pos('(',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);
		LogMessage(dbgstrg);
	If dbgstrg <> '' then AddFieldValue(mfCategory, dbgstrg);
        curpos := PosFrom('<img src="/images/dvd.gif" width="25" height="9" alt="', HTML, actPosEnd);	
	end;     
	
	//MPAA - Mpaa...
	//pull ~MPAA~ from 'Titles'
   dbgstrg := '';
   LogMessage('getting mpaas')
   Curpos := Pos('<h1><a href="/films/index.php/index/' ,HTML);
   EndPos := curPos;
	while (curPos > 0) AND (curPos < PosFrom('<th>Details</th>', HTML, EndPos)) do begin
		EndPos := curPos;
        actPosStart := PosFrom('.gif" width="20" height="16" alt="', HTML, EndPos);
        actPosEnd := PosFrom('</h1>', HTML, actPosStart);
        dbgstrg := TextBetween(HTML, '.gif" width="20" height="16" alt="', '</h1>', true, actPosStart);
		LogMessage('MPAA: ' + dbgstrg);
		debug_pos1:=Pos('(',dbgstrg);
        if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);
		debug_pos1:=Pos('">',dbgstrg);
        if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);
		LogMessage(dbgstrg);
	If dbgstrg <> '' then AddFieldValue(mfMPAA, dbgstrg);
        curpos := PosFrom('.gif" width="20" height="16" alt="', HTML, actPosEnd);	
	end;	
  ///// End of Internal Part <Titles>
	
  ///// Part <Details> :
   If  Pos('<th>Details</th>', HTML) > 0 then begin
    //Year
    //pull ~Year~ from Part 'Details'
   dbgstrg := '';
   CurPos := Pos('<td valign="top">Released: ', HTML);
   endpos := CurPos;
	  LogMessage('getting year');
	  dbgstrg := TextBetween(HTML, '<td valign="top">Released: </td><td width="100%" class="notes">', '</td>', False, CurPos);
	  LogMessage('YEAR:' + dbgstrg);
		debug_pos1:=Pos('?',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1); 
		dbgstrg := StringReplace(dbgstrg, 'c. ', '', true, false, true);
		if Pos(', ', dbgstrg) = 1 then Delete(dbgstrg, 1, 2);
		LogMessage(dbgstrg);
	If dbgstrg <> '' then AddFieldValue(mfYear,dbgstrg);
   
    //Director
    //pull ~Director~ from Part 'Details'
   dbgstrg := '';
   curPos := Pos('<td valign="top">Director: ', HTML);
   EndPos := curPos;
      LogMessage('getting Director');
      dbgstrg:= TextBetween(HTML, '<td valign="top">Director: </td><td width="100%" class="notes">', '</td>', False, CurPos);
	  AddCustomFieldValueByName('EGAFD Director',dbgstrg);
	  LogMessage('DIRECTOR:' + dbgstrg);	
		debug_pos1:=Pos('(',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);				
		debug_pos1:=Pos(', ',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);
		debug_pos1:=Pos(' as ',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);
		debug_pos1:=Pos(' aas ',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);		
		debug_pos1:=Pos('/',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);				
		debug_pos1:=Pos(' and ',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);    
		debug_pos1:=Pos('?',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);	
		debug_pos1:=Pos('or ',dbgstrg);
		if debug_pos1 >0 then dbgstrg := Copy(dbgstrg,0,debug_pos1-1);
		LogMessage(dbgstrg);
    If dbgstrg <> '' then AddMoviePerson(dbgstrg, '', '', '', ctDirectors);
		
    //Director
    //pull ~Director~ from Part 'Details'
   dbgstrg := '';
   curPos := Pos('<td valign="top">Director: ', HTML);
   EndPos := curPos;
      LogMessage('getting Director');
      dbgstrg1:= TextBetween(HTML, '<td valign="top">Director: </td><td width="100%" class="notes">', '</td>', False, CurPos);
	  LogMessage('DIRECTOR:' + dbgstrg1);
		debug_pos1:=Pos('?',dbgstrg1);
		if debug_pos1 >0 then dbgstrg1 := Copy(dbgstrg1,0,debug_pos1-1);
	If dbgstrg1 <> '' then
	LogMessage('DIRECTOR:' + dbgstrg1);
	if GET_DIRECTOR then
	   fullinfo:= fullinfo + dbgstrg1 + #13; 
	LogMessage(fullinfo);

    //Notes
	//pull ~Notes~ from Part 'Details'
   dbgstrg := '';
   curpos := Pos('<td valign="top">Notes: ' , HTML);
   EndPos := curPos;
	  LogMessage('Notes')
	  dbgstrg := TextBetween(HTML, '<td valign="top">Notes: </td><td width="100%" class="notes">', '</td>', False, CurPos);
	  LogMessage('Notes :' + dbgstrg);
	If dbgstrg <> '' then AddFieldValueXML('tagline', dbgstrg);	
	
	//pull ~Countries~ from 'Details' 

	Countries := ''
	
	if Pos('Australia', dbgstrg) > 0 then Countries := Countries + 'Australia, ';
	 dbgstrg := StringReplace(dbgstrg,'Australia','Australia',true,true,true);	
	if Pos('Austria', dbgstrg) > 0 then Countries := Countries + 'Austria, ';
	 dbgstrg := StringReplace(dbgstrg,'Austria','Austria',true,true,true);
	if Pos('Belgium', dbgstrg) > 0 then Countries := Countries + 'Belgium, ';
	 dbgstrg := StringReplace(dbgstrg,'Belgium','Belgium', true, true, true);
	//if Pos('Brazil', dbgstrg) > 0 then Countries := Countries + 'Brazil, ';
	 //dbgstrg := StringReplace(dbgstrg,'Brazil','Brazil', true, true, true);
	if Pos('Bulgaria', dbgstrg) > 0 then Countries := Countries + 'Bulgaria, ';
	 dbgstrg := StringReplace(dbgstrg,'Bulgaria','Bulgaria',true,true,true);	
	if Pos('Canada', dbgstrg) > 0 then Countries := Countries + 'Canada, ';
	 dbgstrg := StringReplace(dbgstrg,'Canada','Canada', true, true, true);
	if Pos('Colombia', dbgstrg) > 0 then Countries := Countries + 'Colombia, ';
	 dbgstrg := StringReplace(dbgstrg,'Colombia','Colombia', true, true, true);
	if Pos('Croatia', dbgstrg) > 0 then Countries := Countries + 'Croatia, ';
	 dbgstrg := StringReplace(dbgstrg,'Croatia','Croatia', true, true, true);
	if Pos('Cuba', dbgstrg) > 0 then Countries := Countries + 'Cuba, ';
	 dbgstrg := StringReplace(dbgstrg,'Cuba','Cuba', true, true, true);
	if Pos('Czech', dbgstrg) > 0 then Countries := Countries + 'Czech, ';
	 dbgstrg := StringReplace(dbgstrg,'Czech','Czech',true,true,true);
	if Pos('Denmark', dbgstrg) > 0 then Countries := Countries + 'Denmark, ';
	 dbgstrg := StringReplace(dbgstrg,'Denmark','Denmark',true,true,true);
	if Pos('Finland', dbgstrg) > 0 then Countries := Countries + 'Finland, ';
	 dbgstrg := StringReplace(dbgstrg,'Finland','Finland', true, true, true);
	if Pos('France', dbgstrg) > 0 then Countries := Countries + 'France, ';
	 dbgstrg := StringReplace(dbgstrg,'France','France',true,true,true);
	if Pos('Germany', dbgstrg) > 0 then Countries := Countries + 'Germany, ';
	 dbgstrg := StringReplace(dbgstrg,'Germany','Germany',true,true,true);	
	if Pos('Greece', dbgstrg) > 0 then Countries := Countries + 'Greece, ';
	 dbgstrg := StringReplace(dbgstrg,'Greece','Greece', true, true, true);
	if Pos('Hong Kong', dbgstrg) > 0 then Countries := Countries + 'Hong Kong, ';
	 dbgstrg := StringReplace(dbgstrg,'Hong Kong','Hong Kong',true,false,true);
	if Pos('Hungary', dbgstrg) > 0 then Countries := Countries + 'Hungary, ';
	 dbgstrg := StringReplace(dbgstrg,'Hungary','Hungary',true,false,true);
	if Pos('Ireland', dbgstrg) > 0 then Countries := Countries + 'Ireland, ';
	 dbgstrg := StringReplace(dbgstrg,'Ireland','Ireland', true, true, true);
	if Pos('Italy', dbgstrg) > 0 then Countries := Countries + 'Italy, ';
	 dbgstrg := StringReplace(dbgstrg,'Italy','Italy',true,true,true);
	if Pos('Japan', dbgstrg) > 0 then Countries := Countries + 'Japan, ';
	 dbgstrg := StringReplace(dbgstrg,'Japan','Japan',true,true,true);
	if Pos('Netherlands', dbgstrg) > 0 then Countries := Countries + 'Netherlands, ';
	 dbgstrg := StringReplace(dbgstrg,'Netherlands','Netherlands',true,true,true);
	if Pos('New Zealand', dbgstrg) > 0 then Countries := Countries + 'New Zealand, ';
	 dbgstrg := StringReplace(dbgstrg, 'New Zealand', 'New Zealand', true, true, true);
	if Pos('Norway', dbgstrg) > 0 then Countries := Countries + 'Norway, ';
	 dbgstrg := StringReplace(dbgstrg,'Norway','Norway',true,true,true);
	if Pos('Poland', dbgstrg) > 0 then Countries := Countries + 'Poland, ';
	 dbgstrg := StringReplace(dbgstrg,'Poland','Poland',true,true,true);
	if Pos('Portugal', dbgstrg) > 0 then Countries := Countries + 'Portugal, ';
	 dbgstrg := StringReplace(dbgstrg,'Portugal','Portugal',true,true,true);
	if Pos('Romania', dbgstrg) > 0 then Countries := Countries + 'Romania, ';
	 dbgstrg := StringReplace(dbgstrg,'Romania','Romania',true,true,true); 
	if Pos('Russia', dbgstrg) > 0 then Countries := Countries + 'Russia, ';
	 dbgstrg := StringReplace(dbgstrg,'Russia','Russia',true,true,true); 
	if Pos('Slovakia', dbgstrg) > 0 then Countries := Countries + 'Slovakia, ';
	 dbgstrg := StringReplace(dbgstrg,'Slovakia','Slovakia',true,true,true);
	if Pos('Slovenia', dbgstrg) > 0 then Countries := Countries + 'Slovenia, ';
	 dbgstrg := StringReplace(dbgstrg,'Slovenia','Slovenia', true, true, true);		
	if Pos('Spain', dbgstrg) > 0 then Countries := Countries + 'Spain, ';
	 dbgstrg:= StringReplace(dbgstrg,'Spain','Spain',true,true,true);
	if Pos('Sweden', dbgstrg) > 0 then Countries := Countries + 'Sweden, ';
	 dbgstrg := StringReplace(dbgstrg,'Sweden','Sweden',true,true,true);
	if Pos('Switzerland', dbgstrg) > 0 then Countries := Countries + 'Switzerland, ';
	 dbgstrg := StringReplace(dbgstrg,'Switzerland','Switzerland',true,true,true);
	if Pos('Turkey', dbgstrg) > 0 then Countries := Countries + 'Turkey, ';
	 dbgstrg := StringReplace(dbgstrg,'Turkey','Turkey', true, true, true);
	if Pos('UK', dbgstrg) > 0 then Countries := Countries + 'UK, ';
	 dbgstrg := StringReplace(dbgstrg, 'UK', 'UK', true, true, true);
	if Pos('USA', dbgstrg) > 0 then Countries := Countries + 'USA, ';
	 dbgstrg := StringReplace(dbgstrg, 'USA', 'USA', true, true, true);
	if Pos('Ukrainian', dbgstrg) > 0 then Countries := Countries + 'Ukrainian, ';
	 dbgstrg := StringReplace(dbgstrg,'Ukrainie','Ukrainian',true,true,true);
	if Pos('Yugoslavian', dbgstrg) > 0 then Countries := Countries + 'Yugoslavia, ';
	 dbgstrg := StringReplace(dbgstrg, 'Yugoslavia', 'Yugoslavia', true, true, true);
      
	If Countries <> '' then Addfieldvalue(mfCountry, Countries);
	LogMessage('COUNTRY: ' + Countries);
  
	//pull ~Studio~ from 'Details'   
	
    Studio := ''
	
	if Pos('Alkrys', dbgstrg) > 0 then Studio := Studio + 'Alkrys, ';
	 dbgstrg := StringReplace(dbgstrg,'Alkrys','Alkrys',true,true,true);
	if Pos('Anabolic', dbgstrg) > 0 then Studio := Studio + 'Anabolic, ';
	 dbgstrg := StringReplace(dbgstrg,'Anabolic','Anabolic',true,true,true);
	if Pos('Anais', dbgstrg) > 0 then Studio := Studio + 'Anais, ';
	 dbgstrg := StringReplace(dbgstrg,'Anais','Anais',true,true,true);
	if Pos('Antigua Pictures', dbgstrg) > 0 then Studio := Studio + 'Antigua Pictures, ';
	 dbgstrg := StringReplace(dbgstrg,'Antigua Pictures','Antigua Pictures',true,true,true);
	if Pos('Avo Film', dbgstrg) > 0 then Studio := Studio + 'Avo Film, ';
	 dbgstrg := StringReplace(dbgstrg,'Avo Film','Avo Film',true,true,true);
	if Pos('Beate Uhse', dbgstrg) > 0 then Studio := Studio + 'Beate Uhse, ';
	 dbgstrg := StringReplace(dbgstrg,'Beate Uhse','Beate Uhse',true,true,true);
    if Pos('Black Rose', dbgstrg) > 0 then Studio := Studio + 'Black Rose, ';
	 dbgstrg := StringReplace(dbgstrg,'Black Rose','Black Rose',true,true,true);
	if Pos('Blue One', dbgstrg) > 0 then Studio := Studio + 'Blue One, ';
	 dbgstrg := StringReplace(dbgstrg,'Blue One','Blue One',true,true,true);
	if Pos('Cal Vista', dbgstrg) > 0 then Studio := Studio + 'Cal Vista, ';
	 dbgstrg:= StringReplace(dbgstrg,'Cal Vista','Cal Vista',true,true,true);	
	if Pos('Capital Film', dbgstrg) > 0 then Studio := Studio + 'Capital Film, ';
	 dbgstrg:= StringReplace(dbgstrg,'Capital Film','Capital Film',true,true,true);
	if Pos('City Realex', dbgstrg) > 0 then Studio := Studio + 'City Realex, ';
	 dbgstrg := StringReplace(dbgstrg,'City Realex','City Realex',true,true,true);
	if Pos('Colmax', dbgstrg) > 0 then Studio := Studio + 'Colmax, ';
	 dbgstrg := StringReplace(dbgstrg,'Colmax','Colmax',true,true,true);
	if Pos('Concorde', dbgstrg) > 0 then Studio := Studio + 'Concorde, ';
	 dbgstrg := StringReplace(dbgstrg,'Concorde','Concorde',true,true,true);
	if Pos('Concorde Video', dbgstrg) > 0 then Studio := Studio + 'Concorde Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Concorde Video','Concorde Video',true,true,true);
	if Pos('Daring', dbgstrg) > 0 then Studio := Studio + 'Daring, ';
	 dbgstrg := StringReplace(dbgstrg,'Daring','Daring',true,true,true);
	if Pos('Défi', dbgstrg) > 0 then Studio := Studio + 'Défi, ';
	 dbgstrg := StringReplace(dbgstrg,'Défi','Défi',true,true,true);
	if Pos('DBM', dbgstrg) > 0 then Studio := Studio + 'DBM, ';
	 dbgstrg := StringReplace(dbgstrg,'DBM','DBM',true,true,true);
	if Pos('Diva Futura', dbgstrg) > 0 then Studio := Studio + 'Diva Futura, ';
	 dbgstrg := StringReplace(dbgstrg,'Diva Futura','Diva Futura',true,true,true);
	if Pos(' Diva Media', dbgstrg) > 0 then Studio := Studio + ' Diva Media, ';
   	 dbgstrg := StringReplace(dbgstrg,'Diva Media','Diva Media',true,true,true);
	if Pos('DVD Plum', dbgstrg) > 0 then Studio := Studio + 'DVD Plum, ';
	 dbgstrg := StringReplace(dbgstrg,'DVD Plum','DVD Plum',true,true,true);
	if Pos('Eromaxx', dbgstrg) > 0 then Studio := Studio + 'Eromaxx, ';
	 dbgstrg := StringReplace(dbgstrg,'Eromaxx','Eromaxx',true,true,true);
	if Pos('Erotic Media', dbgstrg) > 0 then Studio := Studio + 'Erotic Media, ';
	 dbgstrg := StringReplace(dbgstrg,'Erotic Media','Erotic Media',true,true,true);
	if Pos('Erotic Planet', dbgstrg) > 0 then Studio := Studio + 'Erotic Planet, ';
	 dbgstrg := StringReplace(dbgstrg,'Erotic Planet','Erotic Planet',true,true,true);
	if Pos('Eurotique', dbgstrg) > 0 then Studio := Studio + 'Eurotique, ';
	 dbgstrg := StringReplace(dbgstrg,'Eurotique','Eurotique',true,true,true);
	if Pos('Euro Video', dbgstrg) > 0 then Studio := Studio + 'Euro Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Euro Video','Euro Video',true,true,true);
	if Pos('Evil Angel', dbgstrg) > 0 then Studio := Studio + 'Evil Angel, ';
	 dbgstrg := StringReplace(dbgstrg,'Evil Angel','Evil Angel',true,true,true);
	if Pos('Figoro', dbgstrg) > 0 then Studio := Studio + 'Figoro, ';
	 dbgstrg := StringReplace(dbgstrg,'Figoro','Figoro',true,true,true);
	if Pos('FM Video', dbgstrg) > 0 then Studio := Studio + 'FM Video, ';
	 dbgstrg := StringReplace(dbgstrg,'FM Video','FM Video',true,true,true);
	if Pos('Fun Video', dbgstrg) > 0 then Studio := Studio + 'Fun Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Fun Video','Fun Video',true,true,true);
	if Pos('Goldlight', dbgstrg) > 0 then Studio := Studio + 'Goldlight, ';
	 dbgstrg := StringReplace(dbgstrg,'Goldlight','Goldlight',true,true,true);
	if Pos('Heatwave', dbgstrg) > 0 then Studio := Studio + 'Heatwave, ';
	 dbgstrg := StringReplace(dbgstrg,'Heatwave','Heatwave',true,true,true);
	if Pos('IFG', dbgstrg) > 0 then Studio := Studio + 'IFG, ';
	 dbgstrg := StringReplace(dbgstrg,'IFG','IFG',true,true,true);
	if Pos('In-X-Cess', dbgstrg) > 0 then Studio := Studio + 'In-X-Cess, ';
	 dbgstrg := StringReplace(dbgstrg,'In-X-Cess','In-X-Cess',true,true,true);
	if Pos('Jewel Video', dbgstrg) > 0 then Studio := Studio + 'Jewel Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Jewel Video','Jewel Video',true,true,true);
	if Pos('JTC', dbgstrg) > 0 then Studio := Studio + 'JTC, ';
	 dbgstrg := StringReplace(dbgstrg,'JTC','JTC',true,true,true);
	if Pos('Juwel Video', dbgstrg) > 0 then Studio := Studio + 'Juwel Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Juwel Video','Juwel Video',true,true,true);
	if Pos('Legal Pink (Playhouse)', dbgstrg) > 0 then Studio := Studio + 'Legal Pink (Playhouse), ';
	 dbgstrg := StringReplace(dbgstrg,'Legal Pink (Playhouse)','Legal Pink (Playhouse)',true,true,true);
	if Pos('Legend', dbgstrg) > 0 then Studio := Studio + 'Legend, ';
	 dbgstrg := StringReplace(dbgstrg,'Legend','Legend',true,true,true);
	if Pos('Love Time', dbgstrg) > 0 then Studio := Studio + 'Love Time, ';
	 dbgstrg := StringReplace(dbgstrg,'Love Time','Love Time',true,true,true);
	if Pos('Love Video', dbgstrg) > 0 then Studio := Studio + 'Love Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Love Video','Love Video',true,true,true);
	if Pos('Lux Video', dbgstrg) > 0 then Studio := Studio + 'Lux Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Lux Video','Lux Video',true,true,true);	
	if Pos('Luxx Video', dbgstrg) > 0 then Studio := Studio + 'Luxx Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Luxx Video','Luxx Video',true,true,true);
	if Pos('Magma', dbgstrg) > 0 then Studio := Studio + 'Magma, ';
	 dbgstrg := StringReplace(dbgstrg,'Magma','Magma',true,true,true);
	if Pos('Media Partners', dbgstrg) > 0 then Studio := Studio + 'Media Partners, ';
	 dbgstrg := StringReplace(dbgstrg,'Media Partners','Media Partners',true,true,true);
	if Pos('Metabolic', dbgstrg) > 0 then Studio := Studio + 'Metabolic, ';
	 dbgstrg := StringReplace(dbgstrg,'Metabolic','Metabolic',true,true,true);
	if Pos('M.G.R. Communications', dbgstrg) > 0 then Studio := Studio + 'M.G.R. Communications, ';
	 dbgstrg := StringReplace(dbgstrg,'M.G.R. Communications','M.G.R. Communications',true,true,true);
	if Pos('MMV', dbgstrg) > 0 then Studio := Studio + 'MMV, ';
	 dbgstrg := StringReplace(dbgstrg,'MMV','MMV',true,true,true);
	if Pos('Moonlight', dbgstrg) > 0 then Studio := Studio + 'Moonlight, ';
	 dbgstrg := StringReplace(dbgstrg,'Moonlight','Moonlight',true,true,true);
	if Pos('Negro & Azul', dbgstrg) > 0 then Studio := Studio + 'Negro & Azul, ';
	 dbgstrg := StringReplace(dbgstrg,'Negro & Azul','Negro & Azul',true,true,true);
	if Pos('Negro y Azul', dbgstrg) > 0 then Studio := Studio + 'Negro y Azul, ';
	 dbgstrg := StringReplace(dbgstrg,'Negro y Azul','Negro y Azul',true,true,true);
	if Pos('New Machine', dbgstrg) > 0 then Studio := Studio + 'New Machine, ';
	 dbgstrg := StringReplace(dbgstrg,'New Machine','New Machine',true,true,true);
	if Pos('New Sensations', dbgstrg) > 0 then Studio := Studio + 'New Sensations, ';
	 dbgstrg := StringReplace(dbgstrg,'New Sensations','New Sensations',true,true,true);
	if Pos('Paradise Film', dbgstrg) > 0 then Studio := Studio + 'Paradise Film, ';
	 dbgstrg := StringReplace(dbgstrg,'Paradise Film','Paradise Film',true,true,true);
	if Pos('Pinko', dbgstrg) > 0 then Studio := Studio + 'Pinko, ';
	 dbgstrg := StringReplace(dbgstrg,'Pinko','Pinko',true,true,true);
	if Pos('Piranya Film', dbgstrg) > 0 then Studio := Studio + 'Piranya Film, ';
	 dbgstrg := StringReplace(dbgstrg,'Piranya Film','Piranya Film',true,true,true);
	if Pos('Pleasure', dbgstrg) > 0 then Studio := Studio + 'Pleasure, ';
	 dbgstrg := StringReplace(dbgstrg,'Pleasure','Pleasure',true,true,true);
	if Pos('Private', dbgstrg) > 0 then Studio := Studio + 'Private, ';
	 dbgstrg := StringReplace(dbgstrg,'Private','Private',true,true,true);
	if Pos('PVP', dbgstrg) > 0 then Studio := Studio + 'PVP, ';
	 dbgstrg := StringReplace(dbgstrg,'PVP','PVP',true,true,true);
	if Pos('Pure Play', dbgstrg) > 0 then Studio := Studio + 'Pure Play, ';
	 dbgstrg := StringReplace(dbgstrg,'Pure Play','Pure Play',true,true,true);
	if Pos('Ragtime', dbgstrg) > 0 then Studio := Studio + 'Ragtime, ';
	 dbgstrg := StringReplace(dbgstrg,'Ragtime','Ragtime',true,true,true);
	if Pos('Red Light', dbgstrg) > 0 then Studio := Studio + 'Red Light, ';
	 dbgstrg := StringReplace(dbgstrg,'Red Light','Red Light',true,true,true);
	if Pos('Red Light Video', dbgstrg) > 0 then Studio := Studio + 'Red Light Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Red Light Video','Red Light Video',true,true,true);
	if Pos('Ribu', dbgstrg) > 0 then Studio := Studio + 'Ribu, ';
	 dbgstrg := StringReplace(dbgstrg,'Ribu','Ribu',true,true,true);
	if Pos('SAP', dbgstrg) > 0 then Studio := Studio + 'SAP, ';
	 dbgstrg := StringReplace(dbgstrg,'SAP','SAP',true,true,true);
	if Pos('S.A.P', dbgstrg) > 0 then Studio := Studio + 'S.A.P, ';
	 dbgstrg := StringReplace(dbgstrg,'S.A.P','S.A.P',true,true,true);
	if Pos('Scanbox', dbgstrg) > 0 then Studio := Studio + 'Scanbox, ';
	 dbgstrg := StringReplace(dbgstrg,'Scanbox','Scanbox',true,true,true);
	if Pos('Sin City', dbgstrg) > 0 then Studio := Studio + 'Sin City, ';
	 dbgstrg := StringReplace(dbgstrg,'Sin City','Sin City',true,true,true);
	if Pos('Sins Pictures', dbgstrg) > 0 then Studio := Studio + 'Sins Pictures, ';
	 dbgstrg := StringReplace(dbgstrg,'Sins Pictures','Sins Pictures',true,true,true);
	if Pos('Showtime', dbgstrg) > 0 then Studio := Studio + 'Showtime, ';
	 dbgstrg := StringReplace(dbgstrg,'Showtime','Showtime',true,true,true);
	if Pos('Stars Pictures', dbgstrg) > 0 then Studio := Studio + 'Stars Pictures, ';
	 dbgstrg := StringReplace(dbgstrg,'Stars Pictures','Stars Pictures',true,true,true);
	if Pos('Studio X', dbgstrg) > 0 then Studio := Studio + 'Studio X, ';
	 dbgstrg := StringReplace(dbgstrg,'Studio X','Studio X',true,true,true);
	if Pos('Tabu', dbgstrg) > 0 then Studio := Studio + 'Tabu, ';
	 dbgstrg := StringReplace(dbgstrg,'Tabu','Tabu',true,true,true);
	if Pos('Tabu Love', dbgstrg) > 0 then Studio := Studio + 'Tabu Love, ';
	 dbgstrg := StringReplace(dbgstrg,'Tabu Love','Tabu Love',true,true,true);	
	if Pos('Tip Top', dbgstrg) > 0 then Studio := Studio + 'Tip Top, ';
	 dbgstrg := StringReplace(dbgstrg,'Tip Top','Tip Top',true,true,true);
	if Pos('Top Line', dbgstrg) > 0 then Studio := Studio + 'Top Line, ';
	 dbgstrg := StringReplace(dbgstrg,'Top Line','Top Line',true,true,true);
	if Pos('Top Line Video', dbgstrg) > 0 then Studio := Studio + 'Top Line Video, ';
	 dbgstrg := StringReplace(dbgstrg,'Top Line Video','Top Line Video',true,true,true);
	if Pos('TopLine Video', dbgstrg) > 0 then Studio := Studio + 'TopLine Video, ';
	 dbgstrg := StringReplace(dbgstrg,'TopLine Video','TopLine Video',true,true,true);
	if Pos('UFA', dbgstrg) > 0 then Studio := Studio + 'UFA, ';
	 dbgstrg := StringReplace(dbgstrg,'UFA','UFA',true,true,true);
	if Pos('UFA Video', dbgstrg) > 0 then Studio := Studio + 'UFA Video, ';
	 dbgstrg := StringReplace(dbgstrg,'UFA Video','UFA Video',true,true,true);
	if Pos('VCA', dbgstrg) > 0 then Studio := Studio + 'VCA, ';
	 dbgstrg := StringReplace(dbgstrg,'VCA','VCA',true,true,true);
	if Pos('VCP', dbgstrg) > 0 then Studio := Studio + 'VCP, ';
	 dbgstrg := StringReplace(dbgstrg,'VCP','VCP',true,true,true);
	if Pos('VCX', dbgstrg) > 0 then Studio := Studio + 'VCX, ';
	 dbgstrg := StringReplace(dbgstrg,'VCX','VCX',true,true,true);
	if Pos('Video Erotico', dbgstrg) > 0 then Studio := Studio + 'Video Erotico, ';
	 dbgstrg := StringReplace(dbgstrg,'Video Erotico','Video Erotico',true,true,true);
	if Pos('Videorama', dbgstrg) > 0 then Studio := Studio + 'Videorama, ';
	 dbgstrg := StringReplace(dbgstrg,'Videorama','Videorama',true,true,true);
	if Pos('Vivid', dbgstrg) > 0 then Studio := Studio + 'Vivid, ';
	 dbgstrg := StringReplace(dbgstrg,'Vivid','Vivid',true,true,true);
	if Pos('VFL', dbgstrg) > 0 then Studio := Studio + 'VFL, ';
	 dbgstrg := StringReplace(dbgstrg,'VFL','VFL',true,true,true);
	if Pos('Video Marc Dorcel', dbgstrg) > 0 then Studio := Studio + 'Video Marc Dorcel, ';
	 dbgstrg := StringReplace(dbgstrg,'Video Marc Dorcel','Video Marc Dorcel',true,true,true);	
	if Pos('VTO', dbgstrg) > 0 then Studio := Studio + 'VTO, ';
	 dbgstrg := StringReplace(dbgstrg,'VTO','VTO',true,true,true);
	if Pos('Wicked', dbgstrg) > 0 then Studio := Studio + 'Wicked, ';
	 dbgstrg := StringReplace(dbgstrg,'Wicked','Wicked',true,true,true);
	if Pos('Xcel', dbgstrg) > 0 then Studio := Studio + 'Xcel, ';
	 dbgstrg := StringReplace(dbgstrg,'Xcel','Xcel',true,true,true);

	If Studio <> '' then Addfieldvalue(mfStudio, Studio);
	LogMessage('STUDIO: ' + Studio);
	
    end;     
   //// End of Part <Details>
   
   ///// Part <Alternate titles>
   If Pos('<th>Alternate Titles</th>', HTML) > 0 then begin
    //AKA - Titles...
   dbgstrg := '';
   LogMessage('getting all titles')
   Curpos := Pos('<th>Alternate Titles</th>' ,HTML);
   EndPos := curPos;
	while (curPos > 0) AND (curPos < PosFrom('<th>Actresses</th>', HTML, EndPos)) do begin
		EndPos := curPos;
        actPosStart := PosFrom('class="flma"', HTML, EndPos);
        actPosEnd := PosFrom('</span>', HTML, actPosStart);
        dbgstrg := Trim(Copy(HTML, (actPosstart + 13), (actPosEnd - actPosStart - 13) ));
		LogMessage('AKA: ' + dbgstrg);
	If dbgstrg <> '' then AddFieldValue(mfAka, dbgstrg);
        curpos := PosFrom('class="flma"', HTML, actPosEnd);
	end;
	
	//AKA - FullAkas...
	//pull ~FullAkas~ from 'Alternate titles'
   dbgstrg1 := '';
   LogMessage('getting all titles information')
   Curpos := Pos('<th>Alternate Titles</th>' ,HTML);
   EndPos := curPos;
	while (curPos > 0) AND (curPos < PosFrom('</ul>', HTML, EndPos)) do begin
		EndPos := curPos;
        actPosStart := PosFrom('<ul class="list">', HTML, EndPos);
        actPosEnd := PosFrom('</ul>', HTML, actPosStart);
		dbgstrg1 := Trim(Copy(HTML, (actPosstart + 17), (actPosEnd - actPosStart - 17) ));
		LogMessage('AKA: ' + dbgstrg1);
	    dbgstrg1 := StringReplace(dbgstrg1, '<li><span class="flma">', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '</span>', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '</li>', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, 'nbsp;', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '&nbsp;', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '<img src="/images/r18.gif" width="20" height="16" alt="R18">', ' R18 ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '<img src="/images/dvd.gif" width="25" height="9" alt="DVD available">', ' DVD ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '<i class="notes">', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '<i>', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '</i>', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '</i>&', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '&', ' ', true, false, true);
		dbgstrg1 := StringReplace(dbgstrg1, '?', ' ', true, false, true);
		if Pos(', ', dbgstrg1) = 1 then Delete(dbgstrg1, 1, 2);
		LogMessage('AKA: ' + dbgstrg1);
	If dbgstrg1 <> '' then AddCustomFieldValueByName('EGAFD FullAkas', dbgstrg1);
	LogMessage('AKA: ' + dbgstrg1);
        curpos := PosFrom('class="flma"', HTML, actPosEnd);
	end;
	
	//AKA - Titles...
   dbgstrg := '';
   LogMessage('getting all titles')
   Curpos := Pos('<th>Alternate Titles</th>' ,HTML);
   EndPos := curPos;
	while (curPos > 0) AND (curPos < PosFrom('<th>Actresses</th>', HTML, EndPos)) do begin
		EndPos := curPos;
        actPosStart := PosFrom('</span>', HTML, EndPos);
        actPosEnd := PosFrom('</li>', HTML, actPosStart);
        //dbgstrg := Trim(Copy(HTML, (actPosstart + 8), (actPosEnd - actPosStart - 8) ));
		//LogMessage('AKA: ' + dbgstrg);
		//dbgstrg1 := StringReplace(dbgstrg, ' -- ', '—', True, True, False);
        //dbgstrg2 := StringReplace(dbgstrg1, ' --- ', '—', True, True, False);
        //dbgstrg := StringReplace(dbgstrg2, '--', '—', True, True, False);
        //dbgstrg1 := StringReplace(dbgstrg, #13#13#13#13, #13#10#13#10, True, True, False);
        //dbgstrg2 := RemoveTagsEx(dbgstrg1);
		dbgstrg2 := Trim(Copy(HTML, (actPosstart + 8), (actPosEnd - actPosStart - 8) ));
		LogMessage('AKA: ' + dbgstrg2);
		dbgstrg2 := StringReplace(dbgstrg2, 'nbsp;', ' ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '&nbsp;', ' ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '<img src="/images/r18.gif" width="20" height="16" alt="R18">', ' R18 ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '<img src="/images/dvd.gif" width="25" height="9" alt="DVD available">', ' DVD ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '<i class="notes">', ' ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '<i>', ' ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '</i>', ' ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '</i>&', ' ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '&', ' ', true, false, true);
		dbgstrg2 := StringReplace(dbgstrg2, '?', ' ', true, false, true);
		if Pos(', ', dbgstrg2) = 1 then Delete(dbgstrg2, 1, 2);
		LogMessage('AKA: ' + dbgstrg2);
	If dbgstrg2 <> '' then
	LogMessage('AKA: ' + dbgstrg2);
	if GET_AKAS then
	   fullinfo := fullinfo + ' ' + dbgstrg2 + '  ';
	   LogMessage(fullinfo);
       curpos := PosFrom('class="flma"', HTML, actPosEnd);
	end;
	end;     
    ///// End of part <Alternate titles>
	
  if GET_COMMENT then
	AddFieldValueXML('comment', ' • • ' + SCRIPT_NAME + ' ' + SCRIPT_VERSION +' • •   ' + fullinfo);
  if GET_EGAFD then
	AddFieldValueXML('comment', ' • • ' + SCRIPT_NAME + ' ' + SCRIPT_VERSION + ' • •   ');
	
   ///// Part <Actresses>
  If Pos('<th>Actresses</th>', HTML) > 0 then begin
    //Cast
   curPos:= Pos('<th>Actresses</th>', HTML);
   LogMessage('Cast readout');
	If pos('<th>Notes</th>', HTML) > 0 then
		endListStr := '<th>Notes</th>'
	else
		endListStr := '>Suppliers</th>';

	if curPos > 0 then    begin
   	EndPos := curPos;
      		while (curPos > 0) AND (curPos < PosFrom(endlistStr, HTML, EndPos)) do begin 
         	EndPos := curPos;

        	// get url
         	UrlPosStart := PosFrom('href="', HTML, EndPos) + 6;
         	UrlPosEnd := PosFrom('">', HTML, UrlPosStart);
         	URL := BASE_URL + Trim(Copy(HTML, UrlPosStart, UrlPosEnd - UrlPosStart));
         	LogMessage(URL);

		// Get Name
		actposstart := urlposend + 2;
		actposend := Posfrom('</a>', HTML, actposstart);
		Name := Trim(Copy(HTML, actposstart, (actposend-actposstart)));
		Logmessage('Name: ' + Name);
 
         	debug_pos1:=Pos('(',Name);
            	if debug_pos1 >0 then Name := Copy(Name,0,debug_pos1-1);
            	LogMessage('Name:' + Name);

				// Role
		Role := '';
		actposStart := actposend;
		
			If (copy(HTML, actposstart + 4, 1)) <> '<' then begin
			actposStart := actposstart -6;
			Role := TextBetween(HTML, '<i>', '</i>', false, actposstart);
			Role := StringReplace(Role, ';', '  - ', true, false, true);
			Role := StringReplace(Role, '/', ' - ', true, false, true);
			Role := StringReplace(Role, 'as?', '', true, false, true);
			Role := StringReplace(Role, '?', '', true, false, true);
            if Pos(', ', Role) = 1 then Delete(Role, 1, 2);
			end;

			debug_pos1:=Pos('(',Role);
            	if debug_pos1 >0 then Role := Copy(Role,0,debug_pos1-1);
            	LogMessage('Role:'+ Role);

			If URL <> '' then
				If Name <> '' then      
            			AddMoviePerson(Trim(Name), '', Role, LowerCase(URL), ctActors);
		  
      curPos := PosFrom('href="', HTML, actPosEnd);
		end; 
	end;
  end; 
   ///// End of Part <Actresses>
	  
   ///// Part <Notes>
	//Notes
	curpos := Pos('<th>Notes</th>', HTML);
	endpos := curpos;
	While (curpos > 0) AND (curpos < Posfrom('>Suppliers</th></tr>', HTML, EndPos)) do begin
		endpos := curpos;
		PosStart := PosFrom('<tr>', HTML, endpos);
		PosEnd := PosFrom('</tr>', HTML, PosStart);
		Lien := Trim(Copy(HTML, (PosStart + 4), (PosEnd - PosStart - 4)));
		Lien1 := StringReplace(Lien, ' -- ', '—', True, True, False);
        Lien2 := StringReplace(Lien1, ' --- ', '—', True, True, False);
        Lien := StringReplace(Lien2, '--', '—', True, True, False);
        Lien1 := StringReplace(Lien, #13#13#13#13, #13#10#13#10, True, True, False);
        Lien2 := RemoveTagsEx(Lien1);
		LogMessage('Notes' + Lien2);
	curpos := posfrom(' class="review">', HTML, posend);
		AddFieldValue(mfDescription, Lien2);
	end;  
   ///// End of Part <Notes>
	
   ///// Part <Suppliers>
	//Suppliers
	Curpos := PosFrom('>Suppliers</th>', HTML, endpos);
		PosStart := PosFrom('>Suppliers</th></tr>', HTML, endpos);
		PosEnd := PosFrom('</table>', HTML, PosStart);
		Lien := Trim(Copy(HTML, (PosStart + 20), (PosEnd - PosStart - 20)));
		LogMessage('Suppliers' + Lien);
		Lien1 := StringReplace(Lien, ' -- ', '—', True, True, False);
		Lien2 := StringReplace(Lien1, ' --- ', '—', True, True, False);
		Lien := StringReplace(Lien2, '--', '—', True, True, False);
		Lien1 := StringReplace(Lien, #13#13#13#13, #13#10#13#10#, True, True, False);
		Lien2 := RemoveTagsEx(Lien1);
		LogMessage('Suppliers' + Lien2);
		debug_pos1:=Pos('Sorry',Lien2);
		if debug_pos1 >0 then Lien2 := Copy(Lien2,0,debug_pos1-1);
		LogMessage(Lien2);
		Lien2 := StringReplace(Lien2, 'FormatPrice1Supplier/Link', '', true, false, true);
		Lien2 := StringReplace(Lien2, 'Preferred currency2:', '', true, false, true);
		Lien2 := StringReplace(Lien2, '(No Preference)', '', true, false, true);
		Lien2 := StringReplace(Lien2, 'UK Pounds (GBP)', '', true, false, true);
		Lien2 := StringReplace(Lien2, 'US Dollars (USD)', '', true, false, true);
		Lien2 := StringReplace(Lien2, 'Euros (EUR)', '', true, false, true);
		if Pos(', ', Lien2) = 1 then Delete(Lien2, 1, 2);
		LogMessage('Suppliers' + Lien2);
	curpos := posfrom('>Suppliers</th></tr>', HTML, posend); 
        AddFieldValueXML('features', Lien2);
   ///// End of Part <Suppliers>
   
end; // End of the routine...


//---------- Procedure SearchResults ----------//
// Create list of movies found during the search
procedure SearchResults (HTML : String);
Var
URLXPage, URLTemp, NameTemp, BeforeName, ifAlias, AliasName, RealName, firstStr2, noteSearch : String;
TabPartTitle : TwideArray;
TabPartSearch : Array[0..1] of string;
nSearch, ifFind, actPosStart, actPosEnd, URLPosStart, URLPosEnd, notPosStart, notPosEnd : Integer;
CurPos, endPos, actalPosStart, actalPosEnd, TypPVD : Integer;

essai : string;

Begin
  ifFind := 0;      //Initialisation variable
  
// Separate parts of title :
    If Pos(#32,nMovie) = 0 then begin
    nSearch := 1;
    TabPartSearch[0] := nMovie;
    end
    else begin
    ExplodeString(nMovie,TabPartTitle,#32);
// Case where option in PVD is 'Title, the' :
    If Copy(TabPartTitle[High(TabPartTitle)-1],Length(TabPartTitle[High(TabPartTitle)-1]),1) = ',' then begin
    TypPVD := 1;
      TabPartTitle[High(TabpartTitle)-1] := Copy(TabPartTitle[High(TabpartTitle)-1], 1, Length(TabpartTitle[High(TabpartTitle)-1])-1);
      For I := Low(TabPartTitle) to (High(TabPartTitle)-1) do
      Begin
      nMovie := '';
        If I = 0 then begin
          nMovie := TabpartTitle[I];
          end
          else begin
          nMovie := nMovie + #32 + TabPartTitle[I];
          end;
      end;
      nMovie := TabPartTitle[High(TabPartTitle)] + #32 + nMovie;
      LogMessage(nMovie);
      firstStr2 := LowerCase(Copy(TabPartTitle[High(TabPartTitle)], 1 ,1));
      end
      else begin
      TypPVD := 0;
      FirstStr2 := LowerCase(Copy(TabPartTitle[1],1,1));
      end;
      For I := Low(TabPartTitle) to High(TabPartTitle) do
      Begin
      TabPartTitle[I] := Lowercase(TabPartTitle[I]);
      end;
      
// Second value to search :
    TabPartSearch [0] := LowerCase(nMovie);
    nSearch := 1;
    If High(TabPartTitle) > 0 then begin
      If Length(TabPartTitle[0]) > 3 then begin
      TabPartSearch [1] := TabPartTitle[0];
      nSearch := 2;
      end
      else
      If (Length(TabPartTitle[0]) < 4) AND (Length(TabPartTitle[1]) > 3) then begin
      TabPartSearch [1] := TabPartTitle[1];
      nSearch := 2;
      end
      else begin
      TabPartSearch [1] := TabPartTitle[0] + #32 + TabPartTitle[1];
      nSearch := 2;
      end;
    LogMessage('Second search : ' + TabPartSearch[1]);
    end;
    LogMessage ('nSearch' + IntToStr(nSearch));
    end;
    
//Search:
  I:= 0;
  
  While (I < nSearch) AND (ifFind = 0) do Begin
  logMessage ('while commence');
     
    Curpos := Pos(TabPartSearch[I], HTML);

    If curpos > 0 then begin
		  endPos := curpos;

      While (curpos > 0) AND (curpos < PosFrom('<h2 class="index">Films:', HTML, Endpos)) do begin
	     endpos := curpos;
	     actPosStart:= PrevPos('>', HTML, curpos);
	     actPosEnd := Posfrom('</', HTML, actposStart);
	     NameTemp := LowerCase(Trim(Copy(HTML, actPosStart+1, (actPosEnd-actPosStart-1))));
	     LogMessage ('NameTemp: '+ NameTemp);
	     LogMessage ('nmovie: '+ nMovie);
	     beforeName := Trim(Copy(HTML, actposStart-3, 1));
		 
	     If copy(NameTemp,1,21) = 'alternative title for' then
        ifAlias := 'True'
	      else
	      ifAlias := '';

		
		  Case NameTemp of
		    nMovie :  Begin                     //Same name as Title
		    //logmessage('Same Movie as title');
		            Case BeforeName of
		              'l' : Begin             //Real Name
		              RealName := NameTemp;
		              AliasName := NameTemp;
		              //Notes
		              notPosStart := Posfrom('/a>', HTML, Curpos);
		              essai :=  copy(HTML, notPosStart+3,1);
		              LogMessage('essai: ' + essai);
		                If copy(HTML, notPosStart+3,1) = #32 then begin
		                logmessage('if begin');
		                  notPosStart := PosFrom('class="notes">', HTML, notPosstart)+14;
		                  notPosEnd := PosFrom('</i>', HTML, notPosStart);
		                  noteSearch := Trim(Copy(HTML, notPosStart, (notPosEnd-notPosStart)));
		                  logmessage(notesearch);
		                  end
		                else begin
		                  noteSearch := '';
		                end;
		              // Get URL
		              urlPosStart := Prevpos('href="', HTML, actposStart);
					        urlPosend := PosFrom('" class="', HTML, urlPosstart);
					        URLTemp := BASE_URL + Trim(Copy(HTML, urlposStart + 6, (urlPosEnd-urlPosStart-6)));
					        AddSearchResult(AliasName, NoteSearch, '', LowerCase(URLTemp),'');
					        actPosEnd := PosFrom('href="', HTML, urlPosEnd);
					        ifFind := 1;
					        LogMessage('TYPE I: Same Title as original movie');
		              end;
		              
		            else begin              // Good Name but Alternative title
		            //logmessage('Good Name but Alias');
		              AliasName := nMovie;
		              // Real Name
		              actalPosStart := Posfrom('alternative title for ', HTML, actposEnd);
					        actalposEnd := Posfrom('</span>', HTML, actalposstart);
					        RealName := Trim(Copy(HTML, (actalposStart + 22), (actalposEnd - actalposStart - 22)));
					        // Name Temp
					        NameTemp := 'Alternative title for ' + RealName;
					        // Get URL
					        urlPosStart := PrevPos('href="', HTML, actPosstart);
					        urlPosend := PosFrom('" class="', HTML, urlPosstart);
					        URLTemp := BASE_URL + Trim(Copy(HTML, urlposStart + 6, (urlPosEnd-urlPosStart-6)));
					        AddSearchResult(AliasName, NameTemp, '', LowerCase(URLTemp),'');
					        actPosEnd := PosFrom('href="', HTML, urlPosEnd);
					        ifFind := 1;
					        LogMessage('TYPE II: Same Title as an alternative title of a Movie');
		              end;
		            end;
		    end;
		    else begin
		      //Part of Name
          If ifAlias <> 'True' then begin
            If Copy(HTML, actPosStart-1,1) <> '>' then actPosStart := PrevPos('>', HTML, curpos);
		      actPosEnd := Posfrom('</', HTML, actposStart);
		      AliasName := Trim(Copy(HTML, actPosStart+1, (actPosEnd-actPosStart-1)));
		      beforeName := Trim(Copy(HTML, actposStart-3, 1));
		    
		        Case beforename of
		          'l' : Begin                   // Title is a part of a real Movie
		              // Real Name
		              RealName := AliasName;
		              logmessage(Realname);
		              logmessage(AliasName);
		              //Notes
		              notPosStart := Posfrom('/a>', HTML, Curpos);
		              essai :=  copy(HTML, notPosStart+3,1);
		              LogMessage('essai: ' + essai);
		                If copy(HTML, notPosStart+3,1) = #32 then begin
		                logmessage('if begin');
		                  notPosStart := PosFrom('class="notes">', HTML, notPosstart)+14;
		                  notPosEnd := PosFrom('</i>', HTML, notPosStart);
		                  noteSearch := Trim(Copy(HTML, notPosStart, (notPosEnd-notPosStart)));
		                  logmessage(notesearch);
		                  end
		                else begin
		                  noteSearch := '';
		                end;
		              // Get URL
		              urlPosStart := Prevpos('href="', HTML, actposStart);
					        urlPosend := PosFrom('" class="', HTML, urlPosstart);
					        URLTemp := BASE_URL + Trim(Copy(HTML, urlposStart + 6, (urlPosEnd-urlPosStart-6)));
					        logmessage(URLTemp);
					        AddSearchResult(RealName, NoteSearch, '', LowerCase(URLTemp),'');
					        ifFind := 1;
					        actPosEnd := PosFrom('href="', HTML, urlPosEnd);
					        logmessage('TYPE III: Part of a Real Name');
		          end;
		        
		        else begin
		              // Real Name
		              actalPosStart := Posfrom('alternative title for ', HTML, actPosEnd);
					        actalPosEnd := Posfrom('</span>', HTML, actalPosStart);
					        RealName := Trim(Copy(HTML, (actalposStart + 10), (actalposEnd - actalposStart - 10)));
					        NameTemp := 'alternative title for ' + Realname;
					        // Get URL
					        urlPosStart := PrevPos('href="', HTML, actPosstart);
					        urlPosend := PosFrom('" class="', HTML, urlPosstart);
					        URLTemp := BASE_URL + Trim(Copy(HTML, urlposStart + 6, (urlPosEnd-urlPosStart-6)));
					        AddSearchResult(AliasName, NameTemp, '', LowerCase(URLTemp),'');
					        actPosEnd := PosFrom('href="', HTML, actalPosEnd);
					        ifFind := 1;
					        Logmessage('TYPE IV: Part of an alternative Name');
		          end;
		        end;
          end  
		      else begin                  // Name is in Alternative title for ......
		              // Alias Name
		              actPosStart := Prevpos('"flma">', HTML, curpos);
		              actPosEnd := Posfrom('</a>', HTML, actPosStart);
		              Aliasname := Trim(Copy(HTML, (actPosStart + 7), (actPosEnd - actPosStart - 7)));
		              Logmessage('AliasName: '+ AliasName);
		              // Real Name
		              actalPosStart := Posfrom('alternative title for ', HTML, actposEnd);
					        actalPosEnd := Posfrom('</span>', HTML, actalposStart);
		              RealName := Trim(Copy(HTML, (actalposStart + 22), (actalposEnd - actalposStart - 22)));
		              LogMessage('RealName: '+ RealName);
		              // Get URL
					        urlPosStart := PrevPos('href="', HTML, actalPosstart);
					        urlPosend := PosFrom('" class="', HTML, urlPosstart);
					        URLTemp := BASE_URL + Trim(Copy(HTML, urlposStart + 6, (urlPosEnd-urlPosStart-6)));
					        AddSearchResult(AliasName, NameTemp, '', LowerCase(URLTemp),'');
					        ifFind := 1;
					        actPosEnd := PosFrom('href="', HTML, urlPosEnd);
					        logmessage('TYPE V: Part find in alternative title for...');
					end;
		    end;
		  end;
	 curpos := PosFrom(TabPartSearch[I], HTML, actPosEnd);
	 end;
  end;
  I := I + 1;
  end;    // End first while

// Add others alphabetics page in prList (who had not seen) if no result in actual page:
  If (ifFind = 0) then begin
    ShowMessage('Try to find movie in another alphabetic page!', 'MOVIE NOT FIND IN THIS PAGE :');
    For A := Low(TabSeenXPage) to High(TabSeenXPage) do
    Begin
      If TabSeenXPage[A] = '0' then begin
        URLXPage := 'http://www.egafd.com/films/index.php/index/' + TabXPage[A];
        AddSearchResult('Search in Alphabetic page',UpperCase(TabXPage[A]),'', URLXPage,'');
      end;
    end;
  end
  else begin
      CurPos := Pos('<title>Films: ', HTML);
      If (Copy(URLSearch, Length(URLSearch), 1)) = (LowerCase(Copy(HTML,Curpos+14,1))) then begin
        URLXPage := 'http://www.egafd.com/films/index.php/index/' + firstStr2;
        AddSearchResult('Search in Alphabetic page',UpperCase(firstStr2),'', URLXPage,'');
        end;
    end;
  
end;


//---------- PARSE PAGE ----------//

function ParsePage(HTML : String; URL : AnsiString) : Cardinal;
Var
StartPos : integer;

begin
   Wait (pauseBeforeLoad);
   LogMessage('Script version:' + SCRIPT_VERSION);

   If Pos('<title>Films: ', HTML) > 0 then begin
    StartPos := Pos('<title>Films: ', HTML);
    firstStr := trim(copy(HTML,StartPos+14,1));
    If firstStr = '#' then firstStr := '0';
    ActuaTabSeenXPage (lowercase(firststr));
    SearchResults(HTML);
    Result := prList;
  end
  else
  If Pos('<th>Details</th>', HTML) > 0 then begin
    Mode := smNormal;  
    ParseMovie(URL, HTML);
    Result := prFinished;
  end
  else
  If Pos('<th>Alternate Titles</th>', HTML) > 0 then begin
    Mode := smNormal;
    ParseMovie(URL, HTML);
    Result := prFinished;
  end
  else
  If Pos('<th>Actresses</th>', HTML) > 0 then begin
    Mode := smNormal;
    ParseMovie(URL, HTML);
    Result := prFinished;
  end
  else
  If Pos('<th>Notes</th>', HTML) > 0 then begin
    Mode := smNormal;
    ParseMovie(URL, HTML);
    Result := prFinished;
  end else
  Result := prError
end;

begin
 Mode := smSearch;
end.