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 img = document.createElement('img');
  img.src = 'img/ajax-loader.gif';
  document.getElementById(divId).appendChild(img);
  var ajaxIndex = dynamicContent_ajaxObjects.length;
  //document.getElementById(divId).innerHTML = 'Loading content...';
  document.getElementById(divId).innerHTML = '<h3>Loading...</h3> <img src="img/ajax-loader.gif"/>';
  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 Add() {
	var pathToFile = "include/add.php";
	ajax_loadContent('content',pathToFile);
}

function SubmitAddForm(name,type,rank,comments,spoiler,own,viewed,related,url,plot,year,creator) {
	if (comments.indexOf('\r\n')!=-1) {
		var comments = comments.replace ( /\r\n/g, "<br />" );
	} else if (comments.indexOf('\r')!=-1) {
		var comments = comments.replace ( /\r/g, "<br />" );
	} else if (comments.indexOf('\n')!=-1) {
		var comments = comments.replace ( /\n/g, "<br />" );
	}
	if (spoiler.indexOf('\r\n')!=-1) {
		var spoiler = spoiler.replace ( /\r\n/g, "<br />" );
	} else if (spoiler.indexOf('\r')!=-1) {
		var spoiler = spoiler.replace ( /\r/g, "<br />" );
	} else if (spoiler.indexOf('\n')!=-1) {
		var spoiler = spoiler.replace ( /\n/g, "<br />" );
	}
	if (plot.indexOf('\r\n')!=-1) {
		var plot = plot.replace ( /\r\n/g, "<br />" );
	} else if (plot.indexOf('\r')!=-1) {
		var plot = plot.replace ( /\r/g, "<br />" );
	} else if (plot.indexOf('\n')!=-1) {
		var plot = plot.replace ( /\n/g, "<br />" );
	}
	if (related.indexOf('\r\n')!=-1) {
		var related = related.replace ( /\r\n/g, "<br />" );
	} else if (related.indexOf('\r')!=-1) {
		var related = related.replace ( /\r/g, "<br />" );
	} else if (related.indexOf('\n')!=-1) {
		var related = related.replace ( /\n/g, "<br />" );
	}
	for (var i=0; i < document.getElementsByName(own).length; i++) {
		if (document.getElementsByName(own)[i].checked) {
			var own_val = document.getElementsByName(own)[i].value;
		}
	}
	if (name == undefined || name.length == 0) {
		alert("Must enter a name.");
	} else if (type == undefined || type.length == 0) {
		alert("Must choose a type.");
	} else if (year == undefined || year.length == 0) {
		alert("Must enter a year.");	
	} else if (rank == undefined || rank.length == 0) {
		alert("Must choose a grade.");
	} else if (own_val == undefined || own_val.length == 0) {
		alert("Must determine if we own it.");
	} else if (plot.indexOf("=") == 0) {
		//var plot = '';
		alert("Plot cannot begin with =.");
	} else if (comments.indexOf("=") == 0) {
		//var comments = '';
		alert("Comments cannot begin with =.");
	} else if (spoiler.indexOf("=") == 0) {
		//var comments = '';
		alert("Spoiler comments cannot begin with =.");
	} else if (related.indexOf("=") == 0) {
		//var related = '';
		alert("Related cannot begin with =.");
	} else {
		var pathToFile = "include/add.php?NAME=" + name + "&TYPE=" + type + "&RANK=" + rank + "&COMMENTS=" + comments + "&SPOILER=" + spoiler + "&OWN=" + own_val + "&VIEWED=" + viewed + "&RELATED=" + related + "&URL=" + url + "&PLOT=" + plot + "&YEAR=" + year + "&CREATOR=" + creator;
		ajax_loadContent('content',pathToFile);
	}
}

function Edit() {
	var pathToFile = "include/edit.php";
	ajax_loadContent('content',pathToFile);
}

function EditID(id) {
	var pathToFile = "include/edit.php?ID=" + id;
	ajax_loadContent('content',pathToFile);
}

