var start_time = null;
var timeout = 12;
window.display_color = new Object();
window.display_color['Available'] = '#006600';
window.display_color['Premium'] = '#000000';
window.display_color['Suggested'] = '#000000';
window.display_color['Expiring'] = '#000000';
window.display_color['Deleted'] =  '#000000';
window.display_color['International'] =  '#000000';
window.cart_color = '#245FA5';
window.taken_color = '#ff0000';
window.mix_fix = new Object();
window.mix_fix['cart_check'] = new Object();
window.mix_fix['type'] = new Object();

window.noreply_time = new Date();
var post_search_handler = null;

function handleSearch(status, content, contentType)
{
  var search_result = eval('('+content+')');
  var process_id = search_result['process_id'];
  var wait = 500;
  var max_timeout = search_result['max_timeout'];
  if(start_time === null)
      start_time = new Date();  

  if(search_result['show_available'])
    {
	if(typeof(window.display_avail) != 'undefined')
	    {
		for (var domain in search_result['show_available'])
		    window.display_avail[domain] = search_result['show_available'][domain];
	    }
	else
	    {
		window.display_avail = new Object();
		window.display_avail = search_result['show_available'];
	    }
    }
  if(search_result['full_load'] == 1)
      {
	  if(search_result['suggestion']['done'] != 1)
	      getURL('/javascript/search/search.php?full_load=1&process_id='+ process_id ,handleSearch);
	  else
	      {
		  window.search_result.suggestion = search_result['suggestion'];
		  window.search_result.search.result.mix = search_result['search']['result']['mix'];
		  window.search_result.buy_domains = search_result['buy_domains'];
		  window.search_result.name_store = search_result['name_store'];
		  window.search_result.recently_deleted = search_result['recently_deleted'];
		  window.search_result.expiring_internal = search_result['expiring_internal'];
		  window.search_result.mix = search_result['mix'];
		  
		  if(window.current_tab1 == 'mix')
		      {
			  displayIteration('search',window.current_tab1,0);
			  iterateResults('search',window.current_tab1,'next',0,0);
		      }
		  if( !((window.current_tab2 == 'expiring_internal' || window.current_tab2 == 'recently_deleted' || window.current_tab2 == 'name_store') &&  (search_result[window.current_tab2]['num_domains'] == 0)  ) )
		      {
			  displayIteration('suggestion',window.current_tab2,0);
			  iterateResults('suggestion',window.current_tab2,'next',0,0);
		      }
		  if(typeof(window.current_tab2) == 'undefined')
		      {
			  if(typeof(window.search_result['expiring_internal']['num_domains']) != 'undefined' && window.search_result['expiring_internal']['num_domains'] != 0)
			      switchTab(2,'expiring_internal',0);
			  else if(typeof(window.search_result['recently_deleted']['num_domains']) != 'undefined' && window.search_result['recently_deleted']['num_domains'] != 0)
			      switchTab(2,'recently_deleted',0);
			  else if(typeof(window.search_result['name_store']['num_domains']) != 'undefined' && window.search_result['name_store']['num_domains'] != 0)
			      switchTab(2,'name_store',0);
			  else
			      switchTab(2,'buy_domains',0);
		      }
	      }
	  window.finished_full_load = 1;
	  window.full_load = 1;
	  return 1;
      }
  if(search_result['tlds']['write_bar'] == 1)
      {
	  window.noreply_time = new Date();
	  for (var domain in search_result['tlds']['result'])
	      {
		  if(window.total_price == undefined)
		      {
			  window.total_price = 0;
			  window.total_tld_num = 0;
			  window.total_avail = 0;
			  window.avail_tld = new Object();
		      }
		  if(search_result['tlds']['result'][domain]['avail'] == 'org_idn')
		      {
			  document.getElementById("new_div"+domain).className = 'tld_box';
			  var unavail_tld = domain.substring(domain.indexOf('.')).toUpperCase();
			  var unavail_html = "<a style='font-size:11px;' href='/org-idn-registration'>"+unavail_tld+"<br>IDN<br>Search</a>";
			  replaceText("new_div"+domain,unavail_html);
		      }
		  else if(search_result['tlds']['result'][domain]['avail'] == 1)
		      {
			  document.getElementById("new_div"+domain).className = 'tld_box avail_box';
			  document.getElementById("check_"+domain).disabled = false;
			  window.total_price += parseFloat(search_result['tlds']['result'][domain]['price']);
			  if(search_result['tlds']['result'][domain]['tld'] == 'tel')
			      {
				  window.tel_price_new = (Math.round(parseFloat(search_result['tlds']['result'][domain]['price'])*1111)/10000);
				  window.total_price += window.tel_price_new;
			      }
			  window.total_avail++;
			  window.avail_tld[domain] = 'registration';
		      }
		  else if(search_result['tlds']['result'][domain]['avail'] == 2)
		      {
			  document.getElementById("new_div"+domain).className = 'tld_box avail_box';
			  document.getElementById("check_"+domain).disabled = false;
			  document.getElementById("check_"+domain).checked = true;
			  window.total_price += parseFloat(search_result['tlds']['result'][domain]['price']);
			  if(search_result['tlds']['result'][domain]['tld'] == 'tel')
			      {
				  window.tel_price_new = (Math.round(parseFloat(search_result['tlds']['result'][domain]['price'])*1111)/10000);
				  window.total_price += window.tel_price_new;
			      }
			  window.total_avail++;
			  window.avail_tld[domain] = 'registration';
		      }
		  else if(search_result['tlds']['result'][domain]['avail'] == -1)
		      {
			  document.getElementById("new_div"+domain).className = 'tld_box unavail_box';
			  var unavail_tld = domain.substring(domain.indexOf('.'));
			  var unavail_html = unavail_tld+"<br><span>TRY AGAIN</span>";
			  replaceText("new_div"+domain,unavail_html);
			  
		      }
		  else
		      {
			  document.getElementById("new_div"+domain).className = 'tld_box unavail_box';
			  var unavail_tld = domain.substring(domain.indexOf('.'));
			  var unavail_html = unavail_tld+"<br><span>TAKEN</span>";
			  replaceText("new_div"+domain,unavail_html);
			  
		      }
		  window.total_tld_num++;
		  if(window.total_tld_num == search_result['tlds']['num_total'])
		      {
			  if(window.total_price == 0)
			      replaceText('Total_price',"<span style='color:red'>All Taken</span>");
			  else if(window.total_avail > 2)
			      replaceText('Get_all', "<a class='blue_button' href='javascript:getAll(window.avail_tld)' ><span>Register Them All</span></a><span style='font-size:12px;font-weight:bold' id='Total_price'>for only <span style='text-decoration:line-through'>$"+(Math.round(window.total_price*100)/100)+"</span> <span style='color:#CC0000'>$"+(Math.round(window.total_price*90)/100)+"</span> </p></div><div class='clear'>");
			  else
			      {
				  if(window.tel_price_new)
				      window.total_price =  window.total_price - window.tel_price_new;
				  else
				      window.total_price =  window.total_price;
				  replaceText('Get_all', "<a class='blue_button' href='javascript:getAll(window.avail_tld)'><span>Register Them All</span></a><span style='font-size:12px;font-weight:bold' id='Total_price'>for only $"+(Math.round(window.total_price*100)/100)+"</p></div><div class='clear'>");
			      }

		      }
	      }
      }

  var current_registration = '';
  var current_mix = '';
  var current_buy_domains = '';
  var current_name_store = '';
  var current_expiring = '';
  var current_deleted = '';

  if((search_result['suggestion']['done'] == 1 || search_result['suggestion']['timeout'] == 1) && window.suggestion_written != 1)
      {
      window.search_result = search_result;
	if(search_result['suggestion']['really_done'] == 1)
	      window.suggestion_written = 1;		
      
      if(search_result['registration']['display'] == 1 && search_result['search']['result']['registration'] != undefined)
	  {
	      displayIteration('search','registration',0);
	      iterateResults('search','registration','next',0,0);
	      current_registration = 'current';
	      if(window.tab1_clicked != 1)
		  window.current_tab1 = 'your_search';
	  }
      else if(search_result['mix']['display'] == 1 && 0==1)
	  {
	      displayIteration('search','mix',0);
	      iterateResults('search','mix','next',0,0);
	      current_mix = 'current';
	      if(window.tab1_clicked != 1)
		  window.current_tab1 = 'mix';
	  }
      if(search_result['name_store']['display'] == 1)// && search_result['suggestion']['result']['name_store'] != undefined)
	  {
	      displayIteration('suggestion','name_store',0);
	      iterateResults('suggestion','name_store','next',0,0);
	      current_name_store = 'current';
	      if(window.tab2_clicked != 1)
		  window.current_tab2 = 'name_store';
	  }
      else if(search_result['buy_domains']['display'] == 1 && search_result['suggestion']['result']['buy_domains'] != undefined)
	  {
	      displayIteration('suggestion','buy_domains',0);
	      iterateResults('suggestion','buy_domains','next',0,0);
	     current_buy_domains = 'current';
	     window.current_tab2 = 'buy_domains';
	  }
      else if(search_result['expiring_internal']['display'] == 1 && search_result['suggestion']['result']['expiring_internal'] != undefined)
	  {
	      displayIteration('suggestion','expiring_internal',0);
	      iterateResults('suggestion','expiring_internal','next',0,0);
	      current_expiring = 'current';
	      if(window.tab2_clicked != 1)
		  window.current_tab2 = 'expiring_internal';
	  }
      else if(search_result['recently_deleted']['display'] == 1 && search_result['suggestion']['result']['recently_deleted'] != undefined)
	  {
	      displayIteration('suggestion','recently_deleted',0);
	      iterateResults('suggestion','recently_deleted','next',0,0);
	      current_deleted = 'current';
	      if(window.tab2_clicked != 1)
		  window.current_tab2 = 'recently_deleted';
	  }

      if(window.tab1_clicked != 1)
	  {
	      replaceText('Tab1_your_search', "<li id='"+current_registration+"'><a onClick=\"switchTab('1','your_search','1')\">Your Search</a></li>");	   
	      replaceText('Tab1_mix', "<li id='"+current_mix+"'><a onClick=\"switchTab('1','mix','1')\">More Names</a></li>");	   
	  }
      if(window.tab2_clicked != 1)
	  {
	      replaceText('Tab2_buy_domains', "<li id='"+current_buy_domains+"'><a onClick=\"switchTab('2','buy_domains','1')\">Premium</a></li>");	   
	      replaceText('Tab2_name_store', "<li id='"+current_name_store+"'><a onClick=\"switchTab('2','name_store','1')\">Suggested</a></li>");	   
	      replaceText('Tab2_expiring_internal', "<li id='"+current_expiring+"'><a onClick=\"switchTab('2','expiring_internal','1')\">Expiring</a></li>");	   
	      replaceText('Tab2_recently_deleted', "<li id='"+current_deleted+"'><a onClick=\"switchTab('2','recently_deleted','1')\">International</a></li>");	   
	  }
   }
  
  if(search_result['registration']['display'] == 1 && search_result['search']['result']['registration'] != undefined)
      {
	  if(typeof(window.search_result) != 'undefined' )
	      {
		  window.search_result['search']['result']['registration'] = search_result['search']['result']['registration'];
		  window.search_result['registration']['num_iterations'] = search_result['registration']['num_iterations'];
		  window.search_result['registration']['iteration'] = search_result['registration']['iteration'];
		  window.search_result['registration']['num_domains'] = search_result['registration']['num_domains'];
	      }
	  else
	      {
		  window.search_result = new Object();
		  window.search_result = search_result;
	      }

	  if(window.search_tab_hit != 1)
	      {
		  displayIteration('search','registration',0);
		  iterateResults('search','registration','next',0,0);
		  current_registration = 'current';
		  window.current_tab1 = 'your_search';
	      	  
		  replaceText('Tab1_your_search', "<li id='"+current_registration+"'><a onClick=\"switchTab('1','your_search','1')\">Your Search</a></li>");	   
		  replaceText('Tab1_mix', "<li id='"+current_mix+"'><a onClick=\"switchTab('1','mix','1')\">More Names</a></li>");	   
	      }
	  if(window.current_tab2)
	      switchTab(2, window.current_tab2,0);
      }

  var current_time_disp = new Date();
  var elapsed_time_disp = ((current_time_disp.getTime() / 1000) - (start_time.getTime() / 1000));
  if(elapsed_time_disp > 1 || (search_result['suggestion']['done'] == 1 && search_result['tlds']['done'] == 1 && window.finished_full_load == 1 && window.show_available != 1))
      displayAvail();

  if(search_result['suggestion']['really_done'] == 1 && search_result['tlds']['done'] == 1 && window.full_load !=1)
      {
	  getURL('/javascript/search/search.php?full_load=1&process_id='+ process_id ,handleSearch);
	  window.full_load =1;
      }
  if(search_result['suggestion']['done'] == 1 && search_result['tlds']['done'] == 1 && search_result['additional_tlds']['done'] == 1 && window.full_load == 1 && window.finished_full_load == 1)
      {
	  if(typeof(post_search_handler) == 'function')
	      post_search_handler();
	  if(typeof(window.current_tab1) == 'undefined')
	      switchTab(1,'mix',0);
	  
	  if(typeof(window.current_tab2) == 'undefined')
	      {
		  if(typeof(window.search_result['expiring_internal']['num_domains']) != 'undefined' && window.search_result['expiring_internal']['num_domains'] != 0)
		      switchTab(2,'expiring_internal',0);
		  else if(typeof(window.search_result['recently_deleted']['num_domains']) != 'undefined' && window.search_result['recently_deleted']['num_domains'] != 0)
		      switchTab(2,'recently_deleted',0);
		  else
		      switchTab(2,'buy_domains',0);
	      }
	  return 1;
      }

  else
    {
      var current_time = new Date();
      var elapsed_time = ((current_time.getTime() / 1000) - (start_time.getTime() / 1000));
      var elapsed_noreply_time = ((current_time.getTime() / 1000) - (window.noreply_time.getTime() / 1000));
  
      if(elapsed_time > 5)
	wait = 750;
      if(elapsed_time > 10)
	wait = 1000;
      if(elapsed_time > 20)
	wait = 2000;
      if(elapsed_noreply_time > 15)
	  {
	      $(".tld_search_box").addClass('unavail_box').removeClass('tld_search_box').each(function(i){
		      $(this).html("."+ $(this).attr("title")+"<br/> <span>TRY AGAIN</span>");
		  });
	      if(typeof(window.current_tab1) == 'undefined')
		  switchTab(1,'mix',0);
	      if(window.full_load != 1 && typeof(window.current_tab2) == 'undefined')
		  getURL('/javascript/search/search.php?full_load=1&process_id='+ process_id ,handleSearch);
	      displayAvail();
	      return 1;
	  }
    }
  setTimeout("getURL('/javascript/search/search.php?light_loaded="+window.suggestion_written+"&process_id="+ process_id +"',handleSearch);", wait);
}
function selectToggle(form, name, select_type) {
    for (i = 0; i < form.elements.length;i++) {
        if (form.elements[i].name.indexOf(name) != -1){

	  if(select_type == 'select'){
		if(form.elements[i].checked == false && form.elements[i].disabled == false){
		    form.elements[i].checked = true;
		    if(form.elements[i].onclick)
			form.elements[i].onclick('selectall');
		}
	    }
	    else{
		if(form.elements[i].checked == true){
		    form.elements[i].checked = false;
		    if(form.elements[i].onclick)
			form.elements[i].onclick('unselectall');
		}
	    }
        }
    }
}
function renameSelect(id, select_type, form)
{
  if(select_type == 'unselect')
    {
      var html = "<a onClick='selectToggle(window.document."+form+"_names, \""+form+"_domains[]\",\"unselect\");renameSelect(\"Select_"+form+"\",\"select\",\""+form+"\")'> Unselect All </a>";
      replaceText(id,html);
    }
  else
    {
      var html = "<a onClick='selectToggle(window.document."+form+"_names, \""+form+"_domains[]\",\"select\");renameSelect(\"Select_"+form+"\",\"unselect\",\""+form+"\")'> Select All </a>";
      replaceText(id,html);
    }
}

