// JavaScript Document

function findProvince() {
if(document.getElementById('pafiliaCountries').value != 'select') {
actionMain(showProvinces, 'includes/showSelectList.php?pValue='+document.getElementById('pafiliaCountries').value);

findAvailablePropeties(document.getElementById('pafiliaTypes').value, document.getElementById('pafiliaBedrooms').value, '%', document.getElementById('pafiliaCountries').value, document.getElementById('pafiliaPrices').value)

} else {
	//alert('here');
document.getElementById('pafiliaProvincesDIV').innerHTML = '';
document.getElementById('tallyCount').innerHTML = '';
}

}

function findAvailablePropeties(pTypev, pBedroomsv, pProvincev, pCountriesv, pPricev) {
	
	actionMain(tallyCount, 'includes/showTally.php?pType=' + pTypev + '&pBedrooms=' + pBedroomsv + '&pProvince=' + pProvincev + '&pCountries=' + pCountriesv + '&pPrice=' + pPricev);
	
}


function checkProvinceDefined() {

if(document.getElementById('pafiliaProvinces') == undefined) {
var pProvince = '%';
} else {
var pProvince = document.getElementById('pafiliaProvinces').value;
}

findAvailablePropeties(document.getElementById('pafiliaTypes').value, document.getElementById('pafiliaBedrooms').value, pProvince, document.getElementById('pafiliaCountries').value, document.getElementById('pafiliaPrices').value)

}

//begin the ticker counter

var goUpLoop;
var goDownLoop;
function tallyCounter(initValue, finalValue) {
	var endOfString = " Possibilities";
	window.clearInterval(goUpLoop);
	window.clearInterval(goDownLoop);
	
	var startVal = Number(initValue);
	var endVal = Number(finalValue);
	
	if(startVal < endVal) {
		goUpLoop = setInterval("tallyUpCounter(" + endVal + ")", 1);
	} else {
		window.clearInterval(goUpLoop);
		window.clearInterval(goDownLoop);
	}
	
	if(startVal > endVal) {
		goDownLoop = setInterval("tallyDownCounter(" + endVal + ")", 1);
	} else {
		window.clearInterval(goDownLoop);
		window.clearInterval(goDownLoop);
	}
		
}

	function tallyUpCounter(endVal) {
		//alert('up');
	var htmlVal = Number(document.getElementById('tallyCount').innerHTML);
	if(endVal - htmlVal > 5) {
		runningDiff = 5;
	} else {
		runningDiff = 1;
	}
	
	if(htmlVal < endVal) {
		htmlVal = Number(htmlVal) + runningDiff;
		document.getElementById('tallyCount').innerHTML = htmlVal;	
		} else {
			//alert(htmlVal + ' ' + endVal);
		tallyCounter(htmlVal, endVal);
	}
}

function tallyDownCounter(endVal) {
	//alert('down');
	var htmlVal = Number(document.getElementById('tallyCount').innerHTML);
	if(htmlVal - endVal > 5) {
		runningDiff = 5;
	} else {
		runningDiff = 1;
	}
	if(htmlVal > endVal) {
		htmlVal = htmlVal - runningDiff;
		document.getElementById('tallyCount').innerHTML = htmlVal;	
		} else {
			//alert(htmlVal + ' ' + endVal);
		tallyCounter(htmlVal, endVal);
	}
}