function SubmitEditForm(id,name,type,rank,comments,spoiler,own,viewed,related,url,plot,year,creator) {
	if (comments.indexOf('\r\n')!=-1) {
		var comments = comments.replace ( /\r\n/g, "<br />" );
	} else if (comments.indexOf('\r')!=-1) {
		var comments = comments.replace ( /\r/g, "<br />" );
	} else if (comments.indexOf('\n')!=-1) {
		var comments = comments.replace ( /\n/g, "<br />" );
	}
	if (spoiler.indexOf('\r\n')!=-1) {
		var spoiler = spoiler.replace ( /\r\n/g, "<br />" );
	} else if (spoiler.indexOf('\r')!=-1) {
		var spoiler = spoiler.replace ( /\r/g, "<br />" );
	} else if (spoiler.indexOf('\n')!=-1) {
		var spoiler = spoiler.replace ( /\n/g, "<br />" );
	}
	if (plot.indexOf('\r\n')!=-1) {
		var plot = plot.replace ( /\r\n/g, "<br />" );
	} else if (plot.indexOf('\r')!=-1) {
		var plot = plot.replace ( /\r/g, "<br />" );
	} else if (plot.indexOf('\n')!=-1) {
		var plot = plot.replace ( /\n/g, "<br />" );
	}
	if (related.indexOf('\r\n')!=-1) {
		var related = related.replace ( /\r\n/g, "<br />" );
	} else if (related.indexOf('\r')!=-1) {
		var related = related.replace ( /\r/g, "<br />" );
	} else if (related.indexOf('\n')!=-1) {
		var related = related.replace ( /\n/g, "<br />" );
	}
	for (var i=0; i < document.getElementsByName(own).length; i++) {
		if (document.getElementsByName(own)[i].checked) {
			var own_val = document.getElementsByName(own)[i].value;
		}
	}
	if (name == undefined || name.length == 0) {
		alert("Must enter a name.");
	} else if (type == undefined || type.length == 0) {
		alert("Must choose a type.");
	} else if (year == undefined || year.length == 0) {
		alert("Must enter a year.");	
	} else if (rank == undefined || rank.length == 0) {
		alert("Must choose a grade.");
	} else if (own_val == undefined || own_val.length == 0) {
		alert("Must determine if we own it.");
	} else if (plot.indexOf("=") == 0) {
		//var plot = '';
		alert("Plot cannot begin with =.");
	} else if (comments.indexOf("=") == 0) {
		//var comments = '';
		alert("Comments cannot begin with =.");
	} else if (spoiler.indexOf("=") == 0) {
		//var comments = '';
		alert("Spoiler comments cannot begin with =.");
	} else if (related.indexOf("=") == 0) {
		//var related = '';
		alert("Related cannot begin with =.");
	} else {
		var pathToFile = "include/edit.php?ID=" + id+ "&NAME=" + name + "&TYPE=" + type + "&RANK=" + rank + "&COMMENTS=" + comments + "&SPOILER=" + spoiler + "&OWN=" + own_val + "&VIEWED=" + viewed + "&RELATED=" + related + "&URL=" + url + "&PLOT=" + plot + "&YEAR=" + year + "&CREATOR=" + creator;
		ajax_loadContent('content',pathToFile);
	}
}

function Delete(id,confirmed) {
	var pathToFile = "include/delete.php?ID=" + id + "&CONFIRMED=" + confirmed;
	ajax_loadContent('content',pathToFile);
}

function UploadImage(id) {
	var pathToFile = "include/upload.php?ID=" + id;
	ajax_loadContent('content',pathToFile);
}

function EditTop() {
	var pathToFile = "include/edit_top.php";
	ajax_loadContent('content',pathToFile);
}

function EditOther() {
	var pathToFile = "include/edit_other_top.php";
	ajax_loadContent('content',pathToFile);
}

function SubmitEditTopForm(type) {
	var pathToFile = "include/edit_top.php?TYPE=" + type;
	ajax_loadContent('content',pathToFile);
}

function SubmitEditOtherForm(type) {
	var pathToFile = "include/edit_other_top.php?TYPE=" + type;
	ajax_loadContent('content',pathToFile);
}

