// JavaScript Document

//keeping this on a more global scope allows us to have some more fun with advanced sorting if needed later
var tempArray = new Array();

//just for quick refference
//[business_id][cat_id][name][First Letter][contact][phone][fax][email][web_address][description][address][city][state][zip]

function sortByCategory(){
	//build our temp array
	//go through the current array and copy matching content into the temp array.
	
	//clear the temp array
	tempArray = new Array();
	
	//prepare the output
	var theDiv = document.getElementById("BusinessListings");
	var output = "";
	var category = document.getElementById("category").value;
		
	var counter = 0; // tracks the temp array index
	var len = myData.length;
	
	var i;
	for(i = 0; i < len; i += 1){
		if(category == 0){
			tempArray[counter] = myData[i];
			counter += 1;
		}
		else{
			if(myData[i][1] == category){
				tempArray[counter] = myData[i];
				counter += 1;
			} // end of "new" section
		}
	}
	
	//check if our array is empty, otherwise, display it.
	if(tempArray.length === 0){
		output = "<br /><br /><br /><center>Sorry, there are no businesses listed under these criteria.</center><br /><br /><br />"; 
		theDiv.innerHTML = output;
		return;
	}
	else{
		//sort the array now based on name, since by default it's only sorted by business ID
		sortBy(name);
		
		//now we can print this fun stuff out
		var len2 = tempArray.length;
		var n;
		for(n = 0; n < len2; n += 1){
			//check for an image, that changes everything if one exists
			output += "<div class=\"business-result\">\n";
			output += "<h1>"+tempArray[n][2]+"</h1>\n";
			output += "<table width=\"100%\">\n";
			output += "<tr>\n";
			output += "<td width=\"60%\">\n";
			output += "<div class=\"business-result-left\">\n";
			output += "<span class=\"biz-contact\"><strong>Contact</strong>: "+tempArray[n][4]+"</span><br />\n";
			output += "<span class=\"biz-email\"><strong>Email</strong>: <a href=\"mailto:"+tempArray[n][7]+"\">"+tempArray[n][7]+"</a></span> <br />\n";
			output += "<span class=\"biz-phone\"><strong>Phone</strong>: "+tempArray[n][5]+"</span><br>\n";
			if(tempArray[n][8] != ""){
				if(tempArray[n][8].indexOf("http://") == 0){
					var theLink = tempArray[n][8];
					if(theLink.length >= 33){
						//alert('got one!! :: ' + tempArray[n][2]);
						var linkText = tempArray[n][8].substring(0,14);
						linkText += '...';
						linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
						output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
					}
					else{
						output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
					}
				}
				else{
					var theLink = tempArray[n][8];
					if(theLink.length >= 33){
						//alert('got one!! :: ' + tempArray[n][2]);
						var linkText = tempArray[n][8].substring(0,14);
						linkText += '...';
						linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
						output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
					}
					else{
						output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
					}
				}
			}	
			output += "</div>\n";
			output += "</td>\n";
			output += "<td width=\"40%\">\n";
			output += "<div class=\"business-result-right\"><span class=\"biz-address\">\n";
			output += tempArray[n][10]+"<br />\n";
			output += tempArray[n][11]+", "+tempArray[n][12]+" "+tempArray[n][13]+"</span><br />\n";
			output += "<span class=\"biz-map\"><a href=\"http://maps.google.com/maps?f=q&hl=en&q="+ tempArray[n][10] +"+"+ tempArray[n][11]+"+"+tempArray[n][12]+"+"+tempArray[n][13]+"+United+States&ie=UTF8&t=h&z=16&iwloc=addr\" target=\"_blank\">show the map!</a></span><br />\n";
			output += "<span class=\"biz-fax\"><strong>Fax</strong>: "+tempArray[n][6]+"</span>\n";
			output += "</div>\n";
			output += "</td>\n";
			if(tempArray[n][14] != ""){
				//needs to be updated with the photo refference
				output += "<td class=\"business-result-photo\">\n";
				//output += "<div style=\"height:90px; width:100px; vertical-align:middle; overflow:hidden;\">\n";
				output += "<img src=\""+ tempArray[n][14]+"\" width=\"100\">\n";
				//output += "</div>\n";
				output += "</td>\n";
			}
			output += "</tr>\n";
			output += "<tr>\n";
			output += "<td colspan=\"2\">";
			/*
			if(tempArray[n][8] != ""){
				if(tempArray[n][8].indexOf("http://") == 0){
					var theLink = tempArray[n][8];
					if(theLink.length >= 33){
						//alert('got one!! :: ' + tempArray[n][2]);
						var linkText = tempArray[n][8].substring(0,14);
						linkText += '...';
						linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
						output += "<strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\">"+linkText+"</a> <br />\n";
					}
					else{
						output += "<strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\">"+tempArray[n][8]+"</a> <br />\n";
					}
				}
				else{
					var theLink = tempArray[n][8];
					if(theLink.length >= 33){
						//alert('got one!! :: ' + tempArray[n][2]);
						var linkText = tempArray[n][8].substring(0,14);
						linkText += '...';
						linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
						output += "<strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>"+linkText+"</a> <br />\n";	
					}
					else{
						output += "<strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>"+tempArray[n][8]+"</a> <br />\n";	
					}
				}
			}	
			*/			
			output += "</td>\n";
			output += "</tr>\n";
			output += "</table>\n";
			output += "<span class=\"biz-description\">"+tempArray[n][9]+"</span>\n";
			output += "<div id=\"busDirTopLink\"><a href=\"#top\">Top</a></div>";
			output += "</div>\n\n";
		}
		
		theDiv.innerHTML = output;
		setTimeout('window.scrollTo(0, 0)',1);
		return;
	}
}

