function store_locator() {
    if(!document.store_locator_form) {
        return false;
    }

    document.getElementById('yws_location').innerHTML = 'Searching...';

    location_search(document.store_locator_form.query.value, document.store_locator_form.zipcode.value);
    return false;
}

function callback_location(json) {
    var not_found = 'Sorry, we could not find any store matching your keywords and zip code.'
    if (!json.ResultSet || !json.ResultSet.Result) {
        document.getElementById('yws_location').innerHTML = not_found;
        return;
    }

    //get the values out of the object
    var res = json.ResultSet.Result;
    var len = res.length;
    var s = '';
    var phone = '';
    var map = '';

    for(var i = 0; i < len; i++) {
        phone = res[i].Phone ? res[i].Phone : '';
        mapTitle = res[i].MapUrl ? '<a href="'+ res[i].MapUrl +'" target="_blank">' + res[i].Title + '</a>' : res[i].Title;

        s+= '<p>&bull;' + mapTitle + ' - ' + res[i].Address + ', ' + res[i].City + '. ' + phone + '</p>';
    }

    if(s) {
        document.getElementById('yws_location').innerHTML = s;
    }else {
        document.getElementById('yws_location').innerHTML = not_found;
    }
}

function callback_images(json) {
    if (!json.ResultSet || !json.ResultSet.Result) {
        return;
    }

    //get the values out of the object
    var res = json.ResultSet.Result;
    var len = res.length;
    var s = '';

    for(var i = 0; i < len; i++) {
        s+= '<p><img src="' + res[i].Thumbnail.Url + '" border="0" width="' + res[i].Thumbnail.Width + '" height="' + res[i].Thumbnail.Height + '" /></p>';
    }

    //add it in the page DOM
    if(s) {
        document.getElementById('yws_images').innerHTML = '<div class="small_print"><b>Images from Yahoo!</b></div>' + s;
    }
}

function images_search(words, site, start) {
    var site_param = site ? '&site=' + urlencode(site) : '';
    var request = 'http://api.search.yahoo.com/ImageSearchService/V1/imageSearch?appid=tictap_yws&start=' + start + '&results=10&output=json&callback=callback_images&query=' + urlencode(words) + site_param;

    aObj = new JSONscriptRequest(request);
    aObj.buildScriptTag();
    aObj.addScriptTag();
}

function location_search(q, z) {
    var request = 'http://api.local.yahoo.com/LocalSearchService/V3/localSearch?appid=tictap_location&query=' + urlencode(q) + '&zip=' + urlencode(z) + '&results=5&output=json&callback=callback_location';

    aObj = new JSONscriptRequest(request);
    aObj.buildScriptTag();
    aObj.addScriptTag();
}

function urlencode(k){
    if(encodeURIComponent) {
        try {
            return encodeURIComponent(k);
        }catch (e) {
            return '';
        }
    }else if(escape) {
        return escape(k);
    }

    return '';
}

function urlunencode(k){
    if(decodeURIComponent) {
        try {
            return decodeURIComponent(k);
        }catch (e) {
            return '';
        }
    }else if(unescape) {
        return unescape(k);
    }

    return '';
}