function CheckDuplicates(name,limit,checked) {
	var fields=new Array();
	var duplicates=new Array();
	for (var i=1; i <= limit; i++) {
		fields[i]=document.getElementById(name + i).value;
		duplicates[i]="";
		document.getElementById("error" + i).innerHTML = '';
	}
	
	var count=fields.length;
	
	for (i=1; i <= count; i++) {
		// Count through each element
		for (j = i; j > 0; j--) {
			// Check each other element behind this one
			if (fields[i]==fields[j]) {
				// If it matches something, mark it in the duplicates array
				//alert("Checked " + name + i + " vs. " + name + j + ".\nValues were " + fields[i] + " and " + fields[j] + ".");
				if (duplicates[i] == undefined || duplicates[i].length == 0) {
					duplicates[i]=j;
				} else {
					duplicates[i]=duplicates[i] + ", " + j;
				}
			}
		}
		for (j = i + 1; j <= count; j++) {
			// Check each other element ahead of this one
			if (fields[i]==fields[j]) {
				//alert("Checked " + name + i + " vs. " + name + j + ".\nValues were " + fields[i] + " and " + fields[j] + ".");
				// If it matches something, mark it in the duplicates array
				if (duplicates[i] == undefined || duplicates[i].length == 0) {
					duplicates[i]=j;
				} else {
					duplicates[i]=duplicates[i] + ", " + j;
				}
			}
		}
	}
	for (i=1; i <= duplicates.length; i++) {
		if (duplicates[i] != undefined && duplicates[i].length > 0) {
			//alert("Place " + i + " is the same as place " + j);
			document.getElementById("error" + i).style.display == 'block';
			document.getElementById("error" + i).innerHTML = 'Duplicate(s) found: ' + duplicates[i];
		}	
	}
	return false;
}

function SubmitChangedTopForm(name,limit,type) {
	var fields=new Array();
	for (var i=1; i <= limit; i++) {
		var x = i - 1
		fields[i]=document.forms[1][x].value;
	}
	
	var count=fields.length;
	
	for (i=1; i <= count; i++) {
		// Count through each element
		for (j = i - 1; j > 0; j--) {
			// Check each other element behind this one
			if (fields[i]==fields[j]) {
				alert("Duplicate found: " + i + " equals " + j + ".\nValues were " + fields[i] + " and " + fields[j] + ".");
				return true;
			}
		}
		for (j = i + 1; j <= count; j++) {
			// Check each other element ahead of this one
			if (fields[i]==fields[j]) {
				alert("Duplicate found: " + i + " equals " + j + ".\nValues were " + fields[i] + " and " + fields[j] + ".");
				return true;
			}
		}
	}
	var getstring;
	for (i = 1; i <= limit; i++) {
		getstring = getstring + "|" + fields[i];
	}
	
	var pathToFile = "include/edit_top.php?TYPE=" + type + "&LIST=" + getstring;
	ajax_loadContent('content',pathToFile);
}

function SubmitChangedOtherForm(name,limit,type) {
	var fields=new Array();
	for (var i=1; i <= limit; i++) {
		var x = i - 1
		fields[i]=document.forms[1][x].value;
	}
	
	var count=fields.length;
	
	for (i=1; i <= count; i++) {
		// Count through each element
		for (j = i - 1; j > 0; j--) {
			// Check each other element behind this one
			if (fields[i]==fields[j]) {
				alert("Duplicate found: " + i + " equals " + j + ".\nValues were " + fields[i] + " and " + fields[j] + ".");
				return true;
			}
		}
		for (j = i + 1; j <= count; j++) {
			// Check each other element ahead of this one
			if (fields[i]==fields[j]) {
				alert("Duplicate found: " + i + " equals " + j + ".\nValues were " + fields[i] + " and " + fields[j] + ".");
				return true;
			}
		}
	}
	var getstring;
	for (i = 1; i <= limit; i++) {
		getstring = getstring + "|" + fields[i];
	}
	
	var pathToFile = "include/edit_other_top.php?TYPE=" + type + "&LIST=" + getstring;
	ajax_loadContent('content',pathToFile);
}
		
function Show() {
	var pathToFile = "include/show.php";
	ajax_loadContent('content',pathToFile);
}

function ShowID(id) {
	if (id == "all") {
		var pathToFile = "include/show.php?TYPE=all";
		ajax_loadContent('content',pathToFile);
	} else if (id.length == 0 || id == undefined) {
	} else {
		var pathToFile = "include/show.php?ID=" + id;
		ajax_loadContent('content',pathToFile);
	}
}

function GoHome() {
	window.location="index.php";
}

function GoToType(type) {
	var pathToFile = "include/show.php?GROUP=" + type;
	ajax_loadContent('content',pathToFile);
}

function ShowMoreTop10() {
	var pathToFile = "include/top10.php";
	ajax_loadContent('content',pathToFile);
}
