function getHTTPObject()
{ 
	var xmlhttp; 

	if(window.XMLHttpRequest)
	{ 
		xmlhttp = new XMLHttpRequest(); 
	} 
	else if (window.ActiveXObject)
	{ 
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		if (!xmlhttp)
		{ 
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
		}    
	} 
	return xmlhttp;   
} 
var http = getHTTPObject();


function handlehttpresponse()
{    
	if (http.readyState == 4)
	{ 
		if(http.status==200)
		{ 
			var results = http.responseText; 
		    result_splited = results.split('&&');
			var final_result = result_splited[0].split('#');
			message = final_result[0];
			rate_item_original = final_result[1];
			rate_item = rate_item_original;
			
			votes_needed = final_result[2];
			id = final_result[3];
			amount_of_votes = final_result[4];
			voted_star = final_result[5];
			cat = final_result[6];
			
			message_div = id + '_'+'message';
			rate_div = id + '_'+'rate';
			rank_div = id + '_'+ 'rank';
			vote_div = id + '_'+'votes';
			
				
			if(document.getElementById(rate_div))
			{
				if(rate_item > 0)
				{
					//document.getElementById(rate_div).innerHTML = '(' + rate_item + ') Stars';
					document.getElementById(rate_div).innerHTML =  rate_item + ' Stars';						
					document.getElementById(rate_div).title =	rate_item_original;	
				}
				else
				{
					document.getElementById(rank_div).innerHTML =  votes_needed + ' more votes needed';
				
				}
			}
			if(document.getElementById(message_div))
			{
				document.getElementById(message_div).innerHTML = message;
			}
			if(document.getElementById(vote_div))
			{
				document.getElementById(vote_div).innerHTML = "Votes:"+ amount_of_votes;
			}	
			
			var star_div        = id +'_stars';
			var half_stars = 0;
			var str_star ='';
			var count_stars = 1;
			var calculate_stars = rate_item.split('.');
			var yellow_stars    =  calculate_stars[0];
			
			var star_name = 'test_'+id+'_';
			if(calculate_stars[1] >= 500)
			{
				half_stars = 1;
			} 
			
			var grey_stars = (parseInt(yellow_stars) + parseInt(half_stars));
			
			for(var i=0;i<yellow_stars;i++)
			{
				rate_star = i + 1;
				image_name = star_name + rate_star; 
				if(count_stars <= voted_star)
				{
				   
				   str_star = str_star + "<img name='"+image_name+"' class='rating' src='images/yellowborder.jpg'   border='0'";
                
				
				}
				else
				{
					str_star = str_star + "<img name='"+image_name+"' class='rating' src='images/yellow.jpg'  border='0'";

				
				}
				str_star = str_star + " onclick=\"ratebar("+id+","+ rate_star+",'"+cat+"')\" />&nbsp;";
		
				count_stars++; 
			}
			if(half_stars == 1 )
			{
				rate_star = yellow_stars + 1;
				
				image_name = star_name + rate_star; 
				if(count_stars <= voted_star)
				{
				str_star = str_star + "<img name='"+image_name+"' class='rating' src='images/halfyellowborder.jpg'  height='30' width='30'border='0'";
				
				
				}
				else
				{
					str_star = str_star + "<img name='"+image_name+"' class='rating' src='images/halfgray.jpg'  height='30' width='30' border='0'";
				
				}
				str_star = str_star + " onclick=\"ratebar("+id+","+ rate_star+",'"+cat+"')\" />&nbsp;";

							
				count_stars++;
			
			}
			
		 	for(var i=grey_stars+1;i<=5;i++)
	        {
	
				image_name = star_name + i; 
				if(count_stars <= voted_star)
				{
					str_star = str_star + "<img name='"+image_name+"' class='rating' src='images/grayborder.jpg'  border='0'";
				}
				else
				{
					str_star = str_star + "<img name='"+image_name+"' class='rating' src='images/gray.jpg'  height='30' width='30' border='0'";	
				
				}
				str_star = str_star + " onclick=\"ratebar("+id+","+i+",'"+cat+"')\" />&nbsp;";
		
				count_stars++;	
				
				
			}
			
			star_div = id +'_stars';
			str_star = escape(str_star);
			document.getElementById(star_div).innerHTML ='';
			document.getElementById(star_div).innerHTML = unescape(str_star);
			
			rank_str = result_splited[1].split(',');
			for(j=0;j<(rank_str.length)-1;j++)
			{
				rank_id = rank_str[j].split('-');
				rank_div = rank_id[0]+'_rank';
				if(document.getElementById(rank_div))
				{
					if(rank_id[1] == 0)
					{
						document.getElementById(rank_div).innerHTML = rank_id[2] + " more votes needed";
					}
					else
					{
						document.getElementById(rank_div).innerHTML =  'Ranked (#' + rank_id[1] + ')';
					}	
				}	
				
			
			}
			
			
			
		}
	} 
	 
} 


