/* miniHustle v0.1

   The deformed step-cousin of kungFuHustle. */

// Create a new XMLHttpRequest object to talk to the Web server

// Initialize
var xmlHttp = false;

// Begin IE Code
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/
// End IE Code

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  // Mozilla and every other sane browser :)
  xmlHttp = new XMLHttpRequest();
}

function callServer(params) {
  // Call Server using the following url:
  var url = "/services/cartridgecity/yfc_protocol.php";
  if (params != '') {
    url += params;
  }
  // open connection.  usage: xmlHttp.open(METHOD,URL,ASYNC)
  xmlHttp.open("GET", url, true);
  // call a function when onreadystatechange updates
  xmlHttp.onreadystatechange = updatePage;
  // must send *something*, so send null
  xmlHttp.send(null);
}

function updatePage() {
  var response = '';
  if (xmlHttp.readyState == 4) {
    // if readState == 4 (page is complete), get responseText
    response = xmlHttp.responseText;
  }
  if(response != '') {
    yfc_update(response);
  }
}

/* End AJAX Functions */

/* Begin Site Functions */
function yfc_request(item) {
  if (!item.value.match(/^YFC$/i)) {
	window.location = '/viewcart?p='+item.value;
    // document.getElementById('yfc_error').innerHTML = 'Invalid promo code.  Please try again.';
  } else {
    yfc_getStates();
  }
}

function yfc_getChapters(state) {
  var params = '?action=get_chapters&st='+state;
  callServer(params);
}

function yfc_getStates() {
  var params = '?action=pop_dd';
  callServer(params);
}

function yfc_update(res) {
  var i = res.split('::');
  var type = i[0];
  var info = i[1];
  if (type == 'dd') {
    document.getElementById('yfc_error').innerHTML = 'Select your state (chapters populate after selecting state).';
    document.getElementById('promo_area').innerHTML = i[1];
  } else if (type == 'gc') {
    document.getElementById('yfc_error').innerHTML = 'Select your chapter (or state if you chose incorrectly).';
    clearDropBox('chapter');
    yfc_addChapters(info,'chapter');
  }
}

function yfc_addChapters(info,id) {
  var dBox = document.getElementById(id);
  items = info.split(',');
  for (var i=0,n=items.length;i<n;i++) {
    if (items[i] == '') { continue; }
    var z = items[i].split('!');
    dBox.options[dBox.options.length] = new Option(z[1],z[0]);
  }    
}

function yfc_postCode() {
  var s = document.getElementById('state').value;
  var c = document.getElementById('chapter').value;
  if (c.length == 1) {
    c = '0' + '' + c;
  }
  window.location = '/viewcart?c=YFC'+s+c;
}

function clearDropBox(id) {
  var dBox = document.getElementById(id);
  if (dBox.options.length <= 0) {
    alert('error: not a drop box');
  } else {
    for (var i=0,n=dBox.options.length;i<n;i++) {
      dBox.options[i] = null;
    }
    dBox.options[0] = new Option('-- Select Chapter --','');
  }
}
