<?php
/**
 * Share form between add and edit guestbook. 
 * 
 * This application is designed for website generated by RVSiteBuilder.
 * Unauthorized copy of the script is prohibited.
 * 
 * @version 1.00
 * @author pairote@rvglobalsoft.com
 * @copyright Copyright &copy; http://www.rvglobalsoft.com/index.php/rvsitebuilder/sitebuilderbilling/action/terms
 * @package rvsitebuilder
 */
 
function createElements(&$form, &$aConfig, $aLang)
{
    if ($aConfig['field_name']) {
        $form->addElement('text', 'field_name', $aLang['Name'], array('size' => 20, 'maxlength' => 255));
        $form->applyFilter('field_name', 'trim');
        $form->addRule('field_name', $aLang['name warning'], 'required', null, 'client');    
    }
    if ($aConfig['field_email']) {
        $form->addElement('text', 'field_email', $aLang['Email'], array('size' => 20, 'maxlength' => 255));
        $form->addRule('field_email', $aLang['email warning invalid'], 'email', null, 'client');
        if ($aConfig['field_email_required']) {
            $form->addRule('field_email', $aLang['email warning'], 'required', null, 'client');
        }    
    }
    if ($aConfig['field_location']) {
        $form->addElement('text', 'field_location', $aLang['Location'], array('size' => 20, 'maxlength' => 255));
        $form->applyFilter('field_location', 'trim');
        if ($aConfig['field_location_required']) {
            $form->addRule('field_location', $aLang['location warning'], 'required', null, 'client');
        }         
    }
    if ($aConfig['field_website']) {
        $form->addElement('text', 'field_website', $aLang['Website'] , array('size' => 20, 'maxlength' => 255));
        $form->applyFilter('field_website', 'trim');        
        if ($aConfig['field_website_required']) {
            $form->addRule('field_website', $aLang['website warning'], 'required', null, 'client');
        }          
    }
    if ($aConfig['field_icq']) {
        $form->addElement('text', 'field_icq', $aLang['icq'], array('size' => 20, 'maxlength' => 16));
        $form->addRule('field_icq', 'Please enter valid icq number', 'numeric', null, 'client');    
        $form->applyFilter('field_icq', 'trim');
        if ($aConfig['field_icq_required']) {
            $form->addRule('field_icq', $aLang['icq warning'], 'required', null, 'client');
        }                
    }
    if ($aConfig['field_aim']) {
        $form->addElement('text', 'field_aim', $aLang['aim'], array('size' => 20, 'maxlength' => 128));
        $form->applyFilter('field_aim', 'trim');      
        if ($aConfig['field_aim_required']) {
            $form->addRule('field_aim', $aLang['aim warning'], 'required', null, 'client');
        }                 
    }
    if ($aConfig['field_yahoo']) {
        $form->addElement('text', 'field_yahoo', $aLang['yahoo'], array('size' => 20, 'maxlength' => 128));
        $form->applyFilter('field_yahoo', 'trim');      
        if ($aConfig['field_yahoo_required']) {
            $form->addRule('field_yahoo', $aLang['yahoo warning'], 'required', null, 'client');
        }                  
    }    
    if ($aConfig['field_skype']) {
        $form->addElement('text', 'field_skype', $aLang['skype'], array('size' => 20, 'maxlength' => 128));
        $form->applyFilter('field_skype', 'trim');      
        if ($aConfig['field_skype_required']) {
            $form->addRule('field_skype', $aLang['skype warning'], 'required', null, 'client');
        }                  
    }
    if ($aConfig['field_msn']) {
        $form->addElement('text', 'field_msn', $aLang['msn'], array('size' => 20, 'maxlength' => 128));
        $form->applyFilter('field_msn', 'trim');   
        $form->addRule('field_msn', $aLang['msn warning invalid'], 'email', null, 'client');        
        if ($aConfig['field_msn_required']) {
            $form->addRule('field_msn', $aLang['msn warning'], 'required', null, 'client');
        }    
    }
    if ($aConfig['field_optional1']) {
        $form->addElement('text', 'field_optional1',  $aConfig['field_optional1_text'], array('size' => 50));
        $form->applyFilter('field_option1', 'trim');     
        if ($aConfig['field_optional1_required']) {
            $form->addRule('field_optional1', $aLang['Pre optional warning'] . ' ' . strtolower($aConfig['field_optional1_text']) . $aLang['Post optional warning'], 'required', null, 'client');
        }                   
    }
    if ($aConfig['field_optional2']) {
        $form->addElement('text', 'field_optional2',  $aConfig['field_optional2_text'], array('size' => 50));
        $form->applyFilter('field_option2', 'trim');    
        if ($aConfig['field_optional2_required']) {
            $form->addRule('field_optional2', $aLang['Pre optional warning'] . ' ' . strtolower($aConfig['field_optional2_text']) . $aLang['Post optional warning'], 'required', null, 'client');
        }           
    }
    if ($aConfig['field_optional3']) {
        $form->addElement('text', 'field_optional3',  $aConfig['field_optional3_text'], array('size' => 50));
        $form->applyFilter('field_option3', 'trim');    
        if ($aConfig['field_optional3_required']) {
            $form->addRule('field_optional3', $aLang['Pre optional warning'] . ' ' . strtolower($aConfig['field_optional3_text']) . $aLang['Post optional warning'], 'required', null, 'client');
        }           
    }
    if ($aConfig['field_optional4']) {
        $form->addElement('text', 'field_optional4',  $aConfig['field_optional4_text'], array('size' => 50));
        $form->applyFilter('field_option4', 'trim');    
        if ($aConfig['field_optional4_required']) {
            $form->addRule('field_optional4', $aLang['Pre optional warning'] . ' ' . strtolower($aConfig['field_optional4_text']) . $aLang['Post optional warning'], 'required', null, 'client');
        }           
    }
    if ($aConfig['field_optional5']) {
        $form->addElement('text', 'field_optional5',  $aConfig['field_optional5_text'], array('size' => 50));
        $form->applyFilter('field_option5', 'trim');    
        if ($aConfig['field_optional5_required']) {
            $form->addRule('field_optional5', $aLang['Pre optional warning'] . ' ' . strtolower($aConfig['field_optional5_text']) . $aLang['Post optional warning'], 'required', null, 'client');
        }           
    }
    
    $form->addElement('textarea', 'field_msg', $aLang['msg'], array('rows' => 7, 'cols' => '50', 'wrap' => 'virtual'));
    $form->addRule('field_msg', $aLang['msg warning'], 'required', null, 'client');
    $form->addRule('field_msg', $aLang['msg warning min'], 'minlength', $aConfig['min_word_per_message'], 'client');
    $form->addRule('field_msg', $aLang['msg warning max'], 'maxlength', $aConfig['max_word_per_message'], 'client');                          
    $form->applyFilter('field_msg', 'trim');
    
    $captChaImgTag = '';
    if ($aConfig['captcha']) {
        $captChaImgTag = genCaptcha();    
        if ($captChaImgTag) {
            $form->addElement('text', 'captcha', $aLang['captcha info'] . '<br>' . $captChaImgTag, array('size' => 8, 'maxlength' => 8));
            $form->addRule('captcha', $aLang['captcha warning'], 'required', null, 'client');
        }
        else {
            $aConfig['captcha'] = 0;
        }
    }
    
    
    if ($aConfig['field_ip']) {
        $form->addElement('hidden', 'field_ip', getIp());   
    }    
    $form->addElement('submit', null, 'Submit');    
}

