document.write("<style type=\"text/css\" media=\"screen\">.distributor {display: none;}</style>")

try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


window.onload = function() {

	if ($("hear_about_us") != null) {
		new Event.observe("hear_about_us", "change", handle_hear_about_us);
		handle_hear_about_us();
	}

	if (document.cookie) {
		scalePage(read_cookie('size'));
	}

	if (/MSIE/.test(navigator.userAgent)) {
		activateHoverNavi();
		fixHr(); 
	}
	getNewWindowLinks();
  new PrepopulateFields();
}


function scalePage(value) {
  if (!value) { value = '75'; }
  myHtml = document.getElementById('thishtml');
  myBody = document.getElementsByTagName('BODY')[0];
  
  if (/MSIE 6/.test(navigator.userAgent)) {
	  myHtml.style.fontSize = value + "%";
  } else {
  	pixels = value * 0.16;
  	myBody.style.fontSize = pixels + "px";
  }
  
  update_cookie(value);
}

function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
}

function read_cookie(name) {
  var ar = document.cookie.split(';');
  for(var i=0; i<ar.length; i++) {
    var c = ar[i].split('=');
    if (trim(c[0]) == name) {
      return c[1];
    }
  }
  return null;
}

function update_cookie(size) {
  var ablauf = new Date();
  var infuenfTagen = ablauf.getTime() + (10 * 24 * 60 * 60 * 1000);
  ablauf.setTime(infuenfTagen);
  document.cookie = "size="+size+"; path=/; expires=" + ablauf.toGMTString();
}


/* ----------------------------------- */

var handle_hear_about_us = function(){
	if ( ($("hear_about_us").getValue() == "Search Engine") || 
		($("hear_about_us").getValue() == "Directed from other Website") ||
		($("hear_about_us").getValue() == "Other") ) {
		new Element.show($("hear_about_us_other").parentNode)
	}else{
		new Element.hide($("hear_about_us_other").parentNode)
	}
}


/* ----------------------------------- */

function show_response(response) {
  $('livesearch_matches').style.display='block';
	Element.update('livesearch_matches', response.responseText);
}


function popup (myElem, width, height, left, top, scrollbars, status, toolbar) {
myUrl = myElem.href;
myFeatures = 'width='+width+', height='+height+', left='+left+', top='+top+', scrollbars='+scrollbars+', status='+status+', toolbar='+toolbar;
window.open(myUrl, 'abdserotec', myFeatures);
}

var DistributorsSearch = Class.create();
DistributorsSearch.prototype = {
	initialize: function(){
		if (!(/MSIE 5/.test(navigator.userAgent))) {
		var parent = this;

		this.distributors = document.getElementsByClassName("distributor", "content");

		var i = 0;
		this.distributors.each(function(distributor){
			distributor.hide();
			distributor.id = i++;
		});

		var distributors_select = document.createElement("select");
		var distributor_empty = document.createElement("option");
		distributor_empty.innerHTML = "--------";
		distributor_empty.value = "--";

		distributors_select.appendChild(distributor_empty);
		for(var i=0; i < this.distributors.length; i++){
			var image;
			var distributor_option = document.createElement("option");
			distributor_option.value = i;
			distributor_option.innerHTML = this.distributors[i].getElementsByTagName("h2")[0].innerHTML;
			distributors_select.appendChild(distributor_option);
			if(images = this.distributors[i].getElementsByTagName("img")){
				for(var j=0;j<images.length; j++){
					images[j].parentNode.className += "logo";
				}
			}
		}

		var distributors_select_label = $('distributors_select').firstChild;
		var distributors_select_select = distributors_select_label.insertBefore(distributors_select, distributors_select_label.firstChild);

		var distributors_select_select_function = function(){
			if(this.options[this.options.selectedIndex].value != "--"){
				parent.distributors.each(function(distributor){
					distributor.style.display = "none";
				});
				$(this.options[this.options.selectedIndex].value).style.display = "block";
			}
		}
		distributors_select_select.onchange = distributors_select_select_function;
		distributors_select_select.onkeyup = 	distributors_select_select_function;
	}else{
		var divs = document.getElementsByTagName("div");
		for(var i= 0; i<divs.length; i++){
			if(typeof(divs[i].className) != "undefind" &&
				divs[i].className == "distributor"){
				divs[i].style.display="block";
			}
		}
	}
	}
}

