(*
 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_People
 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.msg14854.html#msg14854)
			previous versions posted at forum topic:
			http://www.videodb.info/forum_en/index.php/topic,3021.0.html

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

 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)
*)

  //---------------------- Constants-----------------------

const
 //Script types
 stMovies = 0;
 stPeople = 1; 
 stPoster = 2;
  
 //Script modes
 smSearch   = 0; 
 smNormal   = 1;
 smPhoto    = 2;
 smFinished = 3;
  
 //Parse results
 prError     = 0; 
 prFinished  = 1;
 prList      = 2;
 prListImage = 3;
 prDownload  = 4;
 
 //Download methods
 dmGET  = 0;
 dmPOST = 1;
 
 //Prefix modes
 pmNone   = 0;
 pmEnd    = 1;
 pmBegin  = 2;
 pmRemove = 3; 
  
 //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_People';
 SCRIPT_DESC    = '[EN] Get people information from EGAFD.com';
 SCRIPT_LANG    = $09; //English
 SCRIPT_TYPE    = stPeople;
 
 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_PRA15 = False;   //Set to True if you do want EGAFD in Genre field OR 
                     //Set to False if you do not want EGAFD in Genre field 
 GET_TRANSNAME = False;   
			//Set to True if you do want TRANSNAME in Transname field OR 
                     //Set to False if you do not want TRANSNAME in Transname field 
 GET_ROLE = False;   //Set to True if you do want to ROLE in Filmography field OR 
                     //False if you do not want ROLE in Filmography field 
 GET_PERSONMOVIE = False;   
            //Set to True if you do want to add PERSONMOVIE in Filmography field OR 
                     //False if you do not want to add PERSONMOVIE in Filmography field 
 GET_PERSONMOVIEROLE = False;   
            //Set to True if you do want to add PERSONMOVIEROLE in Filmography field OR 
                     //False if you do not want to add PERSONMOVIEROLE in Filmography field 
 GET_FULLINFO = 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 
 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 ---------------------

var
 Mode : Byte;
 photoURL, URLSearch, firstStr, nomAct : String;
 PartBorn, TabCountry : Twidearray;

//----------------------------- Functions ------------------------

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

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

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

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

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

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


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


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

function GetDownloadMethod : Byte;  //optional
begin
  Result := dmGET;  
end;

function GetPrefixMode : Byte;
begin
 Result := pmBegin;  
end;


function GetDownloadURL : AnsiString;    //obbligatoire

begin

		logMessage('getdownloadurl démarre');

if Mode > 0 then
	begin
		if PhotoURL <> '' then
		begin
			LogMessage('URL de la photo: ' + PhotoURL);
			Result := PhotoURL;
			Exit;
		end;
	end
	else
	begin
	    NomAct := GetFieldValue(pfName);	//Nom actrice
		NomAct := GetFieldValue(mfTitle);	//Nom actrice
		firstStr := Copy(NomAct, 1, (Length(NomAct)-(Length(NomAct)-1)));	//1ère lettre du nom
		logMessage('1 ere LETTRE : ' + firstStr);
		URLSearch := 'http://www.egafd.com/actresses/index.php/index/' + lowercase(firstStr);
		LogMessage('URL page recherche : '+ URLSearch);
		Result := URLSearch;	//Envoi lien vers page de recherche
	end;
end;


