summaryrefslogtreecommitdiffstats
path: root/funcweb/funcweb/static/javascript/ajax.js
blob: 21a3815752ca30a6b98193a6db34e985808138bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function addDomAjaxREsult(){
    
    //just creates the sturctore that is in result.html
    if (getElement('resultbox')==null){
    var result_header = DIV({'align':'center','class':'graytexts'});
    result_header.innerHTML = "Result";
    var minions = DIV({'class':'minions','id':'minions'},result_header);
    var results = DIV({'class':'resultbox','id':'resultbox'});
    var main = DIV(
            {'class':'resultbigbox','id':'resultbigbox'},
            minions,
            results
            );

    //adding those to main part ..
    var result_container=getElement("resultcontent");
    appendChildNodes(result_container,main);
    }
    else
        getElement('resultbox').innerHTML = "";
}


function remoteFormRequest(form, target, options) {
	var query = Array();
    var contents = formContents(form);
    for (var j=0; j<contents[0].length; j++){
        if(compare(target,'group_small')==0){
            if(!query[contents[0][j]]){
                query[contents[0][j]] = [];
            }
            //add that here
            query[contents[0][j]].push(contents[1][j]);

        }
        else
            query[contents[0][j]] = contents[1][j];
    }
	query["tg_random"] = new Date().getTime();
	//makePOSTRequest(form.action, target, queryString(query));
	remoteRequest(form, form.action, target, query, options);
	return true;
}

function remoteRequest(source, target_url, target_dom, data, options) {
    //before
    if (options['before']) {
        eval(options['before']);
    }
    if ((!options['confirm']) || confirm(options['confirm'])) {
        makePOSTRequest(source, target_url, getElement(target_dom), queryString(data), options);
        //after
        if (options['after']) {
            eval(options['after']);
        }
    }
	return true;
}

function makePOSTRequest(source, url, target, parameters, options) {
  var http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/xml');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }

    var insertContents = function () {
        if (http_request.readyState == 4) {
            // loaded
            if (options['loaded']) {
                eval(options['loaded']);
            }
            if (http_request.status == 200) {
                if(target) {
                    target.innerHTML = http_request.responseText;
                }
                //success
                if (options['on_success']) {
                    eval(options['on_success']);
                }
            } else {
                //failure
                if (options['on_failure']) {
                    eval(options['on_failure']);
                } else {
                    alert('There was a problem with the request. Status('+http_request.status+')');
                }
            }
            //complete
            if (options['on_complete']) {
                eval(options['on_complete']);
            }
        }
    }
  
    http_request.onreadystatechange = insertContents;
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}

function glob_submit(form_element,target_dom){
    /*
     * Because it is a common function we have to move it here for better results
     * form_element is what we submit and the target_dom is the place that will be replaced
     */
    
    before_action = null;
    //sometimes we are not sure which dom to get so is that situation
    if(compare(target_dom,'not_sure')==0)
        target_dom = which_dom();

    //if we are in the index page should to that 
    if (compare(target_dom,'minioncontent')==0){
        before_action = "myj('#resultcontent').hide();myj('#widgetcontent').hide();myj('#methotdscontent').hide();myj('#modulescontent').hide();";
    }
    else if(compare(target_dom,'groupscontent')==0){
        before_action = "myj('#miniongroupcontents').hide();";
    }
    
    form_result = remoteFormRequest(form_element,target_dom, {
            'loading': null,
            'confirm': null, 
            'after':null,
            'on_complete':null, 
            'loaded':null, 
            'on_failure':null, 
            'on_success':null, 
            'before':before_action 
            }
            );
    
    return form_result;
}

function which_dom(){
    /*
     * We use the glob submit in lots of places so we should
     * know where we are actually so that method will handle that
     */

    //that is for index.html
    dom_result = getElement('minioncontent');
    if (dom_result != null){
        //alert("Im giving back the minioncontent");
        return 'minioncontent';

    }
    
    //it is for groups_main.html
    dom_result = getElement('minion_small');
    //will change it later
     if (dom_result != null){
        return 'minion_small';
    }

    return dom_result;
}