
const
	script_name='Cinemotions';
	language='French';
	version=34;
	author='JeF/Vlad';
	MinProgramBuild=6000;
	PCount = 12;
	//MaxPicturesCount = 3;
	
var
	DownloadMovieBigCover, DownloadMovieShortCast, DownloadMovieFullCast, 
	DownloadMovieBigDescription, DownloadMovieComments, 
	DownloadMovieStudios,	DownloadMovieAwards, DownloadMovieEarnings, 
	DownloadMovieAdditionalInfo, DownloadMovieSeasonsList: boolean;
	MaxShotsCount, MaxPostersCount, MaxTrailersCount: integer;

	title, MovieName, s, s2, s3, s4, year, director, name, character, BasicURL, country, description, salary: string;
	movie_code, movie_site_url, title_code, small_image, imdb_code: string;
	titles: array of string;
	titles_count, image_mode: integer;
	i, j: integer;
	director_code, code: string;
	directors, writers, actors, characters, genres, imdb_codes: array of string;
	duration: integer;
	imdb_rating: double;
	front_cover_url: string;
	posters_exists, ask_amazon, pictures_exist, posters_exist: boolean;
	original_title, mpaa, mpaa_rating: string;
	prefixes: array [1..PCount] of string;
	occurstart,txtpos: string;
	resolution, barcode, aspect_ratio, features, media_format, video_codec: string;

	OvrMainDlOptions, DlOriginalTitle, DlYear, DlGenres, DlCountries, DlDuration, DlStudios, DlEpisodes : boolean;
	DlMovieDesc, DlDirectors, DlActors, DlWriters, DlComposers : boolean;
	DlIMDBInfos : boolean;

function GetStr(str: String; s1: String; s2: String): String;
var
  I: Integer;
  J: Integer;
begin
	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;	

//Get IMDB Infos (sets imdb_code, imdb_rating, mpaa, mpaa_rating)  // 20090701-JeF
procedure GetIMDBInfos(MovieTitle, Year: string);
var
	url,all,title,NakedMovieTitle: string;
	matches_count, i, j, k: integer;
	sl: TStringList;
begin
	NakedMovieTitle:=ReplaceStr(MovieTitle,'?','',-1);
	NakedMovieTitle:=ReplaceStr(NakedMovieTitle,'!','',-1);
	NakedMovieTitle:=ReplaceStr(NakedMovieTitle,'.','',-1);
	NakedMovieTitle:=ReplaceStr(NakedMovieTitle,',','',-1);
	NakedMovieTitle:=ReplaceStr(NakedMovieTitle,'  ',' ',-1);
	// search for imdb_code
	if (imdb_code='') and (year>'') then begin
		// launch imdb movie search
		url:='http://akas.imdb.com/'
			+format('find?q=%%5E%s%%24&type=regexp&tv=both&from_year=1890&to_year=2009&sort=smart;tt=1', [MovieTitle]);
		url:=ReplaceStr(url, ' ', '+', -1);
		all:=DownloadURL(url);
		if (all<>'-1') then begin
			// imdb movie search result page analysis
			s:='';
			matches_count:=0;
			i:=pos('<p><b>Titles (Approx Matches)</b> (Displaying', all);
			if (i>0) then i:=MyPos('<table>', all, i+1);
			if (i>0) then begin
				j:=MyPos('</table>', all, i+1);
				if j>0 then
					s:=copy(all, i, j-i);
			end;
			if (s='') or (pos('<b>No Matches.</b>', all)>0) then
				//Movie not found
			else begin
				i:=pos('</tr>', s);
				while i>0 do begin
					s2:=copy(s, 1, i-1);
					j:=Pos('</td>', s2);
					j:=MyPos('</td>', s2, j+1);
					s2:=copy(s, j+5, length(s2)-j-4);
					j:=Pos('<a href="/title/tt', s2);
					k:=MyPos('"', s2, j+15);   // 20091126-JeF
					s3:=copy(s2, j, k-j+1);   // 20091126-JeF
					title:=RemoveTags(s2);
					title:=DecodeHTML(title);
					title:=ClearExtraSpaces(title);
					title:=ReplaceStr(title,'?','',-1);
					title:=ReplaceStr(title,'!','',-1);
					title:=ReplaceStr(title,'.','',-1);
					title:=ReplaceStr(title,',','',-1);
					title:=ReplaceStr(title,'  ',' ',-1);
					if (pos('AKA "'+UpperCase(NakedMovieTitle)+'"',UpperCase(title))>0) 
					or (pos(' AKA ',UpperCase(title))=0) and (pos(UpperCase(NakedMovieTitle),UpperCase(title))=1)
					or (pos(UpperCase(NakedMovieTitle),UpperCase(title))=1)
					or (pos('"'+UpperCase(NakedMovieTitle)+'"',UpperCase(title))=1) then begin
						if (pos(Year,title)>0)
						or (pos(IntToStr(StrToInt(Year)+1),title)>0)
						or (pos(IntToStr(StrToInt(Year)-1),title)>0) then begin
							matches_count:=matches_count+1;
							if (imdb_code='') then imdb_code:=ParseString(s3, '<a href="/title/tt(\d+)/"', 1);
						end;
					end;
					delete(s, 1, i+4);
					i:=pos('</tr>', s);
				end;
			end;
			if (matches_count>1) then imdb_code:='';
		end;
	end;
	// imdb_code already known or found by imdb search : retrieve IMDB infos
	if (imdb_code>'') then begin
		imdb_rating:=0;
		mpaa:='';
		mpaa_rating:='';
		url:=format('http://www.imdb.com/title/tt%s', [imdb_code]);
		all:=DownloadURL(url);
		if (all<>'-1') then begin
			//imdb rating
			imdb_rating:=0;
			i:=pos('<h5>User Rating:</h5>', all);
			if i>0 then begin
				i:=MyPos('<b>', all, i+1);
				if i>0 then begin
					j:=MyPos('</b>', all, i+1);
					if j>0 then begin	
						s2:=copy(all, i, j-i);
						if length(s2)>0 then begin
							delete(s2, 1, 3);
							i:=pos('/', s2);
							if i>0 then begin
								s2:=copy(s2, 1, i-1);
								i:=pos('.', s2);
								if i>0 then
									s2[i]:=DecimalSeparator;
								try
									imdb_rating:=StrToFloat(s2);
								except
								end;
							end;
						end;
					end;
				end;
			end;
			//mpaa
			mpaa:='';
			mpaa_rating:='';
			i:=pos('<h5><a href="/mpaa">MPAA</a>:</h5>', all);
			if i>0 then begin
				j:=MyPos('</div>', all, i+1);
				if j>0 then begin
					s2:=copy(all, i, j-i);
					i:=pos('Rated ', s2);
					if i>0 then begin
						delete(s2, 1, i+5);
						i:=pos(' ', s2);
						if i>0 then begin
							mpaa:=copy(s2, 1, i-1);
							delete(s2, 1, i);
							i:=pos('.', s2);
							if i>0 then begin
								mpaa_rating:=copy(s2, 1, i-1);
							end;
						end;
					end;
				end;
			end;
		end;  // all <> -1
	end;  // imdb_code > ''
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;

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

