
const
	script_name='Cinemotions';
	language='French';
	version=21;
	author='JeF';
	MinProgramBuild=6000;
	PCount = 12;
var
	DownloadPersonBiography, DownloadPersonAdditionalInfo: 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: string;
	photo_url: string;
	monthes: array [1..12] of string;
	prefixes: array [1..PCount] of string;
	movie_html: string;
	person_height: integer;
	occurstart,txtpos,allCaps, PhotosURL: string;

	OvrMainDlOptions, DlOriginalName, DlPersonHeight, DlBirthDate, DlBirthPlace, DlDeathDate, DlBiography, DlPersonDesc : boolean;
	DlAwards, DlFilmoAsActor, DlFilmoAsDirector, DlFilmoAsWriter, DlFilmoAsComposer : boolean;
	pictures_exist: Boolean;
	
function GetStr(str: String; s1: String; s2: String): String;
var
  I: Integer;
  J: Integer;
begin
  // Extract from Str the text between s1 and s2 (case insensitive)
	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 AntiLeechGateWay(pagecontent: string): boolean;
var
	msgtxt: string;
begin
	//anti-leech page detect...
	if (pos('deblocage_captcha',pagecontent)>0)
	or (pos('/captcha/',pagecontent)>0) then begin
		msgtxt:='Le site cinemotion bloque le téléchargement en demandant' + #13#10 +
				'de recopier un mot de passe visuel...' + #13#10 +
				'Vous devez y répondre manuellement via votre navigateur internet' + #13#10 +
				'avant de pouvoir de nouveau télécharger depuis Movienizer...';
		showmessage(msgtxt);
		result:=true;
	end;
	result:=false;
end;
	
function GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	url:=format('http://www.cinemotions.com/recherche/%s.html', [PersonName]);
	url:=ReplaceStr(url, ' ', '+', -1);
	result:=url;
end;

//Download search results from URL
function DownloadSearchURL(url: string): integer;
begin
	all:=DownloadURL(url);
	if all='-1' then
		result:=-1
	else
		result:=PersonSearch;
end;

function ClearExtraBlankCharacters(text: string): string;
begin
	// Clear all extra blank charackters
	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 PersonSearch: integer;
var
	url, s, sCaps, s2, s3, s4, small_photo, original_name, description: string;
	i, j, k, l: integer;
begin

	if AntiLeechGateWay(all) then exit;   //anti-leech page detect...  cancel download if anti-leech gateway
	
	s:='';
	persons_count:=0;
	//at least one person
	i:=pos('-nm', all);  
	if (i>0) then begin
		if (i>0) then i:=pos('<b>Résultats de votre recherche</b>', all);  //found a person so extact all results
		j:=pos('Pages de résultats',all);
		if (i>0) and (j>i) then
			s:=copy(all, i, j-i);
	end;
	if (s='') then
	//Person not found
	else begin

		sCaps:=UpperCase(s);
		occurstart:='border="0" width="99%" cellspacing="4" cellpadding="4"';
		i:=pos(occurstart,s);
		while i>0 do begin
			j := MyPos(occurstart, s, i+1);
			if j=0 then
				j := Length(s);
			s2 := copy(s, i, j-i);
			i := Pos('Postes :', s2);
			if i>0 then begin
				person_code := ParseString(s2, 'http://www.cinemotions.com/.*?nm(\d+)', 1);
				if person_code > '' then begin
					persons_count := persons_count + 1;
					small_photo := GetStr(s2, '<img src="', '"');
					original_name := GetStr(s2, '<br>de son vrai nom : <b>', '</b>');
					original_name := DecodeHTML(original_name);
					name := ParseString(s2, 'class="link10"><b>(.*?)</b></a>', 1);
					description := GetStr(s2, '<br>Né', '</TD>');
					if description <> '' then
						description := ClearExtraBlankCharacters(DecodeHTML('Né'+description));
					person_code := ParseString(person_code, '(\d+)', 1);
					SetSearchResult2(name, person_code, '', original_name, description, small_photo);
				end;
			end;
			delete(s, 1, j-1);
			i := Pos(occurstart, s);
		end;
	end;
	
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
var
	s: String;