function displayIteration(id,display_type,direction,from_tab)
{
  if((id == 'suggestion' && window.tab2_clicked != 1) || (id == 'search' && window.tab1_clicked != 1) || from_tab == 1) 
  {
    if( typeof(window.search_result[id]['result']) == 'undefined' )
	{
	    window.search_result[id]['result'] = new Object();
	    window.search_result[id]['result'] = '';
	}
    if(typeof(window.search_result[display_type]) == 'undefined')
      return;

  var search_result = window.search_result;
  var search_html = '';
  var iteration_start = search_result[display_type]['iteration']*search_result['number_results'];
  var number_results = search_result['number_results'];
  var count = 0;
  var iteration_count = 0;
  var result_type = '';
  
  if(typeof(search_result[id]['result'][display_type]) != 'undefined')
  {
    for (var domain in search_result[id]['result'][display_type])
    {
	price = search_result[id]['result'][display_type][domain]['price'];
	search_type = search_result[id]['result'][display_type][domain]['search_type'];
	product_type = convertProductType(search_type);
	cart_check = search_result[id]['result'][display_type][domain]['cart_check'];
	domain_display = domain;
	tag = search_result[id]['result'][display_type][domain]['tag'];
	result_type = ((cart_check == 'unchecked')?search_result[id]['result'][display_type][domain]['display_type']:'Added to Cart');
	domain_display = search_result[id]['result'][display_type][domain]['display'];
	duration = search_result[id]['result'][display_type][domain]['duration'];
	domain_value = search_result[id]['result'][display_type][domain]['domain_value'];
	if(display_type == 'mix')
	    {
		window.mix_fix['cart_check'][domain_value] = cart_check;
		window.mix_fix['type'][domain_value] = result_type;
	    }

      type_color = ((cart_check == 'unchecked')?window.display_color[result_type]:window.cart_color);
      
      if(result_type == 'Premium')
	  result_entry = "<div class='results_premium' id='cart_"+domain_value+"' style='color:"+type_color+"'><a class='premium-hover' href='javascript:void(0);' title='Questions About a Premium Domain? Please Call: 720-249-2374'><img src='/images/call_mini.gif' /></a>"+result_type+"</div>";
      else
	  result_entry = "<div class='results_type' id='cart_"+domain_value+"' style='color:"+type_color+"' >"+result_type+"</div>";

      if(iteration_count >= iteration_start && count < number_results) 
	  {
	  class_type = ((count%2 == 0)?'results_odd':'results_even');
	  search_html = search_html +"<div class='"+class_type+"' id='click_"+domain_value+"' ><div class='results_checkbox'><input id='new_check_"+domain_value+"' name='"+id+"_domains[]' value='"+domain_value+"' type='checkbox' onFocus=\"this.hasFocus=1;\" onClick=\"barClick('"+id+"','"+domain_value+"','"+search_type+"','true','"+tag+"');\" onBlur=\"this.hasFocus=0;\"  "+cart_check+"></div> <span style='cursor:pointer;cursor:hand;' onClick=\"barClick('"+id+"','"+domain_value+"','"+search_type+"','false','"+tag+"');\"> <div class='results_entry'>"+domain_display+"</div>"+result_entry+"<div class='results_price'>$"+price+"</div></span></div>";
	  count++;
	}
      iteration_count++;
    }
    while(count < number_results)
      {
      class_type = ((count%2 == 0)?'results_odd':'results_even');
      search_html = search_html +"<div class='"+class_type+"'><div class='results_checkbox'>&nbsp;</div><div class='results_entry'>&nbsp</div><div class='results_price'>&nbsp;</div></div>";
      count++;
      }
    $("#"+search_result[id]['html_id']).html(search_html);	   
    if(id == 'search')
	{
	    $("#Your_search .premium-hover").tooltip({delay: 0, showURL: false, positionLeft: true, left: -10, top: -10, extraClass: "premium"});
	    $("#Your_search .info-hover").tooltip({delay: 0, showURL: false, positionRight: true, left: -10, top: -10, extraClass: "premium"});
	}
    else
	{
	    $("#Suggestion_results .premium-hover").tooltip({delay: 0, showURL: false, positionLeft: true, left: -10, top: -10, extraClass: "premium"});
	    $("#Suggestion_results .info-hover").tooltip({delay: 0, showURL: false, positionRight: true, left: 20, top: -10, extraClass: "premium"});
	}

  }
  else
      {
	  search_html = '';
	  while(count < number_results)
	  {
	  class_type = ((count%2 == 0)?'results_odd':'results_even');
	  search_html = search_html +"<div class='"+class_type+"'><div class='results_checkbox'>&nbsp;</div><div class='results_entry'>&nbsp</div><div class='results_price'>&nbsp;</div></div>";
	  count++;
	  }  
	  replaceText(search_result[id]['html_id'], search_html);	   
      }
  }
}
function barClick(id, click_domain, display_type, is_check_box, tag)
{
    var search_result = window.search_result;
    if(display_type == 'sedo_domains')
	{
	    product_type = display_type;	
	    duration = 1;
	}
    else
	{
	    if(id == 'search' && display_type != 'registration')
		{
		    search_type = 'mix';
		    duration = 1;
		    product_type = display_type;
		    if(display_type == 'name_store' || display_type == 'recently_deleted' || display_type == 'wrangled')
			product_type = 'registration';
		}
	    else
		{
		    search_type = search_result[id]['result'][display_type][click_domain]['search_type'];
		    duration = search_result[id]['result'][display_type][click_domain]['duration'];
		    product_type = convertProductType(search_type);
		}

    	}
    bar_e = document.getElementById("new_check_"+click_domain);
    if(is_check_box == 'false')
	{     
	    if(bar_e.checked == true)
		bar_e.checked = false;
	    else
		bar_e.checked = true;
	}

    productCartSwap(bar_e.checked, click_domain, duration, product_type, tag, null, productCartSwapSimpleHandler);
    checkCart(id, search_type, click_domain); 
    //cartFade('click_'+click_domain);
}
function cartFade(id)
{
    //$("#shopping_cart_info").css({backgroundColor:"yellow",});
    //$("#shopping_cart_info").animate({backgroundColor:"white"} ,1000);
    //div_id = "#"+id.replace(".","\\.");
    //$(div_id).css({backgroundColor:"yellow"});
    //$(div_id).animate({backgroundColor:"#EEF5F8"} ,1000);
}
function checkCart(id, type_check, domain)
{
    if(id == 'search' && type_check != 'registration')
	type_check = 'mix';
    if(id == 'suggestion' && type_check == 'sedo_domains')
	type_check = 'buy_domains';

    if(type_check == 'mix')
	cart_check = window.mix_fix['cart_check'][domain];
    else
	cart_check = window.search_result[id]['result'][type_check][domain]['cart_check'];
    if(cart_check == 'checked')
	{
	    cart_check = 'unchecked';
	    if(type_check == 'mix')
		result_type = window.mix_fix['type'][domain];
	    else	    
		result_type = convertSearchType(type_check);

	    if(result_type == 'Premium')
		{
		    document.getElementById("cart_" + domain).className = 'results_premium';
		    result_entry = "<a class='premium-hover' href='javascript:void(0);' title='Questions About a Premium Domain? Please Call: 720-249-2374'><img src='/images/call_mini.gif' /></a>"+result_type;
		}
	    else
		result_entry = result_type;

	    replaceText("cart_" + domain, result_entry);
	    document.getElementById("cart_" + domain).style.color = (result_entry == 'Available')?'#006600':'#000000'
	    if(type_check != 'mix')
		window.search_result[id]['result'][type_check][domain]['cart_check'] = cart_check;
	    else
		window.mix_fix['cart_check'][domain] = cart_check;
	    return 1;
	}
    if(cart_check == 'unchecked')
	{
	    cart_check = 'checked';
	    replaceText("cart_" + domain, "Added to Cart");
	    document.getElementById("cart_" + domain).style.color = window.cart_color;	
	    if(type_check != 'mix')
		window.search_result[id]['result'][type_check][domain]['cart_check'] = cart_check;
	    else
		window.mix_fix['cart_check'][domain] = cart_check;
	    return 1;
	}
}
function iterateResults(id,type_iterate,direction,iterate,from_tab)
{
  if(typeof(window.search_result[type_iterate]) == 'undefined')
      return;
  var num_iterations = window.search_result[type_iterate]['num_iterations'];
  var iteration = window.search_result[type_iterate]['iteration'];
  var num_results = window.search_result['number_results'];

  if(type_iterate == 'buy_domains' && id=='suggestion')
      replaceText("Suggestion_domain", "Domain: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp info? call: 720-249-2374");  
  else if(id =='suggestion')
      replaceText("Suggestion_domain", "Domain:");
  if(iterate == 1)
    {
      if(direction == 'next' && iteration < num_iterations)
	window.search_result[type_iterate]['iteration']++;
      else if(direction == 'previous' && iteration > 0)
	window.search_result[type_iterate]['iteration']--;
      iteration = window.search_result[type_iterate]['iteration'];
      if(id == 'search')
	  window.search_tab_hit = 1;

    }
  var beg = iteration*num_results+1;
  var end = iteration*num_results+num_results;
  if( num_iterations == iteration)
      end = search_result[type_iterate]['num_domains'];

  var next_text = "'cursor:none'>&nbsp";
  var previous_text = "'cursor:none'>&nbsp";
  var number_text = "<div class='search_number'>"+beg+"-"+end+" of "+search_result[type_iterate]['num_domains']+"</div>";
  if(iteration == 0)
    next_text = "''>Next &gt;&gt;";
  else if(iteration == num_iterations)
    previous_text = "''>&lt;&lt; Back";
  else
    {
      next_text = "''>Next &gt;&gt;";
      previous_text = "''>&lt;&lt; Back";
    }
  
  var next_html = "<div class='search_next'><a onClick='iterateResults(\""+id+"\",\""+type_iterate+"\",\"next\",1,1)' style="+next_text+" </a></div>";
  var previous_html = "<div class='search_back''><a onClick='iterateResults(\""+id+"\",\""+type_iterate+"\",\"previous\",1,1)' style="+previous_text+" </a></div>";
  replaceText(id+"_iterator", previous_html+number_text+next_html);

  if(id == 'suggestion')
      replaceText("suggestion_footer", "<a href='"+ConvertSearchHref(type_iterate)+"'>&lt;&lt; See more like this</a>");  
  displayIteration(id,type_iterate,0,from_tab);
  //renameSelect("Select_"+id,"select",id,from_tab);
}
function switchTab(id,type,tab_clicked)
{
    if(id == '1')
	{
	    if(tab_clicked == 1)	
	       window.tab1_clicked = 1;
	    cur_tab = window.current_tab1;
	    window.current_tab1 = type;
	    tab_id = 'Tab1_';
	    type_id = 'search';
	    window.search_tab_hit = 1;
	    do_tab = (tab_clicked == 1 || (tab_clicked != 1 && window.tab1_clicked != 1))?1:0;
	}
    else if(id =='2')
	{
	    if(tab_clicked == 1)	
	       window.tab2_clicked = 1;
	    cur_tab = window.current_tab2;
	    window.current_tab2 = type;
	    tab_id = 'Tab2_';
	    type_id = 'suggestion';
	    do_tab = (tab_clicked == 1 || (tab_clicked != 1 && window.tab2_clicked != 1))?1:0;	    
	}
  if(do_tab)
  { 	
    var untab_html = "<li><a  onClick=\"switchTab('"+id+"','"+cur_tab+"','1')\">"+convertSearchType(cur_tab)+"</a></li>";
    var tab_html = "<li id='current'><a onClick=\"switchTab('"+id+"','"+type+"','1')\">"+convertSearchType(type)+"</a></li>";

    replaceText(tab_id+cur_tab, untab_html);  
    replaceText(tab_id+type, tab_html);  
    
    if(type == 'your_search')
	type='registration';
    
    if(window.search_result[type]['num_domains'] == 0 && window.full_load == 1)
	{
	    tune_html = '';
	    search_html = '';
	    if(type == 'expiring_internal')
		tune_html = "<div class='results_even' style='height:57px;text-align:center'><div style='width:100%;margin:10px'> No expiring domain names match your search.<br/><br/> Please <a href='/expired_domains.php' style='color:blue'>click here</a> to fine tune your search for better results </div></div>";
	    else if(type == 'recently_deleted')
		tune_html = "<div class='results_even' style='height:57px;text-align:center'><div style='width:100%;margin:10px'> No international domain names match your search.<br/><br/> Please <a href='/domain-suggestions/' style='color:blue'>click here</a> to fine tune your search for better results </div></div>";
	    else if(type == 'name_store_disable')
		tune_html = "<div class='results_even' style='height:57px;text-align:center'><div style='width:100%;margin:10px'> No suggested domain names match your search.<br/><br/> Please <a href='/domain-suggestions' style='color:blue'>click here</a> to fine tune your search for better results </div></div>";
	    else if(type == 'buy_domains')
		tune_html = "<div class='results_even' style='height:57px;text-align:center'><div style='width:100%;margin:10px'> No premium domain names match your search.<br/><br/> Please <a href='/premium_domains.php' style='color:blue'>click here</a> to fine tune your search for better results </div></div>";
	    
	    if(tune_html == '')
		iterateResults(type_id,type,'next',0,tab_clicked);
	    else
		{
		    count = 0;
		    while(count < (window.search_result['number_results']/2 -3))
			{
			    search_html = search_html +"<div class='results_even' style='height:19px'>&nbsp;</div>";
			    count++;
			}
		    search_html += tune_html;
		    while(count < (window.search_result['number_results']-3))
			{
			    search_html = search_html +"<div class='results_even' style='height:19px'>&nbsp;</div>";
			    count++;
			}
		    replaceText(search_result['suggestion']['html_id'], search_html);
		}
	}
    else
	{
	    //if(tab_clicked == 1 && id == 2)
	    //window.current_tab2 = type;
	    iterateResults(type_id,type,'next',0,tab_clicked);
	}
  }
	
}

