// 20100921-Vlad: New design adjustments
// 20101001-JeF : Options User Interface + Debug mode

const
	script_name='imdb';
	language='English';
	version=57;
	author='Alex/JeF';
	MinProgramBuild=6200;
	PCount = 12;
	//MAX_PHOTOS_COUNT = 3;
	defaultPicSizeX=800;
	defaultPicSizeY=600;

var
	DownloadPersonBiography, DownloadPersonAdditionalInfo, DownloadPersonAwards: boolean;
	MaxPhotosCount: integer;
	
	persons_count: integer;
	persons: array of string;
	imdb_codes: array of string;
	imdb_code, person_code: string;
	name, code, year, description: string;
	person, BasicURL, biography, trivia, character, birth_date, birthplace, birth_name, salary, height, original_name, death_date, person_site_url, awards: string;
	photo_url: string;
	monthes: array [1..12] of string;
	prefixes: array [1..PCount] of string;
	movie_html: string;
	person_height: integer;
	jTraceMode, jTraceLevel, jTraceAsk: integer;   // 20101001-JeF

	DlPersonalQuotes, DlAlternateNames, DlNickNames, DlDesc, DlBio : integer;
	DlFilmoAsActor, DlFilmoAsDirector, DlFilmoAsWriter, DlFilmoAsComposer : integer;
	IMDBLocalService: integer;
	
	RequestParameters: TStringList;


function GetStr(str: String; s1: String; s2: String): String;
var
  I: Integer;
  J: Integer;
begin
  // Вырезает кусок строки между s1 и s2
	Result := '';
	I := Pos(LowerCase(s1), LowerCase(str));
	if I > 0 then
	begin
		J := MyPos(LowerCase(s2), LowerCase(str), I + Length(s1));
		if J > 0 then
  		Result := Copy(str, I + Length(s1), J - I - Length(s1));
	end;
end;
	
function GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	PersonName := EncodeURL(PersonName);
  // url:=format('http://www.imdb.com/find?q=%s&s=all&ref_=fn_nm', [PersonName]);
	url:=format('http://www.imdb.com/find?q=%s&s=nm&ref_=fn_nm', [PersonName]);
	//url:=ReplaceStr(url, ' ', '+', -1);

	if (UpperCase(PersonName) = '$OPT') then begin   // 20101001-JeF - Temporary
		url:='';
		ShowScriptOptions;
	end;
	
	result:=url;
end;

function ProcessMovieYear(AYear: string): string;
begin
  Result := Trim(AYear);
  if (Pos('-', AYear) > 0) then
    Result := Copy(AYear, 1, Pos('-', AYear) - 1);
end;

procedure ExtractSpouses();
const
  SEPARATOR_SPOUSE = '<tr>';
  SEPARATOR_SPOUSE_END = '</tr>';
var
  I: Integer;
  J: Integer;
  K: Integer;
  Spouses: string;
  SingleSpouse: string;
  DateText: string;
  DateFrom: string;
  DateTo: string;
  PersonURL: string;
  PersonName: string;
  PersonCode: string;
  PersonStatus: Integer;
  PersonStatusStr: string;
  ChildrenCount: integer;

  function SpouseDateToFixedDate(ASpouseDate: string): string;
  var
    I: Integer;
    SpouseDay: string;
    SpouseYear: string;
    SpouseMonth: string;
    MonthPos: Integer;
  begin
    SpouseMonth := '';
    MonthPos := 0;
    for I := 1 to 12 do
    begin
      MonthPos := Pos(monthes[I], ASpouseDate);
      if (MonthPos > 0) then
      begin
        SpouseMonth := monthes[I];
        Break;
      end;
    end;
    if (SpouseMonth = '') then
      Result := Trim(ASpouseDate)
    else
    begin
      SpouseYear := Trim(Copy(ASpouseDate, MonthPos + Length(SpouseMonth),
        Length(ASpouseDate)));
      SpouseDay := Trim(Copy(ASpouseDate, 1, MonthPos - 1));
      SpouseMonth := IntToStr(I);
      if (Length(SpouseMonth) = 1) then
        SpouseMonth := '0' + SpouseMonth;
      if (Length(SpouseDay) = 1) then
        SpouseDay := '0' + SpouseDay;
      if (Length(SpouseDay) > 0) then
        Result := SpouseDay + '.' + SpouseMonth + '.' + SpouseYear
      else
        Result := SpouseYear;
    end;
    // check present days
    if (Pos('present', Result) > 0) then
      Result := '';
  end;

begin
	Spouses := '';
  I := Pos('<h5>Spouse</h5>', all);
  if (I > 0) then
  begin
    J := MyPos('</table>', all, I);
    if (J > 0) then
    begin
      Spouses := Copy(all, I, J - I);
      K := Pos(SEPARATOR_SPOUSE, Spouses);
      while (K > 0) do
      begin
        PersonName := '';
        PersonCode := '';
        PersonStatusStr := '';
        J := MyPos(SEPARATOR_SPOUSE_END, Spouses, K);
        if (J > 0) then
        begin
          SingleSpouse := Copy(Spouses, K, J - K);
          // Name and code
          I := Pos('<td valign="top">', SingleSpouse);
          if (I > 0) then
          begin
            I := I + Length('<td valign="top">');
            PersonName := Copy(SingleSpouse, I, MyPos('</td>', SingleSpouse, I) - I);
            Delete(SingleSpouse, 1, I + Length(PersonName) + Length('</td>'));
            I := Pos('<a href="/name/nm', PersonName);
            if (I > 0) then
            begin
              I := I + Length('<a href="/name/nm');
              J := MyPos('/">', PersonName, I);
              PersonCode := Copy(PersonName, I, J - I);
              Delete(PersonName, 1, J + Length('/">') - 1);
              PersonName := RemoveTags(PersonName);
            end;
          end;
          // date/status/children
          I := Pos('<td valign="top">', SingleSpouse);
          if (I > 0) then
          begin
            I := I + Length('<td valign="top">');
            SingleSpouse := Trim(Copy(SingleSpouse, I, MyPos('</td>', SingleSpouse, I) - I));
            DateText := '';
            DateFrom := '';
            DateTo := '';
            // date
            I := Pos(' - ', SingleSpouse);
            if (I > 0) then
            begin
              J := Pos(')', SingleSpouse);
              if ((J > 0) and (J > I)) then
              begin
                DateText := Trim(DecodeHTML(RemoveTags(Copy(SingleSpouse, 2, J - 2))));
                Delete(SingleSpouse, 1, J);
                I := Pos(' - ', DateText);
                DateFrom := SpouseDateToFixedDate(Copy(DateText, 1, I));
                DateTo := SpouseDateToFixedDate(Copy(DateText, I + Length(' - '), Length(DateText)));
              end;
            end;
            // status
            PersonStatus := 1;
            I := Pos('(', SingleSpouse);
            if (I > 0) then
            begin
              J := Pos(')', SingleSpouse);
              PersonStatusStr := Copy(SingleSpouse, I + 1, J - I - 1);
              //ShowMessage(PersonStatusStr);
              // statuses of spouses:
              // 1 - active
              // 2 - Divorced
              // 3 - Dead
              // 4 - Filled for Divorce
              // 5 - Cancelled (mitting at kinopoisk)
              // 6 - Separated
              if (Pos('divorced', PersonStatusStr) > 0) then
                PersonStatus := 2
              else if (Pos('death', PersonStatusStr) > 0) then
                PersonStatus := 3
              else if (Pos('filed for divorce', PersonStatusStr) > 0) then
                PersonStatus := 4
              else if (Pos('separated', PersonStatusStr) > 0) then
                PersonStatus := 6;
              Delete(SingleSpouse, 1, J);
            end;
            // children
            I := Pos('child', SingleSpouse);
            ChildrenCount := 0;
            if (I > 0) then
            begin
              SingleSpouse := Trim(Copy(SingleSpouse, 1, I - 1));
              try
                if (SingleSpouse <> '') then
                  ChildrenCount := StrToInt(SingleSpouse)
              except
                ChildrenCount := 0;
              end;
            end;
            // set spouse
            if (PersonName <> '') then
            begin
              PersonURL := '';
              if (PersonCode <> '') then
                PersonURL := GetPersonURL(PersonCode, False);
              SetPersonSpouse(PersonName, PersonCode, DateFrom, DateTo,
                ChildrenCount, PersonStatus, PersonURL, '');
            end;
          end;
        end;
        K := MyPos(SEPARATOR_SPOUSE, Spouses, K + 1);
      end;
    end;
  end;
