if (typeof realm == 'undefined') realm = {};

realm.APPURL = 'http://www.smartcitypeople.com/';
realm.tId = 0;

realm.materiaClass = function(form) {
    this.form = form;
    this.dataType = 'json'; // json|html
    this.data = { 'agent': 'materia' },
    this.target = null;
    this.validator = null;
    this.validateOption = null;
    this.doBeforeSubmit = null;
    this.beforeSubmit = function() {
        this.validator = null;
        if (this.validateOption != null) {
            if (this.validateOption.errorLabelContainer == null || this.validateOption.errorLabelContainer == undefined) this.validateOption.errorLabelContainer = $("div.Warning", this.form);
            this.validateOption.wrapper = 'li';
            this.validator = this.form.validate(this.validateOption);
            return this.form.valid();
        };
        return true;
    };

    this.afterSuccess = function(data) {
        if (this.dataType == "json") {
            if (data.Prototype && data.Prototype == "JsonResult") {
                if (data.Result > 0 && data.IsSuccess) {
                    var result = parseInt(data.Result);
                    if (result > 0) $(":input[name=ItemID]", this.form).val(result);
                    $("div.Warning", this.form).html('').hide();
                    alert(data.Message);
                }
                else {
                    var jWarning = $("div.Warning", this.form);
                    if (jWarning.length > 0) jWarning.html('<li>' + data.Message + '</li>').show();
                    else alert(data.Message);
                }
            }
        } else if (this.dataType == 'text') {
            alert(data);
        } else if (this.dataType == 'html') {
            if (this.target != null) this.target.html(data).slideToggle();
        };
        if (this.doAfterSuccess) this.doAfterSuccess(data);
    };

    this.success = function(msg, e, mine) {
        $("#idLoader").hide(1000);
        realm.tId++;
        $("input[name=Service]", this.form).val(''); //clear
        this.afterSuccess(msg, e, mine);
    };

    this.submit = function(service, itemid) {
        if (this.doBeforeSubmit) if (this.doBeforeSubmit(service, itemid) == false) return;
        if (itemid) $("input[name=ItemID]", this.form).val(itemid);
        if (this.beforeSubmit() == true) {
            $("#idLoader").show();
            if (this.target != null) this.target.slideToggle('fast');
            $("input[name=Service]", this.form).val(service);
            $(this.form).ajaxSubmit(this);

			if (this.afterSuccess2) this.afterSuccess2();
        }
        if (realm.autoFunction) realm.autoFunction();
		return false;
    };

    // reset materia to original state
    this.reset = function() {
        materia.dataType = 'json';
        materia.target = null;
        materia.validateOption = null;
        this.validator = null;
    };
};

jQuery(function() {
	jQuery('<div id="idLoader"><img src="' + realm.APPURL + 'images/loading4.gif" alt="loading..." />&nbsp;Now Loading</div>').appendTo("body").hide();
	jQuery().ajaxStart(function() { $("#idLoader").show(); })
		.ajaxStop(function() { $("#idLoader").hide(); })
		.ajaxError(function(a, b, e) { alert(b.responseText); throw e; });
});