function convertSearchType(search_type)
{
  var result_type = '';
  if(search_type =='buy_domains')
    result_type = 'Premium';
  if(search_type =='sedo_domains')
    result_type = 'Premium';
  if(search_type =='expiring_internal')
    result_type = 'Expiring';
  if(search_type =='name_store')
    result_type = 'Suggested';
  if(search_type =='recently_deleted')
    result_type = 'International';
  if(search_type =='mix')
    result_type = 'More Names';
  if(search_type =='registration')
    result_type = 'Available';
 if(search_type =='your_search')
    result_type = 'Your Search';
  return result_type;
}
function convertProductType(product_type)
{
  var result_type = '';
  if(product_type =='buy_domains')
    result_type = 'buy_domains';
  if(product_type =='sedo_domains')
    result_type = 'sedo_domains';
  if(product_type =='expiring_internal')
    result_type = 'expiring_internal';
  if(product_type =='name_store')
    result_type = 'registration';
  if(product_type =='recently_deleted')
    result_type = 'registration';
  if(product_type =='registration')
    result_type = 'registration';
  return result_type;
}
function ConvertSearchHref(search_type)
{
  var href_type = '';
  var search_string = window.search_result['search_string'];
  if(search_type =='buy_domains')
      href_type = '/premium_domains.php?maxresults=200&domain='+search_string;
  if(search_type =='expiring_internal')
      href_type = '/expired_domains.php?count=250&domain='+search_string;
  if(search_type =='name_store')
      href_type = '/domain-suggestions/#'+search_string;
  if(search_type =='recently_deleted')
      href_type = '/domain-suggestions/#'+search_string;
  if(search_type =='registration')
      href_type = '/asdf';
  return href_type;   
}
function displayAvail()
{
    if(window.display_avail)
	{
	    for (var domain in window.display_avail)
		{
		    if(window.set_avail != 1 && window.set_special != 1)
			{
			    window.search_result['display_available'] = new Object();
			    window.search_result['display_available'][domain] = window.display_avail[domain]; 
			    window.set_avail = 1;
			    avail_domain = domain;
			}
		    if(window.display_avail[domain]['search_type'] == 'buy_domains' || window.display_avail[domain]['search_type'] == 'expiring_internal')
			{
			    window.search_result['display_available'] = new Object();
			    window.search_result['display_available'][domain] = window.display_avail[domain]; 
			    avail_domain = domain;
			    window.set_special = 1;
			}
		    if(window.display_avail[domain]['exact_match'] == 1)
			{
			    window.search_result['display_available'] = new Object();
			    window.search_result['display_available'][domain] = window.display_avail[domain]; 
			    avail_domain = domain;
			    break;
			}
		}
	    for (var domain in window.search_result['display_available'])
		writeAvailableDomain(avail_domain, window.search_result);
	    window.show_available = 1;
	}
}
function writeAvailableDomain(domain, search_result)
{
if(window.write_avail != 1)
  {
    var domain_display = search_result['display_available'][domain]['domain_display'];  
    search_type = convertSearchType(search_result['display_available'][domain]['search_type']);
    var break_html = '';
    domain_html = "<a class='blue_button' onClick=\"buyNow('"+domain+"','"+search_result['display_available'][domain]['search_type']+"');\"><span>Buy Now!</span></a><div class='info'><b>"+domain_display+"</b><span style='color:black;line-height:15px'>"+break_html+" is available for <span style='color:#CC0000;font-weight:bold'>$" + search_result['display_available'][domain]['price'] + "</span> &nbsp; </div>";
    info_html = '';
    if(search_type == 'Premium')
    info_html += "Call 720-249-2374 for information regarding Premium Domains.";
    replaceText('Avail_domain', domain_html);	   
    replaceText('Avail_info', info_html);
    search_type = search_result['display_available'][domain]['search_type'];
    var duration = search_result['display_available'][domain]['duration'];

    write_price = search_result['display_available'][domain]['price']
    domain = domain.toLowerCase();
    
    if(search_type == 'name_store' || search_type == 'recently_deleted' || search_type == 'tlds' || search_type == 'additional_tlds' || search_type == 'wrangled' || search_type == 'international' || search_type == 'v_sug' || search_type == 'adword')
	{
	    search_type_fix = 'registration';
	    search_code = 'pb-ys2v';
	}
    else
	{
	    search_type_fix = search_type;
	    search_code = 'pb-ys2v';
	}
    document.getElementById("new_div"+domain).id = "new_div_"+domain;
    document.getElementById("new_div_"+domain).className = 'tld_box avail_box';
    write_tld = domain.substring(domain.indexOf("."));
    if(write_tld == '.tel')
	search_code = 'pa-ys2v';
    replaceText("new_div_"+domain,"<div><input value='"+domain+"' type='checkbox' name='basic_domains[]' id='check_"+domain+"' onClick=\"productCartSwap(this.checked, '"+domain+"',"+duration+", '"+search_type_fix+"','"+search_code+"',null,productCartSwapSimpleHandler)\";>"+write_tld+"<br/><div><span>$"+write_price+"</span></div></div></div>");
    
    if(window.avail_tld)
	window.avail_tld[domain] = search_type;
    else
	
    replaceText("new_span"+domain,"$"+write_price);
    if(search_type == 'buy_domains' || search_type == 'expiring_internal')
	{
	    if(window.total_avail > 2)
		replaceText('Get_all', "<a class='blue_button' href='javascript:getAll(window.avail_tld)'><span>Register Them All</span></a><br/><span style='font-size:12px;font-weight:bold' id='Total_price'>for only <span style='text-decoration:line-through'>$"+(Math.round( (window.total_price+parseFloat(write_price))*100 )/100)+"</span> <span style='color:#CC0000'>$"+(Math.round(window.total_price*90+parseFloat(write_price)*100)/100)+"</span> </p></div><div class='clear'>");
	    else
		replaceText('Get_all', "<a class='blue_button' href='javascript:getAll(window.avail_tld)'><span>Register Them All</span></a><br/><span style='font-size:12px;font-weight:bold' id='Total_price'>for only $"+(Math.round( (window.total_price + parseFloat(write_price))*100 )/100)+"</p></div><div class='clear'>");
	}
    window.write_avail = 1;
  }
}
function buyNow(domain, search_type)
{
    if(search_type == 'name_store' || search_type == 'recently_deleted' || search_type == 'tlds' || search_type == 'additional_tlds' )
	search_type = 'registration';
    productCartSwap('checked', domain, 1, search_type, '','',productCartSwapSimpleHandler,1);
    window.location="/checkout.php";
}
function insertAvailableDomain(domain,search_type,price)
{
    window.search_result['show_available'][domain]['search_type'] = search_type;
    window.search_result['show_available'][domain]['price'] = price;
}
function dropBulkSearch()
{
    document.getElementById("Search_area").style.height = '140px';
    document.getElementById("Search_button").style.left = '124px';
    replaceText('Search_box',"<textarea name='domain' rows='5'></textarea>");
}
function dropKeywordSearch()
{
    document.getElementById("Search_area").style.height= '37px';
    document.getElementById("Search_button").style.left = '20px';
    replaceText('Search_box',"<input type='text' name='domain'>");
}