function rendererFormTable(&$renderer, $registeredOption, $aConfig, $aLang, $aError)
{        
        $count=0;
        $data ='';
        foreach ($registeredOption['elements'] as $element => $aValue) {
            if ($aValue['type'] == 'submit' || $aValue['type'] == 'hidden' || $aValue['type'] == 'password') {
                continue;
            }
            
            $tabId = $count%2 + 1; 
            $denote = '';
            if ($aValue['required']) {
                $denote = '<span style="color: rgb(255, 0, 0);">*</span>';
            }
            
            $err = '';
            if(isset($aError[$aValue['name']])) {
                $err = '<span style="color: rgb(255, 0, 0);">' . $aError[$aValue['name']] . '</span><br>';
            }    
                                          
            
            $data .= '<tr class="bgcolortab' . $tabId . '">';
            if ($aValue['name'] == 'captcha') {
                $data .= '<td width="20%" align="left"><img src="' . SCRIPT_URL . $aConfig['theme'] . '/images/' . $aValue['name'] . '.gif" alt="" /> ';
                $data .= $aLang['captcha'] . $denote . ':</td>';
                $data .= '<td width="80%" align="left">' . $err . $renderer->elementToHtml($aValue['name']) . '<br>' . $aValue['label']  . '</td>';
            }
            else if ($aValue['name'] == 'field_msg') {
                $data .= '<td width="20%" align="left"><img src="' . SCRIPT_URL . $aConfig['theme'] . '/images/' . $aValue['name'] . '.gif" alt="" /> ' . $aValue['label'];
                $data .= $denote . ':';
                if(isset($aConfig['smileys']) && $aConfig['smileys']) {
                 $data .= getSmile();
                }
                $data .= '</td>';
                $data .= '<td width="80%" align="left">' . $err . $renderer->elementToHtml($aValue['name']) . '</td>';                  
            }
            else if ($aValue['name'] == 'field_optional1' || $aValue['name'] == 'field_optional2' || $aValue['name'] == 'field_optional3' || $aValue['name'] == 'field_optional4' || $aValue['name'] == 'field_optional5') {
                $data .= '<td width="20%" align="left"><img src="' . SCRIPT_URL . $aConfig['theme'] . '/images/field_optional.gif" alt="" /> ' . $aValue['label'];
                $data .= $denote . ':</td>';
                $data .= '<td width="80%" align="left">' . $err . $renderer->elementToHtml($aValue['name']) . '</td>';              
            }            
            else {
                $data .= '<td width="20%" align="left"><img src="' . SCRIPT_URL . $aConfig['theme'] . '/images/' . $aValue['name'] . '.gif" alt="" /> ' . $aValue['label'];
                $data .= $denote . ':</td>';
                $data .= '<td width="80%" align="left">' . $err . $renderer->elementToHtml($aValue['name']) . '</td>';              
            }
            $data .= '</tr>';        
            $count++;
        }       
        
        $data .= "                </table>\n" . 
                "               </td>\n" . 
                "            </tr>          \n" . 
                "            <tr>\n" . 
                "                <td colspan=\"2\" align=\"left\"><span style=\"font-size: 80%; color: rgb(255, 0, 0);\">*</span><span style=\"font-size: 80%;\"> " . $aLang['denotes'] . "</td>\n" . 
                "            </tr>\n" . 
                "        </table>\n" . 
                "        </td>\n" . 
                "    </tr>\n" . 
                "</table></div>";
    return $data;
}

