/**
 * xh_Request.js - basic XMLHttpRequest object
 *
 * @version 10 Jun 2005
 * @author  Joseph Oster, wingo.com
 */

function xh_newRequest() {
  // CONSTRUCTOR for basic XMLHttpRequest object 'xh_Request'
  // CORRECT USAGE: var xmlhttp = xh_newRequest();
  // DO _NOT_ USE!: var xmlhttp = new xh_newRequest();

  // instantiate an XMLHttpRequest object; different for IE vs. Mozilla
  // try IE first - source: http://jibbering.com/2002/4/httprequest.html
  var xmlhttp=false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
     xmlhttp = false;
    }
   }
  @end @*/

  if (!xmlhttp && (typeof XMLHttpRequest != 'undefined')) xmlhttp = new XMLHttpRequest(); // Mozilla
  return xmlhttp;
  }

function xh_statusMsg(state) {
  var msg = state;
  switch (state) {
    case 0: {msg = "Uninitialized"; break;}
    case 1: {msg = "Loading..."; break;}
    case 2: {msg = "Loaded"; break;}
    case 3: {msg = "Interactive"; break;}
    case 4: {msg = "Complete"; break;}
    }
  return msg;
  }
  
  
/*Copyright (c) 2005 JSON.org*/
var JSON = function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            'boolean': function (x) {
                return String(x);
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            },
            object: function (x) {
                if (x) {
                    var a = [], b, f, i, l, v;
                    if (x instanceof Array) {
                        a[0] = '[';
                        l = x.length;
                        for (i = 0; i < l; i += 1) {
                            v = x[i];
                            f = s[typeof v];
                            if (f) {
                                v = f(v);
                                if (typeof v == 'string') {
                                    if (b) {
                                        a[a.length] = ',';
                                    }
                                    a[a.length] = v;
                                    b = true;
                                }
                            }
                        }
                        a[a.length] = ']';
                    } else if (x instanceof Object) {
                        a[0] = '{';
                        for (i in x) {
                            v = x[i];
                            f = s[typeof v];
                            if (f) {
                                v = f(v);
                                if (typeof v == 'string') {
                                    if (b) {
                                        a[a.length] = ',';
                                    }
                                    a.push(s.string(i), ':', v);
                                    b = true;
                                }
                            }
                        }
                        a[a.length] = '}';
                    } else {
                        return;
                    }
                    return a.join('');
                }
                return 'null';
            }
        };
    return {
        copyright: '(c)2005 JSON.org',
        license: 'http://www.JSON.org/license.html',

        stringify: function (v) {
            var f = s[typeof v];
            if (f) {
                v = f(v);
                if (typeof v == 'string') {
                    return v;
                }
            }
            return null;
        },

        parse: function (text) {
            try {
                return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                        text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
                    eval('(' + text + ')');
            } catch (e) {
                return false;
            }
        }
    };
}();


function extractId(elementId) {
  var customerId = elementId.split('_');
  return customerId[1];
}

function updateStatus(searchClass, statusType, node, server) {
  if (statusType == 'cp') {
    return;
  }

  if ( node == null )
    node = document;
    var tag = 'div';
  var idArray = new Array();
  var els = node.getElementsByTagName(tag);
  var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
  for (i = 0, j = 0; i < els.length; i++) {
    if ( pattern.test(els[i].className) ) {
      var classElement = els[i];
      var elementId = classElement.id;
      idArray[j] = extractId(elementId);
      j++;
    }
  }
  var idString = idArray.join(",");
  var xmlhttp = xh_newRequest();
  xmlhttp.open("POST", "/catalog/web_status_updater.php", true);
  xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
      if (xmlhttp.responseText) {
      var resultArray = JSON.parse(xmlhttp.responseText);
      for (i = 0, j = 0; i < els.length; i++) {
        if ( pattern.test(els[i].className) ) {
          var classElement = els[i];
          var customerId = extractId(classElement.id);
          var toggle = resultArray[customerId];
          if ( toggle ) {
            classElement.className = searchClass + '_' + toggle;
          }
          j++;
        }
      }
      }     
    }
    }
    xmlhttp.send(statusType + "=1&list=" + idString);
}
