#include <IE.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

_IEErrorHandlerRegister()

;*************************************************************
Func DPW_IE_GetElemntUnderCur($hwnd, $x, $y)
    $ret = ""
    
    $title = ""
    $url = ""
    $tag = ""
    $id = ""
    $name =  ""
    $options_vals = ""
    $options_texts = ""
    
    $parent_hwnd = _WinAPI_GetAncestor ( $hwnd , $GA_ROOTOWNER )
    
    If ($parent_hwnd) Then
        $title = WinGetTitle ( $parent_hwnd )
        
        $exp_name_pos = StringInStr ( $title, " - ", $STR_CASESENSE, -1) - 1
        If($exp_name_pos >= 0) Then
            $title = StringTrimRight($title, StringLen($title) - $exp_name_pos) 
        EndIf
           
        if ($title <> "") Then
            $ie = _IEAttach($title)

            if (not @error) AND _IEPropertyGet($ie, 'visible') Then
                ;$title = _IEPropertyGet($ie, 'title')
                $url = _IEPropertyGet($ie, 'locationurl')
                
                $elm = $ie.document.elementFromPoint($x, $y)

                if IsObj($elm) Then
                    $tag = $elm.tagName & "-" & $elm.type
                    $id = $elm.id
                    $name = $elm.name
                    
                    if($tag == 'SELECT-select-one') Then
                        ;$options = _IETagNameGetCollection ( $elm, "option" )
                        $options = $elm.getElementsByTagName("option")
                        For $option In $options
                            $options_vals &= "|" & $option.value 
                            $options_texts &= "|" & $option.text
                        Next
                        $options_vals = StringTrimLeft($options_vals, 1)
                        $options_texts = StringTrimLeft($options_texts, 1)
                    ElseIf($tag == 'INPUT-checkbox') Then
                        $options_vals = "0|1"
                    EndIf
                    
                EndIf
            Else
                $x = @error
            EndIf
        EndIf
    EndIf
    
    $ret = $url & ';' & $title & ';' & $tag & ';' & $id & ';' & $name & ';' & $options_vals & ';' & $options_texts & ';'
    ;ConsoleWrite($parent_hwnd & ";" & $hwnd & ";" & $ie_hwnd & ";" & $ret & @LF)
    
    return $ret
EndFunc

Func DPW_IE_GetElemntUnderCur2($hwnd_var, $x_var, $y_var)
   $ret = ""
   If(IsDeclared ( $hwnd_var ) AND IsDeclared ( $x_var ) AND IsDeclared ( $y_var )) Then
	  $ret = DPW_IE_GetElemntUnderCur(Eval ( $hwnd_var ), Eval ( $x_var ), Eval ( $y_var ))
   EndIf
   return $ret
EndFunc

;*************************************************************
Func DPW_IE_Attach($wnd_name)
   ;Global $ie = _IEAttach(StringLeft ( $wnd_name, 6))
   Global $ie = _IEAttach($wnd_name)
   Global $ie_hWnd = ControlGetHandle(_IEPropertyGet($ie, 'hwnd'), '', 'Internet Explorer_Server1')
EndFunc

Func DPW_IE_Attach2($var_str)
    if(IsDeclared ( $var_str )) Then
        DPW_IE_Attach(Eval ( $var_str ))
    Endif
EndFunc


;*************************************************************
Func DPW_IE_SetTag($type, $id, $value)
    If($type == "INPUT-text") Then
        SetInputText($id, $value)
    ElseIf ($type == "SELECT-select-one") Then
        SetSelectValue($id, $value)
        SetSelectText($id, $value)
    ElseIf ($type == "INPUT-checkbox") Then
        SetCheckbox($id, $value)
    ElseIf ($type == "INPUT-radio") Then
        SetRadio($id, $value)
    EndIf
EndFunc

Func DPW_IE_SetTag2()
    if(IsDeclared ( "__IE_TAG_TYPE" ) AND IsDeclared ( "__IE_TAG_ID" ) AND  IsDeclared ( "__IE_TAG_VALUE" )) Then
        DPW_IE_SetTag($__IE_TAG_TYPE, $__IE_TAG_ID, $__IE_TAG_VALUE)
    EndIf
EndFunc


;*************************************************************
Func SetInputText($id, $val)
   For $i = 0 To 2 Step 1
	  $obj = _IEGetObjById($ie,$id)
	  If(@error == 0) Then
		;$obj.value = $val
        _IEAction ( $obj, "focus" )
        _IEFormElementSetValue ( $obj, $val )
        $obj.fireEvent("onkeydown")
        Send("{LEFT}")
		Return
	  EndIf
	  Sleep(1000)
   Next
EndFunc

Func SetInputText2($id, $var_str)
   If(IsDeclared ( $var_str )) Then
	  SetInputText($id, Eval ( $var_str ))
   EndIf
EndFunc

;*************************************************************
Func SetSelectValue($id, $val)
   For $i = 0 To 2 Step 1
	  $obj =  _IEGetObjById($ie,$id)
	  If(@error == 0) Then
         _IEAction($obj, "focus")
		 _IEFormElementOptionSelect($obj, $val, 1, "byValue", 1)
         Send("{LEFT}")
		 Return
	  EndIf
	  Sleep(1000)
   Next
EndFunc

Func SetSelectValue2($id, $var_str)
   If(IsDeclared ( $var_str )) Then
	  SetSelectValue($id, Eval ( $var_str ))
   EndIf
EndFunc

;*************************************************************
Func SetSelectText($id, $val)
   For $i = 0 To 2 Step 1
	  $obj =  _IEGetObjById($ie,$id)
	  If(@error == 0) Then
         _IEAction($obj, "focus")
		 _IEFormElementOptionSelect($obj, $val, 1, "byText", 1)
         Send("{LEFT}")
		 Return
	  EndIf
	  Sleep(1000)
   Next
EndFunc

Func SetSelectText2($id, $var_str)
   If(IsDeclared ( $var_str )) Then
	  SetSelectText($id, Eval ( $var_str ))
   EndIf
EndFunc


;*************************************************************
Func SetCheckbox($id, $val)
   For $i = 0 To 2 Step 1
	  $obj =  _IEGetObjById($ie,$id)
	  If(@error == 0) Then
        _IEAction($obj, "focus")
        $obj.checked = ($val <> 0) AND ($val <> "0") AND ($val <> "")
		Return
	  EndIf
	  Sleep(1000)
   Next
EndFunc

Func SetCheckbox2($id, $var_str)
   If(IsDeclared ( $var_str )) Then
	  SetCheckbox($id, Eval ( $var_str ))
   EndIf
EndFunc


;*************************************************************
Func SetRadio($id, $val)
   For $i = 0 To 2 Step 1
	  $obj =  _IEGetObjById($ie,$id)
	  If(@error == 0) Then
        _IEAction($obj, "focus")
        $obj.checked = ($val <> 0) AND ($val <> "0") AND ($val <> "")
		Return
	  EndIf
	  Sleep(1000)
   Next
EndFunc

Func SetRadio2($id, $var_str)
   If(IsDeclared ( $var_str )) Then
	  SetRadio($id, Eval ( $var_str ))
   EndIf
EndFunc