begin
	s := ParseString(code, '^(\d+)', 1);
	if s='' then
		result:=''
	else
		result:='http://www.cinemotions.com/s-nm'+code;
end;


//returnes images links
procedure GetImages;
var
	s2: string;
	i: integer;
begin
	if photo_url<>'' then begin
		SetImageName('', photo_url, '', 6);
		i:=pos('/h200/',photo_url);
		if (i>0) then begin
			s2:=photo_url;
			delete(s2,i,5);
			SetImageName('', s2, '', 6);
		end;
	end;
	if pictures_exist and (MaxPhotosCount > 0) then
		GetPhotos;
	//GetGoogleImage;
end;

//gets urls of photos
procedure GetPhotos;
var
	url, s, photo_url: string;
	i, j, k: Integer;
	photos_count: Integer;
begin
	all:=GetHTMLPageContentFromRequest(PhotosURL);
	if (all <> '')and(all <> '-1') then
	begin
    i := pos('<img src="http://www.cinemotions.com/data/films/', all);
		photos_count := 0;
		while (i > 0) and (photos_count < MaxPhotosCount) do begin
			j := myPos('"', all, i+12);
      s := copy(all, i, j-i+1); 
      photo_url := GetStr(s, 'src="', '"');
			j := pos('/h80/', photo_url);
			if (j>0) then delete(photo_url,j,4);
			SetImageName('', photo_url, '', 6);
			photos_count := photos_count + 1;
      i := myPos('<img src="http://www.cinemotions.com/data/films/', all, i+10);
		end;
	end;
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);
	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;

// 20121014-JeF
function ConvertMovieCode(id: string): string;
var
  newId: String;
	i: integer;
begin
	newId := id;
  if (pos('-tt',id)>0) then begin
    i := pos('-tt',id);
    newId := copy(id,i+3,length(id)) + '/' + copy(id,1,i-1);
  end
  else if (pos('/',id)>0) then begin
    i := pos('/',id);
    newId := copy(id,i+1,length(id)) + '-tt' + copy(id,1,i-1);
  end;     
	Result := newId;
end;

// 20121014-JeF
function ConvertPersonCode(id: string): string;
var
  newId: String;
	i: integer;
begin
	newId := id;
  if (pos('-nm',id)>0) then begin 
    i := pos('-nm',id);
    newId := copy(id,i+3,length(id)) + '/' + copy(id,1,i-1);    
  end
  else if (pos('/',id)>0) then begin
    i := pos('/',id);
    newId := copy(id,i+1,length(id)) + '-tt' + copy(id,1,i-1);
  end;     
	Result := newId;
end;

function ClearExtraBrackets(name: string): string;
begin
	if length(name)>0 then
		if (name[1]='(') and (name[length(name)]=')') then
			name := copy(name,2,length(name)-2);
	Result := name;
end;

function ExtractDate(s: string): string;
var
	i, j: integer;
	day, month, year: string;
begin
	day:=ParseString(s, 'le (\d+) ', 1);
	if day='' then
		//no date
	else begin
		j:=pos(day, s);
		delete(s, 1, j+length(day));
		month:=ParseString(s, '^(.*?) ', 1);
		if month<>'' then begin
			j:=pos(month, s);
			delete(s, 1, j+length(month));
			year:=ParseString(s, '^(\d+) ', 1);
			if year='' then
				year:=ParseString(s, '^(\d+)$', 1);
		end;
		
		// pb when accents are not present as they should be
		if (pos('evrier',month)>0) then month:='Février';
		if (pos('out',month)>0) then month:='Août';
		if (pos('ecemb',month)>0) then month:='Décembre';
		i:=1;
		while (i<=12) and (monthes[i]<>month) do begin	
			i:=i+1;
		end;
		if (i<=12) and (month<>'') then
			result:=IntToStr(i)+format('/%s/%s', [day, year]);
	end;
	if (day='') or (month='') or (year='') then
		result:=''
