var currCircle = 1;
var newsDetailOpen = 0;
var articleDetailOpen = 0;
var brandsIndex = 0;
var newsIndex = 0;
var articlesIndex = 0;
var brandFormIndex = 0;

function moveHeaderText() {
  var itemSize = 200;
	var id = "header-text-container";
	var position = $(id)["style"]["left"].replace("px","");
	if(position - itemSize > -(nrOfHeaderTextItems * itemSize)) {
		position -= itemSize;
		setActiveCircle(currCircle + 1);
		move(id, "left", position, 10, 15, "px");
	} else {
		position = 0;
		setActiveCircle(1);
		move(id, "left", position, 20, 15, "px");
	}
}
function moveCirclePosition(i) {
  var itemSize = 200;
	var position = -((i-1) * itemSize);
	setActiveCircle(i);
	move('header-text-container', "left", position, 20, 15, "px");
	clearInterval(moveInt);
	moveInt = setInterval("moveHeaderText()", 5000);
}
function setActiveCircle(i) {
  for(var j = 1; j <= nrOfHeaderTextItems; j++) {
	  $('text-control' + j)["src"] = "images/circle-inactive.png";
	}
	$('text-control' + i)["src"] = "images/circle-active.png";
	currCircle = i;
}
function moveBrandsLeft() {
  var itemSize = 950;
	var id = "brand-container";
	if(brandsIndex-1 > -nrOfBrandItems) {
	  brandsIndex--;
		move(id, "left", brandsIndex * itemSize, 20, 15, "px");
	} else {
	  brandsIndex = 0;
		move(id, "left", 0, 40, 15, "px");
	}
}
function moveBrandsRight() {
  var itemSize = 950;
	var id = "brand-container";
	if(brandsIndex + 1 <= 0) {
	  brandsIndex++;
		move(id, "left", brandsIndex * itemSize, 20, 15, "px");
	} else {
	  brandsIndex = -(nrOfBrandItems-1);
		move(id, "left", brandsIndex * itemSize, 40, 15, "px");
	}
}
function moveNewsUp() {
  if(newsDetailOpen != 0) toggleNewsDetail(newsDetailOpen);
  var itemSize = 171;
	var id = "news-container";
	if(newsIndex-1 > -(nrOfNewsItems-3)) {
	  newsIndex--;
		move(id, "top", newsIndex * itemSize, 20, 15, "px");
	}
}
function moveNewsDown() {
  if(newsDetailOpen != 0) toggleNewsDetail(newsDetailOpen);
  var itemSize = 171;
	var id = "news-container";
	if(newsIndex + 1 <= 0) {
		newsIndex++;
		move(id, "top", newsIndex * itemSize, 20, 15, "px");
	}
}
function toggleNewsDetail(i, id) {
  var itemSize = 171;
  if(newsDetailOpen == 0) {
    move("news-container", "top", -(i * itemSize), 20, 15, "px");
    move("news-item-" + id, "height", 650, 20, 15, "px");
    //window.location.hash = "newsanchor";
    newsDetailOpen = id;
  } else {
    move("news-container", "top", newsIndex * itemSize, 20, 15, "px");
    move("news-item-" + id, "height", 105, 20, 15, "px");
    newsDetailOpen = 0;
  }
}
function moveArticlesUp() {
  if(articleDetailOpen != 0) toggleArticleDetail(articleDetailOpen);
  var itemSize = 360;
	var id = "article-container";
	if(articlesIndex-1 > -(nrOfArticleItems-1)) {
	  articlesIndex--;
		move(id, "top", articlesIndex * itemSize, 20, 15, "px");
	}
}
function moveArticlesDown() {
  if(articleDetailOpen != 0) toggleArticleDetail(articleDetailOpen);
  var itemSize = 360;
	var id = "article-container";
	if(articlesIndex + 1 <= 0) {
		articlesIndex++;
		move(id, "top", articlesIndex * itemSize, 20, 15, "px");
	}
}
function toggleArticleDetail(i, id) {
  var itemSize = 360;
  if(articleDetailOpen == 0) {
    move("article-container", "top", -(i * itemSize), 20, 15, "px");
    move("article-item-" + id, "height", 650, 20, 15, "px");
    //window.location.hash = "articleanchor";
    articleDetailOpen = id;
  } else {
    move("article-container", "top", articlesIndex * itemSize, 20, 15, "px");
    move("article-item-" + id, "height", 105, 20, 15, "px");
    articleDetailOpen = 0;
  }
}
function moveBrandFormLeft() {
  var nrOfItems = 3;
  var itemSize = 340;
  var id = "column-contact-container";
	if(brandFormIndex-1 > -nrOfItems) {
	  brandFormIndex--;
		move(id, "left", brandFormIndex * itemSize, 20, 15, "px");
	} else {
	  brandFormIndex = 0;
		move(id, "left", 0, 40, 15, "px");
	}
}
function moveBrandFormRight() {
	var nrOfItems = 3;
  var itemSize = 340;
  var id = "column-contact-container";
	if(brandFormIndex + 1 <= 0) {
	  brandFormIndex++;
		move(id, "left", brandFormIndex * itemSize, 20, 15, "px");
	} else {
	  brandFormIndex = -(nrOfItems-1);
		move(id, "left", brandFormIndex * itemSize, 40, 15, "px");
	}
}
function checkBrandFormTwo() {
  var ok = true;
  $('message2')["style"]["display"] = "none";
  $('brand-form-two-two')["className"] = "form";
  if($('adults')["selectedIndex"] == 0 && $('children')["selectedIndex"] == 0) {
    $('brand-form-two-two')["className"] = "formred";
    ok = false;
  }
  if(!ok) {
    $('message2')["style"]["display"] = "block";
    return;
  }
  moveBrandFormLeft();
}

