var NetDirector = Class.create({

    ajaxURL:            'ajax.php',
    ajaxMethod:         'post',
    debugMode:          false,

    initialize: function () {
    },


    browserIsIE: function () {
		return ( navigator.userAgent.toLowerCase().indexOf('msie') > -1 ) ? true : false;
	},


    changeClass: function ( config ) {

    	defaults = ['className', 'container', 'newClassName', 'interval', 'tag'];
		config = this.setDefaults( config, defaults );

		var objString = '';

		if ( config.className == '' ) {
			this.debugOut( 'No class name specified for changeClass()' );
			return false;
		}

		if ( config.interval == '' ) {
			config.interval = -1;
		}

		if ( config.container ) {
			objString += '#' + config.container + ' ';
		}

		if ( config.tag ) {
			objString += config.tag;
		}

		objString += '.' + config.className;

    	objs = $$( objString );

    	this.debugOut( 'changeClass("' + config.className + '") : ' + objs.length + ' objects found.' );

    	for ( var i = 1; i <= objs.length; i++ ) {

			if ( ( config.interval == -1 ) || ( ( config.interval > -1 ) && ( i % config.interval == 0 ) ) ) {

				objs[(i-1)].removeClassName( config.className );

				if ( config.newClassName != '' ) {
					objs[(i-1)].addClassName( config.newClassName );
				}

			}

		}

	},


	chooseSelectOption: function ( obj, value ) {

		var valid = true;

        switch ( typeof( obj ) ) {

            case 'string':

                obj = $(obj);

                if ( !obj ) {
                    valid = false;
                }

            break;

            case 'object':
            break;

            default:
            	valid = false;

        }

        if ( !valid ) {
			this.debugOut( 'chooseSelectOption: Unable to find ' + obj );
			return false;
		}

		for ( var i = 0; i < obj.options.length; i++ ) {

			if ( obj.options[i].value == value ) {
				obj.selectedIndex = i;
				break;
			}

		}

	},


    debugOut: function ( msg ) {

        if ( this.debugMode && ( typeof(console) != 'undefined' ) ) {
            console.log( msg );
        }

    },


    emptySelect: function ( obj, options ) {

    	var leave = 0;

    	if ( options ) {
    		if ( options.keepFirst ) {
				leave = 1;
			}
		}

		//  Remove options
		for ( var i = obj.options.length; i >= leave; i-- ) {
			obj.options[i] = null;
		}

	},


    getDepartments: function ( callback ) {

    	if ( !callback ) {
			callback = 'cbGetDepartments';
		}

    	this.xhr({
    		callback: callback,
    		type: 'getDepartments'
		});

	},


    getVacancies: function () {

    	this.xhr({
    		callback: 'cbGetVacancies',
    		params: '',
    		type: 'getVacancies'
		});

	},


    jsonPopulateSelect: function ( config ) {

    	valid = true;

    	if ( typeof(config.data) == 'string' ) {
    		config.data = config.data.evalJSON();
		}

        switch ( typeof( config.combo ) ) {

            case 'string':

                combo = $(config.combo);

                if ( !config.combo ) {
                    valid = false;
                }

            break;

            case 'object':
                combo = config.combo
            break;

            default:
            	valid = false;

        }

        if ( !valid ) {
			this.debugOut( 'Invalid combo for jsonPopulateSelect' );
			return false;
		}

        for (var i = 0; i < config.data.length; i++) {
            combo[combo.options.length] = new Option( eval('config.data[i].'+config.labelField), eval('config.data[i].'+config.valueField) )
        }

    },


    populateForm: function ( form, config ) {

    	var keys   = Object.keys(config.fields);
    	var values = Object.values(config.fields);

    	if ( typeof( keys ) != 'object' || keys.length == 0 ) {
			return false;
		} else {

			if ( isNaN(config.delay) ) {
				config.delay = 0;
			}

			new PeriodicalExecuter( function ( pe ) {

				for ( var i = 0; i < keys.length; i++ ) {
					f = $(keys[i]);

					if ( f ) {

						switch ( f.type ) {

							case 'checkbox':
								if ( values[i] ) {
									f.checked = true;
								}
							break;

							case 'select-one':
								this.chooseSelectOption( f, values[i] );
							break;

							case 'text':
							case 'textarea':
								f.value = values[i];
							break;

						}

					}

				}

				pe.stop();

			}.bind(this), config.delay);

		}

	},


    setDefaults: function ( config, defaults ) {

		for ( i = 0; i < defaults.length; i++ ) {

			if ( !eval( 'config.' + defaults[i] ) ) {
				eval( 'config.' + defaults[i] + ' = false' );
			}

		}

		return config;

	},


    xhr: function ( options ) {

        if ( !options ) {
            this.debugOut( 'No options specified for XHR.' );
            return false;
        }

        if ( !options.type ) {
            this.debugOut( 'No type option specified for XHR.' );
            return false;
        }

        if ( !options.callback ) {
            this.debugOut( 'No callback option specified for XHR.' );
            return false;
        }

        var params = 'type=' + options.type

        if ( options.parameters ) {
            params += '&' + options.parameters;
        }

        new Ajax.Request( this.ajaxURL, {
            method: this.ajaxMethod,
            parameters: params,
            onSuccess: function ( r ) {

                t = r.responseText.evalJSON();

                eval( 'this.' + options.callback + '(t);' );

            }.bind(this)
        });

    }

});