end;

function GetInfo(url: string; images_only: boolean): string;
var
	se, s, s2, s3, s4, original_title, character, temp, BioURL, AwardsURL: string;
	i, ie, j, k, ke, m, n: integer;
  biographyExists, addInfosExist: boolean;   //20121014-JeF
begin
	result:='';
	BasicURL:=url;
	person_code:=ParseString(url, 'http://www.cinemotions.com/modules/Artistes/fiche/(.*?).html', 1);
	person_code := ParseString(person_code, '(\d+)', 1);
	if person_code = '' then
		person_code := ParseString(url, 'nm(\d+)', 1);
	person_site_url:=GetPersonSiteURL(person_code);

	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	if AntiLeechGateWay(all) then exit;   //anti-leech page detect...  cancel download if anti-leech gateway
	
	//Galerie
	pictures_exist := (pos('/modules/Artistes/fiche/'+person_code+'/photos.html', all) > 0) or  //20121014-JeF
                    (pos('href="/photos-', all) > 0);
	if (pictures_exist) and (MaxPhotosCount > 0) then begin
		PhotosURL:='http://www.cinemotions.com/photos-s-nm'+person_code;
		AddRequestForHTMLPageContent(PhotosURL, false);
	end;
	
	BiographyExists := (pos('/modules/Artistes/fiche/'+person_code+'/biographie.html', all) > 0) or  //20121014-JeF
                     (pos('href="/biographie-', all) > 0);
  if DlBiography and DownloadPersonBiography and biographyExists then begin
		BioURL := 'http://www.cinemotions.com/biographie-s-nm'+person_code;
		AddRequestForHTMLPageContent(BioURL, false);
	end;
	
  addInfosExist := (pos('/modules/Artistes/fiche/'+person_code+'/nominations.html', all)>0) or  //20121014-JeF
                   (pos('href="/nominations-', all) > 0);
	if DlAwards and DownloadPersonAdditionalInfo and addInfosExist then begin
		AwardsURL:='http://www.cinemotions.com/nominations-s-nm'+person_code;
		AddRequestForHTMLPageContent(AwardsURL, false);
	end;
		
	allCaps:=UpperCase(all);  //the only purpose of this is to get rid of upper/lowercase mismatch of some tags... (using pos/MyPos with caps search strings)
		
	//name   //20100719-JeF
	i:=pos('<TITLE>',allCaps);
	j:=pos('</TITLE>',allCaps);
	person:=copy(all, i, j-i);
	person:=RemoveTags(person);
	person:=DecodeHTML(person);
	person:=ClearExtraSpaces(person);

	//birth_name
	birth_name:='';
	if DlOriginalName then begin
		i:=pos('SON VRAI NOM', allCaps);
		if i>0 then begin
			i:=MyPos('<', allCaps,i);
			j:=MyPos('</FONT>', allCaps,i);
			s:=copy(all,i,j-i);
			s:=DecodeHTML(s);
			s:=RemoveTags(s);
			birth_name:=ClearExtraSpaces(s);
			original_name:=ClearExtraSpaces(s);
		end;
	end;

	//photo
	photo_url:='';
	i := Pos(' src="http://www.cinemotions.com/data/artistes', all);
	if i>0 then begin
		j:=MyPos('</div>',all, i+1);
		s := copy(all, i, j-i);
		photo_url := GetStr(s, ' src="', '"');
		photo_url:=ReplaceStr(photo_url,'h200/','',-1);
	end;
	
	
	//few infos on his/her life (height, date married, date divorced, etc...)
	i:=pos('Quelques infos sur',all);
	if (i>0) then i:=MyPos('</H2>',allCaps,i);
	j:=MyPos('<A NAME=',allCaps,i);
	if (i>0) and (j>i) then begin
		s:=copy(all,i,j-i);
		s:=RemoveTags(s);
		s:=DecodeHTML(s);
		s:=ClearExtraSpaces(s);
		s:=ClearExtraCharacters(s);
		while (copy(s,1,1)<=' ') do delete(s,1,1);
		//height
		s2:='';
		person_height:=0;
		if DlPersonHeight then begin
			i:=pos('Taille',s);
			if (i>0) then begin
				s2:=copy(s,i+7,length(s));
				i:=pos('(',s2);
				if (i>0) then s2:=copy(s2,1,i-1);
				while (copy(s2,1,1)<'0') or (copy(s2,1,1)>'9') do delete(s2,1,1);
				while (copy(s2,length(s2),1)<'0') or (copy(s2,length(s2),1)>'9') do delete(s2,length(s2),1);
				i:=1;
				while (i<length(s2)) do begin
					if (copy(s2,i,1)<'0') or (copy(s2,i,1)>'9') then delete(s2,i,1)
					else i:=i+1;
				end;
				person_height:=StrToInt(s2);
			end;
		end;
		//miscellaneous infos go in description...
		if DlPersonDesc then description:=s+#13#10;
	end;
	
	//birthplace, date of birth, date of death
	i:=pos('>Né', all);
	j:=MyPos('</TABLE>', all, i+1);
	s2:=copy(all,i+3,j-i-3);

	//date of birth
	birth_date:='';
	if DlBirthDate then begin
		i:=pos('<font ', s2);
		if i>0 then begin
			j:=MyPos('</font>', s2, i+1);
			s3:=copy(s2,i,j-i+8);
			s3:=RemoveTags(s3);
			s3:=DecodeHTML(s3);
			birth_date:=ExtractDate(s3);
		end;
	end;

	//birth place
	birthplace:='';
	if DlBirthPlace then begin
		i:=MyPos('<font ', s2, j+1);
		j:=MyPos('</font>', s2, i+1);
		if i>0 then begin
			s3:=copy(s2,i,j-i+8);
			s3:=RemoveTags(s3);
			s3:=DecodeHTML(s3);
			s3:=ClearExtraSpaces(s3);
			s3:=ClearExtraCharacters(s3);
			s3:=ClearExtraSpaces(s3);
			if (pos('Décédé',s3)>0) then s3:='';
			if (copy(s3,1,2)='à ') then delete(s3,1,2);
			birthplace:=ClearExtraSpaces(s3);
		end;
	end;
	
	//date of death
	death_date:='';
	if DlDeathDate then begin
		i:=pos('Décédé', s2);
		if (i>0) then begin
			i:=MyPos('<font ', s2, i);
			j:=MyPos('</font>', s2, i+1);
			s3:=copy(s2,i,j-i+8);
			s3:=RemoveTags(s3);
			s3:=DecodeHTML(s3);
			death_date:=ExtractDate(s3);
		end;
	end;
	
	//filmography if page present
	i:=pos('<table border="0" width="99%">',all);
	if (i>0) then begin
		//20121014-JeF// j := MyPos('<div id=', all, i+1);
    j := MyPos('title="Publicité"', all, i+1);  //20121014-JeF
		temp := copy(all, i, j-i)+'>';
	
		//Filmography formatting is quite 'clean' : each section begins with her proper name : 
		//-> 2 sections for each name : one for cinema, one for TV
		//--> Section Names are : "Realisation", "Interpretation", "Ecriture", "Production", "Montage", "Photographie", "Son" and "Autres-postes"
		year:='';
		occurstart:='<A name=';
		i:=pos(occurstart,temp);
		while (i>0) do begin
			j:=MyPos(occurstart,temp,i+5);
			if (j=0) then j:=Length(temp);
			s:=copy(temp,i,j-i);  //one entire section in s
			se:=GetStr(temp, '<A name="', '"');

			//skip movienizer unsupported sections
			if  (se<>'Realisation')
			and (se<>'Ecriture')
			and (se<>'Interpretation')
			and (se<>'Musique') then se:='';

			if (se='Realisation')    and Not DlFilmoAsDirector
			or (se='Ecriture') 		 and Not DlFilmoAsWriter
			or (se='Interpretation') and Not DlFilmoAsActor
			or (se='Musique') 			 and Not DlFilmoAsComposer
			then se:='';
			
			if (se<>'') then begin
				//first film in section
				j:=pos('<a href=',s);
				while (j>0) do begin
					
					s2 := GetStr(s, '<a href=', '</table>');
					name := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(GetStr(s2, '<b>', '</b>'))));					
					code := GetStr(s2, '"/', '"');
          if (pos('-tt',code) > 0) then code := ConvertMovieCode(code);
					
					year := GetStr(s2, '<br/><font class="smallblue" >', '</font>');
					year := ParseString(year, '(\d+)', 1);
					
					//set filmography according to the section name
					k:=0;
					if (se='Realisation') 	 then k:=1;
					if (se='Ecriture') 	     then k:=2;
					if (se='Interpretation') then k:=3;
					if (se='Musique') 			 then k:=4;

					character := '';
          if (k=3) then begin 
            character := GetStr(s2, '<i>... ', '</i>');
  					character := ClearExtraBrackets(character);
          end;
					code := ParseString(code, '(\d+)', 1);
					if (name>'') and (k>0) then SetMovieName(name, character, k, code, year);
					j := Pos('</table>', s);
					delete(s, 1, j);
					//next movie in the section
					j:=Pos('<a href=',s);
				end;
			end;
			//next section
			delete(temp, 1, i);
			i:=Pos(occurstart,temp);
		end;
	end;
	
	//biography
	biography := '';
	if DlBiography then begin
		if DownloadPersonBiography then begin
			//download biography page
			all:=GetHTMLPageContentFromRequest(BioURL);
			temp := all;
			i:=pos('>Biographie de', temp);
			if i=0 then
				i := Pos(': biographie</A>', temp);
			delete(temp, 1, i);
			i := Pos('<div align="justify">', temp);
			if (i>0) then begin
				i:=MyPos('<',temp,i);
				j:=MyPos('</font></div>', temp, i+1);
				s2:=copy(temp, i, j-i);
				s2:=RemoveTags(s2);
				s2:=DecodeHTML(s2);
				i:=1;
				while(copy(s2,1,1)<=' ') do delete(s2,1,1);
				i:=pos('<br />',s2);
				while(i>0) do begin
					delete(s2,i,6);
					i:=pos('<br />',s2);
				end;
				biography:=ClearExtraBlankCharacters(s2);
			end;
		end;
	end;
	
	//Awards infos go in description (no dedicated Awards field for a person)
	if DlAwards then begin
		if DownloadPersonAdditionalInfo then begin
			//download awards page
			all:=GetHTMLPageContentFromRequest(AwardsURL);
			ie:=pos('<A HREF="/modules/Awards/Award/',all);
			while (ie>0) do begin
				k:=MyPos('</table>',all,ie);
				se:=copy(all,ie,k-ie);
				se:=RemoveTags(se);
				se:=ClearExtraSpaces(se);
				ie:=k;
				while (copy(se,1,1)<=' ') and (length(se)>0) do delete(se,1,1);
				while (copy(se,length(se),1)<=' ') and (length(se)>0) do delete(se,length(se),1);
				if description <> '' then
					description := description + #13#10;
				description:=description+se+#13#10;
				occurstart:=('/Awards/Award_detail/');
				i:=pos(occurstart,all);
				if (i>0) then begin
					j:=MyPos('<A HREF="/modules/Awards/Award/',all,ie+10);
					if (j=0) then j:=length(all);
					s:=copy(all,ie,j-ie+1);
				end;
				i:=pos(occurstart,s);
				while (i>0) do begin
					i:=MyPos('>',s,i)+1;
					j:=MyPos(occurstart,s,i);
					if (j=0) then j:=MyPos('</TABLE>',UpperCase(s),i);
					j:=MyPos('>',s,j);
					s2:=copy(s,i,j-i+1);
					s2:=RemoveTags(s2);
					s2:=DecodeHTML(s2);
					s2:=ClearExtraSpaces(s2);
					s2:=ClearExtraCharacters(s2);
					j:=1;
					while (j<length(s2)) do begin
						if (copy(s2,j,1)<' ') then s2:=ReplaceStr(s2,copy(s2,j,1),' ',-1)
						else if (copy(s2,j,2)='  ') then delete(s2,j,1)
						else j:=j+1;
					end;
					s2:='   - '+ClearExtraBlankCharacters(s2);
					if (s2<>'') then description:=description+s2+#13#10;
					//next nomination
					i:=MyPos(occurstart,s,i+5);
				end;
				//next award
				ie:=MyPos('<A HREF="/modules/Awards/Award/',all,ie+10);
			end;
		end;
	end;
	
