// Send request to AJAX file
var http = createRequestObject();
var query = ''

function createRequestObject() 
{
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
    {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function sndReq(action) 
{
    http.open('get', action);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

// Handle response of an AJAX file
function handleResponse() 
{
	if(http.readyState == 4)
	{
		if(http.responseText != '')
		{
			document.getElementById('loader').style.display= 'none';
			document.getElementById('searchresult').innerHTML = http.responseText;
			setWidth();
		}
	}
	else
	{
		document.getElementById('loader').style.display= 'block';
		document.getElementById('mainbox').style.display= 'none';
		document.getElementById('searchresult').innerHTML = '';
	}
}

// Search by combine data of company, color, price, category.
function doSearchAll(combo, page)
{
	var cmb_val = combo.split("+");
/*	for(var c=0; c < cmb_val.length; c++ )
	{
		if(cmb_val[c] == 'undefined') { cmb_val[c] = ''}
	}*/
	combo_val = '';
	if(cmb_val[0] != '') combo_val += '&company='+cmb_val[0];
	if(cmb_val[1] != '') combo_val += '&color='+cmb_val[1];
	if(cmb_val[2] != '') combo_val += '&price='+cmb_val[2];
	if(cmb_val[3] != '') combo_val += '&category='+cmb_val[3];

	var val = 'ajax/search?combo=cmb' + combo_val + '&page='+page;
//	sndReq('http://'+location.hostname+'/'+val);
	return false;
}

// search by search box string
function doSearch(page) 
{
	query = document.getElementById('qst').value;
	query = query.replace(' ','-');
	
	if(query != '')
	{
		var val = 'searchajax/all/keyword/'+ query+'/latest/'+page; 
		sndReq('http://'+location.hostname+'/'+val);
		return false;
	}
}

// search by price
function doSlideSearch(ses,page) 
{
	var val = 'searchajax/all/price/' + ses+'/latest/'+page;
	sndReq('http://'+location.hostname+'/'+val);
	return false;
}

// search by color
function doColorSearch(clr,page) 
{
	var val = 'searchajax/all/color/' + clr+'/latest/'+page;
	sndReq('http://'+location.hostname+'/'+val);
	return false;
}

// search by category
function doCatSearch(cat,page) 
{
	var val = 'searchajax/all/category/'+cat+'/latest/'+page;
	sndReq('http://'+location.hostname+'/'+val);
	return false;
}

// search by company
function doCoSearch(cmp,page) 
{
	var val = 'searchajax/all/company/'+cmp+'/latest/'+page;
	sndReq('http://'+location.hostname+'/'+val);
	return false;
}
// End search function.

function ajaxRes(url)
{
	url = url.replace('href="','')
	url = url.replace('"','')
	sndReq(url);
	return false;
}

function openWindow(url) 
{
  	window.open(url,'popupWindow','resizable=no,scrollbars=yes,toolbar=no,status=no,height=650,width=720');
}

function expandOrClose(arrow_id, list_num, list_name)  
{
	var a = document.getElementById(arrow_id);
	
	if(a.name=='d') 
	{ 
		a.src='arrowRight.gif';
		a.name='r';
		hideSearchTypes(list_num,list_name);
	}
	else 
	{
		a.src='arrowDown.gif';
		a.name='d';
		showSearchTypes(list_num,list_name);
	}
	
	return false;
}

function hideSearchTypes (i,j) 
{ 
	var theDiv = document.getElementById(j).getElementsByTagName('li');
	
	for(var d = 1; d < theDiv.length; d++) 
	{
		var theID = 'res-'+i+'-'+d;
		Effect.DropOut(theID);
	}
}	

function showSearchTypes (i,j) 
{ 
	var theDiv = document.getElementById(j).getElementsByTagName('li');
	
	for(var d = 1; d < theDiv.length; d++) 
	{
		var theID = 'res-'+i+'-'+d;
		Effect.Appear(theID);
	}
}

// start (layout)
function Trim(val)
{
	val = val.replace(new RegExp(/^\s+/),""); // START
	val = val.replace(new RegExp(/\s+$/),""); // END
	return val
}

function searchTee(val)
{
	if(Trim(document.getElementById('qst').value) == "")
  		{
  			return false;
  		} 
  		else 
  		{ 
  			doSearch("0");
  		}
}

function addCommentByKeyPress(e) 
{
     var key;
     
     if(window.event)
     {
     	key = window.event.keyCode; //IE
     }
     else
     {
     	key = e.which; //firefox     
     }

     if(key ==13) 
     {
      	return false;
     }
}

function mainSearch(evt)
{
	var oEvent = (window.event) ? window.event : evt;
   	//  mozilla workthrough
   	var nKeyCode = oEvent.keyCode ? oEvent.keyCode : oEvent.which ? oEvent.which : void 0;
   	//alert(nKeyCode)
  	if(nKeyCode == 13 || nKeyCode == 1)
  	{
  		if(Trim(document.getElementById('qst').value) == "")
  		{
  			return false;
  		} 
  		else 
  		{ 
  			doSearch("0");
  		}
	}
}

function setWidth()
{
	var width = window.innerWidth != null? window.innerWidth :
		document.documentElement && document.documentElement.clientWidth ?
		document.documentElement.clientWidth : document.body != null ?
		document.body.clientWidth : null;

	if(navigator.appName == 'Microsoft Internet Explorer')
	{
		document.getElementById('right').style.width = width-300+"px";
	}
	else
	{
		document.getElementById('right').style.width = width-270+"px";
	}
	return true
}
// end

