function addObjectEvent(obj, evType, fn, useCapture){
  if (!obj){
      return false;
  }

  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  }
}

function addEvent(objid, evType, fn, useCapture){
  var obj = document.getElementById(objid);

  addObjectEvent(obj, evType, fn, useCapture);
} 

function removeEvent(objid, evType, fn, useCapture){
  var obj = document.getElementById(objid);
 
  if (!obj){
      return false;
  }

  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  }
}

function decor_submit_page(){
  if( document.copage && document.copage.destination &&
      (document.copage.destination.value == '') ){ 
    document.copage.destination.value = default_destination;
  }
  document.copage.decor_redirect_url.value = '';
  document.copage.submit();
  return true; 
}

function search_type_change(){
    var widget = document.getElementById("decor_search_type");
    var button = document.getElementById("search_button");
    if (widget && button){
	if( widget.options[widget.selectedIndex].text == "Products" ){
	   
	    document.getElementById('multisearch').style.display = 'inline';
	    if (document.getElementById('ps')){
		document.getElementById('ps').style.display = 'inline';
	    }
	    var search_name = document.getElementById('search_name');	    

	    button.value = search_name.value; 
	} else {         
	    document.getElementById('multisearch').style.display = 'none';
	    if (document.getElementById('ps')){
		document.getElementById('ps').style.display = 'none';
	    }
	    
	    button.value = "Search";
	}

    	var selectedoption = widget.options[widget.selectedIndex];
	var which_option = selectedoption.id;
	var options = ds_all_options[which_option];
	if (!options){
	    options = new Array();
	}


	
	
	var field_select = document.getElementById('decor_search_field');
	for(i = 0; i < options.length; i+=1){
	    field_select.options[i] = options[i];
	    
	    var default_option = document.getElementById('pref_' + which_option);
	    if (default_option){
		var default_values = default_option.value.split("?");
		
		var field_option = default_values[0];
		var style_option = default_values[1];
		    
		if (field_option && field_select.options[i].value == field_option){
		    field_select.selectedIndex = i;
		}
	    }
	}
	
	if (style_option){
	    var style_elem = document.getElementById("decor_search_style_" + style_option);
	    style_elem.checked = "checked";
	}	
	
	field_select.options.length = options.length;

	widget.focus();
    }
    
    check_selection();
}

function check_selection()
{
  var dropdown;
  var rdbutton;
  dropdown = document.getElementById("decor_search_type");
  rd_sw = document.getElementById("decor_search_style_sw");
  rd_ct = document.getElementById("decor_search_style_ct");
  if(dropdown && rd_sw && rd_ct && dropdown.selectedIndex >= 0)
  {
    if(dropdown.options[dropdown.selectedIndex].text == "Products")
    {
      rd_sw.checked = "checked";
      rd_ct.disabled = true;
    }
    else
    {
      rd_ct.disabled = false;
    }
  }
}

function set_search_redirect(){
    var temp = document.copage.decor_search_type;  
    var temp_value = escape( document.copage.decor_search_string.value);

    temp_value = temp_value.replace( /\+/, '%2B');

   var url = temp.options[temp.selectedIndex].value + 
      "search_string=" + temp_value;
    
    var selected_type = document.getElementById('decor_search_type');
    if (selected_type){
	url = url + "&search_type=" + escape(selected_type.options[selected_type.selectedIndex].id);
    }

    var search_field = document.getElementById('decor_search_field');
    if (search_field){
	url = url + "&search_field=" + escape(search_field.value);
    }

    var search_style = document.getElementsByName('decor_search_style');
    if (search_style){
      for (var i=0;i<search_style.length;i++)
      {
        if(search_style[i].checked == true)
        {
	        url = url + "&search_style=" + escape(search_style[i].value);
          break;
        }
      }
    }
    set_redirect(url);
    return true;
}

function dead_link(){
    // This function was intentionally left blank
}

function search_string_keypress_handler(e){
    key = e.keyCode;
    if( key == 13 ){
	set_search_redirect(); 
    }
    return true;
}