function sortByThisCategory(sort_cat_id){ //category will be a number, for the cat ID
	//build our temp array
	//go through the current array and copy matching content into the temp array.
	
	//clear the temp array
	tempArray = new Array();
	
	//prepare the output
	var theDiv = document.getElementById("BusinessListings");
	var output = "";
	var category = document.getElementById("category").value;
		
	var counter = 0; // tracks the temp array index
	var len = myData.length;
	
	var i;
	for(i = 0; i < len; i += 1){
		if(category == 0){
			tempArray[counter] = myData[i];
			counter += 1;
		}
		else{
			if(myData[i][1] == category){
				tempArray[counter] = myData[i];
				counter += 1;
			} // end of "new" section
		}
	}
	
	//check if our array is empty, otherwise, display it.
	if(tempArray.length === 0){
		output = "<br /><br /><br /><center>Sorry, there are no businesses listed under these criteria.</center><br /><br /><br />"; 
		theDiv.innerHTML = output;
		return;
	}
	else{
		//sort the array now based on name, since by default it's only sorted by business ID
		sortBy(name);
		
		//now we can print this fun stuff out
		var len2 = tempArray.length;
		var n;
		for(n = 0; n < len2; n += 1){
			if(tempArray[n][1] == sort_cat_id){
				output += "<div class=\"business-result\">\n";
				output += "<h1>"+tempArray[n][2]+"</h1>\n";
				output += "<table width=\"100%\">\n";
				output += "<tr>\n";
				output += "<td width=\"60%\">\n";
				output += "<div class=\"business-result-left\">\n";
				output += "<span class=\"biz-contact\"><strong>Contact</strong>: "+tempArray[n][4]+"</span><br />\n";
				output += "<span class=\"biz-email\"><strong>Email</strong>: <a href=\"mailto:"+tempArray[n][7]+"\">"+tempArray[n][7]+"</a></span><br />\n";
				output += "<span class=\"biz-phone\"><strong>Phone</strong>: "+tempArray[n][5]+"</span><br>\n";
				if(tempArray[n][8] != ""){
					if(tempArray[n][8].indexOf("http://") == 0){
						var theLink = tempArray[n][8];
						if(theLink.length >= 33){
							//alert('got one!! :: ' + tempArray[n][2]);
							var linkText = tempArray[n][8].substring(0,14);
							linkText += '...';
							linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
							output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
						}
						else{
							output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
						}
					}
					else{
						var theLink = tempArray[n][8];
						if(theLink.length >= 33){
							//alert('got one!! :: ' + tempArray[n][2]);
							var linkText = tempArray[n][8].substring(0,14);
							linkText += '...';
							linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
							output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
						}
						else{
							output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
						}
					}
				}	
				output += "</div>\n";
				output += "</td>\n";
				output += "<td width=\"40%\">\n";
				output += "<div class=\"business-result-right\"><span class=\"biz-address\">\n";
				output += tempArray[n][10]+"<br />\n";
				output += tempArray[n][11]+", "+tempArray[n][12]+" "+tempArray[n][13]+"</span><br />\n";
				output += "<span class=\"biz-map\"><a href=\"http://maps.google.com/maps?f=q&hl=en&q="+ tempArray[n][10] +"+"+ tempArray[n][11]+"+"+tempArray[n][12]+"+"+tempArray[n][13]+"+United+States&ie=UTF8&t=h&z=16&iwloc=addr\" target=\"_blank\">show the map!</a></span><br />\n";
				output += "<span class=\"biz-fax\"><strong>Fax</strong>: "+tempArray[n][6]+"</span>\n";
				output += "</div>\n";
				output += "</td>\n";
				if(tempArray[n][14] != ""){
					//needs to be updated with the photo refference
					output += "<td class=\"business-result-photo\">\n";
					//output += "<div style=\"height:90px; width:100px; vertical-align:middle; overflow:hidden;\">\n";
					output += "<img src=\""+ tempArray[n][14]+"\" width=\"100\">\n";
					//output += "</div>\n";
					output += "</td>\n";
				}
				output += "</tr>\n";
				output += "<tr>\n";
				output += "<td colspan=\"2\">";
				/*
				if(tempArray[n][8] != ""){
					if(tempArray[n][8].indexOf("http://") == 0){
						var theLink = tempArray[n][8];
						if(theLink.length >= 33){
							//alert('got one!! :: ' + tempArray[n][2]);
							var linkText = tempArray[n][8].substring(0,14);
							linkText += '...';
							linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
							output += "<strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\">"+linkText+"</a> <br />\n";
						}
						else{
							output += "<strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\">"+tempArray[n][8]+"</a> <br />\n";
						}
					}
					else{
						var theLink = tempArray[n][8];
						if(theLink.length >= 33){
							//alert('got one!! :: ' + tempArray[n][2]);
							var linkText = tempArray[n][8].substring(0,14);
							linkText += '...';
							linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
							output += "<strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>"+linkText+"</a> <br />\n";	
						}
						else{
							output += "<strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>"+tempArray[n][8]+"</a> <br />\n";	
						}
					}
				}			
				*/
				output += "<br />\n<br />\n";
				output += "</td>\n";
				output += "</tr>\n";
				output += "</table>\n";
				output += "<span class=\"biz-description\">"+tempArray[n][9]+"</span>\n";
				output += "<div id=\"busDirTopLink\"><a href=\"#top\">Top</a></div>";
				output += "</div>\n\n";
			}
		}
		
		theDiv.innerHTML = output;
		setTimeout('window.scrollTo(0, 0)',1);
		return;
	}
}

