QuestionaryController = function( data ) {
	QuestionaryController.superClass.apply( this, arguments );
	
	var form = this.getForm( 'questionary' );
	this.form = form;

	var how = form.getInput( 'uzn' );
	if( how ) {
		how.attachObserver( 'Changed', this.setHowCheck, this );
		this.setHowCheck();
	}

	var simpleOptions = {
		'alert': alert_required,
		'reg': /^(.|\n)+$/
	};

	if( window.PageStatus && PageStatus.Lang() != 'id' ) {
		form.addInput({ 'id': 'middleName', 'options': simpleOptions });
		form.addInput({ 'id': 'secondName', 'options': simpleOptions });
	}
	else if( window.PageStatus && PageStatus.Lang() == 'id' ) {
		form.addInput({ 'id': 'middleName' });
		form.addInput({ 'id': 'secondName' });
	}

	var courses = form.getInput( 'courses' );
	if( courses ) {
		courses.attachObserver( 'Changed', this.setWhatCourses, this );
	}

	var isSmallBusiness = form.getInput( 'isSmallBusiness' );
	if( isSmallBusiness ) {
		isSmallBusiness.attachObserver( 'Changed', this.setIsSmallBusiness, this );
	}

	this.hasSite = form.getInput( 'hassite' );
	if( this.hasSite ) {
		this.hasSite.attachObserver( 'Changed', this.toggleSiteField, this );
		this.toggleSiteField();
	}
}
QuestionaryController.inheritsFrom( FormController );

/* ---------------------------------------------------------------------------------------------- */

QuestionaryController.prototype.toggleSiteField = function() {
    var value = this.hasSite.getValue();

    if( value == '1' ) {
        this.form.addInput({
            id: 'site_url',
            options: {
				alert:  alert_required,
				reg: /^.+$/
			}
        })
        $( '.site_url_input' ).show()
    }
    else {
        this.form.removeInput( 'site_url' );
        $( '.site_url_input' ).hide()
    }
}

QuestionaryController.prototype.setHowCheck = function() {
	var form = this.getForm( 'questionary' );
	var how = form.getInput( 'uzn' );

	if( how.getValue() == 'other' ) {
		form.addInput({
			'id': 'other',
			'options': {
				'alert':  alert_required,
				'reg': /^.+$/
			}
		});
	} else {
		form.removeInput( 'other' );
	}

	var otherPart = this.getEl( 'other_part' );
	if( otherPart ) {
		this.setClassName( 'hidden', ( how.getValue() != 'other' ), otherPart );
	}
}

/* ---------------------------------------------------------------------------------------------- */

QuestionaryController.prototype.setWhatCourses = function() {
	var form = this.getForm( 'questionary' );
	var courses = form.getInput( 'courses' );

	if( courses.getValue() == 'on' ) {
		form.addInput({
			'id': 'whatCourses',
			'options': {
				'alert':  alert_required,
				'reg': /^.+$/
			}
		});
	} else {
		form.removeInput( 'whatCourses' );
	}

	var coursesPart = this.getEl( 'whatCourses_part' );
	if( coursesPart ) {
		this.setClassName( 'hidden', ( courses.getValue() != 'on' ), coursesPart );
	}
}

/* ---------------------------------------------------------------------------------------------- */

QuestionaryController.prototype.setIsSmallBusiness = function() {
	var form = this.getForm( 'questionary' );
	var isSmallBusiness = form.getInput( 'isSmallBusiness' );

	form.removeAlerts();

	if( isSmallBusiness.getValue() == 1 ) {
		for( var c in window.smallBusinessPart ) {
			var input = window.smallBusinessPart[ c ];
			form.addInput( input );
		}
	} else {
		for( var c in window.smallBusinessPart ) {
			var input = window.smallBusinessPart[ c ];
			form.removeInput( input[ 'id' ] );
		}
	}

	var isSmallBusinessPart = this.getEl( 'isSmallBusiness_part' );
	if( isSmallBusinessPart ) {
		this.setClassName( 'hidden', ( isSmallBusiness.getValue() != 1 ), isSmallBusinessPart );
	}
}

/* ------------------------------------------------------------------------------------------------------------ */

QuestionaryController.prototype.dataAnswer = function( answer ) {
    this.setLoad( false );

    if( answer && answer[ 'status' ] ) {
		if( answer[ 'status' ] == 'success' ) {
		    //if( /test1/.test( document.location.href ) ) {
		        this.setLoad( true );
                this.form.submit( this.form.getValues() );
            /*}
            else
                document.location.href = 'http://' + location.hostname + '/' + PageStatus.Lang() + '/partnership/registration/submitted/';
            */
		} else if( answer[ 'status' ] == 'error' && answer[ 'errors' ] ) {
            this.updateCaptcha( 'captcha' );
			var notFieldError = '';
			for( var c in answer[ 'errors' ] ) {
				if( !this.form.getInput( c ) ) {
					notFieldError += _text( answer[ 'errors' ][ c ] );
				}
			}
			if( notFieldError != '' ) {
				this.window.fill( notFieldError ).setTitle( _text( 'ERROR_TITLE' ) ).show();
			}
			this.form.setFieldError( answer[ 'errors' ] );
		} else {
            this.updateCaptcha( 'captcha' );    			    		    
		    this.handleError();
		}
	} else {
        this.updateCaptcha( 'captcha' );
	    this.handleError();
	}

}

QuestionaryController.prototype.sendData = function() {
   this.setLoad( true );
   //if( /test1/.test( document.location.href ) ) {
        var url = '/' + PageStatus.Lang() + '/partnership_form/',
            params = this.form.getValues(),
            store = new Store( url );
        
        store.setParams( params );
        store.attachObserver( 'Loaded', this.dataAnswer, this );
        store.attachObserver( 'Error', this.handleError, this );
        store.load();
    /*}
    else {
        this.form.submit( this.form.getValues() );
    } */
}

