var txtBrowser = navigator.userAgent
var intIEVersion = parseFloat(txtBrowser.substring(txtBrowser.indexOf('MSIE')+5, txtBrowser.indexOf(';', txtBrowser.indexOf('MSIE'))))
var isIE4 = (navigator.appName.indexOf('Microsoft') > -1 && parseInt(intIEVersion) >= 4)

var currentQuestionDiv = null;
var currentAnswerDiv = null;

function showDiv(key) {
	var divQuestion = document.getElementById('qst'+key);
	var divAnswer = document.getElementById('ans'+key);
	if(divQuestion) {
		divQuestion.style.display = "none";
		divAnswer.style.display = "block";
		if(currentQuestionDiv) {
			currentQuestionDiv.style.display = "block";
			currentAnswerDiv.style.display = "none";
		}
		currentQuestionDiv = divQuestion;
		currentAnswerDiv = divAnswer;
	}
}

function swapLayer(el) {
	var myLayer = document.getElementById(el);
	if (myLayer.style.visibility != 'visible') {
		myLayer.style.visibility = 'visible';
	} else {
		myLayer.style.visibility = 'hidden';
	}
}

function GetSelectedCategories(frm)
{
	var selection = document.forms[frm].selectie; // get checkbox selection
	var separator = ""; // declare separator for separator-separated string
	document.forms[frm].categories.value = ""; // empty categories hidden form field
	for(var i=0; i < selection.length; i++) // loop through checkbox selection
	{
		if(selection[i].checked) //  if checkbox is selected, continue
		{
			document.forms[frm].categories.value += separator + selection[i].value; //  add checkbox selection to categories hidden form field
			separator = ","; // use this separator for separator-separated string
		}
	}
}

function rollOut(imageName)
{
    var imageSrc = document.images[imageName].src;
    var indexOver = imageSrc.indexOf("_over");
    if (indexOver != -1) {
    		document.images[imageName].src = imageSrc.substring(0,indexOver) + imageSrc.substring(imageSrc.length-4);
    }
    // else do nothing
}

function rollOver(imageName)
{
    var imageSrc = document.images[imageName].src ; 
    if (imageSrc.indexOf("_over") == -1) {
    	document.images[imageName].src = imageSrc.substring(0,imageSrc.length-4) + "_over" + imageSrc.substring(imageSrc.length-4);
    	}
    	// else do nothing
}

// Browsercheck object

function cm_bwcheck(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent.toLowerCase()
	this.dom=document.getElementById?1:0
	this.op5=(this.agent.indexOf("opera 5")>-1 || this.agent.indexOf("opera/5")>-1) && window.opera 
	this.op6=(this.agent.indexOf("opera 6")>-1 || this.agent.indexOf("opera/6")>-1) && window.opera   
	this.ie5 = (this.agent.indexOf("msie 5")>-1 && !this.op5 && !this.op6)
	this.ie55 = (this.ie5 && this.agent.indexOf("msie 5.5")>-1)
	this.ie60 = (this.agent.indexOf("msie 6")>-1 && !this.op5 && !this.op6)
	this.ie4=(this.agent.indexOf("msie")>-1 && document.all &&!this.op5 &&!this.op6 &&!this.ie5&&!this.ie6)
	this.ie = (this.ie4 || this.ie5 || this.ie6)
	this.mac=(this.agent.indexOf("mac")>-1)
	this.ns6=(this.agent.indexOf("gecko")>-1 || window.sidebar)
	this.ns4=(!this.dom && document.layers)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.op5 || this.op6)
	this.usedom= this.ns6//Use dom creation
	this.reuse = this.ie||this.usedom //Reuse layers
	this.px=this.dom&&!this.op5?"px":""
	return this
}

// document.all simulator for NN6
if (cm_bwcheck().ns6) {
  Node.prototype.__defineGetter__("all", function() {
    if (document.getElementsByTagName("*").length) {
      switch (this.nodeType) {
        case 9: 
		  return document.getElementsByTagName("*")
          break
        case 1:
		  return this.getElementsByTagName("*")
		  break
		}
      }
      return ""
	}
  )
  Node.prototype.__defineSetter__("all", function() {})
}

//z-index fix for selects in IE
var arrHiddenSelects = new Array()
function hideSelects(obj) { 
  if (cm_bwcheck().ie) {  	 
    var arrSelectObjects = document.getElementsByTagName('SELECT')
    var arrObjPosition = getPosition(obj)
    for (var i=0; i<arrSelectObjects.length; i++) {
      var objElement = arrSelectObjects[i]
      var arrSelectPosition = getPosition(objElement)
      if ((arrSelectPosition.left >= arrObjPosition.right) || (arrSelectPosition.right <= arrObjPosition.left) || (arrSelectPosition.bottom <= arrObjPosition.top) || (arrSelectPosition.top >= arrObjPosition.bottom)) {
	    objElement.style.visibility = 'visible'
	  } else {
	    arrHiddenSelects[arrHiddenSelects.length] = objElement
	    objElement.style.visibility = 'hidden'
	  }
    }
  }
}

