var http;// = getHTTPObject();
var iSubCateId;
function handleHttpResponse_subCat() 
{ 
	document.getElementById("subcat").length = 1;
	if (http.readyState == 4) 
	{	
      	isWorking = false;
		//alert(http.responseText);
    	if (http.responseText.indexOf('invalid') == -1) 
		{
			var xmlDocument = http.responseXML;
			var no = xmlDocument.getElementsByTagName('tot').item(0).firstChild.data;
			if(parseInt(no) == 0)
			{
				//var iCatId = xmlDocument.getElementsByTagName('icategoryid').item(0).firstChild.data;
				var iCatId = -1;
				var vCatName =xmlDocument.getElementsByTagName('catname').item(0).firstChild.data;
					document.getElementById("subcat").options[1] = new Option(vCatName,iCatId);
					document.getElementById("subcat").options[1].selected=true;
   				isWorking = false;
			}
			else if(parseInt(no)>0)
			{
				//document.getElementById("subcat").length = 1;
				for(j=0,i=1;j<no;i++,j++)
				{
		   			var iCatId = xmlDocument.getElementsByTagName('icategoryid').item(j).firstChild.data;
	   		   		var vCatName =xmlDocument.getElementsByTagName('catname').item(j).firstChild.data;
					if(document.getElementById("eSubCate").value==iCatId)
					{
						document.getElementById("subcat").options[i] = new Option(vCatName,iCatId);
						document.getElementById("subcat").options[i].selected=true;
					}else{
						document.getElementById("subcat").options[i] = new Option(vCatName,iCatId);
					}
					
   					isWorking = false;
  				}
    		}
		}
  	}
	else
	{
		document.getElementById("subcat").options[1] = new Option('Loading....','');
		document.getElementById("subcat").options[1].selected=true;
	}
}

function setSubCategory(iCateogryId) 
{
	if(iCateogryId=='')
	{
		document.getElementById("subcat").length=1;
		//var SelVal = (sel_state_name == 'Other')? true : false ;
		document.getElementById("subcat")[0].value	= '';
   		document.getElementById("subcat")[0].text 	= 'By Subcategory (optional)';
		return false;
	}
	var url="";
	url = AJAX_URL+"getSubCategory.php?iCategoryId="+iCateogryId
	//alert(url);
	var isWorking = false;
	if (!isWorking)
	{
		isWorking = true;
		if (window.XMLHttpRequest)
	  	{
	  		http=new XMLHttpRequest()
	  		http.open("GET",url, true);
		  	http.onreadystatechange=handleHttpResponse_subCat
		  	http.send(null)
	  	}
		// code for IE
		else if (window.ActiveXObject)
	  	{
	  		http=new ActiveXObject("Microsoft.XMLHTTP")
	    	if (http)
	    	{
	    		http.open("GET",url, true);
	    		http.onreadystatechange=handleHttpResponse_subCat
	    		http.send()
	    	}
	  	}
  	}
}
function chkVaild()
{
	var objSubCat = document.getElementById('subcat');
	var objCat = document.getElementById('cat')
	if(objCat.value ==0){
		alert('Please Select Category');
		objCat.focus();
		return false;
	}
	/*if(objSubCat.value ==0){
		alert('Please Select Sub Category');
		objSubCat.focus();
		return false;
	}*/
	return true;
}

function chkVaildKey()
{
	var objsearch = document.getElementById('txtSearch');
	if(objsearch.value == '' || objsearch.value == 'By Keywords' ){
		alert('Please Enter Search Keyword');
		objsearch.focus();
		return false;
	}
	return true;
}