QuestionaryController.prototype.updateCaptcha = function( id ) {
	var captcha = this.getEl( id );
	if(
		captcha &&
		captcha.nodeName == 'IMG'
	) {
		var psid = VisitorStatus.GetCookie( 'PHPSESSID' );
		captcha.src = '/' + PageStatus.Lang() + '/school/captcha/?renew=1&base=' + ( Math.random() * 10000 ) + ( psid ? ( '&PHPSESSID=' + psid ) : '' );
	}
}


/* ------------------------------------------------------------------------------------------------------------ */

var lang = ( window.PageStatus ? PageStatus.Lang() : ( window.lang ? window.lang : 'ru' ) );
$(function() {
	//var lngs = new RegExp( /\/(ru|en|es|ar|id|cn)\//i ).exec( window.location.href );
	//window.lang = PageStatus.Lang() : 'en';

	/*window.msgs = {
		'ru': 'Поле обязательно для заполнения',
		'en': 'This field is required',
		'fr': 'Ce champ est obligatoire',
		'id': 'Informasi ini dibutuhkan',
		'cn': '必须填写的内容。',
		'ar': 'يجب ملء الخانة',
		'id': 'Kolom ini dibutuhkan',
		'es': 'Este campo es obligatorio',
		'tr': 'Tamamlayabilmek için bu alan gereklidir.'
	};*/

    window.alert_required = {
		'UNKNOWN': 'FIELD_REQUIRED',
		'EMPTY': 'FIELD_REQUIRED',
		'INCORRECT': 'FIELD_INCORRECT'
	};
    
	var simpleOptions = {
		'alert': alert_required,
		'reg': /^(.|\n)+$/
	};
	
	var selectOptions = {
		'alert': alert_required
	};

	var digitOptions = {
		'alert': alert_required,
		'reg': /^[\d\,\.]+$/
	};
    
    var notNullDigit = {
        'alert': alert_required,
		'reg': /^[1-9]{1}[\d\,\.]*$/
    }

	window.questionaryController = new QuestionaryController([{
		'id': 'questionary',
		'options': [
			//{ 'id': 'secondName', 'options': simpleOptions },
			{ 'id': 'firstName', 'options': simpleOptions },
			//{ 'id': 'middleName', 'options': simpleOptions },
			{ 'id': 'birthday', 'options': simpleOptions },
			{ 'id': 'country', 'options': selectOptions },
			{ 'id': 'city', 'options': simpleOptions },
			{ 'id': 'phone', 'options': {
				'alert': alert_required,
				'reg': /^\+[\d\(\)\-\s]{5,}$/,
				'template': '+' + _text( '7 495 7107676' )
			}},
			{ 'id': 'email', 'options': {
				'reg': /^[\.\-_A-z0-9]+?@[\.\-A-z0-9]+?\.[A-z0-9]{2,6}$/,
				'alert':  alert_required
			}},
			{ 'id': 'experience', 'options': selectOptions },
			{ 'id': 'uzn', 'options': selectOptions },
			{ 'id': 'advBudget', 'options': digitOptions },
			{ 'id': 'concurents', 'options': simpleOptions },
			{ 'id': 'concurentsPartner', 'options': simpleOptions },
			{ 'name': 'courses', 'type': 'radio', 'options': {
				'alert':  alert_required,
				'error_to': 'courses_et'
			}},
			{ 'name': 'isSmallBusiness', 'type': 'radio', 'options': {
				'alert':  alert_required,
				'error_to': 'isSmallBusiness_et'
			}},
			{ name: 'hassite',
				type: 'radio',
				options: {
					'alert':  alert_required
				}
			},
			{ 'id': 'esse', 'options': simpleOptions },
			{ 'id': 'imgcaptcha', 'options': digitOptions }
		],
		'handler': function() {
			this.sendData();
		}
	}]);

	var notObligatoryDigitOptions = {
		'alert': alert_required,
		'reg': /^[\d\.\,]*$/
	}

	window.smallBusinessPart = [
		{ 'id': 'companyName', 'options': simpleOptions },
		{ 'id': 'companyExperience', 'options': notNullDigit },
		{ 'id': 'companySpecialization', 'options': simpleOptions },
		{ 'id': 'companyAddress1', 'options': simpleOptions },
		{ 'id': 'companyAddress2', 'options': simpleOptions },
		{ 'id': 'companySite', 'options': {
			'alert': alert_required,
			'reg': /^(.{0}|[A-z0-9\-_\.\/\:\?\=\&]+)$/
		}},
		{ 'id': 'companyEmployes', 'options': notNullDigit },
		{ 'id': 'companyClients', 'options': selectOptions },
		{ 'id': 'companyOfficeArea', 'options': notNullDigit },
		{ 'id': 'photoOffice', 'options': {
			'alert': alert_required,
			'reg': /^(.{0}|.+\.(jpg|gif|png|jpeg))$/
		}},
		{ 'id': 'companyOffice', 'options': selectOptions },
		{ 'id': 'kindsOfAdvertising', 'options': simpleOptions },
		{ 'id': 'cityPopulation', 'options': notNullDigit },
		{ 'id': 'aroundPopulation', 'options': notNullDigit }
	];

	if( window.InputCalendar ) 
		new InputCalendar({
			id : 'birthday', 
			params : {
				reg : /([0-3][0-9]).([01][0-9]).([0-9])/, 
				tplShow : 'dd MM yyyy',
				tplInput : 'dd.mm.yyyy',
				delimiter : /\./, 
				mask: '01.01.2010',
				date: ' '
			}
		})
	else if( window.Calendar )
		new Calendar( 'birthday' );
});
