var xmlHttp = false;
var theType = false;
var theParams = false;

try {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
} catch (e) {
  // Internet Explorer
  try {
    xmlHttp=new ActiveXObject("Msxml2.XMLHttp");
  } catch (e) {
    try {
      xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
    } catch (e) {
      // no workie
    }
  }
}

/******************************************
 * AJAX FUNCTIONS
 ******************************************/

function getInfo(typ,theclass,func,params) {
  theType = typ;  // type of request so this can be used for more than one thing
  theParams = params;  // parameters for function call,other info

  // request dynamic information
  switch (typ) {
  case 'refined':
    var url = "/services/ajax/get_info.php?keywords="+params.value+"&class="+theclass+"&function="+func; 
    break;
  }

  // call server
  xmlHttp.onreadystatechange = checkReadyState;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function checkReadyState() {
  // wait for server response
  if (xmlHttp.readyState==4) {
    // ready to do stuff
    response = xmlHttp.responseText;
    switch (theType) {
    case 'refined':
      refinedSearch(response);
      break;
    }
  }
}

/******************************************
 * SITE FUNCTIONS
 ******************************************/

function refinedSearch(response) {
  // get the next field name
  var get_id = theParams.name.split('_');

  if (response != '') {
    var id = get_id[0]+'_'+get_id[1]+'_'+get_id[2]+'_'+(parseInt(get_id[3])+1);
    var select_box = document.getElementById(id);
    var new_options = response.split('\|');

    // clear the current options except for the title
    if (select_box.options.length > 0) { 
      for(var i=1;i<select_box.options.length;i=1) { 
        select_box.options[i] = null; 
      }
    }

    // add the new options 
    var len = new_options.length;
    for (var i=0;i<len;i++) {
      var opts = new_options[i].split('::');
      if (typeof opts[0] != 'undefined' && typeof opts[1] != 'undefined') {
        select_box.options[select_box.options.length] = new Option(opts[1],opts[0],false,false);
      }
    }
    // enable the select box
    select_box.disabled = false;
  } else {
    // forward to category or prod detail list
    window.location = '/search_redirect/'+theParams.value+'/f';
  }
}