window.noreply_time = new Date();
function handleBulkSearch(status,content,contentType)
{
    search_code = 0;
    var wait = 500;
    var results = content.split(";");

    for ( var i in results )
    {
	if(results[i] == 'done')
	{
	    if(typeof(post_search_handler) == 'function')
		post_search_handler();
	    $("#Bulk_results").tablesorter(); 
	    return 0;
	}

	result = results[i].split(",");

	if(result[0] == 'code')
	    search_code = result[1];
	else
	{
	    if(results[i] != 'done')
		window.noreply_time = new Date();

	    if(result[1] == 1)
		{
		    if(document.getElementById("div_"+result[0]).innerHTML != "Added to Cart")
			{
			    replaceText("div_" + result[0], "Available");
			    document.getElementById("div_" + result[0]).style.color = '#006600';
			    if(document.getElementById('div_fast_' + result[0]) != null) document.getElementById('div_fast_' + result[0]).style.display = 'inline';
			}

		document.getElementById('check_' + result[0]).disabled = false;
		}
	    else if(result[1] == 2)
	    {
		replaceText("div_" + result[0], "Added to Cart");
		document.getElementById("div_" + result[0]).style.color = window.cart_color;
		document.getElementById('check_' + result[0]).disabled = false;
		document.getElementById('check_' + result[0]).checked = true;
	    }
	    else if (result[1] == -1)
	    {
		replaceText("div_" + result[0], "Try Again");
		if(document.getElementById("div_" + result[0])) document.getElementById("div_" + result[0]).style.color = window.taken_color;
	    }
	    else
	    {
		replaceText("div_" + result[0], "Taken");
		if(document.getElementById("div_" + result[0])) document.getElementById("div_" + result[0]).style.color = window.taken_color;
	    }
	}
    }

    if(start_time === null)
	start_time = new Date();
    else
    {
	var current_time = new Date();
	var elapsed_time = ((current_time.getTime() / 1000) - (start_time.getTime() / 1000));
	var elapsed_noreply_time = ((current_time.getTime() / 1000) - (window.noreply_time.getTime() / 1000));

	if(elapsed_time > 3)
	    wait = 750;
	if(elapsed_time > 10)
	    wait = 1000;
	if(elapsed_time > 20)
	    wait = 2000;

	if(elapsed_noreply_time > 15)
	    {
		$("#Bulk_results").tablesorter(); 
		return(1);
	    }
    }
    setTimeout("getURL('/javascript/search/bulk_search.php?get_results=' + search_code,handleBulkSearch);", wait);
}

function getAll(tld_arr)
{
    //document.getElementById("LoadingDiv").id = 'LoadingDivTemp';   
    document.getElementById("LoadingDiv").style.display = 'block';
    for(domain in tld_arr)
	{
	    get_type = tld_arr[domain];
	    if(get_type == 'name_store' || get_type == 'recently_deleted' || get_type == 'tlds' || get_type == 'additional_tlds')
		get_type = 'registration';
	    write_tld = domain.substring(domain.indexOf("."));
	    if(write_tld == '.tel')
		productCartSwap('checked', domain, 3, get_type, 'pa-ys2v','',productCartSwapSimpleHandler,1);
	    else if(get_type == 'registration')
		productCartSwap('checked', domain, 1, get_type, 'pb-ys2v','',productCartSwapSimpleHandler,1);
	    else
		productCartSwap('checked', domain, 1, get_type, 'pa-ys2v','',productCartSwapSimpleHandler,1);
		
	}
    window.location = "/checkout.php";
}
