
var categories = null;
var xmlhttp = null;
/**
 * loadxml(url)
 * 
 * loads an xml file
 */
function loadxml(url){
	try{
	if(window.XMLHttpRequest){
		xmlhttp= new XMLHttpRequest();
	} else if(window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return;
	}
	xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	}catch(error){
//		alert("error " + error);
	}
}


function storesinit(xmlfilename){
	loadxml(xmlfilename);
	categories = xmlhttp.responseXML.getElementsByTagName("category");
}

function loadlocals(){

	for(var x = 0; x < categories.length; x ++){
		var table = "";
		table += '<table  width="100%" cellspacing="0" cellpadding="0" border="0">';
		//tabel += '<tr>';
		table += '<tr>';
		table += '</tr>';

		var bins = categories[x].getElementsByTagName("bin");
		//var bin = categories[0].getElementsByTagName("bin");
	
	
		for(var y = 0; y < bins.length; y ++){
			var vItems =3;
			if (x==1) {
				vItems = 10; 	
			}
			if((y + vItems) % vItems == 0){
				table += '	<td><table width="150px"><tr>';		//ever 4th cell
			}
			table += "		<tr>";
			table += "		<td>";
			table += "			<a href=\"javascript:void(0);\" onclick=\"openStorePopUp('./statespopup.html?bin=" + bins[y].getAttribute("name").replace(" ", "+") + "&category=" + categories[x].getAttribute("name").replace(" ", "+") + "')\" >";
			table += 				bins[y].getAttribute("name")
			table += "			</a>";
			table += "		</td>";
			table += "		</tr>";

			if( y == bins.length -1){	//last one
				while((y +1) % vItems != 0){
					table += "<tr><td>&nbsp;</td></tr>";
					y ++;
				}
			}
		
			if((y +1) % vItems == 0 ){
				table += "	</tr></table></td>";		//every 4th cell
				
			}
			
		}
		
		
		
/*		if(x=1){
		for(var y = 0; y < bins.length; y ++){
			if((y + 9) % 9 == 0){
				table += '	<td><table><tr>';		//ever 4th cell
			}
			table += "		<tr>";
			table += "		<td>";
			table += "			<a href=\"javascript:void(0);\" onclick=\"openStorePopUp('./statespopup.html?bin=" + bins[y].getAttribute("name").replace(" ", "+") + "&category=" + categories[x].getAttribute("name").replace(" ", "+") + "')\" >";
			table += 				bins[y].getAttribute("name")
			table += "			</a>";
			table += "		</td>";
			table += "		</tr>";

			if( y == bins.length -1){	//last one
				while((y +1) % 9 != 0){
					table += "<tr><td>&nbsp;</td></tr>";
					y ++;
				}
			}
		
			if((y +1) % 9 == 0 ){
				table += "	</tr></table></td>";		//every 4th cell
				
			}
		}
		}*/
		
		//......................................................
		
/*		for(var y = 0; y < bin.length; y ++){
			if((y + 3) % 3 == 0){
				table += '	<td></td></table><table  width="100%" cellspacing="0" cellpadding="0" border="0"><table><tr>';		//ever 4th cell
			}
			table += "		<tr>";
			table += "		<td>";
			table += "			<a href=\"javascript:void(0);\" onclick=\"openStorePopUp('./statespopup.html?bin=" + bin[y].getAttribute("name").replace(" ", "+") + "&category=" + categories[0].getAttribute("name").replace(" ", "+") + "')\" >";
			table += 				bin[y].getAttribute("name")
			table += "			</a>";
			table += "		</td>";
			table += "		</tr>";
			
			
			if( y == bin.length -1){	//last one
				while((y +1) % 3 != 0){
					table += "<tr><td>&nbsp;</td></tr>";
					y ++;
				}
			}
			if((y +1) % 3 == 0 ){
				table += "	</tr></table>";		//every 4th cell
				
			}
		}*/
		//.................................................................
		//table += "</tr>";
		
		table += "</table>";
		//alert(table);
		document.getElementById(categories[x].getAttribute("name")).innerHTML = table;
	}

	
	
	//omniturecode
	setvars("home:", "store");
	hit();
	
}


/**
 * load the state on the page
 */
