var JQ = jQuery.noConflict();

var storeCode = "HOL";
var intl = "USA";
var inSearch=0;
var noop = {};
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function switchState(o){
	intl = o.val();
	JQ(".stateList").hide();
	JQ("#"+intl).css("display","inline");
}
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(JQ("#postalCode").val()==""){
		JQ("#errorMessage").html(SLCONS.SL_ERROR02);
		return false;
	}
	
	JQ("#errorMessage").html('');
	JQ("#loading").show();

	//JQ("#genLeftCol").hide();
	JQ("#genRightCol").hide();
	JQ("#results").hide();
	JQ("#resultsTable").html('');
	JQ("#route").hide();
	JQ("#routeTable").html('');

	var getUrl= "/storelocator/searchAPI.do?addressLine=&primaryCity=&method=search&isStateSearch=N&storeCode="+storeCode+"&postalCode=";
	getUrl += JQ("#postalCode").val();
	getUrl += "&storeProximity=";
	getUrl += JQ("#storeProximity").val();

	jQuery.ajax({
	    type: "POST",
		url: getUrl,
		data: noop,
		dataType: "xml",
		success: function(xml){
			JQ("#loading").hide();
			if(JQ(xml).find('error').text()=='Y'){
				//Error has occured
				JQ("#pageTitle").html(SLCONS.STORELOCATOR);
				JQ("#titleDescription").html(SLCONS.SL_DESC);
				JQ("#mappedResults").attr("src", '/hol/images/I-spacer.gif');
				//JQ("#ppNotes").hide();
				JQ("#genLeftCol").show();
				JQ("#genRightCol").show();
				JQ("#results").hide();
				JQ("#printLink").hide();
				
				var err = trim(JQ(xml).find('errorMsg').text());

				if(err == 'ERROR1'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR01);
				}else if(err == 'ERROR2'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR02);
				}else if(err == 'ERROR3'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR03);
				}else if(err == 'ERROR4'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR04);
				}else if(err == 'ERROR5'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR05);
				}
			}else{
				//GetMap
				JQ("#mappedResults").attr("src", JQ(xml).find('mapURL').text());

				//Set Title & description
				JQ("#pageTitle").html( JQ(xml).find('title').text() );
				JQ("#titleDescription").html( JQ(xml).find('description').text() );
						
				var tableHTML = "<table class='slResultsTable'><tr class='altRow'>";
	 				tableHTML +="<td width='200' class='storeHeader tHead txtLft'>"+SLCONS.SL_LOCATION+"</td>";
	 				tableHTML +="<td width='144' class='storeHeader tHead'>"+SLCONS.SL_DIRECTIONS+"</td>";
	 				tableHTML +="<td width='220' class='storeHeader tHead'>"+SLCONS.SL_CURRENTHOURS+"</td></tr>";
			
				var tmpCt=0;
				JQ(xml).find('store').each(function(){
					tmpCt++;
					if(tmpCt%2==0){
						tableHTML += "<tr class='altRow'>";
					}else{
						tableHTML += "<tr>";
					}

					tableHTML += "<td class='frstCol'>";
					var id = JQ(this).attr('id');
					var toAddress1 = JQ(this).find('toAddress1').text();
					var toAddressLine = JQ(this).find('toAddressLine').text();
					var primaryCity = JQ(this).find('primaryCity').text();
					var state = JQ(this).find('state').text();
					var postalCode = JQ(this).find('postalCode').text();
					var phoneNumber = JQ(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(JQ(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'>";
						JQ(this).find('hours').each(function(){
							tableHTML += "<span class='tbday'>"+SLCONS.SL_MONDAY+": </span><span class='tbtime'>"+JQ(this).find('monday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_TUESDAY+": </span><span class='tbtime'>"+JQ(this).find('tuesday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_WEDNESDAY+": </span><span class='tbtime'>"+JQ(this).find('wednesday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_THURSDAY+": </span><span class='tbtime'>"+JQ(this).find('thursday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_FRIDAY+": </span><span class='tbtime'>"+JQ(this).find('friday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_SATURDAY+": </span><span class='tbtime'>"+JQ(this).find('saturday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_SUNDAY+": </span><span class='tbtime'>"+JQ(this).find('sunday').text()+"</span><br/>";
						});
					}
					tableHTML += "</td></tr>";
				
					//Build Store Object
					var toLatitude = JQ(this).find('toLatitude').text();
					var toLongitude = JQ(this).find('toLongitude').text();
					var fromLatitude = JQ(this).find('fromLatitude').text();
					var fromLongitude = JQ(this).find('fromLongitude').text();
					var storeCode = JQ(this).find('storeCode').text();
					var method = JQ(this).find('method').text();
					var isStateSearch = JQ(this).find('isStateSearch').text();
					var isStartGeocoded = JQ(this).find('isStartGeocoded').text();
					var isDrillDown = JQ(this).find('isDrillDown').text();
					var toAddressLine = JQ(this).find('toAddressLine').text();
					var toPrimaryCity = JQ(this).find('toPrimaryCity').text();
					var postalCode = JQ(this).find('postalCode').text();
					var toPostalCode = JQ(this).find('toPostalCode').text();
					var toAddress1 = JQ(this).find('toAddress1').text();
					var whatState = JQ(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>";
				JQ("#resultsTable").append(tableHTML);
				JQ("#results").show();
				//JQ("#ppNotes").show();
				JQ("#printLink").show();
			}
			inSearch=0;
		}
	});
}
function searchState(){
	if(!JQ("#stateSearch .stateList:visible").find("select option:selected").val()) {
		JQ("#errorMessage").html(SLCONS.SL_ERROR04);
		return false;
	}
	
	JQ("#errorMessage").html('');
	JQ("#loading").show();
	inSearch=1;
	//JQ("#genLeftCol").hide();
	JQ("#genRightCol").hide();
	JQ("#results").hide();
	JQ("#resultsTable").html('');
	JQ("#route").hide();
	JQ("#routeTable").html('');

	var stateAbbrev = JQ("#State"+intl).val();
	var stateFull = JQ("#State"+intl+" option:selected").text();
	var getUrl= "/storelocator/searchAPI.do?state="+stateAbbrev+"&storeCode="+storeCode+"&method=searchState&isStateSearch=Y&whatState="+stateFull;


	jQuery.ajax({
	    type: "POST",
		url: getUrl,
		data: noop,
		dataType: "xml",
		success: function(xml){
			JQ("#loading").hide();
			if(JQ(xml).find('error').text()=='Y'){
				//Error has occured
				JQ("#pageTitle").html(SLCONS.STORELOCATOR);
				JQ("#titleDescription").html(SLCONS.SL_DESC);
				JQ("#mappedResults").attr("src", '/hol/images/I-spacer.gif');
				//JQ("#ppNotes").hide();
				JQ("#genLeftCol").show();
				JQ("#genRightCol").show();
				JQ("#results").hide();
				JQ("#printLink").hide();
				
				var err = trim(JQ(xml).find('errorMsg').text());				
				if(err == 'ERROR1'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR01);
				}else if(err == 'ERROR2'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR02);				
				}else if(err == 'ERROR3'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR03);
				}else if(err == 'ERROR4'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR04);
				}else if(err == 'ERROR5'){
					JQ("#errorMessage").html(SLCONS.SL_ERROR05);
				}
			}else{
				//GetMap
				JQ("#mappedResults").attr("src", JQ(xml).find('mapURL').text());

				//Set Title & description
				JQ("#pageTitle").html( JQ(xml).find('title').text() );
				JQ("#titleDescription").html( JQ(xml).find('description').text() );

				var tableHTML = "<table cellspacing='0' cellpadding='0' class='slResultsTable'><tr class='altRow'>";
 					tableHTML +="<td width='200' class='storeHeader tHead txtLft lCell'>"+SLCONS.SL_LOCATION+"</td>";
 					tableHTML +="<td width='144' class='storeHeader tHead lCell'>"+SLCONS.SL_DIRECTIONS+"</td>";
	 				tableHTML +="<td width='220' class='storeHeader tHead'>"+SLCONS.SL_CURRENTHOURS+"</td></tr>";

				var tmpCt=0;
				JQ(xml).find('store').each(function(){
					tmpCt++;
					if(tmpCt%2==0){
						tableHTML += "<tr class='altRow'>";
					}else{
						tableHTML += "<tr>";
					}

					tableHTML += "<td class='frstCol lCell'>";
					var id = JQ(this).attr('id');
					var toAddress1 = JQ(this).find('toAddress1').text();
					var toAddressLine = JQ(this).find('toAddressLine').text();
					var primaryCity = JQ(this).find('toPrimaryCity').text();
					var state = JQ(this).find('state').text();
					var postalCode = JQ(this).find('postalCode').text();
					var phoneNumber = JQ(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(JQ(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'>";
						JQ(this).find('hours').each(function(){
							tableHTML += "<span class='tbday'>"+SLCONS.SL_MONDAY+": </span><span class='tbtime'>"+JQ(this).find('monday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_TUESDAY+": </span><span class='tbtime'>"+JQ(this).find('tuesday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_WEDNESDAY+": </span><span class='tbtime'>"+JQ(this).find('wednesday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_THURSDAY+": </span><span class='tbtime'>"+JQ(this).find('thursday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_FRIDAY+": </span><span class='tbtime'>"+JQ(this).find('friday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_SATURDAY+": </span><span class='tbtime'>"+JQ(this).find('saturday').text()+"</span><br/>";
							tableHTML += "<span class='tbday'>"+SLCONS.SL_SUNDAY+": </span><span class='tbtime'>"+JQ(this).find('sunday').text()+"</span><br/>";
						});
					}
					tableHTML += "</td></tr>";

					//Build Store Object
					var toLatitude = JQ(this).find('toLatitude').text();
					var toLongitude = JQ(this).find('toLongitude').text();
					var destAddress = JQ(this).find('destAddress').text();
					var destCity = JQ(this).find('destCity').text();
					var destState = JQ(this).find('destState').text();
					var destZip = JQ(this).find('destZip').text();
					var destPhone = JQ(this).find('destPhone').text();
					var errorRowNumber = JQ(this).find('errorRowNumber').text();
					var isStartGeocoded = JQ(this).find('isStartGeocoded').text();				
					var method = 'routePath';
					var isStateSearch = JQ(this).find('isStateSearch').text();
					var state = JQ(this).find('state').text();
					var whatState = JQ(this).find('whatState').text();
					var isDrillDown = JQ(this).find('isDrillDown').text();											
					var storeCode = JQ(this).find('storeCode').text();
					var toAddressLine = JQ(this).find('toAddressLine').text();
					var toAddress1 = JQ(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>";
				JQ("#resultsTable").append(tableHTML);
				JQ("#results").show();
				//JQ("#ppNotes").show();
				JQ("#printLink").show();
			}
			inSearch=0;
		}
	});
}
function getZip(num){
	JQ("#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>';
	JQ("#zipCell"+num).html(pHTML);
}
function routeSearch(storeObj,n){
	JQ("#loading").show();
	inSearch=1;
	if(n!=null){
		storeObj.postalCode=JQ("#zipCode"+n).val();
	}
	JQ("#results").hide();
	JQ("#route").hide();
	//JQ("#ppNotes").hide();
	//JQ("#genLeftCol").hide();
	JQ("#genRightCol").hide();
	JQ("#routeResults").attr("src", '/hol/images/I-spacer.gif');	

	var getUrl= "/storelocator/routeAPI.do?";
	jQuery.ajax({
	    type: "POST",
		url: getUrl,
		data: storeObj,
		dataType: "xml",
		success: function(xml){
			JQ("#loading").hide();
			if(JQ(xml).find('error').text()=='Y'){
				//Error has occured
				var tmpErrorRow = JQ(xml).find('errorRow').text();

				if(JQ(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>";
				}
				JQ("#zipCell"+tmpErrorRow).append(errorHTML);
				JQ("#results").show();
				if(tmpErrorRow>2){
					tmpErrorRow--;
					var errorR = "#row"+tmpErrorRow;
					window.location.href=errorR;				
				}
			}else{
				//GetMap
				JQ("#routeResults").attr("src", JQ(xml).find('mapURL').text());

				//Set Title & description
				JQ("#pageTitle").html( JQ(xml).find('title').text() );			
				JQ("#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 += JQ(xml).find('fromPostal').text();
				tableHTML += "</td><td>";
				tableHTML += "<span class='storeHeader'>TO</span><br/>";
				tableHTML += JQ(xml).find('toAddress1').text()+"<br/>";
				tableHTML += JQ(xml).find('toAddressLine').text()+"<br/>";
				tableHTML += JQ(xml).find('toPrimaryCity').text()+", "+JQ(xml).find('toState').text()+" "+JQ(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;
				JQ(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+" "+JQ(this).find('direction').text();
					tableHTML += "</td><td class='resultsRows'>";
					tableHTML += JQ(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+" "+JQ(xml).find('totalDistance').text()+" "+SLCONS.SL_MILES+"<br/>";
				tableHTML += SLCONS.SL_TOTALTIME+" "+JQ(xml).find('totalTime').text()+"<br/>";
				tableHTML += "</tr></table>";
				JQ("#routeTable").append(tableHTML);
				JQ("#route").show();
				//JQ("#ppNotes").show();
				JQ("#printLink").show();
			}
			inSearch=0;
		}
	});
}
function resetSearch(){
	if(inSearch==0){
		JQ("#errorMessage").html(' ');
		JQ("#pageTitle").html(SLCONS.STORELOCATOR);			
		JQ("#titleDescription").html(SLCONS.SL_DESC);
		JQ("#mappedResults").attr("src", '/hol/images/I-spacer.gif');
		JQ("#routeResults").attr("src", '/hol/images/I-spacer.gif');
		//JQ("#ppNotes").hide();
		JQ("#genLeftCol").show();
		JQ("#genRightCol").show();
		JQ("#resultsTable").html(' ');
		JQ("#routeTable").html(' ');	
		JQ("#results").hide();
		JQ("#route").hide();
		JQ("#loading").hide();
		JQ("#printLink").hide();
	}
}

