var storeCode = "ACF";
var intl = "USA";
var inSearch=0;
var noop = {};
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function switchState(o){
	intl = o.val();
	$(".stateList").hide();
	$("#"+intl).show();
}
function handleKeyPress(e,form,n){
	var key=e.keyCode || e.which;
	if (key==13){
		if(form=='zip'){
			searchZipCode();
			return false;
		}
		if(form=='state'){
			searchState();
			return false;
		}		
		if(form=='stateZip'){
			routeSearch(storeResults[n],n);
			return false;
		}
		return false;
	}
}
var storeResults = new Array();
function storeObject(toLatitude,toLongitude,fromLatitude,fromLongitude,storeCode,method,isStateSearch,isStartGeocoded,isDrillDown,toAddressLine,toPrimaryCity,postalCode,toPostalCode,toAddress1,whatState){
	this.toLatitude = toLatitude; 
	this.toLongitude = toLongitude; 
	this.fromLatitude = fromLatitude; 
	this.fromLongitude = fromLongitude;
	this.storeCode = storeCode;
	this.method = method;
	this.isStateSearch = isStateSearch;
	this.isStartGeocoded = isStartGeocoded;
	this.isDrillDown = isDrillDown;
	this.toAddressLine = toAddressLine;
	this.toPrimaryCity = toPrimaryCity;
	this.postalCode = postalCode;
	this.toPostalCode = toPostalCode;
	this.toAddress1 = toAddress1;
	this.whatState = whatState;
}
function stateStoreObject(toLatitude,toLongitude,destAddress,destCity,destState,destZip,destPhone,errorRowNumber,isStartGeocoded,method,isStateSearch,state,whatState,isDrillDown,storeCode,toAddressLine,toAddress1){
	this.toLatitude = toLatitude;
	this.toLongitude = toLongitude;
	this.destAddress = destAddress;
	this.destCity = destCity;
	this.destState = destState;
	this.destZip = destZip;
	this.destPhone = destPhone;
	this.errorRowNumber = errorRowNumber;
	this.isStartGeocoded = isStartGeocoded;
	this.method = method;
	this.isStateSearch = isStateSearch;
	this.state = state;
	this.whatState = whatState;
	this.isDrillDown = isDrillDown;
	this.storeCode = storeCode;
	this.toAddressLine = toAddressLine;
	this.toAddress1 = toAddress1;
}
function searchZipCode(){
	inSearch=1;
	if($("#postalCode").val()==""){
		$("#errorMessage").html(SLCONS.SL_ERROR02);
		return false;
	}	
	$("#loading").show();	
	$("#genLeftCol").hide();
	$("#genRightCol").hide();
	$("#resultsTable").html(' ');

	var getUrl= "/storelocator/searchAPI.do?addressLine=&primaryCity=&method=search&isStateSearch=N&storeCode="+storeCode+"&postalCode=";
	getUrl += $("#postalCode").val();
	getUrl += "&storeProximity=";
	getUrl += $("#storeProximity").val();

	$.ajax({
	    type: "POST",
		url: getUrl,
		data: noop,
		dataType: "xml",
		success: function(xml){
			$("#loading").hide();
			if($(xml).find('error').text()=='Y'){
				//Error has occured
				$("#pageTitle").html(SLCONS.STORELOCATOR);
				$("#titleDescription").html(SLCONS.SL_DESC);
				$("#mappedResults").attr("src", '/anf/images/spacer.gif');
				$("#ppNotes").hide();
				$("#genLeftCol").show();
				$("#genRightCol").show();
				$("#results").hide();
				
				var err = trim($(xml).find('errorMsg').text());

				if(err == 'ERROR1'){
					$("#errorMessage").html(SLCONS.SL_ERROR01);
				}else if(err == 'ERROR2'){
					$("#errorMessage").html(SLCONS.SL_ERROR02);
				}else if(err == 'ERROR3'){
					$("#errorMessage").html(SLCONS.SL_ERROR03);
				}else if(err == 'ERROR4'){
					$("#errorMessage").html(SLCONS.SL_ERROR04);
				}else if(err == 'ERROR5'){
					$("#errorMessage").html(SLCONS.SL_ERROR05);
				}
			}else{
				//GetMap
				$("#mappedResults").attr("src", $(xml).find('mapURL').text());

				//Set Title & description
				$("#pageTitle").html(SLCONS.SL_SEARCHRESULTS);
				var tmpDesc = SLCONS.SL_CLOSESTSTORE.replace("{0}", $("#storeProximity").val()).replace("{1}", $("#postalCode").val());				
				$("#titleDescription").html(tmpDesc);					
				var tableHTML = "<table class='slResultsTable'><tr class='altRow'>";
	 				tableHTML +="<td width='440' class='storeHeader tHead txtLft'>"+SLCONS.SL_LOCATION+"</td>";
	 				tableHTML +="<td width='250' class='storeHeader tHead'>"+SLCONS.SL_DIRECTIONS+"</td>";
	 				tableHTML +="<td width='270' class='storeHeader tHead'>"+SLCONS.SL_CURRENTHOURS+"</td></tr>";
			
				var tmpCt=0;
				$(xml).find('store').each(function(){
					tmpCt++;
					if(tmpCt%2==0){
						tableHTML += "<tr class='altRow'>";
					}else{
						tableHTML += "<tr>";
					}

					tableHTML += "<td class='frstCol'>";
					var id = $(this).attr('id');
					var toAddress1 = $(this).find('toAddress1').text();
					var toAddressLine = $(this).find('toAddressLine').text();
					var primaryCity = $(this).find('primaryCity').text();
					var state = $(this).find('state').text();
					var postalCode = $(this).find('postalCode').text();
					var phoneNumber = $(this).find('phoneNumber').text();

					tableHTML += "<div style='line-height:13px;'><span class='storeHeader'>"+tmpCt+" "+toAddress1+"</span><br/>";
					tableHTML += toAddressLine+"<br/>";
					tableHTML += primaryCity+", "+state+" "+postalCode+"<br/>";
					tableHTML += phoneNumber+"<br/>";
					tableHTML += "</div></td>";

					tableHTML += "<td class='txtcntr'><a href='#nogo' onclick='routeSearch(storeResults["+tmpCt+"])'>"+SLCONS.SL_GETMAPSDIRECTIONS+"</a></td>";
				
					if($(this).find('storeHours').text()!=''){
						tableHTML += "<td class='pdbtm' style='text-align:center;'>";
						tableHTML += "<span class='callfh'>"+SLCONS.SL_CALLFORHOURS+"</span><br/>";
					}else{
						tableHTML += "<td class='pdbtm'>";
						$(this).find('hours').each(function(){
							tableHTML += "<span class='tbday'>"+SLCONS.SL_MONDAY+": </span><span class='tbtime'>"+$(this).find('monday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_TUESDAY+": </span><span class='tbtime'>"+$(this).find('tuesday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_WEDNESDAY+": </span><span class='tbtime'>"+$(this).find('wednesday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_THURSDAY+": </span><span class='tbtime'>"+$(this).find('thursday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_FRIDAY+": </span><span class='tbtime'>"+$(this).find('friday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_SATURDAY+": </span><span class='tbtime'>"+$(this).find('saturday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_SUNDAY+": </span><span class='tbtime'>"+$(this).find('sunday').text()+"</span><br/>";
						});
					}
					tableHTML += "</td></tr>";
				
					//Build Store Object
					var toLatitude = $(this).find('toLatitude').text();
					var toLongitude = $(this).find('toLongitude').text();
					var fromLatitude = $(this).find('fromLatitude').text();
					var fromLongitude = $(this).find('fromLongitude').text();
					var storeCode = $(this).find('storeCode').text();
					var method = $(this).find('method').text();
					var isStateSearch = $(this).find('isStateSearch').text();
					var isStartGeocoded = $(this).find('isStartGeocoded').text();
					var isDrillDown = $(this).find('isDrillDown').text();
					var toAddressLine = $(this).find('toAddressLine').text();
					var toPrimaryCity = $(this).find('toPrimaryCity').text();
					var postalCode = $(this).find('postalCode').text();
					var toPostalCode = $(this).find('toPostalCode').text();
					var toAddress1 = $(this).find('toAddress1').text();
					var whatState = $(this).find('state').text();
					storeResults[tmpCt] = new storeObject(toLatitude,toLongitude,fromLatitude,fromLongitude,storeCode,method,isStateSearch,isStartGeocoded,isDrillDown,toAddressLine,toPrimaryCity,postalCode,toPostalCode,toAddress1,whatState);
				});

				tableHTML += "</table>";
				$("#resultsTable").append(tableHTML);
				$("#results").show();
				$("#ppNotes").show();
			}
			inSearch=0;
		}
	});
}

function searchState(){
	if(!$("#stateSearch .stateList:visible").find("select option:selected").val()) {
		$("#errorMessage").html(SLCONS.SL_ERROR04);
		return false;
	}

	$("#loading").show();
	inSearch=1;
	$("#genLeftCol").hide();
	$("#genRightCol").hide();
	$("#resultsTable").html(' ');

	var stateAbbrev = $("#State"+intl).val();
	var stateFull = $("#State"+intl+" option:selected").text();
	var getUrl= "/storelocator/searchAPI.do?state="+stateAbbrev+"&storeCode="+storeCode+"&method=searchState&isStateSearch=Y&whatState="+stateFull;

	$.ajax({
	    type: "POST",
		url: getUrl,
		data: noop,
		dataType: "xml",
		success: function(xml){
			$("#loading").hide();
			if($(xml).find('error').text()=='Y'){
				//Error has occured
				$("#pageTitle").html(SLCONS.STORELOCATOR);
				$("#titleDescription").html(SLCONS.SL_DESC);
				$("#mappedResults").attr("src", '/anf/images/spacer.gif');
				$("#ppNotes").hide();
				$("#genLeftCol").show();
				$("#genRightCol").show();
				$("#results").hide();
				
				var err = trim($(xml).find('errorMsg').text());				
				if(err == 'ERROR1'){
					$("#errorMessage").html(SLCONS.SL_ERROR01);
				}else if(err == 'ERROR2'){
					$("#errorMessage").html(SLCONS.SL_ERROR02);				
				}else if(err == 'ERROR3'){
					$("#errorMessage").html(SLCONS.SL_ERROR03);
				}else if(err == 'ERROR4'){
					$("#errorMessage").html(SLCONS.SL_ERROR04);
				}else if(err == 'ERROR5'){
					$("#errorMessage").html(SLCONS.SL_ERROR05);
				}
			}else{
				//GetMap
				$("#mappedResults").attr("src", $(xml).find('mapURL').text());

				//Set Title & description
				$("#pageTitle").html(SLCONS.SL_SEARCHRESULTS);
				var tmpDesc = SLCONS.SL_WITHINSTATE.replace("{0}", stateFull);
				$("#titleDescription").html(tmpDesc);

				var tableHTML = "<table cellspacing='0' cellpadding='0' class='slResultsTable'><tr class='altRow'>";
 					tableHTML +="<td width='440' class='storeHeader tHead txtLft lCell'>"+SLCONS.SL_LOCATION+"</td>";
 					tableHTML +="<td width='250' class='storeHeader tHead lCell'>"+SLCONS.SL_DIRECTIONS+"</td>";
	 				tableHTML +="<td width='270' class='storeHeader tHead'>"+SLCONS.SL_CURRENTHOURS+"</td></tr>";

				var tmpCt=0;
				$(xml).find('store').each(function(){
					tmpCt++;
					if(tmpCt%2==0){
						tableHTML += "<tr class='altRow'>";
					}else{
						tableHTML += "<tr>";
					}

					tableHTML += "<td class='frstCol lCell'>";
					var id = $(this).attr('id');
					var toAddress1 = $(this).find('toAddress1').text();
					var toAddressLine = $(this).find('toAddressLine').text();
					var primaryCity = $(this).find('toPrimaryCity').text();
					var state = $(this).find('state').text();
					var postalCode = $(this).find('postalCode').text();
					var phoneNumber = $(this).find('phoneNumber').text();

					tableHTML += "<div style='line-height:13px;'><span class='storeHeader'><a name='row"+tmpCt+"'>"+tmpCt+" "+toAddress1+"</a></span><br/>";
					tableHTML += toAddressLine+"<br/>";
					tableHTML += primaryCity+", "+state+" "+postalCode+"<br/>";
					tableHTML += phoneNumber+"<br/>";
					tableHTML += "</div></td>";

					tableHTML += "<td id='zipCell"+tmpCt+"' class='txtcntr lCell'><a href='#nogo' onclick='getZip("+tmpCt+")'>"+SLCONS.SL_GETMAPSDIRECTIONS+"</a></td>";
				
					if($(this).find('storeHours').text()!=''){
						tableHTML += "<td class='pdbtm lCell' style='text-align:center;'>";
						tableHTML += "<span class='callfh'>"+SLCONS.SL_CALLFORHOURS+"</span><br/>";
					}else{
						tableHTML += "<td class='pdbtm'>";
						$(this).find('hours').each(function(){
							tableHTML += "<span class='tbday'>"+SLCONS.SL_MONDAY+": </span><span class='tbtime'>"+$(this).find('monday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_TUESDAY+": </span><span class='tbtime'>"+$(this).find('tuesday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_WEDNESDAY+": </span><span class='tbtime'>"+$(this).find('wednesday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_THURSDAY+": </span><span class='tbtime'>"+$(this).find('thursday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_FRIDAY+": </span><span class='tbtime'>"+$(this).find('friday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_SATURDAY+": </span><span class='tbtime'>"+$(this).find('saturday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_SUNDAY+": </span><span class='tbtime'>"+$(this).find('sunday').text()+"</span><br/>";
						});
					}
					tableHTML += "</td></tr>";

					//Build Store Object
					var toLatitude = $(this).find('toLatitude').text();
					var toLongitude = $(this).find('toLongitude').text();
					var destAddress = $(this).find('destAddress').text();
					var destCity = $(this).find('destCity').text();
					var destState = $(this).find('destState').text();
					var destZip = $(this).find('destZip').text();
					var destPhone = $(this).find('destPhone').text();
					var errorRowNumber = $(this).find('errorRowNumber').text();
					var isStartGeocoded = $(this).find('isStartGeocoded').text();				
					var method = 'routePath';
					var isStateSearch = $(this).find('isStateSearch').text();
					var state = $(this).find('state').text();
					var whatState = $(this).find('whatState').text();
					var isDrillDown = $(this).find('isDrillDown').text();											
					var storeCode = $(this).find('storeCode').text();
					var toAddressLine = $(this).find('toAddressLine').text();
					var toAddress1 = $(this).find('toAddress1').text();
					storeResults[tmpCt] = new stateStoreObject(toLatitude,toLongitude,destAddress,destCity,destState,destZip,destPhone,errorRowNumber,isStartGeocoded,method,isStateSearch,state,whatState,isDrillDown,storeCode,toAddressLine,toAddress1);
				});

				tableHTML += "</table>";
				$("#resultsTable").append(tableHTML);
				$("#results").show();
				$("#ppNotes").show();
			}
			inSearch=0;
		}
	});
}
function getZip(num){
	$("#zipCell"+num).html(' ');
	var pHTML = '<div class="zipBox">'+SLCONS.SL_ENTERPOSTALCODE+'<br/>';
	   pHTML += '<input id="zipCode'+num+'" type="text" class="inZip" onkeypress="handleKeyPress(event,\'stateZip\','+num+')">';
	   pHTML += '<a name="GoButton" id="GoButton" href="#nogo" onclick="routeSearch(storeResults['+num+'],'+num+')" class="buttonClass mrgnTb"><span>'+SLCONS.SL_GO+'</span></a></div>';
	$("#zipCell"+num).html(pHTML);
}
function routeSearch(storeObj,n){
	$("#loading").show();
	inSearch=1;
	if(n!=null){
		storeObj.postalCode=$("#zipCode"+n).val();
	}
	$("#results").hide();
	$("#ppNotes").hide();
	$("#genLeftCol").hide();
	$("#genRightCol").hide();
	$("#routeResults").attr("src", '/anf/images/spacer.gif');	
	$("#routeTable").html(' ');

	var getUrl= "/storelocator/routeAPI.do?";
	$.ajax({
	    type: "POST",
		url: getUrl,
		data: storeObj,
		dataType: "xml",
		success: function(xml){
			$("#loading").hide();
			if($(xml).find('error').text()=='Y'){
				//Error has occured
				var tmpErrorRow = $(xml).find('errorRow').text();

				if($(xml).find('errorMsg').text()=='ERROR1'){
					var errorHTML = "<div class='clearer'></div><span class='error'>"+SLCONS.SL_ERROR02+"</span>";
				}else{
					var errorHTML = "<div class='clearer'></div><span class='error'>"+SLCONS.SL_ERROR06+"</span>";
				}
				$("#zipCell"+tmpErrorRow).append(errorHTML);
				$("#results").show();
				if(tmpErrorRow>2){
					tmpErrorRow--;
					var errorR = "#row"+tmpErrorRow;
					window.location.href=errorR;				
				}
			}else{
				//GetMap
				$("#routeResults").attr("src", $(xml).find('mapURL').text());

				//Set Title & description
				$("#pageTitle").html(SLCONS.SL_MAPSDIRECTIONS);
				$("#titleDescription").html(SLCONS.SL_HEREISMAP);

				var tableHTML =  "<div>";
					tableHTML += "<table class='fromLocTable'>";
					tableHTML += "<tr><td valign='top' class='fromLocTableC1'>";
				tableHTML += "<span class='storeHeader'>FROM</span><br/>"; 
				tableHTML += $(xml).find('fromPostal').text();
				tableHTML += "</td><td>";
				tableHTML += "<span class='storeHeader'>TO</span><br/>";
				tableHTML += $(xml).find('toAddress1').text()+"<br/>";
				tableHTML += $(xml).find('toAddressLine').text()+"<br/>";
				tableHTML += $(xml).find('toPrimaryCity').text()+", "+$(xml).find('toState').text()+" "+$(xml).find('toPostalCode').text();
				tableHTML += "</td></tr></table>";				
				tableHTML += "</div>";
				tableHTML += "<table class='slResultsTable'><tr class='altRow'>";
 				tableHTML +="<td width='750' class='storeHeader tHead txtLft lCell'>"+SLCONS.SL_DIRECTIONS+"</td>";
 				tableHTML +="<td width='210' class='storeHeader tHead txtLft'>"+SLCONS.SL_ESTDISTANCE+"</td></tr>";

				var tmpCt=0;
				$(xml).find('steps').find('step').each(function(){
					tmpCt++;
					if(tmpCt%2==0){
						tableHTML += "<tr class='altRow'>";
					}else{
						tableHTML += "<tr>";
					}
					tableHTML += "<td class='resultsRows lCell'>"+tmpCt+" "+$(this).find('direction').text();
					tableHTML += "</td><td class='resultsRows'>";
					tableHTML += $(this).find('distance').text()+" "+SLCONS.SL_MILES;
					tableHTML += "</td></tr>";
				});
				tmpCt++;
				if(tmpCt%2==0){
					tableHTML += "<tr class='altRow'>";
				}else{
					tableHTML += "<tr>";
				}
				tableHTML += "<td class='lCell'></td><td class='resultsRows'>";
				tableHTML += SLCONS.SL_TOTALDISTANCE+" "+$(xml).find('totalDistance').text()+" "+SLCONS.SL_MILES+"<br/>";
				tableHTML += SLCONS.SL_TOTALTIME+" "+$(xml).find('totalTime').text()+"<br/>";
				tableHTML += "</tr></table>";
				$("#routeTable").append(tableHTML);
				$("#route").show();
				$("#ppNotes").show();
			}
			inSearch=0;
		}
	});
}
function resetSearch(){
	if(inSearch==0){
		$("#errorMessage").html(' ');
		$("#pageTitle").html(SLCONS.STORELOCATOR);			
		$("#titleDescription").html(SLCONS.SL_DESC);
		$("#mappedResults").attr("src", '/anf/images/spacer.gif');
		$("#routeResults").attr("src", '/anf/images/spacer.gif');
		$("#ppNotes").hide();
		$("#genLeftCol").show();
		$("#genRightCol").show();
		$("#resultsTable").html(' ');
		$("#routeTable").html(' ');	
		$("#results").hide();
		$("#route").hide();
		$("#loading").hide();
	}
}