/*
    SHOP.COM Javascript utilities
    by Jordan Zimmerman
    (c)2006 by SHOP.COM
    All rights reserved worldwide
*/
var     sc_data_map = new Array();

var		sc_ajax_data_tab = new Array();
var		sc_ajax_data_callback_tab = new Array();

var     SC_DISPLAY_MODE_STANDARD = 0;
var     SC_DISPLAY_MODE_FADE = 1;
var     SC_DISPLAY_MODE_SLIDE_DOWN = 2;
var     SC_DISPLAY_MODE_SLIDE_UP = 3;
var     SC_DISPLAY_MODE_SLIDE_LEFT = 4;
var     SC_DISPLAY_MODE_SLIDE_RIGHT = 5;
var     SC_DISPLAY_MODE_ZOOM = 6;

var     SC_SLIDE_INCREMENT = 5;
var     SC_SLIDE_INTERVAL = 10;

var	    SC_FADE_IN_RATES = Array(1, 1, 1, 1, 1, 5, 5, 5, 10, 25);
var	    SC_FADE_OUT_RATES = Array(1, 1, 1, 1, 1, 1, 1, 1, 5);
var     SC_FADE_INTERVAL = 50;

var     SC_ZOOM_STEP = 5;
var     SC_ZOOM_MIN = 25;
var     SC_ZOOM_STEP_QTY = 25;

var     SC_POPUP_CLOSE_TIME = 2500;

var     SC_TRANSPORT_STOP = 0;
var     SC_TRANSPORT_NEXT = 1;
var     SC_TRANSPORT_PREVIOUS = 2;
var     SC_TRANSPORT_PLAY = 3;

var		SC_POPUP_WINDOW_LEFT_OFFSET = 150;
var		SC_POPUP_WINDOW_TOP_OFFSET = 150;

var		SC_IMMEDIATE_SCRIPT_HEADER_PREFIX = "AMOS_IMMEDIATE_SCRIPT_";
var		SC_REDIRECT_HEADER = "AMOS_AJAX_REDIRECT";
var		SC_HREF_HEADER = "AMOS_AJAX_HREF";
var     SC_VEIL_ID = 'veil';

var     HTTP_STATUS_NO_CONTENT = 204;
var     HTTP_STATUS_NO_CONTENT_IE = 1223;

function sc_on_load(script)
{
    var local_on_load = function() {
        eval(script);
    }

    sc_add_listener(window,"load",local_on_load);
}

function sc_change_via_form_request(id, url, form_name, prefix, caller)
{
    var     item = document.getElementById(id);
    var     data = sc_get_data(id);
    var form = document.forms[form_name];
    var values = sc_get_form_data(form,prefix);
    if( caller.id )
        eval("values['" + caller.id + "'] = true");

    sc_disable_return_key_override();
    sc_force_change_via_request(id,url,sc_url_encode_object(values));
}

function sc_get_form_data(form,prefix)
{
    var map = new Object();

    for (i=0; i < form.elements.length; ++i )
    {
        if( form.elements[i].name.indexOf(prefix) == 0 )
        {
            var value;

            if( form.elements[i].type == 'checkbox' && !form.elements[i].checked )
                continue;
            else if( form.elements[i].type == 'radio' && !form.elements[i].checked )
                continue;
            else if( form.elements[i].type == 'select-multiple' )
            {
                value = '';
                var element = form.elements[i];
                for (j=0; j < element.childNodes.length; ++j )
                {
                    var this_var = element.childNodes[j];
                    if( this_var && this_var.selected )
                        value +=','+element.childNodes[j].value;
                }
            }
            else
                value = form.elements[i].value;

            map[form.elements[i].name] = value;
        }
    }

    return map;
}

