
<?php

  $module  = $_REQUEST['module'];
  $cM      = $_REQUEST['createModule'];
  $lA      = $_REQUEST['loaderAction'];
  $fN      = $_REQUEST['loaderFieldName'];
  $record  = $_REQUEST['record'];
  if($lA == "EditView") {
    ob_start();
      $_REQUEST['module'] = $cM;
      $_REQUEST['action'] = "EditView";
      $_REQUEST['record'] = $record;   
      $_POST['module'] = $cM;
      $_POST['action'] = "EditView";
      $_POST['record'] = $record;
      $_GET['record'] = $record;
      include('index.php');
    $out = ob_get_contents();
    ob_end_clean();
    echo $out;
    return;
  }

  if($lA == "get_module_fields") {
    global $beanList, $beanFiles;
    $file = 'cache/modules/'.$cM.'/'.$beanList[$cM].'vardefs.php';
    if(file_exists($file)) { 
      include($file); 
      $dict = $GLOBALS['dictionary'][$beanList[$cM]]['fields']; 
    }
    else {
      $file = 'modules/'.$cM.'/vardefs.php';
      if(file_exists($file)) { include($file); $dict = $dictionary[$beanList[$cM]]['fields']; } else return;
    }

    $file = $beanFiles[$beanList[$cM]];
    if(file_exists($file)) {
      require_once($file);
      $bean = new $beanList[$cM]();
      $bean->retrieve($record);
      if(isset($bean->id) && $bean->id != '') {
        $arr = array(); 
        foreach($dict as $key => $value) {
          if(isset($bean->$value['name']) && (is_string($bean->$value['name']) || is_float($bean->$value['name']) || is_int($bean->$value['name']) || is_bool($bean->$value['name'])))
            $arr[$value['name']] = $bean->$value['name'];
        }
        $json = getJSONobj();
        echo '['.$json->encode($arr).']';
      } else return;
    }
    return;
  }

?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <title></title>
  <script type="text/javascript" src="include/javascript/sugar_grp1_yui.js?s=5.0.0c&c="></script>
  </head>
  <body>
  
    <div id="hidder" style="position:absolute;left:0;top:0;width:105%;height:100%;background-color:white;visibility:visible;text-align:center;padding:40px;"><img src="themes/default/images/loading.gif"/></div>
   
    <script language="javascript">
    
      function doRequest(where,post,success,error) {
        this.Display = function(result) { success(result.responseText); }
        this.Fail = function(result){ if(error) error(result); }
        YAHOO.util.Connect.asyncRequest('POST',where,{success:this.Display,failure:this.Fail},post);       
      }      
    
      function iframeLoad() {
        var frame = document.getElementById("input_create");
        var doc = frame.contentDocument;
        if(doc == undefined || doc == null)
          doc = frame.contentWindow.document;  
          
        if(doc && doc.forms && ((doc.forms.EditView || doc.forms.DetailView || doc.forms.Save) || doc.return_module_fields)) {
          
          if(doc.forms.EditView) {

          } else
          if(doc.forms.DetailView) {
            var record = doc.forms.DetailView.record;
            if(record && record.value != "") {
              doRequest(
                "index.php",
                "module="+module+"&action=formloader&loaderAction=get_module_fields&createModule="+cM+"&to_pdf=1&record="+record.value,
                function(result) {
                  if(result == '') 
                    window.close();
                  else {
                    var obj = eval(result);
                    if(obj) {
                      obj = obj[0];
                      eval('window.opener.'+fN+'.responseData(obj)');
                    }
                    window.close();
                  }
                },
                function(result) {
                  window.close();
                }
              );
            } else window.close();
          } else
          if(doc.forms.Save) {

          }
          
        } else { window.close(); return; }  
        
        var main = doc.getElementById('main');
        if(main) {
          var div = doc.createElement('div');
          div.innerHTML = main.innerHTML;
          main.innerHTML = '';
          var div2 = doc.createElement('div');
          div2.style.display = 'none';
          div2.innerHTML = doc.body.innerHTML;
          doc.body.innerHTML = '';
          doc.body.appendChild(div2);
          doc.body.appendChild(div);
          if(doc && doc.forms && !doc.forms.DetailView) doc.body.FormLoader = true;
        }
        
      }
      
      var oldLocation = '';
      setInterval(function(){
        var frame = document.getElementById("input_create");
        if(frame) {
          var doc = frame.contentDocument;
          if(doc == undefined || doc == null)
            doc = frame.contentWindow.document;   
          
          if(doc && doc.body && doc.body.FormLoader) { 
            if(hidder.style.visibility = "visible") hidder.style.visibility = "hidden";
          } else {
            if(hidder.style.visibility = "hidden") hidder.style.visibility = "visible"
          }
        }
      },60);
      
    </script>

    <?php
      if($lA == "ViewForm") {
        echo '<script language="javascript">
              var module = "'.$module.'";
              var cM = "'.$cM.'";
              var lA = "'.$lA.'";
              var fN = "'.$fN.'";
              </script>';
        $data = '';
        foreach($_REQUEST as $key=>$value) {
          if(strpos($key,"fl_") === 0) {
            $data .= "&".substr($key,3)."=".$value;
          }
        }
        echo "<iframe onload='iframeLoad();' frameborder='no' width='100%' height='100%' id='input_create' name='input_create' src='index.php?module=$module&action=formloader&to_pdf=1&loaderAction=EditView&createModule=$cM".$data."'></iframe>";
      }
    ?>
  </body>
</html>