<?php

namespace OfficeBrain\Bundle\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\CssSelector\XPath\Translator;
use Symfony\Component\Validator\ExecutionContextInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Doctrine\ORM\EntityRepository;


class UserType extends AbstractType
{
    protected $coreManger;
    protected $formFields;

    /**
     *
     * @param ContainerInterface $container
     * @param $mode
     */
    public function __construct($coreManger, $formFields=array(), $formType='')
    {
    	$this->coreManger= $coreManger;
        $this->formFields = $formFields['fields'];
        $this->formvalidation = $formFields['validations'];
        $this->formtype = $formType;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $master = $this->coreManger;
        $gender = array('male' => 'user.gender.male', 'female' => 'user.gender.female');

       /*  $countryConfiguration=array();
        foreach ($this->container->getParameter('country') as $key=>$value) {
            $countryConfiguration[]=$value['short_name'];
        } */
        $country=$master->getCountryData();

        if($this->formtype == 'admin_add' || $this->formtype == 'main_register' || $this->formtype == 'admin_edit' || $this->formtype == 'main_edit' ){
        	$countryIdData = $builder->getData()->getCountryId();
        	$stateIdData = $builder->getData()->getStateId();
        	$isIndividualData = $builder->getData()->getIsIndividual();
        }else{
        	$countryIdData = $builder->getData()['countryId'];
        	$stateIdData = $builder->getData()['stateId'];
        	$isIndividualData = $builder->getData()['isIndividual'];
        }


        if ($countryIdData != "") {
            $state = $master->getStateData($countryIdData);
        } else {
            $state = array();
        }
        if ($stateIdData != "") {
            $city = $master->getCityData($stateIdData, $countryIdData);
        } else {
            $city = array();
        }

        $constraints = array(
                'username' => array(
                        new Assert\Length(array('min'=>5, 'minMessage'=>'user.username.min')),
                        new Assert\Regex(array('pattern'=>'/^([a-zA-Z0-9_]{5,25})$/', 'message'=>'user.username.invalid')),
                ),
                'email' => array(
                        //new Assert\Email(array('message' => 'user.email.invalid')),
                		new Assert\Regex(array('pattern'=>'/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/', 'message'=>'user.email.invalid')),
                ),
        		'password' => array(
        				new Assert\Length(array('min'=>8, 'minMessage'=>'user.password.min','max'=>25, 'maxMessage'=>'user.password.max')),
                        new Assert\Regex(array('pattern'=>'/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*\W)(?!.* ).{8,25}$/', 'message'=>'user.password.invalid')),

                ),
                'gender' => array(),
                'firstName' => array(),
                'middleName' => array(),
                'lastName' => array(),
                'userCompany' => array(),
                'street1' => array(),
                'street2' => array(),
                'countryId' => array(),
        		'countryName' => array(),
                'stateId' => array(),
        		'stateName' => array(),
                'cityId' => array(),
        		'cityName' => array(),
                'zipcode' => array(
                        // new Assert\Regex(array('pattern' => '/^([a-zA-Z0-9]{5})$/', 'message' => 'user.zipcode.invalid')),
                ),
                'phone' => array(new Assert\Length(array('min' => 1,'max' => 999, 'exactMessage' => 'user.phone.invalid'))),
                'mobile' => array(new Assert\Length(array('min' => 12,'max' => 12, 'exactMessage' => 'user.mobile.invalid'))),
                'fax' => array(new Assert\Length(array('min' => 12,'max' => 12, 'exactMessage' => 'user.fax.invalid')),),
                'website' => array(
                        new Assert\Regex(array('pattern' => "/^(https?:\/\/|http?:\/\/|HTTPS?:\/\/|HTTP?:\/\/)?([\da-zA-Z\.-]+)\.([a-zA-Z\.]{2,6})([\/\w \.-]*)*\/?$/", 'message' => 'user.website.invalid')),
                        //new Assert\Url(array('message' => 'user.website.invalid')),
                ),
                'locatedDistributorId' => array(),
                'membershipNumber1' => array(),
                'membershipNumber2' => array(),
                'membershipNumber3' => array(),
                'membershipNumber4' => array(),
                'membershipNumber5' => array(),
                'membershipNumber6' => array(),
                'membershipNumber7' => array(),
                'obCustomerId' => array(),
                'jobTitle' => array(),
                'isCompany' => array(),
                'isIndividual' => array(),
                'companyNumber' => array(),
                'companyName' => array(),
                'newslettersUnsubscription' => array(),
        		'photo'=>array(),
        		'extension'=>array(),
        		'contactInfoType'=>array(),
        );

        if (in_array('username', $this->formvalidation)) {
            $constraints['username'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.username.not_blank')),
            ), $constraints['username']);
        }
        if (in_array('email', $this->formvalidation)) {
            $constraints['email'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.email.not_blank')),
            ), $constraints['email']);
        }
        if (in_array('password', $this->formvalidation)) {
            $constraints['password'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.password.not_blank')),
            ), $constraints['password']);
        }
        if (in_array('gender', $this->formvalidation)) {
            $constraints['gender'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.gender.not_blank')),
            ), $constraints['gender']);
        }
        if (in_array('firstName', $this->formvalidation)) {
            $constraints['firstName'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.firstName.not_blank')),
            ), $constraints['firstName']);
        }
        if (in_array('middleName', $this->formvalidation)) {
            $constraints['middleName'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.middleName.not_blank')),
            ), $constraints['middleName']);
        }
        if (in_array('lastName', $this->formvalidation)) {
            $constraints['lastName'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.lastName.not_blank')),
            ), $constraints['lastName']);
        }
        if (in_array('street1', $this->formvalidation)) {
            $constraints['street1'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.street1.not_blank')),
            ), $constraints['street1']);
        }
        if (in_array('street2', $this->formvalidation)) {
            $constraints['street2'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.street2.not_blank')),
            ), $constraints['street2']);
        }
        if (in_array('countryId', $this->formvalidation)) {
            $constraints['countryId'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.countryId.not_blank')),
            ), $constraints['countryId']);
        }
        if (in_array('countryName', $this->formvalidation)) {
        	$constraints['countryName'] = array_merge(array(
        			new Assert\NotBlank(array('message' => 'user.countryName.not_blank')),
        	), $constraints['countryName']);
        }
        if (in_array('stateId', $this->formvalidation)) {
            $constraints['stateId'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.stateId.not_blank')),
            ), $constraints['stateId']);
        }
        if (in_array('stateName', $this->formvalidation)) {
        	$constraints['stateName'] = array_merge(array(
        			new Assert\NotBlank(array('message' => 'user.stateName.not_blank')),
        	), $constraints['stateName']);
        }
        if (in_array('cityId', $this->formvalidation)) {
            $constraints['cityId'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.cityId.not_blank')),
            ), $constraints['cityId']);
        }
        if (in_array('cityName', $this->formvalidation)) {
        	$constraints['cityName'] = array_merge(array(
        			new Assert\NotBlank(array('message' => 'user.cityName.not_blank')),
        	), $constraints['cityName']);
        }
        if (in_array('zipcode', $this->formvalidation)) {
            $constraints['zipcode'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.zipcode.not_blank')),
            ), $constraints['zipcode']);
        }
        if (in_array('phone', $this->formvalidation)) {
            $constraints['phone'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.phone.not_blank')),
            ), $constraints['phone']);
        }
        if (in_array('mobile', $this->formvalidation)) {
            $constraints['mobile'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.mobile.not_blank')),
            ), $constraints['mobile']);
        }
        if (in_array('fax', $this->formvalidation)) {
            $constraints['fax'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.fax.not_blank')),
            ), $constraints['fax']);
        }
        if (in_array('website', $this->formvalidation)) {
            $constraints['website'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.website.not_blank')),
            ), $constraints['website']);
        }
        if (in_array('locatedDistributorId', $this->formvalidation)) {
            $constraints['locatedDistributorId'] = array_merge(array(
                new Assert\NotBlank(array('message' => 'user.locatedDistributorId.not_blank')),
            ), $constraints['locatedDistributorId']);
        }
        if (in_array('membershipNumber1', $this->formvalidation)) {
            $constraints['membershipNumber1'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.membershipNumber1.not_blank')),
            ), $constraints['membershipNumber1']);
        }
        if (in_array('membershipNumber2', $this->formvalidation)) {
            $constraints['membershipNumber2'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.membershipNumber2.not_blank')),
            ), $constraints['membershipNumber2']);
        }
        if (in_array('membershipNumber3', $this->formvalidation)) {
            $constraints['membershipNumber3'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.membershipNumber3.not_blank')),
            ), $constraints['membershipNumber3']);
        }
        if (in_array('membershipNumber4', $this->formvalidation)) {
            $constraints['membershipNumber4'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.membershipNumber4.not_blank')),
            ), $constraints['membershipNumber4']);
        }
        if (in_array('membershipNumber5', $this->formvalidation)) {
            $constraints['membershipNumber5'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.membershipNumber5.not_blank')),
            ), $constraints['membershipNumber5']);
        }
        if (in_array('membershipNumber6', $this->formvalidation)) {
            $constraints['membershipNumber6'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.membershipNumber6.not_blank')),
            ), $constraints['membershipNumber6']);
        }

        /*
         * start Validation membershipNumber
         * */
        /* if($builder->getData()->getMembershipNumber1() ==""
        		&& $builder->getData()->getMembershipNumber2()==""
        		&& $builder->getData()->getMembershipNumber3()==""
        		&& $builder->getData()->getMembershipNumber4()==""
        		&& $builder->getData()->getMembershipNumber5()==""
        		&& $builder->getData()->getMembershipNumber6()==""
        		)
        {
        	$constraints['membershipNumber1'] = array_merge(array(
        			new Assert\NotBlank(array('message' => 'user.membershipNumberValidation.not_blank')),
        	), $constraints['membershipNumber1']);
        } */
 /*
  * End Validation
  * */
        if (in_array('obCustomerId', $this->formvalidation)) {
            $constraints['obCustomerId'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.obCustomerId.not_blank')),
            ), $constraints['obCustomerId']);
        }
        if (in_array('jobTitle', $this->formvalidation)) {
            $constraints['jobTitle'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.jobTitle.not_blank')),
            ), $constraints['jobTitle']);
        }
        if (in_array('isCompany', $this->formvalidation)) {
            $constraints['isCompany'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.isCompany.not_blank')),
            ), $constraints['isCompany']);
        }
        if (in_array('isIndividual', $this->formvalidation)) {
            $constraints['isIndividual'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.isIndividual.not_blank')),
            ), $constraints['isIndividual']);
        }

        if (in_array('userCompany', $this->formvalidation) ||
                (isset($this->formFields['userCompany']) && $this->formFields['userCompany']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit'))  ) {
            $constraints['userCompany'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.userCompany.not_blank')),
            ), $constraints['userCompany']);
        }

         #if (isset($this->formFields['isCompany']) && isset($_POST['is_company']) && $_POST['is_company'] == 'ob_user_isCompany' ) {
         if (in_array('companyNumber', $this->formvalidation)) {
            $constraints['companyNumber'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.companyNumber.not_blank')),
            ), $constraints['companyNumber']);
         }

        if (in_array('companyName', $this->formvalidation)) {
            $constraints['companyName'] = array_merge(array(
                    new Assert\NotBlank(array('message' => 'user.companyName.not_blank')),
            ), $constraints['companyName']);
        }


        if (isset($this->formFields['username']) &&
                (($this->formFields['username']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['username']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['username']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('loginUsername', 'text', array('constraints' => $constraints['username']));
        }

        if (isset($this->formFields['email']) &&
                (($this->formFields['email']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['email']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['email']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('email', 'text', array('constraints' => $constraints['email']));
        }

        if (isset($this->formFields['password']) &&
                (($this->formFields['password']->main==1 && ($this->formtype == 'main_register')) ||
                        ($this->formFields['password']->admin==1 && ($this->formtype == 'admin_add')) ||
                            ($this->formFields['password']->admin==1 && ($this->formtype == 'emp_add'))
                )) {
            $builder->add('password', 'repeated', array(
                        'type' => 'password',
                        'constraints'=>$constraints['password'],
                        'invalid_message' => 'user.password.mismatch'
                        )
                );
        }

        if (isset($this->formFields['gender']) &&
                (($this->formFields['gender']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['gender']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['gender']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('gender', 'choice', array(
                    'choices' => $gender,
                    'constraints' => $constraints['gender']
            ));
        }

        if (isset($this->formFields['firstName']) &&
                (($this->formFields['firstName']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['firstName']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['firstName']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('firstName', 'text', array('constraints' => $constraints['firstName']));
        }

        if (isset($this->formFields['middleName']) &&
                (($this->formFields['middleName']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['middleName']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['middleName']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('middleName', 'text', array('constraints' => $constraints['middleName']));
        }

        if (isset($this->formFields['lastName']) &&
                (($this->formFields['lastName']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['lastName']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['lastName']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('lastName', 'text', array('constraints' => $constraints['lastName']));
        }

        if (isset($this->formFields['street1']) &&
                (($this->formFields['street1']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['street1']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['street1']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('street1', 'text', array('constraints' => $constraints['street1']));
        }

        if (isset($this->formFields['street2']) &&
                (($this->formFields['street2']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['street2']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['street2']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('street2', 'text', array('constraints' => $constraints['street2']));
        }

        if (isset($this->formFields['countryId']) &&
                (($this->formFields['countryId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['countryId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['countryId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('countryId', 'choice', array(
                        'choices' => $country,
                        'constraints' => $constraints['countryId']
                ));
        }

        if (isset($this->formFields['countryName']) &&
        		(($this->formFields['countryName']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
        				($this->formFields['countryName']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
        				($this->formFields['countryName']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
        				)) {
        					$builder->add('countryName', 'text', array('constraints' => $constraints['countryName']));
        }

        if (isset($this->formFields['stateId']) &&
                (($this->formFields['stateId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['stateId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['stateId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('stateId', 'choice', array(
                        'choices' => $state,
                        'constraints' => $constraints['stateId']
                ));
        }

        if (isset($this->formFields['stateName']) &&
        		(($this->formFields['stateName']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
        				($this->formFields['stateName']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
        				($this->formFields['stateName']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
        				)) {
        					$builder->add('stateName', 'text', array('constraints' => $constraints['stateName']));
        }

        if (isset($this->formFields['cityId']) &&
                (($this->formFields['cityId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['cityId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['cityId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('cityId', 'choice', array(
                        'choices' => $city,
                        'constraints' => $constraints['cityId']
                ));
        }

        if (isset($this->formFields['cityName']) &&
        		(($this->formFields['cityName']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
        				($this->formFields['cityName']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
        				($this->formFields['cityName']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
        				)) {
        					$builder->add('cityName', 'text', array('constraints' => $constraints['cityName']));
        }

        if (isset($this->formFields['zipcode']) &&
                (($this->formFields['zipcode']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['zipcode']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['zipcode']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('zipcode', 'text', array('constraints' => $constraints['zipcode']));
        }

        if (isset($this->formFields['phone']) &&
                (($this->formFields['phone']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['phone']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['phone']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('phone', 'text', array('constraints' => $constraints['phone']));
        }

        if (isset($this->formFields['mobile']) &&
                (($this->formFields['mobile']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['mobile']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['mobile']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('mobile', 'text', array('constraints' => $constraints['mobile']));
        }

        if (isset($this->formFields['fax']) &&
                (($this->formFields['fax']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['fax']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['fax']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('fax', 'text', array('constraints' => $constraints['fax']));
        }

        if (isset($this->formFields['website']) &&
                (($this->formFields['website']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['website']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['website']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('website', 'text', array('constraints' => $constraints['website']));
        }

        if (isset($this->formFields['locatedDistributorId']) &&
                (($this->formFields['locatedDistributorId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['locatedDistributorId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['locatedDistributorId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('locatedDistributorId', 'hidden', array('constraints' => $constraints['locatedDistributorId'], 'attr' => array('class' => 'js-locate-distributor-value')));
        }

        if (isset($this->formFields['membershipNumber1']) &&
                (($this->formFields['membershipNumber1']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['membershipNumber1']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['membershipNumber1']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('membershipNumber1', 'hidden', array('constraints' => $constraints['membershipNumber1']));
        }

        if (isset($this->formFields['membershipNumber2']) &&
                (($this->formFields['membershipNumber2']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['membershipNumber2']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['membershipNumber2']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('membershipNumber2', 'hidden', array('constraints' => $constraints['membershipNumber2']));
        }

        if (isset($this->formFields['membershipNumber3']) &&
                (($this->formFields['membershipNumber3']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['membershipNumber3']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['membershipNumber3']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('membershipNumber3', 'hidden', array('constraints' => $constraints['membershipNumber3']));
        }

        if (isset($this->formFields['membershipNumber4']) &&
                (($this->formFields['membershipNumber4']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['membershipNumber4']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['membershipNumber4']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('membershipNumber4', 'hidden', array('constraints' => $constraints['membershipNumber4']));
        }

        if (isset($this->formFields['membershipNumber5']) &&
                (($this->formFields['membershipNumber5']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['membershipNumber5']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['membershipNumber5']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('membershipNumber5', 'hidden', array('constraints' => $constraints['membershipNumber5']));
        }

        if (isset($this->formFields['membershipNumber6']) &&
                (($this->formFields['membershipNumber6']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['membershipNumber6']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['membershipNumber6']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('membershipNumber6', 'hidden', array('constraints' => $constraints['membershipNumber6']));
        }
        if (isset($this->formFields['membershipNumber7']) &&
        		(($this->formFields['membershipNumber7']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
        				($this->formFields['membershipNumber7']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
        				($this->formFields['membershipNumber7']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
        				)) {
        					$builder->add('membershipNumber7', 'hidden', array('constraints' => $constraints['membershipNumber7']));
        				}

        if (isset($this->formFields['obCustomerId']) &&
                (($this->formFields['obCustomerId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['obCustomerId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                        ($this->formFields['obCustomerId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('obCustomerId', 'text', array('constraints' => $constraints['obCustomerId']));
        }

        if (isset($this->formFields['jobTitle']) &&
                (($this->formFields['jobTitle']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['jobTitle']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['jobTitle']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('jobTitle', 'text', array('constraints' => $constraints['jobTitle']));
        }

        if (isset($this->formFields['isCompany']) &&
                (($this->formFields['isCompany']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['isCompany']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['isCompany']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            if(($this->formtype == 'main_register' || $this->formtype == 'main_edit') && $this->formFields['isIndividual']->main!=1)
                $defaultValue = "1";
            else
                $defaultValue = (isset($_POST['is_company']) && $_POST['is_company'] == 'ob_user_isCompany' ?"1":"0");
            $builder->add('isCompany', 'hidden', array('constraints' => $constraints['isCompany'], 'attr' => array('class' => 'js-company','value' => $defaultValue),
                'data'=>$defaultValue));
        }

        if (isset($this->formFields['isIndividual']) &&
                (($this->formFields['isIndividual']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                    ($this->formFields['isIndividual']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['isIndividual']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $defaultValue = (!isset($_POST['is_company']) || $_POST['is_company'] != 'ob_user_isCompany' ?"0":"1");
            $builder->add('isIndividual', 'hidden', array('constraints' => $constraints['isIndividual'], 'attr' => array('class' => 'js-company', 'value' => $defaultValue)));
        }

        if (isset($this->formFields['companyNumber']) &&
                (($this->formFields['companyNumber']->main==1 && ($this->formtype == 'main_register'))
                )) {
            $builder->add('companyNumber', 'text', array('constraints' => $constraints['companyNumber']));
        }

        if (isset($this->formFields['companyName']) &&
                (($this->formFields['companyName']->main==1 && ($this->formtype == 'main_register'  || $this->formtype == 'main_edit')) ||
                ($this->formFields['companyName']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit'))
                )) {
            $builder->add('companyName', 'text', array('constraints' => $constraints['companyName']));
        }

        if (isset($this->formFields['userCompany']) &&
                (($this->formFields['userCompany']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                    ($this->formFields['userCompany']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                ) && $isIndividualData != 1) {    //  $builder->add('userCompany', 'text',array('constraints' => $constraints['userCompany']));
               // echo "htehtreh====>";die;

        }

        $builder->add('userCompany', 'entity', array(
        		'class' => 'OfficeBrainCompanyBundle:Company',
        		'query_builder' => function (EntityRepository $er) {
        		return $er->createQueryBuilder('c')
        		->Where("c.companyName!=''")
        		->andWhere("c.status = 1")
        		->andWhere("c.deletedAt is NULL")
        		->groupBy("c.companyName")
        		->orderBy('c.companyName', 'ASC');
        		},
        		'property' => 'companyName',
        		'empty_value' => false,
        		'empty_data'  => 1
        		));





        if (isset($this->formFields['newslettersUnsubscription']) &&
                (($this->formFields['newslettersUnsubscription']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                        ($this->formFields['newslettersUnsubscription']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                            ($this->formFields['newslettersUnsubscription']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                )) {
            $builder->add('newslettersUnsubscription', 'checkbox', array('constraints' => $constraints['newslettersUnsubscription'],'data' => true));
        }

        if (isset($this->formFields['photo']) && ($this->formFields['photo']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))) {
        	$builder->add('photo', 'file', array('data_class' => null,'constraints' => $constraints['photo']));
        }

        if (isset($this->formFields['extension']) && ($this->formFields['extension']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))) {
        	$builder->add('extension', 'text', array('constraints' => $constraints['extension']));
        }

        $choices = array('0'=>'Business', '1'=>'Residential');
        $builder->add('contactInfoType', 'choice', array(
        		'data' => 0,
        		'multiple' => false,
        		'expanded' => true,
        		'choices' => $choices,

        ));

        $formModifier1 = function (FormInterface $form, $state, $constraints) {
            $form->add('stateId', 'choice', array(
                    'choices' => $state,
                    'constraints' => $constraints['stateId']
            ));
        };

        $formModifier2 = function (FormInterface $form, $city, $constraints) {
            $form->add('cityId', 'choice', array(
                    'choices' => $city,
                    'constraints' => $constraints['cityId']
            ));
        };


        $builder->add('erpId', 'hidden')
                ->add('status', 'hidden');

        if (isset($this->formFields['captcha']) &&
                (($this->formFields['captcha']->main==1 && ($this->formtype == 'main_register'))
        )) {
            $builder->add('captcha', 'captcha', array(
                                            'required' => false,
                                            'width' => 220,
                                            'height' => 50,
                                            'length' => 8,
                                            'keep_value' => true,
                                            'as_url' => true,
                                            'reload' => true,
                                            'invalid_message' => 'user.captcha.invalid'));
        }

/*         $builder->add('recaptcha', 'pv_recaptcha', array(
            'label' => 'Recaptcha',
            'mapped' => false,
        ));
 */
        $builder->addEventListener(
                    FormEvents::PRE_SET_DATA,
                    function (FormEvent $event) use ($formModifier1, $formModifier2, $master, $constraints) {
                        // this would be your entity, i.e. SportMeetup
                        $data = $event->getData();
                        $country = $data->getCountryId()? $data->getCountryId():'';
                        $state1 = $data->getStateId()? $data->getStateId():'';
                        if ($country!='' && isset($this->formFields['stateId']) &&
                                (($this->formFields['stateId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                                        ($this->formFields['stateId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                                        ($this->formFields['stateId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                                )) {
                            $state=$master->getStateData($country);
                            $formModifier1($event->getForm(), $state, $constraints);
                        }
                        if ($state1!='' && isset($this->formFields['cityId']) &&
                                (($this->formFields['cityId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                                        ($this->formFields['cityId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                                        ($this->formFields['cityId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                                )) {
                            $city=$master->getCityData($state1, $country);
                            $formModifier2($event->getForm(), $city, $constraints);
                        }
                    }
            );



        $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($formModifier1, $formModifier2, $master, $constraints) {
            $user = $event->getData();
            $form = $event->getForm();


            if ($form->has('countryId') && isset($user['countryId'])  && isset($this->formFields['stateId']) &&
                    (($this->formFields['stateId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                            ($this->formFields['stateId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                            ($this->formFields['stateId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                    )) {
                $stateDataArray=$master->getStateData($user['countryId']);

                $formModifier1($form, $stateDataArray, $constraints);
            }

            if ($form->has('stateId') && isset($user['stateId']) && isset($this->formFields['cityId']) &&
                    (($this->formFields['cityId']->main==1 && ($this->formtype == 'main_register' || $this->formtype == 'main_edit')) ||
                            ($this->formFields['cityId']->admin==1 && ($this->formtype == 'admin_add' || $this->formtype == 'admin_edit')) ||
                            ($this->formFields['cityId']->admin==1 && ($this->formtype == 'emp_add' || $this->formtype == 'emp_edit'))
                    )) {
                $cityDataArray=$master->getCityData($user['stateId']);

                $formModifier2($form, $cityDataArray, $constraints);
            }

        });
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OfficeBrain\Bundle\UserBundle\Entity\User'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'ob_user';
    }
}