// 20100719-JeF
function ClearNPCharacters(text: string): string;
var
	i: integer;
begin
	// Clear Non Printable Characters
	i:=1;
	while (i<=length(text)) do begin
		if (ord(text[i]) in [1..20]) 
			then delete(text, i, 1)
		else i:=i+1;
	end;
	Result := text;
end;

// 20091126-JeF
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 MovieSearch: integer;
var
	i, j, k, l: integer;
	title, wyear, original_title, description, small_cover: string;
begin
	
	if AntiLeechGateWay(all) then exit;   //anti-leech page detect...  cancel download if anti-leech gateway
	
	s:='';
	titles_count:=0;
	i:=pos('avec', all);  //at least one title
	if (i=0) then i:=pos('/Films/fiche/',all);
	if (i>0) then i:=pos('<table  width="100%"  border="0"', all);  //found a title so extact all results
	j:=pos('Pages de résultats',all);
	if (i>0) and (j>i) then
		s:=copy(all, i, j-i);
	if (s='') then
	//movie not found
	else begin

		i:=Pos('résultats</b></font></td>', s);
		if i>0 then begin
			s := copy(s, i, length(s)-i+1);
			i := Pos('border="0" width="99%" cellspacing="4" cellpadding="4"', s);
			while i>0 do begin
				j := MyPos('border="0" width="99%" cellspacing="4" cellpadding="4"', s, i+1);
				if j<=i then
					j := length(s);
				s2 := copy(s, i, j-i);
				// 20100719-JeF //k := Pos('avec </font>', s2);
				k := Pos('avec', s2);   // 20100719-JeF
				if k>0 then begin
					title_code := GetStr(s2, '<a href="/', '"');
					if title_code > '' then begin
						titles_count := titles_count + 1;
						title := ParseString(s2, 'class="link10"><b>(.*?)</b></a>', 1);
						original_title := GetStr(s2, '<br>', '<br>');
						original_title := ClearExtraBlankCharacters(original_title);
						original_title := DecodeHTML(original_title);
						// 20100719-JeF //s3 := ParseString(s2, '<br><font class=noir>(.*?)</td>', 1);
						// 20100719-JeF //s3 := ReplaceStr(s3, '<br><font class=noir>', ' ', -1);
						k := Pos('<br>', s2); 					 // 20100719-JeF
						if (MyPos('<br>',s2,k+4)>0) then k:=MyPos('<br>',s2,k+4);
						l := MyPos('Correspondance', s2, k+1);   // 20100719-JeF
						s3:=copy(s2,k,l-k-1); 				     // 20100719-JeF
						s3 := RemoveTags(s3);
						s3 := ClearNPCharacters(s3);	   // 20100719-JeF
						s3 := ClearExtraBlankCharacters(s3);
						s3 := ReplaceStr(s3, '  ', ' ', -1);
						s3 := ReplaceStr(s3, ' R&eacute;alisation', ' - R&eacute;alisation', -1);
						description := ReplaceStr(s3, ' avec', ' - avec', -1);
						title := DecodeHTML(title);
						small_cover := GetStr(s2, '<IMG SRC="', '"');
						k := Pos('<br>', s2);
						delete(s2, 1, k);
						k := Pos('<br>', s2);
						delete(s2, 1, k-1);
						wyear := ParseString(s2, '<br>(\d+) ', 1);
						k := Pos(' - ', description);						
						if k>0 then
							delete(description, 1, k+2);
						title_code := ParseString(title_code, 'tt(\d+)', 1);
						description := DecodeHTML(description);
						SetSearchResult2(title, title_code, wyear, original_title, description, small_cover);
					end;
				end;
				delete(s, 1, j-1);
				i := Pos('border="0" width="99%" cellspacing="4" cellpadding="4"', s);
			end;
		end;
	end;
	result:=titles_count;

end;

//get  movie name by barcode(UPC-12)
function GetMovieNameByUPC(barcode: string): string;
var
	url, all, s2, s3, res: string;
	i, j: integer;
begin
	res:='';
	url:=format('http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=0J0N83PT9M72BYH98YR2&Operation=ItemSearch&Keywords=%s&SearchIndex=Video&ResponseGroup=Request,Images,Tracks,ItemAttributes', [barcode]);	
	url:=ReplaceStr(url, ' ', '+', -1);
	all:=DownloadURL(url);
	//search image by title
	i:=pos('<Item>', all);
	while (i>0) and (res='') do begin
		j:=MyPos('</Item>', all, i+1);
		if j>0 then begin
			s2:=copy(all, i, j-i);
			s3:=ParseString(s2, '<Title>(.*?) \(.*\)</Title>', 1);
			if s3='' then
				s3:=ParseString(s2, '<Title>(.*?)</Title>', 1);
			if s3<>'' then
				res:=s3;
		end;
		i:=MyPos('<Item>', all, i+1);
	end;
	i:=pos('[', res);
	if i>0 then begin
		j:=MyPos(']', res, i+1);
		if j>0 then
			delete(res, i, j-i+1);
	end;
	result:=res;
end;

//Get search URL by movie name
function GetSearchURLbyName(MovieName, AYear, ABarcode: string): string;
var
	url: string;
begin
	MovieName := EncodeURL(MovieName, 'ISO-8859-1');
	url:=format('http://www.cinemotions.com/recherche/%s.html', [MovieName]);
	//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:=MovieSearch;
end;

function GetMovieURL(code: string; images_only: boolean): string;
var
	s: String;
begin	
	s := ParseString(code, '^(\d+)', 1);
	if (pos('modules/',code)>0) then
		result:=format('http://www.cinemotions.com%s.html', [code])
	else begin
		if s <> '' then
			result:=format('http://www.cinemotions.com/s-tt%s', [s]);
	end;
end;

//returned link to front cover
function GetAmazonImages: string;
var
	res, all, url, s, s2, s3, amazon_url: string;
	i, j, frames_count, k: integer;
	item_url: string;
	image_data, image_height, image_width, image_url: string;
	frame_urls: array[1..MaxPicturesCount] of string;
	int_year: Word;
	year_prew, year_next: string;
	valid_year: boolean;