end;

//get movie site url
function GetPersonSiteURL(code: string): string;
var
	s: String;
begin
	s := ParseString(code, '^(\d+)', 1);
	if s='' then
		result:=''
	else
		result:='http://www.cinemotions.com/s-nm'+code;
end;

//get movie site url
function GetMovieSiteURL(code: string): string;
var
	s: String;
begin
	s := ParseString(code, '^(\d+)', 1);
	if s='' then
		result:=''
	else
		result:='http://www.cinemotions.com/s-tt'+s;
end;

function BoolToInt(b: boolean): integer;
begin
  if b then result:=1
  else result:=0;
end;

function IntToBool(intval: integer): boolean;
begin
  if intval < 1 then result := false
  else result := true;
end;

//show available settings for the script
function ShowScriptOptions: integer;
var
	s: string;
	f: TForm;
	b: TButton;
	cbOvrMainDlOptions, cbDlOriginalName, cbDlBirthDate, cbDlBiography, cbDlBirthPlace, cbDlDeathDate: TCheckBox;
	cbDlPersonDesc, cbDlPersonHeight, cbDlFilmoAsDirector, cbDlFilmoAsActor, cbDlFilmoAsWriter, cbDlFilmoAsComposer: TCheckBox;
	cbDlAwards, cbAdvSearch: TCheckBox;
	labTitleLang, labMPAADefault, labMPAAReplace, labLocalService, labPicSize, labPicMulti: TLabel;
	edTitleLang, edMPAADefault, edMPAAReplace, edPicSizeX, edPicSizeY: TEdit;
	gbDlOptions: TGroupBox;
	comboLocalService: TComboBox;
	posx, posy, my_value: integer;
	cbTMDBPosters, cbTMDBImages: TCheckBox;

