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) {
	var divs = document.getElementById("show_profiles").getElementsByTagName("div");
	for(i = 0; i < divs.length; i++){
		divs[i].style.display = "none";
	}
	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 chooseBuild(character) {
	setCookie('ckCharacter',character,10);
	addToList(character,1);
	document.getElementById('list_profiles').innerHTML = '';
	document.getElementById('show_profiles').innerHTML = '';
	document.getElementById('choose_points').innerHTML = '';
	var pathToFile = "include/choose_build.php?ID=" + character;
	ajax_loadContent('choose_build',pathToFile);
	showList();
}

function choosePoints(build,character) {
	setCookie('ckBuild',build,10);
	document.getElementById('list_profiles').innerHTML = '';
	document.getElementById('show_profiles').innerHTML = '';
	var pathToFile = "include/choose_points.php?ID=" + character + "&Build=" + build;
	ajax_loadContent('choose_points',pathToFile);
	showList();
}

function showProfiles(points,build,character) {
	setCookie('ckPoints',points,10);
	var pathToFile = "include/show_profiles.php?ID=" + character + "&Build=" + build + "&Points=" + points + "&Type=l";
	ajax_loadContent('list_profiles',pathToFile);
	var pathToFile = "include/show_profiles.php?ID=" + character + "&Build=" + build + "&Points=" + points + "&Type=f";
	ajax_loadContent('show_profiles',pathToFile);
	showList();
}

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

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

function showList(unit) {
	var pathToFile = "include/add_del_unit.php?Type=show";
	ajax_loadContent('show_list',pathToFile);
}




