<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004 - 2007 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
********************************************************************************/



if( !isset( $install_script ) || !$install_script ){
	die($mod_strings['ERR_NO_DIRECT_SCRIPT']);
}

$validationErr = '';
if( isset($validation_errors) ){
	if( count($validation_errors) > 0 ){
		 $validationErr  = '<div id="errorMsgs" >';
		 $validationErr .= "<p class='error'>{$mod_strings['ERR_DBCONF_VALIDATION']}</p>";
		 $validationErr .= '<ul>';

		 foreach( $validation_errors as $error ){
				$validationErr .= '<li>' . $error . '</li>';
		 }
		 $validationErr .= '</ul>';
		 $validationErr .= '</div>';
	}
}


// DB split 
$createDbCheckbox = '';
$createDb = (isset($_SESSION['setup_db_create_database']) && !empty($_SESSION['setup_db_create_database'])) ? 'checked="checked"' : '';
$dropCreate = (isset($_SESSION['setup_db_drop_tables']) && !empty($_SESSION['setup_db_drop_tables'])) ? 'checked="checked"' : ''; 
$demoData = (isset($_SESSION['setup_db_pop_demo_data']) && !empty($_SESSION['setup_db_pop_demo_data'])) ? 'checked="checked"' : '';
$mbDemoData = (isset($_SESSION['setup_db_use_mb_demo_data']) && $_SESSION['setup_db_use_mb_demo_data'] == 'yes') ? 'checked="checked"' : '';
$instanceName = '';
if (isset($_SESSION['setup_db_host_instance']) && !empty($_SESSION['setup_db_host_instance'])){
	$instanceName = $_SESSION['setup_db_host_instance'];
}


///////////////////////////////////////////////////////////////////////////////
////	BEGIN PAGE OUTPUT

$out =<<<EOQ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta http-equiv="Content-Script-Type" content="text/javascript">
	<meta http-equiv="Content-Style-Type" content="text/css">
	<title>{$mod_strings['LBL_WIZARD_TITLE']} {$mod_strings['LBL_DBCONF_TITLE_USER_INFO']}</title>
	<link rel="stylesheet" href="install/install.css" type="text/css" />
	<script type="text/javascript" src="install/installCommon.js"></script>
	<script type="text/javascript" src="install/dbConfig.js"></script>
   <link REL="SHORTCUT ICON" HREF="include/images/sugar_icon.ico">
   <script src="include/javascript/sugar_grp1_yui.js?s={SUGAR_VERSION}&c={JS_CUSTOM_VERSION}"></script>
    <link rel='stylesheet' type='text/css' href='include/javascript/yui/assets/container.css' />     
</head>
EOQ;
$out .= '<body >';

$out2 =<<<EOQ2
<form action="install.php" method="post" name="setConfig" id="form">
<input type='hidden' name='setup_db_drop_tables' id='setup_db_drop_tables' value='false'>
<input type="hidden" id="hidden_goto" name="goto" value="{$mod_strings['LBL_BACK']}" />
<table cellspacing="0" cellpadding="0" border="0" align="center" class="shell">

      <tr><td colspan="2" id="help"><a href="{$help_url}" target='_blank'>{$mod_strings['LBL_HELP']} </a></td></tr>
    <tr>
      <th width="500">
		<p><img src="include/images/sugar_md.png" alt="SugarCRM" border="0"></p>
		{$mod_strings['LBL_STEP']}: {$mod_strings['LBL_DBCONF_TITLE_USER_INFO']}</th>
	<th width="200" height="30" style="text-align: right;"><a href="http://www.sugarcrm.com" target="_blank">
		<IMG src="include/images/sugarcrm_login.png" width="145" height="30" alt="SugarCRM" border="0"></a>        
        </th>
</tr>
<tr>
	<td colspan="2">
		 <p>{$mod_strings['LBL_DBCONF_INSTRUCTIONS']}</p>
		 <div id="errorMsgs" style="display:none"></div>
<div class="required">{$mod_strings['LBL_REQUIRED']}</div>
<table width="100%" cellpadding="0" cellpadding="0" border="0" class="StyleDottedHr">
<tr><th colspan="3" align="left">{$mod_strings['LBL_DBCONF_TITLE_USER_INFO']} </td></tr>
<tr><td colspan="3" align="left">{$mod_strings['LBL_DBCONFIG_B_MSG1']}</td></tr>
<tr>
	<td><span class="required">*</span></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DB_USER']}</b></td>
	<td nowrap align="left">
		 <input type="text" name="setup_db_admin_user_name" maxlength="16" value="{$_SESSION['setup_db_admin_user_name']}" />
	</td>
