var xmlHttp = createXmlHttpRequestObject();
var xmlHttp2 = createXmlHttpRequestObject();
var from = 0;

function createXmlHttpRequestObject() {
  var xmlHttp;
  if(window.ActiveXObject) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      xmlHttp = false;
    }
  } else {
    try {
      xmlHttp = new XMLHttpRequest();
    } catch(e) {
      xmlHttp = false;
    }
  }
  if(!xmlHttp) {
    alert("Error creating the XMLHttpRequest object.");
  } else {
    return xmlHttp;
  }
}

function refreshPics() {
  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    xmlHttp.open("GET", "js/pics.php?from=" + from, true);
    xmlHttp.onreadystatechange = handleServerResponse;
    xmlHttp.send(null);
  } else {
    setTimeout("refreshPics()", 100);
  }
}

function nextPics() {
  if((from + 8) < picCount) {
    from += 8;
  } else {
    from = 0;
  }
  refreshPics();
}

function prevPics() {
  if((from - 8) >= 0) {
    from -= 8;
    refreshPics();
  }
}

function handleServerResponse() {
  if(xmlHttp.readyState == 4) {
    if(xmlHttp.status == 200) {
      document.getElementById("pics-container")["innerHTML"] = xmlHttp.responseText;
      addOnLoad();
      $(function() {
          $('a[@rel*=lightbox]').lightBox();
      });
    } else {
      alert("There was a problem accessing the server: "+xmlHttp.statusText);
    }
  }
}

function sendContactMail() {
  if(xmlHttp2.readyState == 4 || xmlHttp2.readyState == 0) {
    xmlHttp2.open("GET", "js/mail.php?naam=" + $('naam')["value"] + "&bedrijf=" + $('bedrijf')["value"] + "&email=" + $('email')["value"] + "&bericht=" + $('bericht')["value"], true);
    xmlHttp2.onreadystatechange = handleServerResponse2;
    xmlHttp2.send(null);
  } else {
    setTimeout("sendContactMail()", 100);
  }
}

function handleServerResponse2() {
  if(xmlHttp2.readyState == 4) {
    if(xmlHttp2.status == 200) {
      var id = "move-container";
      if(xmlHttp2.responseText == "gelukt") {
        move(id, "left", -(238 * 2), 40, 15, "px"); // gelukt
      } else {
        move(id, "left", -(238 * 3), 40, 15, "px"); // mislukt
      }
    } else {
      alert("There was a problem accessing the server: "+xmlHttp2.statusText);
    }
  }
}

function showOverlay(id) {
  document.getElementById(id)["style"]["display"] = "block";
}
function hideOverlay(id) {
  document.getElementById(id)["style"]["display"] = "none";
}

function getElementsByClassName(classname) {
  var node = document.getElementsByTagName("body")[0];
  var a = [];
  var re = new RegExp('\\b' + classname + '\\b');
  var els = node.getElementsByTagName("*");
  for(var i=0,j=els.length; i<j; i++) {
    if(re.test(els[i].className))a.push(els[i]);
  }
  return a;
}

function addOnLoad() {
  var n = getElementsByClassName("photo");
  for(var i in n) {
    n[i].setAttribute("onload", "showMe(this);");
  }
}
addOnLoad();

function showMe(obj) {
  obj["style"]["visibility"] = "visible";
}

function move(who, what, where, step, interval, suffix, isLooping) {
  if(isLooping == undefined) var isLooping = false;
	if(suffix == undefined) var suffix = "";
	if(!isLooping && document.all && !window.opera) interval = interval/2;
	var obj = document.getElementById(who);
	var curr = obj["style"][what];
	curr = curr.replace("px","");
	curr = curr.replace(" ","");
	if(curr > Number(where)) {
		if(Number(curr)-Number(step) > where) {
			obj["style"][what] = Number(curr)-Number(step) + suffix;
			setTimeout("move('" + who + "', '" + what + "', " + where + ", " + step + ", " + interval + ", '" + suffix + "', true)", interval);
		} else {
			obj["style"][what] = where + suffix;
		}
	} else if(curr < Number(where)) {
		if(Number(curr)+Number(step) < where) {
			obj["style"][what] = Number(curr)+Number(step) + suffix;	
			setTimeout("move('" + who + "', '" + what + "', " + where + ", " + step + ", " + interval + ", '" + suffix + "', true)", interval);
		} else {
			obj["style"][what] = where + suffix;
		}
	}
}

function checkForm() {
  var id = "move-container";
  var ok = true;
  $('contact-form-one')["className"] = "";
  $('contact-form-two')["className"] = "";
  $('contact-form-three')["className"] = "";
  if($('naam')["value"] == "") {
    $('contact-form-one')["className"] = "red";
    ok = false;
  }
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if($('email')["value"] == "" || !filter.test($('email')["value"])) {
    $('contact-form-two')["className"] = "red";
    ok = false;
  }
  if($('bericht')["value"] == "") {
    $('contact-form-three')["className"] = "red";
    ok = false;
  }
  if(ok) {
    sendContactMail();
  } else {
    move(id, "left", -(238 * 1), 40, 15, "px"); // onvolledig
  }
}

function backToForm() {
  var id = "move-container";
  move(id, "left", 0, 40, 15, "px");
}