


function getAjaxResponseVal(resp)
{
	var doc = resp.responseXML.documentElement;
	if(doc.hasChildNodes()) {
		return doc.childNodes[0].firstChild.data;
	}
	return '';
}



function updateProductDisplay(resetPage)
{
	if(resetPage == true){
		$('page_num').value = '';
	}
	var search_parameter_string;
	if (updateProductDisplay.arguments.length == 2) {
		search_parameter_string = updateProductDisplay.arguments[1];
	
		
	}
	else{
		search_parameter_string = Form.serialize($("productSearchForm"));
		
	}
	var current_page = $('current_page').innerHTML;
	
	$('productDisplayArea').innerHTML = '<div style="text-align: center; height: 100px; line-height: 170%; font-weight: bold; color: #ccc; font-size: 18px;"><br />Searching...<br /><img src="/pics/ajax-load-bar.gif" /></div>';
	
	//$('topResults').innerHTML = 'Searching...';
//	$('diamond_name_h1').innerHTML = '';

	var timestamp = $('timestamp').value;
	timestamp++;
	$('timestamp').value = timestamp;
			
	new Ajax.Request("/diamond_cat_info.ajax.php", {

		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);

			if($('timestamp').value == timestamp){
				$('productDisplayArea').innerHTML = result;
				//$('topResults').innerHTML = $('results2').innerHTML;
				var url = escape(current_page+"#"+Form.serialize($("productSearchForm")));
				var unescaped_url = current_page+"#"+Form.serialize($("productSearchForm"));
				
				
				$('currency_links').innerHTML = '<dt>Currency</dt><dd class="c"><a href="/currency.php?form_submitted=1&to=CAD&returnUrl='+url+'" rel="nofollow" title="Canadian" title=""">Canadian $</a></dd><dd class="a"><a href="/currency.php?form_submitted=1&to=AUD&returnUrl='+url+'" rel="nofollow" title="Australian">Australian $</a></dd><dd><a href="/currency.php?form_submitted=1&to=USD&returnUrl='+url+'" rel="nofollow" title="USD">$</a></dd><dd><a href="/currency.php?form_submitted=1&to=EUR&returnUrl='+url+'" rel="nofollow" title="Euro">&#8364;</a></dd><dd><a href="/currency.php?form_submitted=1&to=GBP&returnUrl='+url+'" rel="nofollow" title="GB Pound">&#163;</a></dd>';
			
				//alert( $('shape_name_results').innerHTML);
				
				$('diamond_name_h1').innerHTML = $('shape_name_results').innerHTML;
				
				$('shape_content_id').innerHTML = $('shape_desc_results').innerHTML;
				//alert('search done....');
				window.location = '#'+ Form.serialize($("productSearchForm"));
			    setDisplay('hide_show_searches','');
	    	    setDisplay('hide_show_loading','none');
	    	    
	    	    var title = "Loose ";
	    	    var diamond_shape = "";
	    	    var selected_shapes = $$(".search_slide_panel_list .selected_shape img");
	    	    if(selected_shapes.length < 5 && selected_shapes.length > 0){
		    	    selected_shapes.each(function(s){
		    	    	diamond_shape += s.alt+"/"
		    	    });
	    	    }else{
	    	    	diamond_shape = "Assorted,"
	    	    }
	    	    title += diamond_shape.substring(0,diamond_shape.length-1)+" Cut diamonds: ";
	    	    
	    	    
	    	    
	    	    var carat_low = $("search_ca1").value;
	    	    var carat_high = $("search_ca2").value;
	    	    
	    	    if(carat_low == carat_high){
	    	    	title += carat_low + " carat, "
	    	    }else{
	    	    	title += (carat_low*1) + " to " + (carat_high*1) + " carat, "
	    	    }
	    	    
	    	    var clarity_low = $("search_cl1").value;
	    	    var clarity_high = $("search_cl2").value;
	    	    
	    	    if(clarity_low == clarity_high){
	    	    	title += slider_clarity_labels[clarity_low] + " clarity, "
	    	    }else{
	    	    	title += slider_clarity_labels[clarity_low] + " to " + slider_clarity_labels[clarity_high] + " clarity, "
	    	    }
	    	    
	    	    var color_low = $("search_co1").value;
	    	    var color_high = $("search_co2").value;
	    	    
	    	    if(color_low == color_high){
	    	    	title += slider_color_labels[color_low] + " color, "
	    	    }else{
	    	    	title += slider_color_labels[color_low] + " to " + slider_color_labels[color_high] + " color, "
	    	    }
	    	    
	    	    var price_low = $("search_pr1").value;
	    	    var price_high = $("search_pr2").value;
	    	    
	    	    if(price_low == price_high){
	    	    	title += "at $"+ price_low;
	    	    }else{
	    	    	title += "between $"+ price_low + ".00 and $" + price_high + ".00 criteria from Excel Diamonds as Excel Diamonds";
	    	    }
	    	    
	    	    document.title = title;
	    	    
			}
		},
		
		onFailure : function(resp) { },

		parameters : search_parameter_string

	});


}

function updateProductCount()
{	
	return;
	new Ajax.Request("/diamond_cat_info.ajax.php", {
	
		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);
			//$('topResults').innerHTML = result;
			var end = new Date();  			
		},

		onFailure : function(resp) { },

		parameters : Form.serialize($("productSearchForm")) + '&view=product_ct'

	});

}


function showAddADiamond(id)
{
	new Ajax.Request("/diamond_cat_info.ajax.php", {

		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);
			$('resultstwo').innerHTML = result;
		},

		onFailure : function(resp) { },

		parameters : 'view=product&product_id='+id

	});
}


function updateSort(sort_by, sort_asc)
{
	$('sort_by').value = sort_by;
	$('sort_asc').value = sort_asc;

	updateProductDisplay();

}


function updateColumns(show_columns)
{
	$('show_extra_columns').value = show_columns;
	updateProductDisplay();

}

function addCommas2Num(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function addRemoveToCompare(val)
{
	var add_remove = '';
	if($("checkbox_diamond_"+val).checked)
		add_remove = 'add';
	else
		add_remove = 'remove';	
	new Ajax.Request("/compare.ajax.php", {

	onSuccess : function(resp) {
		
	},

	onFailure : function(resp) { },

	parameters : 'add_remove='+add_remove+'&diamond_id='+val});
	
}


