<?php
$admin_email = settings('contact_page');
$email = explode("#", $admin_email['contact_page']);
if ($url['pid'] == $email[0])
{
    $self = $_SERVER['REQUEST_URI']."#msg";
//spam settings
    $cap = settings('contact_form_use_recaptcha');
    $contact_form_use_recaptcha = 'y'; //$cap['contact_form_use_recaptcha'];
    //send mail
    $errors = 0;
    if (isset($_POST['email']))
    {
        $mainhtml .= '<span id="error">&nbsp;</span>';
        //check for hidden field bot check
        if (!isset($_POST['g-recaptcha-response']))
        {
            $mainhtml .= '<p class="messageError">Sorry, your anti-spam answer is incorrect.</p>';
            $errors++;
        }
        //check if name has http or https
        if (stristr($_POST['name'], 'http'))
        {
            $mainhtml .= '<p class="messageError">Your name contains a link</p>';
            $errors++;
        }
        //check name
        if (empty($_POST['name']) && isset($_POST['name']))
        {
            $mainhtml .= '<p class="messageError">Please enter your name</p>';
            $errors++;
        }
        //check email
        if (empty($_POST['email']))
        {
            $mainhtml .= '<p class="messageError">Please enter your email address</p>';
            $errors++;
        }
        //check for spam
        if (!$form->recaptchaConfirm($_POST['g-recaptcha-response']))
        {
            $mainhtml .= '<p class="messageError">Sorry, your anti-spam answer is incorrect.</p>';
            $errors++;
        }
        if ($errors > 0)
        {
            $mainhtml .= '<script>document.observe(\'dom:loaded\',function(){$(\'error\').scrollTo();});</script>';
        }
        //send email is there are no errors
        if ($errors == 0)
        {
            $to = $email[1];
            $msg = '';
            if (isset($_POST['name']))
            {
                $msg = "Name: ".$_POST['name']."\n";
            }
            if (isset($_POST['email']))
            {
                $msg .= "Email: ".$_POST['email']."\n";
            }
            if (isset($_POST['company']))
            {
                $msg .= "Company: ".$_POST['company']."\n";
            }
            if (isset($_POST['phone']))
            {
                $msg .= "Phone: ".$_POST['phone']."\n";
            }
            if (isset($_POST['address']))
            {
                $msg .= "Address: ".$_POST['address']."\n";
            }
            if (isset($_POST['find']))
            {
                $msg .= "How did you find us: ".$_POST['find']."\n";
            }
            $msg .= "Sender's IP address: ".$_SERVER['REMOTE_ADDR']."\n";
            $msg .= "Message: \n".$_POST['msg']."\n";
            $ref = rand(100, 999).'-'.rand_letters(3);
            emailWithPhpMailer($to, "message from ".$_SERVER['SERVER_NAME'].' ('.$ref.')', $msg, $_POST['email'], false, '', $_POST['name']);
            //send a copy to the sender if set to yes in settings
            $cc = settings('cc_contact_form');
            if ($cc['cc_contact_form'] == 'y')
            {
                emailWithPhpMailer($_POST['email'], $_SERVER['SERVER_NAME'].' ('.$ref.')', $msg, $to, false, '', '', $_POST['name']);
            }
            //show message
            $mainhtml .= '<p class="messageSent" name="thanks" id="thanks">Your message has been sent!</p><script>document.observe(\'dom:loaded\',function(){$(\'thanks\').scrollTo();});</script>';
            unset($_POST);
        }
    }
    //show the form
    $fields = settings('contact_us_fields,contact_us_fields_req');
    $reqFields = explode("|", $fields['contact_us_fields_req']);
    $fields = explode("|", $fields['contact_us_fields']);
    $mainhtml .= '<form name="contactus" id="contactus" method="post" action="?thanks">
	<table border="0" cellspacing="0" cellpadding="0">';
    //name field
    if (in_array('name', $fields))
    {
        $req = (in_array('name', $reqFields)) ? 'required' : '';
        $mainhtml .= '<tr><th>Your name</th><td><input name="name" id="name" type="text" size="32" value="'.$_POST['name'].'" class="'.$req.'" /></td></tr>';
    }
    //email field
    $mainhtml .= '<tr><th>Your email address</th><td><input name="email" id="email" type="text" size="32" value="'.$_POST['email'].'" class="required validate-email" /></td></tr>';
    //company name field
    if (in_array('company', $fields))
    {
        $req = (in_array('company', $reqFields)) ? 'required' : '';
        $mainhtml .= '<tr><th>Company name</th><td><input id="company" name="company" type="text" size="32" value="'.$_POST['company'].'" class="'.$req.'" /></td></tr>';
    }
    //phone field
    if (in_array('phone', $fields))
    {
        $req = (in_array('phone', $reqFields)) ? 'required' : '';
        $mainhtml .= '<tr><th>Phone number</th><td><input name="phone" type="text" id="phone" size="32" value="'.$_POST['phone'].'" class="'.$req.'" /></td></tr>';
    }
    //postal address field
    if (in_array('address', $fields))
    {
        $req = (in_array('address', $reqFields)) ? 'required' : '';
        $mainhtml .= '<tr><th valign="top">Mailing address</th><td><textarea name="address" id="address" cols="25" rows="3" class="'.$req.'">'.$_POST['address'].'</textarea></td></tr>';
    }
    //where did you find us field
    if (in_array('find', $fields))
    {
        $req = (in_array('find', $reqFields)) ? 'required' : '';
        $mainhtml .= '<tr><th valign="top">How did you find us?<small>(e.g. Google/Facebook etc)</small></th><td><input name="find" type="text" id="find" size="32" value="'.$_POST['find'].'" class="'.$req.'" /></td></tr>';
    }
    //message field
    $mainhtml .= '<tr><th valign="top">Message</th><td><textarea name="msg" id="msg" rows="8" class="required">'.$_POST['msg'].'</textarea></td></tr>';
    //spam code
    if ($contact_form_use_recaptcha == 'y')
    {
        $mainhtml .= '<tr><th>&nbsp;</th><td>'.$form->recaptchaShowCaptcha().'</td></tr>';
    }
    //submit button
    $mainhtml .= '<tr><th>&nbsp;</th><td><input type="submit" name="Submit" value="Send Message" class="button" /></td></tr></table></form>';
    //form validation
    $mainhtml .= '<script>new Validation(\'contactus\',{immediate:true});</script>';
    //inject code into the head
    //js form validation
    $head = '';
    if (!strstr($mainhtml, 'blog/src/style.css'))
    {
        $head .= '<link rel="stylesheet" type="text/css" href="/inc/blog/src/style.css" />';
    }
    if (!strstr($mainhtml, 'prototype.js'))
    {
        $head .= '<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>';
    }
    if (!strstr($mainhtml, 'scriptaculous.js'))
    {
        $head .= '<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/scriptaculous/1.8.3/scriptaculous.js?load=effects,builder"></script>';
    }
    if (!strstr($mainhtml, 'validation.js'))
    {
        $head .= '<script type="text/javascript" src="/inc/blog/src/validation.js"></script>';
    }
    if ($contact_form_use_recaptcha == 'y')
    {
        $head .= $form->recaptchaHeaderCode();
    }
    $mainhtml = str_replace("</head>", $head.'</head>', $mainhtml);
    //include this add-ons's overwrite css file from the template being used, if there is one
    $t = settings('template');
    if (file_exists('templates/'.$t['template'].'/addons/contact_us.css'))
    {
        $mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="/templates/'.$t['template'].'/addons/contact_us.css" /></head>', $mainhtml);
    }
    else
    {
        $mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="https://www.freesites.com.au/addons/contact_us.css" /></head>', $mainhtml);
    }
}