function chkvalidoption(optionval)
{
	if(optionval=='catsearch')
		return chkVaild();
	else if(optionval=='keywordsearch')
		return chkVaildKey();	
}
function SearchItem(criteriaval)
{
	if(chkVaild())
	{
		var url='';

		if(criteriaval == 'company'){
			if(SEO_FRIENDLY_URL == 'Y'){
				if(document.getElementById('cat').value!='')
					url+="Cat/"+document.getElementById('cat').value+'/';
				if(document.getElementById('subcat').value!='')
					url+="SubCat/"+document.getElementById('subcat').value+'/';
				window.location.href = site_url+'content/company_search/1/1/'+url;
			}else{
				if(document.getElementById('cat').value!='')
					url+="&Cat="+document.getElementById('cat').value;
				if(document.getElementById('subcat').value!='')
					url+="&SubCat="+document.getElementById('subcat').value;
				window.location.href = site_url+'index.php?file=c-company_search&nstart=1&start=1'+url;
			}
		}else if(criteriaval == 'salesrep'){	
			if(SEO_FRIENDLY_URL == 'Y'){
				if(document.getElementById('cat').value!='')
					url+="Cat/"+document.getElementById('cat').value+'/';
				if(document.getElementById('subcat').value!='')
					url+="SubCat/"+document.getElementById('subcat').value+'/';
				//alert(site_url+'content/salesrep_search/1/1/'+url+'');
				window.location.href = site_url+'content/salesrep_search/1/1/'+url;
			}else{
				if(document.getElementById('cat').value!='')
					url+="&Cat="+document.getElementById('cat').value;
				if(document.getElementById('subcat').value!='')
					url+="&SubCat="+document.getElementById('subcat').value;
				window.location.href = site_url+'index.php?file=c-salesrep_search&nstart=1&start=1'+url;
			}
		}
	}

}
function SearchKeyItem(criteriaval)
{
	if(chkVaildKey())
	{
		var url='';
		
		if(criteriaval == 'company'){
			if(SEO_FRIENDLY_URL == 'Y'){
				url = 'content/company_search/1/1/';
				if(document.getElementById('txtSearch').value!='')
					url+="txtSearch/"+document.getElementById('txtSearch').value+'/';
			}else{
				url = 'index.php?file=c-company_search&nstart=1&start=1';

				var condtype = document['frmKeySearch'].elements['srchcond'];
				if(condtype[0].checked)
					url += "&condtype="+condtype[0].value;
				else
					url += "&condtype="+condtype[1].value;

				if(document.getElementById('txtSearch').value!='')
					url += "&txtSearch="+document.getElementById('txtSearch').value;
			}
//			alert(url);
			//window.location = site_url+'content/company_search/1/1/'+url;
			window.location.href = site_url+url;
			
		}else if(criteriaval == 'salesrep'){
			if(SEO_FRIENDLY_URL == 'Y'){
				url = 'content/salesrep_search/1/1/';
				if(document.getElementById('txtSearch').value!='')
					url+="txtSearch/"+document.getElementById('txtSearch').value+'/';
			}else{
				url = 'index.php?file=c-salesrep_search&nstart=1&start=1';
				
				var condtype = document['frmKeySearch'].elements['srchcond'];
				if(condtype[0].checked)
					url += "&condtype="+condtype[0].value;
				else
					url += "&condtype="+condtype[1].value;
				
				if(document.getElementById('txtSearch').value!='')
					url+="&txtSearch="+document.getElementById('txtSearch').value;
			}
			//window.location = site_url+'content/salesrep_search/1/1/'+url;
			window.location.href= site_url+url;
		}
	}
}
function searchTop(criteriaval,optionval)
{
	//alert(criteriaval);
	//alert(optionval)	
	//return false;	
	if(chkvalidoption(optionval))
	{
		var url='';
		
		if(criteriaval == 'company'){
			if(SEO_FRIENDLY_URL == 'Y')
			{
				url = 'content/company_search/1/1/';				
				if(optionval=='catsearch')
				{
					if(document.getElementById('cat').value!='')
						url+="Cat/"+document.getElementById('cat').value+'/';					
					if(document.getElementById('subcat').value!='')
						url+="SubCat/"+document.getElementById('subcat').value+'/';
				}
				else if(optionval=='keywordsearch')		
				{				
					if(document.getElementById('txtSearch').value!='')
						url+="txtSearch/"+document.getElementById('txtSearch').value+'/';
				}			
					
				window.location.href = site_url+'content/company_search/1/1/'+url;
				
			}
			else
			{
				url = 'index.php?file=c-company_search&nstart=1&start=1';				
				if(optionval=='catsearch')
				{		
					if(document.getElementById('cat').value!='')
						url+="&Cat="+document.getElementById('cat').value;
					if(document.getElementById('subcat').value!='')
						url+="&SubCat="+document.getElementById('subcat').value;	
				}
				else if(optionval=='keywordsearch')		
				{
					var condtype = document['frmSearch'].elements['srchcond'];
					if(condtype[0].checked)
						url += "&condtype="+condtype[0].value;
					else
						url += "&condtype="+condtype[1].value;
						
					if(document.getElementById('txtSearch').value!='')
						url += "&txtSearch="+document.getElementById('txtSearch').value;	
				}
						
			}
			//alert(url);
			//return false;
			window.location = site_url+'content/company_search/1/1/'+url;
			
			window.location.href = site_url+url;
			
		}
		else if(criteriaval == 'salesrep')
		{
			if(SEO_FRIENDLY_URL == 'Y')
			{
				url = 'content/salesrep_search/1/1/';
				if(optionval=='catsearch')
				{					
					if(document.getElementById('cat').value!='')
						url+="Cat/"+document.getElementById('cat').value+'/';
					if(document.getElementById('subcat').value!='')
						url+="SubCat/"+document.getElementById('subcat').value+'/';	
				}		
				else if(optionval=='keywordsearch')		
				{
					if(document.getElementById('txtSearch').value!='')
						url+="txtSearch/"+document.getElementById('txtSearch').value+'/';
				}		
			}
			else
			{
				url = 'index.php?file=c-salesrep_search&nstart=1&start=1';
				
				if(optionval=='catsearch')
				{					
					if(document.getElementById('cat').value!='')
						url+="&Cat="+document.getElementById('cat').value;
					
					if(document.getElementById('subcat').value!='')
						url+="&SubCat="+document.getElementById('subcat').value;	
				}
				else if(optionval=='keywordsearch')		
				{		
					var condtype = document['frmSearch'].elements['srchcond'];
					if(condtype[0].checked)
						url += "&condtype="+condtype[0].value;
					else
						url += "&condtype="+condtype[1].value;
				
					if(document.getElementById('txtSearch').value!='')
						url+="&txtSearch="+document.getElementById('txtSearch').value;
				}			
			}
			//window.location = site_url+'content/salesrep_search/1/1/'+url;
			//alert(url);
			//return false;
			window.location.href= site_url+url;
		}
	
	}
	
}