procedure ButtonClick(Sender: TButton);
var
	zn: integer;
begin
	try
		RegWriteInteger('CINEMOTIONS_Per_OvrMainDlOptions'  , BoolToInt(cbOvrMainDlOptions.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlOriginalName'    , BoolToInt(cbDlOriginalName.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlPersonHeight'    , BoolToInt(cbDlPersonHeight.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlBirthDate'       , BoolToInt(cbDlBirthDate.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlBiography'       , BoolToInt(cbDlBiography.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlBirthPlace'      , BoolToInt(cbDlBirthPlace.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlDeathDate'       , BoolToInt(cbDlDeathDate.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlPersonDesc'      , BoolToInt(cbDlPersonDesc.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlAwards'          , BoolToInt(cbDlAwards.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlFilmoAsDirector' , BoolToInt(cbDlFilmoAsDirector.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlFilmoAsActor'    , BoolToInt(cbDlFilmoAsActor.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlFilmoAsWriter'   , BoolToInt(cbDlFilmoAsWriter.Checked));
		RegWriteInteger('CINEMOTIONS_Per_DlFilmoAsComposer' , BoolToInt(cbDlFilmoAsComposer.Checked));
	except
	end;

	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.Height:=300;
	f.AutoSize:=false;
	
	posx :=  10;
	posy := -10;
	
	posy := posy + 20;
	cbOvrMainDlOptions := TCheckBox.Create(f);
	cbOvrMainDlOptions.Name := 'cbOvrMainDlOptions';
	cbOvrMainDlOptions.Parent := f;
	cbOvrMainDlOptions.SetBounds(posx, posy, 350, 20);
	if GetLangStr(2744) = '' 
		then cbOvrMainDlOptions.Caption := 'Override Global Download Options (F10)'
	else cbOvrMainDlOptions.Caption := GetLangStr(2744);
	if GetLangStr(2745) = '' 
		then cbOvrMainDlOptions.Hint := 'Override values/flags set in main download options window (F10)'
	else cbOvrMainDlOptions.Hint := GetLangStr(2745);
	cbOvrMainDlOptions.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_OvrMainDlOptions', 0));

	posy := posy + 10;
	gbDlOptions:=TGroupBox.Create(f);
	gbDlOptions.Name:='gbDlOptions';
	gbDlOptions.Parent:=f;
	gbDlOptions.SetBounds(posx, posy + 20, 425, 30);
	gbDlOptions.Caption:=' ' + GetLangStr(2734) + ' ';
	gbDlOptions.Hint:=GetLangStr(2734);
	posy := 0;
	
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlOriginalName := TCheckBox.Create(f);
	cbDlOriginalName.Name := 'cbDlOriginalName';
	cbDlOriginalName.Parent := gbDlOptions;
	cbDlOriginalName.SetBounds(posx, posy, 200, 20);
	cbDlOriginalName.Caption := GetLangStr(284);
	cbDlOriginalName.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlOriginalName', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlPersonHeight := TCheckBox.Create(f);
	cbDlPersonHeight.Name := 'cbDlPersonHeight';
	cbDlPersonHeight.Parent := gbDlOptions;
	cbDlPersonHeight.SetBounds(posx, posy, 200, 20);
	cbDlPersonHeight.Caption := GetLangStr(709);
	cbDlPersonHeight.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlPersonHeight', 1));
	//Not available from this site
	//cbDlPersonHeight.Checked := False;
	//cbDlPersonHeight.Enabled := False;

	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlBirthDate := TCheckBox.Create(f);
	cbDlBirthDate.Name := 'cbDlBirthDate';
	cbDlBirthDate.Parent := gbDlOptions;
	cbDlBirthDate.SetBounds(posx, posy, 200, 20);
	cbDlBirthDate.Caption := GetLangStr(285);
	cbDlBirthDate.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlBirthDate', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlBirthPlace := TCheckBox.Create(f);
	cbDlBirthPlace.Name := 'cbDlBirthPlace';
	cbDlBirthPlace.Parent := gbDlOptions;
	cbDlBirthPlace.SetBounds(posx, posy, 200, 20);
	cbDlBirthPlace.Caption := GetLangStr(287);
	cbDlBirthPlace.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlBirthPlace', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlDeathDate := TCheckBox.Create(f);
	cbDlDeathDate.Name := 'cbDlDeathDate';
	cbDlDeathDate.Parent := gbDlOptions;
	cbDlDeathDate.SetBounds(posx, posy, 200, 20);
	cbDlDeathDate.Caption := GetLangStr(286);
	cbDlDeathDate.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlDeathDate', 1));

	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlAwards := TCheckBox.Create(f);
	cbDlAwards.Name := 'cbDlAwards';
	cbDlAwards.Parent := gbDlOptions;
	cbDlAwards.SetBounds(posx, posy, 200, 20);
	cbDlAwards.Caption := GetLangStr(1831) + ' (-> ' + GetLangStr(162) + ')';
	cbDlAwards.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlAwards', 1));
		
	posx := 220;
	posy := 0;
	
	posy := posy + 20;
	cbDlBiography := TCheckBox.Create(f);
	cbDlBiography.Name := 'cbDlBiography';
	cbDlBiography.Parent := gbDlOptions;
	cbDlBiography.SetBounds(posx, posy, 200, 20);
	cbDlBiography.Caption := GetLangStr(282);
	cbDlBiography.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlBiography', 1));

	posy := posy + 20;
	cbDlPersonDesc := TCheckBox.Create(f);
	cbDlPersonDesc.Name := 'cbDlPersonDesc';
	cbDlPersonDesc.Parent := gbDlOptions;
	cbDlPersonDesc.SetBounds(posx, posy, 200, 20);
	cbDlPersonDesc.Caption := GetLangStr(162);
	cbDlPersonDesc.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlPersonDesc', 1));
		
	posy := posy + 20;
	cbDlFilmoAsActor := TCheckBox.Create(f);
	cbDlFilmoAsActor.Name := 'cbDlFilmoAsActor';
	cbDlFilmoAsActor.Parent := gbDlOptions;
	cbDlFilmoAsActor.SetBounds(posx, posy, 200, 20);
	cbDlFilmoAsActor.Caption := GetLangStr(289);
	cbDlFilmoAsActor.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlFilmoAsActor', 1));
		
	posy := posy + 20;
	cbDlFilmoAsDirector := TCheckBox.Create(f);
	cbDlFilmoAsDirector.Name := 'cbDlFilmoAsDirector';
	cbDlFilmoAsDirector.Parent := gbDlOptions;
	cbDlFilmoAsDirector.SetBounds(posx, posy, 200, 20);
	cbDlFilmoAsDirector.Caption := GetLangStr(290);
	cbDlFilmoAsDirector.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlFilmoAsDirector', 1));
		
	posy := posy + 20;
	cbDlFilmoAsWriter := TCheckBox.Create(f);
	cbDlFilmoAsWriter.Name := 'cbDlFilmoAsWriter';
	cbDlFilmoAsWriter.Parent := gbDlOptions;
	cbDlFilmoAsWriter.SetBounds(posx, posy, 200, 20);
	cbDlFilmoAsWriter.Caption := GetLangStr(291);
	cbDlFilmoAsWriter.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlFilmoAsWriter', 1));
		
	posy := posy + 20;
	cbDlFilmoAsComposer := TCheckBox.Create(f);
	cbDlFilmoAsComposer.Name := 'cbDlFilmoAsComposer';
	cbDlFilmoAsComposer.Parent := gbDlOptions;
	cbDlFilmoAsComposer.SetBounds(posx, posy, 200, 20);
	cbDlFilmoAsComposer.Caption := GetLangStr(492);
	cbDlFilmoAsComposer.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlFilmoAsComposer', 1));
		
	posx :=  10;
	posy := gbDlOptions.Top + gbDlOptions.Height - 5;

	f.Height := posy + 110;
	
	b := TButton.Create(f);
	b.Name := 'btnOK';
	b.Parent := f;
	b.SetBounds(trunc(f.width/2)-40, f.Height-70, 80, 25);
	b.Caption := '&OK';
	b.OnClick := @ButtonClick;

	f.ShowModal;
	f.Free;
end;


begin

	monthes[1]:='Janvier';
	monthes[2]:='Février';
	monthes[3]:='Mars';
	monthes[4]:='Avril';
	monthes[5]:='Mai';
	monthes[6]:='Juin';
	monthes[7]:='Juillet';
	monthes[8]:='Août';
	monthes[9]:='Septembre';
	monthes[10]:='Octobre';
	monthes[11]:='Novembre';
	monthes[12]:='Décembre';

	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';

	OvrMainDlOptions  := IntToBool(RegReadInteger('CINEMOTIONS_Per_OvrMainDlOptions', 0));
	DlOriginalName    := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlOriginalName', 1));
	DlPersonHeight    := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlPersonHeight', 1));
	DlBirthDate       := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlBirthDate', 1));
	DlBirthPlace      := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlBirthPlace', 1));
	DlDeathDate       := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlDeathDate', 1));
	DlBiography       := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlBiography', 1));
	DlPersonDesc      := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlPersonDesc', 1));
	DlAwards          := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlAwards', 1));
	DlFilmoAsDirector := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlFilmoAsDirector', 1));
	DlFilmoAsActor    := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlFilmoAsActor', 1));
	DlFilmoAsWriter   := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlFilmoAsWriter', 1));
	DlFilmoAsComposer := IntToBool(RegReadInteger('CINEMOTIONS_Per_DlFilmoAsComposer', 1));

end.
