// support old variable names for compatibility (will be removed soon)

if (window.netoPartner     != null) neto_partner      = netoPartner;
if (window.netoResultShown != null) neto_search_limit = netoResultShown;
if (window.netoSearchTerms != null) neto_search_terms = netoSearchTerms;
if (window.netoSearchParam != null) neto_search_param = netoSearchParam;

// support alternative variable names

if (window.neto_search_terms != null) neto_search_phrase = neto_search_terms;
if (window.neto_search_limit != null) neto_num_listings = -neto_search_limit;
if (window.neto_search_exact != null) neto_num_listings = +neto_search_exact;

// set defaults

if (!window.neto_partner) neto_partner = "";
if (!neto_is_numeric(window.neto_frame_width))    neto_frame_width = 176;
if (!neto_is_numeric(window.neto_frame_height))   neto_frame_height = 0;
if (!neto_is_numeric(window.neto_frame_border))   neto_frame_border = 0;
if (!neto_is_numeric(window.neto_num_listings))   neto_num_listings = 0;
if (!neto_is_numeric(window.neto_title_length))   neto_title_length = 0;
if (!neto_is_numeric(window.neto_descr_length))   neto_descr_length = 0;
if (!neto_is_numeric(window.neto_descr_nolink))   neto_descr_nolink = 0;
if (!neto_is_numeric(window.neto_edge_padding))   neto_edge_padding = 0;
if (!neto_is_numeric(window.neto_edge_vpadding))  neto_edge_vpadding = 0;
if (!neto_is_numeric(window.neto_fontsize_title)) neto_fontsize_title = 0;
if (!neto_is_numeric(window.neto_fontsize_descr)) neto_fontsize_descr = 0;
if (!neto_is_numeric(window.neto_fontsize_url))   neto_fontsize_url = 0;
if (!neto_is_numeric(window.neto_prepare_listings)) neto_prepare_listings = 0;

if (window.neto_frame_layout  == null)            neto_frame_layout  = '';
if (window.neto_search_phrase == null)            neto_search_phrase = '';
if (window.neto_search_param  == null)            neto_search_param  = '';

if (window.neto_link_target == null)              neto_link_target = '';
if (window.neto_link_decor  == null)              neto_link_decor  = '';
if (window.neto_hide_url    == null)              neto_hide_url    = '';
if (window.neto_hide_descr  == null)              neto_hide_descr  = '';
if (window.neto_color_bg0   == null)              neto_color_bg0   = '';
if (window.neto_color_bg    == null)              neto_color_bg    = '';
if (window.neto_color_title == null)              neto_color_title = '';
if (window.neto_color_descr == null)              neto_color_descr = '';
if (window.neto_color_url   == null)              neto_color_url   = '';
if (window.neto_color_edge  == null)              neto_color_edge  = '';
if (window.neto_color_delim == null)              neto_color_delim = '';
if (window.neto_top_image   == null)              neto_top_image   = '';
if (window.neto_bot_image   == null)              neto_bot_image   = '';
if (window.neto_extra_css   == null)              neto_extra_css   = '';

var use_prepared_listings = typeof window.neto_prepared_ids == "object";
var has_prepared_listings = use_prepared_listings && neto_prepared_ids[0];

var skip_listings = 0;

if (!use_prepared_listings && neto_partner == "dapaz") {
	if (0 && Math.random() * 4 >= 1) skip_listings = 1;
}

var num_listings = Math.abs(neto_num_listings) || 3;

if (use_prepared_listings && num_listings > neto_prepared_ids.length)
	num_listings = neto_prepared_ids.length;

var has_neto_frame_height_set = neto_frame_height != 0;

if (neto_frame_height == 0 && window.neto_listing_height)
	neto_frame_height = neto_listing_height * num_listings
		+ (window.neto_decoration_height || 24);

var factor = neto_hide_url == '' ? 1 : 0.9;
if (neto_frame_height == 0 && neto_frame_layout.substring(0, 1) == "h")
	neto_frame_height = num_listings * 39000 / neto_frame_width * factor;

if (neto_frame_height == 0)
	neto_frame_height = num_listings * 22500 / neto_frame_width + 108;

if (neto_partner == "dapaz" && !window.neto_test)
	neto_frame_height = 0;

// build query url

var neto_frame_name = "neto_frame";
var query_url_prefix = "http://query.neto.co.il";
if (window.neto_test) query_url_prefix = "http://www.neto.co.il/query-test";
var query_url = query_url_prefix + "/search.cgi";

query_url += "?n=" + neto_num_listings;