function checkBrandFormThree() {
  var ok = true;
  $('message3')["style"]["display"] = "none";
  $('brand-form-three-one')["className"] = "label-container";
  $('brand-form-three-two')["className"] = "label-container";
  $('brand-form-three-three')["className"] = "label-container";
  $('brand-form-three-four')["className"] = "label-container";
  $('brand-form-three-five')["className"] = "label-container";
  $('brand-form-three-six')["className"] = "label-container";
  $('brand-form-three-seven')["className"] = "label-container";
  if($('first name')["value"] == "") {
    $('brand-form-three-one')["className"] = "label-container-red";
    ok = false;
  }
  if($('last name')["value"] == "") {
    $('brand-form-three-two')["className"] = "label-container-red";
    ok = false;
  }
  if($('street')["value"] == "" || $('housenumber')["value"] == "") {
    $('brand-form-three-three')["className"] = "label-container-red";
    ok = false;
  }
  if($('areacode')["value"] == "" || $('areacode2')["value"] == "") {
    $('brand-form-three-four')["className"] = "label-container-red";
    ok = false;
  }
  if($('city')["value"] == "") {
    $('brand-form-three-five')["className"] = "label-container-red";
    ok = false;
  }
  var numlength = $('netnumber')["value"]["length"] + $('phonenumber')["value"]["length"];
  if($('netnumber')["value"] == "" || $('phonenumber')["value"] == "" || numlength != 10) {
    $('brand-form-three-six')["className"] = "label-container-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"])) {
    $('brand-form-three-seven')["className"] = "label-container-red";
    ok = false;
  }
  if(!ok) {
    $('message3')["style"]["display"] = "block";
    return;
  }
  $('brandform').submit();
}

function checkAdvertForm() {
  var ok = true;
  $('message3')["style"]["display"] = "none";
  $('advert-form-two')["className"] = "label-container";
  $('advert-form-three')["className"] = "label-container";
  $('advert-form-four')["className"] = "label-container";
  $('advert-form-five')["className"] = "label-container";
  $('advert-form-six')["className"] = "label-container";
  if($('name')["value"] == "") {
    $('advert-form-two')["className"] = "label-container-red";
    ok = false;
  }
  var numlength = $('netnumber')["value"]["length"] + $('phonenumber')["value"]["length"];
  if($('netnumber')["value"] == "" || $('phonenumber')["value"] == "" || numlength != 10) {
    $('advert-form-three')["className"] = "label-container-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"])) {
    $('advert-form-four')["className"] = "label-container-red";
    ok = false;
  }
  if($('website')["value"] == "") {
    $('advert-form-five')["className"] = "label-container-red";
    ok = false;
  }
  if($('captcha')["value"] == "") {
    $('advert-form-six')["className"] = "label-container-red";
    ok = false;
  }
  if(!ok) {
    $('message3')["style"]["display"] = "block";
    return;
  }
  $('advertform').submit();
}