end;

//Download search results from URL
function DownloadSearchURL(url: string): integer;
begin
	if (jTraceLevel > 0) then TraceAlert;   				 // 20101001-JeF
	all:=DownloadURL(url);
	if (jTraceLevel >= 1) then TraceExecute('Search',all);   // 20101001-JeF
	if all='-1' then
		result:=-1
	else
		result:=PersonSearch;
end;

function PersonSearch: integer;
var
	url, s, s2, s3: string;
	Name, Description, original_name, small_cover, code: string;
	i, j, k, l: integer;
begin
	persons_count:=0;

	s:='';
	//redirected to person page
	s2 := GetStr(all, '<link rel="canonical"', '>');
	code := ParseString(s2, 'www.imdb.com/name/nm(\d+)', 1);
	if code <> '' then begin
		Name := GetStr(all, '<title>', ' - IMDb</title>');
		SetSearchResult(Name, code);
		Result := 1;
		exit;
	end;
	//new design - 2012-11-28
	i := Pos('<a name="nm"></a>Names</h3>', all);
	if i>0 then begin
		j := MyPos('</table>', all, i+1);
		s := copy(all, i, j-i);
	end;
	if s <> '' then begin
		i := Pos('<tr class="findResult', s);
		while i>0 do begin
			s2 := GetStr(s, '<tr class="findResult', '</tr>');
			Name := GetStr(s2, '<td class="result_text">', '</a>');
			Name := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(Name)));
			code := ParseString(s2, ' href="/name/nm(\d+)', 1);
			description := GetStr(s2, '<small>', '</small>');
			description := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(description)));
			if (Pos('(', description) = 1) and (description[length(description)] = ')') then begin
				delete(description, 1, 1);
				delete(description, length(description), 1);
			end;
			small_cover := GetStr(s2, ' src="', '"');
			if (Pos('nopicture', small_cover)>0) then
				small_cover := '';
			k := Pos('_V1._', small_cover);
			if k = 0 then
				k := Pos('_V1_', small_cover);
			if k>0 then begin
				l := MyPos('_.', small_cover, k+5);
				s3 := copy(small_cover, l, length(small_cover)-l+1);
				delete(small_cover, k+5, length(small_cover)-k-4);
				//set maximum image resolution
				small_cover := small_cover + 'SY100_SX100' +  s3;
			end;
			if (Name <> '') and (Code <> '') then begin
				SetSearchResult2(Name, code, '', '', Description, small_cover);
				Inc(persons_count);
			end;
			delete(s, 1, i);
			i := Pos('<tr class="findResult', s);
		end;
	end
	//end new design - 2012-11-28
	else begin
		
		i:=pos('<p><b>Names (Approx Matches)</b> (Displaying', all);
		if i>0 then begin
			j:=MyPos('</p>', all, i+1);
			if j>0 then
				s:=copy(all, i+3, j-i-4);
		end;

		if (s='') or (pos('<b>No Matches.</b>', all)>0) then
			//Person not found
		else begin
		  i := pos('<table>',s);
		  s := copy(s,i+7,length(s));

		  i:=pos('</tr>', s);
		  while i>0 do begin
			persons_count:=persons_count+1;
			SetLength(persons, persons_count);
			s2:=copy(s, 1, i-1);
			// parse small cover
			small_cover := '';
			k := Pos('<img src="http://ia.media-imdb.com/images/', s2);
			if k >0 then begin
				l := MyPos('"', s2, k+11);
				small_cover := copy(s2, k+10, l-k-10);
				k := Pos('_V1._', small_cover);
				if k = 0 then
					k := Pos('_V1_', small_cover);
				if k>0 then begin
					l := MyPos('_.', small_cover, k+5);
					s3 := copy(small_cover, l, length(small_cover)-l+1);
					delete(small_cover, k+5, length(small_cover)-k-4);
					//set maximum image resolution
					small_cover := small_cover + 'SY100_SX100' +  s3;
				end;
			end;
			j:=Pos('</td>', s2);
			j:=MyPos('</td>', s2, j+1);
			s2:=copy(s, j+5, length(s2)-j-4);

			code:=ParseString(s2, '<a href="/name/nm(\d+)/"', 1);
			
			// parse real name
			j := Pos('</a>', s2);
			Name := copy(s2, 1, j-1);
			Name := RemoveTags(Name);
			Name := DecodeHTML(Name);
			Name := ClearExtraSpaces(Name);
			
			//parse additional information
			Description := ParseString(s2, '<small>\((.*?)\)</small>', 1);
			Description := RemoveTags(Description);
			Description := DecodeHTML(Description);
			
			//parse original name
			original_name := ParseString(s2, 'birth name <em>"(.*?)"</em>', 1);
			original_name := DecodeHTML(original_name);
			
			while (length(s2)>0) and (s2[1]=' ') do
				delete(s2, 1, 1);
			SetSearchResult2(Name, code, '', original_name, Description, small_cover);
			// SetSearchResult(s2, imdb_code);
			delete(s, 1, i+4);
			i:=pos('</tr>', s);
		  end;
		end;
	end;
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
	if images_only then
		result:=format('http://www.imdb.com/name/nm%s', [code])
	else
		result:=format('http://www.imdb.com/name/nm%s', [code])
end;

//gets uls of photos
procedure GetPhotos;
var
	url, s, photo_url: string;
	i, j, k, picSizeX, picSizeY: Integer;
	photos_count: Integer;
begin
	//all:=DownloadURL(url);
	all := GetHTMLPageContentFromRequest(BasicURL + '/mediaindex');
	if (jTraceLevel >= 5) then TraceExecute('Mediaindex',all);   // 20101001-JeF
	picSizeX := RegReadInteger('IMDBPicSizeX', defaultPicSizeX);
	picSizeY := RegReadInteger('IMDBPicSizeY', defaultPicSizeY);
	if (all <> '')and(all <> '-1') then
	begin
		j := pos('<div class="media_index_thumb_list', all);
		k := MyPos('</div>', all, j);
		photos_count := 0;
		if (j > 0)and(k > 0) then
		begin
			s := copy(all, j, k-j);
			i := pos('src=', s);
			while (i > 0)and(photos_count < MaxPhotosCount) do
			begin
				i := MyPos('"', s, i) + 1;
				j := MyPos('"', s, i);
				photo_url := copy(s, i, j-i);
				i := pos('V1._', photo_url);
				if i = 0 then
					i := pos('V1_', photo_url);
				if i>0 then begin
					photo_url := copy(photo_url, 1, i+1);
					if picSizeX>0 then
						photo_url := photo_url + '_SX' + IntToStr(picSizeX);
					if picSizeY>0 then
						photo_url := photo_url + '_SY' + IntToStr(picSizeY);
					if (picSizeX>0) or (picSizeY>0) then
						photo_url := photo_url + '_';
					photo_url := photo_url + '.jpg';
				end;
				SetImageName('', photo_url, '', 6);
				photos_count := photos_count + 1;
				delete(s, 1, j);
				i := pos('src=', s);
			end;
		end;
	end;
end;

//returnes images links
procedure GetImages;
begin
	if photo_url<>'' then
		SetImageName('', photo_url, '', 6);
	if MaxPhotosCount > 0 then
		GetPhotos;
	//GetGoogleImage;
end;

function GetGoogleImage: boolean;
var
	all, url: string;
	i, j: integer;
begin
	url:=format('http://images.google.com/images?svnum=20&hl=en&lr=&safe=on&c2coff=1&q=%%22%s%%22', [person]);
	url:=ReplaceStr(url, ' ', '+', -1);
	all:=DownloadURL(url);
	if (jTraceLevel >= 9) then TraceExecute('GoogleImage',all);   // 20101001-JeF
	i:=pos('dyn.Img(', all);
	if i>0 then begin
		i:=MyPos('","', all, i+1);
		i:=MyPos('","', all, i+1);
		i:=MyPos('","', all, i+1) + 3;
		j:=MyPos('","', all, i+1);
		if j>0 then begin
			url:=copy(all, i, j-i);
			SetImageName('', url, '', 6);
		end;
	end;
end;

function SavePhoto(url, FileName: string): boolean;
var
	fs: TFileStream;
	s: string;
begin
	s:=DownloadURL(url);
	if (length(s)>0) and (s<>'-1') then begin
		try
			fs:=TFileStream.Create(FileName, fmCreate);
			fs.Write(s, Length(s));
			fs.Free;
			result:=true;
		except
			result:=false;
		end;
	end
	else
		result:=false;