if (neto_search_phrase.length == 0 && neto_search_param.length != 0) {
	var query = location.search.split("?")[1];
	for (var i = 0; i < query.split("&").length; i++) {
		if (query.split("&")[i].split("=")[0] == neto_search_param) {
			neto_search_phrase = query.split("&")[i].split("=")[1];
		}
	}
}

if (typeof neto_search_phrase == "object")
	neto_search_phrase = neto_search_phrase.join("|");

query_url += "&phrase=" + neto_search_phrase;
query_url += "&partner=" + neto_partner;
query_url += "&frame_width=" + neto_frame_width;
query_url += "&frame_height=" + neto_frame_height;
query_url += "&pad=" + neto_edge_padding;
query_url += "&vpad=" + neto_edge_vpadding;
query_url += "&layout=" + neto_frame_layout;
query_url += "&target=" + neto_link_target;
query_url += "&ldecor=" + neto_link_decor;
query_url += "&title_len=" + neto_title_length;
query_url += "&descr_len=" + neto_descr_length;
query_url += "&descr_nol=" + neto_descr_nolink;
query_url += "&fs_title=" + neto_fontsize_title;
query_url += "&fs_descr=" + neto_fontsize_descr;
query_url += "&fs_url=" + neto_fontsize_url;
query_url += "&hide_url=" + neto_hide_url;
query_url += "&hide_descr=" + neto_hide_descr;
query_url += "&color_bg0=" + neto_color_bg0;
query_url += "&color_bg=" + neto_color_bg;
query_url += "&color_title=" + neto_color_title;
query_url += "&color_descr=" + neto_color_descr;
query_url += "&color_url=" + neto_color_url;
query_url += "&color_edge=" + neto_color_edge;
query_url += "&color_delim=" + neto_color_delim;
query_url += "&top_image=" + neto_top_image;
query_url += "&bot_image=" + neto_bot_image;
query_url += "&extra_css=" + neto_extra_css;

if (use_prepared_listings && neto_prepare_listings == 0) {
	var ids = neto_prepared_ids.splice(0, num_listings);
	query_url += "&use_ids=" + ids.join(",");
}

if (window.neto_fallback_phrase != null) {
	if (typeof neto_fallback_phrase == "object")
		neto_fallback_phrase = neto_fallback_phrase.join("|");
	query_url += "&fb_phrase=" + neto_fallback_phrase;
}

if (window.neto_geo_phrase != null) {
	if (typeof neto_geo_phrase == "object")
		neto_geo_phrase = neto_geo_phrase.join("|");
	query_url += "&geo_phrase=" + neto_geo_phrase;
}
if (window.neto_geo_area)
	query_url += "&geo_area=" + escape(window.neto_geo_area);
if (window.neto_geo_reference)
	query_url += "&geo_ref=" + escape(window.neto_geo_reference);

if (window.neto_search_order)
	query_url += "&sorder=1";

if (window.neto_content_url != null)
	query_url += "&url=" + escape(neto_content_url);

if (window.neto_click_url_prefix != null)
	query_url += "&click_url=" + escape(neto_click_url_prefix);

if (window.neto_use_bingo != null)
	query_url += "&bingo=" + escape(neto_use_bingo);

query_url = neto_quote(query_url);

// fetch and write content if needed

if (!skip_listings && (!window.neto_void_if_no_prepared || has_prepared_listings)) {
	var content = neto_prepare_listings != 0
		? '<script src="' + query_url
			+ '&prepare=' + neto_prepare_listings + '">'
			+ '</script>'
		: '<iframe name="' + neto_frame_name + '"'
			+ ' width="' + neto_frame_width + '"'
			+ ' height="' + neto_frame_height + '"'
			+ ' frameborder="' + neto_frame_border + '"'
			+ ' src="' + query_url + '"'
			+ ' marginwidth="0" marginheight="0"'
			+ ' vspace="0" hspace="0"'
			+ ' allowtransparency="true" scrolling="no">'
			+ '</iframe>';

	if (window.neto_container_id == null)
		neto_container_id = "neto_container";
	container = neto_container_id
		? document.getElementById(neto_container_id) : null;
	if (container)
		container.innerHTML += content;
	else
		document.write(content);
}

neto_prepare_listings = null;
neto_void_if_no_prepared = null;
if (!has_neto_frame_height_set) neto_frame_height = null;

function neto_quote(str) {
	if (str == null) return '';
	str = new String(str);
	str = str.replace(/'/g, '%27')
	str = str.replace(/"/g, '%22');
	str = str.replace(/#/g, '%23');
	return str;
}

function neto_is_numeric(num) {
	return num != null && !isNaN(parseInt(num));
}

