//hideShowObj and positionDiv functions come from the scripts.js page

Estimator = {
	win: document.createElement('div'),
	init: function(config,zip){
		if(!zip) zip = '';
		this.config = config
		this.win.id = "estPU";
		this.win.className = "popupWin";
		this.win.innerHTML = '<div id="estHead">Sizing Estimator for Heating & Cooling Equipment</div>'
						+	'<div id="estClose" onclick="Estimator.show(0);"></div>'
						+	'<div id="estForm">'
						+	'<form action="javascript: Estimator.getValue();" onsubmit="return Estimator.validate();">'
						+	'<table align="center" class="mb10">'
						+	'<tr><td><input type="text" name="estSqFeet" id="estSqFeet" value="" size="5" onkeyup="Estimator.numbersOnly(this);"> &nbsp;</td><td>Home\'s Square Feet<br><span class="tiny">(exclude basement)</span></td></tr>'
						+	'<tr><td><input type="text" name="estZip" id="estZip" value="'+ zip +'" size="5" maxlength="5"> &nbsp;</td><td>Zip Code in USA</td></tr>'
						+	'</table>'
						+	'<input type="image" name="estSubmit" id="estSubmit" src="/images/offers/estimator_submit.png"></form>'
						+	'<div id="estError"><img src="/images/icons/24-message-warn.png" height="24" width="24" border="0" align="left">'
						+	'<div id="estErrorMess"></div></div></div>'
						+	'<div id="estResults"></div>';
		document.body.appendChild(Estimator.win);
		positionDiv(Estimator.win,Estimator.config);
	},
	
	show: function(hs,config){
		if(config){//if the page requires us to change the coordinates after init this will do it
			if(this.win.id != 'estPU'){ //if not already initialized then init
				this.init(config); 
			}else{ // just set the position
				this.config = config;
				positionDiv(Estimator.win,Estimator.config);
			}
		}
		this.hideShow('estPU',hs);
		this.hideShow("estForm",hs);
		this.hideShow("estResults",0);
		this.hideShow("estError",0);
	},
	
	error: function(thisObj,isError){
		if(isError){
			thisObj.style.border = '2px solid #cc0033';
			$("estErrorMess").innerHTML = 'The values entered are not in the correct format. Please correct the value in the text box higlighted above.'
			this.hideShow('estError',1);
			thisObj.focus();
		}else{
			thisObj.style.borderColor = 'white';
			$("estErrorMess").innerHTML = '';
			this.hideShow('estError',0);
		}
	},
	hideShow: function(id,hs){
		hs =(hs)?'visible':'hidden';
		var el = document.getElementById(id)
		if(el) el.style.visibility = hs;
	},
	numbersOnly: function(field) {
		var re = /^[0-9-'.']*$/;
		if (!re.test(field.value)) {
			field.value = field.value.replace(/[^0-9-'.']/g,"");
			return false;
		}
		return true;
	},
	validate: function(){
		estSqFeet = $('estSqFeet');
		estSqFeet.onchange = Estimator.validate;
		estSqFeet.value.replace(/[^0-9-'.']/g,"");
		if(estSqFeet.value.isNumeric()){
			Estimator.error(estSqFeet,false);
		}else{
			Estimator.error(estSqFeet,true);
			return false;
		}
		estZip = $('estZip');
		estZip.onchange = this.validate;
		if(estZip.value.isNumeric()){
			Estimator.error(estZip,false);
		}else{
			Estimator.error(estZip,true);
			return false;
		}
		if(estZip.value.length == 5){
			Estimator.error(estZip,false);
		}else{
			Estimator.error(estZip,true);
			return false;	
		}
		return true;
	},
			
	getValue: function(){
		this.hideShow('estForm',0);		
		elm=$("estResults");
		this.hideShow('estResults',1);
		elm.innerHTML = '&nbsp; Loading... Please wait...';
		var d = new Date();
		objXml.open("GET","/templates/ajax_estimator.cfm?sqFeet="+ $('estSqFeet').value +"&zip=" + $('estZip').value +"&ts=" + d.getTime(), true);
		objXml.onreadystatechange=function() {
			if (objXml.readyState==4) {
				estimate = eval('(' + objXml.responseText.trim() + ')');
				if(estimate && estimate.HTML && estimate.error == ''){
					elm.innerHTML = estimate.HTML;
				}else{
					Estimator.show(1);
					$('estErrorMess').innerHTML = estimate.error;
					hideShowObj($('estError'),1);
				}
			}
		}
		objXml.send(null);
	}
}