 //This version of script is for use with PVD versions 0.9.9.16 and above!!!
 (*
 Additional types and functions that can be used in scripts:

  //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 
 //This version of script is for use with PVD versions 0.9.9.16 and above!!!

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

 SEARCH_STR  = 'www.iafd.com/results.asp?SearchType=Mozilla-search&SearchString=%s';
 
 CODE_PAGE   = 0; //Use 0 for Autodetect 

 
//Global variables 
var
 Mode : Byte;
 PosterURL : String;

//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;
begin
 if PosterURL = '' then
  Result := SEARCH_STR
 else
  Result := PosterURL; 
end;

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

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

procedure FindPoster(HTML : String);
var
 curPos, EndPos : Integer;
begin
// 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;
  
// ParseMovie
//  - after searchlist selection this function is called with the new html content
procedure ParseMovie(MovieURL : String; HTML : String);
var
 curPos, EndPos, P, P2, L: Integer;
 actPosStart,actPosEnd, UrlPosStart,UrlPosEnd,debug_pos1:Integer;
 Tmp, URL, Name,dbgstrg,tmpstrg : String;
 ActorNames: TWideArray;
 ActorNumber,I,J: Integer;
begin
	 AddFieldValue(mfURL, MovieURL);
	 LogMessage('Page parsing started');
	 EndPos := 1;

	//Check for title. No orig. title info present, so duplicate..
	dbgstrg:= TextBetween(HTML, '<h2>', '(', False, EndPos);
	LogMessage('Title: ' + dbgstrg);
	AddFieldValue(mfOrigTitle,dbgstrg); 
	AddFieldValue(mfTitle,dbgstrg); 
	
	//Year
	 dbgstrg:= Trim(TextBetween(HTML, '(', ')', False, EndPos));
	 LogMessage('YEAR:' + dbgstrg);
	 AddFieldValue(mfYear,dbgstrg); 
	 
	 // AKA - Titles...
	LogMessage('getting all titles');
	CurPos:= Pos('Also Known As',HTML);
	EndPos := curPos;
	while (curPos > 0) AND (curPos < PosFrom('<dt>Distributor</dt>', HTML, EndPos)) do begin
		
		dbgstrg:= TextBetween(HTML, '<dd>', '</dd>', False, CurPos);
		LogMessage('AKA: ' + dbgstrg);
		curPos := PosFrom('</dd>', HTML,CurPos);	
		AddFieldValue(mfAka,dbgstrg);
	 end; 
	
 
 //Studio
 // <dd><a href="/studio.rme/studio=2984/VTO.htm">VTO</a> (<a href="/distrib.rme/distrib=2984/vto.htm">VTO</a>)</dd>
 
	 curPos := PosFrom('<a href="/studio.rme', HTML, EndPos);
	 if curPos > 0 then
	 AddFieldValue(mfStudio, TextBetween(HTML, '>', '</a>', False, curPos))
	 else
	  curPos := EndPos; 
 
// Distributor
// Same Code as for an actor
	curPos := Pos('<dt>Distributor</dt>', HTML); 
	if curPos>0 then 
	begin
		// get url
		UrlPosStart := PosFrom('href=', HTML, curPos);  // search for url start
		UrlPosEnd := PosFrom('>', HTML, UrlPosStart);  // search for url start
		URL := BASE_URL + '/' + Trim(Copy(HTML, UrlPosStart + 7, (UrlPosEnd - UrlPosStart - 8) ));
		LogMessage(URL);
	
		// Get Name
		actPosStart:=PosFrom('.htm">', HTML, curPos);  	// search for url start;
		actPosEnd:=PosFrom('</a>', HTML, actPosStart); 	// search for url end
		if (actPosStart > 0) then 
		begin
			Name := Trim(Copy(HTML, (actPosStart + Length('.htm">')), (actPosEnd - actPosStart - 6) ));
			LogMessage(Name);
			debug_pos1:=Pos('(',Name);
			if debug_pos1 >0 then 
				Name := Copy(Name,0,debug_pos1-1);
			LogMessage(Name);
		//	AddMoviePerson(Trim(Name), '', '', URL, ctDirectors); Where to store?
		end;
	end;

 
// Director
// Same Code as for an actor
	curPos := Pos('<dt>Director</dt>', HTML); 
	if curPos>0 then 
	begin
		// get url
		UrlPosStart := PosFrom('href=', HTML, curPos);  // search for url start
		UrlPosEnd := PosFrom('>', HTML, UrlPosStart);  // search for url start
		URL := BASE_URL + '/' + Trim(Copy(HTML, UrlPosStart + 7, (UrlPosEnd - UrlPosStart - 8) ));
		LogMessage(URL);
	
		// Get Name
		actPosStart:=PosFrom('.htm">', HTML, curPos);  	// search for url start;
		actPosEnd:=PosFrom('</a>', HTML, actPosStart); 	// search for url end
		if (actPosStart > 0) then 
		begin
			Name := Trim(Copy(HTML, (actPosStart + Length('.htm">')), (actPosEnd - actPosStart - 6) ));
			LogMessage(Name);
			debug_pos1:=Pos('(',Name);
			if debug_pos1 >0 then 
				Name := Copy(Name,0,debug_pos1-1);
			LogMessage(Name);
			AddMoviePerson(Trim(Name), '', '', LowerCase(URL), ctDirectors);
		end;
	end;
	
  
 //Cast
 curPos := Pos('<h3>Actresses</h3>', HTML); 
 LogMessage('Cast readout');
if curPos > 0 then 	begin
	EndPos := curPos;

	while (curPos > 0) AND (curPos < PosFrom('<form action="/correct.asp"', HTML, EndPos)) do begin
		
		EndPos := curPos; // Set last position to actual position
		// get url
		UrlPosStart := PosFrom('href="', HTML, EndPos);  // search for url start
		UrlPosEnd := PosFrom('>', HTML, UrlPosStart);  // search for url end
		
		URL := BASE_URL + Trim(Copy(HTML, UrlPosStart + 6, (UrlPosEnd - UrlPosStart - 7) ));
	
		LogMessage(URL);
	
		// Get Name
		actPosStart:=PosFrom('.htm">', HTML, EndPos);  	// search for url start;
		actPosEnd:=PosFrom('</a>', HTML, actPosStart); 	// search for url end
		Name := Trim(Copy(HTML, (actPosStart + Length('.htm">')), (actPosEnd - actPosStart - 6) ));
		LogMessage(Name);
		
		debug_pos1:=Pos('(',Name);
		if debug_pos1 >0 then 
			Name := Copy(Name,0,debug_pos1-1);
		LogMessage(Name);
		
		AddMoviePerson(Trim(Name), '', '', LowerCase(URL), ctActors);
						
		curPos := PosFrom('href="', HTML, actPosEnd);
	 end; 
end;
 
 curPos := Pos('<h3>Scene Breakdowns</h3>', HTML); 
 
if curPos > 0 then 	begin
	LogMessage('Scene readout');
	EndPos := curPos;
	curPos := PosFrom('Scene', HTML, EndPos+10);
	dbgstrg := '';
	J:=1;
	//while (curPos > 0) AND (curPos < PosFrom('id=ratings', HTML, EndPos)) do begin
	while (curPos > 0) AND (curPos < PosFrom('<h3>', HTML, EndPos+10)) do begin	
		// G
		actPosStart:=PosFrom('Scene', HTML, curPos);  		// search for url start;
		actPosEnd:=PosFrom('</li>', HTML, actPosStart); 	// search for url end
		Name := Trim (Copy(HTML, actPosStart,actPosEnd - actPosStart));
		LogMessage(Name);
	
		// NEW: Split string by comma and insert hyperlink-tags
		// Scene 1. Julianne James, Tom Byron
		// Final Strin: Scene ##. <link name="actor">actor</link> <link name="actor2">actor2</link>
		
			// remove static text from string (scene 1.) and split cleaned string by ','
			tmpstrg := Copy(Name,10,100);
			ActorNumber := ExplodeString(tmpstrg, ActorNames,',');		
			LogMessage(tmpstrg);	
			
			// Prepare first part of string...
			dbgstrg:= dbgstrg + 'Scene ' + IntToStr(j)+ '. ';
			
			// format splitted strings into links
			 For I := Low (ActorNames) To High (ActorNames) Do
				begin
				dbgstrg := dbgstrg+ '<link name="' + ActorNames[I] + '">' + ActorNames[I] + '</link> ';
			  end;
			 dbgstrg:= dbgstrg +  #13#10;
			
		//dbgstrg:= dbgstrg + Name + #13#10;
		
		j:=j+1;				
		curPos := PosFrom('Scene', HTML, actPosEnd);
	 end; 
	 LogMessage(dbgstrg);
	 AddFieldValue(mfTagline,dbgstrg);
end;
  
end; // End of the routine...


// ParseSearchResults
// - create list of movies found during the search
procedure ParseSearchResults(HTML : String);
var
 curPos, EndPos, P : Integer;
 Title, Year, URL, Studio : String;
begin
	EndPos := 1;
	LogMessage('Parsing movie list...');
	curPos := PosFrom('<dt>', HTML, EndPos); 
	 
	while curPos > 0 do
		begin
		// Each movie entry starts with <dt>
		EndPos := curPos + Length('<dt>');
			curPos := PosFrom('<a href="', HTML, EndPos); 
			if curPos > 0 then 		begin
				curPos := curPos + Length('<a href="');
				EndPos := PosFrom('"', HTML, curPos);
				URL := BASE_URL + '/' + Copy(HTML, curPos, EndPos - curPos);
				LogMessage('URL detected: '+URL);
			end; 
			Title := TextBetween(HTML, '>', '</a>', False, EndPos);
			LogMessage(Title);

			// Now that we got the title, move on to the studio...
			curPos := PosFrom('<b>Release Info:</b>', HTML, EndPos);
			
			if curPos > 0 then begin
				curPos := curPos + Length('<b>Release Info:</b>') + 1;
				EndPos := PosFrom(',', HTML, curPos);
			
				Studio := Copy(HTML, curPos, EndPos - curPos);
				LogMessage(Studio);
				curPos := EndPos +  2;
				EndPos := PosFrom('<', HTML, curPos);
				Year := Copy(HTML, curPos, EndPos - curPos);
				LogMessage(Year);
			end;
	  AddSearchResult(Title, Studio, Year, URL, '');

	  curPos := PosFrom('<dt>', HTML, EndPos); 
	 end;
end;

// ParsePage
// - Entrypoint for the script. Check if the search-term leads to zero, one or many results.
function ParsePage(HTML : String; URL : AnsiString) : Cardinal;
begin
	Wait (pauseBeforeLoad);
	LogMessage('Script version:' + SCRIPT_VERSION);

	if Pos('<h2>Movie Titles - Top 50 Results</h2>', HTML) > 0 then begin
		ParseSearchResults(HTML);
		Result := prList; //search results retrieved
	end else
	if Pos('<h2>Movie Titles - Top 50 Results</h2>', HTML) < 0 then
		Result := prError //error (movie not found)
	else
	if (Pos(': movie detail :', HTML) > 0) then begin
		Mode := smNormal;
		ParseMovie(URL, HTML);
		Result := prFinished; //script has finished it's job
	end else
		Result := prError; //error (unknown page retrieved)
end;

begin
 Mode := smSearch; 
end.
