Menu hotkeys system implementation.
[fa-stable.git] / js / utils.js
index 5dbbcbf96c7cfc42b5494c32db685f313e84bf2a..b2fc71c4b5bb83f1acb0c065d57b3684228eb638 100644 (file)
 // 
     JsHttpRequest.request= function(trigger, form) {
 
-               
                var submitObj = typeof(trigger) == "string" ? 
                        document.getElementsByName(trigger)[0] : trigger;
                
                form = form || (submitObj && submitObj.form);
 
+               var upload = form && form.enctype=='multipart/form-data';
+               
                var url = form ? form.action : 
                  window.location.toString();
 
+               var content = this.formInputs(trigger, form, upload);
+
                if (!form) url = url.substring(0, url.indexOf('?'));
 
-               var values = this.formValues(trigger, form);
                if (!submitObj) 
-                       values[trigger] = 1;
+                       content[trigger] = 1;
                // this is to avoid caching problems
-               values['_random'] = Math.random()*1234567;
+               content['_random'] = Math.random()*1234567;
+
         JsHttpRequest.query(
-            'POST '+url, // backend
-               values,
+            (upload ? "form." : "")+"POST "+url, // force form loader
+               content,
             // Function is called when an answer arrives. 
            function(result, errors) {
                 // Write the answer.
             false  // do not disable caching
         );
     }
-       // returns input field values submitted when form button 'name' is pressed
-       //
-       JsHttpRequest.formValues = function(inp, objForm)
+       // collect all form input values plus inp trigger value
+       JsHttpRequest.formInputs = function(inp, objForm, upload)
        {
                var submitObj = inp;
                var q = {};
-               
 
                if (typeof(inp) == "string")
                        submitObj = document.getElementsByName(inp)[0];
                        for( var i=0; i < formElements.length; i++)
                        {
                          var el = formElements[i];
+                         var name = el.name;
                                if (!el.name) continue;
+                               if(upload) { // for form containing file inputs collect all 
+                                       // form elements and add value of trigger submit button
+                                       // (internally form is submitted via form.submit() not button click())
+                                       q[name] = submitObj.type=='submit' && el==submitObj ? el.value : el;
+                                       continue;
+                               }
                                if (el.type )
                                  if( 
                                  ((el.type == 'radio' || el.type == 'checkbox') && el.checked == false)
                                        continue;
                                if (el.disabled && el.disabled == true)
                                        continue;
-                               var name = el.name;
                                if (name)
                                {
                                        if(el.type=='select-multiple')
 //
 //     User price formatting
 //
-function price_format(post, num, dec, label) {
+function price_format(post, num, dec, label, color) {
+       var el = label ? document.getElementById(post) : document.getElementsByName(post)[0];
        //num = num.toString().replace(/\$|\,/g,'');
        if(isNaN(num))
                num = "0";
@@ -149,9 +157,12 @@ function price_format(post, num, dec, label) {
         num = ((sign)?'':'-') + num;
         if(dec!=0) num = num + user.ds + cents;
        if(label)
-           document.getElementById(post).innerHTML = num;
+           el.innerHTML = num;
        else
-           document.getElementsByName(post)[0].value = num;
+           el.value = num;
+       if(color) {
+                       el.style.color = (sign) ? '' : '#FF0000';
+       }
 }
 
 function get_amount(doc, label) {
@@ -172,12 +183,17 @@ function goBack() {
 }
 
 function setFocus(name, byId) {
-
+  if(document.location.pathname.indexOf('index.php') != -1) {
+       // this is application menu page - set focus on first link
+       // var el = document.getElementById('msgbox');
+       // TODO find first link after msgbox and set focus
+  }
   if(!name) {
        if (_focus)     
                name = _focus;  // last focus set in onfocus handlers
-       else {  // no current focus -  set it from from hidden var (first page display)
-         var cur = document.getElementsByName('_focus')[0];
+       else 
+        if (document.forms.length) {   // no current focus (first page display) -  set it from from last form
+         var cur = document.getElementsByName('_focus')[document.forms.length-1];
          if(cur) name = cur.value;
        }
   }