var activateHoverNavi = function(){
	
	var getUl = function(li){
		if (li.nodeType != 3){
			return li;
		} else {
			return li.nextSibling;
		}
	}
	var mainNavi = $('main-navi');
	
	$A(mainNavi.childNodes).each(function(li) {
		if(li.nodeType == 1){
			if (li.id.length > 0) {
				var myUl = getUl(li.firstChild.nextSibling);
				li.onmouseover = function () {
						hideSelect();
						Element.addClassName(li, "hovered");
						myUl.style.display = "block";
						
				}

				li.onmouseout = function () {
						showSelect();
						Element.removeClassName(li, "hovered");
						myUl.style.display= "none";
				}
			}
		}
	});
}

var searchPopUp = function(){
	var pop = function(){
			var options = myFeatures = 'width=570, height=600, top=20, left=20, scrollbars=yes, status=false, resizeable=true';
      window.open(root_path()+"/popup.html", this.href.split('#').last(), options);
      return false;
  }
  if (/MSIE/.test(navigator.userAgent)) {
    for(var i = 0; i < document.links.length; i++){
      if(typeof(document.links[i].className) != "undefined" &&
         document.links[i].className.length > 0 &&
         document.links[i].className == "popup"){
      		document.links[i].onclick = pop;
      		document.links[i].keydown = pop;
      		document.getElementById(document.links[i].href.split('#').last()).style.display = "none"; 
      }
    }
  }else{
    document.getElementsByClassName('popup').each(function(link){
			if(link.tagName == "A"){
				link.onclick = pop;
				link.keydown = pop;
				$(link.href.split('#').last()).hide();
			}
		});
  }
}

function hideSelect() {
	if (!(/MSIE 7/.test(navigator.userAgent))) {
		$A(document.getElementsByTagName("SELECT")).each(function(mySelect) {
			mySelect.style.left = "-9999px";
		});
	}
}

function showSelect() {
	$A(document.getElementsByTagName("SELECT")).each(function(mySelect) {
		mySelect.style.left = "0px";
	});
}

var fixHr = function(){
	$A(document.getElementsByTagName("hr")).each(function(hr){
		var new_hr = document.createElement("div");
		new_hr.className = "hr";
		new_hr.innerHTML = "&nbsp;";
		hr.parentNode.replaceChild(new_hr, hr);
	});
}

function getNewWindowLinks() {
	var links = document.getElementsByTagName('a');
	var location = window.location.toString().split('/')[2];
	for (var i = 0; i < links.length; i++) {
		if(/http/.test(links[i].href) && !(/serotec|ab-direct/.test(links[i].href))){
			if(!(new RegExp(location).test(links[i].href))){
				links[i].setAttribute("target", "_blank");
			}
		}
	}
}


var PrepopulateFields = function(){
  var queryString = window.location.toString().split("?");
  var queryObject = {};
  var entitys ={
  	"%24":"$",
  	"%26":"&",
  	"%2b":"+",
  	"%2c":",",
  	"%2f":"/",
  	"%3a":":",
  	"%3b":";",
  	"%3d":"=",
  	"%3f":"?",
  	"%40":"@",
  	"%20":" ",
  	"%22":"\"",
  	"%3c": "<",
    "%3e": ">",
    "%23": "#",
    "%25": "%",
    "%7b": "{",
    "%7d": "}",
    "%7c": "|",
    "%5c": "\\",
    "%5c": "^",
    "%7e": "~",
    "%5b": "[",
    "%5d": "]",
    "%60": "`"
  };
  if(queryString.length > 1) {
  
	queryString = queryString[1];
    var queryArray = queryString.split("&");
    for (var i = queryArray.length - 1; i >= 0; i--){
      var keyValue = queryArray[i].split("=");
	  var value = "";
	  if(keyValue[1]) value = keyValue[1].replace(/\+/g, " ").replace(/%[0-9][0-9,A-Z,a-z]/g, function(srWithExpression, sr){
        var str = "";
		var sr = sr.toString().toLowerCase();
    		if(entitys[sr]) return entitys[sr];
    		if(entitys[srWithExpression]) return entitys[srWithExpression];
    		return "";
      });
	  
	  queryObject[keyValue[0]] = value;
    };
  }
  
  for(var key in queryObject) {
    var formElement = $(key);
	if(!formElement) continue;
    if((formElement.tagName == "INPUT" && (formElement.type != "checkbox" || formElement.type != "radio" )) || 
      formElement.tagName == "TEXTAREA") {          
      formElement.value = queryObject[key];
    }
    
    if(formElement.tagName == "INPUT" && formElement.type == "checkbox") {
      formElement.checked = new Boolean(queryObject[key]);
    }
  }
};