function sc_url_encode(plaintext)
{
	// I found this at http://www.albionresearch.com/misc/urlencode.php -- JZ

	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function sc_url_encode_object(obj)
{
    var str = new String();

    for (var i in obj)
    {
        str += escape(i) + "=" + escape(obj[i]) + "&";
    }

    return str + ""; // for IE append the empty string
}

function sc_add_listener(obj, evType, fn)
{
    if (obj.addEventListener)
    {
        obj.addEventListener(evType, fn, false);
        return true;
    }
    else if (obj.attachEvent)
        return obj.attachEvent('on' + evType, fn);
    else
        return false;
}

function sc_hide_on_click(id)
{
    function _process_clicks()
    {
        var     item = document.getElementById(id);
        if ( !sc_is_invisible(item) )
        {
            sc_hide_show(id, false);
            document.onclick = null;
        }
        return true;
    }

    document.onclick = _process_clicks;
}

var SC_VEIL_PAD = 10;
function sc_show_hide_veil(show_it)
{
   var veilOverlay = document.getElementById(SC_VEIL_ID);
   if( show_it )
   {
       if( veilOverlay ) //Already up
        return;
       veilOverlay = document.createElement('div');
       veilOverlay.id = SC_VEIL_ID;
       veilOverlay.style.zIndex = 1000;
       veilOverlay.innerHTML = '&nbsp;';
       var setVeilWidth = function() {
            veilOverlay.style.width = (document.body.scrollWidth+SC_VEIL_PAD) + "px";
            veilOverlay.style.height = (document.body.scrollHeight+SC_VEIL_PAD) + "px";
       }
       setVeilWidth();
       sc_add_listener(window, "resize", setVeilWidth);
       document.body.appendChild(veilOverlay);
       veilOverlay.style.display = "block";
       sc_z_compare_and_enable_all(veilOverlay,false);
   }
   else
   {
       if( !veilOverlay ) //Already down
        return;

       sc_z_compare_and_enable_all(veilOverlay,true);
       veilOverlay.parentNode.removeChild(veilOverlay);
   }
}

var SC_SELECT_BOX_STATE = new Object();

function sc_z_compare_and_enable_all(item, enable)
{
    if( !sc_is_ie_6_or_earlier() )
     return;

   var selects = document.getElementsByTagName("select");
   for (var i in selects)
   {
       var select = selects[i];

       if( sc_z_compare(item, select) )
       {
           if( SC_SELECT_BOX_STATE[select.name] )
           {
               select.disabled = SC_SELECT_BOX_STATE[select.name];
               SC_SELECT_BOX_STATE[select.name] = null;
           }
           else
           {
               SC_SELECT_BOX_STATE[select.name] = select.disabled;
               select.disabled = !enable;
           }
       }
   }

    var inputs = document.getElementsByTagName("input");
    for (var i in inputs)
    {
        var input = inputs[i];

        if( sc_z_compare(item, input) )
        {
            input.disabled = !enable;
        }
    }

    var as = document.getElementsByTagName("a");
    for (var i in as)
    {
        var a = as[i];

        a.disabled = !enable;
    }
}

function sc_hide_selects(item)
{
    if( !sc_is_ie_6_or_earlier() )
     return;

   var selects = document.getElementsByTagName("select");
   for ( var j = 0; j < selects.length; ++j )
   {
       var select = selects[j];

       if( sc_is_overlapping(item, select) && !sc_is_parent(item, select) )
       {
           select.style.visibility = "hidden";
       }
   }
}

function sc_show_selects(item)
{
    if( !sc_is_ie_6_or_earlier() )
        return;

    // If the veil is up never ignore
    var veilOverlay = document.getElementById(SC_VEIL_ID);
    if( veilOverlay != null )
        return;

   var selects = document.getElementsByTagName("select");
   for ( var i = 0; i < selects.length; ++i )
   {
       var select = selects[i];
       if( sc_is_overlapping(item, select) && !sc_is_parent(item, select) )
       {
           select.style.visibility = "visible";
       }
   }
}

function sc_child_has_style(item,style)
{
    if( item && item.nodeType && item.nodeType == 1 )
    {
        var value;

        if( item.style )
        {
            value = eval("item.style." + style);

            if( value )
                return value;
        }

        for(var c in item.childNodes )
        {
            if( (value = sc_child_has_style(item.childNodes[c],style)) )
            {
                return value;
            }
        }
    }

    return null;
}

function sc_z_compare(item1, item2)
{
    var item1_z_index = sc_has_style(item1,"zIndex");
    var item2_z_index = sc_has_style(item2,"zIndex");

    if( !item1_z_index && item2_z_index )
        return false;

    if( item1_z_index && !item2_z_index )
        return true;

    if( !item1_z_index && !item2_z_index )
        return false;

    if( item1_z_index && item2_z_index )
        return (item1_z_index*1) > (item2_z_index*1);
}

var SC_MAX_PARENT_DEPTH = 25;

function sc_is_parent(parent, child)
{
    // Clip this depth test
    var depth = 0;

    var is_parent = false;
    if (child.parentNode)
    {
        while (child.parentNode)
        {
            if( depth > SC_MAX_PARENT_DEPTH )
                return false;

            if( child.parentNode == parent )
                return true;

            child = child.parentNode;
            depth += 1;
        }
    }

    return false;
}

function sc_has_style(item, style)
{
    // Clip this depth test
    var depth = 0;

    var is_parent = false;
    if (item.offsetParent)
    {
        while (item.offsetParent)
        {
            if( depth > SC_MAX_PARENT_DEPTH )
                return null;

            if( item && item.nodeType && item.nodeType == 1 )
            {
                value = eval("item.style." + style);

                if( value )
                    return value;

                item = item.offsetParent;
                depth += 1;
            }
        }
    }

    return null;
}

function sc_is_overlapping(obj1, obj2)
{
   var pos1 = sc_get_pos(obj1);
   var pos2 = sc_get_pos(obj2);

   // get the radi of each rect
   var width1  = pos1.width/2;
   var height1 = pos1.height/2;
   var width2  = pos2.width/2;
   var height2 = pos2.height/2;

   // compute center of each rect
   var cx1 = pos1.left + width1;
   var cy1 = pos1.top + height1;
   var cx2 = pos2.left + width2;
   var cy2 = pos2.top + height2;

   // compute deltas
   var dx = Math.abs(cx2 - cx1);
   var dy = Math.abs(cy2 - cy1);

   // test if rects overlap
   if (dx < (width1+width2) && dy < (height1+height2))
       return true;
   else
   // else no collision
       return false;

}

function sc_get_pos(obj)
{
   var pos = new Object();
   var x = sc_get_posx(obj,false);
   var y = sc_get_posy(obj,false);
   pos.top = y;
   pos.left = x;
   pos.width = obj.offsetWidth;
   pos.height = obj.offsetHeight;
   return pos;
}

function sc_get_posx(obj1)
{
	var curleft = 0;

    if (obj1.offsetParent)
	{
		while (obj1.offsetParent)
		{
			curleft += obj1.offsetLeft
            obj1 = obj1.offsetParent;
		}
	}
	else if (obj1.x)
		curleft += obj1.x;

	return curleft;
}

function sc_get_posy(obj1)
{
    var curtop = 0;

    if( !sc_is_ie() )
        curtop -= obj1.offsetHeight;

    if (obj1.offsetParent)
	{
		while (obj1.offsetParent)
		{
			curtop += obj1.offsetTop
            obj1 = obj1.offsetParent;
		}
	}
	else if (obj1.y)
		curtop += obj1.y;

	return curtop;
}

function sc_attach(objName1, objName2, offsetx, offsety, side)
{
    var obj1 = document.getElementById(objName1);
	var obj2 = document.getElementById(objName2);

	var x = sc_get_posx(obj1);
	var y = sc_get_posy(obj1);

    var display = obj2.style.display;
    if( display == 'none' )
    {
        sc_set_opacity(obj2, 0)
        obj2.style.display = '';
    }

    if( side == "TOP" )
    {
        obj2.style.left = (x + offsetx + "px");
        obj2.style.top = (y - obj2.clientHeight + offsety + "px");
    }
    else if( side == "RIGHT" )
    {
        if( sc_is_absolute(obj1) )
        {
            obj2.style.left = (obj1.offsetWidth + offsetx + "px");
            obj2.style.top = (offsety + "px");
        }
        else
        {
            obj2.style.left = (x + obj1.offsetWidth + offsetx + "px");
            obj2.style.top = (y + offsety + "px");
        }
    }
    else if( side == "BELOW" )
    {
        obj2.style.left = (x + offsetx + "px");
        obj2.style.top = (y + (obj1.offsetHeight + offsety) + "px");
    }
    else if( side == "LEFT" )
    {
        obj2.style.left = (x - obj2.clientWidth + offsetx + "px");
        obj2.style.top = (y + offsety + "px");
    }

    if( display == 'none' )
    {
        obj2.style.display = display;
        sc_remove_opacity(obj2);
    }

}

function sc_center_section(id)
{
    var     item = document.getElementById(id);

    var display = item.style.display;
    if( display == 'none' )
    {
        sc_set_opacity(item, 0)
        item.style.display = '';
    }

    var width = sc_window_width();
    item.style.left = (width / 2) - (item.clientWidth / 2) + "px";

    var height = sc_window_height();
    item.style.top = (height / 2) - (item.clientHeight / 2) + "px";

    if( display == 'none' )
    {
        item.style.display = display;
        sc_remove_opacity(item);
    }
}

function sc_delayed_reload(ticks)
{
    window.setTimeout(_reload, ticks);
    function _reload()
    {
	window.location.reload(true);
    }
}

function sc_get_data(id)
{
    var         data = sc_data_map[id];
    if ( !data )
    {
        var     item = document.getElementById(id);

        data = new Object();
        sc_data_map[id] = data;

        data.request_key = "";
        data.request_in_progress = false;
        data.display_method = 0;
        data.display_method_arguments = null;
        data.has_been_faded = false;
        data.has_been_slid = false;
        data.is_busy = false;
        data.is_in_popup = false;
        data.rotate_data = null;
        data.on_show_inset_id = null;
        data.on_show_inset_padding = null;
        data.toggle_id = null;
        data.wait_timeout = null;
		if (!item)
			data.style_display = "";
		else
		{
			data.style_display = item.style.display;
			if ( data.style_display == "none" )
			{
				data.style_display = "";
			}
		}
        data.opacity = 100;
        data.delete_on_fade_out = false;
        data.fade_in_rates = SC_FADE_IN_RATES;
        data.fade_out_rates = SC_FADE_OUT_RATES;
        data.fade_interval = SC_FADE_INTERVAL;
    }

    return data;
}

function sc_change_visibility(item, data, visible_flag)
{
    if ( visible_flag )
    {
        item.style.display = data.style_display;
    }
    else
    {
        item.style.display = "none";
    }
}

function sc_is_absolute(item)
{
    if( item != null )
    {
        if( item.style.position == 'absolute' )
            return true;
        else
            return sc_is_absolute(item.parentElement);
    }
    return false;
}

function sc_is_invisible(item)
{
	if ( item )
	{
		return (item.style.display == "none") || (item.style.visibility == "hidden");
	}
	else
		return true;
}

function sc_reset(item, data)
{
    if ( sc_is_invisible(item) )
    {
        if ( data.has_been_slid )
        {
            item.style.left = 0;
            item.style.top = 0;
        }
    }
    else
    {
        if ( data.has_been_faded )
        {
            sc_set_opacity(item, data.opacity);
        }
    }
}

function sc_get_top_left(item)
{
    var     top_left = new Object();
    top_left.top = 0;
    top_left.left = 0;
    while ( item )
    {
        top_left.top += item.offsetTop;
        top_left.left += item.offsetLeft;

        item = item.offsetParent;
    }

    return top_left;
}

function sc_make_width_height(width, height, wait)
{
    var     o = new Object();
    o.width = width;
    o.height = height;
    o.wait = wait;
    return o;
}

function sc_make_zoom_data(zoom_id, zoom_point_left, zoom_point_top)
{
    var     o = new Object();
    o.zoom_id = zoom_id;
    o.zoom_point_left = zoom_point_left;
    o.zoom_point_top = zoom_point_top;
    return o;
}

function sc_make_zoom_data_alt(zoom_id, from_id)
{
    var     from_item = document.getElementById(from_id);
    var     from_top_left = sc_get_top_left(from_item);
    return sc_make_zoom_data(zoom_id, from_top_left.left, from_top_left.top);
}

function sc_set_toggle_id(id, toggle_id)
{
    var     data = sc_get_data(id);
    data.toggle_id = toggle_id;
}

function sc_set_on_show_inset_id(id, inset_id)
{
    var     data = sc_get_data(id);
    data.on_show_inset_id = inset_id;

    var     inset_item = document.getElementById(inset_id);
    data.on_show_inset_padding = inset_item.style.padding;
}

function sc_set_display_method(id, method, arguments)
{
    var     data = sc_get_data(id);

    data.display_method = method;
    data.display_method_arguments = arguments;
}

function sc_unescape(str)
{
	if ( !str || !str.length )
	{
		return "";
	}

	while ( true )
	{
		var i = str.indexOf('+');
		if ( i < 0 )
		{
			break;
		}
		str = str.substring (0, i) + '%20' +
 		str.substring (i + 1, str.length);
	}
	return unescape(str);
}

function sc_parse_query_string(str)
{
	var	map = new Array();

	var	items = str.split("&");
	for ( var i = 0; i < items.length; ++i )
	{
		var	spec = items[i].split("=");
		map[sc_unescape(spec[0])] = sc_unescape(spec[1]);
	}

	return map;
}

function sc_parse_structured(str)
{
	var		map = new Array();

	do
	{
		var		index = str.indexOf("\r\n");
		if ( index < 0 )
		{
			break;
		}
		var		separator = "\r\n" + str.substring(0, index) + "\r\n";

		var		split_map = str.substring(index + 2).split(separator);
		for ( var i = 0; i < split_map.length; ++i )
		{
			var		work_str = split_map[i];
			index = work_str.indexOf("\r\n");
			if ( index < 0 )
			{
				break;
			}
			var		field_name = work_str.substring(0, index);
			var		value = work_str.substring(index + 2);

			map[field_name] = value;
		}
	} while ( false );

	return map;
}

function sc_process_callbacks(name)
{
    var		callback_tab = sc_ajax_data_callback_tab[name];
    sc_ajax_data_callback_tab[name] = null;

    if ( callback_tab )
    {
        for ( var i = 0; i < callback_tab.length; ++i )
        {
            var		callback = callback_tab[i];
            callback();
        }
    }
}

function sc_set_loaded_data(name, structured_data)
{
    sc_ajax_data_tab[name] = sc_parse_structured(structured_data);
    sc_process_callbacks(name);
}

function sc_get_redirect_url(xmlhttp)
{
	try
	{
		return xmlhttp.getResponseHeader(SC_REDIRECT_HEADER);
	}
	catch ( ignore )
	{
	}
	return null;
}

function sc_clear_ajax_data(name)
{
    sc_ajax_data_tab[name] = null;
}

function sc_load_via_request(name, url, post_data)
{
    var     is_post = post_data != undefined;

    sc_clear_ajax_data(name);

	function _handler(xmlhttp)
    {
        var			redirect_url = sc_get_redirect_url(xmlhttp);
        if ( redirect_url && (redirect_url.length > 0) )
        {
            window.location.href = redirect_url;
        }
        else
        {
            sc_set_loaded_data(name, xmlhttp.responseText);
        }
    }

	sc_make_xmlhttp(_handler, url, is_post, post_data);
}

function sc_make_xmlhttp(the_handler, url, is_post, post_data)
{
	var xmlhttp = window.XMLHttpRequest ? (new XMLHttpRequest()) : (new ActiveXObject("Msxml2.XMLHTTP.3.0"));

    function _handler()
    {
        if ( xmlhttp && (xmlhttp.readyState == 4) )
        {
            the_handler(xmlhttp);
        }
    }

    xmlhttp.onreadystatechange = _handler;
	xmlhttp.open(is_post ? "POST" : "GET", url, true);
	xmlhttp.setRequestHeader(SC_HREF_HEADER, window.location.href);
	if ( is_post )
	{
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	xmlhttp.send(is_post ? post_data : "");
}

function sc_get_data_loaded_via_request(name, object_name)
{
	if ( sc_ajax_data_tab[name] )
	{
        var		value_map = sc_ajax_data_tab[name];
        var		value = value_map[object_name];
		return value ? value : "";
	}
	return "";
}

function sc_execute_when_loaded_data_arrives(name, callback)
{
    if ( !sc_ajax_data_callback_tab[name] )
    {
        sc_ajax_data_callback_tab[name] = new Array();
    }

    var		callback_tab = sc_ajax_data_callback_tab[name];
    callback_tab[callback_tab.length] = callback;

	if ( sc_ajax_data_tab[name] )
	{
        sc_process_callbacks(name);
	}
}

function sc_eval_when_loaded_data_arrives(name, object_name)
{
	function _callback()
	{
		eval(sc_get_data_loaded_via_request(name, object_name));
	}
	sc_execute_when_loaded_data_arrives(name, _callback);
}

function sc_change_when_loaded_data_arrives(id, name, object_name)
{
	function _callback()
	{
		sc_change(id, sc_get_data_loaded_via_request(name, object_name));
	}
	sc_execute_when_loaded_data_arrives(name, _callback);
}

function sc_force_change_via_request(id, url,post_data)
{
	sc_internal_change_via_request(id, url, post_data, true);
}

function sc_change_via_request(id, url, post_data)
{
	sc_internal_change_via_request(id, url, post_data, false);
}

function sc_internal_change_via_request(id, url, post_data, force_it)
{
    var     item = document.getElementById(id);
    var     data = sc_get_data(id);
    var     is_post = (post_data != undefined) && (post_data != null);

    function _handler(xmlhttp)
    {
        // There's an open bug where IE status comes back as 1223 instead of 204.  Have to check both for now
        if( xmlhttp.status == HTTP_STATUS_NO_CONTENT || xmlhttp.status == HTTP_STATUS_NO_CONTENT_IE)
            return;

        var			redirect_url = sc_get_redirect_url(xmlhttp);
        if ( redirect_url && (redirect_url.length > 0) )
        {
            window.location.href = redirect_url;
        }
        else
        {
            item.innerHTML = xmlhttp.responseText;

            for ( var i = 0; /* no check */; ++i )
            {
                try
                {
                    var		script = xmlhttp.getResponseHeader(SC_IMMEDIATE_SCRIPT_HEADER_PREFIX + i);
                    if ( !script || (script.length == 0) )
                    {
                        break;
                    }
                    eval(script);
                }
                catch ( e )
                {
                    // ignore any errors
                    break;
                }
            }
        }
        data.request_in_progress = false;
    }

	var		ok_to_request = false;
	if ( (data.request_key == url) && data.request_in_progress )
	{
		ok_to_request = false;
	}
	else if ( (data.request_key != url) || force_it )
	{
		ok_to_request = true;
	}

	if ( ok_to_request )
    {
        data.request_key = force_it ? null : url;
		data.request_in_progress = true;

		sc_make_xmlhttp(_handler, url, is_post, post_data);
    }
}

function sc_change_after_time(url, milliseconds)
{
    function _change()
    {
        window.location.href = url;
    }

    if ( milliseconds == 0 )
    {
        _change();
    }
    else
    {
        setTimeout(_change, milliseconds);
    }
}

function sc_valueof(id)
{
	var     item = document.getElementById(id);
	return item.value ? item.value : "";
}

function sc_connect_to(id, script, event_name)
{
	var     item = document.getElementById(id);

	function _runner()
	{
		eval(script);
	}

	eval("item." + event_name + " = _runner");
}

function sc_change_src(id, url)
{
    var     item = document.getElementById(id);
    if ( item )
    {
        if ( item.contentWindow && item.contentWindow.location )
        {
            item.contentWindow.location.replace(url);
        }
        else
        {
            item.src = url;
        }
    }
}

function sc_eval_onload(script)
{
    var     parent_on_load = window.onload;
    function _handler()
    {
        if ( parent_on_load )
        {
            parent_on_load();
        }
        eval(script);
    }
    window.onload = _handler;
}

function sc_change(id, new_content)
{
    var     item = document.getElementById(id);
	item.innerHTML = new_content;
}

function sc_make_rollover_area(image_id, rollover_image_src)
{
    // pre-load rollover image
    var     rollover_image = new Image();
    rollover_image.src = rollover_image_src;

    var     rollover_item = document.getElementById(image_id);
    var     standard_image_src = rollover_item.src;

    rollover_item.onmouseover = _mouseover;
    rollover_item.onmouseout = _mouseout;

    function _mouseover()
    {
        rollover_item.src = rollover_image_src;
    }

    function _mouseout()
    {
        rollover_item.src = standard_image_src;
    }
}

function sc_make_popup_area(area_id, popup_id)
{
    sc_make_popup_area_with_delay(area_id, popup_id, 0);
}

function sc_make_popup_area_with_delay(area_id, popup_id, delay)
{
    var     area_item = document.getElementById(area_id);
    var     popup_item = document.getElementById(popup_id);
    var     popup_interval = null;
    var     area_item_mouseover_proc = area_item.onmouseover;

    if ( delay != 0 )
    {
        area_item.onmouseover = _delay_mouseover;
    }
    else
    {
        area_item.onmouseover = _mouseover;
    }

    area_item.onmouseout = _mouseout;
    area_item.style.cursor = "pointer";

    function _clear_interval()
    {
        if ( popup_interval != null )
        {
            window.clearInterval(popup_interval);
            popup_interval = null;
        }
    }

    function _close()
    {
        _clear_interval();
        sc_hide_show(popup_id, false);
        popup_item.onmouseover = null;
        popup_item.onmouseout = null;
    }

    function _popup_mouseover()
    {
        _clear_interval();
        sc_hide_show(popup_id, true);
    }

    function _popup_mouseout()
    {
        _clear_interval();
        popup_interval = window.setInterval(_close, 100);
    }

    function _delay_mouseover()
    {
        _clear_interval();
        window.setTimeout(_mouseover, delay);
    }

    function _mouseover()
    {
        if ( area_item_mouseover_proc )
        {
            area_item_mouseover_proc();
        }

        popup_item.onmouseover = _popup_mouseover;
        popup_item.onmouseout = _popup_mouseout;

        sc_hide_show(popup_id, true);
    }

    function _mouseout()
    {
        _clear_interval();
        popup_interval = window.setInterval(_close, 100);
    }
}

function sc_rotate_allocate(id)
{
    var     data = sc_get_data(id);
    if ( !data.rotate_data )
    {
        data.rotate_data = new Object();
        data.rotate_data.entries = new Array();
        data.rotate_data.index = -1;
        data.rotate_data.is_playing = false;
        data.rotate_data.current_nested_id = null;
    }

    return data;
}

function sc_rotate_add_content(id, transition_ticks, content)
{
    sc_rotate_add_internal(id, transition_ticks, null, null, content);
}

function sc_rotate_add_id(id, transition_ticks, source_id)
{
    sc_rotate_add_internal(id, transition_ticks, source_id, null, null);
}

function sc_rotate_add_nested_id(id, transition_ticks, nested_id)
{
    var     item = document.getElementById(nested_id);
    var     data = sc_get_data(id);

    sc_change_visibility(item, data, false);
    item.style.position = "absolute";
    item.style.top = 0;
    item.style.left = 0;

    var     parent = document.getElementById(id);
    if ( parent.offsetHeight < item.offsetHeight )
    {
        parent.style.height = item.offsetHeight + "px";
    }

    sc_rotate_add_internal(id, transition_ticks, null, nested_id, null);
}

function sc_rotate_add_internal(id, transition_ticks, source_id, nested_id, content)
{
    var     data = sc_rotate_allocate(id);

    var     entry_data = new Object();
    entry_data.transition_ticks = transition_ticks;
    entry_data.source_id = source_id;
    entry_data.nested_id = nested_id;
    entry_data.content = content;
    entry_data.interval = null;

    data.rotate_data.entries[data.rotate_data.entries.length] = entry_data;
}

function sc_rotate_change_content(id, index, transition_ticks, content)
{
    sc_rotate_change_internal(id, index, transition_ticks, null, content);
}

function sc_rotate_change_id(id, index, transition_ticks, source_id)
{
    sc_rotate_change_internal(id, index, transition_ticks, source_id, null);
}

function sc_rotate_change_internal(id, index, transition_ticks, source_id, content)
{
    var     data = sc_rotate_allocate(id);
    if ( (index >= 0) && (index < data.rotate_data.entries.length) )
    {
        var     entry_data = data.rotate_data.entries[index];
        entry_data.transition_ticks = transition_ticks;
        entry_data.source_id = source_id;
        entry_data.content = content;
    }
}

function sc_rotate_transport(id, opcode)
{
    var     item = document.getElementById(id);
    var     data = sc_rotate_allocate(id);

    function _rotate_interval()
    {
        if ( data.rotate_data.interval )
        {
            window.clearInterval(data.rotate_data.interval);
            data.rotate_data.interval = null;
        }

        if ( data.rotate_data.current_nested_id )
        {
            var     nested_item = document.getElementById(data.rotate_data.current_nested_id);
            var     nested_data = sc_get_data(data.rotate_data.current_nested_id);
            sc_change_visibility(nested_item, nested_data, false);
            data.rotate_data.current_nested_id = null;
        }

        ++data.rotate_data.index;
        if ( data.rotate_data.index >= data.rotate_data.entries.length )
        {
            data.rotate_data.index = 0;
        }
        else if ( data.rotate_data.index < 0 )
        {
            data.rotate_data.index = data.rotate_data.entries.length - 1;
        }

        if ( (data.rotate_data.index >= 0) && (data.rotate_data.index < data.rotate_data.entries.length) )
        {
            var     this_entry = data.rotate_data.entries[data.rotate_data.index];

            if ( this_entry.nested_id )
            {
                var     nested_item = document.getElementById(this_entry.nested_id);
                var     nested_data = sc_get_data(this_entry.nested_id);
                sc_change_visibility(nested_item, nested_data, true);
                data.rotate_data.current_nested_id = this_entry.nested_id;
            }
            else if ( this_entry.content )
            {
                item.innerHTML = this_entry.content;
            }
            else
            {
                var     source_item = document.getElementById(this_entry.source_id);
                item.innerHTML = source_item.innerHTML;
            }

            if ( data.rotate_data.is_playing )
            {
                data.rotate_data.interval = window.setInterval(_rotate_interval, this_entry.transition_ticks);
            }
        }
    }

    sc_hide_show(id, true);

    data.rotate_data.is_playing = (opcode == SC_TRANSPORT_PLAY);
    if ( opcode == SC_TRANSPORT_STOP )
    {
        --data.rotate_data.index;
    }
    else if ( opcode == SC_TRANSPORT_PREVIOUS )
    {
        if ( data.rotate_data.index <= 0 )
        {
            data.rotate_data.index = data.rotate_data.entries.length - 2;
        }
        else
        {
            data.rotate_data.index -= 2;
        }
    }

    _rotate_interval();
}

function sc_pre_show_hide(item, data, show_it)
{
    if ( data.toggle_id && show_it )
    {
        var     toggle_item = document.getElementById(data.toggle_id);
        var     toggle_data = sc_get_data(data.toggle_id);
        sc_change_visibility(toggle_item, toggle_data, false);
    }
}

function sc_post_show_hide(item, data, show_it)
{
    sc_check_on_show_inset(item, data, show_it);
    if ( data.toggle_id && !show_it )
    {
        var     toggle_item = document.getElementById(data.toggle_id);
        var     toggle_data = sc_get_data(data.toggle_id);
        sc_change_visibility(toggle_item, toggle_data, true);
    }
    data.is_busy = false;
}

function sc_check_on_show_inset(item, data, show_it)
{
    if ( data.on_show_inset_id )
    {
        var     inset_item = document.getElementById(data.on_show_inset_id);
        if ( show_it )
        {
            inset_item.style.padding = "0px 0px 0px " + item.offsetWidth + "px";
        }
        else
        {
            inset_item.style.padding = data.on_show_inset_padding;
        }
    }
}

function sc_wait_hide_show(id, show_it, ticks)
{
    var     data = sc_get_data(id);
    if ( data.wait_timeout )
    {
	return;
    }

    data.wait_timeout = window.setTimeout(_hide_show, ticks);
    function _hide_show()
    {
	data.wait_timeout = null;

        var     item = document.getElementById(id);
        var     should_be_invisible = !show_it;
        if ( sc_is_invisible(item) != should_be_invisible )
        {
            sc_hide_show(id, show_it);
        }
    }
}

function sc_force_hide_show(id,show_it)
{
    var     item = document.getElementById(id);
    var     data = sc_get_data(id);

    var prev_method = data.display_method;
    data.display_method = SC_DISPLAY_MODE_STANDARD;

    sc_hide_show(id,show_it);

    data.display_method = prev_method;

}

function sc_hide_show(id, show_it)
{
    var     item = document.getElementById(id);
    var     data = sc_get_data(id);

    if ( data.is_busy || data.is_in_popup )
    {
        return;
    }

    sc_hide_show_internal(id, item, data, show_it);
}

function sc_toggle_hide_show(id)
{
    var     item = document.getElementById(id);
    var     data = sc_get_data(id);

    if ( data.is_busy || data.is_in_popup )
    {
        return;
    }

    sc_hide_show_internal(id, item, data, sc_is_invisible(item));
}

function sc_hide_show_internal(id, item, data, show_it)
{
    if ( data.wait_timeout )
    {
    	window.clearTimeout(data.wait_timeout);
	    data.wait_timeout = null;
    }

    data.is_busy = true;

    sc_pre_show_hide(item, data, show_it);

    switch ( data.display_method )
    {
        default:
        case SC_DISPLAY_MODE_STANDARD:
        {
            if( !show_it )
                sc_show_selects(item);

            sc_reset(item, data);
            sc_change_visibility(item, data, show_it);
            sc_post_show_hide(item, data, show_it);

            if( show_it )
                sc_hide_selects(item);

            break;
        }

        case SC_DISPLAY_MODE_FADE:
        {
            sc_fade(id, show_it);
            break;
        }

        case SC_DISPLAY_MODE_SLIDE_DOWN:
        case SC_DISPLAY_MODE_SLIDE_UP:
        case SC_DISPLAY_MODE_SLIDE_LEFT:
        case SC_DISPLAY_MODE_SLIDE_RIGHT:
        {
            if( data.display_method_arguments.wait && show_it)
            {
                sc_slide_wait(id, data.display_method_arguments.width, data.display_method_arguments.height, data.display_method, show_it,data.display_method_arguments.wait);
            }
            else
                sc_slide(id, data.display_method_arguments.width, data.display_method_arguments.height, data.display_method, show_it);
            break;
        }

        case SC_DISPLAY_MODE_ZOOM:
        {
            sc_zoom(id, data.display_method_arguments.zoom_id, data.display_method_arguments.zoom_point_left, data.display_method_arguments.zoom_point_top, show_it);
            break;
        }
    }

}

function sc_slide_wait(id, width, height, mode, show, wait)
{
    var     item = document.getElementById(id);
    item.slide_wait_id = id;
    item.slide_wait_width = width;
    item.slide_wait_height = height;
    item.slide_wait_mode = mode;
    item.slide_wait_show = show;

    setTimeout('sc_slide_wait_internal(\''+id+'\');',wait);
}

function sc_slide_wait_internal(id)
{
    var     item = document.getElementById(id);
    sc_slide(item.slide_wait_id,item.slide_wait_width,item.slide_wait_height,item.slide_wait_mode,item.slide_wait_show);
}

function sc_slide(id, width, height, mode, show)
{
    var     item = document.getElementById(id);
    var     data = sc_get_data(id);
    sc_reset(item, data);

    data.has_been_slid = true;

    var	    current_top = 0;
    var     current_left = 0;
    switch ( mode )
    {
        case SC_DISPLAY_MODE_SLIDE_DOWN:
        {
            current_top = show ? (-1 * height) : 0;
            break;
        }

        case SC_DISPLAY_MODE_SLIDE_UP:
        {
            current_top = show ? sc_window_height() : sc_window_height() - height;
            current_left = 100;
            break;
        }

        case SC_DISPLAY_MODE_SLIDE_RIGHT:
        {
            current_left = show ? (-1 * width) : 0;
            break;
        }

        case SC_DISPLAY_MODE_SLIDE_LEFT:
        {
            current_left = show ? width : 0;
            break;
        }
    }

    var     interval = window.setInterval(_slide, SC_SLIDE_INTERVAL);
	function _slide()
	{
        var         is_done = false;
        switch ( mode )
        {
            case SC_DISPLAY_MODE_SLIDE_DOWN:
            {
                if ( show )
                {
                    current_top += SC_SLIDE_INCREMENT;
                    if ( current_top >= 0 )
                    {
                        is_done = true;
                    }
                }
                else
                {
                    current_top -= SC_SLIDE_INCREMENT;
                    if ( current_top <= (-1 * height) )
                    {
                        is_done = true;
                    }
                }
                break;
            }

            case SC_DISPLAY_MODE_SLIDE_UP:
            {
                if ( show )
                {
                    current_top -= SC_SLIDE_INCREMENT;
                    if ( current_top <= (sc_window_height() - height)  )
                    {
                        is_done = true;
                    }
                }
                else
                {
                    current_top += SC_SLIDE_INCREMENT;
                    if ( current_top >= sc_window_height() )
                    {
                        is_done = true;
                    }
                }
                break;
            }

            case SC_DISPLAY_MODE_SLIDE_RIGHT:
            {
                if ( show )
                {
                    current_left += SC_SLIDE_INCREMENT;
                    if ( current_left >= 0 )
                    {
                        is_done = true;
                    }
                }
                else
                {
                    current_left -= SC_SLIDE_INCREMENT;
                    if ( current_left <= (-1 * width) )
                    {
                        is_done = true;
                    }
                }
                break;
            }

            case SC_DISPLAY_MODE_SLIDE_LEFT:
            {
                if ( show )
                {
                    current_left -= SC_SLIDE_INCREMENT;
                    if ( current_left <= 0 )
                    {
                        is_done = true;
                    }
                }
                else
                {
                    current_left += SC_SLIDE_INCREMENT;
                    if ( current_left >= width )
                    {
                        is_done = true;
                    }
                }
                break;
            }
        }

		if ( is_done )
		{
			window.clearInterval(interval);

            if ( !show )
            {
                sc_change_visibility(item, data, false);
            }

	        if( mode != SC_DISPLAY_MODE_SLIDE_UP )
	        {
                item.style.top = "0px";
                item.style.left = "0px";
            }

            sc_post_show_hide(item, data, show);
        }
        else
        {
            item.style.top = current_top + "px";
            item.style.left = current_left + "px";
            sc_change_visibility(item, data, true);
        }
    }
}

function sc_set_opacity(item, percent)
{
    percent = (percent >= 100) ? 100 : percent;
	var     firefox_percent = (percent >= 100) ? 99.999 : percent;

	// IE/Win
    item.style.filter = "alpha(opacity=" + percent + ")";

    // Safari<1.2, Konqueror
	item.style.KHTMLOpacity = percent / 100;

	// Older Mozilla and Firefox
	item.style.MozOpacity = firefox_percent / 100;

	// Safari 1.2, newer Firefox and Mozilla, CSS3
    item.style.opacity = firefox_percent / 100;
}

function sc_remove_opacity(item)
{
	// IE/Win
    item.style.filter = null;

    // Safari<1.2, Konqueror
	item.style.KHTMLOpacity  = null;

	// Older Mozilla and Firefox
	item.style.MozOpacity  = null;

	// Safari 1.2, newer Firefox and Mozilla, CSS3
    item.style.opacity  = null;
}

function sc_fade(id, fade_in)
{
    var     item = document.getElementById(id);
    var     data = sc_get_data(id);

	var	    fade_rates = fade_in ? data.fade_in_rates : data.fade_out_rates;
	var	    rate = 0;
	var	    percent = fade_in ? 0 : data.opacity;
    var     direction = fade_in ? 1 : -1;

    sc_reset(item, data);

    data.has_been_faded = true;
    sc_set_opacity(item, percent);
    sc_change_visibility(item, data, true);
    if( !data.is_veil )
        sc_hide_selects(item);

    var     interval = window.setInterval(_fade, data.fade_interval);
    function _fade()
	{
		percent += direction * fade_rates[rate];
		if ( ++rate >= fade_rates.length )
		{
			rate = fade_rates.length - 1;
		}

        sc_set_opacity(item, percent);

        if ( (percent >= data.opacity) || (percent <= 0) )
		{
			if ( percent <= 0 )
			{
                data.has_been_faded = false;
                if( data.delete_on_fade_out && item.parentNode )
                    item.parentNode.removeChild(item);

                if( !data.is_veil )
                    sc_show_selects(item);

                sc_change_visibility(item, data, false);
                sc_set_opacity(item, data.opacity);
            }
            else
            {
                sc_show_selects(item);
            }

            window.clearInterval(interval);
            sc_post_show_hide(item, data, fade_in);
        }
	}
}

function sc_zoom(id, zoom_id, zoom_point_left, zoom_point_top, show_it)
{
    var     item = document.getElementById(id);
    var     data = sc_get_data(id);
    var     zoom_item = document.getElementById(zoom_id);
    var     zoom_data = sc_get_data(zoom_id);

    sc_reset(item, data);

    var     item_top_left = sc_get_top_left(item);

    var     zoom_left = show_it ? zoom_point_left : item_top_left.left;
    var     zoom_top = show_it ? zoom_point_top : item_top_left.top;
    var     zoom_width = show_it ? SC_ZOOM_MIN : item.offsetWidth;
    var     zoom_height = show_it ? SC_ZOOM_MIN : item.offsetHeight;

    var     zoom_end_left = show_it ? item_top_left.left : zoom_point_left;
    var     zoom_end_top = show_it ? item_top_left.top : zoom_point_top;

    var     number_of_steps = SC_ZOOM_STEP_QTY;

    var     needed_width_change = item.offsetWidth - SC_ZOOM_MIN;
    var     needed_height_change = item.offsetHeight - SC_ZOOM_MIN;
    var     width_change = Math.max(Math.round(needed_width_change / SC_ZOOM_STEP_QTY), 1);
    var     height_change = Math.max(Math.round(needed_height_change / SC_ZOOM_STEP_QTY), 1);

    var     needed_left_change = Math.abs(zoom_point_left - item_top_left.left);
    var     needed_top_change = Math.abs(zoom_point_top - item_top_left.top);
    var     left_change = Math.max(Math.round(needed_left_change / SC_ZOOM_STEP_QTY), 1);
    var     top_change = Math.max(Math.round(needed_top_change / SC_ZOOM_STEP_QTY), 1);
    if ( show_it )
    {
        if ( zoom_point_left > item_top_left.left )
        {
            left_change = -1 * left_change;
        }
        if ( zoom_point_top > item_top_left.top )
        {
            top_change = -1 * top_change;
        }
    }
    else
    {
        width_change = -1 * width_change;
        height_change = -1 * height_change;
        if ( item_top_left.left > zoom_point_left )
        {
            left_change = -1 * left_change;
        }
        if ( item_top_left.top > zoom_point_top )
        {
            top_change = -1 * top_change;
        }
    }

    sc_change_visibility(item, data, false);

    var         interval = window.setInterval(_zoom, 1);
    function _zoom()
    {
        zoom_item.style.left = zoom_left + "px";
        zoom_item.style.top = zoom_top + "px";
        zoom_item.style.width = zoom_width + "px";
        zoom_item.style.height = zoom_height + "px";
        sc_change_visibility(zoom_item, zoom_data, true);

        if ( number_of_steps <= 0 )
        {
            sc_change_visibility(zoom_item, zoom_data, false);
            sc_change_visibility(item, data, show_it);

            window.clearInterval(interval);
            sc_post_show_hide(item, data, show_it);
        }
        else
        {
            zoom_left += left_change;
            zoom_top += top_change;
            zoom_width += width_change;
            zoom_height += height_change;

            if ( zoom_width > item.offsetWidth )
            {
                zoom_width = item.offsetWidth;
            }
            else if ( zoom_width < SC_ZOOM_MIN )
            {
                zoom_width = SC_ZOOM_MIN;
            }
            if ( zoom_height > item.offsetHeight )
            {
                zoom_height = item.offsetHeight;
            }
            else if ( zoom_height < SC_ZOOM_MIN )
            {
                zoom_height = SC_ZOOM_MIN;
            }

            if ( left_change < 0 )
            {
                if ( zoom_left < zoom_end_left )
                {
                    zoom_left = zoom_end_left;
                }
            }
            else
            {
                if ( zoom_left > zoom_end_left )
                {
                    zoom_left = zoom_end_left;
                }
            }
            if ( top_change < 0 )
            {
                if ( zoom_top < zoom_end_top )
                {
                    zoom_top = zoom_end_top;
                }
            }
            else
            {
                if ( zoom_top > zoom_end_top )
                {
                    zoom_top = zoom_end_top;
                }
            }
        }
		number_of_steps--;
	}
}

function sc_window_height()
{
    return document.documentElement.clientHeight;
}

function sc_window_width()
{
    return document.documentElement.clientWidth;
}

function sc_match_height(resize_id, source_id)
{
    var     resize_item = document.getElementById(resize_id);
    var     source_item = document.getElementById(source_id);

    resize_item.style.height = source_item.offsetHeight + "px";
}

function sc_match_width(resize_id, source_id)
{
    var     resize_item = document.getElementById(resize_id);
    var     source_item = document.getElementById(source_id);

    resize_item.style.width = source_item.offsetWidth + "px";
}

function sc_is_ie()
{
    return navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent;
}

function sc_is_ie_6_or_earlier()
{
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    return sc_is_ie() && (rslt != null && Number(rslt[1]) < 7);
}

function sc_resize_window_content(width, height)
{
	var isXPSP2 = (navigator.userAgent.indexOf("NT 5.1")!=-1 && navigator.appMinorVersion.indexOf("SP2")!=-1);
	if ( isXPSP2 )
	{
		height += 30;
	}

	if ( document.all )
	{
		width += 12;
		height += 31;
	}

	window.resizeTo(width, height);
}


// --- popup stuff

window.onunload = popup_close;

var		popup_image_window = null;
var		popup_scroll_window = new Array();
var		popup_depth = 0;

function popup_close()
{
    if ( popup_image_window != null )
    {
        if ( !popup_image_window.closed )
        {
			try
			{
				popup_image_window.close();
				popup_image_window.opener = null;
			}
			catch ( ignore )
			{
			}
        }
    }
    if ( popup_scroll_window != null )
    {
        for ( var i = 0; i < popup_scroll_window.length; i++ )
        {
            if ( !popup_scroll_window[i].closed )
            {
				try
				{
					popup_scroll_window[i].close();
                	popup_scroll_window[i].opener = null;
				}
				catch ( ignore )
				{
				}
			}
        }
    }
    return false;
}

function popup_open_scroll(url, width, height, under)
{
	if ( popup_depth == 0 && window.opener != null )
    {
        try
        {
            popup_depth = window.opener.popup_depth + 1;
        }
        catch(err)
        {
            popup_depth = 0;
        }
    }
    var		depth = popup_depth + 1;
    var		popup_name;
    var		index = popup_scroll_window.length;
    if ( popup_depth > 0 )
        popup_name = 'cc_scroll' + depth;
    else
        popup_name = 'cc_scroll';
	var popup_window = window.open(url, popup_name, 'status=no,directories=no,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height);
	if (typeof popup_window == "undefined" || popup_window == null) return;
    popup_scroll_window[index] = popup_window;
    window.name='the_opener';
    if ( popup_scroll_window != null )
    {
        if ( under == 1 )
        {
            popup_scroll_window[index].blur();
        }
        else
        {
            popup_scroll_window[index].focus();
        }
    }
    else
    {
        window.href = url;
    }
}

function popup_open_nonscroll(url, width, height, under)
{
	if ( popup_depth == 0 && window.opener != null )
		popup_depth = window.opener.popup_depth + 1;
	var		depth = popup_depth + 1;
	var		popup_name;
	var		index = popup_scroll_window.length;
	if ( popup_depth > 0 )
		popup_name = 'cc_scroll' + depth;
	else
		popup_name = 'cc_scroll';
	popup_scroll_window[index] = window.open(url, popup_name, 'status=no,directories=no,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height);
	window.name='the_opener';
	if ( under == 1 )
	{
		popup_scroll_window[index].blur();
	}
	else
	{
		popup_scroll_window[index].focus();
	}
}

function popup_blank_offset(url)
{
	popup_named_offset(url, "_blank");
}

function popup_named_offset(url, name)
{
    var     screen_fraction_x = 2 * (screen.width / 3);
    var     screen_fraction_y = 2 * (screen.height / 3);
    var     position_left = screen_fraction_x;
    var     position_top = screen_fraction_y;
    var     width = screen_fraction_x - 25;
    var     height = screen_fraction_x - 25;

    var		w = window.open
	(
		url,
		name,
		'status=yes,directories=yes,location=yes,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,fullscreen=no' +
        ',left=' + position_left +
        ',top=' + position_top +
        ',width=' + width +
        ',height=' + height
    );
    w.focus();
}

// --- end popup stuff

function no_close_window(window_id, url, width, height)
{
    my_window = window.open(url, window_id,'status=no,directories=no,status=no,directories=no,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' +height);
    my_window.focus();
}

function make_tag(tag_name)
{
    return '<' +
    tag_name +
    '>';
}

function popup_open_image(title, image, width, height, under, scrollable)
{
	var				url = "/op/img-" + sc_url_encode(image) + '-' + width + '-' + height + '-' + sc_url_encode(title);
	popup_image_window = window.open(url, 'cc_image', 'status=no,directories=no,status=no,directories=no,location=no,toolbar=no,menubar=no,scrollbars='+scrollable+',resizable=yes,width=' + width + ',height=' + height);
    if ( under == 1 )
    {
        popup_image_window.blur();
    }
    else
    {
        popup_image_window.focus();
    }
}

function set_domain(domain_str)
{
    // Don't do this for IE.
    if ( !document.all )
    {
        document.domain = domain_str;
    }
}

function load_from_popup(url)
{
    window.location=url;
}

function load_opener(url)
{
    if ( document.all )
    {
        window.open(url,'the_opener');
    }
    else
    {
        window.opener.load_from_popup(url);
    }
    window.close();
}

// text submit stuff

function ccts_clear_all_fields(the_field, the_form)
{
    the_elements = the_form.elements;
    for ( j = 0; j < the_elements.length; ++j )
    {
        this_element = the_elements[j];
        if ( this_element != the_field )
        {
            this_element_name = this_element.name;
            if ( this_element_name.length > 6 )
            {
                this_element_base_name = this_element_name.substring(0, 6);
                if ( this_element_base_name == "_ccts_" )
                {
                    this_element.value = "";
                }
            }
        }
    }
    return true;
}

function ccts_text_submit(the_field)
{
    the_form = the_field.form;
    ccts_clear_all_fields(the_field, the_form);

    the_field.value = the_field.name;
    the_form.submit();
}

// don't go stuff....

var sc_dg_cleared = false;

function sc_dg_set()
{
	sc_dg_cleared = true;
}

function sc_dg_check()
{
	if ( !sc_db_cleared )
	{
		window.alert("If you go away...");
	}
}

function sc_init_dg()
{
	if ( window.onunload )
	{
		window.onunload = window.onunload + ";sc_dg_check();";
	}
	else
	{
		window.onunload = "sc_dg_check();";
	}

	var 	anchors = document.getElementsByTagName("A");
	for ( var i = 0; i < anchors.length; i++ )
	{
		if ( anchors[i].onclick )
		{
			anchors[i].onclick = anchors[i].onclick + ";sc_dg_set();";
		}
		else
		{
			anchors[i].onclick = "sc_dg_set();";
		}
	}

	var 	forms = document.getElementsByTagName("FORM");
	for ( var i = 0; i < forms.length; i++ )
	{
		if ( forms[i].onsubmit )
		{
			forms[i].onsubmit = forms[i].onsubmit + ";sc_dg_set();";
		}
		else
		{
			forms[i].onsubmit = "sc_dg_set();";
		}
	}
}

// ..................Transparent PNG fix for IE
if (sc_is_ie())
{
	window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	for (var i = document.all.length - 1, obj = null; (obj = document.all[i]); i--) {
		if (itsAllGood && obj.currentStyle.backgroundImage.match(/\.png/i) != null) {
			this.fnFixPng(obj);
			obj.attachEvent("onpropertychange", this.fnPropertyChanged);
		}
	}
}

function fnPropertyChanged() {
	if (window.event.propertyName == "style.backgroundImage") {
		var el = window.event.srcElement;
		if (!el.currentStyle.backgroundImage.match(/x\.gif/i)) {
			var bg	= el.currentStyle.backgroundImage;
			var src = bg.substring(5,bg.length-2);
			el.filters.item(0).src = src;
			el.style.backgroundImage = "url(/ps.gif)";
		}
	}
}

function fnFixPng(obj) {
	var bg	= obj.currentStyle.backgroundImage;
	var src = bg.substring(5,bg.length-2);
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
	obj.style.backgroundImage = "url(/ps.gif)";
}


function select_country(the_form,prefix)
{
    var    	selected_index = the_form[prefix +'.field_state'].selectedIndex;
    var    	country_index = state_to_country_tab[selected_index];
    if ( country_index != -1 )
    {
        the_form[prefix +'.field_country'].selectedIndex = country_index;
    }

    var     region_element = the_form[prefix +'.field_region']
    if( region_element )
    {
        region_element.readOnly = selected_index != 0;
        region_element.disabled = selected_index != 0;
        if( region_element.readOnly )
            region_element.value = '';
    }
}

function select_state(the_form, prefix)
{
    var    	state_index = country_to_state_tab[the_form[prefix +'.field_country'].selectedIndex];


    the_form[prefix +'.field_state'].selectedIndex = state_index;

    var     region_element = the_form[prefix +'.field_region']
    if( region_element )
    {
        region_element.readOnly = state_index != 0;
        region_element.disabled = state_index != 0;
        if( region_element.readOnly )
            region_element.value = '';
    }
}

function region_check_for_change(the_form, prefix)
{
    if ( the_form[prefix +'.field_region'].value.length != 0 )
    {
        the_form[prefix +'.field_state'].selectedIndex = 0;
    }
}

function cc_checkout_single_new_user_ship_calc(zip_id,country_id,state_id,javascript_eval_str)
{
    var zip = document.getElementById(zip_id);
    var state = document.getElementById(state_id);
    var country = document.getElementById(country_id);

    if( ((state && state.selectedIndex != 0) || (country.value != 334 && country.selectedIndex > 0 )) && zip.value.length > 0 )
    {
        eval(javascript_eval_str);
    }
}

function cc_checkout_single_new_user_add_payment(account_number_id,javascript_eval_str)
{
    var account_number = document.getElementById(account_number_id);

    if( account_number && account_number.value.length > 0 )
    {
        eval(javascript_eval_str);
    }
}

function sc_hide_class_pair(className, childClassName)
{
    var divs = document.getElementsByTagName("div");
    for (var i in divs)
    {
        var div = divs[i];

        if( div.className && div.className.toLowerCase() == className.toLowerCase() )
        {
            for( var k in div.childNodes )
            {
                var child = div.childNodes[k];

                if( child.className && child.className.toLowerCase() == childClassName.toLowerCase() )
                {
                    sc_force_hide_show(child.id,false);
                }
            }
        }
    }
}

function sc_hide_class(tagName,className,showit)
{
    var tags = document.getElementsByTagName(tagName);
    for (var i in tags)
    {
        var tag = tags[i];

        if( tag.className && tag.className.toLowerCase() == className.toLowerCase() )
        {
            if( !showit )
                tag.style.display = 'none';
            else
                tag.style.display = '';
        }
    }
}

function sc_intra_page_scroll(anchor_name)
{
	window.location.href = "#" + anchor_name;
}

function sc_set_focus(form_id, field_id)
{
    if ( form_id )
    {
        var form = document.forms[form_id];
        if( form )
        {
            var field = form.elements[field_id];
            if( field )
                field.focus();
        }
    }
    else
    {
        var     f = document.getElementById(field_id);
        if ( f )
        {
            f.focus();
        }
    }
}

function sc_get_form_by_name(name)
{
    var     the_form = document.getElementById(name);
    if( !the_form )
    {
        // May have given the form a name and not an ID
        var forms = window.document.getElementsByTagName('form');
        for (var i in forms)
        {
            var local_form = forms[i];
            if( local_form.name && local_form.name == name )
            {
                the_form = local_form;
                break;
            }
        }
    }

    return the_form;
}

function sc_submit_form(form_id)
{
    var form = sc_get_form_by_name(form_id);
    if( form ) form.submit();
}

function sc_delayed_input_disable(obj)
{
    function _disable()
    {
        if( obj )
            obj.disabled = true;
    }

    window.setInterval(_disable, 500);
}

function sc_index_of(the_array, search_for)
{
    for ( var i = 0; i < the_array.length; ++i )
    {
        if ( the_array[i] == search_for )
        {
            return i;
        }
    }
    return -1;
}

function sc_form_field_values(form_name, field_name, separator, current_values)
{
    var     current_values_tab = (current_values && (current_values.length > 0)) ? current_values.split(separator) : new Array();

    var     form = sc_get_form_by_name(form_name);
    if ( form && form.elements )
    {
        for ( var i = 0; i < form.elements.length; ++i )
        {
            var e = form.elements.item(i);
            if ( e.name && e.value && (e.name == field_name) )
            {
                var encoded_value = sc_url_encode(e.value);
                var current_index = sc_index_of(current_values_tab, encoded_value);
                if ( current_index >= 0 )
                {
                    current_values_tab.splice(current_index, 1);
                }

                if ( (e.type == "checkbox") || (e.type == "radio") )
                {
                    if ( e.checked )
                    {
                        current_values_tab.push(encoded_value);
                    }
                }
                else
                {
                    current_values_tab.push(encoded_value);
                }
            }
        }
    }

    return current_values_tab.join(separator);
}

function sc_form_field_values_click(base_url, form_name, field_name, separator, current_values)
{
    window.location.href = base_url + sc_form_field_values(form_name, field_name, separator, current_values);
}

function sc_submit_form_click(form_name, field_name)
{
    var form = document.forms[form_name]
    form.action = sc_safe_append_url_parameter(sc_safe_append_url_parameter(form.action,field_name + ".y",1),field_name + ".x",1);
    form.submit();
}

function sc_safe_append_url_parameter(url, name, value)
{
    if( url.indexOf('?') > -1 )
        return url + '&' + name + '=' + sc_url_encode(value);
    else
        return url + '?' + name + '=' + sc_url_encode(value);
}

function sc_get_style_top(item)
{
    return item.style.top.substring(0,item.style.top.indexOf('px')) * 1;
}

function sc_get_style_left(item)
{
    return item.style.left.substring(0,item.style.left.indexOf('px')) * 1;
}

function sc_select_element(the_form, field_name, field_value)
{
	for (var i = 0; i < the_form.elements.length; ++i)
	{
        var     element = the_form.elements[i];
        if ( element.name.toLowerCase() == field_name.toLowerCase() )
        {
            if ( element.tagName.toLowerCase() == "select" )
            {
                for ( var j = 0; j < element.options.length; ++j )
                {
                    var     option = element.options[j];
                    if ( option.value.toLowerCase() == field_value.toLowerCase() )
                    {
                        element.selectedIndex = j;
                        break;
                    }
                }
                break;
            }
            if ( element.tagName.toLowerCase() == "input" && element.type.toLowerCase() == "checkbox" )
            {
                element.checked = true;
                break;
            }
            else if ( element.value.toLowerCase() == field_value.toLowerCase() )
            {
                element.checked = true;
                break;
            }
        }
	}
}

var SC_CHAR_COUNTER_ID = "char_counter"
function sc_textarea_char_counter(field_name, max)
{
    var field = sc_find_field(field_name);

    if( field == null )
        return;

    var size = field.value.length;
    if(size > max )
    {
        field.value = field.value.substring(0,max);
        size = max;
    }

    var element = document.getElementById(SC_CHAR_COUNTER_ID+ "." + field_name);
    element.innerHTML = '('+Math.max(max - size,0)+' Chars. left)';
}

function sc_find_field(field_name)
{
    for ( var i = 0; i < document.forms.length; ++i )
    {
        if ( document.forms[i].elements[field_name] != null )
        {
            return document.forms[i].elements[field_name];
        }
    }

    return null;
}

function sc_return_key_override(id)
{
    function _overridden_event(event)
    {
        var keynum;
        var is_text_area = false;

        if (window.event) // eg. IE
        {
            keynum = window.event.keyCode;
            is_text_area = window.event.srcElement.type.toLowerCase() == "textarea";
        }
        else if (event.which) // eg. Firefox
        {
            keynum = event.which;
            is_text_area = event.target.type.toLowerCase() == "textarea";
        }

        if ( keynum == 13  && !is_text_area)
        {
            var element = document.getElementById(id);

            if( element == null )
                return true;

            var elements = element.getElementsByTagName("input");

            for ( var j = 0; j < elements.length; ++j )
            {
                var     innerElement = elements[j];
                if ( innerElement.type == 'image' )
                {
                    innerElement.parentNode.onclick();
                    return false;
                }
            }
        }

        return true;
    }

    function _trap_function(event)
    {
        var keynum;

        if (window.event) // eg. IE
        {
            keynum = window.event.keyCode;
        }
        else if (event.which) // eg. Firefox
        {
            keynum = event.which;
        }

        return keynum != 13;
    }

    document.onkeyup = _overridden_event;
    document.onkeydown = _trap_function; // IE, Firefox, Safari
    document.onkeypress = _trap_function; // only Opera needs the backspace nullifying in onkeypress
}

function sc_disable_return_key_override()
{
    function _normal_event(event)
    {
        return true;
    }

    document.onkeyup = _normal_event;
    document.onkeydown = _normal_event; // IE, Firefox, Safari
    document.onkeypress = _normal_event; // only Opera needs the backspace nullifying in onkeypress
}

//---------- hint stuff start

var SC_HINT_ID_PREFIX = "hint_";
var SC_HINT_DATA_NAME = "__snhints";
var SC_HINT_MAX = 10;

function sc_unselect_hint(text_field_id)
{
	var	data = sc_get_data(text_field_id);

	if ( data.hint.selected_index >= 0 )
	{
		var	hint_div = document.getElementById(SC_HINT_ID_PREFIX + data.hint.selected_index);
        if ( hint_div != null )
        {
            hint_div.setAttribute("class", data.hint.unselected_class_name);
            hint_div.setAttribute("className", data.hint.unselected_class_name);
            hint_div.style.backgroundColor = '#fff';
        }
	}
	data.hint.selected_index  = -1;
}

function sc_select_hint(text_field_id, new_index, update_text_field, submit_form)
{
	sc_unselect_hint(text_field_id);

	var	data = sc_get_data(text_field_id);
    var	text_field = document.getElementById(text_field_id);

    var         record = null;
    if ( data.hint.selected_index != new_index )
	{
		data.hint.selected_index = new_index;
        record = data.hint.narrowed_word_tab[data.hint.selected_index];

		var	hint_div = document.getElementById(SC_HINT_ID_PREFIX + data.hint.selected_index);

       if ( hint_div != null )
       {
           hint_div.setAttribute("class", data.hint.selected_class_name);
           hint_div.setAttribute("className", data.hint.selected_class_name);
       }

        if ( update_text_field)
        {
            if ( hint_div != null )
                hint_div.style.backgroundColor = '#e9eef8';
            if ( (record != null) && (record != undefined) && record.redirect_url == null )
            {
                text_field.value = record.display_word;
            }
        }
	}
    else
    {
        record = data.hint.narrowed_word_tab[data.hint.selected_index];
    }

    if ( submit_form && (record != null) )
    {
        if ( record.redirect_url != null )
        {
            window.location.href = record.redirect_url;
        }
        else
        {
            text_field.form.submit();
        }
    }
}

function sc_fetch_hints(data, text_field_id)
{
    var		text_field = document.getElementById(text_field_id);

    data.hint.current_loaded_hint_prefix = text_field.value.slice(0, 1).toUpperCase();
    data.hint.word_tab = new Array();
    data.hint.redirect_tab = new Array();

    var     url = data.hint.base_url.replace(data.hint.url_replacement, sc_url_encode(data.hint.current_loaded_hint_prefix));
    sc_load_via_request(SC_HINT_DATA_NAME, url, null);
    sc_eval_when_loaded_data_arrives(SC_HINT_DATA_NAME, text_field_id);
}

function sc_update_hint(text_field_id, force_clear)
{
    var		data = sc_get_data(text_field_id);

    sc_unselect_hint(text_field_id);

	var		text_field = document.getElementById(text_field_id);

    if ( force_clear || (text_field.value.length == 0) )
    {
        data.hint.current_loaded_hint_prefix = "";
    }
    else if ( data.hint.current_loaded_hint_prefix != text_field.value.slice(0, 1).toUpperCase() )
    {
        sc_fetch_hints(data, text_field_id);
    }

    var     local_redirect_tab = force_clear ? new Array() : sc_hint_search_word_tab(text_field.value, SC_HINT_MAX, data.hint.redirect_tab);
    var     local_word_tab = force_clear ? new Array() : sc_hint_search_word_tab(text_field.value, SC_HINT_MAX - local_redirect_tab.length, data.hint.word_tab);

    data.hint.redirect_qty = local_redirect_tab.length;
    
    data.hint.narrowed_word_tab = local_redirect_tab.concat(local_word_tab);
	data.hint.found_qty = data.hint.narrowed_word_tab.length;

    var		new_content = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" >\n";
    for ( var i = 0; i < data.hint.found_qty; ++i )
	{
        var	    hint_id = SC_HINT_ID_PREFIX + i;
        var 	mouse_over_command = "sc_select_hint('" + text_field_id + "', '" + i + "', false, false);";
        var 	mouse_click_command = "sc_select_hint('" + text_field_id + "', '" + i + "', true, true);return false;";

        if ( (data.hint.redirect_qty > 0) && (i == data.hint.redirect_qty) )
        {
            new_content = new_content + "<tr><td><hr></td></tr>\n";
        }

        new_content = new_content + "<tr><td id=\"" + hint_id + "\" class=\"" + data.hint.unselected_class_name + "\" onClick=\"" + mouse_click_command + "\" onMouseOver=\"" + mouse_over_command + "\">" + data.hint.narrowed_word_tab[i].display_word + "</td></tr>\n";
    }
    new_content = new_content + "</table>\n"

    var     make_visible = (data.hint.found_qty > 0);
    var     popup = document.getElementById(data.hint.popup_id);
    var     popup_content = document.getElementById(data.hint.popup_content_id);

    if ( make_visible )
    {
        data.hint.previous_value = text_field.value;
        popup_content.innerHTML = new_content;

        popup.style.left = sc_get_posx(text_field, false) + "px";
        popup.style.fontFamily = text_field.style.fontFamily;
        popup.style.fontSize = text_field.style.fontSize;
    }
    popup.style.visibility = make_visible ? "visible" : "hidden";
    data.hint.is_visible = make_visible;
}

function sc_add_to_hint_word_tab(text_field_id, match_word, display_word, redirect_url)
{
    if ( match_word.length == 0 )
    {
        sc_update_hint(text_field_id, false);
    }
    else
    {
        var	        data = sc_get_data(text_field_id);
        var         word_record = new Object();
        word_record.match_word = match_word.toUpperCase();
        word_record.display_word = display_word;
        word_record.redirect_url = redirect_url;

        if ( redirect_url == null )
        {
            data.hint.word_tab.push(word_record);
        }
        else
        {
            data.hint.redirect_tab.push(word_record);
        }
    }
}

function sc_set_hint_to_previous_value(data, text_field)
{
    if ( data.hint.previous_value != null )
    {
        text_field.value = data.hint.previous_value;
    }
}

function sc_handle_hint_direction(data, text_field_id, direction)
{
    var new_selected_index;

    if ( data.hint.selected_index == -1 )
    {
        if ( !data.hint.is_visible )
        {
            sc_update_hint(text_field_id, false);
        }
        new_selected_index = (direction < 0) ? (data.hint.found_qty - 1) : 0;
    }
    else
    {
        var         current_column_start = data.hint.selected_index;
        var         current_column_end = data.hint.found_qty - 1;

        new_selected_index = data.hint.selected_index + direction;
        if (direction < 0)
        {
            if (new_selected_index < 0)
            {
                new_selected_index = current_column_end;
            }
        }
        else if (direction > 0)
        {
            if (new_selected_index > current_column_end)
                new_selected_index = 0;
        }
    }

    sc_set_hint_new_selected_index(data, text_field_id, new_selected_index);
}

function sc_set_hint_new_selected_index(data, text_field_id, new_selected_index)
{
    var	text_field = document.getElementById(text_field_id);
    if ( (new_selected_index >= 0) && (new_selected_index < data.hint.found_qty) )
    {
        sc_select_hint(text_field_id, new_selected_index, true, false);
    }
    else
    {
        sc_unselect_hint(text_field_id);
        sc_set_hint_to_previous_value(data, text_field);
        data.hint.selected_index = -1;
    }
}

function sc_make_hint(base_url, url_replacement, text_field_id, popup_id, popup_content_id, unselected_class_name, selected_class_name)
{
    var	data = sc_get_data(text_field_id);
    var	text_field = document.getElementById(text_field_id);
    var popup = document.getElementById(popup_id);

	data.hint = new Object();
	data.hint.base_url = base_url;
	data.hint.url_replacement = url_replacement;
	data.hint.current_loaded_hint_prefix = "";
    data.hint.selected_index = -1;
	data.hint.found_qty = 0;
	data.hint.redirect_qty = 0;
	data.hint.popup_id = popup_id;
	data.hint.popup_content_id = popup_content_id;
	data.hint.update_is_set = false;
	data.hint.unfocus_is_set = false;
	data.hint.unfocus_timeout_id = 0;
    data.hint.word_tab = new Array();
    data.hint.redirect_tab = new Array();
    data.hint.narrowed_word_tab = new Array();
    data.hint.previous_value = null;
    data.hint.is_visible = false;
    data.hint.unselected_class_name = unselected_class_name;
    data.hint.selected_class_name = selected_class_name;

    function _do_update()
	{
		sc_update_hint(text_field_id, false);
        data.hint.update_is_set = false;
	}

    function _set_to_previous_value()
    {
        sc_set_hint_to_previous_value(data, text_field);
    }

    function _handle_key(the_event)
	{
		if ( !the_event && event )
		{
			the_event = event;
		}

		var	c = "";
		if ( the_event.keyCode )
		{
			c = the_event.keyCode;
		}
		else if ( the_event.which )
		{
			c = the_event.which;
		}
		else if ( the_event.charCode )
		{
			c = the_event.charCode;
		}

        var	result = true;
		if ( c == 27 )	// esc
		{
            _set_to_previous_value();
            sc_update_hint(text_field_id, true);
            result = false;
        }
        else if ( (c == 38) || (c == 40) )	// 38=up 40=down
		{
            var direction = (c == 38) ? -1 : 1;
            sc_handle_hint_direction(data, text_field_id, direction);
            result = false;
		}
        else
        {
            if ( c == 13 )  // enter
            {
                if ( data.hint.selected_index >= 0 )
                {
                    var record = data.hint.narrowed_word_tab[data.hint.selected_index];
                    if ( record.redirect_url != null )
                    {
                        window.location.href = record.redirect_url;
                        result = false;
                    }
                }
            }

            if ( result && !data.hint.update_is_set )
            {
                data.hint.update_is_set = true;
                window.setTimeout(_do_update, 500);
            }
        }

        the_event.returnValue = result;
		return result;
	}

    function _lose_focus()
    {
        sc_update_hint(text_field_id, true);
        data.hint.unfocus_is_set = false;
    }

    function _cancel_unfocus_timeout()
    {
        if ( data.hint.unfocus_is_set )
        {
            data.hint.unfocus_is_set = false;
            window.clearTimeout(data.hint.unfocus_timeout_id);
        }
    }

    function _lose_focus_timeout()
    {
        if ( !data.hint.unfocus_is_set )
        {
            data.hint.unfocus_is_set = true;
            data.hint.unfocus_timeout_id = window.setTimeout(_lose_focus, 500);
        }
    }

    text_field.onkeydown = _handle_key;
	text_field.setAttribute("autocomplete", "off");
    text_field.onblur = _lose_focus_timeout;
    text_field.onfocus = _cancel_unfocus_timeout;

    popup.onblur = _lose_focus_timeout;
    popup.onfocus = _cancel_unfocus_timeout;
    popup.style.textAlign = "left";
    popup.style.fontWeight = "normal";
    popup.style.position = "absolute";
	popup.style.overflow = "hidden";
	popup.style.visibility = "hidden";
	popup.style.border = "1px solid black";
	popup.style.backgroundColor = "white";
	popup.style.color = "black";
	popup.style.zIndex = 1;
    popup.style.cursor = "default";
}

function sc_hint_prefix_compare(tab, prefix, mid)
{
    var     uppercasePrefix = prefix.toUpperCase();
    var     midVal = tab[mid].match_word;
    if ( !midVal )
	{
		midVal = "";
	}

    var     cmp;
    if ( midVal.length < uppercasePrefix.length )
    {
        var uppercaseMidVal = midVal.toUpperCase();
        cmp = (uppercaseMidVal < uppercasePrefix) ? -1 : ((uppercaseMidVal > uppercasePrefix) ? 1 : 0);
        if ( cmp == 0 )
        {
            cmp = -1;
        }
    }
    else
    {
        var midValPrefix = midVal.substring(0, uppercasePrefix.length).toUpperCase();
        cmp = (midValPrefix < uppercasePrefix) ? -1 : ((midValPrefix > uppercasePrefix) ? 1 : 0);
    }

    return cmp;
}

function sc_hint_prefix_bsearch(tab, prefix)
{
    // bsearch code intially from JDK...
    var low = 0;
    var high = tab.length - 1;

    while ( low <= high )
    {
        var     mid = Math.floor((low + high) / 2);
        var     cmp = sc_hint_prefix_compare(tab, prefix, mid);

        if ( cmp < 0 )
        {
            low = mid + 1;
        }
        else if ( cmp > 0 )
        {
            high = mid - 1;
        }
        else
        {
            return mid; // key found
        }
    }
    return -1;  // key not found
    //...............
}

function sc_hint_search_word_tab(prefix, max_results, word_tab)
{
    var     found_tab = new Array();

    var     index = (prefix.length > 0) ? sc_hint_prefix_bsearch(word_tab, prefix) : -1;
    while ( index > 0 )
    {
        // go to start of section
        if ( sc_hint_prefix_compare(word_tab, prefix, index - 1) != 0 )
        {
		    break;
        }
        --index;
    }

    if ( index >= 0 )
    {
        while ( (index < word_tab.length) && (max_results > 0) && (sc_hint_prefix_compare(word_tab, prefix, index) == 0) )
        {
            found_tab.push(word_tab[index]);

            ++index;
            --max_results;
        }
    }

    return found_tab;
}

//---------- hint stuff end