function checkDokterForm() {
  var ok = true;
  $('message3')["style"]["display"] = "none";
  $('dokter-form-one')["className"] = "label-container";
  $('dokter-form-two')["className"] = "label-container";
  $('dokter-form-three')["className"] = "label-container";
  $('dokter-form-four')["className"] = "label-container";
  $('dokter-form-five')["className"] = "label-container";
  $('dokter-form-six')["className"] = "label-container";
  $('dokter-form-seven')["className"] = "label-container";
  if($('name')["value"] == "") {
    $('dokter-form-one')["className"] = "label-container-red";
    ok = false;
  }
  if($('street')["value"] == "") {
    $('dokter-form-two')["className"] = "label-container-red";
    ok = false;
  }
  if($('housenumber')["value"] == "") {
    $('dokter-form-two')["className"] = "label-container-red";
    ok = false;
  }
  if($('areacode')["value"] == "") {
    $('dokter-form-three')["className"] = "label-container-red";
    ok = false;
  }
  if($('areacode2')["value"] == "") {
    $('dokter-form-three')["className"] = "label-container-red";
    ok = false;
  }
  if($('city')["value"] == "") {
    $('dokter-form-four')["className"] = "label-container-red";
    ok = false;
  }
  var numlength = $('netnumber')["value"]["length"] + $('phonenumber')["value"]["length"];
  if($('netnumber')["value"] == "" || $('phonenumber')["value"] == "" || numlength != 10) {
    $('dokter-form-five')["className"] = "label-container-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"])) {
    $('dokter-form-six')["className"] = "label-container-red";
    ok = false;
  }
  if($('captcha')["value"] == "") {
    $('dokter-form-seven')["className"] = "label-container-red";
    ok = false;
  }
  if(!ok) {
    $('message3')["style"]["display"] = "block";
    return;
  }
  $('dokterform').submit();
}

function checkOpnameForm() {
  var ok = true;
  $('message3')["style"]["display"] = "none";
  $('opname-form-two')["className"] = "label-container";
  $('opname-form-three')["className"] = "label-container";
  $('opname-form-four')["className"] = "label-container";
  if($('name')["value"] == "") {
    $('opname-form-two')["className"] = "label-container-red";
    ok = false;
  }
  var numlength = $('netnumber')["value"]["length"] + $('phonenumber')["value"]["length"];
  if($('netnumber')["value"] == "" || $('phonenumber')["value"] == "" || numlength != 10) {
    $('opname-form-three')["className"] = "label-container-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"])) {
    $('opname-form-four')["className"] = "label-container-red";
    ok = false;
  }
  if(!ok) {
    $('message3')["style"]["display"] = "block";
    return;
  }
  $('opnameform').submit();
}

function checkFinanceForm() {
  var ok = true;
  $('message3')["style"]["display"] = "none";
  $('finance-form-two')["className"] = "label-container";
  $('finance-form-three')["className"] = "label-container";
  $('finance-form-four')["className"] = "label-container";
  $('finance-form-five')["className"] = "label-container";
  $('finance-form-six')["className"] = "label-container";
  $('finance-form-seven')["className"] = "label-container";
  $('finance-form-eight')["className"] = "label-container";
  if($('name')["value"] == "") {
    $('finance-form-two')["className"] = "label-container-red";
    ok = false;
  }
  if($('street')["value"] == "") {
    $('finance-form-three')["className"] = "label-container-red";
    ok = false;
  }
  if($('housenumber')["value"] == "") {
    $('finance-form-three')["className"] = "label-container-red";
    ok = false;
  }
  if($('areacode')["value"] == "") {
    $('finance-form-four')["className"] = "label-container-red";
    ok = false;
  }
  if($('areacode2')["value"] == "") {
    $('finance-form-four')["className"] = "label-container-red";
    ok = false;
  }
  if($('city')["value"] == "") {
    $('finance-form-five')["className"] = "label-container-red";
    ok = false;
  }
  var numlength = $('netnumber')["value"]["length"] + $('phonenumber')["value"]["length"];
  if($('netnumber')["value"] == "" || $('phonenumber')["value"] == "" || numlength != 10) {
    $('finance-form-seven')["className"] = "label-container-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"])) {
    $('finance-form-eight')["className"] = "label-container-red";
    ok = false;
  }
  if(!ok) {
    $('message3')["style"]["display"] = "block";
    return;
  }
  $('financeform').submit();
}