end;

function MovePrefixLeft(name: string): string;
var
	i, j: integer;
begin
	// replace "The", "A", "An"... to the left
	for j:=1 to PCount do
	begin
		i:=pos(', '+Prefixes[j], name);
		if i>0 then
			if (i = length(name)-length(Prefixes[j])-1) or
			(length(name) >= i+2+length(Prefixes[j])) and (name[i+2+length(Prefixes[j])]=':') then
			begin
				if Prefixes[j][length(Prefixes[j])] = '''' then
					name := Prefixes[j] + copy(name, 1, i-1) + copy(name, i+length(Prefixes[j])+2, length(name)-i-length(Prefixes[j])-2+1)
				else
					name := Prefixes[j]+' ' + copy(name, 1, i-1) + copy(name, i+length(Prefixes[j])+2, length(name)-i-length(Prefixes[j])-2+1);
				break;
			end;
	end;
	Result := name;
end;

function ClearBrackets(name: string): string;
var
	i: integer;
begin
	// clear word in brackets at the end of name
	i := pos(' (', name);
	if i>0 then
		name := copy(name, 1, i-1);
	Result := name;
end;

function ClearExtraQuotes(name: string): string;
begin
	// Clears extra quotes «"»
	if length(name)>0 then
		if (name[1]='"') and (name[length(name)]='"') then
			name := copy(name,2,length(name)-2);
	Result := name;
end;

// 20090729-JeF
function ClearExtraBlankCharacters(text: string): string;
begin
	// Clear all extra blank characters
	while (length(text) > 0) and (ord(text[1]) in [1..20, 32]) do
		delete(text, 1, 1);
	while (length(text) > 0) and (ord(text[length(text)]) in [1..20, 32]) do
		delete(text, length(text), 1);
	Result := text;
end;

function InList(Item, List: string): boolean;
begin
	result:=(pos(','+Item+',',','+List+',')>0);
end;

procedure ExtractMovies(text: String; mode: integer);
var
	i, j, k, l, local_mode: Integer;
	s, s2, s3, name, character, year: String;
begin
	local_mode := mode;
	if mode = 5 then
		local_mode := 3;
	i := Pos('<div class="clear"/>&nbsp;</div>', text);
	while i>0 do begin
		s := copy(text, 1, i-1);
		k := Pos('<b>', s);
		if k>0 then begin
			k := MyPos('<a ', s, k+1);
			j := MyPos('<br/>', s, k+1);
			s2 := copy(s, k, j-k);
			//skip video games
			if  (Pos('(Video Game)', s2)=0) then begin
				code := ParseString(s2, ' href="/title/tt(\d+)/', 1);
				name := ParseString(s2, '<a .*?>(.*?)</a></b>', 1);
				name := DecodeHTML(name);
				name := ClearExtraBlankCharacters(name);
				year := ParseString(s, '"year_column">(\d+)</span>', 1);
				if year='' then
					year := ParseString(s, '"year_column">(.*?)</span>', 1);
				k := Pos('/', year);
				if k>0 then
					delete(year, k, length(year)-k+1);
				character := '';
				if mode = 3 then begin
					k := MyPos('<br/>', s, j);
					if k>0 then begin
						l := MyPos('<div', s, k+1);
						if l<k then
							l := Length(s) + 1;
						character := copy(s, k, l-k);
						character := RemoveTags(character);
						character := DecodeHTML(character);
						character := ClearExtraBlankCharacters(character);
						k := Pos('(', character);
						if (pos('episode', character)>k) 								// 20101001-JeF
							then k:=MyPos(')',character,pos('episode', character))+1;   // 20101001-JeF
						character := ReplaceStr(character,#13,' ',-1);					// 20101001-JeF
						character := ReplaceStr(character,#10,' ',-1);					// 20101001-JeF
						character := ReplaceStr(character,'   ',' ',-1);				// 20101001-JeF
						character := ReplaceStr(character,'  ',' ',-1);					// 20101001-JeF
						if k>0 then
							delete(character, k, length(character) - k + 1);
					end;
				end;
				if mode = 5 then
					character := 'self';
				SetMovieName(name, character, local_mode, code, ProcessMovieYear(year));
			end;
		end;
		delete(text, 1, i+1);
		i := Pos('<div class="clear"/>&nbsp;</div>', text);
	end;
end;

function RemoveSpacesInText(text: String): String;
var
	i: Integer;
begin
	i := Pos('  ', text);
	while i>0 do begin
		delete(text, i, 1);
		i := Pos('  ', text);
	end;
	result := text;
end;

function GetInfo(url: string; images_only: boolean): string;
var
	day, month, year, s, s2, s3, s4, jAltNames, temp: string;
	i, j, k, l, ke, picSizeX, picSizeY: integer;
begin
	result:='';
	i:=pos('/maindetails', url);
	if i>0 then
		BasicURL:=copy(url, 1, i-1)
	else
		BasicURL:=url;
	if (jTraceLevel > 0) then TraceAlert;   			   // 20101001-JeF
	
	if (MaxPhotosCount > 1) then
		AddRequestForHTMLPageContent(BasicURL + '/mediaindex', False);
	if DownloadPersonBiography or DownloadPersonAdditionalInfo then
		AddRequestForHTMLPageContent(BasicURL + '/bio', False, '', '', RequestParameters);
		
	if DownloadPersonAwards then
		AddRequestForHTMLPageContent(BasicURL + '/awards', False, '', '', RequestParameters);
		
	imdb_code:=ParseString(url, 'www.imdb.com/name/nm(\d+)', 1);
	//i := length('http://www.imdb.com/name/nm');
	//imdb_code := copy(url, i+2, length(url)-i-1);
	person_code:=imdb_code;
	person_site_url:=GetPersonSiteURL(person_code);

	all:=DownloadURLWithReferer(url, '', True, RequestParameters);
	//if length(all)<100 then
		//ShowMessage(all);

	if (jTraceLevel >= 2) then TraceExecute('Info',all);   // 20101001-JeF
	//need to check for 'Http/1.1 Service Unavailable'?
	if (all='-1') {or (length(all)<100)} then begin
		result:='-1';
		//exit;
	end
	else
		result:=all;

	s2 := ParseString(all, '<title>(.*)</title>', 1);
	person := s2;
	person := ClearBrackets(person);
	person := DecodeHTML(person);
	
	i := Pos(' - IMDb', person);
	if i>0 then
		delete(person, i, length(person) - i + 1);

	s2:='IMDb - ';
	i:=pos(s2, person);
	if i>0 then
		delete(person, i, length(s2));

	original_name:='';

	//person photo
	picSizeX := RegReadInteger('IMDBPicSizeX', defaultPicSizeX);
	picSizeY := RegReadInteger('IMDBPicSizeY', defaultPicSizeY);
	s2 := '<a name="headshot" ';
	i := pos(s2, all);
	if i=0 then
		i := Pos('<td id="img_primary"', all);
	if i > 0 then
	begin
		i := MyPos('src="',all,i+1) + 5;
		j := MyPos('"',all,i+1);
		if j > 0 then
			photo_url := copy(all,i,j-i);
		i := Pos('_V1._', photo_url);
		if i = 0 then
			i := Pos('_V1_', photo_url);
		if i>0 then begin
			delete(photo_url, i + 3, length(photo_url) - i - 2);
			if picSizeX>0 then
				photo_url := photo_url + '_SX' + IntToStr(picSizeX);
			if picSizeY>0 then
				photo_url := photo_url + '_SY' + IntToStr(picSizeY);
			if (picSizeX>0) or (picSizeY>0) then
				photo_url := photo_url + '_';
			photo_url := photo_url + '.jpg';
		end;
	end;
	
	if photo_url='http://ia.imdb.com/media/imdb/01/I/24/27/20/10.gif' then
		photo_url:=''
	else if photo_url='http://i.imdb.com/Help/nohs-m.gif' then
		photo_url:=''
	else if photo_url='http://i.imdb.com/Help/nohs-f.gif' then
		photo_url:=''
	else if photo_url='http://i.imdb.com/Help/nohs.gif' then
		photo_url:='';
	if Pos('nopicture', photo_url)>0
		then photo_url := '';

	//date of birth
	i:=pos('<h5>Date of Birth:</h5>', all);
	if i = 0 then
		i := Pos('Born:</h4>', all);
	if i>0 then begin
		j:=MyPos('</div>', all, i+1);
		s2:=copy(all, i, j-i-1);
		//date
		i:=MyPos('a href="/OnThisDay?', s2,1);
		if (i=0) then i:=MyPos('a href="/date/', s2,1);   // 20100224-JeF
		if (i=0) then i:=Pos('birth_monthday', s2);
		if (i=0) then i:=Pos('birth_year', s2);
		if i>0 then
    begin
			j:=MyPos('</a>', s2, i+1)+4;
			s3:=copy(s2, i, j-i);
			birth_date:=RemoveTags('<' + s3);
			i:=pos(' ', birth_date);
			if i>0 then begin
				day:=copy(birth_date, 1, i-1);
				month:=copy(birth_date, i+1, length(birth_date)-i);
				
				if length(day) > 2 then begin
					s3 := day;
					day := month;
					month := s3;
				end;
				
				i:=MyPos('a href="/BornInYear?', s2,1);
				if (i=0) then i:=MyPos('birth_year', s2,1);  // 20100214-JeF
				if i>0 then begin
					j:=MyPos('</a>', s2, i+1)+4;
					s3:=copy(s2, i, j-i);
					year:=RemoveTags('<' + s3);

					i:=1;
					while (i<=12) and (monthes[i]<>month) do begin	
						i:=i+1;
					end;
					if (i<=12) and (month<>'') then begin
						birth_date:=IntToStr(i)+format('/%s/%s', [day, year]);
					end;
				end;
			end;
		end;

		i:=MyPos('<a href="/BornWhere', s2,1);
		if (i=0) then i:=MyPos('href="/search/name?birth_place', s2,1);  // 20101001-JeF
		if i>0 then begin
			j:=MyPos('</a>', s2, i+1)+4;
			s3:='<'+copy(s2, i, j-i);
			birthplace:=RemoveTags(s3);
		end;
	end;
	

	death_date:='';
	//Date of Death
	i:=pos('<h5>Date of Death:</h5>', all);
		if i = 0 then
		i := Pos('Died:</h4>', all);
	if i>0 then begin
		j:=MyPos('</div>', all, i+1);
		s2:=copy(all, i, j-i-1);
		//date
		s3 := GetStr(s2, 'itemprop="deathDate" datetime="', '"');
		if s3<>'' then begin
			year := ParseString(s3, '^(\d+)-', 1);
			month := ParseString(s3, '-(\d+)-', 1);
			day := ParseString(s3, '-(\d+)$', 1);
			if (day<>'') and (month<>'') and (year<>'') then
				death_date := Format('%s/%s/%s', [month, day, year]);
		end
		else begin
			i:=MyPos('a href="/OnThisDay?', s2,1);
			if (i=0) then i:=MyPos(' href="/date/', s2,1);   // 20100224-JeF
			if i>0 then begin
				j:=MyPos('</a>', s2, i+1)+4;
				s3:=copy(s2, i, j-i);
				death_date:=RemoveTags('<' + s3);
				i:=pos(' ', death_date);
				if i>0 then begin
					day:=copy(death_date, 1, i-1);
					month:=copy(death_date, i+1, length(death_date)-i);
					
					if length(day) > 2 then begin
						s3 := day;
						day := month;
						month := s3;
					end;
					
					i:=MyPos('a href="/DiedInYear?', s2,1);
					if (i=0) then i:=MyPos(' href="/search/name?death_date', s2,1);  // 20100214-JeF
					if (i=0) then i:=MyPos('a href="/search/name?death_year', s2,1);  // 20100214-JeF
					if i>0 then begin
						j:=MyPos('</a>', s2, i+1)+4;
						s3:=copy(s2, i, j-i);
						year:=RemoveTags('<' + s3);

						i:=1;
						while (i<=12) and (monthes[i]<>month) do begin	
							i:=i+1;
						end;
						if (i<=12) and (month<>'') then begin
							death_date:=IntToStr(i)+format('/%s/%s', [day, year]);
						end;
					end;
				end;
			end;
		end;
	end;

	// Alternate Names // 20091002-JeF_Begin
	jAltNames:='';
	if (RegReadInteger('IMDBDownloadAlternateNames',0)>0) then begin
		i:=pos('<h5>Alternate Names:</h5>', all);
		if i>0 then begin
			if (biography>'') then biography:=biography+'<br><br>';
			biography:=biography+'Alternate Name(s):';
			i:=i+25;
			j:=MyPos('</div>', all, i+1);
			s2:=copy(all, i, j-i);
			s2:=DecodeHTML(s2);
			s2:=RemoveTags(s2);
			s2:=ReplaceStr(s2, #10, ' ', -1);
			s2:=ClearExtraCharacters(s2);
			s2:=ClearExtraSpaces(s2);
			jAltNames:=s2;
		end;
	end;
	// 20091002-JeF_End
	
	//Actor - filmography
	i:=pos('<h5><a name="actor">Actor:</a></h5>', all);
	if i=0 then
		i:=pos('<h5><a name="actress">Actress:</a></h5>', all);
	if (i>0) and (RegReadInteger('IMDBDownloadFilmoAsActor',1)>0) then begin
		k:=MyPos('<ol>', all, i+1);
		j:=MyPos('</ol>', all, i+1);
		s2:=copy(all, i, j-i);

		i:=pos('</li>', s2);
		while i>0 do begin
			s3:=copy(s2, 1, i+4);
			if (Pos('(VG)', s3)>0) then
			//skip video games
			else begin
				k:=pos('href="', s3);
				if k>0 then begin
					ke:=MyPos('<br>', s3, k+1);
					if ke>0 then begin
						s3:=copy(s3, 1, ke+3);
					end; 
				end;
				code:=ParseString(s3, 'href="/title/tt(\d+)/"', 1);
				name:=ParseString(s3, 'href="/title/tt.*?">(.*?)</a>', 1);   // 20091105-JeF
				year:=ParseString(s3, '</a> \((\d+)', 1);
				
				if (year='') then year:=ParseString(s3, ', (\d+)', 1);   // 20090724-JeF

				if name<>'' then begin
					name := RemoveTags(name);
					name := DecodeHTML(name);
					name := ClearExtraSpaces(name);
					name := ClearBrackets(name);
					name := ClearExtraSpaces(name);
					name := ClearExtraQuotes(name);
					name := MovePrefixLeft(name);
					//character
					character:='';
					j:=pos('.... ', s3);
					if j>0 then begin
						k:=MyPos('<br>', s3, j+1);
						if k>0 then begin
							character:=copy(s3, j+5, k-j-5);
						end
						else begin
							k:=MyPos(#10, s3, j+1);
							if k>0 then begin
								character:=copy(s3, j+5, k-j-5);
							end
							else begin
								k:=MyPos('</li>', s3, j+1);
								if k>0 then begin
									character:=copy(s3, j+5, k-j-5);
								end
							end;
						end;
					end
					// 20090714-JeF_Begin
					else begin
						j:=pos('</a>',s3);
						delete(s3,1,j-1);
						s3:=RemoveTags(s3);
						s3:=DecodeHTML(s3);
						s3:=ClearExtraSpaces(s3);
						if (pos('(',s3)>0) and (pos(')',s3)>0) 
							and (pos(')',s3)-pos('(',s3)>5)   // 20091002-JeF
							then character:=s3;
					end;
					// 20090714-JeF_End

					character:=RemoveTags(character);
					character:=DecodeHTML(character);
					character:=ClearExtraSpaces(character);
					SetMovieName(name, character, 3, code, ProcessMovieYear(year));
				end;
			end;
			delete(s2, 1, i+4);
			i:=pos('</li>', s2);
		end;
	end
	//New design
	else if (RegReadInteger('IMDBDownloadFilmoAsActor',1)>0) then begin
		i:=Pos('<a name="Actor">Actor</a>', all);
		if i=0 then
			i:=Pos('<a name="Actress">Actress</a>', all);
		if i>0 then begin
			j:=MyPos('<div id="', all, i+1);
			s2:=copy(all, i, j-i);
			ExtractMovies(s2, 3);
		end;
	end;
	
	//Self
	if (RegReadInteger('IMDBDownloadFilmoAsSelf',0)>0) then begin
		i:=Pos('<a name="Self">Self</a>', all);
		if i>0 then begin
			j:=MyPos('<div id="', all, i+1);
			s2:=copy(all, i, j-i);
			ExtractMovies(s2, 5);
		end;
	end;
	
	//Director - filmography 
	i:=pos('<h5><a name="director">Director:</a></h5>', all);
	if (i>0) and (RegReadInteger('IMDBDownloadFilmoAsDirector',1)>0) then begin
		i:=MyPos('<ol>', all, i+1);
		j:=MyPos('</ol>', all, i+1);
		s2:=copy(all, i, j-i);

		i:=pos('</li>', s2);
		while i>0 do begin
			s3:=copy(s2, 1, i+4);
			code:=ParseString(s3, 'href="/title/tt(\d+)/"', 1);
			name:=ParseString(s3, 'href="/title/tt.*?">(.*?)</a>', 1);   // 20091105-JeF
			year:=ParseString(s3, '</a> \((\d+)', 1);
			if name<>'' then begin
				name := RemoveTags(name);
				name := DecodeHTML(name);
				name := ClearExtraSpaces(name);
				name := ClearBrackets(name);
				name := ClearExtraSpaces(name);
				name := ClearExtraQuotes(name);
				name := MovePrefixLeft(name);
				SetMovieName(name, '', 1, code, ProcessMovieYear(year));
			end;
			delete(s2, 1, i+4);
			i:=pos('</li>', s2);
		end;
	end
	else if (RegReadInteger('IMDBDownloadFilmoAsDirector',1)>0) then begin
		i:=Pos('<a name="Director">Director</a>', all);
		if i>0 then begin
			j:=MyPos('<div id="', all, i+1);
			s2:=copy(all, i, j-i);
			ExtractMovies(s2, 1);
		end;
	end;

	//Writer - filmography 
	i:=pos('<h5><a name="writer">Writer:</a></h5>', all);
	if (i>0) and (RegReadInteger('IMDBDownloadFilmoAsWriter',1)>0) then begin
		i:=MyPos('<ol>', all, i+1);
		j:=MyPos('</ol>', all, i+1);
		s2:=copy(all, i, j-i);

		i:=pos('</li>', s2);
		while i>0 do begin
			s3:=copy(s2, 1, i+4);
			code:=ParseString(s3, 'href="/title/tt(\d+)/"', 1);
			name:=ParseString(s3, 'href="/title/tt.*?">(.*?)</a>', 1);   // 20091105-JeF
			year:=ParseString(s3, '</a> \((\d+)', 1);
			if name<>'' then begin
				name := RemoveTags(name);
				name := DecodeHTML(name);
				name := ClearExtraSpaces(name);
				name := ClearBrackets(name);
				name := ClearExtraSpaces(name);
				name := ClearExtraQuotes(name);
				name := MovePrefixLeft(name);
				SetMovieName(name, '', 2, code, ProcessMovieYear(year));
			end;
			delete(s2, 1, i+4);
			i:=pos('</li>', s2);
		end;
	end
	else if (RegReadInteger('IMDBDownloadFilmoAsWriter',1)>0) then begin
		i:=Pos('<a name="Writer">Writer</a>', all);
		if i>0 then begin
			j:=MyPos('<div id="', all, i+1);
			s2:=copy(all, i, j-i);
			ExtractMovies(s2, 2);
		end;
	end;

	//Composer - filmography 
	i:=pos('<h5><a name="composer">Composer:</a></h5>', all);
	if (i>0) and (RegReadInteger('IMDBDownloadFilmoAsComposer',1)>0) then begin
		i:=MyPos('<ol>', all, i+1);
		j:=MyPos('</ol>', all, i+1);
		s2:=copy(all, i, j-i);

		i:=pos('</li>', s2);
		while i>0 do begin
			s3:=copy(s2, 1, i+4);
			code:=ParseString(s3, 'href="/title/tt(\d+)/"', 1);
			name:=ParseString(s3, 'href="/title/tt.*?">(.*?)</a>', 1);   // 20091105-JeF
			year:=ParseString(s3, '</a> \((\d+)', 1);
			if name<>'' then begin
				name := RemoveTags(name);
				name := DecodeHTML(name);
				name := ClearExtraSpaces(name);
				name := ClearBrackets(name);
				name := ClearExtraSpaces(name);
				name := ClearExtraQuotes(name);
				name := MovePrefixLeft(name);
				SetMovieName(name, '', 4, code, ProcessMovieYear(year));
			end;
			delete(s2, 1, i+4);
			i:=pos('</li>', s2);
		end;
	end
	else if (RegReadInteger('IMDBDownloadFilmoAsComposer',1)>0) then begin
		i:=Pos('<a name="Composer">Composer</a>', all);
		if i>0 then begin
			j:=MyPos('<div id="', all, i+1);
			s2:=copy(all, i, j-i);
			ExtractMovies(s2, 4);
		end;
	end;

	movie_html := all; // for photos downloading

	//additional info
	if DownloadPersonBiography or DownloadPersonAdditionalInfo then begin
		//url:=BasicURL+'/bio';
		all:=GetHTMLPageContentFromRequest(BasicURL + '/bio');
		if (jTraceLevel >= 3) then TraceExecute('Bio',all);   // 20101001-JeF

		//birth name
		birth_name:='';     // 20090729-JeF
		i:=pos('<h5>Birth Name</h5>', all);
		if i>0 then begin
			i:=i+length('<h5>Birth Name</h5>');     // 20090729-JeF
			j:=MyPos('<br/>', all, i+1);
			birth_name:=copy(all, i, j-i-1);
			birth_name:=RemoveTags(birth_name);
			birth_name:=DecodeHTML(birth_name);
			birth_name:=ClearExtraBlankCharacters(birth_name);     // 20090729-JeF
			birth_name:=ClearExtraSpaces(birth_name);
		end;
		original_name:=birth_name;     // 20090729-JeF
		if DownloadPersonBiography then begin
			//biography
			i:=pos('<h5>Mini Biography</h5>', all);
			if i>0 then begin
				i:=MyPos('<p>', all, i+1) + 3;
				j:=MyPos('</p>', all, i+1);
				biography:=copy(all, i, j-i);
				biography:=DecodeHTML(biography);
				biography:=ReplaceStr(biography, '</p>', #10, -1);
				biography:=ReplaceStr(biography, '<br>', #10, -1);
				biography:=RemoveTags(biography);
				biography:=ClearExtraCharacters(biography);
				biography:=ClearExtraSpaces(biography);
				biography:=ReplaceStr(biography, #10, '<br>', -1);
			end;
		end;
		
		if DownloadPersonAdditionalInfo then begin
			// Biography - Spouses
			ExtractSpouses();

			if (RegReadInteger('IMDBDownloadBiography',1)=0) then biography:='';

			// Biography - Alternate Names // 20091002-JeF_Begin
			if jAltNames<>'' then begin
				if (biography>'') then biography:=biography+'<br><br>';
				biography:=biography+'Alternate Name(s):';
				biography:=biography+'<br>'+jAltNames;
			end;
			// 20091002-JeF_End

			// Biography - Nick Names // 20091002-JeF_Begin
			if (RegReadInteger('IMDBDownloadNickNames',0)>0) then begin
				i:=pos('<h5>Nickname</h5>', all);
				if i>0 then begin
					if (biography>'') then biography:=biography+'<br><br>';
					biography:=biography+'Nickname(s):';
					i:=i+17;
					j:=MyPos('<h5>', all, i+1);
					s2:=copy(all, i, j-i);
					s2:=DecodeHTML(s2);
					s2:=ReplaceStr(s2, #10, ' ', -1);
					s2:=ReplaceStr(s2, '<br/>',#10, -1);
					s2:=RemoveTags(s2);
					s2:=ReplaceStr(s2, #10,'<br>', -1);
					s2:=ClearExtraCharacters(s2);
					s2:=ClearExtraSpaces(s2);
					s2:=ReplaceStr(s2, '<br> <br>','', -1);
					biography:=biography+'<br>'+s2;
				end;
			end;
			// 20091002-JeF_End
			
			//description
			i:=pos('<h5>Trivia</h5>', all);
			if i>0 then begin
				i:=MyPos('<p>', all, i+1) + 3;
				j:=MyPos('<br/>', all, i+1);
				if j=0 then j:=MyPos('<h5>', all, i+1);
				if j=0 then j:=MyPos('<script', all, i+1);
				if j=0 then j:=MyPos('<div', all, i+1);
				trivia:=copy(all, i, j-i-1);
				trivia:=DecodeHTML(trivia);
				trivia:=ReplaceStr(trivia, '</p>', #13#10, -1);
				trivia:=ReplaceStr(trivia, '<br>', #13#10, -1);
				trivia:=RemoveTags(trivia);
				trivia:=ClearExtraBlankCharacters(trivia);
			end;

			//salary
			i:=pos('<h5>Salary</h5>', all);
			if i>0 then begin
				salary:='<br><br>Salary:<br>';
				j:=MyPos('</table>', all, i+1);
				s2:=copy(all, i, j-i-1);
				i:=pos('</tr>', s2);
				while i>0 do begin
					s3:=copy(s2, 1, i+4);
					name:=ParseString(s3, 'href="/title/tt\d+/">(.*?)</a>', 1);
					name:=RemoveTags(name);
					name:=DecodeHTML(name);
					s4:=ParseString(s3, '<td valign="top">(.*?)</td>', 1);
					s4:=DecodeHTML(s4);
					s4:=RemoveTags(s4);
					s4:=DecodeHTML(s4);
					s4:=ClearExtraSpaces(s4);
					salary:=salary+name+' '+s4+#13#10;
					delete(s2, 1, i+4);
					i:=pos('</tr>', s2);
				end;
				//if (length(salary)>3) then delete(salary,length(salary)-3,4);    // 20090730-JeF : Remove last <br>
			end;
			salary := ClearExtraBlankCharacters(salary);
			
			description := trivia + salary;
			if (RegReadInteger('IMDBDownloadPersonDescription', 1)=0) then description:='';
			
			// Personal Quotes // 20090730-JeF_Begin
			if (RegReadInteger('IMDBDownloadPersonalQuotes',0)>0) then begin
				i:=pos('<h5>Personal Quotes</h5>', all);
				if (i>0) then begin
					if (description>'') then description:=description+'<br><br>';
					description:=description+'Personal Quotes:<br>';
					j:=MyPos('<br/>',all,i+1);
					i:=MyPos('<p>',all,i+1);
					s2:=copy(all,i,j-i);
					s2:=ReplaceStr(s2, #10, '', -1);
					s2:=ReplaceStr(s2, '</p>', #13#10, -1);
					s2:=DecodeHTML(s2);
					s2:=RemoveTags(s2);
					s2:=ClearExtraBlankCharacters(s2);
					if (s2>'') then description:=description+s2;
				end;
			end;
			// 20090730-JeF_End
			
			person_height := 0;
			i := pos('<h5>Height</h5>', all);
			if i>0 then begin
				j := MyPos('<br/>', all, i);
				s := copy(all, i, j-i);
				i := pos('(', s);
				j := MyPos(')', s, i);
				if (i>0)and(j>0) then begin
					i := i + 1;
					s2 := copy(s, i, j-i);
					i := pos(' ', s2);
					s2 := copy(s2, 1, i-1);
					s2 := ClearExtraSpaces(s2);
					i := pos('.', s2);
					delete(s2, i, 1);
					try
						person_height := StrToInt(s2);
					except
					end;
				end;
			end;
		end;
	end;
	
	if DownloadPersonAwards then begin
		all := GetHTMLPageContentFromRequest(BasicURL + '/awards');
		i:=Pos('<big><a href="/Sections/Awards/', all);
		if i>0 then begin
			//we copy the table of awards
			j:=MyPos('</table>', all, i+1);
			s:=copy(all, i, j-i);
			repeat begin
				j:=Pos('<td colspan="4">&nbsp;</td>', s);
				s2:=copy(s, 1, j);
				//Who gives the award
				s3:=ParseString(s2, '<big>.*?">(.*?)</a></big>', 1);
				if s3<>'' then begin
					if awards<>'' then
						awards:=awards+'<br><br>';
					awards:=awards+s3+'<br>';
				end
				else
					awards := awards + '<br>';
				//Year
				year:=ParseString(s2, '">(\d+) </a>', 1);

				k:=Pos('<b>Won</b>', s2);
				if k>0 then begin
					l:=MyPos('<b>Nominated</b>', s2, k+1);
					if l=0 then
						l:=length(s2);
					s3:=copy(s2, k, l-k);
					awards:=awards+ExtractAwards(s3, year, 1);
				end;
				k:=Pos('<b>Nominated</b>', s2);
				if k>0 then begin
					s3:=copy(s2, k, length(s2) - k);
					awards:=awards+ExtractAwards(s3, year, 0);
				end;
				k := Pos(' place</b>', s2);
				if k>0 then begin
					s3:=copy(s2, k, length(s2) - k);
					awards:=awards+ExtractAwards(s3, year, 2);
				end;
				delete(s, 1, j+10);
				i:=Pos('<big><a href="/Sections/Awards/', s);
				end
			until i=0;
		end;
		awards := ReplaceStr(awards, '<br>', #13#10, -1);
		awards:=DecodeHTML(RemoveTags(awards));
	end;

end;

//status 0 - nominated, 1 - won, 2 - some place
function ExtractAwards(text, year: string; status: integer): string;
var
	i, j, k, l, m: integer;
	s, s2, name, subject, extracted_awards: string;
begin
	result := '';
	extracted_awards:='';
	i:=Pos('<td valign="top">', text);
	m := 0;
	while i>0 do begin
		//name of the award
		name := GetStr(text, '<td ', '</td>');
		if name <> '' then begin
			j := Pos('<', name);
			if j > 0 then
				delete(name, j, length(name)-j+1);
			name := ClearExtraBlankCharacters(RemoveTags('<' + name));
		end;
		if m = 0 then
			name := name + ' - ' + year;
		if (Pos(name, awards) = 0) or (m > 0) then begin
			if (extracted_awards<>'') then
				extracted_awards:=extracted_awards+'<br>';
			extracted_awards := extracted_awards + name;
		end;
		
		if m = 0 then begin
			if (status = 1) then
				extracted_awards := extracted_awards + '<br>Won'
			else
				extracted_awards := extracted_awards + '<br>Nominated';
		end;
			
		//what the award is for
		subject := GetStr(text, '<td valign="top">', '<br>');
		if (Pos('<b>for:</b>', subject) > 0) or (Pos('<small>', subject) > 0) then begin
			subject := '';
		end;
		subject:=ClearExtraBlankCharacters(RemoveTags(subject));
		if (subject <> '') and (subject <> name) then
			extracted_awards := extracted_awards + '<br>' + subject;
		//Now which movie got the award
		s := GetStr(text, '<b>for:</b>', '<br />');
		k:=Pos('<a href="/title/', s);
		if (k>0) and (extracted_awards<>'') then begin
			if subject <> '' then
				extracted_awards := extracted_awards + ' '
			else
				extracted_awards := extracted_awards + '<br>';
			l:=MyPos('</a>', s, k+1);
			s2:=copy(s, k, l-k);
			s2:=ClearExtraBlankCharacters(RemoveTags(s2));
			if s2<>'' then
				extracted_awards := extracted_awards + '(' + s2 + ')';
		end;
		delete(text, 1, i);
		i:=Pos('<td valign="top">', text);
		m := m + 1;
	end;
	Result := DecodeHTML(extracted_awards);  // 20090910-JeF
end;

//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.imdb.com/title/tt'+code;
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else	
		result:=format('http://www.imdb.com/name/nm%s', [code]);
end;

// File Exists Tool // 20091002-JeF
function MyFileExists(FileName: string): boolean;
var
	fs: TFileStream;
	s: string;
begin
	try
		fs:=TFileStream.Create(FileName, fmOpenRead);
		fs.Free;
		result:=true;
	except
		result:=false;
	end;
end;

// Boolean/String Conversion Tool // 20091002-JeF
function StrToBool(BoolStr: string): boolean;
var
	str: string;
begin
	str:=ClearExtraSpaces(BoolStr);
	if (uppercase(str)='TRUE') then result:=true else result:=false;
end;
function BoolToStr(Bool: boolean): string;
begin
	if (Bool) then result:='True' else result:='False';
end;

// Clear extra simple or double Quotes Tool // 20091002-JeF
function ClearExtraSDQuotes(name: string): string;
begin
	if length(name)>0 then
		if (name[1]='"') and (name[length(name)]='"')
		or (name[1]='''') and (name[length(name)]='''') then
			name := copy(name,2,length(name)-2);
	Result := name;
end;

// Trace Message when Trace is Active (Debug) // 20101001-JeF
procedure TraceAlert;
var
	s: string;
begin
	if (jTraceMode=1) then s:=GetLangStr(1687)
	else s:=GetLangStr(1688);
	showmessage(format(GetLangStr(1689),[s,IntToStr(jTraceLevel)]));
end;

// Save Trace To Disc (Debug) // 20101001-JeF
procedure SaveTraceToDisc(DumpName, DumpData: string);
var
	DumpFile: string;
	sl: TStringList;
begin
	sl:=TStringList.Create;
	DumpFile:=ScriptsPath+script_name+'_'+DumpName+'.trc';
	try
		sl.Text:=DumpData;
		sl.SaveToFile(DumpFile);
	except
		sl.Clear;
	end;
	sl.Free;
end;

// Load Trace From Disc (Debug) // 20101001-JeF
function LoadTraceFromDisc(DumpName: string): string;
var
	DumpFile: string;
	LoadIt: Boolean;
	i: integer;
	s: string;
	sl: TStringList;
begin
	result:='';
	sl:=TStringList.Create;
	DumpFile:=ScriptsPath+script_name+'_'+DumpName+'.trc';
	LoadIt:=False;
	if MyFileExists(DumpFile) then begin
		if jTraceAsk then begin
			s:=Format(GetLangStr(1690),[DumpName]);
			i:=MessageDlg(s,mtCustom,mbYes+mbCancel, 0);
			LoadIt:=(i=mrYes);
		end;
		if LoadIt then begin
			try
				sl.LoadFromFile(DumpFile);
				result:=sl.Text;
			except
				result:='';
			end;
		end;
	end;
	sl.Free;
end;

// Load or Save Trace according to Trace Mode (Debug) // 20101001-JeF
procedure TraceExecute(DumpName: string; VAR DumpData: string);
var
	DumpFile, s: string;
begin
	s:=DumpData;
	if jTraceMode = 1 then begin
		s:=LoadTraceFromDisc(DumpName);
		if (s <> '') then DumpData:=s;
	end
	else SaveTraceToDisc(DumpName, DumpData);
end;

//show available settings for the script // 20101001-JeF
function ShowScriptOptions: integer;
var
	s: string;
	f: TForm;
	b: TButton;

	cbDlPersonalQuotes, cbDlAlternateNames, cbDlNickNames, cbDlDesc, cbDlBio, cbDlFilmoAsActor, cbDlFilmoAsDirector, cbDlFilmoAsWriter, cbDlFilmoAsComposer, cbDlFilmoAsSelf: TCheckBox;
	labLocalService, labPicSize, labPicMulti: TLabel;
	edPicSizeX, edPicSizeY: TEdit;
	comboLocalService: TComboBox;
	gbTrace: TGroupBox;								  // 20101001-JeF
	rbTraceModeSave, rbTraceModeLoad: TRadioButton;   // 20101001-JeF
	labTraceLevel: TLabel;							  // 20101001-JeF
	edTraceLevel: TEdit;							  // 20101001-JeF
	cbTraceAsk: TCheckBox;							  // 20101001-JeF
	my_value: integer;

procedure ButtonClick(Sender: TButton);
var
	zn: integer;
begin
	try
		if cbDlPersonalQuotes.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadPersonalQuotes', zn);	
		if cbDlAlternateNames.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadAlternateNames', zn);
		if cbDlNickNames.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadNickNames', zn);
		if cbDlDesc.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadPersonDescription', zn);	
		if cbDlBio.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadBiography', zn);	
		if cbDlFilmoAsActor.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadFilmoAsActor', zn);	
		if cbDlFilmoAsDirector.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadFilmoAsDirector', zn);
		if cbDlFilmoAsWriter.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadFilmoAsWriter', zn);	
		if cbDlFilmoAsComposer.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadFilmoAsComposer', zn);
		if cbDlFilmoAsSelf.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBDownloadFilmoAsSelf', zn);
		try 
			zn := StrToInt(edPicSizeX.Text);
			RegWriteInteger('IMDBPicSizeX', zn);
		except
		end;
		try
			zn := StrToInt(edPicSizeY.Text);
			RegWriteInteger('IMDBPicSizeY', zn);
		except
		end;
		
		// 20100923-JeF
		if rbTraceModeLoad.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBTraceMode', zn);
		try 
			zn := StrToInt(edTraceLevel.Text);
			RegWriteInteger('IMDBTraceLevel', zn);
		except
		end;
		if cbTraceAsk.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('IMDBTraceAsk', zn);
		
		RegWriteInteger('IMDBLocalService', comboLocalService.ItemIndex);
	except
	end;

	jTraceMode := RegReadInteger('IMDBTraceMode', 0);     // 20101001-JeF
	jTraceLevel := RegReadInteger('IMDBTraceLevel', 0);   // 20101001-JeF
	jTraceAsk := RegReadInteger('IMDBTraceAsk', 1);  	  // 20101001-JeF

	f.ModalResult := mrOk;
end;

begin
	result:=0;
	f := TForm.Create(nil);
	f.Caption := GetLangStr(1924);
	f.BorderStyle := bsDialog;
	f.Position := poScreenCenter;
	f.Width:=450;
	f.Width:=600;  // 20101001-JeF
	f.Height:=345;
	f.AutoSize:=false;

	cbDlPersonalQuotes := TCheckBox.Create(f);
	cbDlPersonalQuotes.Name := 'cbDlPersonalQuotes';
	cbDlPersonalQuotes.Parent := f;
	cbDlPersonalQuotes.SetBounds(10, 10, 350, 20);
	cbDlPersonalQuotes.Caption := GetLangStr(1674);
	cbDlPersonalQuotes.Hint:=GetLangStr(1675);
	my_value := RegReadInteger('IMDBDownloadPersonalQuotes', 0);
	if my_value=1 then
		cbDlPersonalQuotes.Checked := True;
	
	// 20101001-JeF
	gbTrace:=TGroupBox.Create(f);
	gbTrace.Name:='gbTrace';
	gbTrace.Parent:=f;
	gbTrace.SetBounds(380, 10, 200, 105);
	gbTrace.Caption:=GetLangStr(1691);
	gbTrace.Hint:=GetLangStr(1692);
	rbTraceModeSave:=TRadioButton.Create(f);
	rbTraceModeSave.Name:='rbTraceModeSave';
	rbTraceModeSave.Parent:=gbTrace;
	rbTraceModeSave.SetBounds(10, 20, 80, 20);
	rbTraceModeSave.Caption:=GetLangStr(1688);
	rbTraceModeSave.Hint:=GetLangStr(1693);
	rbTraceModeLoad:=TRadioButton.Create(f);
	rbTraceModeLoad.Name:='rbTraceModeLoad';
	rbTraceModeLoad.Parent:=gbTrace;
	rbTraceModeLoad.SetBounds(100, 20, 100, 20);
	rbTraceModeLoad.Caption:=GetLangStr(1687);
	rbTraceModeLoad.Hint:=GetLangStr(1694);
	my_value := RegReadInteger('IMDBTraceMode', 0);
	if my_value=0 then
		rbTraceModeSave.Checked := True
	else if my_value=1 then
		rbTraceModeLoad.Checked := True;
	labTraceLevel:=TLabel.Create(f);
	labTraceLevel.Name := 'labTraceLevel';
	labTraceLevel.Parent := gbTrace;
	labTraceLevel.SetBounds(10, 50, 100, 25);
	labTraceLevel.Caption := GetLangStr(1695);
	labTraceLevel.Hint:=GetLangStr(1696);
	edTraceLevel := TEdit.Create(f);
	edTraceLevel.Name := 'edTraceLevel';
	edTraceLevel.Parent := gbTrace;
	edTraceLevel.SetBounds(170, 45, 20, 25);
	edTraceLevel.Text := IntToStr(RegReadInteger('IMDBTraceLevel', 0));
	edTraceLevel.Hint := labTraceLevel.Hint;
	cbTraceAsk := TCheckBox.Create(f);
	cbTraceAsk.Name := 'cbTraceAsk';
	cbTraceAsk.Parent := gbTrace;
	cbTraceAsk.SetBounds(10, 80, 185, 20);
	cbTraceAsk.Caption := GetLangStr(1697);
	my_value := RegReadInteger('IMDBTraceAsk', 1);
	if my_value=1 then
		cbTraceAsk.Checked := True;

	cbDlAlternateNames := TCheckBox.Create(f);
	cbDlAlternateNames.Name := 'cbDlAlternateNames';
	cbDlAlternateNames.Parent := f;
	cbDlAlternateNames.SetBounds(10, 30, 350, 20);
	cbDlAlternateNames.Caption := GetLangStr(1676);
	cbDlAlternateNames.Hint := GetLangStr(1677);
	my_value := RegReadInteger('IMDBDownloadAlternateNames', 0);
	if my_value=1 then
		cbDlAlternateNames.Checked := True;
		
	cbDlNickNames := TCheckBox.Create(f);
	cbDlNickNames.Name := 'cbDlNickNames';
	cbDlNickNames.Parent := f;
	cbDlNickNames.SetBounds(10, 50, 350, 20);
	cbDlNickNames.Caption := GetLangStr(1678);
	cbDlNickNames.Hint := GetLangStr(1679);
	my_value := RegReadInteger('IMDBDownloadNickNames', 0);
	if my_value=1 then
		cbDlNickNames.Checked := True;
		
	cbDlDesc := TCheckBox.Create(f);
	cbDlDesc.Name := 'cbDlDesc';
	cbDlDesc.Parent := f;
	cbDlDesc.SetBounds(10, 70, 350, 20);
	cbDlDesc.Caption := GetLangStr(2700);
	cbDlDesc.Hint := GetLangStr(2701);
	my_value := RegReadInteger('IMDBDownloadPersonDescription', 1);
	if my_value=1 then
		cbDlDesc.Checked := True;
		
	cbDlBio := TCheckBox.Create(f);
	cbDlBio.Name := 'cbDlBio';
	cbDlBio.Parent := f;
	cbDlBio.SetBounds(10, 90, 370, 20);
	cbDlBio.Caption := GetLangStr(2702);
	cbDlBio.Hint := GetLangStr(2703);
	my_value := RegReadInteger('IMDBDownloadBiography', 1);
	if my_value=1 then
		cbDlBio.Checked := True;	
		
	cbDlFilmoAsActor := TCheckBox.Create(f);
	cbDlFilmoAsActor.Name := 'cbDlFilmoAsActor';
	cbDlFilmoAsActor.Parent := f;
	cbDlFilmoAsActor.SetBounds(10, 110, 370, 20);
	cbDlFilmoAsActor.Caption := GetLangStr(2704);
	cbDlFilmoAsActor.Hint := GetLangStr(2705);
	my_value := RegReadInteger('IMDBDownloadFilmoAsActor', 1);
	if my_value=1 then
		cbDlFilmoAsActor.Checked := True;
		
	cbDlFilmoAsDirector := TCheckBox.Create(f);
	cbDlFilmoAsDirector.Name := 'cbDlFilmoAsDirector';
	cbDlFilmoAsDirector.Parent := f;
	cbDlFilmoAsDirector.SetBounds(10, 130, 370, 20);
	cbDlFilmoAsDirector.Caption := GetLangStr(2706);
	cbDlFilmoAsDirector.Hint := GetLangStr(2707);
	my_value := RegReadInteger('IMDBDownloadFilmoAsDirector', 1);
	if my_value=1 then
		cbDlFilmoAsDirector.Checked := True;
		
	cbDlFilmoAsWriter := TCheckBox.Create(f);
	cbDlFilmoAsWriter.Name := 'cbDlFilmoAsWriter';
	cbDlFilmoAsWriter.Parent := f;
	cbDlFilmoAsWriter.SetBounds(10, 150, 370, 20);
	cbDlFilmoAsWriter.Caption := GetLangStr(2708);
	cbDlFilmoAsWriter.Hint := GetLangStr(2709);
	my_value := RegReadInteger('IMDBDownloadFilmoAsWriter', 1);
	if my_value=1 then
		cbDlFilmoAsWriter.Checked := True;
		
	cbDlFilmoAsComposer := TCheckBox.Create(f);
	cbDlFilmoAsComposer.Name := 'cbDlFilmoAsComposer';
	cbDlFilmoAsComposer.Parent := f;
	cbDlFilmoAsComposer.SetBounds(10, 170, 370, 20);
	cbDlFilmoAsComposer.Caption := GetLangStr(2710);
	cbDlFilmoAsComposer.Hint := GetLangStr(2711);
	my_value := RegReadInteger('IMDBDownloadFilmoAsComposer', 1);
	if my_value=1 then
		cbDlFilmoAsComposer.Checked := True;
		
	cbDlFilmoAsSelf := TCheckBox.Create(f);
	cbDlFilmoAsSelf.Name := 'cbDlFilmoAsSelf';
	cbDlFilmoAsSelf.Parent := f;
	cbDlFilmoAsSelf.SetBounds(10, 190, 370, 20);
	cbDlFilmoAsSelf.Caption := GetLangStr(3891);
	cbDlFilmoAsSelf.Hint := GetLangStr(3892);
	my_value := RegReadInteger('IMDBDownloadFilmoAsSelf', 0);
	if my_value=1 then
		cbDlFilmoAsSelf.Checked := True;
		
	edPicSizeX := TEdit.Create(f);
	edPicSizeX.Name := 'edPicSizeX';
	edPicSizeX.Parent := f;
	edPicSizeX.SetBounds(10, 220, 40, 25);
	edPicSizeX.Text := IntToStr(RegReadInteger('IMDBPicSizeX', defaultPicSizeX));
	
	labPicMulti := TLabel.Create(f);
	labPicMulti.Name := 'labPicMulti';
	labPicMulti.Parent := f;
	labPicMulti.SetBounds(56, 223, 10, 25);
	labPicMulti.Caption := 'X';
	
	edPicSizeY := TEdit.Create(f);
	edPicSizeY.Name := 'edPicSizeY';
	edPicSizeY.Parent := f;
	edPicSizeY.SetBounds(70, 220, 40, 25);
	edPicSizeY.Text := IntToStr(RegReadInteger('IMDBPicSizeY', defaultPicSizeY));
	
	labPicSize := TLabel.Create(f);
	labPicSize.Name := 'labPicSize';
	labPicSize.Parent := f;
	labPicSize.SetBounds(120, 225, 160, 25);
	labPicSize.Caption := GetLangStr(1948);
		
	comboLocalService := TComboBox.Create(f);
	comboLocalService.Name := 'comboLocalService';
	comboLocalService.Parent := f;
	comboLocalService.SetBounds(10, 250, 100, 20);
	comboLocalService.Style := csDropDownList;
	comboLocalService.Items.Add('imdb.com');
	comboLocalService.Items.Add('imdb.de');
	comboLocalService.Items.Add('imdb.es');
	comboLocalService.Items.Add('imdb.fr');
	comboLocalService.Items.Add('imdb.it');
	comboLocalService.Items.Add('imdb.pt');
	comboLocalService.ItemIndex := RegReadInteger('IMDBLocalService', 0);
	
	labLocalService:=TLabel.Create(f);
	labLocalService.Name := 'labLocalService';
	labLocalService.Parent := f;
	labLocalService.SetBounds(120, 255, 160, 20);
	labLocalService.Caption := GetLangStr(1947);

	b := TButton.Create(f);
	b.Name := 'btnOK';
	b.Parent := f;
	//b.SetBounds(180, 400, 80, 25);
	b.SetBounds(trunc(f.width/2)-40, 280, 80, 25);   // 20100923-JeF
	b.Caption := '&OK';
	b.OnClick := @ButtonClick;

	f.ShowModal;
	f.Free;
end;


begin
	monthes[1]:='January';
	monthes[2]:='February';
	monthes[3]:='March';
	monthes[4]:='April';
	monthes[5]:='May';
	monthes[6]:='June';
	monthes[7]:='July';
	monthes[8]:='August';
	monthes[9]:='September';
	monthes[10]:='October';
	monthes[11]:='November';
	monthes[12]:='December';

	prefixes[1]:='The';
	prefixes[2]:='A';
	prefixes[3]:='An';
	prefixes[4]:='La';
	prefixes[5]:='Les';
	prefixes[6]:='Des';
	prefixes[7]:='L''';	
	prefixes[8]:='Un';
	prefixes[9]:='El';
	prefixes[10]:='Il';
	prefixes[11]:='Le';
	prefixes[12]:='Uno';

	jTraceMode := RegReadInteger('IMDBTraceMode', 0);     // 20101001-JeF
	jTraceLevel := RegReadInteger('IMDBTraceLevel', 0);   // 20101001-JeF
	jTraceAsk := RegReadInteger('IMDBTraceAsk', 1);  	  // 20101001-JeF

	IMDBLocalService := RegReadInteger('IMDBLocalService', 0);
	RequestParameters := TStringList.Create();
	RequestParameters.Add('AcceptLanguage=en-US');
	
end.