var cm_opts = '';
var c_opts = '';

$(document).ready(function() {
	cm_opts = $('#carmodels').html();
	c_opts = $('#cities').html();
});

function loadBrandModels( bid , cid) {
	var cid = cid;
	if (bid > 0) {
		$('#spin_model').html( '&nbsp;<img src="/images/loader.gif" />');
		$.ajax({
			url: '/directory/ajax/carmodels/bid/' + bid,
			cache: false,
			dataType: 'json',
			success: function( models) {
				var html = cm_opts;
				for (var i = 0; i < models.length; i++) {
					html += '<option value="' + models[i].id + '">' + models[i].name + '</option>';
				}
				$('#carmodels').html( html);
				$('#carmodels').removeAttr( 'disabled');
				$('#spin_model').html( '');
				
				if (cid > 0) {
					document.getElementById( 'carmodels').value = cid;
				}
			}
		});
	} else {
		$('#carmodels').attr( 'disabled', 'disabled');
	}
}

function loadCities( cid , curr) {
	var curr = curr;
	if (cid > 0) {
		$('#spin_city').html( '&nbsp;<img src="/images/loader.gif" />');
		$.ajax({
			url: '/directory/ajax/cities/rid/' + cid,
			cache: false,
			dataType: 'json',
			success: function( cities) {
				var html = c_opts;
				for (var i = 0; i < cities.length; i++) {
					html += '<option value="' + cities[i].id + '">' + cities[i].name + '</option>';
				}
				$('#cities').html( html);
				$('#cities').removeAttr( 'disabled');
				$('#spin_city').html( '');
				
				if (curr > 0) {
					document.getElementById( 'cities').value = curr;
				}
			}
		});
	} else {
		$('#cities').attr( 'disabled', 'disabled');
	}
}

function highlightTextOption( obj, operation) {
    if (operation == 'highlight') {
    	if (obj.className == 'text_option') {
			obj.className = 'text_option_highlighted';
		}
	} else {
		if (obj.className == 'text_option_highlighted') {
			obj.className = 'text_option';
		}		
	}
}

function toggleTextOption( obj, targetObjId, optionValue) {
	var targetObj = document.getElementById( targetObjId);
	switch (obj.className) {
		case 'text_option_highlighted': {
			obj.className = 'text_option_selected';
			break;
		}
		case 'text_option_selected': {
			obj.className = 'text_option_highlighted';
			break;
		}
	}
	
	if (targetObj.value.indexOf( optionValue) == -1) {
		// add new option
		if (targetObj.value.length == 0){
			targetObj.value += optionValue;
		} else {
			targetObj.value += ',' + optionValue;
		}
	} else {
		// remove option
		if (targetObj.value.indexOf( ',' + optionValue) != -1) {
			targetObj.value = targetObj.value.replace( ',' + optionValue, '');
		} else if (targetObj.value.indexOf( optionValue + ',') != -1) {
			targetObj.value = targetObj.value.replace( optionValue + ',', '');
		} else {
			targetObj.value = targetObj.value.replace( optionValue, '');
		}
	}
}

function useCarmodelsBlock( block) {
	if (block == 'custom') {
		document.getElementById( 'carmodels_selector').style.display = 'none';
		document.getElementById( 'carmodels_custom').style.display = 'block';
		document.getElementById( 'carmodels').disabled = true;
		document.getElementById( 'model_name_custom').disabled = false;
	} else if (block == 'catalog') {
		document.getElementById( 'carmodels_selector').style.display = 'block';
		document.getElementById( 'carmodels_custom').style.display = 'none';
		document.getElementById( 'carmodels').disabled = false;
		document.getElementById( 'model_name_custom').disabled = true;
	
	}
}

function convertMiles2Km() {
	var dialogWidth = 400;
	var dialogHeight = 140;
	var posX= document.documentElement.clientWidth/2/* - dialogWidth/2*/;
	var posY = document.documentElement.clientHeight/2/* - dialogHeight/2*/;
	//alert('location=0,status=0,scrollbars=0,width=' + dialogWidth +',height=' + dialogHeight + ',top=' + posX + ',left=' + posY + ',screenY=' + posX + ',screenX=' + posY);
	window.open( '/ad/convert-miles', 'convert-miles', 'location=0,status=0,scrollbars=0,width=' + dialogWidth +',height=' + dialogHeight + ',top=' + posY + ',left=' + posX + ',screenY=' + posY + ',screenX=' + posX);
}

function insertContactInfo( checkObj, defaultContact) {
	if (checkObj.checked) {
		document.getElementById( 'contact_person').value = defaultContact['person'];
		document.getElementById( 'contact_phone').value = defaultContact['phone'];
		document.getElementById( 'contact_email').value = defaultContact['email'];
	}
}