begin
	res:=front_cover_url;
	if DownloadMovieBigCover then begin
		s:=title;
		url:=format('http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=0J0N83PT9M72BYH98YR2&Operation=ItemSearch&Keywords=%s&SearchIndex=Video&ResponseGroup=Request,Images,Tracks,ItemAttributes', [title]);
		url:=ReplaceStr(url, ' ', '+', -1);
		all:=DownloadURL(url);
		//search image by title
		amazon_url:='';
		frames_count := 0;
		i:=pos('<Item>', all);
		while (i>0) and (amazon_url='') do 
		begin
			j := MyPos('</Item>', all, i+1);
			if j>0 then 
			begin
				s2 := copy(all, i, j-i);
				s3 := GetTextBetween(s2, '<Title>', '</Title>');
				s3 := ClearBrackets(s3);
				s3 := DecodeHTML(s3);
				if s3<>'' then 
				begin
					if (UpperCase(title) = UpperCase(s3)) then
					begin
						valid_year := true;
						if pos('<TheatricalReleaseDate>', s2) > 0 then
						begin
							int_year := StrToInt(year);
							year_prew := IntToStr(int_year - 1);
							year_next := IntToStr(int_year + 1);
							valid_year := false;
							valid_year := pos('<TheatricalReleaseDate>'+year, s2) > 0;
							valid_year := valid_year or (pos('<TheatricalReleaseDate>'+year_prew, s2) > 0);
							valid_year := valid_year or (pos('<TheatricalReleaseDate>'+year_next, s2) > 0);						
						end;
						if valid_year then
						begin
							s := '';
							frames_count := 0;
							repeat
								image_data := GetTextBetween(s2, '<LargeImage>', '</LargeImage>');
								image_url := GetTextBetween(image_data, '<URL>', '</URL>');
								image_height := GetTextBetween(image_data, '<Height','</Height>');
								image_height := GetTextBetween(image_height, '>','');
								image_width := GetTextBetween(image_data, '<Width','</Width>');
								image_width := GetTextBetween(image_width, '>','');
								try
									if (image_height <> '0') and ((StrToInt(image_width) / StrToInt(image_height)) > 1.2) then 
									begin
										for k := 1 to frames_count do
											if frame_urls[k] = image_url then break;
										if (frames_count = 0) or (frame_urls[k] <> image_url) then begin
											frames_count := frames_count + 1;
											frame_urls[frames_count] := image_url;
										end;
									end
									else if amazon_url = '' then
										amazon_url := image_url;
								except
									//Wrong XML format!
								end;
								delete(s2, 1, 25 + length(image_data));
							until (image_data = '') or (frames_count = MaxPicturesCount);
						end;
					end; //if
				end; //if
			end; //if
			i := MyPos('<Item>', all, i+1);
		end; //while

		for i:=1 to frames_count do  // frames from amazon
			SetImageName('', frame_urls[i], '', 3);
		if amazon_url<>'' then   // front-cover from amazon
			SetImageName('', amazon_url, '', 1);
	end;
	if front_cover_url<>'' then // front-cover from movie site
		SetImageName('', front_cover_url, '', 1);
	Result := amazon_url;
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);
	i := pos(' [', name);
	if i>0 then
		name := copy(name, 1, i-1);
	Result := name;
end;
	
function GetTextBetween(fromStr, str1, str2: string): string;
var
	i, j: integer;
begin
	Result := '';
	if str1 = '' then
		i := 1
	else
		i := pos(str1, fromStr);
	if str2 = '' then
		j := Length(fromStr) + 1
	else
		j := MyPos(str2, fromStr, i+length(str1));
	if (i>0)and(j>0) then begin
		i := i + length(str1);
		Result := copy(fromStr, i, j-i);
	end;
end;

