/**
 * Copyright (c) 2006, ARC Technologies.
 * All Rights Reserved
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this application.
 *
 * File     $Id: Ajax.js 1533 2011-08-28 06:18:42Z emann $ <br />
 * Author   Ed Mann <ed.mann at arctechnologies dot net>
 * Last Changed By   $LastChangedBy: emann $ <br />
 * Last Changed Date $LastChangedDate: 2011-08-28 01:18:42 -0500 (Sun, 28 Aug 2011) $ <br />
 * Description: This file has our ajax code. It will handle setting up the XMLHttpRequest and sending the requests to the server.
 * @package eve.php.applications.includes.javascript
 */
 
/**
 * set our global values
 */
var req;
var mass_arr = Array();
/**
 * set our XMLHttpRequest request object.
 */
var req = false;
   try {
     req = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       req = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         req = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         req = false;
       } 
     }
   }

   if (!req) {
     alert("Error initializing XMLHttpRequest!");
}

/**
 * doRequest
 * 
 * Function will do an http request to supplied url
 * 
 * @param {url}
 *            url to preform request on.
 * @param {action}
 *            function to preform on request return.
 */
function doRequest(url,action,submit_str){
    if (req) {
        req.onreadystatechange = function() {};
        req.abort();
   } 
	req.open('POST', url, true);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	req.onreadystatechange = action;
    req.send(submit_str);
}

/*
 * isEmpty
 * 
 * Function will check to see if the string passed in is empty or not This is
 * setup to work just like php empty. @return bool False if empty True if not
 */