function checkContactForm() {
  var ok = true;
  $('message3')["style"]["display"] = "none";
  $('contact-form-one')["className"] = "label-container";
  $('contact-form-two')["className"] = "label-container";
  $('contact-form-three')["className"] = "label-container";
  if($('name')["value"] == "") {
    $('contact-form-one')["className"] = "label-container-red";
    ok = false;
  }
  var numlength = $('netnumber')["value"]["length"] + $('phonenumber')["value"]["length"];
  if($('netnumber')["value"] == "" || $('phonenumber')["value"] == "" || numlength != 10) {
    $('contact-form-two')["className"] = "label-container-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-three')["className"] = "label-container-red";
    ok = false;
  }
  var safeFiles = ["doc","docx","pdf","txt"];
  if($('bijlage')["value"] != "") {
    var fileOk = false;
    var bits = $('bijlage')["value"].split(".");
    for(var i in safeFiles) {
      if(bits[bits["length"]-1] == safeFiles[i]) fileOk = true;
    }
    if(!fileOk) {
      ok = false;
      alert("Bijlage 1 bevat een ongeldig bestandstype: " + bits[bits["length"]-1]);
    }
  }
  if($('bijlage2')["value"] != "") {
    var fileOk = false;
    var bits = $('bijlage2')["value"].split(".");
    for(var i in safeFiles) {
      if(bits[bits["length"]-1] == safeFiles[i]) fileOk = true;
    }
    if(!fileOk) {
      ok = false;
      alert("Bijlage 2 bevat een ongeldig bestandstype: " + bits[bits["length"]-1]);
    }
  }
  if($('bijlage3')["value"] != "") {
    var fileOk = false;
    var bits = $('bijlage3')["value"].split(".");
    for(var i in safeFiles) {
      if(bits[bits["length"]-1] == safeFiles[i]) fileOk = true;
    }
    if(!fileOk) {
      ok = false;
      alert("Bijlage 3 bevat een ongeldig bestandstype: " + bits[bits["length"]-1]);
    }
  }
  if(!ok) {
    $('message3')["style"]["display"] = "block";
    return;
  }
  $('contactform').submit();
}

/* GLOBAL */
function $(id) {
	return document.getElementById(id);
}
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 = $(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;
		}
	}
}

/* MENU */
var timeoutMenuInt = 0;
var switchMenuInt = 2;
var autoCloseMenuSet = false;
var menuActive = "";

function autoCloseMenuTimeout() {
  if(!autoCloseMenuSet) {
    timeoutMenuInt = setTimeout("autoCloseMenu()", 1000);
    autoCloseMenuSet = true;
  }
}

function stopAutoCloseMenuTimeout() {
  clearTimeout(timeoutMenuInt);
  autoCloseMenuSet = false;
}

function autoCloseMenu() {
  closeMenu();
  autoCloseMenuSet = false;
}

function openMenu(which) {
  stopAutoCloseMenuTimeout();
  if(menuActive != which) {
    closeMenu();
    menuActive = which;
    $(menuActive)["style"]["display"] = "block";
  }
}