(*//------------ Procedure New URL ------------//
procedure NewURL(URLSearch : String);

Begin
	AddFieldValue(pfURL, URLSearch);
	logmessage('url ajouté');
end;
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 parse people -----------------//
procedure ParsePeople(URL : String; HTML : String);

///////////////////////////////////////////////////////////////////////////////////////////////////
var curpos, endpos, PosStart, PosEnd, debug_Pos1 : Integer;					 //
    actPosstart, actposstart2, actPosStart3, actposend, UrlposStart, UrlposEnd, difPos : Integer;//
    Pseudo, Born, Born1, Born2, Lien, URL1, Name, Title, Year, Sites, Sites1,
	Bomis, MSN, MySpace, Wikipedia, Yahoo, Av, Role, Notes, Note, tmpYear, OrigT, fullinfo : String;
    Birthday, DeathDate : string;
    I : integer;//
///////////////////////////////////////////////////////////////////////////////////////////////////

begin
    if GET_PRA15 then
	AddFieldValue(pfGenre, 'EGAFD');
	
	// Write real Name of the web page //
	Curpos := Pos('<html>',HTML);
	if GET_TRANSNAME then
	AddFieldValue(pfTransname,(TextBetween(HTML,'<title>','</title>',False,Curpos)));
		
	// URL: //

	Addfieldvalue(pfURL,URL);

	/////--- Infos in <notes>
  If Pos('<th>Notes</th>', HTML) > 0 Then begin
  
	// BORN: //

	curpos := Pos('<th>Notes</th>', HTML);
	endpos := curpos;
	While (curpos > 0) AND (curpos < Posfrom('</tr>', HTML, EndPos)) do begin
		endpos := curpos;
		PosStart := PosFrom('<td><ul class="list"><li>', HTML, endpos);
		PosEnd := PosFrom('</li></ul></td>', HTML, PosStart);
		Born := Trim(Copy(HTML, (PosStart + 25), (PosEnd - PosStart - 25)));
		Born := Uppercase(Copy(Born,0,1)) + Copy(Born,2, length(Born)-1) + #13;
		LogMessage('BORN :' + Born);
		curpos := posfrom('<td><ul class="list"><li>', HTML, posend);
		Born1 := StringReplace(Born, ' -- ', '—', True, True, False);
        Born2 := StringReplace(Born1, ' --- ', '—', True, True, False);
        Born := StringReplace(Born2, '--', '—', True, True, False);
        Born1 := StringReplace(Born, #13#13#13#13, #13#10#13#10, True, True, False);
        Born2 := RemoveTagsEx(Born1);
		LogMessage('Notes' + Born2);
(*		//AddFieldValue(pfBirthplace, Born);
		//LogMessage('BORN :' + Born);
		//AddFieldValue(pfBirthplace, Born2);
*)		
		LogMessage('BORN :' + Born2);
	end;

	/// BirthDay:

	ExplodeString(Born, PartBorn, #46);

	If High(partBorn) > 0 then
	Begin
	  logmessage('birhtday commence')
    For I := Low(partBorn) to High(partBorn) do
    Begin
    PartBorn[I] := Trim(partBorn[I]);
    End;

	 For I := Low(partBorn) to High(partBorn) do
	 Begin
		If (Lowercase(partBorn[I]) = 'b') OR (Copy(partBorn[I], length(partBorn[I])-2,3) = ', b') OR (Copy(partBorn[I], length(partBorn[I])-2,3) = '? b') then
     BirthDay := '01/01/' + Copy(partBorn[I+1], 0, 4);
	    If (Lowercase(partBorn[I]) = 'c') then
     BirthDay := '01/01/' + Copy(partBorn[I+1], 0, 4);
		end;
	end;
	
	If BirthDay <> '' then AddFieldValue(pfBirthday, BirthDay);

  // Birthplace:
  
    TabCountry := ['Albanian', 'Austrian', 'Belgian', 'Brazilian', 'Bulgarian', 'Belarusian'
				, 'Canadian', 'Cuban', 'Colombian', 'Croatian', 'Cyprus', 'Czech', 'Dutch'
				, 'Danish', 'English', 'Estonian', 'Ecuadorian', 'Finnish', 'French', 'German'
				, 'Hungarian', 'Indian', 'Irish', 'Italian', 'Kyrgyzstan', 'Latvian'
				, 'Lithuanian', 'Moldovan', 'Norwegian', 'Portuguese', 'Polish', 'Romanian'
				, 'Russian', 'Slovak', 'Slovenian', 'Spanish', 'Swiss', 'Swedish', 'Venezuelan'
				, 'Ukrainian', 'Yugoslavian', 'exit'];
  
  I := 0;
  For I := Low(TabCountry) to High(Tabcountry) do
  Begin
    If Pos((copy(TabCountry[I], 1 ,Length(TabCountry[I])-1)),Born) > 0 then
    Begin
	Addfieldvalue(pfBirthPlace, TabCountry[I]);
    I := High(TabCountry);
    end;
  End;
		
    /// DeathDate:
  
    ExplodeString(Born, PartBorn, #46);

	If High(partBorn) > 0 then
	Begin
	  logmessage('deathday commence')
    For I := Low(partBorn) to High(partBorn) do
    Begin
    PartBorn[I] := Trim(partBorn[I]);
    End;

	 For I := Low(partBorn) to High(partBorn) do
	 Begin
	    If (Lowercase(partBorn[I]) = 'd') OR (Copy(partBorn[I], length(partBorn[I])-2,3) = ', d') OR (Copy(partBorn[I], length(partBorn[I])-2,3) = '. d') then
     DeathDate := '28/12/' + Copy(partBorn[I+1], 0, 4);
		end;
	end;
  
	If DeathDate <> '' then AddFieldValue(pfDeathDate, DeathDate);
  end;  
  /////----- End of Infos in <notes>
	
  /////----- Infos in <pseudonyms>
  If Pos('<th>Pseudonyms</th>', HTML) > 0 then begin
  
	// AKA: //

	curpos := Pos('<th>Pseudonyms</th>', HTML);
	endpos := curpos;
	While (curpos > 0) AND (curpos < Posfrom('<th>Films</th>', HTML, EndPos)) do begin
		endpos := curpos;
		PosStart := PosFrom('class="acta">', HTML, endpos);
		PosEnd := PosFrom('</span>', HTML, PosStart);
		Pseudo := Trim(Copy(HTML, (PosStart + 13), (PosEnd - PosStart - 13)));
		LogMessage('AKA :' + Pseudo);
		curpos := posfrom('class="acta">', HTML, posend);
		AddFieldValue(pfAltnames, Pseudo);
	end;
	end; 
	/////----- End of Infos in <pseudonyms>
	
	/////----- Infos in <Films>
	If  Pos('<th>Films</th>', HTML) > 0 then begin
	
	// BIO: //

	curpos := Pos('<th>Films</th>', HTML);		
    	LogMessage('Films readout');
    	if curPos > 0 then    begin

	Lien := '----- Filmographie (EGAFD) -----';
	//Lien := '';
    	EndPos := curPos;

       		while (curPos > 0) AND (curPos < PosFrom('</ul>', HTML, EndPos)) do begin
             	EndPos := curPos; // Set last position to actual position

         	/// Get URL: ///

         UrlPosStart := PosFrom('<a href="', HTML, EndPos);  // search for url start
         UrlPosEnd := PosFrom('" class="', HTML, UrlPosStart);  // search for url end     
         URL1 := BASE_URL + Copy(HTML, UrlPosStart + 9, (UrlPosEnd - UrlPosStart - 9) );
         LogMessage(URL1);
		 	
			
         	/// Get Name: ///

         actPosStart := PosFrom('<a href="', HTML, EndPos);   // search for url start;
         actPosStart2 := PosFrom('">', HTML, actPosStart)
         actPosEnd:=PosFrom('</a>', HTML, actPosStart2);    // search for url end
         Name := Trim(Copy(HTML, (actPosStart2 + 2), (actPosEnd - actPosStart2 - 2) ));
         LogMessage(Name);
         debug_pos1:=Pos('(',Name);
            if debug_pos1 >0 then Name := Copy(Name,0,debug_pos1-1);
            LogMessage(Name);
		
		
		 /// Get Title  (for movies): ///

         actPosStart := PosFrom('<a href="', HTML, EndPos);   // search for url start;
         actPosStart2 := PosFrom('">', HTML, actPosStart)
         actPosEnd:=PosFrom('</a>', HTML, actPosStart2);    // search for url end
         Title := Trim(Copy(HTML, (actPosStart2 + 2), (actPosEnd - actPosStart2 - 2) ));
         LogMessage(Title);
         debug_pos1:=Pos('(',Title);
            if debug_pos1 >0 then Title := Copy(Title,0,debug_pos1-1);
            LogMessage(Title);

		/// If Original: ///

	actposstart := actposEnd + 5;
	actposstart := PosFrom('">', HTML, actposstart) + 2;
	actPosEnd := PosFrom('</', HTML, actPosstart) - 1;
	If copy(HTML, actposstart, 3) = 'alt' then
		OrigT := Copy(HTML, (actPosstart + 22),(actPosEnd-actPosStart-21))
	else
		OrigT := Title;

		/// Notes: ///

	actPosStart := PosFrom('<a href="', HTML, EndPos);
	actPosStart2 := PosFrom('</a>', HTML, actPosStart);
	Av := Trim(Copy(HTML, (actposstart2 + 5), 1));
	     logmessage('AV : ' + Av);
	     If Av = '<' then begin
	     actposStart2 := (actposstart2 + 5);           //Step to go after "</a>"
	     actposstart3 := PosFrom('>', HTML, actposStart2);
	     actposend := PosFrom('<', HTML, actposstart3);
	     Notes := Trim(Copy(HTML, (actposstart3 +1), (actposend - actposStart3 - 1)));
	     logmessage('Notes :' + Notes);	
	     end;   
		 
         debug_pos1:=Pos('(',Notes);
         if debug_pos1 >0 then Year:= Copy(Notes,0,debug_pos1-1);
         LogMessage(Notes);

		/// Get Year & Note: ///
	tmpYear := Copy(Notes, 0, 2);
	logMessage('tmpYear :' + tmpYear);
	
		Case tmpYear of
		'c.' : Begin
			Year := Copy(Notes,4,4);
			Note := '';
			end;

		'19', '20' : Begin
			If Copy(Notes,0,5) = 's' {OR Copy(Notes,0,5) = '?'} then begin
				Year := Copy(Notes,0,5);
				Note := Copy(Notes,7, Length(Notes)-6);
				end
			else begin
				Year := Copy(Notes,0,4);
				Note := Copy(Notes,6, Length(Notes)-5);
			end;
			If Copy(Notes,0,5) = '?' then begin
				Year := Copy(Notes,0,5);
				Note := Copy(Notes,7, Length(Notes)-6);
				end
			else begin
				Year := Copy(Notes,0,4);
				Note := Copy(Notes,6, Length(Notes)-5);
			end;
			end;

		else begin
			Year := '';
			Note := Notes;
		end;
		end;

	logmessage('Year :' + Year);
	logmessage('Note :' + Note);
	  
	if GET_PERSONMOVIE then
    AddPersonMovie(Trim(OrigT), '', '', Year, LowerCase(URL1), ctActors); //Add movie in database

		/// Get Role: ///
	difpos := (PosFrom('<i>', HTML, (actposEnd-1))+4) - actposend;
	logmessage('DIFFERENCE : ' + intToStr(difpos));
	
	If difpos > 0 then begin
		If difpos < 200 then begin

         	actPosStart := PosFrom('<i>', HTML, (actposend-1)) + 4;
         	{actPosStart := PosFrom('> <i>', HTML, actPosStart) + 6;}
            actPosEnd:=PosFrom('</i></li>', HTML, actPosStart) - 1;
        	Role := Trim(Copy(HTML, actposStart, (actPosEnd - actPosStart)));
			Role := StringReplace(Role, ';', '  - ', true, false, true);
	    	Role := StringReplace(Role, '/', ' - ', true, false, true);
            if Pos(', ', Role) = 1 then Delete(Role, 1, 2);
         	LogMessage('Role: ' + Role);
      
         	debug_pos1:=Pos('(',Role);
			if debug_pos1 >0 then Role:= Copy(Role,0,debug_pos1-1);
         	LogMessage(Role);

		end;
	end;
		
	if GET_PERSONMOVIEROLE then
	AddPersonMovie(Trim(OrigT), '', Role, Year, LowerCase(URL1), ctActors);
	
		/// Total Line: ///
	If Lien <> '' then
	Lien := Lien + #13;
	If URL1 <> '' then begin
		If OrigT <> Title then
			Lien := Lien + Name
		else 
			Lien := Lien + '<link url="' + URL1 + '">' + Name + '</link>';
	end;
	If Year <> '' then
	Lien := Lien + ' • ' + Year;
	If Note <> '' then
	Lien := Lien + ' • ' + Note;
	If Role <> '' then
	Lien := Lien + ' • ' + Role;

	LogMessage('LIEN :' + Lien);	
            
         curPos := PosFrom('<a href="', HTML, actPosEnd);
       end;
end; 
    /////----- End of infos in <Films>
	    
(*      if (Lien <> '') AND (Born = '') then
		AddFieldValue(pfBio, Lien);
		if (Lien  <> '') AND (Born <> '') then
		AddFieldValue(pfBio, Born + #13#10 + Lien);
*)
		if (Lien <> '') AND (Born2 = '') then
		AddFieldValue(pfBio, Lien);
		if (Lien  <> '') AND (Born2 <> '') then
		AddFieldValue(pfBio, Born2 + #13#10 + Lien);
	end;

	/////----- Infos in <Web Sites>
	If  Pos('<th>Films</th>', HTML) > 0 then begin
	
	// WEB SITES: //
	curpos := Pos('<th>Web Sites</th>', HTML);		
    	LogMessage('Web Sites readout');
    	if curPos > 0 then    begin
		Sites := '    • •     Web Sites   • • ';
	endPos := curPos;
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
	while (curPos > 0) AND (curPos < PosFrom('<p class="cpyrgt" align="center">', HTML, endPos)) do begin
		curPos := curPos + Length('<a class="lnk" target="_new" href="');
		endPos := PosFrom('">', HTML, curPos);
		URL1 := Copy(HTML, curPos, endPos - curPos);
		URL1 := BASE_URL + URL1;
		curPos := endPos + 2;
		endPos := PosFrom('</a></li>', HTML, curPos);
		Name := Copy(HTML, curPos, endPos - curPos); 
     	
		/// Total Line: ///
		If Sites <> '' then
		Sites := Sites + #13;
		If URL1 <> '' then 
		Sites := Sites + '<link url="' + URL1 + '">' + Name + '</link>';
		LogMessage('WEB SITES :' + Sites);	
	curPos := PosFrom('<a href="', HTML, actPosEnd);
		end;
	end; 
	
	If Sites <> '' then
		LogMessage('WEB SITES :' + Sites);	
		fullinfo:= fullinfo + Sites + #13;
		LogMessage(fullinfo);
	end;
	/////----- End of infos in <Web Sites>
		 
	/////----- Infos in <Internet Clubs and Groups>
	If  Pos('<th>Internet Clubs and Groups</th>', HTML) > 0 then begin
	
	// CLUB AND GROUPS: //
	curpos := Pos('<th>Internet Clubs and Groups</th>', HTML);		
    	LogMessage('Internet Clubs and Groups readout');
    	if curPos > 0 then    begin
		Sites1 := '   • •   Internet Clubs and Groups:  • •';
		If Sites1 <> '' then 
		Sites1 := Sites1;
		LogMessage('CLUB AND GROUPS :' + Sites1);
		If Sites1 <> '' then
		LogMessage('CLUB AND GROUPS :' + Sites1);
		fullinfo:= fullinfo + #13 + Sites1 + #13;
		LogMessage(fullinfo);
	    end;
	end; 

    /////----- Infos in <Bomis Rings>
	If  Pos('<li>Bomis Rings:</li>', HTML) > 0 then begin
    
	// BOMIS: //
	curpos := Pos('<li>Bomis Rings:</li>', HTML);		
    	LogMessage('Bomis Rings readout');
    	if curPos > 0 then    begin
		Bomis := '   •   Bomis Rings   •';
	endPos := curPos;
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
	while (curPos > 0) AND (curPos < PosFrom('</ul>', HTML, endPos)) do begin
		curPos := curPos + Length('<a class="lnk" target="_new" href="');
		endPos := PosFrom('">', HTML, curPos);
		URL1 := Copy(HTML, curPos, endPos - curPos);
		URL1 := BASE_URL + URL1;
		curPos := endPos + 2;
		endPos := PosFrom('</a></li>', HTML, curPos);
		Name := Copy(HTML, curPos, endPos - curPos); 
     
		/// Total Line: ///
		If Bomis <> '' then
		Bomis := Bomis + #13;
		If URL1 <> '' then 
		Bomis := Bomis + '<link url="' + URL1 + '">' + Name + '</link>';
		LogMessage('BOMIS :' + Bomis);
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
		end;  
	end; 

    If Bomis <> '' then
		LogMessage('BOMIS :' + Bomis);	
		fullinfo:= fullinfo + Bomis + #13;
		LogMessage(fullinfo);
	end;
	/////----- End of infos in <Bomis Rings>
	
	/////----- Infos in <MSN Groups>
	If  Pos('<li>MSN Groups:</li>', HTML) > 0 then begin
	
	// MSN: //
	curpos := Pos('<li>MSN Groups:</li>', HTML);		
    	LogMessage('MSN Groups readout');
    	if curPos > 0 then    begin
		MSN := '   •   MSN Groups   •';
	endPos := curPos;
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
	while (curPos > 0) AND (curPos < PosFrom('</ul>', HTML, endPos)) do begin
		curPos := curPos + Length('<a class="lnk" target="_new" href="');
		endPos := PosFrom('">', HTML, curPos);
		URL1 := Copy(HTML, curPos, endPos - curPos);
		URL1 := BASE_URL + URL1;
		curPos := endPos + 2;
		endPos := PosFrom('</a></li>', HTML, curPos);
		Name := Copy(HTML, curPos, endPos - curPos); 
     
		/// Total Line: ///
		If MSN <> '' then
		MSN := MSN + #13;
		If URL1 <> '' then 
		MSN := MSN + '<link url="' + URL1 + '">' + Name + '</link>';
		LogMessage ('MSN :' + MSN);
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
	   end;  
	end; 

    If MSN <> '' then
		LogMessage('MSN  :' + MSN);	
		fullinfo:= fullinfo + MSN  + #13;
		LogMessage(fullinfo);
	end;
	/////----- End of infos in <MSN Groups>
	
	/////----- Infos in <MySpace>
	If  Pos('<li>MySpace:</li>', HTML) > 0 then begin  
	
	// MYSPACE: //
	curpos := Pos('<li>MySpace:</li>', HTML);		
    	LogMessage('MySpace readout');
    	if curPos > 0 then    begin	
		MySpace := '   •   MySpace   •';
	endPos := curPos;
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
	while (curPos > 0) AND (curPos < PosFrom('</ul>', HTML, endPos)) do begin
		curPos := curPos + Length('<a class="lnk" target="_new" href="');
		endPos := PosFrom('">', HTML, curPos);
		URL1 := Copy(HTML, curPos, endPos - curPos);
		URL1 := BASE_URL + URL1;
		curPos := endPos + 2;
		endPos := PosFrom('</a></li>', HTML, curPos);
		Name := Copy(HTML, curPos, endPos - curPos); 
     
		/// Total Line: ///
		If MySpace <> '' then
		MySpace := MySpace + #13;
		If URL1 <> '' then 
		MySpace := MySpace + '<link url="' + URL1 + '">' + Name + '</link>';
		LogMessage('MYSPACE :' + MySpace);
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
		end;  
	end; 

    If MySpace <> '' then
		LogMessage('MYSPACE :' + MySpace);	
		fullinfo:= fullinfo + MySpace + #13;
		LogMessage(fullinfo);
	end;
	/////----- End of infos in <MySpace>
 
	/////----- Infos in <Wikipedia>
	If  Pos('<li>Wikipedia:</li>', HTML) > 0 then begin 
	
	// WIKIPEDIA: //
	curpos := Pos('<li>Wikipedia:</li>', HTML);		
    	LogMessage('Wikipedia readout');
    	if curPos > 0 then    begin
		Wikipedia := '   •   Wikipedia   •';
	endPos := curPos;
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
	while (curPos > 0) AND (curPos < PosFrom('</ul>', HTML, endPos)) do begin
		curPos := curPos + Length('<a class="lnk" target="_new" href="');
		endPos := PosFrom('">', HTML, curPos);
		URL1 := Copy(HTML, curPos, endPos - curPos);
		URL1 := BASE_URL + URL1;
		curPos := endPos + 2;
		endPos := PosFrom('</a></li>', HTML, curPos);
		Name := Copy(HTML, curPos, endPos - curPos); 
     
		/// Total Line: ///
		If Wikipedia <> '' then
		Wikipedia := Wikipedia + #13;
		If URL1 <> '' then 
		Wikipedia := Wikipedia + '<link url="' + URL1 + '">' + Name + '</link>';
		LogMessage('WIKIPEDIA :' + Wikipedia);
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
		end;  
	end; 

    If Wikipedia <> '' then
		LogMessage('WIKIPEDIA :' + Wikipedia);	
		fullinfo:= fullinfo + Wikipedia + #13;
		LogMessage(fullinfo);
	end;
	/////----- End of infos in <Wikipedia>
	
	/////----- Infos in <Yahoo! Groups>
	If  Pos('<li>Yahoo! Groups:</li>', HTML) > 0 then begin
	
	// YAHOO: //
	curpos := Pos('<li>Yahoo! Groups:</li>', HTML);		
    	LogMessage('Yahoo! Groups readout');
    	if curPos > 0 then    begin
		Yahoo := '   •   Yahoo! Groups   •';
	endPos := curPos;
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
	while (curPos > 0) AND (curPos < PosFrom('</ul>', HTML, endPos)) do begin
		curPos := curPos + Length('<a class="lnk" target="_new" href="');
		endPos := PosFrom('">', HTML, curPos);
		URL1 := Copy(HTML, curPos, endPos - curPos);
		URL1 := BASE_URL + URL1;
		curPos := endPos + 2;
		endPos := PosFrom('</a></li>', HTML, curPos);
		Name := Copy(HTML, curPos, endPos - curPos); 
     
		/// Total Line: ///
		If Yahoo <> '' then
		Yahoo := Yahoo + #13;
		If URL1 <> '' then 
		Yahoo := Yahoo + '<link url="' + URL1 + '">' + Name + '</link>';
		LogMessage('YAHOO :' + Yahoo);
	curPos := PosFrom('<a class="lnk" target="_new" href="', HTML, curPos);
		end;  
	end; 

    If Yahoo <> '' then
		LogMessage('YAHOO :' + Yahoo);	
		fullinfo:= fullinfo + Yahoo + #13;
		LogMessage(fullinfo);
	end;
	/////----- End of infos in <Yahoo! Groups>
	
	/////----- End of infos in <Internet Clubs and Groups> 
        
	If fullinfo <> '' then
		if GET_FULLINFO then
		AddFieldValueXML('comment', ' • • ' + SCRIPT_NAME + ' ' + SCRIPT_VERSION + ' • •   ' + #13 + fullinfo)
    else
		AddFieldValueXML('comment', ' • • ' + SCRIPT_NAME + ' ' + SCRIPT_VERSION + ' • •   ');	
		if GET_COMMENT then
	AddFieldValueXML('comment', fullinfo);
	/////----- End of infos in <Web Sites> and <Internet Clubs and Groups> 
		
		
		// Photo:
		curPos :=Pos('src="/actresses/id/',HTML);
	if curPos > 0 then begin
		EndPos := PosFrom('" width', HTML, curPos);
		PhotoURL := BASE_URL + Copy(HTML, curPos + 5, EndPos - curPos - 5);
		LogMessage('URL de la photo: '+ PhotoURL);
		{PhotoURL := HTMLToText (PhotoURL);}
		AddImageURL(4, PhotoURL);
	end
	else begin
		PhotoURL := '';
	end;

end;


//--------------------------------Procedure SearchResults------------------------------
procedure SearchResults(HTML : string);

///////////////////////////////////////////////////////////////////////////////////////////
var curpos,endpos : Integer;							 //
    actPosStart, actPosEnd, actalposStart, actalposEnd, urlPosStart, urlposEnd : integer;//
    beforeName, URLTemp, AliasName, NameTemp, RealName, ifAlias : string;					 //
///////////////////////////////////////////////////////////////////////////////////////////

Begin

	//Search Name or Pseudo or part of name (with start in same alphabetic page as RealName)
	Curpos := Pos(nomAct, HTML);

	If curpos > 0 then begin
		
	endPos := curpos;

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

		
		  Case NameTemp of
		    nomAct :  Begin                     //Same name as Title
		    //logmessage('Same Name as title');
		            Case BeforeName of
		              'c' : Begin             //Real Name
		              RealName := NameTemp;
		              AliasName := NameTemp;
		              // 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),'');
					        LogMessage('TYPE I');
		              end;
		              
		            else begin              // Good Name but Alias
		            //logmessage('Good Name but Alias');
		              AliasName := NomAct;
		              // Real Name
		              actalPosStart := Posfrom('alias for ', HTML, actposEnd);
					        actalposEnd := Posfrom('</span>', HTML, actalposstart);
					        RealName := Trim(Copy(HTML, (actalposStart + 10), (actalposEnd - actalposStart - 10)));
					        // Name Temp
					        NameTemp := 'Alias 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),'');
					        LogMessage('TYPE II');
		              end;
		            end;
		    end;
		    else begin
		      //Alias 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
		          'c' : Begin                   // Alias is real Name
		              // Real Name
		              RealName := AliasName;
		              // 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, RealName, '', LowerCase(URLTemp),'');
					        logmessage('TYPE III');
		          end;
		        
		        else begin
		              // Real Name
		              actalPosStart := Posfrom('alias for ', HTML, actPosEnd);
					        actalPosEnd := Posfrom('</span>', HTML, actalPosStart);
					        RealName := Trim(Copy(HTML, (actalposStart + 10), (actalposEnd - actalposStart - 10)));
					        NameTemp := 'alias 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),'');
					        Logmessage('TYPE IV');
		          end;
		        end;
          end  
		      else begin                  // Name is in Alias for ......
		      logmessage ('trouve dans alias');
		              // Alias Name
		              actPosStart := Prevpos('"acta">', HTML, curpos);
		              actPosEnd := Posfrom('</a>', HTML, actPosStart);
		              Aliasname := Trim(Copy(HTML, (actPosStart + 7), (actPosEnd - actPosStart - 7)));
		              // Real Name
		              actalPosStart := Posfrom('alias for ', HTML, actposEnd);
					        actalPosEnd := Posfrom('</span>', HTML, actalposStart);
		              RealName := Trim(Copy(HTML, (actalposStart + 10), (actalposEnd - actalposStart - 10)));
		              // 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),'');
					        actPosEnd := PosFrom('href="', HTML, actalPosEnd);
					        logmessage('TYPE V');
					end;
		    end;
		  end;
	 curpos := PosFrom(nomAct, HTML, actPosEnd);
	 end;
  end;
end;


//---------------------- Parse Page -------------------------
// ParsePage
// - Entrypoint for the script. Check if the search-term leads to zero, one or many results.
function ParsePage(HTML : String; URL : AnsiString) : Cardinal;
var NomTitle : string;

begin

	//Choix suivant page chargée

	
	If Pos('<title>Actresses:', HTML) > 0 then begin	//Si page index actrices
		SearchResults(HTML);	//Appel de la fonction recherche dans page index
		Result := prList;	//Résultat de la recherche
	end else
	If Pos('<title>Actresses:', HTML) < 0 then begin
			LogMessage('Erreur...');
			Result := prError; //error (actrice non trouvée)
		end
	else	
	//Page actrice
	If Pos('<th>Notes</th>', HTML) > 0 then begin			//Si page actrice
		Mode := smNormal;
		ParsePeople(URL, HTML);	//Recherche des infos
		Result := prDownload;	//Récupérer les données
	end
	else
	If Pos('<th>Pseudonyms</th>', HTML) > 0 then begin			//Si page actrice
		Mode := smNormal;
		ParsePeople(URL, HTML);	//Recherche des infos
		Result := prDownload;	//Récupérer les données
	end
	else
	If Pos('<th>Films</th>', HTML) > 0 then begin			//Si page actrice
		Mode := smNormal;
		ParsePeople(URL, HTML);	//Recherche des infos
		Result := prDownload;	//Récupérer les données
	end else
		Result := prError; //erreur (page non trouvée)

end;

//-----------------------Initialisation du script-----------------

begin
 Mode := smSearch;   //Mode d'initialisation du script
end.
