var dynamicContent_ajaxObjects = new Array();

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
      } 
    }
  return "";
}

function showStuff(id) {
	if (document.getElementById(id).style.display == 'block') {
		document.getElementById(id).style.display = 'none';
	} else {
		document.getElementById(id).style.display = 'block';
	}
}

function hideStuff(id) {
	if (document.getElementById(id).style.display == 'block') {
		document.getElementById(id).style.display = 'none';
	} else {
		document.getElementById(id).style.display = 'block';
	}
}

function ajax_loadContent(divId,pathToFile)
{
  var ajaxIndex = dynamicContent_ajaxObjects.length;
  document.getElementById(divId).innerHTML = 'Loading content...';
  dynamicContent_ajaxObjects[ajaxIndex] = new sack();
  dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;

  dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){ ajax_showContent(divId,ajaxIndex,pathToFile); };  

  dynamicContent_ajaxObjects[ajaxIndex].runAJAX();  
} 

function ajax_showContent(divId,ajaxIndex,pathToFile)
{
  document.getElementById(divId).innerHTML = dynamicContent_ajaxObjects[ajaxIndex].response;
  dynamicContent_ajaxObjects[ajaxIndex] = false;
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function chooseFaction(faction,level) {
	var pathToFile = "include/choose_faction.php?FACTION=" + faction + "&LEVEL=" + level;
	ajax_loadContent('choose_faction',pathToFile);
	var pathToFile = "include/show_profiles.php?FACTION=" + faction;
	ajax_loadContent('profiles',pathToFile);
	//document.getElementById('profiles').innerHTML = '&nbsp';
	showList(faction,level);
}

function addToList(unit) {
	var pathToFile = "include/add_del_unit.php?Type=add&ID=" + unit;
	ajax_loadContent('army_list',pathToFile);
}

function delFromList(unit) {
	var pathToFile = "include/add_del_unit.php?Type=del&ID=" + unit;
	ajax_loadContent('army_list',pathToFile);
}

function showList(faction,level) {
	var pathToFile = "include/add_del_unit.php?Type=show&FACTION=" + faction + "&LEVEL=" + level;
	ajax_loadContent('army_list',pathToFile);
}

function showProfiles(faction) {
	var pathToFile = "include/show_profiles.php?FACTION=" + faction;
	ajax_loadContent('profiles',pathToFile);
}

function showResources(unit,list) {
	var pathToFile = "include/add_del_resources.php?Type=show&ID=" + unit + "&LIST=" + list;
	ajax_loadContent('profiles',pathToFile);
}

function addResource(unit,res) {
	var pathToFile = "include/add_del_resources.php?Type=add&ID=" + unit + "&RESOURCE=" + res;
	ajax_loadContent('profiles',pathToFile);
	sleep(500);
	var pathToFile = "include/add_del_unit.php?Type=show";
	ajax_loadContent('army_list',pathToFile);
	var pathToFile = "include/add_del_resources.php?Type=show&ID=" + unit;
	ajax_loadContent('profiles',pathToFile);
}

function delResource(unit,res) {
	var pathToFile = "include/add_del_resources.php?Type=del&ID=" + unit + "&RESOURCE=" + res;
	ajax_loadContent('profiles',pathToFile);
	sleep(500);
	var pathToFile = "include/add_del_unit.php?Type=show";
	ajax_loadContent('army_list',pathToFile);
	var pathToFile = "include/show_profiles.php";
	ajax_loadContent('profiles',pathToFile);
}

function chooseBOOMMinionType(id) {
	document.getElementById('BOOMMinionTypeBonus').innerHTML = '&nbsp';
	document.getElementById('BOOMMinionLevelAndNumber').innerHTML = '&nbsp';
	document.getElementById('BOOMMinionUpgrades').innerHTML = '&nbsp';
	var pathToFile = "boom_load_bonus.php?ID=" + id;
	ajax_loadContent('BOOMMinionTypeBonus',pathToFile);
	sleep(500);
	var pathToFile = "boom_level_and_number.php";
	ajax_loadContent('BOOMMinionLevelAndNumber',pathToFile);
}

function chooseBOOMMinionLevelAndNumber(val) {
	//alert(val);
	var pathToFile = "boom_level_and_number.php?VAL=" + val;
	ajax_loadContent('BOOMMinionLevelAndNumber',pathToFile);
	sleep(500);
	var pathToFile = "boom_upgrades.php";
	ajax_loadContent('BOOMMinionUpgrades',pathToFile);
}
