// ce scripte a pour but de vous montrer comment aspirer les résultats à partir du portail CRISP

AUTORUN;
var PIName:string;
var PIEmail:string;
var PITitle:string;
var ProjectTitle:string;
var Abstract:string;        
var Thesaurus_Terms_temp:string;
var Thesaurus_Terms:string;
var Institution:string;
var Fiscal_Year: string;
var Department:string;
var projectstart,projectend:string;
var s:string;
var links:string;
var i:integer;
var ts: Tstringlist;ts:=Tstringlist.create;

/////////// partie1

BlockJava(true,1);
BlockSounds(true,1);
BlockActiveX(true,1);
BlockPopups(true,1);


navigate('http://crisp.cit.nih.gov/crisp/crisp_query.generate_screen',1);
showmessage('Naviguez sur la page jusqu''à apparition des résultats');

repeat;
    sleep(10);
until ( pos('crisp_lib.query',getBrowserUrl(1))>0);
wait_browser(1);


/////////// partie 2
links:=extracturlsfrombrowser(1);
ts.commatext:=links;
println(ts.count);
createTable('PIName','PIEmail','PITitle','ProjectTitle','Abstract','Thesaurus_Terms','Institution','Fiscal_Year','Department','ProjectSTart','ProjectEnd');
DataBaseSize(2048);
newbrowser(2);

for i:=2 to ts.count-1 do begin;

    navigate(ts[i],2);
    //println(ts[i]);
    PIName:=getText('TD(0)/TR(3)/TBODY(0)/TABLE(0)/EM(0)/H2(0)/BLOCKQUOTE(0)/BODY(0)/HTML(0)/',2);
    //println(PIName);
    PIEmail:=getText('A(0)/TD(0)/TR(2)/TBODY(0)/TABLE(0)/EM(0)/H2(0)/BLOCKQUOTE(0)/BODY(0)/HTML(0)/',2);
    //println(PIEmail);

    PITitle:=getText('TD(0)/TR(1)/TBODY(0)/TABLE(0)/EM(0)/H2(0)/BLOCKQUOTE(0)/BODY(0)/HTML(0)/',2);
    //println(PITitle);

    ProjectTitle:=getText('TD(0)/TR(0)/TBODY(0)/TABLE(0)/EM(0)/H2(0)/BLOCKQUOTE(0)/BODY(0)/HTML(0)/',2);
    //println(ProjectTitle);

    Abstract:=getText('P(2)/EM(0)/H2(0)/BLOCKQUOTE(0)/BODY(0)/HTML(0)/',2);
    Abstract:=Trim(Abstract);
    //println(Abstract);

    Thesaurus_Terms_temp:=getText('P(1)/EM(0)/H2(0)/BLOCKQUOTE(0)/BODY(0)/HTML(0)/',2);
    //println(Thesaurus_Terms_temp);
    i:=1;

    Thesaurus_Terms:=trim(parsetag(Thesaurus_Terms_temp,'Thesaurus Terms:','Institution:',i));
    ReplaceNL(Thesaurus_Terms,'/');
    println(Thesaurus_Terms);
    i:=1;
    Institution:=trim(parsetag(Thesaurus_Terms_temp,'Institution:','Fiscal Year:',i));
    ReplaceNL(Institution,'/');
    //println(Institution);

    i:=1;
    Fiscal_Year:=trim(parsetag(Thesaurus_Terms_temp,'Fiscal Year:','Department',i));
    //println(Fiscal_Year);

    i:=1;
    Department:=trim(parsetag(Thesaurus_Terms_temp,'Department:','Project',i));
    //println(Department);

    i:=1;
    projectstart:=trim(parsetag(Thesaurus_Terms_temp,'Start:','Project',i));
    //println(Department);

    i:=1;
    projectend:=trim(parsetag(Thesaurus_Terms_temp,'End:','ICD',i));
    //println(Department);



    InsertData(PIName,PIEmail,PITitle,ProjectTitle,Abstract,Thesaurus_Terms,Institution,Fiscal_Year,Department,projectstart,projectend);
end;
//ShowdataBase;
exportDataToExcel;
//quit;