function loadbin(){
	//load vars from url
	//catagory, bin
	var searchdata = window.location.search.substr(1);
	var searcharray = searchdata.split(/[&;]/g);

	var line = null;
	var category = null;
	var bin = null;
	var rank_value = null;

	//get the url information
	for(var i = 0; i < searcharray.length; i ++){
		searcharray[i] = searcharray[i].replace(/\+/g," ");
		var searchpair = searcharray[i].split("=");
		if(searchpair[0] == "bin"){
			bin = searchpair[1];
		}
		if(searchpair[0] == "category"){
			category = searchpair[1];
		}
	}
	if(category == null || bin == null){	//no info given
		return;
	}
//	alert("bin: " + bin + " cat: " + category);

	//set image based on category name
	document.getElementById("categoryimage").src = "../images/store_images/pop_titles/" + bin.replace(" ", "").toLowerCase() + ".gif";
	document.getElementById("categoryimage").alt = bin;

	//create table
	var stores = null;
	
	//finds all the stores inside a sorp (state or province)
	for(var x = 0; x < categories.length; x ++){
		if(categories[x].getAttribute("name") == category){
			var thecategory = categories[x].getElementsByTagName("bin");
			for(var y = 0; y < thecategory.length; y ++){
				if(thecategory[y].getAttribute("name") == bin){
					stores = thecategory[y].getElementsByTagName("store");
				}
			}
		}
	}
	if(stores == null){
		return;	//could not find the bin
	}
	
	var table = "";
	table += '<table  width="100%" cellspacing="7px" cellpadding="0" border="0">';
	//set table header
	
	
	table += '<tr>'
	table += '	<td width="15%" style="vertical-align:bottom" class="underline"><img src="../images/store_images/stores_subheadings_f07.gif" width="37" height="12" alt="City" /></td>'
	table += '	<td width="28%" style="vertical-align:bottom" class="underline"><img src="../images/store_images/stores_subheadings_f06.gif" width="51" height="12" alt="Store" /></td>'
	table += '	<td width="19%" style="vertical-align:bottom" class="underline"><img src="../images/store_images/stores_subheadings_f05.gif" width="58" height="12" alt="Phone Number" /></td>'
	table += '	<td width="30%" style="vertical-align:bottom" class="underline"><img src="../images/store_images/stores_subheadings_f03.gif" width="45" height="12" alt="Lines" /></td>'
	table += '	<td width="8%" style="vertical-align:bottom" class="underline"><img src="../images/store_images/stores_subheadings_f09.gif" width="77" height="27" alt="Store Ranking" /></td>'
	table += '</tr>'
/*	table += '<tr>';
	table += '<td>&nbsp;</td>';
	table += '<td>&nbsp;</td>';
	table += '<td>&nbsp;</td>';
	table += '<td>&nbsp;</td>';
	table += '<td>&nbsp;</td>';
	table += '</tr>';*/
	
	
	for(var x = 0; x < stores.length; x ++){
		table += "<tr>";
		table += "<td>";
		table += stores[x].getAttribute("city");
		table += "</td>";
		table += "<td>";
		table += stores[x].getAttribute("name");
		table += "</td>";
		table += "<td>";
		table += stores[x].getAttribute("phone");
		table += "</td>";
		table += "<td>";
		table += stores[x].getAttribute("lines");
		table += "</td>";
		table += "<td>";
		table += '<img src="../images/store_images/';
		
		//checking rank value
		rank_value = checkBlank(stores[x].getAttribute("rank"));
		
		table += rank_value;
		table += '.jpg" alt="">';
		table += "</td>";		
		table += "</tr>";
	}
	table += "</table>";

	document.getElementById("stores").innerHTML = table;
	
	//omniturecode
	setvars("home:", "store." + category + "." + bin);
	hit();
}

//Checks if rank from xml is blank, if so sets value to "blank"
function checkBlank(value) {
	var change = "blank";
	if (value == "")		
		return change;
	else
		return value;
}

/**
 * Hit box code execution
 *
 */


/**
 * setvars
 *
 * set the omniture vars
 * path refers to MULTI-LEVEL CONTENT CATEGORY
 * name refers to page name
 */
 var mlc = "";
 var pn = "";
function setvars(path, name){
	mlc = path;
	pn = name;
}

//count this as a hit
function hit(){
	document.getElementById("omnitureiframe").src = "http://www.disneybridal.com/omniture/omniture.html?path=" + mlc + "&pagename=" + pn;
}
