/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var SERVER_PATH = "Templates/store-locator/storeSearch.php";
var IMAGE_PATH = "Templates/shared/images/";

function search(){
    // Validate form fields
    zipcode = $.trim($('#zipSearch').val());

    if(zipcode != '' && zipcode != 'Search by Zip Code'){
        getLocByZipcode(zipcode);
    }
    else{
        alert('Please fill out a zip code first');
        return;
    }
}

function evalJSON(json){
    return eval("("+json+")");
}

function getLocByZipcode(zipcode){
    $.ajax({
        url: SERVER_PATH+"?searchDistance=1&viewAd=1&zipcode="+zipcode,
        type: "GET",
        cache: false,
        success: function(json){
            data = evalJSON(json);
            locations = data['locations'];
            showStepOne(locations);
        }
    });
}

function hasSpecialItems(arrayItems){
    for(i in arrayItems){
        arrayItem = arrayItems[i];
        if(arrayItem == 1){
            return true;
        }
    }
    return false;
}

function resolveDestination(brand){
    var siteDestination = "http://";
    if(false){
        siteDestination += "www.";
        switch(brand){
            case "Bealls":
                siteDestination += "beallstx";
                break;
            case "Goodys":
                siteDestination += "goodysonline";
                break;
            case "Palais Royal":
                siteDestination += "palaisroyal";
                break;
            case "Peebles":
                siteDestination += "peebles";
                break;
            case "Stage":
                siteDestination += "stagestores";
                break;
        }
        siteDestination += ".com/";
    }
    else{
        siteDestination += "192.168.9.";
        switch(brand){
            case "Bealls":
                siteDestination += "15";
                break;
            case "Goodys":
                siteDestination += "16";
                break;
            case "Palais Royal":
                siteDestination += "17";
                break;
            case "Peebles":
                siteDestination += "10";
                break;
            case "Stage":
                siteDestination += "18";
                break;
        }
        siteDestination += "/";
    }
    return siteDestination;
}

function showStepOne(locations){
    html = '';
    html += "<div>";
    counter = 0;
    for(var i in locations){
           
           locationData = locations[i];
           /*if(locationData['distance'] > 100){
               continue;
           }*/
           counter++;
           if(i % 3 == 0 && i != 0){
               html += "<div class='dashedLine'></div></div><div>";
           }
           addressData = locationData['addressInfo'];
           storeInfo = locationData['storeInfo'];
           
           adLink = "view-ad?adSelection=http://" + document.domain + "/view-ecirc.php?store=" + locationData['storeID'] + "&pub="+ encodeURI(locationData['company']) + locationData['division'];
           
           adLink = adLink.replace(/&/g,'%26');
           adLink = adLink.replace(/ /g,'%20');
           //alert(adLink);
           html += "<div class='item'>";
           letter = String.fromCharCode("A".charCodeAt(0) + (locationData['resultNumber']%26));
           if(locationData['resultNumber'] > 25){
               letter = String.fromCharCode("A".charCodeAt(0) + (locationData['resultNumber']/25) - 1) + letter;
           }
           displayLetter = letter+") ";
           displayName = locationData['company']+" "+locationData['name'];
           html += "<p class='name'>"+displayLetter+"<a href='"+adLink+"'>"+displayName+"</a></p>";
           

           
           htmlp1 = html;
           html = '';

           html += "<p class='info'>";
           
           html += addressData['address1'] + "<br />";
           if(addressData['address2'] != ''){
               html += addressData['address2'] + "<br />";
           }
           html += addressData['city'] + ", " + addressData['state'] + " " + addressData['zip']+"<br />";
           if(storeInfo['phone'] != ''){
                html += storeInfo['phone']+"<br />";
           }
           if(storeInfo['hours'] != ''){
                html += storeInfo['hours'];
           }
           html += "<span class='adLink'><a href='"+adLink+"'>View Current Ad</a></span>";
           html += "</p>";
           
           if(hasSpecialItems(storeInfo['atLocation'])){
                atLocation = storeInfo['atLocation'];
                html += "<div class='atLocation'>";
                html += "<span>At this location:</span>";
                if(atLocation['clinique'] == 1){
                    html += "<img src='"+IMAGE_PATH+"consumer/location/clinique.jpg' alt='Clinique'/>";
                }
                if(atLocation['estee'] == 1){
                    html += "<img src='"+IMAGE_PATH+"consumer/location/estee-lauder.jpg' alt='Est&eacute;e Lauder'/>";
                }
                if(atLocation['formalwear'] == 1){
                    html += "<img src='"+IMAGE_PATH+"consumer/location/formalwear-rental.jpg' alt='Formalwear Rental'/>";
                }
                html += "</div>";
           }

           html = htmlp1 + html;
           

           html += "</div>";
           
    }
    html += "<br class='clear' /></div>";
   if(counter == 0){
       html += "<p>Your search yielded no results within 100 miles.</p>";
   }
    $('#multipleListing').html(html);
    $('#searchStepOne').css('display','block');
}

function setTextZip(element,hasFocus){
	currentValue = $(element).val();
	if(hasFocus == 1){
		if(currentValue == 'Search by Zip Code'){
			$(element).val('');
		}
	}
	else{
		if(jQuery.trim(currentValue) == ''){
			$(element).val('Search by Zip Code');
		}
	}
}