function show_help( myURL, width, height){
    if (width){
    } else {
	width = 800;
    }

    if (height){
    } else {
	height = 500;
    }
    
    pop_up( myURL,
		 'Help',
		 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1,width=' + width + ',height=' + height + ',resizable=yes');        
}

function redirlink ( dest ){
    var base = document.getElementById('url_base');
    
    return base.value + dest;
} 

function jlink ( dest ){
  var base = document.getElementById('url_base');
   
    return base.value + dest;
}

function remove_hotlist() {
    pop_up( jlink('Quotes/hotlist_delete.epl'), 
		 'HotlistPopup', 
		 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=410,height=440');
}

function choose_ub_memory(){
    sel = document.copage.ubmodel.selectedIndex;
    opt = document.copage.ubmodel.options[sel].value;
    set_redirect( redirlink('Search/ub_memory_search.epl?category=') + opt );
}
      

function set_redirect( myURL ){
    var starting_search_string = document.getElementById('decor_starting_search_string');
    var search_string = document.getElementById('decor_search_string');
    var redirect_url = document.getElementById('decor_redirect_url');
   
    if ( redirect_url ){
        if ( starting_search_string && search_string &&
	     starting_search_string.value != search_string.value ){
	    var change = document.getElementById('decor_search_string_change');
	    if (change){
		change.value = "yes";
	    }
        }
	redirect_url.value = myURL;
        document.copage.submit();

        return true;
    } else {
	return false;
    }     
}

function pop_it_up () {
    var kill_links = document.getElementById('kill_links');
    
    pop_up( jlink('Search/expert_pop_up.epl'), 
		 'CopyPopup',
		 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=770,height=160');
}


function change_active_quote(){
    var qo_link = document.getElementById('qo_link');
    var po_link = document.getElementById('po_link');
    var active_quote = document.getElementById('active_quote');
    var recent_widget = document.getElementById('active_doc');

    if (recent_widget && active_quote && qo_link && po_link){
	var sel_index = recent_widget.selectedIndex;
	var val = recent_widget.options[sel_index].value;
	var val_str = new String(val);
	var quote_id = val_str.match(/^(\-?\d+)\|/);
	var type = val_str.match(/\|(\d+)$/);
	var url = new String;
	active_quote.value =  quote_id[1];
	if( type[1] == 4 ){
	    url = po_link.value + '?id=' + quote_id[1];
	} else {
	    url = qo_link.value + '?quote_id=' + quote_id[1];
	}
	set_redirect(url);
    }
}


function set_anchor_statuses (){
    var anchors = document.getElementsByTagName('a');
	
    for (var i = 0; i < anchors.length; i++){
	if (anchors[i].title){
	    addObjectEvent(anchors[i], 'mouseover', set_status, false);
	    addObjectEvent(anchors[i], 'mouseout', unset_status, false);
	}       
    }    
}

function set_status (e){
    var widget = getEventTarget(e);
    
    var title = widget.title;
    if (title){
	window.status=title;
	window.defaultStatus=title;
    }
    return true;
}

function unset_status (){
    window.status = ' ';
    window.defaultStatus = ' ';
  
    return true;
}

function getEventTarget (e){
    var widget;
    if (e.target){
	// LETs do REAL browsers first
	widget = e.target;
    } else {
	// Now let us do non compliant browsers (MSIE).
	widget = e.srcElement;
    }
    return widget;
}

function change_catalog(link)
{
  cat_drp = document.copage.decor_catalog;
  cat = cat_drp.selectedIndex;
  val = cat_drp.options[cat].value;
  if(val >= 0)
	  set_redirect(link + val);
}


function pop_up_upload(link)
{
  pop_up(link,'upload_file', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,width=470,height=225');
}

function print_text(obj){
	pw = window.open("","_blank","width=760,height=500,menubar=yes");
	pw.document.open("text/plain");
	pw.document.write(obj.value);
	pw.document.close();
	pw.print();
	pw.close();
}

function mouse_click(e)
{
  if(window.event)
   {
     mouse_pos_x = window.event.screenX;
     mouse_pos_y = window.event.screenY;
   }
   else
   {
     mouse_pos_x = e.screenX;
     mouse_pos_y = e.screenY;
   }
}

function pop_calendar(url,elem)
{
  tag = elem + "&date=" + document.getElementById(elem).value;
  pop_up(url + tag,"CopyPopup","alwaysRaised=yes,height=140,width=160,top=" + mouse_pos_y + ",left=" + mouse_pos_x);
}

var mouse_pos_x = 0;
var mouse_pos_y = 0;

function mouse_pos(e)
{
   if(window.event)
   {
     total_height = window.document.body.clientHeight;
     mouse_pos_x = window.event.clientX;
     mouse_pos_y = window.event.clientY;
     mouse_pos_y = ((total_height - mouse_pos_y) > 180) ? mouse_pos_y : mouse_pos_y - 180;
     mouse_pos_x += document.body.scrollLeft;
     mouse_pos_y += document.body.scrollTop;
   }
   else
   {
     total_height = window.innerHeight + window.pageYOffset;
     mouse_pos_x = e.pageX;
     mouse_pos_y = e.pageY;
     mouse_pos_y = ((total_height - mouse_pos_y) > 180) ? mouse_pos_y : mouse_pos_y - 180;
   }
}

//this function pops up the link provided 
//@args = link , target , options
function pop_up(link,target,options)
{
  window.open(link,target,options);
}

// this takes height and width
function pop_up_generic(url,name,height,width)
{
	var param = "scrollbars=yes,alwaysRaised=yes,top=" + mouse_pos_y + ",left=" + mouse_pos_x;
	param += ",height=" + height + ",width=" + width;
  pop_up(url,name,param);
}

function display_categories(classid){
	var category;
	if (category = document.getElementById('d_class_' + classid + '_list')){
	    // turn any that are on off now that we have a new one
	    for (i = 0; i < d_class_list.length; i++){
		clicker = document.getElementById('d_class_'+ d_class_list[i] +'_m');
		cat_list = document.getElementById('d_class_'+ d_class_list[i] + '_list');
		if (clicker){
		    clicker.className = 'd_class_clicker';
		}
		if (cat_list){
		    cat_list.className = 'd_hidden_cat';
		}
	    }

	    selected = document.getElementById('d_class_'+ classid + '_m');
	    if (selected){
		selected.className = 'd_class_clicker_selected';
	    }
	    category.className = 'd_shown_cat';
	}	

}

function set_param_redirect()
{
    var the_form = document.getElementById('copage');
    if (the_form)
    {
        //should only be used for product searching
	if (document.getElementById('dirty').value == 1)
        {
            // dirty bit has been set.   This page has been refreshed
	    // get the parameters that we must ignore
	    var resetVals = document.getElementsByName('reset');
            for (i=0; i < resetVals.length; i++)
            {
	        var key = resetVals[i].value;
	        var init = 'init|' + key;
	        document.getElementById(key).value =
	            document.getElementById(init).value;
            } 
        }
        var quote = 0;
        for (var i = 0; i < arguments.length; i += 2)
        {
            var param = arguments[i];
            var val = arguments[i+1];
            if (param.match(/^quote/) )
            {
                quote = 1;
            }
            document.getElementById(param).value = val;
        }
        if (! quote)
        {
            document.getElementById('quote|item_add').value = "";
        }
        
        the_form.decor_redirect_url.value="";
        the_form.dirty.value = 1;
        the_form.submit();
    }
}

function decor_class_cat_search(field_name,field_val)
{
  var the_form = document.getElementById('copage');
  the_form.decor_redirect_url.value = document.getElementById('d_search_action').value + "?" + field_name + "=" + field_val;
  the_form.submit();
}


function redirect()
{
	document.copage.decor_redirect_url.value="";

	document.copage.submit();
}

function unable_to_add_fav()
{
  alert("Your Favorite Products List is full.  You have reached the 20 product limit.  Please delete products from the Favorites Products List before attempting to add other products to the list.");
}


function change_subsidiary (){
	var url = jlink("Quotes/shopping_cart.epl");	
	var companies = document.getElementById('decor_authorized_companies');
	var cust_id = companies.options[companies.selectedIndex].value;
	url = url + "?Action=change_store&customer_id=" + cust_id;
	set_redirect(url);
}
