function showCovAreaIssue(iid) {
	$('div.isstext').each(function() {
		if ($(this).attr('id') == 'it' + iid) {
			$(this).css("display","block");
		} else {
			$(this).css("display","none");
		}
	});
}

function close_cities() {
	$('#cities_list').hide();
}

function mod(div,base) {
	return Math.round(div - (Math.floor(div/base)*base));
}

function get_cities(e) {
	var x = e.pageX;
	var y = e.pageY;
	
	$('#list').text('');
	
	$.getJSON("http://tgd.uncadunca.com:10000/site/load_city_list",
		function(data){
			$.each(data.dealers, function(i,item) {
				if ( mod(i,3) == 0 ) {
					$('#list').append('<li class="clear"><a href="http://tgd.uncadunca.com:10000/site' + item.site_id + '/index.html">' + item.city + '</a></li>');
				} else {
					$('#list').append('<li><a href="http://tgd.uncadunca.com:10000/site' + item.site_id + '/index.html">' + item.city + '</a></li>');
				}
			});
		});

	
	$('#cities_list').css('display','block');
}

function getGETvalue(key){
	var strQueryString="";
	var hasQueryString = document.URL.indexOf('?');
	var hasAnchor = document.URL.indexOf('#');
	if (hasAnchor == -1){
		hasAnchor = document.URL.length;
	}
	if (hasQueryString != -1) {
		strQueryString = document.URL.substring(hasQueryString+1, hasAnchor);
		
		var Variables = strQueryString.split("&");
		
		for ( var i in Variables){
			keyvalue = Variables[i].split("=");
		
			if (keyvalue.length == 2 && 
		    	keyvalue[0] == key){
		    	return keyvalue[1];
		    }   
	    }
	}
	return "";
}

function getAnchor(){
	var hasAnchor = document.URL.indexOf("#");
	if (hasAnchor == -1){
		return "";
	}
	else {
		return document.URL.substring(hasAnchor, document.URL.length);
	}
	

}

var SEARCH_LIMIT = 10;

function getSearchResults(){
	$(document).ready(function(){  
		var query = getGETvalue("q");
		$.getJSON("/search/results?q=" + query, function(data){
			if (data.num_found > 0) {
				for (var i = 0; i < data.num_found; i++) {
					$('#results').append('<p><a href="' + data.results[i].url + '">' + data.results[i].title + '</a><br>' + data.results[i].descr + '</p>');
				}
			} else {			
				$('#results').html('<p>No Results Found</p>');
			}
		});
	
	});
}

function getSearchCount(){

	$(document).ready(function(){  
		
		var query = getGETvalue("v");
		var offset = getGETvalue("off");
	
		if(query == ""){
			return "";
		}
		
		if (offset == NaN || offset == ""){
			offset = 0;
		}
		
		$.get("/search/getCount?key=" + query, function(data){
			
			if(data != NaN){
				var count = $("#resultCount")[0];
				var temp_max = parseInt(offset) + parseInt(SEARCH_LIMIT);
				
				if (temp_max > data){
					 $("#lnNxt").toggle();
					temp_max = data;
				}
				
				var display_start = parseInt(offset) + 1;
				
				 $("<p>" + "showing " + display_start + " to " + temp_max + " of " + data  + "</p>").appendTo(count)
				 
				  if (offset < SEARCH_LIMIT){
					 $("#lnPrv").toggle();
				 }
					 
					 
				 
				 $("#lnNxt").click(function(){
					window.location = "/search.html?v=" + query + "&off=" + temp_max;
				 })
				  $("#lnPrv").click(function(){
					 var temp_min = parseInt(offset) - parseInt(SEARCH_LIMIT);
					 var new_location =  "/search.html?v=" + query + "&off=" + temp_min;
					 window.location =new_location;
				 })
				 
				
			}
			else {
				alert(data + " is NaN");
			}
		});
	
	});
}


function showResult(name, description){
	$('<li>' + ". " + name + '<br/>' + description + '</li>').appendTo('#results');
	
}

function serviceChange(id) {
        var id_val = $('#' + id).val();
	$.get('/services/brand_url?sid=' + id_val,
			function(data) {
				if (data.length > 0) {
					window.location = data;
				}
			});
}

function brandChange(id) {
        var id_val = $('#' + id).val();
	$.get('/services/brand_url?bid=' + id_val,
			function(data) {
				if (data.length > 0) {
					window.location = data;
				}
			});
}