function showSelects() {
  if (cm_bwcheck().ie) {
    for (var i=0; i<arrHiddenSelects.length; i++) {
      arrHiddenSelects[i].style.visibility = 'visible'
    }
  }
}


function getPosition(objReference) {
	var arrPosition = new Array()
	
	if (objReference != null) {
  	arrPosition.left = 0
  	arrPosition.right = objReference.offsetWidth
  	arrPosition.top = 0
  	arrPosition.bottom = objReference.offsetHeight
  	while (objReference != null && objReference.tagName != 'BODY' && (objReference.style.position == '' || objReference.style.position == 'relative')) {
    	arrPosition.left += objReference.offsetLeft
    	arrPosition.top += objReference.offsetTop
	  	objReference = objReference.offsetParent
	  } 
	  arrPosition.right += arrPosition.left
	  arrPosition.bottom += arrPosition.top
  }
 	return arrPosition
}

function swapLayer(el) {
	var myLayer = document.getElementById(el);
	if (myLayer.style.visibility != 'visible') {
		myLayer.style.visibility = 'visible';
	} else {
		myLayer.style.visibility = 'hidden';
	}
}

function popupWindow(url, windowsettings) {
	window.open(url, "popup", windowsettings); 
}

function openNewWindow (url) {
	var browserWidth = screen.availWidth-20;

	if (browserWidth<780)
	{
		var left = (screen.availWidth - 550) / 2;
		var top = (screen.availHeight - 350) / 2;
		window.open(url, "newwindow", "left="+left+", top="+top+", width=550, height=350, location=yes, menubar=yes, status=yes, toolbar=yes, scrollbars=yes, resizable=yes");
	}
	else 
	{
		var left = (screen.availWidth - 780) / 2;
		var top = (screen.availHeight - 500) / 2;
		window.open(url, "newwindow","left="+left+", top="+top+", width=780, height=500, location=yes, menubar=yes, status=yes, toolbar=yes, scrollbars=yes, resizable=yes");
	}
}

// add email validation method to String object; usage: document.formName.inputName.value.isEmail()
// email should not have:
// - two @ signs separated by 0 or more characters
// - two sequential periods not separated by any characters
// - @ sign directly followed by period
// - leading period
// email should have:
// - begin with one or more characters before @
// - one optional [ directly after @
// - one or more alphanumerics, numbers, dashes or periods (domain name doesn't allow underscore)
// - decimal followed by 2-3 alphanumerics or 1-3 numbers
// - one optional ] at the end
String.prototype.isEmail = function isEmail() {
  return (!/(@.*@)|(\.\.)|(@\.)|(^\.)/.test(this) && /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(this))
}

// add trim method to String object; usage: document.formName.inputName.value.trim('left', 'trimtext')
// strTrimType is 'left' or 'right'; default (no parameters) is full trim of spaces
// character support check only available in IE4+ due to buggy ns4 (returns false every 2nd time in ns4)
String.prototype.trim = function trim(strTrimType, strChar) {
  strTrimType = (typeof strTrimType != 'string') ? 'full' : strTrimType
  strChar = (typeof strChar != 'string') ? ' ' : strChar
  if ((isIE4) && (!/^([ \^\$\*\+\?\.\|\[\]\\\(\)a-zA-z0-9,-~!@#_{}&%:;<=>])*$/g.test(strChar))) {return this} //check supported characters
  strChar = strChar.replace(/([\^\$\*\+\?\.\|\[\]\\\(\)])/g, '\\$1')
  return this.replace(new RegExp((/^left$/i.test(strTrimType) ? '^' : '')+'('+strChar+')+'+(/^right$/i.test(strTrimType) ? '$' : ''), 'g'), '')
}

function openWindowAndPrint () {
	var browserWidth = screen.availWidth-20;
	var positionRight = location.href.length;
	if (location.href.indexOf("#") != -1) {
		positionRight = location.href.indexOf("#") - 1;
	}
	var printUrl = location.href.substr(0,positionRight);
	printUrl = printUrl + ((printUrl.indexOf("?") != -1) ? "&" : "?") + "popup=true";	

	if (browserWidth<780)
	{
		var left = (screen.availWidth - 550) / 2;
		var top = (screen.availHeight - 350) / 2;
		var printwindow = window.open(printUrl, "newwindow", "left="+left+", top="+top+", width=550, height=350, location=no, menubar=yes, status=no, toolbar=no, scrollbars=yes, resizable=yes");
	}
	else 
	{
		var left = (screen.availWidth - 550) / 2;
		var top = (screen.availHeight - 600) / 2;
		var printwindow = window.open(printUrl, "newwindow","left="+left+", top="+top+", width=550, height=600, location=no, menubar=yes, status=no, toolbar=no, scrollbars=yes, resizable=yes");
	}
	printwindow.print();
}