function sortByLetter(letter){
	//build our temp array
	//go through the current array and copy matching content into the temp array.
	
	//clear the temp array
	tempArray = new Array();
	
	//prepare the output
	var theDiv = document.getElementById("BusinessListings");
	var output = "";
	var category = document.getElementById("category").value;
	
	var counter = 0; // tracks the temp array index
	var len = myData.length;
	
	var i;
	for(i = 0; i < len; i += 1){
		if(category == 0){ // specific categories
			if(myData[i][3].toUpperCase() == letter){
				tempArray[counter] = myData[i];
				counter += 1;
			} // end of "new" section
		}
		else{ // all categories
			if(myData[i][1] == category && myData[i][3].toUpperCase() == letter){
				tempArray[counter] = myData[i];
				counter += 1;
			} // end of "new" section
		}
	}
	
	//check if our array is empty, otherwise, display it.
	if(tempArray.length === 0){
		output = "<br /><br /><br /><center>Sorry, there are no businesses listed under these criteria.</center><br /><br /><br />"; 
		theDiv.innerHTML = output;
		return;
	}
	else{
		//sort the array now based on name, since by default it's only sorted by business ID
		sortBy(name);
		
		//now we can print this fun stuff out
		var len2 = tempArray.length;
		var n;
		for(n = 0; n < len2; n += 1){
			output += "<div class=\"business-result\">\n";
			output += "<h1>"+tempArray[n][2]+"</h1>\n";
			output += "<table width=\"100%\">\n";
			output += "<tr>\n";
			output += "<td width=\"60%\">\n";
			output += "<div class=\"business-result-left\">\n";
			output += "<span class=\"biz-contact\"><strong>Contact</strong>: "+tempArray[n][4]+"</span><br />\n";
			output += "<span class=\"biz-email\"><strong>Email</strong>: <a href=\"mailto:"+tempArray[n][7]+"\">"+tempArray[n][7]+"</a></span><br />\n";
			output += "<span class=\"biz-phone\"><strong>Phone</strong>: "+tempArray[n][5]+"</span><br>\n";
			if(tempArray[n][8] != ""){
				if(tempArray[n][8].indexOf("http://") == 0){
					var theLink = tempArray[n][8];
					if(theLink.length >= 33){
						//alert('got one!! :: ' + tempArray[n][2]);
						var linkText = tempArray[n][8].substring(0,14);
						linkText += '...';
						linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
						output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
					}
					else{
						output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
					}
				}
				else{
					var theLink = tempArray[n][8];
					if(theLink.length >= 33){
						//alert('got one!! :: ' + tempArray[n][2]);
						var linkText = tempArray[n][8].substring(0,14);
						linkText += '...';
						linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
						output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
					}
					else{
						output += "<span class=\"biz-website\"><strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>Click Here</a></span><br />\n";
					}
				}
			}	
			output += "</div>\n";
			output += "</td>\n";
			output += "<td width=\"40%\">\n";
			output += "<div class=\"business-result-right\"><span class=\"biz-address\">\n";
			output += tempArray[n][10]+"<br />\n";
			output += tempArray[n][11]+", "+tempArray[n][12]+" "+tempArray[n][13]+"</span><br />\n";
			output += "<span class=\"biz-map\"><a href=\"http://maps.google.com/maps?f=q&hl=en&q="+ tempArray[n][10] +"+"+ tempArray[n][11]+"+"+tempArray[n][12]+"+"+tempArray[n][13]+"+United+States&ie=UTF8&t=h&z=16&iwloc=addr\" target=\"_blank\">show the map!</a></span><br />\n";
			output += "<span class=\"biz-fax\"><strong>Fax</strong>: "+tempArray[n][6]+"</span>\n";
			output += "</div>\n";
			output += "</td>\n";
			if(tempArray[n][14] != ""){
				//needs to be updated with the photo refference
				output += "<td class=\"business-result-photo\">\n";
				//output += "<div style=\"height:90px; width:100px; vertical-align:middle; overflow:hidden;\">\n";
				output += "<img src=\""+ tempArray[n][14]+"\" width=\"100\">\n";
				//output += "</div>\n";
				output += "</td>\n";
			}
			output += "</tr>\n";
			output += "<tr>\n";
			output += "<td colspan=\"2\">";
			/*
			if(tempArray[n][8] != ""){
				if(tempArray[n][8].indexOf("http://") == 0){
					var theLink = tempArray[n][8];
					if(theLink.length >= 33){
						//alert('got one!! :: ' + tempArray[n][2]);
						var linkText = tempArray[n][8].substring(0,14);
						linkText += '...';
						linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
						output += "<strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\">"+linkText+"</a> <br />\n";
					}
					else{
						output += "<strong>Web Site</strong>: <a href=\""+tempArray[n][8]+"\">"+tempArray[n][8]+"</a> <br />\n";
					}
				}
				else{
					var theLink = tempArray[n][8];
					if(theLink.length >= 33){
						//alert('got one!! :: ' + tempArray[n][2]);
						var linkText = tempArray[n][8].substring(0,14);
						linkText += '...';
						linkText += tempArray[n][8].substring(tempArray[n][8].length-15,tempArray[n][8].length-1);
						output += "<strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>"+linkText+"</a> <br />\n";	
					}
					else{
						output += "<strong>Web Site</strong>: <a href=\"http://"+tempArray[n][8]+"\" target='_blank'>"+tempArray[n][8]+"</a> <br />\n";	
					}
				}
			}			
			*/
			output += "<br />\n";
			output += "</td>\n";
			output += "</tr>\n";
			output += "</table>\n";
			output += "<span class=\"biz-description\">"+tempArray[n][9]+"</span>\n";
			output += "<div id=\"busDirTopLink\"><a href=\"#top\">Top</a></div>";
			output += "</div>\n\n";
		}
		
		theDiv.innerHTML = output;
		setTimeout('window.scrollTo(0, 0)',1);
		return;
	}
}

var nameSort = "asc";

function sortBy(field){
	//run the sort function
	if(field == "name"){
		tempArray.sort(sortByName);
		if(nameSort == "desc"){
			tempArray.reverse();
			nameSort = "asc";
		}
		else{
			revSort = "desc";
		}
		//reset the others
	}	
}

function sortByName(a, b) {
	//sorts by business name
    a = a[2];
	b = b[2];
	return a == b ? 0 : (a < b ? -1 : 1);
}

function sortByStandard(a, b) {
	//sorts by standard then revision
    a = a[0] + a[1];
	b = b[0] + b[1];
	return a == b ? 0 : (a < b ? -1 : 1);
}

function sortBySubmitter(a, b) {
    a = a[2];
	b = b[2];
	return a == b ? 0 : (a < b ? -1 : 1);
}

function sortBySubmitDate(a, b) {
	//order by date, then revisionID
    a = a[3] + a[1];
	b = b[3] + b[1];
	return a == b ? 0 : (a < b ? -1 : 1);
}