function closeMenu() {
  if(menuActive != "") {
    $(menuActive)["style"]["display"] = "none";
    menuActive = '';
  }
}
//////////////////////////////////////////////////////////////////////////////////////////////
// create a tag around selected text
//////////////////////////////////////////////////////////////////////////////////////////////
function makeTag(id, tag) {
  var obj = $(id);
  var sl = '';
  if(obj.setSelectionRange) {
    obj["value"] = (obj["value"]).substring(0, obj["selectionStart"]) + '['+tag+']' + (obj["value"]).substring(obj["selectionStart"], obj["selectionEnd"]) + '[/'+tag+']' + (obj["value"]).substring(obj["selectionEnd"]);
  } else if(document["selection"]) {
    var range = document["selection"].createRange();
    var stored_range = range.duplicate();
    stored_range.moveToElementText(obj);
    stored_range.setEndPoint('EndToEnd', range);
    var start = stored_range["text"]["length"] - range["text"]["length"];
	var end = start + range["text"]["length"];
	  obj["value"] = (obj["value"]).substring(0, start) + '['+tag+']' + (obj["value"]).substring(start, end) + '[/'+tag+']' + (obj["value"]).substring(end);
  }  
  if(document["selection"]) {
    document["selection"].empty();
    obj.blur();
  } else {
    window.getSelection().removeAllRanges();
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////
// create a link around selected text and prompt for link location
//////////////////////////////////////////////////////////////////////////////////////////////
function makeLink(id) {
  var obj = $(id);
  var sl = '';
  if(obj.setSelectionRange) {
    var url = prompt("Naar welke website moet de link verwijzen?","http://");
    if(url != null) {
      var alt = prompt("Title tag (optioneel)","");
      if(alt != null && alt != "") {
        obj["value"] = (obj["value"]).substring(0, obj["selectionStart"]) + '[link=' + url + '][alt=' + alt + ']' + (obj["value"]).substring(obj["selectionStart"], obj["selectionEnd"]) + '[/alt][/link]' + (obj["value"]).substring(obj["selectionEnd"]);
      } else {
        obj["value"] = (obj["value"]).substring(0, obj["selectionStart"]) + '[link=' + url + ']' + (obj["value"]).substring(obj["selectionStart"], obj["selectionEnd"]) + '[/link]' + (obj["value"]).substring(obj["selectionEnd"]);
      }
    }
  } else if(document["selection"]) {
    var range = document["selection"].createRange();
    var stored_range = range.duplicate();
    stored_range.moveToElementText(obj);
    stored_range.setEndPoint('EndToEnd', range);
    var start = stored_range["text"]["length"] - range["text"]["length"];
	  var end = start + range["text"]["length"];
	  var url = prompt("Naar welke website moet de link verwijzen?","http://");
    if(url != null) {
      obj["value"] = (obj["value"]).substring(0, start) + '[link='+url+']' + (obj["value"]).substring(start, end) + '[/link]' + (obj["value"]).substring(end);
    }
  }
  if(document["selection"]) {
    document["selection"].empty();
    obj.blur();
  } else {
    window.getSelection().removeAllRanges();
  }
}

function showDealer(id) {
  for(var i = 1; i <= nrOfDealers; i++) {
    $('dealer-' + i)["style"]["display"] = "none";
  }
  $('dealer-' + id)["style"]["display"] = "block";
}

var dealerInt;
function startDealerListLeftInterval() {
  dealerInt = setInterval("moveDealerListLeft();", 10);
}
function stopDealerListLeftInterval() {
  clearInterval(dealerInt);
}
function startDealerListRightInterval() {
  dealerInt = setInterval("moveDealerListRight();", 10);
}
function stopDealerListRightInterval() {
  clearInterval(dealerInt);
}
function moveDealerListLeft() {
  if(dealerListWidth > 380) {
    var l = Number($('dealer-container')["style"]["left"].split("px")[0]);
    if(l < 0) {
  	  $('dealer-container')["style"]["left"] = (l + 5) + "px";
  	} else {
  	  $('dealer-container')["style"]["left"] = 0 + "px";
      clearInterval(dealerInt);
    }
  }
}
function moveDealerListRight() {
  if(dealerListWidth > 380) {
    var l = Number($('dealer-container')["style"]["left"].split("px")[0]);
    var d = -( Math.round((dealerListWidth - 380) / 5) * 5);
    if(l > d) {
    	$('dealer-container')["style"]["left"] = (l - 5) + "px";
    } else {
  	  $('dealer-container')["style"]["left"] = d + "px";
      clearInterval(dealerInt);
    }
  }
}
function calculateDealerListWidth() {
  var w = 0;
  var elements = $('dealer-container').getElementsByTagName("ol")[0].getElementsByTagName("li");
  for(var i in elements) {
    var obj = elements[i];
		var objWStr = "";
		var objW = 0;
		if(obj["style"] && obj["style"]["pixelWidth"]) {
	  	objWStr = String(obj["style"]["pixelWidth"]);
	  } else {
	 		objWStr = String(obj["offsetWidth"]);
	 	}
	 	objW = Number(objWStr.split("px")[0]);
	 	if(!isNaN(objW)) w += objW + 18;
  }
  return w;
}
