/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function ajaxEmailFeature(id) {
	var email = document.getElementById('submit_email_addr').value;
	if (!isValidEmail(email)) {
		alert("Please enter a valid email address!");
	}
	else {
		var myConn = new XHConn();
		document.getElementById('submit_email_btn').value = 'Please wait...';
		document.getElementById('submit_email_btn').disabled = true;
		myConn.connect('send_feature.asp', 'GET', 'id='+id+'&email='+escape(email), ajaxEmailFeature_Done);
	}
	return false;
}

function ajaxEmailFeature_Done(response) {
	document.getElementById('submit_email_form').innerHTML = response.responseText;
}

function ajaxEmailTheme(id) {
	var email = document.getElementById('submit_email_addr').value;
	if (!isValidEmail(email)) {
		alert("Please enter a valid email address!");
	}
	else {
		var myConn = new XHConn();
		document.getElementById('submit_email_btn').value = 'Please wait...';
		document.getElementById('submit_email_btn').disabled = true;
		myConn.connect('send_theme.asp', 'GET', 'id='+id+'&email='+escape(email), ajaxEmailTheme_Done);
	}
	return false;
}

function ajaxEmailTheme_Done(response) {
	document.getElementById('submit_email_form').innerHTML = response.responseText;
}

function ajaxEmailFeatureLb(id) {
	var email = document.getElementById('submit_email_addr').value;
	if (!isValidEmail(email)) {
		alert("Please enter a valid email address!");
	}
	else {
		var myConn = new XHConn();
		document.getElementById('submit_email_btn').value = 'Please wait...';
		document.getElementById('submit_email_btn').disabled = true;
		myConn.connect('send_lightbox.asp', 'GET', 'id='+id+'&email='+escape(email), ajaxEmailFeatureLb_Done);
	}
	return false;
}

function ajaxEmailFeatureLb_Done(response) {
	document.getElementById('submit_email_form').innerHTML = response.responseText;
}

function ajaxAddLightbox() {
	var id = document.boxfrm.addpicid.value;
	var isSelected = false;
	if (id > 0) {
		vars = 'id='+id+'&box=';
		for (i = 0; i < document.boxfrm.length; i++) {
			var tempobj = document.boxfrm.elements[i];
			if (tempobj.type.toLowerCase() == "checkbox" && tempobj.checked == true) {
				vars = vars + tempobj.name;
				isSelected = true;
			}
		}
		if (isSelected) {
			var myConn = new XHConn();
			myConn.connect('add_to_lightbox.asp', 'GET', vars, ajaxAddLightbox_Done);
		}
		else {
			hideBox();
		}
	}
	return false;
}

function ajaxAddLightbox_Done(response) {
	hideBox();
}

function ajaxCreateLightbox() {
	var name = document.newboxfrm.name.value;
	if (name !== "" && name !== "Enter name...") {
		var myConn = new XHConn();
		myConn.connect('create_lightbox.asp', 'GET', 'name='+escape(name), ajaxCreateLightbox_Done);
	}
	return false;
}

function ajaxCreateLightbox_Done(response) {
	document.getElementById('checkboxlist').innerHTML += response.responseText;
	document.newboxfrm.name.value = "Enter name...";
}

function ajaxRemoveImage(id, box) {
	var myConn = new XHConn();
	myConn.connect('remove_lightpic.asp', 'GET', 'id='+id+'&lb='+box, ajaxRemoveImage_Done);
	return false;
}

function ajaxRemoveImage_Done(response) {
	document.getElementById('imageblock'+response.responseText).style.display = 'none';
}