</tr>
<tr>
	<td></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DB_PASSWORD']}</b></td>
	<td nowrap align="left"><input type="password" name="setup_db_admin_password" value="{$_SESSION['setup_db_admin_password']}" /></td></tr>
EOQ2;

if(isset($_SESSION['install_type'])  && !empty($_SESSION['install_type'])  && strtolower($_SESSION['install_type'])=='custom' && $_SESSION['setup_db_type'] != 'oci8' ){
    $checked = '';
    if(isset($_SESSION['setup_db_provide_own_user']) && $_SESSION['setup_db_provide_own_user']){$checked = 'checked = "checked"';}
$out2 .=<<<EOQ2
<tr><td colspan="3" align="left">For security reasons, we will create a database user that will be used exclusively by the SugarCRM application to interact with the database.  If you wish, you may provide your own user.
<br>Provide your own <input type="checkbox" class="checkbox" name="setup_db_provide_own_user" id="setup_db_provide_own_user" value="yes" onclick="toggleownUser();" {$checked}>
</td></tr>
<tr><td colspan="3" align="left">
<span id='connection_user_div' style="display:none">
    <table>
    <tr><td colspan="3" align="left">{$mod_strings['LBL_DBCONFIG_MSG4']}</td></tr>
    <tr>
        <td><span class="required">*</span></td>
        <td nowrap><b>{$mod_strings['LBL_DBCONF_DB_USER']}</b></td>
        <td nowrap align="left">
         <input type="text" name="setup_db_sugarsales_user" maxlength="16" value="{$_SESSION['setup_db_sugarsales_user']}" />
        </td>
</td></tr>
<tr>
    <td></td>
    <td nowrap><b>{$mod_strings['LBL_DBCONF_DB_PASSWORD']}</b></td>
    <td nowrap align="left"><input type="password" name="setup_db_sugarsales_password" value="{$_SESSION['setup_db_sugarsales_password']}" /></td>
</tr></table>
<span>

EOQ2;
}
$no_select = '';$yes_select ='';$multi_select = '';
if(isset($_SESSION['demoData'])){
    if($_SESSION['demoData']=='yes')    {$yes_select ='selected';}
    if($_SESSION['demoData']=='no')      {$no_select ='selected';}
    if($_SESSION['demoData']=='multi'){$multi_select ='selected';}
}else{
    $no_select = 'selected';
}
    
    
$out3 =<<<EOQ3
<tr><td colspan="3" align="left"><br>Please select whether you wish us to populate the database with demo data.  </td></tr>
<tr>
	<td></td>
	<td nowrap><b>{$mod_strings['LBL_DBCONF_DEMO_DATA']}</b></td>
	<td nowrap align="left">
		<select name='demoData' id='demoData'><option value="no" $no_select>No</option><option value="yes" $yes_select>Yes</option></select>
	</td>
</tr>
EOQ3;
$out4 =<<<EOQ4

</table>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<hr>
	 <input type="hidden" name="current_step" value="{$next_step}">
	 <table cellspacing="0" cellpadding="0" border="0" class="stdTable">
		<tr>
			<td>
				<input class="button" type="button" name="goto" value="{$mod_strings['LBL_BACK']}" onclick="document.getElementById('form').submit();" />
			</td>
			<td>
				<input class="button" type="button" name="goto" id="defaultFocus" value="{$mod_strings['LBL_NEXT']}" onClick="callDBCheck();"/>
			</td>
		</tr>
	 </table>
</td>
</tr>
</table>
</form>
<br>
<script>
function toggleownUser(){
     if(typeof(document.getElementById('setup_db_provide_own_user')) !='undefined'
     && document.getElementById('setup_db_provide_own_user') != null){
        if(document.getElementById('setup_db_provide_own_user').checked == true){
            document.getElementById('connection_user_div').style.display = '';       
        }else{
            document.getElementById('connection_user_div').style.display = 'none';   
        }
    }
}
    toggleownUser();
    