function ratebar(id,vote,category)
{
	if(document.bars_search.state)
	{
		var state = document.bars_search.state.value;	
		var city  = document.bars_search.city_name.value;	
	   	var url = 'process_vote.php?id='+id+'&vote='+vote+'&category='+category+'&state='+state+'&city='+city; 
	}
	else
	{
		var url = 'process_vote.php?id='+id+'&vote='+vote+'&category='+category;	
	}
	var view_option = document.getElementById('view').value;
	
	if(view_option != '' && view_option !='Bar' && view_option !='Club')
    {
		url = url + "&type="+view_option;	
	}
	http.open('GET',url, true); 
	http.onreadystatechange = handlehttpresponse; 
	http.send(null);
}

function ratebar_all(id,vote,category)
{
	
   	var url = 'process_vote.php?id='+id+'&vote='+vote+'&category='+category; 
	http.open('GET',url, true); 
	
	http.onreadystatechange = handlehttpresponse; 
	
	http.send(null); 
}

function validate()
{
	var keyword	=	document.bars_search.keyword.value;
	var type	=	document.bars_search.type_browse.value;

	if(keyword == "" && type == 'Zip')
	{
		alert('Please Enter Zip to search');
		return false;
	}
    else if(validate_numeric_only(keyword) == false)
	{
		alert(' Enter proper Zip Code'+ "\n");
		return false;
	}
	else
	{
		return true;
	}
	
}
function validate_numeric_only(zip) 
	{
			var str="0123456789"; 
			var i=0; 
			zip=zip.replace(/(\s+)$/,""); 
			zip=zip.replace(/^(\s+)/,""); 
		
			for(i=0;i < zip.length;i++)
			{	
				if(zip.charAt(i)!='') 
				{ 
					if (str.indexOf(zip.charAt(i)) == -1) 
					{ 
						return false; 
					} 
				}				
			}
				return true;

	}



function handlehttpresponse_city()
{    
	if (http.readyState == 4)
	{ 
		if(http.status==200)
		{ 
			var results=http.responseText; 
		    document.getElementById('cityone').innerHTML = results; 			
			
		}
	} 
} 

function get_city()
{
    var state_id = document.bars_search.state.value;	
	var city_name= document.bars_search.city_name.value;	
		
	if(city_name != '')
	{
		var url = 'get_city.php?state_id='+state_id+'&city_name='+city_name; 
	}
	else
	{
		var url = 'get_city.php?state_id='+state_id; 
	}
	http.open('GET',url, true); 
	
	http.onreadystatechange = handlehttpresponse_city; 
	
	http.send(null); 
}

function chk_type()
{
	
	if(document.getElementById('type_browse').value == 'City')
	{
		get_city();
		document.getElementById('cityone').style.display		=	'';
		document.getElementById('keyword').style.display	=	'none';			
	}
	else if(document.getElementById('type_browse').value == 'Zip')
	{
		document.getElementById('cityone').style.display	=	'none';
		document.getElementById('keyword').style.display	=	'';
	}
	else
	{
		document.getElementById('cityone').style.display	=	'';
		document.getElementById('keyword').style.display	=	'none';
	}
}

function change_image(current_image,total_images,fraction,id,cat)
{
	
	var count = 1;
	var is_half = 0;
	var str ='';
	var images = new Array();
		
	for(var j=0;j<total_images;j++)
	{
		images[count++] = 'images/yellowborder.jpg';
	}
	if(fraction >= 500)
	{
		half = 1;
		is_half = 0;
		images[count++] = 'images/halfyellowborder.jpg';

	}
	var gray_images = 5 - (total_images + is_half);
	
	
	for(var k=0; k < gray_images;k++)
	{
		images[count++] = 'images/grayborder.jpg';
	}
	
	for(var x=1;x<=current_image;x++)
	{
		 name = 'test_'+id+'_'+x;
		
		 document[name].src = images[x];
		
	}
  
}

function remove_image(current_image,total_images,fraction,id,cat,prev_vote)
{
	var count = 1;
	var is_half = 0;
	var str ='';
	var images = new Array();
	
	
	for(var j=0;j < total_images;j++)
	{
		images[count++] = 'images/yellow.jpg';
    }
	if(fraction > 500)
	{
		is_half = 1;
		images[count++] = 'images/halfyellow.jpg';
    }
	var gray_images =  5 - parseInt(total_images) + parseInt(is_half);
	for(var k=0;k < gray_images;k++)
	{
		images[count++] = 'images/gray.jpg';
    }
	for(var x=1;x<=current_image;x++)
	{
		name = 'test_'+id+'_'+x;
		document[name].src = images[x];
	}
	
  

}
