////////////////////////////////////////////////////////
/* Waterland javascript */
////////////////////////////////////////////////////////


window.onload = init;


//------------------------------------------------------
// INIT

var W3CDOM = (document.createElement && document.getElementsByTagName);
function init(){
  // EK: uncomment for infolution
  // if (!W3CDOM) return;
  // addZappLiveSearchEvents('keyword');
}
//------------------------------------------------------

//------------------------------------------------------
// IE Detect (Windows only!)

var detect = navigator.userAgent.toLowerCase();

// use this variable to check if browser is Internet Explorerfor Windows 
var WinIE;

if (checkIt('msie')) WinIE = true;
if (checkIt('opera')||checkIt('mac')) WinIE = false;
function checkIt(string){
	place = detect.indexOf(string) + 1;
	return place;
}
// End IE Detect
//------------------------------------------------------

//------------------------------------------------------
// Jump to the next input 'elm' after 'n' chracters

function jumpTab(elm,n){
  if (elm.value.length>=n){
    var pe=fcd((fsd((fpd(elm,'DIV')),'DIV')),'INPUT');
    pe.focus();
  }
}
//------------------------------------------------------


//------------------------------------------------------
// toggle all checkboxes within element id (elID)

function checkAll(elID){
  var el=document.getElementById(elID);
  var e=fpd(el,'UL');
  var i = e.getElementsByTagName('INPUT');
  el.onclick=function(){
    this.checked = (this.checked) ? true : false;
    for(n=0;n<i.length;n++){
      if(i[n]==this)continue;
      if(!i[n].checked)i[n].checked=true;
    }
  }
  for(n=0;n<i.length;n++){
    if(i[n]==el)continue;
    i[n].onclick=function(){
      if(!this.checked)el.checked=false;
    }
  }
}
//------------------------------------------------------


//------------------------------------------------------
// Generic toggle script

function toggleME(el,t,l){
  var t = ( t=="" || t==undefined || t==null ) ? 'LI' : t
  var e = fpd(el,t);
  var o="open";
  var c="closed";
  var liElm = e.getElementsByTagName('LI')
  if (liElm.length>5 && l==1 ){ o='openSpecial' }
  if (e.className.indexOf(c)>-1){
    removeCSSClass(e,c);
    addCSSClass(e,o);
  } else if (e.className.indexOf(o)>-1) {
    removeCSSClass(e,o);
    addCSSClass(e,c);
  }
}
//------------------------------------------------------


//------------------------------------------------------
// Classname handlers

function addCSSClass(elem,cn){elem.className=(elem.className+" "+cn).trim();}
function removeCSSClass(elem,cn){elem.className=elem.className.replace(cn,"").trim();}
String.prototype.trim=function(){return this.replace( /^\s+|\s+$/, "" );}
function switchClassName(e,a,b){e.className=e.className.replace(a,"").trim();e.className=(e.className+" "+b).trim();}
function fpd(elem,e){var o=elem;while(o.parentNode.nodeName!=e.toUpperCase()){o=o.parentNode;}return o.parentNode;}

// End Classname handlers
//------------------------------------------------------

//------------------------------------------------------
// Form validation

var fa = new String();

var errorTypes = {
  required: new errorType(false, /^\s+|\B$/, "Please enter some text before submitting, \nan empty field or a 'space' character is not valid.")
}

function errorType( frmError, reg, errMsg ) {
  this.frmError = frmError;
  this.errMsg = errMsg;
  this.reg = reg;
}

function validateForm(e,elmId,tval){
  var i = document.getElementById(elmId);
  var f = fpd(i,'FORM')
  if(fa=="")fa = f.action;
  var t = chkInput(i.value, eval("errorTypes."+tval));
  if (t.frmError) {
    f.action = null;
    alert(t.errMsg);
    return false;
  } else {
    f.action = fa;
    //f.action();
    return true;
  }
}

function chkInput(i, t){
  if (t.reg.test(i)) {
   t.frmError = true;
  }
  return t;
}


// End Form validation
//------------------------------------------------------




//------------------------------------------------------
// XMLHttpRequest init functions

var t = null;
var xmlhttp = false;

if (window.XMLHttpRequest) {
	xmlhttp = new XMLHttpRequest();
}

function callGetSuggest() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("zappSuggest()",200);
}

function zappSuggest() {
	if (xmlhttp && xmlhttp.readyState < 4 ) {
		xmlhttp.abort();
	}
	var elem = document.getElementById('suggestions');
	var container = document.getElementById('zappSuggestsResults');
	var queryWord = document.getElementById('keywordID').value;
  var baseValue = document.getElementById('searchBaseValue').value + 'startkey/' + queryWord + '/index.cfm';
  //spacePos = queryWord.lastIndexOf(" ");  
  //if (spacePos > -1) queryWord = queryWord.substr(spacePos+1,queryWord.length+1);
  if (queryWord.length>1) {
  	elem.innerHTML = "<li><a> ... </a></li>";
    getSuggest(baseValue);  
	}
} 

function getSuggest(fragment_url) {
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
  xmlhttp.onreadystatechange = getResponce;
  xmlhttp.open("GET", fragment_url);
 	xmlhttp.send(null);
} 

function getResponce() {
	var elem = document.getElementById('suggestions');
	var container = document.getElementById('zappSuggestsResults');
	container.style.display = "block";
	if (xmlhttp.readyState == 4) {
	  elem.innerHTML = xmlhttp.responseText;
    xmlhttp.abort();    
	}
}

function enterSelection(formn,fieldn,s,c){ 
  var frm = fpd(c,'FORM');
  frm[fieldn].value = c.innerHTML;

  frm.submit();
}

function addZappLiveSearchEvents(inputID) {
	var e = document.getElementById(inputID);
	var r = document.getElementById("zappSuggestsResults");
	if (navigator.userAgent.indexOf("Safari") > 0) {
		e.addEventListener("click",delayZappLiveSearchHide,false);
		//e.addEventListener("blur",delayZappLiveSearchHide,false);
		//r.addEventListener("onmouseover",zappLiveSearchKeysShow,false);
	} else if (navigator.product == "Gecko") {
		e.addEventListener("click",delayZappLiveSearchHide,false);
		//e.addEventListener("blur",delayZappLiveSearchHide,false);
		//r.addEventListener("onmouseover",zappLiveSearchKeysShow,false);
	} else {
		e.attachEvent('onclick',delayZappLiveSearchHide);
		//e.attachEvent("onblur",delayZappLiveSearchHide);
		//r.attachEvent("onmouseover",zappLiveSearchKeysShow);
	}
	e.setAttribute("autocomplete","off");
}

function delayZappLiveSearchHide() {
	window.setTimeout("zappLiveSearchHide()",400);
}

function zappLiveSearchHide() {
		document.getElementById("zappSuggestsResults").style.display = "none"; 
}
function zappLiveSearchKeysShow() {
	document.getElementById('keyword').focus();
	if (document.getElementById('keyword').value!="") document.getElementById("zappSuggestsResults").style.display = "block";
}
function zappLiveSearchKeys(event) {

	//KEY DOWN
	if (event.keyCode == 40 ) {
		if (!WinIE) { event.preventDefault(); }
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		if (!WinIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		document.getElementById("zappSuggestsResults").style.display = "none";
	} 
}

// END: XMLHttpRequest init functions
//------------------------------------------------------




//  EOF