 if (window.addEventListener)
 {
 	window.addEventListener( "load", init, false );
 }
 else if(window.attachEvent)
 {
 	window.attachEvent( "onload", init );
 }
 
 function init()
 {	
 	initShowHideDivs();
 	if ($('header'))
		$('header').onclick = function() { window.location = 'index.lasso' };
		
	if ($('demoButton'))
	{
		$('demoButton').onclick = function() { demoClick() };
		//if ($('IE6')) $('demoButton').onclick = function() { demoClickIE6() };
	}
	
	//----------Help (?) popup control---------------
	
 	var imgs = document.getElementsByTagName('IMG');
	for (i=0; i<imgs.length; i++)
	{
		if (imgs[i].id && imgs[i].id.match(/^add/))
		{
			imgs[i].onclick = toggleMenu;
		}
		if (imgs[i].name && imgs[i].name == 'info')
		{
			imgs[i].onclick = infopopup;
		}
		if (imgs[i].name && imgs[i].name == 'addProblem')
			imgs[i].onclick = cloneProblem;
		
	}
	
	//---------Form Controls---------------------
	
	var inputs = document.getElementsByTagName('INPUT'); //fix for IE6's lack of attribute selector support
	for (i=0; i<inputs.length; i++)
	{
		if (inputs[i].type == 'text') inputs[i].className += ' textInput';
	}
	
	if ($('appliancesRadioNo'))
		$('appliancesRadioNo').onclick = function() {
			$('propertyFormHidden').style.display = 'block' };
		
	if ($('appliancesRadioYes'))
		$('appliancesRadioYes').onclick = function() {
			$('propertyFormHidden').style.display = 'none' };
		
	if ($('problemDescription'))
		$('problemDescription').onfocus = function() { this.value = "" };
		
	//if ($('zip'))
	//	$('zip').onblur = function() { isZip(this.value) };
 }
 
 function demoClick()
 {
 	var demoBgFx;
	var pageSize = getPageSize();
	var demoBg = $('flashDemoBg');
		demoBg.style.width = pageSize[0]+'px';
		demoBg.style.height = pageSize[1]+'px';
	demoBgFx = new Fx.Style('flashDemoBg', 'opacity', {onComplete: openDemo});
	demoBgFx._start(0, 1);
	$('flashDemo').style.visibility = 'visible';
 }
 
  function demoClickIE6()
 {
 	var demoBgFx;
	var pageSize = getPageSize();
	var demoBg = $('flashDemoBg');
		demoBg.style.width = pageSize[0]+'px';
		demoBg.style.height = pageSize[1]+'px';
	demoBgFx = new Fx.Style('flashDemoBg', 'opacity', {onComplete: openDemo});
	demoBgFx._start(0, 1);
	$('flashDemo').style.visibility = 'visible';
 }
 
 function openDemo()
 {
 	var demo = $('flashDemo');
 	var so = new SWFObject("index.swf", "Total Protect Demo", "700", "600", "9", "#FFFFFF");
 	so.write(demo);
	var close = kreateElement('div', 
		{
			width:99+'px',
			height:16+'px',
			position:'absolute',
			top:demo.offsetTop-24+'px',
			left:document.body.offsetWidth/2+252+'px',
			zIndex:500,
			backgroundImage: 'url(imgs/button-closeDemo.gif)',
			cursor: 'pointer'
		});
		close.id = 'closeButton';
		close.onclick = closeDemo;
	document.body.appendChild(close);
 }
 
 function closeDemo()
 {
	var pageSize = getPageSize();
	var demoBgFx = new Fx.Style('flashDemoBg', 'opacity', {onComplete: unloadDemo});
		demoBgFx._start(1, 0);
 }
 
 function unloadDemo()
 {
	$('flashDemo').innerHTML = '';
	$('flashDemo').style.visibility='hidden';
 	$('flashDemoBg').style.visibility='hidden';
	document.body.removeChild($('closeButton'));
 }
 
 function toggleMenu()
 {
 	var menu = this.nextSibling;
 	while(menu.nodeType != 1)
	{
		menu = menu.nextSibling;
	}
 	menu.style.display == 'none' | menu.style.display == "" ? menu.style.display = 'block' : menu.style.display = 'none';
 }
 
 function cloneProblem()
 {
 	var c = $('problem_1').cloneNode(true);
	var i = c.id.indexOf('_');
	var num = c.id.substr(i+1);
		num++;
	c.id = c.id.substr(0, i)+num;
	$('propertyFormHidden').appendChild(c);
	var p = document.getElementById("problemDescription").value;
	if(p != "Describe the problem"){
		// create a loop to find out the number of instances of problemDescription
		var cnt = 0;
		cnt = cnt * 1;
		for(i=0; i<document.propertyForm.elements.length; i++){
			if(document.propertyForm.elements[i].name=="problemDescription"){
				cnt = cnt+1;
			}// end if
		}
		//loop again to set the last instance of problemDescription to 'Describe the problem'
		var cur = 1;
		cur = cur * 1;
		for(i=0; i<document.propertyForm.elements.length; i++){
			if(document.propertyForm.elements[i].name=="problemDescription"){
				var desc = document.propertyForm.elements[i].value;
				if(cur == cnt){
					document.propertyForm.elements[i].value = "Describe the problem";
				} else {
					cur = cur + 1;
				}
			}// end if
		} //end for loop
	}
 }
 
 function infopopup()
 {
	$('popupOverlay').style.display = 'block';
	$('popup').style.display = 'block';
 }
 
 function isZip(s) 
 {
	  reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
	  if (!reZip.test(s))
	  {
 		alert("Please enter a valid zip code.");
		return false;
      }
 return true;
 }
 
function kreateElement(tagName, args)
{
	var el = document.createElement(tagName);
	for (var props in args)
	{
		el.style[props] = args[props];
	}
	return el;
}

 function log(s)
 {
 	 if (window.addEventListener)
	 {
	 	window.console.log(s);
	 }
	 else if(window.attachEvent)
	 {
	 	alert(s);
	 }
 }

//------------------- SHOW HIDE (for dave) -----------------------//

var dhtmlgoodies_slideSpeed = 10;	// Higher value = faster
var dhtmlgoodies_timer = 10;	// Lower value = faster

var objectIdToSlideDown = false;
var dhtmlgoodies_activeId = false;
var dhtmlgoodies_slideInProgress = false;
function showHideContent(e,inputId)
{
	if(dhtmlgoodies_slideInProgress)return;
	dhtmlgoodies_slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(dhtmlgoodies_activeId &&  dhtmlgoodies_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
		dhtmlgoodies_activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	
	var obj =document.getElementById('dhtmlgoodies_a' + inputId);
	var contentObj = document.getElementById('dhtmlgoodies_ac' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display='block';
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
			}else{
				dhtmlgoodies_slideInProgress = false;
			}
		}else{
			dhtmlgoodies_activeId = inputId;
			dhtmlgoodies_slideInProgress = false;
		}
	}
}



function initShowHideDivs()
{
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='dhtmlgoodies_question'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'dhtmlgoodies_q'+divCounter;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'dhtmlgoodies_a'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='dhtmlgoodies_answer_content';
			contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
}
window.onload = initShowHideDivs;