function isEmpty(inputStr) {
    if (inputStr == "" || inputStr == null || inputStr.length == 0) {
        return true;
    }
   return false;
}

 /**
	 * do Export Toggle
	 * 
	 * Function will toggle all the viewable export checkboxs that are on the
	 * screen. In order to populate the export_count just pass in the name. Make
	 * sure that the name ends in _t for top and _b for bottom.
	 * 
	 * @param {string}
	 *            name of toggle button.
	 * @param {string}
	 *            name of listboxes.
	 */
 function doExportToggle(toggle,fields){
     var selected = toggle.checked;
     var list = document.getElementsByName(fields);
     var box_tot = list.length;
     if(selected){
          for(i=0; i < box_tot; i++){
              if(!list[i].checked){
              mass_arr.push(list[i].value);
              list[i].checked = true;
            }
          }
     } else {
      for(i=0; i < box_tot; i++){
          if(list[i].checked){
              list[i].checked = false;
         mass_arr_up = Array();
        var size = mass_arr.length;
        for(tt=0; tt < size; tt++) {
            if(list[i].value != mass_arr[tt]){
             mass_arr_up.push(mass_arr[tt]);
         }
        }
         mass_arr = mass_arr_up;
        }
      }
     }
    document.getElementById('export_count_t').innerHTML =  mass_arr.length;
    document.getElementById('export_count_b').innerHTML =  mass_arr.length;   
 }
 
 /**
	 * do CheckBox Toggle
	 * 
	 * Function will toggle single checkbox in the viewable screen
	 * 
	 * @param {string}
	 *            name of toggle button.
	 */
 function doCheckBoxToggle(toggle){
     var selected = toggle.checked;
    if(selected){
        mass_arr.push(toggle.value);
    } else {
        mass_arr_up = Array();
        var size = mass_arr.length;
        for(i=0; i < size; i++) {
            if(toggle.value != mass_arr[i]){
             mass_arr_up.push(mass_arr[i]);
         }
        }
        mass_arr = mass_arr_up;
    }
    document.getElementById('export_count_t').innerHTML =  mass_arr.length;
    document.getElementById('export_count_b').innerHTML =  mass_arr.length;
 }
 /**
	 * do Export
	 * 
	 * This function will call the module /action passed in and build a string
	 * off the mass_arr and pass it to the doRequest function.
	 * 
	 * @deprecated Renamed from doExport to doExportOld
	 * 
	 */
 function doExportOld(js_func){
     var submit_str = "module="+module+"&action="+action+"&context="+context;
     submit_str = submit_str+doUrlArray('export_list',mass_arr);
     doRequest(url,js_func,submit_str);
 }
 
 
  /**
	 * do Export
	 * 
	 * Function will submit a form that has the selected items that we would
	 * like to export. If our form was named contact this is how you would add
	 * this js action to the page. <code>
   *  doExport(document.contact,'csv_export');
   * </code>
	 * You can insert this into the common a (a href) or a input. You can really
	 * do anything based on your apps needs.
	 * 
	 * @param {string}
	 *            name of the form document.form_name
	 * @param {string}
	 *            Context what type of export to do csv,xml,pdf,odf.
	 * @return {void}
	 */
  function doExport(form,context){
     document.getElementById('context').value=context; 
     if(fillExport('export_hidden')){ 
     form.submit();
     }
  }
 
 /**
	 * url Array
	 * 
	 * Function will build the string for a url that has one name multiple
	 * instances.
	 * 
	 * @param{array} array to build url from
	 * @return {string} formated url array string
	 */
  function doUrlArray(obj_name,haystack){
      arr_size = haystack.length;
      var output = "";
      for(var arr_xxx = 0; arr_xxx < arr_size; arr_xxx++){
          output   = output+"&"+obj_name+"%5B%5D="+haystack[arr_xxx];
      }
      return output;
  }
  
  /**
	 * fill Export Function will take in an array and fill the elements in the
	 * database.
	 */
  function fillExport(fill){
      var field = document.getElementById(fill);
      field.innerHTML = "";
      // field.style.display= "block";
      arr_size = mass_arr.length;
      if(arr_size == 0){
          return false;
      }
      var output = "";
      for(var arr_xxx = 0; arr_xxx < arr_size; arr_xxx++){
          var input = document.createElement("input");
       input.setAttribute('name','export_list[]');
       input.setAttribute('value',mass_arr[arr_xxx]);
       input.setAttribute('type','hidden');
       field.appendChild(input);
      }
      return true;
  }
  
 /**
	 * inArray Function will return true if value is in array. Works like the
	 * PHP function
	 */
 function inArray(needle, haystack){
     var size = haystack.length;
     for(arr_zz=0; arr_zz < size; arr_zz++){
         if(needle == haystack[arr_zz]){
             return true;
         }
     }
     return false;
 }
 
 /**
	 * arrayPop Pop an item off an array
	 * 
	 * @param needle
	 *            item to remove
	 * @param array
	 *            array list
	 * @return array updated array
	 */
  function arrayPop(needle, haystack){
     var size = haystack.length;
     var output = new Array();
     for(arr_zz=0; arr_zz < size; arr_zz++){
         if(needle != haystack[arr_zz]){
             output.push(haystack[arr_zz]);
         }
     }
     return output;
  }
  
 /**
	 * reset Export
	 * 
	 * Function will clear the export array, uncheck the export all button at
	 * the top of the list and reset the count back to zero.
	 * 
	 * @param {string}
	 *            name of export all box
	 * @return void
	 */
 function resetExport(toggle,field){
     toggle = document.getElementById(toggle);
     toggle.checked = false;
     doExportToggle(toggle, field);
     mass_arr = Array();
     document.getElementById('export_count_t').innerHTML =  mass_arr.length;
     document.getElementById('export_count_b').innerHTML =  mass_arr.length;
 }
 
 /**
	 * build Url Function will take an array of input id's and get the value
	 * from the field. If it is not empty it will be added to the url. All items
	 * will be run thru the urlEncode function.
	 * 
	 * @param array
	 *            name of fields to pull values for
	 * @param string
	 *            url Url string to append to
	 * @return string url Finished url
	 */
  function buildUrl(arr,url){
     for (i = 0; i < arr.length; i++){
      if(document.getElementById(arr[i])) {
            element   = document.getElementById(arr[i]).value;
            if(!isEmpty(element)){
              url = url+"&"+arr[i]+"="+urlEncode(element);
            }   
        }
     }
   return url;
  }
  
  /**
	 * build Hash Url Function will take an array of input id's and get the
	 * value from the field. If it is not empty it will be added to the url. All
	 * items will be run thru the urlEncode function.
	 * 
	 * @param array
	 *            name of fields to pull values for
	 * @return string url Finished hast url
	 */
  function buildHashUrl(arr){
          var url = "";
          for (i = 0; i < arr.length; i++){
              if(document.getElementById(arr[i])) {
                  element   = document.getElementById(arr[i]).value;
                    if(!isEmpty(element)){
                        if(url.length == 0){
                      url = arr[i]+"="+urlEncode(element);
                    } else {
                        url = url+"&"+arr[i]+"="+urlEncode(element);
                    }
                }
             }
          }
         return url;
  }
  function setLocationHash(value){
                window.location.hash = "#"+value;
        }

        /**
		 * return a single element from the location hash
		 * 
		 * @return
		 */
        function getLocationHash(){
                if(window.location.hash){
                        return window.location.hash.slice(1);

                } else {
                        // nothing to do so return true;
                        return null;
        }
        }
        
        /**
		 * get location Hash array return an array of set values from the
		 * location hash. parsed from & separator
		 * 
		 * @return array of items.
		 */
        function getLocationHashArray(){
                retArray =  new Array();
                startHash = getLocationHash().split("&");
                for (i = 0; i < startHash.length; i++){
                        item = startHash[i].split("=");
                        retArray[item[0]] = item[1];
                }
                return retArray;
        }

