//Load the colors
function loadColors(id, img, divID, highlight) {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
    if (xhr.overrideMimeType) {
      xhr.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }

  var obj = document.getElementById(divID);
  var lText = img.src;
  var colors = 0;
  
  //document.body.style.cursor = "wait";
  
  colors = (lText.substring(lText.length-10,lText.length) == 'expand.gif') ? 0 : 22;
  
  //xhr = new XMLHttpRequest();
  if (xhr) {
    var qs = "/schemes/scheme.php?id="+id+"&c="+colors;
    if (highlight!='') qs = qs + "&h=" + highlight
    
    xhr.open("GET", qs, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4 && xhr.status == 200) {
        obj.innerHTML = xhr.responseText;
        
        img.src = (lText.substring(lText.length-10,lText.length) == 'expand.gif') ? '/images/collapse.gif' : '/images/expand.gif';
        //document.body.style.cursor = "default";
      }
    }
    xhr.send(null);
  }
}

//Rate a scheme
function rateScheme(id, rating, divID) {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
    if (xhr.overrideMimeType) {
      xhr.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  
  var obj = document.getElementById(divID);
  
  if (xhr) {
    var qs = "/schemes/rate.php?id="+id+"&rating="+rating;
    
    xhr.open("GET", qs, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4 && xhr.status == 200) {
        obj.innerHTML = xhr.responseText;
      }
    }
    xhr.send(null);
  }
}

//Schemes Upload Checker
function check(tform)	{
  var isempty = true;
  
  for (var i=0; i < tform.elements.length; i++)	{
  	var elem = tform.elements[i];
  	if (elem.type == 'file') {
  		if (elem.value != "")	{
  			isempty = false;
          
          var ext = elem.value;
          ext = ext.substring(ext.length-2,ext.length);
          ext = ext.toLowerCase();
          
          if (ext!='cs') {
            alert("\""+elem.value+"\" is not a valid Color Schemer file!\n\nPlease select a valid Color Schemer file (*.cs) to upload!");
  	      return false;
          }
  		}
  	}
  }
  
  if (isempty) {
    alert("Please select a file to upload using the \"Browse...\" button.");
  	return false;
  } else {
    return true;
  }
}

function HighlightRow(input) {
  if(input.value>0)
    input.parentNode.parentNode.style.fontWeight = 'bold';
  else
    input.parentNode.parentNode.style.fontWeight = 'normal';
}

function open_window(url,id,width,height)
{
  popup = window.open(url,id,'toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=no,width=' + width + ',height=' + height + '' );
  popup.focus();
}

// ------------------------------------------------
// Based on StyleSwitcher functions by Paul Sowden
// http://www.idontsmoke.co.uk/ss/
// - - - - - - - - - - - - - - - - - - - - - - - -
// For details, visit ALA:
// http://www.alistapart.com/stories/alternate/
// ------------------------------------------------

var defaultBackgroundColor = "blue";
var currentBackgroundColor;

/*function setBackgroundColor(color) {
  if(color == "orange") {
    document.body.style.backgroundColor = "#E8B36D";
    document.body.style.backgroundImage = "url(/images/bg_center_orange.gif)";
  }
  else if(color == "pink") {
    document.body.style.backgroundColor = "#E58A99";
    document.body.style.backgroundImage = "url(/images/bg_center_pink.gif)";
  }
  else if(color == "blue") {
    document.body.style.backgroundColor = "#73C5E5";
    document.body.style.backgroundImage = "url(/images/bg_center_blue.gif)";
  }
  else if(color == "slate") {
    document.body.style.backgroundColor = "#BABAAB";
    document.body.style.backgroundImage = "url(/images/bg_center_slate.gif)";
  }
  
  currentBackgroundColor = color;
}*/

function setBackgroundColor(color) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      /*a.disabled = !(a.getAttribute("title") == color);
      if(a.getAttribute("title") == color)
        alert(a.getAttribute("title"));*/
      
      a.disabled = true;
      if(a.getAttribute("title") == color) a.disabled = false;
    }
  }
  
  currentBackgroundColor = color;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

/*window.onload = function(e) {
  var cookie = readCookie("backgroundColor");
  var bgColor = cookie ? cookie : defaultBackgroundColor;
  setBackgroundColor(bgColor);
}*/

window.onunload = function(e) {
  createCookie("backgroundColor", currentBackgroundColor, 365);
}

var cookie = readCookie("backgroundColor");
var bgColor = cookie ? cookie : defaultBackgroundColor;
setBackgroundColor(bgColor);
