'#Script "Capture PJEspagne"
'#Version "2.0"
'#Signature "Demo"
'
'On creer la base de donnée
'#Base "Nom,Adresse,CP,Ville,Region,Tel,Fax,Email,Site"
'
Action.Valeur = "Start"
Action.doLoop = True
'
Call Main
'Fonction executée a chaque chargement de page
Public Function doLoop()
	Set Suite = getBouton
	If Not Suite Is Nothing Then
        Call Main
	Else
        Call Main
        BarreInfo.Text = "Fin"
		Action.doLoop = False
	End If
End Function
'Main
Public Sub Main()
    Select Case Action.Valeur
    Case "Start"
		Set oLIs = InternetExplorer(1).Document.All.Tags("LI")
		For Each oLI In oLIs
		bFicha = False
		bTel = False
		Set Link = Nothing
		Set LI = Nothing
			If Left(uCase(Trim(oLI.className)),5) = "VCARD" Then
				Set oAs = oLI.All.Tags("A")
				For Each oA In oAs
					If uCase(Left(oA.innerText,5)) = "FICHA" And bTel = False Then
						'On a le lien de la fiche détaillée
						bFicha = True
						Set Link = oA	'Le lien vers le détail
						Set LI = oLi	'Le Block avec tout
						Exit For
					End If
				Next
				If bFicha = False Then
					For Each oA In oAs
						If oA.firstChild.className = "org" Then
							'On a le lien de la fiche détaillée
							bTel = True
							Set Link = oA	'Le lien vers le détail
							Set LI = oLi	'Le avec tout
							Exit For
						End If
					Next
				End If
			End If
		'On a tout faut decoder
		If bTel = True Or bFicha = True Then
			Ajax.Send "GET", Link.href, True
			Set oDocument = Ajax.ajaxDocument.body
			Set oDocs = oDocument.All.Tags("DIV")
			'On recherche la fiche VCARD
			For Each oDoc In oDocs
				If oDoc.className = "vcard" Then
					DecodePJEspagne oDoc
				End If
			Next
		End If
		Next
		'On va à la page suivante
		Set Suite = getBouton
		If Not Suite Is Nothing Then
			Suite.Click
		End If

    End select
End Sub
'
Public Function getBouton()
    Set oScripts = InternetExplorer(1).Document.All.Tags("a")
    For Each oScript In oScripts
		If Left(oScript.InnerText,9) = "Siguiente" Then
			Set getBouton = oScript
			Exit Function
		End If
	Next
	Set getBouton = Nothing
End Function
'
Public Function DecodePJEspagne(oDoc)
	Set ObjRegex = New RegExp
    ObjRegex.Global = True
    ObjRegex.IgnoreCase = True

	'Societe
	Set oH1s = oDoc.All.Tags ("H1")
	For Each oH1 in oH1s
		If oH1.className = "org" Then
			Nom = Trim(oH1.innerText)
		End If
	Next
	'Adresse CP VILLE REGION
	Set oPs = oDoc.All.Tags ("P")
	For Each oP in oPs
		If oP.className = "adr" Then
			Set oCoords = oP.All.Tags ("SPAN")
			For Each oCoord In oCoords
				Select Case oCoord.className
					Case "street-address"
						Adresse = Trim(oCoord.innerText)
					Case "postal-code"
						CodePostal = Trim(oCoord.innerText)
					Case "locality"
						Ville = Trim(oCoord.innerText)
					Case "region"
						Region = Trim(oCoord.innerText)
				End Select
			Next
		End If
	Next	
	'Tel Fax
	Set oSpans = oDoc.All.Tags ("SPAN")
	For Each oSpan in oSpans
		If oSpan.className = "tel" Then
			If Left(Trim(oSpan.previousSibling.data),3) = "Tel" Then
				Tel = oSpan.innerText			
			End If
			If Right(Trim(oSpan.previousSibling.data),4) = "Fax:" Then
				Fax = oSpan.innerText
			End If
		End If
	Next
	'Site et Email
	Set oAs = oDoc.All.Tags ("A")
	For Each oA in oAs
		Select Case oA.className
			Case "url"
				Site = Trim(oA.innerText)
			Case "email"
				Mail = Trim(oA.innerText)
		End Select
	Next	
    'If chkDoublons(Tel, 4) = False Then
        Set Record = Base.ListItems.Add(, , CStr(Nom))
        Record.SubItems(1) = CStr(Adresse)
        Record.SubItems(2) = CStr(Codepostal)
        Record.SubItems(3) = CStr(Ville)
        Record.SubItems(4) = CStr(Region)
        Record.SubItems(5) = CStr(Tel)
        Record.SubItems(6) = CStr(Fax)
        Record.SubItems(7) = CStr(Mail)
        Record.SubItems(8) = CStr(Site)
        Record.Checked = True
        Base.Refresh
    'End If
End Function