//save front cover to file
function SaveFrontCover(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 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;

function ExtractDate(s: string): string;
var
	i, j: integer;
	s2, day, month, year: string;
begin
	i:=pos('/',s);
	if (i>2) then begin
		s2:=copy(s,i-2,10);
		while (s2>'') and ((s2[1]<'0') or (s2[1]>'9')) do delete(s2,1,1);
		while (s2>'') and ((s2[length(s2)]<'0') or (s2[length(s2)]>'9')) do s2:=copy(s2,1,length(s2)-1);
		i:=pos('/',s2);
		day:=copy(s2,1,i-1);
		delete(s2,1,i);
		i:=pos('/',s2);
		month:=copy(s2,1,i-1);
		delete(s2,1,i);
		year:=s2;
{	end
	else if (pos(' le ',s)>0) then 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;
}	end;
	result:=month+'/'+day+'/'+year;
	if (day='') or (month='') or (year='') then
		result:=''
end;

procedure ExtractPeople(text: string; mode: integer);
var
	i, j : Integer;
	s, s2, name, code, character: string;
begin
	i := Pos('<A HREF=', text);
	while i>0 do begin
		character := '';
		j := MyPos(', <A', text, i+1);
		if j=0 then
			j := MyPos(' et ', text, i+1);
		if j=0 then
			j := Length(text)+1;
		s := copy(text, i, j-i);
		s := ClearExtraBlankCharacters(s);
		code := ParseString(s, '<A HREF="http://www.cinemotions.com/modules/Artistes/fiche/(.*?).html"', 1);
		code := ParseString(code, '(\d+)', 1);
		name := ParseString(s, 'class="link10">(.*?)</A>', 1);
		character := ParseString(s, 'nbsp;\((.*?)\)$', 1);
		i := Pos('(', character);
		if i>0 then
			delete(character, i, length(character) - i + 1);
		if name > '' then
			SetPersonName(name, character, mode, code);
		delete(text, 1, j);
		i := Pos('<A HREF=', text);
	end;
end;

function GetInfo(url: string; images_only: boolean): string;
var 
	keyword, s, s2, s3, plot_outline: string;
	season, episode, genres, countries, longduration: string;
	i, j, k, l: integer;
	serie: boolean;
	season_nbr, episode_nbr: integer;
	AirDate, EpisodeTitle, EpisodeIMDB, EpisodeDescription, day, month, wyear: string;
	small_photo, actors_url, techs_url: String;

begin
	result:='';
	BasicURL:=url;
	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
	
	//i:=pos('>Accueil<',all);
	//j:=pos('mise à jour',all);
	//all:=copy(all, i, j-i+1);
	
	movie_code:=ParseString(url, 'http://www.cinemotions.com/modules/Films/fiche/(.*?).html', 1);
	movie_code := ParseString(movie_code, '(\d+)', 1);
	if movie_code = '' then
		movie_code:=ParseString(url, 'tt(\d+)', 1);
	movie_site_url:=GetMovieSiteURL(movie_code);
	
	actors_url := '';
	techs_url := '';
	
	if DownloadMovieFullCast or OvrMainDlOptions then begin
		actors_url:='http://www.cinemotions.com/acteurs-s-tt'+movie_code;
		AddRequestForHTMLPageContent(actors_url, False);
		if DownloadMovieFullCast or OvrMainDlOptions then begin
			techs_url:='http://www.cinemotions.com/equipe-s-tt'+movie_code;
			AddRequestForHTMLPageContent(techs_url, False);
		end;
	end;
	
	//Main infos  //20100719-JeF
	i:=pos('class="home_nom_fiche"',all);
	j:=MyPos('</table>',all,i);
	s2:=copy(all, i+1, j-i-1);

	//Movie title  //20100719-JeF
	i:=pos('>',s2);
	j:=MyPos('</a>',s2,i);
	title:=copy(s2, i+1, j-i-1);
	title:=RemoveTags(title);
	title:=DecodeHTML(title);
	title:=ClearExtraSpaces(title);
	delete(s2, 1, j-1);
	
	//Original title
	original_title := '';
	if DlOriginalTitle then begin
		i:=Pos('<font class="home_nom_fiche"', all);
		if i>0 then begin
			i:=MyPos('>',all,i)+1;
			j:=MyPos('</font>', all, i+1);
			s3 := copy(all, i, j-i);
			s3 := RemoveTags(s3);
			s3 := DecodeHTML(s3);
			s3 := ClearNPCharacters(s3);
			original_title := ClearExtraSpaces(s3);
		end;
	end;

	//extract cumulative desc: year - country - genre - duration
	s2:=GetStr(all, '<font face="arial" size="2">', '</font>');
	serie:=(pos(' x ',s2)<>0);
	if NOT(serie) then serie:=(pos(' saison',s2)>0);
	j:=pos(' - ',s2);
	wyear:=copy(s2,1,j);
	delete(s2,1,j+2);
	j:=pos(' - ',s2);
	countries:=copy(s2,1,j);
	countries:=ClearExtraSpaces(countries);
	delete(s2,1,j+2);
	j:=pos(' - ',s2);
  if (j=0) then j := length(s2);  //20121014-JeF
	genres:=copy(s2,1,j);
	genres:=ClearExtraSpaces(genres);
	delete(s2,1,j+2);
	j:=pos(' - ',s2);
	k:=pos('aison',s2);
	if (j>0) and (k>0) and (k<j) then delete(s2,1,j+2);
	longduration:=ClearExtraSpaces(s2);
	j:=pos('x',longduration);
	if (j>0) then longduration:=copy(longduration,j+1,length(longduration));
	j:=pos('m',longduration);
	if (j>0) then longduration:=copy(longduration,1,j-1);
	
	//Year
	year := '';
	if DlYear then begin
		wyear:=DecodeHTML(wyear);
		year:=ClearExtraBlankCharacters(wyear);   // 20091126-JeF
		j:=pos('&',year);
		if (j=0) then j:=pos('à', year);
		if (j>0) then delete(year,j,length(year));
		year:=ClearExtraBlankCharacters(year);   // 20091126-JeF
	end;

	//Genre
	if DlGenres then begin
		if (serie) then
			setGenre('Série');
		while (length(genres)>0) do begin
			j:=pos('/', genres);
			if (j=0) then j:=length(genres)+1;
			s2:=copy(genres, 1, j-1);
			name:=ClearExtraSpaces(s2);
			if name<>'' then begin
				name:=DecodeHTML(name);
				setGenre(name);
			end;
			delete(genres, 1, j);
		end;
	end;
	
	//countries
	if DlCountries then begin
		while (length(countries)>0) do begin
			j:=pos('/', countries);
			if (j=0) then j:=length(countries)+1;  //20121014-JeF
			s2:=copy(countries, 1, j-1);
			name:=ClearExtraSpaces(s2);
			if name<>'' then begin
				name:=DecodeHTML(name);
				setCountry(name);
			end;
			if (name='américain') or (name='britannique') or (name='australien') then
				ask_amazon:=true;
			delete(countries, 1, j);
		end;
	end;

	//duration
	duration:=0;
	if DlDuration then begin
		txtpos:=' x ';
		i:=pos(txtpos, longduration);
		if i>0 then longduration:=copy(longduration,i+2,length(longduration));
		longduration:=ClearExtraSpaces(longduration);
		s2:=longduration;
		duration:=0;
		j:=0;         
		s3:=ParseString(s2, '^(\d+)h', 1);
		if s3<>'' then
			try
			//extract hours to j
			j:=StrToInt(s3);
			except
			end;
		//hours to minutes
		j:=j*60;
		s3:=ParseString(s2, 'h(\d+)', 1);
		if s3='' then
			//if the duration is less than an hour
			s3:=ParseString(s2, '^(\d+)', 1);
		if s3<>'' then
			try
				duration:=StrToInt(s3);
			except
		end;
		duration:=duration+j;
	end;
	
	//Synopsis / description
	description := '';
	if DlMovieDesc then begin
		txtpos:='L''histoire';
		i:=pos(txtpos, all);
		if i>0 then begin
			i:=i+length(txtpos);
			j:=MyPos('</font></div>', all, i+1);   // 20100131-JeF
			s2:=copy(all, i, j-i+10);   // 20100131-JeF
			if s2<>'' then begin
				s2:=ParseString(s2, '<font .*?>(.*?)</font>', 1);
				description:=RemoveTags(s2);
				description:=DecodeHTML(description);
				description:=ClearExtraSpaces(description);
			end;
		end;
	end;
	
	//small image
	i:=pos('<img src="http://www.cinemotions.com/data/films', all);   // 20100220-JeF
	if i>0 then begin
		i := MyPos('http://', all, i+1);
		j:=MyPos('"', all, i+1);
		s2:=copy(all, i, j-i);
		if s2<>'' then begin
			small_image:=s2;
			front_cover_url:=small_image;
			i:=pos('h200/',front_cover_url);
			if (i>0) then begin
				delete(front_cover_url,i,5);
			end;
		end;
	end;

	//Studios
	if DlStudios then begin
		i:=pos('Sociétés de production', all);
		if (i>0) and DownloadMovieStudios then begin
			j:=MyPos('</TABLE>', UpperCase(all), i+1);
			s2:=copy(all, i+24, j-i-24);
			i:=pos('<font',s2);
			while i>0 do begin
				j:=MyPos('</font>', s2, i+1);
				s3:=copy(s2, i, j-i+8);
				s3:=ParseString(s3, '<font.*?>(.*?)</font>', 1);
				j:=pos('(',s3);
				if (j>0) then delete(s3,j,length(s3));
				j:=pos('[',s3);
				if (j>0) then delete(s3,j,length(s3));
				name:=ClearExtraSpaces(s3);
				name:=DecodeHTML(name);
				if (name<>'') and (name[1]<>'(') then
					SetStudio(name);
				i:=MyPos('<font',s2,i+4);
			end;
		end;
	end;

	//check if the 'galerie' exists
	pictures_exist:=false;
	i:=pos('/photos-',all);
	if (i>0) then
		pictures_exist:=true;
		
	//posters exist?
	posters_exist := false;
	if (Pos('/affiche-', all)>0) then
		posters_exist := true;
		
	if DownloadMovieFullCast or OvrMainDlOptions then begin
		//download actors casting page
		all:=GetHTMLPageContentFromRequest(actors_url);

		//Actors
		if DlActors then begin
			//txtpos:='Distribution Artistique';
			txtpos:='<TABLE border="0" width="96%">';
			i:=pos(txtpos, all);
			//20121014-JeF// txtpos:='HREF="http://www.cinemotions.com/modules/Artistes/fiche/';
      txtpos:='<table width="100%"'; //20121014-JeF
			i:=MyPos(txtpos, all,i);
			if i>0 then begin
				j:=MyPos('</TABLE>', all, i+1);
				s2:=copy(all, i, j-i+1);
				//20121014-JeF// occurstart:='HREF="http://www.cinemotions.com/modules/Artistes/fiche/';
				occurstart:='<table width="100%"'; //20121014-JeF
				i:=pos(occurstart, s2);
				while i>0 do begin
					j:=MyPos('</TR>', s2, i+1);
					s3:=copy(s2, i, j-i+6);   //all infos of one person in s3
					//20121014-JeF// character:=ParseString(s3, 'font.*?>(.*?)</font>', 1);
					character:=GetStr(s3, '<font class="textes">','</font>');  //20121014-JeF
					character:=DecodeHTML(character);
					small_photo := GetStr(s3, '<IMG SRC="', '"');
					k := Pos('w40', small_photo);
					if k>0 then
						delete(small_photo, k, 4);
					code:=ParseString(s3, 'HREF="http://www.cinemotions.com/(.*?)"', 1);
          if (pos('-nm',code)>0) 
            then code:=ConvertPersonCode(code);   //20121014-JeF
					name:=ParseString(s3, 'HREF=.*?>(.*?)</A>', 1);
					name:=DecodeHTML(name);
					code := ParseString(code, '(\d+)', 1);
          if name<>'' then
						setPersonName(name, character, 3, code, small_photo);
					delete(s2, 1, j+10);
					i:=pos(occurstart, s2);
				end;
			end;
		end;
		
		//download techs casting page
		all:=GetHTMLPageContentFromRequest(techs_url);
		//extract all and only casting infos
		txtpos:='Distribution Technique';
		i:=pos(txtpos, all);
		if (i>0) then delete(all,1,i);
		i:=pos('<SCRIPT LANGUAGE=',all);
		if (i>0) then delete(all,i,length(all)-i);
		
		//Directors
		if DlDirectors then begin
			txtpos:='>Réalisation<';
			i:=pos(txtpos, all);
			if i>0 then begin
				j:=MyPos('</B></font>', all, i+length(txtpos)+10);
				s2:=copy(all, i, j-i+1);  //all persons of one section
				occurstart:='HREF="http://www.cinemotions.com/'; 	//20121014-JeF
				i:=pos(occurstart, s2);
				while i>0 do begin
					j:=MyPos('</TD>', s2, i+1);
					s3:=copy(s2, i, j-i+6);   //all infos of one person in s3
					character:=ParseString(s3, 'font.*?>(.*?)</font>', 1);
					character:=DecodeHTML(character);
					code:=ParseString(s3, 'HREF="http://www.cinemotions.com/(.*?)"', 1); 	//20121014-JeF
          if (pos('-nm',code)>0) 
            then code:=ConvertPersonCode(code);   //20121014-JeF
					name:=ParseString(s3, 'HREF=.*?>(.*?)</A>', 1);
					name:=DecodeHTML(name);
					code := ParseString(code, '(\d+)', 1);
					if name<>'' then
						setPersonName(name, '', 1, code);
					delete(s2, 1, j+10);
					i:=pos(occurstart, s2);
				end;
			end;
		end;

		//Screenplay writers
		if DlWriters then begin
			txtpos:='>Ecriture<';
			i:=pos(txtpos, all);
			if i>0 then begin
				j:=MyPos('</B></font>', all, i+length(txtpos)+10);
				s2:=copy(all, i, j-i+1);  //all persons of one section
				occurstart:='HREF="http://www.cinemotions.com/'; 	//20121014-JeF
				i:=pos(occurstart, s2);
				while i>0 do begin
					j:=MyPos('</TD>', s2, i+1);
					s3:=copy(s2, i, j-i+6);   //all infos of one person in s3
					character:=ParseString(s3, 'font.*?>(.*?)</font>', 1);
					character:=DecodeHTML(character);
					code:=ParseString(s3, 'HREF="http://www.cinemotions.com/(.*?).html"', 1); 	//20121014-JeF
          if (pos('-nm',code)>0) 
            then code:=ConvertPersonCode(code);   //20121014-JeF
					name:=ParseString(s3, 'HREF=.*?>(.*?)</A>', 1);
					name:=DecodeHTML(name);
					code := ParseString(code, '(\d+)', 1);
					if name<>'' then
						setPersonName(name, '', 2, code);
					delete(s2, 1, j+10);
					i:=pos(occurstart, s2);
				end;
			end;
		end;

		//Composers
		if DlComposers then begin
			txtpos:='>Musique<';
			i:=pos(txtpos, all);
			if i>0 then begin
				j:=MyPos('</B></font>', all, i+length(txtpos)+10);
				s2:=copy(all, i, j-i+1);  //all persons of one section
				occurstart:='HREF="http://www.cinemotions.com/'; 	//20121014-JeF
				i:=pos(occurstart, s2);
				while i>0 do begin
					j:=MyPos('</TR>', s2, i+1);
					s3:=copy(s2, i, j-i+6);   //all infos of one person in s3
					character:=ParseString(s3, 'font.*?>(.*?)</font>', 1);
					character:=DecodeHTML(character);
					code:=ParseString(s3, 'HREF="http://www.cinemotions.com/(.*?).html"', 1); 	//20121014-JeF
          if (pos('-nm',code)>0) 
            then code:=ConvertPersonCode(code);   //20121014-JeF
					name:=ParseString(s3, 'HREF=.*?>(.*?)</A>', 1);
					name:=DecodeHTML(name);
					code := ParseString(code, '(\d+)', 1);
					if name<>'' then
					setPersonName(name, '', 4, code);
					delete(s2, 1, j+10);
					i:=pos(occurstart, s2);
				end;
			end;
		end;
	end
	else begin
		i := Pos('R&eacute;alisation :', all);
		if i>0 then begin
			j := MyPos('</TABLE>', all, i+1);
			s := copy(all, i, j-i);
			i := Pos('</td>', s);
			if (i>0) and DlDirectors then begin
				s2 := copy(s, 1, i);
				ExtractPeople(s2, 1);
			end;
			delete(s, 1, i);
			i := Pos('Auteurs &amp; sc&eacute;naristes', s);
			if (i>0) and DlWriters then begin
				j := MyPos('</td>', s, i+1);
				s2 := copy(s, i, j-i);
				ExtractPeople(s2, 2);
				delete(s, 1, j);
			end;
			i := Pos('avec :', s);
			if (i>0) and DlActors then begin
				j := MyPos('</font>', s, i+1);
				s2 := copy(s, i, j-i);
				ExtractPeople(s2, 3);
				delete(s, 1, j);
			end;
		end;
	end;

	//if serie, download seasons/episodes page (if exists) and extact infos
	j:=pos('Guide des ',all);
	if NOT(serie) and (j>0) then serie:=true;  //some series like 'ncis' don't give any unique string in search results page to set 'serie' flag
	if (serie) and (j>0) and (DownloadMovieSeasonsList or OvrMainDlOptions and DlEpisodes) then begin
		i:=pos('savoir plus sur',all);
		s2:=copy(all,i,j-i+15);
		url:=ParseString(s2, '<A HREF="(.*?)" ', 1);
		all:=DownloadURL(url);
		i:=pos('>Saison',all);  //!! some series like 'magnum' have corrupted tags like <hé> instead of <h2> !...
		while i>0 do begin  //scan all seasons
			j:=MyPos('>Saison', all, i+5);
			if (j=0) then j:=MyPos('</TABLE>', all, i+5);
			s2:=copy(all,i,j-i+10); //all episodes of one season in s2
			j:=pos('</h2>', s2);
			s3:=copy(s2,8,j-5);
			s3:=ClearExtraSpaces(s3);
			wyear:='';
			j:=pos('-', s3);
			if (j=0) then j:=pos(':',s3);
			if (j>0) then begin
				s4:=copy(s3,j+1,length(s3));
				if (j>1) then delete(s3,j,length(s3));
				s4:=ClearExtraSpaces(s4);
				wyear:=ParseString(s4, '^(\d+).*?</h2>', 1);
			end;
			j:=pos('<',s3);
			if (j>0) then delete(s3,j,length(s3));
			s3:=ClearExtraCharacters(s3);
			s3:=ClearExtraSpaces(s3);
			season_nbr:=StrtoInt(s3);
			episode_nbr:=0;
			j:=Pos('</h2>',s2);
			while j>0 do begin  //scan all episodes of the examined season
				episode_nbr:=episode_nbr+1;
				//Several formattings for episodes !! The #1 below seems to be the most used/recent...
				//--> Formatting template #1
				if (pos('<b>',s2)>0) then begin
					j:=MyPos('<b>',s2,j);  // why? because on the first occurrence, j isn't pointing to a <b> tag
					k:=MyPos('<br />'+#13#10+'<br />',s2,j+6);
					if (k=0) then k:=length(s2);
					s3:=copy(s2,j,k-j);  // complete description of one episode in s3
					j:=k;  //prepare next episode search
					k:=pos('</b>',s3);
					s4:=copy(s3,4,k-4);
					s4:=RemoveTags(s4);
					s4:=DecodeHTML(s4);
					s4:=ClearExtraCharacters(s4);
					k:=pos(#13#10,s4);   //zap #13#10 inside title like episode 20 of season 1 of 'desperate housewives'
					while k>0 do begin
						delete(s4,k,2);
						k:=pos(#13#10,s4);
					end;
					EpisodeTitle:=ClearExtraSpaces(s4);
					EpisodeIMDB:='';
					//Original AirDate  ( format MM/DD/YYYY )
					k:=0;
					AirDate:='';
					s4:=RemoveTags(s3);
					if (pos('/',s4)>0) then AirDate:=ExtractDate(s4)
					else k:=pos('iffusion',s3);
					if (k>0) then begin
						l:=pos('iffusion France',s3);   //  to skip french air date and get original air date...
						if (l=k) then l:=MyPos('iffusion',s3,l+10) 
						else l:=k;
						if (l>0) then begin
							// zap everything but date (with figures and/or letters!...)
							k:=l;
							l:=length(s3);
							while (k<l) and ((copy(s3,k,1)<'0') or (copy(s3,k,1)>'9')) do k:=k+1;
							while (l>k) and ((copy(s3,l,1)<'0') or (copy(s3,l,1)>'9')) do l:=l-1;
							s4:=copy(s3,k,l-k+1);
							s4:=UpperCase(s4);
							k:=pos('LE ',s4);
							if (k>0) then delete(s4,1,k+1);
							k:=pos('FRANCE',s4);
							if (k>0) then delete(s4,k,length(s4));
							k:=pos('sur',s4);
							if (k>0) then delete(s4,k,length(s4));
							k:=1;
							l:=length(s4);
							while (k<l) and ((copy(s4,k,1)<'0') or (copy(s4,k,1)>'9')) do k:=k+1;
							while (l>k) and ((copy(s4,l,1)<'0') or (copy(s4,l,1)>'9')) do l:=l-1;
							s4:=copy(s4,k,l-k+1);
							s4:=ClearExtraSpaces(s4);
							//s4 now contains only a date (with figures and/or letters!...)
							k:=length(s4);
							while (k>1) and (copy(s4,k,1)>='0') and (copy(s4,k,1)<='9') do k:=k-1;
							wyear:=copy(s4,k+1,length(s4));
							delete(s4,k,length(wyear)+1);
							//now extracting day
							day:='';
							k:=1;
							while (k<length(s4)) and (copy(s4,k,1)>='0') and (copy(s4,k,1)<='9') do k:=k+1;
							if (k<=2) then begin
								day:=copy(s4,1,k-1);
								delete(s4,1,k);
							end;
							//now extacting month (which is between day and year)
							s4:=ClearExtraSpaces(s4);
							if (copy(s4,1,1)='/') then delete(s4,1,1);
							if (copy(s4,length(s4),1)='/') then delete(s4,length(s4),1);
							month:=ClearExtraSpaces(s4);
							if (length(month)>2) then begin
								if (pos('JAN',month)>0) then month:='01';
								if (pos('FEV',month)>0) then month:='02';
								if (pos('FÉV',month)>0) then month:='02';
								if (pos('MAR',month)>0) then month:='03';
								if (pos('AVR',month)>0) then month:='04';
								if (pos('MAI',month)>0) then month:='05';
								if (pos('JUN',month)>0) then month:='06';
								if (pos('JUIN',month)>0) then month:='06';
								if (pos('JUL',month)>0) then month:='07';
								if (pos('JUIL',month)>0) then month:='07';
								if (pos('AO',month)>0) then month:='08';
								if (pos('SEP',month)>0) then month:='09';
								if (pos('OCT',month)>0) then month:='10';
								if (pos('NOV',month)>0) then month:='11';
								if (pos('DEC',month)>0) then month:='12';
								if (pos('DÉC',month)>0) then month:='12';
							end;
							if (day='') then day:='01';
							if (month='') then month:='01';
							//now formatting AirDate
							AirDate:=month+'/'+day+'/'+wyear;
						end;
					end;
					EpisodeDescription:='';
					k:=pos('</font>',s3);
					while (MyPos('</font>',s3,k+3)>0) do begin
						l:=k;
						k:=MyPos('</font>',s3,k+3);
					end;
					if (pos('</TABLE>',s3)>0) then k:=l;  //step one </font> back if end of all seasons
					s4:='';
					if (k>0) then s4:=copy(s3,k,length(s3)-k+1);
					s4:=RemoveTags(s4);
					s4:=DecodeHTML(s4);
					s4:=ClearExtraCharacters(s4);
					s4:=ClearExtraSpaces(s4);
					EpisodeDescription:=s4;
					SetEpisode(season_nbr, episode_nbr, EpisodeTitle, EpisodeDescription, EpisodeIMDB, AirDate);
					j:=MyPos('<b>',s2,j+5);
				end
				else begin  //--> Formatting Template #beurk...
					//here is an old(?) and crappy formatting for old series like 'magnum'...
					s3:='';
					k:=MyPos('<br />', s2, j+6);
					if (k>0) then s3:=copy(s2,j+6,k-j-6);
					k:=pos('hr>',s3);
					if (k>0) then delete(s3,1,3);
					if (s3<>'') then s3:=ClearExtraCharacters(s3);
					k:=pos(')',s3);
					l:=pos('(',s3);
					if (k<l) then delete(s3,1,k);
					if (s3<>'') then s3:=ClearExtraSpaces(s3);
					if (s3='') then begin
						j:=0;
						continue;
					end;
					EpisodeTitle:=RemoveTags(s3);
					EpisodeIMDB:='';
					AirDate:='01/01/'+year;  //format MM/DD/YYYY
					EpisodeDescription:='';
					{
					//No Description for 'magnum' serie : assuming for now there's no desc for other series using this crappy formatting...
					}
					SetEpisode(season_nbr, episode_nbr, EpisodeTitle, EpisodeDescription, EpisodeIMDB, AirDate);
					j:=MyPos('<br />',s2,j+10);
				end;
			end;
			i:=MyPos('>Saison',all,i+10);
		end;
	end;
	
	// IMDB infos
	if DlIMDBInfos then GetIMDBInfos(title,year);
	
end;

//returned images links
procedure GetImages;
begin
	if DownloadMovieBigCover and (front_cover_url<>'') then
		SetImageName('', front_cover_url, '', 1)
	else begin
		if small_image<>'' then
			SetImageName('', small_image, '', 1);
		if front_cover_url<>'' then
			SetImageName('', front_cover_url, '', 4);
	end;
	if pictures_exist and (MaxShotsCount>0) then GetShots;
	if posters_exist and (MaxPostersCount>0) then GetPosters;
end;

procedure GetPosters;
var
	url, s, s2, s3: string;
	ShotsCount, i, j, k, l: integer;
begin
	//if ask_amazon then
	//	GetAmazonImages;
	ShotsCount := 0;
	url:='http://www.cinemotions.com/affiche-s-tt'+movie_code;
	s:=DownloadURL(url);
	i := Pos('<A name="affiche"><h1', s);
	if i>0 then begin
		j := MyPos('<br><br>', s, i+1);
		if j = 0 then
			j := Length(s);
		s2 := copy(s, i, j-i);
		if j>i then begin
			i:=pos('http://www.cinemotions.com/data/',s2);
			while (i>0) and (ShotsCount<MaxPostersCount) do begin
				j:=MyPos('"',s2,i);
				s3:=copy(s2,i,j-i);
				inc(ShotsCount);
				SetImageName('', s3, '', 4);
				delete(s2, 1, j);
				i:=Pos('http://www.cinemotions.com/data/',s2);
			end;
		end;
	end;
end;

procedure GetShots;
var
	url, s, s2, s3: string;
	ShotsCount, i, j, k, l: integer;
begin
	//if ask_amazon then
	//	GetAmazonImages;
	ShotsCount := 0;
	url:='http://www.cinemotions.com/photos-s-tt'+movie_code;
	s:=DownloadURL(url);
	i := Pos('<h2>Photos 1', s);
	if i>0 then begin
		j := MyPos('<h2>Photos 1', s, i+1);
		s2 := copy(s, i, j-i);
		if j>i then begin
			i:=pos('http://www.cinemotions.com/data/',s2);
			while (i>0) and (ShotsCount<MaxShotsCount) do begin
				j:=MyPos('"',s2,i);
				s3:=copy(s2,i,j-i);
				if pos('affiche',s3)=0 then begin
					inc(ShotsCount);
					SetImageName('', s3, '', 3);
				end;
				delete(s2, 1, j);
				i:=Pos('http://www.cinemotions.com/data/',s2);
			end;
		end;
	end;
end;

//get person 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;

// File Exists Tool // 20090808-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;

// Debug
procedure TrcDump(TrcData, Suffix:string; TrcMode: integer);
var
	Path: string;
	sl: TStringList;
begin
	Path:=ScriptsPath+Script_Name+Suffix+'.trc';
	sl:=TStringList.create;
	if (TrcMode=1) then begin
		if MyFileExists(Path) then sl.LoadFromFile(Path);
		sl.Add(TrcData);
	end
	else sl.Text:=TrcData;
	sl.SaveToFile(Path);
	sl.free;
end;
function TrcLoad(Suffix: string): string;
var
	Path: string;
	sl: TStringList;
begin
	Path:=ScriptsPath+Script_Name+Suffix+'.trc';
	sl:=TStringList.create;
	sl.Text:='';
	if MyFileExists(Path) then sl.LoadFromFile(Path);
	result:=sl.Text;
	sl.free;
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, cbDlOriginalTitle, cbDlYear, cbDlGenres, cbDlCountries, cbDlDuration: TCheckBox;
	cbDlMovieDesc, cbDlDirectors, cbDlActors, cbDlWriters, cbDlComposers, cbDlStudios, cbDlEpisodes: TCheckBox;
	cbDlIMDBInfos, 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_Mov_OvrMainDlOptions', BoolToInt(cbOvrMainDlOptions.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlOriginalTitle' , BoolToInt(cbDlOriginalTitle.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlYear'          , BoolToInt(cbDlYear.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlGenres'        , BoolToInt(cbDlGenres.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlCountries'     , BoolToInt(cbDlCountries.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlDuration'      , BoolToInt(cbDlDuration.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlStudios'       , BoolToInt(cbDlStudios.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlEpisodes'      , BoolToInt(cbDlEpisodes.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlMovieDesc'     , BoolToInt(cbDlMovieDesc.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlDirectors'     , BoolToInt(cbDlDirectors.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlActors'        , BoolToInt(cbDlActors.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlWriters'       , BoolToInt(cbDlWriters.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlComposers'     , BoolToInt(cbDlComposers.Checked));
		RegWriteInteger('CINEMOTIONS_Mov_DlIMDBInfos'     , BoolToInt(cbDlIMDBInfos.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_Mov_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;
	cbDlOriginalTitle := TCheckBox.Create(f);
	cbDlOriginalTitle.Name := 'cbDlOriginalTitle';
	cbDlOriginalTitle.Parent := gbDlOptions;
	cbDlOriginalTitle.SetBounds(posx, posy, 200, 20);
	cbDlOriginalTitle.Caption := GetLangStr(167);
	cbDlOriginalTitle.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlOriginalTitle', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlDuration := TCheckBox.Create(f);
	cbDlDuration.Name := 'cbDlDuration';
	cbDlDuration.Parent := gbDlOptions;
	cbDlDuration.SetBounds(posx, posy, 200, 20);
	cbDlDuration.Caption := GetLangStr(147);
	cbDlDuration.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlDuration', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlYear := TCheckBox.Create(f);
	cbDlYear.Name := 'cbDlYear';
	cbDlYear.Parent := gbDlOptions;
	cbDlYear.SetBounds(posx, posy, 200, 20);
	cbDlYear.Caption := GetLangStr(169);
	cbDlYear.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlYear', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlGenres := TCheckBox.Create(f);
	cbDlGenres.Name := 'cbDlGenres';
	cbDlGenres.Parent := gbDlOptions;
	cbDlGenres.SetBounds(posx, posy, 200, 20);
	cbDlGenres.Caption := GetLangStr(179);
	cbDlGenres.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlGenres', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlCountries := TCheckBox.Create(f);
	cbDlCountries.Name := 'cbDlCountries';
	cbDlCountries.Parent := gbDlOptions;
	cbDlCountries.SetBounds(posx, posy, 200, 20);
	cbDlCountries.Caption := GetLangStr(180);
	cbDlCountries.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlCountries', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlStudios := TCheckBox.Create(f);
	cbDlStudios.Name := 'cbDlStudios';
	cbDlStudios.Parent := gbDlOptions;
	cbDlStudios.SetBounds(posx, posy, 200, 20);
	cbDlStudios.Caption := GetLangStr(493);
	cbDlStudios.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlStudios', 1));

	posx := 220;
	posy := 0;
	
	posy := posy + 20;
	cbDlMovieDesc := TCheckBox.Create(f);
	cbDlMovieDesc.Name := 'cbDlMovieDesc';
	cbDlMovieDesc.Parent := gbDlOptions;
	cbDlMovieDesc.SetBounds(posx, posy, 200, 20);
	cbDlMovieDesc.Caption := GetLangStr(162);
	cbDlMovieDesc.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlMovieDesc', 1));
		
	posy := posy + 20;
	cbDlEpisodes := TCheckBox.Create(f);
	cbDlEpisodes.Name := 'cbDlEpisodes';
	cbDlEpisodes.Parent := gbDlOptions;
	cbDlEpisodes.SetBounds(posx, posy, 200, 20);
	cbDlEpisodes.Caption := GetLangStr(525);
	cbDlEpisodes.Hint := GetLangStr(1285);
	cbDlEpisodes.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlEpisodes', 1));

	posy := posy + 20;
	cbDlDirectors := TCheckBox.Create(f);
	cbDlDirectors.Name := 'cbDlDirectors';
	cbDlDirectors.Parent := gbDlOptions;
	cbDlDirectors.SetBounds(posx, posy, 200, 20);
	cbDlDirectors.Caption := GetLangStr(181);
	cbDlDirectors.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlDirectors', 1));
		
	posy := posy + 20;
	cbDlActors := TCheckBox.Create(f);
	cbDlActors.Name := 'cbDlActors';
	cbDlActors.Parent := gbDlOptions;
	cbDlActors.SetBounds(posx, posy, 200, 20);
	cbDlActors.Caption := GetLangStr(164);
	cbDlActors.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlActors', 1));
		
	posy := posy + 20;
	cbDlWriters := TCheckBox.Create(f);
	cbDlWriters.Name := 'cbDlWriters';
	cbDlWriters.Parent := gbDlOptions;
	cbDlWriters.SetBounds(posx, posy, 200, 20);
	cbDlWriters.Caption := GetLangStr(182);
	cbDlWriters.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlWriters', 1));
		
	posy := posy + 20;
	cbDlComposers := TCheckBox.Create(f);
	cbDlComposers.Name := 'cbDlComposers';
	cbDlComposers.Parent := gbDlOptions;
	cbDlComposers.SetBounds(posx, posy, 200, 20);
	cbDlComposers.Caption := GetLangStr(491);
	cbDlComposers.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlComposers', 1));
		
	posx :=  10;
	posy := gbDlOptions.Top + gbDlOptions.Height - 5;

	posy := posy + 20;
	cbDlIMDBInfos := TCheckBox.Create(f);
	cbDlIMDBInfos.Name := 'cbDlIMDBInfos';
	cbDlIMDBInfos.Parent := f;
	cbDlIMDBInfos.SetBounds(posx, posy, 400, 20);
	cbDlIMDBInfos.Caption := GetLangStr(1684);
	cbDlIMDBInfos.Hint := GetLangStr(1684);
	cbDlIMDBInfos.Checked := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlIMDBInfos', 0));
	
	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

	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_Mov_OvrMainDlOptions', 0));
	DlOriginalTitle  := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlOriginalTitle', 1));
	DlYear           := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlYear', 1));
	DlGenres         := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlGenres', 1));
	DlCountries      := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlCountries', 1));
	DlDuration       := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlDuration', 1));
	DlStudios        := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlStudios', 1));
	DlEpisodes       := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlEpisodes', 1));
	DlMovieDesc      := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlMovieDesc', 1));
	DlDirectors      := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlDirectors', 1));
	DlActors         := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlActors', 1));
	DlWriters        := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlWriters', 1));
	DlComposers      := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlComposers', 1));
	// Download (or not) IMDB infos (id, rating, mpaa, mpaa rating)
	DlIMDBInfos      := IntToBool(RegReadInteger('CINEMOTIONS_Mov_DlIMDBInfos', 0)); 

end.