function addLoadListener(fn) {
  if(typeof window.addEventListener != 'undefined') {
    window.addEventListener('load', fn, false);
  } else if(typeof document.addEventListener != 'undefined') {
    document.addEventListener('load', fn, false);
  } else if(typeof window.attachEvent != 'undefined') {
    window.attachEvent('onload', fn);
  } else {
    var oldfn = window.onload;
    if(typeof window.onload != 'function') {
      window.onload = fn;
    } else {
      window.onload = function() {
        oldfn();
        fn();
      };
    }
  }
}

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return i;
		}
	}
	return false;
}

function genForms() {
  var numForms = document.getElementById('pictures').value;
  ajax('/cms/photoUpload.php?num='+numForms, 'pictureInputs');
}

function selectAll(id,selectAll) {
  var selectBox = document.getElementById(id);
	// have we been passed an ID
	if (typeof selectBox == "string") {
		selectBox = document.getElementById(selectBox);
	}
	// is the select box a multiple select box?
	if (selectBox.type == "select-multiple") {
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = selectAll;
		}
	}
}

function ajax(url,target) {
  try {
    req = new XMLHttpRequest();
  } catch(error) {
    try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(error) {
      req = null;
    }
  }
  req.onreadystatechange = function() {ajaxDone(target);};
  req.open("GET", url, true);
  req.send(null);
}

function ajaxDone(target) {
  if (req.readyState < 4 ) {
      document.getElementById(target).innerHTML = '<img src="/images/ajax-loader.gif" alt="Loading..." />';
  } else if (req.readyState == 4) {
    if (req.status == 200 || req.status == 304) {
      results = req.responseText;
      document.getElementById(target).innerHTML = results;
    } else {
      document.getElementById(target).innerHTML="ajax error:\n" +
      req.statusText;
    }
  }
}

function moveDown(id) {
  var options = document.getElementById(id);
  var selected = [];
  var selectPara = [];
  var selectPara2 = [];
  var newLineup = [];
  var newLineupPara = [];
  for(var i = 0; i < options.length; i++) {
    if(options[i].selected) {
      selected[selected.length] = i;
      selectPara[selectPara.length] = options[i].value;
      selectPara2[selectPara2.length] = options[i].innerHTML;
    }
  }
  for(var i = 0; i < options.length; i++) {
    if(selected.in_array(i-1) !== false) {
      newLineup[newLineup.length] = selectPara[selected.in_array(i-1)];
      newLineupPara[newLineupPara.length] = selectPara2[selected.in_array(i-1)];
    } else if(selected.in_array(i) !== false && i != options.length-1) {
      newLineup[newLineup.length] = options[i+1].value;
      newLineupPara[newLineupPara.length] = options[i+1].innerHTML;
    } else {
      newLineup[newLineup.length] = options[i].value;
      newLineupPara[newLineupPara.length] = options[i].innerHTML;
    }
  }
  options.innerHTML = "";
  for(var i = 0; i < newLineup.length; i++) {
    var option = document.createElement('option');
    option.value = newLineup[i];
    option.innerHTML = newLineupPara[i];
    options.appendChild(option);
  }
  return false;
}

function moveUp(id) {
  var options = document.getElementById(id);
  var selected = [];
  var selectPara = [];
  var selectPara2 = [];
  var newLineup = [];
  var newLineupPara = [];
  for(var i = 0; i < options.length; i++) {
    if(options[i].selected) {
      selected[selected.length] = i;
      selectPara[selectPara.length] = options[i].value;
      selectPara2[selectPara2.length] = options[i].innerHTML;
    }
  }
  for(var i = 0; i < options.length; i++) {
    if(selected.in_array(i+1) !== false) {
      newLineup[newLineup.length] = selectPara[selected.in_array(i+1)];
      newLineupPara[newLineupPara.length] = selectPara2[selected.in_array(i+1)];
    } else if(selected.in_array(i) !== false && i != 0) {
      newLineup[newLineup.length] = options[i-1].value;
      newLineupPara[newLineupPara.length] = options[i-1].innerHTML;
    } else {
      newLineup[newLineup.length] = options[i].value;
      newLineupPara[newLineupPara.length] = options[i].innerHTML;
    }
  }
  options.innerHTML = "";
  for(var i = 0; i < newLineup.length; i++) {
    var option = document.createElement('option');
    option.value = newLineup[i];
    option.innerHTML = newLineupPara[i];
    options.appendChild(option);
  }
  return false;
}