var msgPanel;
function callDBCheck(){

            //begin main function that will be called
            ajaxCall = function(msg_panel){
                //create success function for callback

                getPanel = function() {        
                var args = {    width:"300px", 
                                modal:true,
                                fixedcenter: true,
                                constraintoviewport: false,  
                                underlay:"shadow",  
                                close:false, 
                                draggable:true, 
                                
                                effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:.5}
                               } ; 
                        msg_panel = new YAHOO.widget.Panel('p_msg', args);

                        msg_panel.setHeader("{$mod_strings['LBL_LICENSE_CHKDB_HEADER']}"); 
                        msg_panel.setBody(document.getElementById("checkingDiv").innerHTML);
                        msg_panel.render(document.body);
                        msgPanel = msg_panel;
                } 
                
                
                passed = function(url){
                    document.setConfig.goto.value="{$mod_strings['LBL_NEXT']}";
                    document.getElementById('hidden_goto').value="{$mod_strings['LBL_NEXT']}";
                    document.setConfig.current_step.value="{$next_step}";
                    document.setConfig.submit();
                }
                success = function(o) {
                    
                    //condition for just the preexisting database
                    if (o.responseText.indexOf('preexeest')>=0){

                        //  throw confirmation message
                        msg_panel.setBody(document.getElementById("sysCheckMsg").innerHTML);
                        msg_panel.render(document.body);
                        msgPanel = msg_panel;
                        
                    //condition for no errors
                    }else if (o.responseText.indexOf('dbCheckPassed')>=0){
                        //make navigation
                        passed("install.php?goto={$mod_strings['LBL_NEXT']}");

                    //condition for other errors    
                    }else{
                        //turn off loading message
                        msgPanel.hide();
                        document.getElementById("errorMsgs").innerHTML = o.responseText;
                        document.getElementById("errorMsgs").style.display = '';
                        return false;
                    }

                    
                }//end success
        
                //set loading message and create url

                postData = "checkDBSettings=true&to_pdf=1&sugar_body_only=1";
                postData += "&setup_db_admin_user_name="+document.setConfig.setup_db_admin_user_name.value;
                postData += "&setup_db_admin_password="+document.setConfig.setup_db_admin_password.value;
                if(typeof(document.setConfig.setup_db_sugarsales_password) != 'undefined'){
                    postData += "&setup_db_sugarsales_user="+document.setConfig.setup_db_sugarsales_user.value;
                }
                if(typeof(document.setConfig.setup_db_sugarsales_password) != 'undefined'){
                    postData += "&setup_db_sugarsales_password="+document.setConfig.setup_db_sugarsales_password.value;
                }
                if(typeof(document.setConfig.setup_db_sugarsales_password) != 'undefined'){
                    postData += "&setup_db_provide_own_user="+document.setConfig.setup_db_provide_own_user.value;
                }
                postData += "&demoData="+document.setConfig.demoData.value;
                postData += "&to_pdf=1&sugar_body_only=1";                                                
                 
                //if this is a call already in progress, then just return               
                    if(typeof ajxProgress != 'undefined'){ 
                        return;                            
                    }

                getPanel();
                msgPanel.show;
                var ajxProgress = YAHOO.util.Connect.asyncRequest('POST','install.php', {success: success, failure: success}, postData);                
                        
    
            };//end ajaxCall method
              ajaxCall();
            return;   
}

function confirm_drop_tables(yes_no){

        if(yes_no == true){
            document.getElementById('setup_db_drop_tables').value = true;
           //make navigation
                    document.setConfig.goto.value="{$mod_strings['LBL_NEXT']}";
                    document.getElementById('hidden_goto').value="{$mod_strings['LBL_NEXT']}";
                    document.setConfig.current_step.value="{$next_step}";
                    document.setConfig.submit();
        }else{
            //set drop tables to false
            document.getElementById('setup_db_drop_tables').value = false;
            msgPanel.hide();
        }
}
    
</script>



           <div id="checkingDiv" style="display:none">
           <table cellspacing="0" cellpadding="0" border="0">
               <tr><td>
                    <p><img src='install/processing.gif'> <br>{$mod_strings['LBL_LICENSE_CHKDB_HEADER']}</p>
                </td></tr>
            </table>
            </div>

          <div id='sysCheckMsg' style="display:none">
           <table cellspacing="0" cellpadding="0" border="0" >
               <tr><td>
                    <p>"{$mod_strings['LBL_DROP_DB_CONFIRM']}"</p>
               </td></tr>
               <tr><td align='center'>
                    <input type='button' class='button' onclick='confirm_drop_tables(true)' value="{$mod_strings['LBL_ACCEPT']}">
                    <input type='button' class='button' onclick='confirm_drop_tables(false)' value="{$mod_strings['LBL_CANCEL']}">
                </td></tr>
            </table>
                
          <div>
          
</body>
</html>
EOQ4;

echo $out.$out2;



    echo $out3;



echo $out4;


////	END PAGE OUTPUT
///////////////////////////////////////////////////////////////////////////////

?>
