﻿// JavaScript Document for Search Screens

var outerCarType = '0';
/* at end of page load resets the checked radio buttons background color */

document.onreadystatechange = resetRadios;
function resetRadios(){
	if(document.readyState=="complete"){
               
		var allInputs = document.getElementsByTagName('input');
		for(var i = 0; i<allInputs.length; i++){
			if((allInputs(i).type == 'radio') && (allInputs(i).checked == true)){
				allInputs(i).fireEvent('onclick');
			}
		}
	}
}

/* changes the class of the TD with the checked radio */
function changeRadioBg(clickedRadio){							// clickedRadio = this
	var radioName = clickedRadio.name;
	radioCollection = document.getElementsByName(radioName);	// = eval("document.forms[0]."+radioName);
	for(var a=0; a<radioCollection.length; a++){
		var radioTd = radioCollection[a];
		if(radioCollection[a].checked){							// or if(radioCollection[a]==clickedRadio)
			while(radioTd.tagName!='TD'){
				radioTd = radioTd.parentElement;
			}
			radioTd.className = "bg1";
		}else{
			while(radioTd.tagName!='TD'){
				radioTd = radioTd.parentElement;
			}
			radioTd.className = "";
		}
	}
}


/* chnges the background color of the car type picture and radio button */
function carTypeChangeClass(){
	rd_carType = document.forms[0].rd_carType;
	if(rd_carType[0].checked){
		outerCarType = rd_carType[0].value;
		document.getElementById("all1").className = "bg1";
		document.getElementById("all2").className = "bg1";
	}else{
		document.getElementById("all1").className = "";
		document.getElementById("all2").className = "";
	}
	if(rd_carType[1].checked){
		outerCarType = rd_carType[1].value;
		document.getElementById("private1").className = "bg1";
		document.getElementById("private2").className = "bg1";
	}else{
		document.getElementById("private1").className = "";
		document.getElementById("private2").className = "";
	}
	if(rd_carType[2].checked){
		outerCarType = rd_carType[2].value;
		document.getElementById("commercial1").className = "bg1";
		document.getElementById("commercial2").className = "bg1";
	}else{
		document.getElementById("commercial1").className = "";
		document.getElementById("commercial2").className = "";
	}
	if(rd_carType[3].checked){
		outerCarType = rd_carType[3].value;
		document.getElementById("pickup1").className = "bg1";
		document.getElementById("pickup2").className = "bg1";
	}else{
		document.getElementById("pickup1").className = "";
		document.getElementById("pickup2").className = "";
	}
	if(rd_carType[4].checked){
		outerCarType = rd_carType[4].value;
		document.getElementById("jeep1").className = "bg1";
		document.getElementById("jeep2").className = "bg1";
	}else{
		document.getElementById("jeep1").className = "";
		document.getElementById("jeep2").className = "";
	}
        if(rd_carType.length  > 5) {
		if(rd_carType[5].checked){
			outerCarType = rd_carType[5].value;
			document.getElementById("masait1").className = "bg1";
			document.getElementById("masait2").className = "bg1";
		}else{
			document.getElementById("masait1").className = "";
			document.getElementById("masait2").className = "";
		}
        }
	try{
		//loadSelect();		// used in search_degem.jsp
	}catch(e){}
}

/* for screens search_leasPrice and search_carPrice */
function changeDiffValue(){
	var searchFrm = document.forms[0];
	var diff = searchFrm.diff;
	var diffType = searchFrm.diffType;
	var carPrice = searchFrm.carPrice;
	var roundNumber;
	if(diff.value != 0){
		if(diffType[0].checked){
			diff.value = 10;
		}
		else{
			if(carPrice.value != 0){
				roundNumber = parseInt(carPrice.value)/10;
				roundNumber = Math.round(roundNumber*Math.pow(10,0))/Math.pow(10,0);	//round the number
				diff.value = roundNumber;
			}
			else{
				diff.value = 0;
			}
		}
	}
	showSearchValues();
}

var counterFlag = 0;

/* for screens search_leasPrice and search_carPrice, calculates and displays the range */
var roundFrom;
var roundTo;
function showSearchValues(){
	var searchFrm = document.forms[0];
	var diff = searchFrm.diff.value*1;
	var diffType = searchFrm.diffType;
	var carPrice = searchFrm.carPrice.value*1;
	var flag = false;
	if(counterFlag == 0){
		counterFlag = 1;
		flag = searchValuesValidation();
		if(flag){																// if filling ok
			if(carPrice > 0){													// if price filed has more than 0
				if(diffType[0].checked){
					roundFrom = parseInt(carPrice) - (parseInt(diff) * parseInt(carPrice) / 100);
					roundTo = parseInt(carPrice) + (parseInt(diff) * parseInt(carPrice) / 100);
				}
				else{
					roundFrom = parseInt(carPrice) - parseInt(diff);
					roundTo = parseInt(carPrice) + parseInt(diff);
					if(roundFrom < 0){
						roundFrom = 0;
					}
				}
			}
			else{																// if price field has 0
				from.innerHTML = 0;
				to.innerHTML = 0;
				return;
			}
			roundFrom = Math.round(roundFrom*Math.pow(10,0))/Math.pow(10,0);	// round the number
			roundTo = Math.round(roundTo*Math.pow(10,0))/Math.pow(10,0);		// round the number
			var fromCommas = addCommas(roundFrom);								// add commas to the number
			var toCommas = addCommas(roundTo);									// add commas to the number
			from.innerHTML = fromCommas;										// print from price on screen
			to.innerHTML = toCommas;											// print to price on screen
		}
	}
	counterFlag = 0;
}