function getSmile() 
{
    include(SCRIPT_PATH . 'smilies.php');
    $table = '<!-- JavaScript is originate from Advanced Guestbook http://proxy2.de -->';
    $table .= "<script language=\"JavaScript\" type=\"text/Javascript\">\n" . 
            "<!--\n" . 
            "function smiley(text) {\n" . 
            "  text = ' ' + text + ' ';\n" . 
            "  if (document.guestForm.field_msg.createTextRange && document.guestForm.field_msg.caretPos) {\n" . 
            "   var caretPos = document.guestForm.field_msg.caretPos;\n" . 
            "   caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;\n" . 
            "   document.guestForm.field_msg.focus();\n" . 
            "  } else {\n" . 
            "    document.guestForm.field_msg.value  += text;\n" . 
            "   document.guestForm.field_msg.focus();\n" . 
            "  }\n" . 
            "}" .
            "//-->\n" . 
            "</script>";
            
    $table .= '<table align="center" border="0" cellpadding="2" cellspacing="0">'; 
    $table .= '<tr align="center" valign="middle">';    
    $count= 0;
    foreach ($aSmiley as $ascii => $img) {
        $id = $count%4;
        if ($id == 0) {
            $table .=  '</tr>';
            $table .=  '<tr align="center" valign="middle">';
        }
        
        $table .=  '<td><a href="javascript:smiley(\'' . addslashes($ascii) . '\')">';
        $table .= '<img src="' . SCRIPT_URL . 'smilies/' . $img . '" ';
        $table .=  'alt="' . addslashes($ascii) . '" title="' .  str_replace('.gif', '' , $img).  '" border="0" />';
        $table .=  '</a></td>';
 
        $count++;
    }
    $table .=  '</tr>';
    $table .=  '</table>';
    
    return $table;
}

function getBbCode()
{

}
?>