/* validates the filling of the relevant field */
function searchValuesValidation(){
	var currObj = event.srcElement;
	var currValue = currObj.value;
	var returnValue = true;
	if((currValue != '') && (currValue != '0')){
		switch(currObj.name){
			case 'diff':
				var maxValue = null;
				if(document.forms[0].diffType[0].checked){
					maxValue = 100;
				}
				switch(isNumber(currObj.value, 0, maxValue, 0)){
					case 0:
						returnValue = true;
						break;
					case 1:
						alertMsg(0, 'גודל הסטייה');
						currObj.value = '0';
						currObj.focus();
						returnValue = false;
						break;
					/*case 2:
						alertMsg(4, '0^'+maxValue+'^גודל הסטייה');
						currObj.value = '0';
						currObj.focus();
						returnValue = false;
						break;*/
					case 3:
						alertMsg(4, '0^'+maxValue+'^גודל הסטייה');
						currObj.value = '100';
						currObj.focus();
						returnValue = false;
						break;
				}
				break;
			case 'carPrice':
				switch(isNumber(currObj.value, 0, null, 0)){
					case 0:
						returnValue = true;
						break;
					case 1:
						if(currObj.nextSibling.value == 'carPrice'){
							msgText = 'מחיר הרכב';
						}else if(currObj.nextSibling.value == 'leasePrice'){
							msgText = 'סכום תשלום חודשי';
						}
						alertMsg(0, msgText);
						currObj.value = '0';
						currObj.focus();
						returnValue = false;
						break;
					/*case 2:
						alertMsg(4, '0^100^מחיר הרכב');
						currObj.value = '0';
						currObj.focus();
						returnValue = false;
						break;
					case 3:
						alertMsg(0, '0^100^ ');
						currObj.value = '100';
						currObj.focus();
						returnValue = false;
						break;*/
				}
				break;
			case 'diffType':
				break;
		}
	}else if(currValue == ''){
		returnValue = false;
	}
	return returnValue;
}

function searchValuesValidationOld(){
	var searchFrm = document.forms[0];
	diff = searchFrm.diff;
	var carPrice = searchFrm.carPrice;
	var diffType = searchFrm.diffType;
	if (parseInt(carPrice.value) != carPrice.value){		// if price field empty
		alertMsg(0, 'המחיר');
		return returnToField(carPrice);
	}
	if (parseInt(diff.value) != diff.value){				// if not numbers in the diff field
		alertMsg(0, 'הטווח');
		return returnToField(diff);
	}
	if(carPrice.value < 0){
		alertMsg(7, 'המחיר');
		carPrice.value = 0;
		return returnToField(carPrice);
	}
	if(diff.value < 0){
		alertMsg(7, 'טווח החיפוש');
		diff.value = 0;
		return returnToField(diff);
	}
	if(diffType[0].checked){
		if(diff.value > 100){
			alertMsg(4, '0^100^טווח החיפוש');
			diff.value = 0;
			return returnToField(diff);
		}
	}
	if(diff.value == 0){									// if filled 0 in the diff field - ask if sure
		if(event.srcElement.name=="searchForm"){
			var confirmDiff = window.confirm("הזנת 0 בשדה הטווח תחזיר רכבים רק בסכום המדויק שהזנת. האם את/ה רוצה להמשיך?");
			if(confirmDiff==false){
				return returnToField(diff);
			}
		}
	}
	return true;
}

/* diplays the tool tip */
function shoTip(){
	var tdObj = event.srcElement;
	while(tdObj.tagName != 'TD'){										// hold the TD Object
		tdObj = tdObj.parentElement;
	}
	var tipTbl = document.getElementById('tipTable');					// holds the tip table
	var currTip = tdObj.children(1).innerHTML;							// holds the tip text
	// postion the tip:
	var dealTypeTr = document.getElementById('dealTypeTR2');
	var mouseTop = dealTypeTr.offsetTop;
	tipTbl.style.top = mouseTop + 280 + 'px';
	tipTbl.children(0).rows(2).cells(1).innerHTML = currTip;			// place the current text in the TD
	var arrowRow = tipTbl.children(0).rows(0).cells(1).children(0).rows(0);
	for(var c=0; c< arrowRow.cells.length; c++){
		arrowRow.cells(c).children(0).style.display = 'none';			// hide the previouse arrow
	}
	var arrowPlace = 0;
	for(var t=0; t<tdObj.parentElement.cells.length; t++){
		if(tdObj == tdObj.parentElement.cells(t)){
			arrowPlace = t;
		}
	}
	arrowRow.cells(arrowPlace).children(0).style.display = 'block';		// show the correct arrow
	tipTbl.style.display = 'block';										// show the table
}

/* hides the tool tip */
function hideTip(){
	var tipTbl = document.getElementById('tipTable');					// holds the tip table
	tipTbl.style.display = 'none';										// hide the table
}

