function setTree(obj,obj2) {
 sort(obj2,obj.value);
}
function copyToSelected(chk,to)	{

	var options = new Object();
	if (hasOptions(to)) {
		for (var i=0; i<to.options.length; i++) {
			options[to.options[i].value] = to.options[i].text;
			}
		}
		var o = chk;
		if(o.checked)	{
   		  if(to.name == 'location2')	{
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.id) {
				if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
		        		  to.options[index] = new Option( o.name, o.value, false, false);	    		
		       	}
			}
			else if(to.name == 'industry2')	{
				if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.id) {
					if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
		        		  to.options[index] = new Option( o.id, o.value, false, false);	    		
		       		}
			}
			else	{
				if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.id) {
					if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
		        		  to.options[index] = new Option( o.id, o.value, false, false);	    		
		       		}
			}
		}
}

function removeFromSelected(chk,from)	{
	if (!hasOptions(from)) { return; }
	
		for (var i=(from.options.length-1); i>=0; i--) { 
			var o=chk; 
				if(from.name=='sector2')	{
					if(from.options[i].text==o.id)
					from.options[i] = null; 
				}
				else if(from.name=='industry2')	{
					if(from.options[i].text==o.id)
					from.options[i] = null; 
				}
				else	{
					if(from.options[i].value==o.id)
					from.options[i] = null; 
				}
			}
		
	from.selectedIndex = -1; 
} 	

function removeSelectedOptions(from) { 
	if (!hasOptions(from)) { return; }
	
		for (var i=(from.options.length-1); i>=0; i--) { 
			var o=from.options[i]; 
			if (o.selected) { 		
			from.options[i] = null;		

					if(from.name == 'sector2')	{ 
						var rm = document.getElementById(o.text);
						rm.checked = false;
					}
					else if(from.name == 'industry2')	{ 
						var rm = document.getElementById(o.text);
						rm.checked = false;
					}					
					else	{
						var checks = document.getElementsByTagName('input');
							for(j = 0; j < checks.length; j++)	{
								if(checks[j].id == o.value)	{
									var chk = checks[j];
									chk.checked = false;									
								}
							}
					} 
		}
		 
	}
	from.selectedIndex = -1;			
} 	
	
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
}

function doSearch() {

	var thesectors;
    var thelocations;
    var theindustries;
	var lists = document.getElementsByTagName('ul');
	for (i = 0; i < lists.length; i++) {
		var theList = lists[i];
		if(theList.id == 'sector1')	{
			var checkboxes = theList.getElementsByTagName('input');
			for (var j = 0; j < checkboxes.length; j++) {
				var theCheckbox = checkboxes[j];
				
				if(theCheckbox.checked == true)	{										
					if(theCheckbox.value.length == 1)	{
						var sums = null;
						for(var ii = 0; ii < checkboxes.length; ii++)	{
							if(checkboxes[ii].value.charAt(0) == theCheckbox.value.charAt(0))	{
								if(sums != null) {
 	           						sums += ','+'Z'+checkboxes[ii].value;
 	          					}
						        else {
					 	            sums = 'Z'+checkboxes[ii].value;
					          	}
							}
						}
						if(sums!=null)
							theCheckbox.value = sums;
					}					
					if(thesectors) {
					if(theCheckbox.value.charAt(0) == 'Z')
      					thesectors += ","+theCheckbox.value;
      				else
      					thesectors += ","+'Z'+theCheckbox.value;
      				}
      				else {
      				if(theCheckbox.value.charAt(0) == 'Z')
        				thesectors = theCheckbox.value;
        			else
        				thesectors = 'Z'+theCheckbox.value;
      				}
				}
			}
		}
		else if(theList.id == 'industry1')	{
			var checkboxes = theList.getElementsByTagName('input');
			for (var j = 0; j < checkboxes.length; j++) {
				var theCheckbox = checkboxes[j];
				
				if(theCheckbox.checked == true)	{										
					if(theCheckbox.value.length == 1)	{
						var sums = null;
						for(var ii = 0; ii < checkboxes.length; ii++)	{
							if(checkboxes[ii].value.charAt(0) == theCheckbox.value.charAt(0))	{
								if(sums != null) {
 	           						sums += ','+checkboxes[ii].value;
 	          					}
						        else {
					 	            sums = checkboxes[ii].value;
					          	}
							}
						}
						if(sums!=null)
							theCheckbox.value = sums;
					}					
					if(theindustries) {
						theindustries += ","+theCheckbox.value;      				
      				}
      				else {
      					theindustries = theCheckbox.value;        			
      				}
				}
			}
		}
		else if(theList.id == 'location1')	{
			var checkboxes = theList.getElementsByTagName('input');
			for (var j = 0; j < checkboxes.length; j++) {
				var theCheckbox = checkboxes[j];
				
				if(theCheckbox.checked == true)	{					
					if(theCheckbox.value.length==4)	{
						var suml = null;
						for(var ii = 0; ii < checkboxes.length; ii++)	{
							if(checkboxes[ii].value.substr(0,4) == theCheckbox.value)	{
								if(suml != null) {
 	           						suml += ','+checkboxes[ii].value;
 	          					}
						        else {
					 	            suml += checkboxes[ii].value;
					          	}
							}
						}
						theCheckbox.value = suml;							
					}
					if(thelocations) {
      					thelocations += ","+theCheckbox.value;	      					
      				}
      				else {
        				thelocations = theCheckbox.value;
      				}
				}
			}
		}
	}
	if(!thesectors) thesectors = "null";
	if(!theindustries) theindustries = "null";
    if(!thelocations) thelocations = "null";           
	
	main.elements['thesectors'].value = thesectors;
	main.elements['theindustries'].value = theindustries;
	main.elements['thelocations'].value = thelocations;
	
	var elms = document.getElementsByTagName('input');
	for(var x = 0; x < elms.length; x++ )	{
		var theinp = elms[x];
		if(theinp.id == 'monetary')	{
			theinp.value = unFormatNumber(theinp.value);			
			theinp.value = theinp.value.replace(',','.');
		}
	}
	
	if(main.elements['type'])	{

		if(thesectors == "null" && thelocations == "null" && theindustries == "null")	{
			loadPage('showselect');
		}
		else	{
			loadPage('search');
		}
	}
	else	{
		loadPage('search');
	}	
	
}
   

function checkUncheck(el,type)	{
	var chk;
	var to;
	
	if(type=='sec')	{
		to = document.getElementById('sector2');
		chk = document.getElementById(el);
	}
	else if(type=='ind')	{
		to = document.getElementById('industry2');
		chk = document.getElementById(el);
	}
	else if(type=='loc')	{
		to = document.getElementById('location2');
		var checks = document.getElementsByTagName('input');
		for(i = 0; i < checks.length; i++)	{
			if(checks[i].id == el)	{
				chk = checks[i];
				break;
			}
		}
	}	
	if(chk.checked == true)	{
		chk.checked = false;
		removeFromSelected(chk,to);
	}
	else	{
		chk.checked = true;	
		copyToSelected(chk,to);
	}
}

function checkUncheckBox(el,type)	{
	var chk;
	var to;
	
	if(type=='sec')	{
		to = document.getElementById('sector2');
		chk = document.getElementById(el);
	}
	else if(type=='ind')	{
		to = document.getElementById('industry2');
		chk = document.getElementById(el);
	}
	else if(type=='loc')	{
		to = document.getElementById('location2');
		var checks = document.getElementsByTagName('input');
		for(i = 0; i < checks.length; i++)	{
			if(checks[i].id == el)	{
				chk = checks[i];
				break;
			}
		}
	}	
	if(chk.checked == false)	{
		chk.checked = false;
		removeFromSelected(chk,to);
	}
	else	{
		chk.checked = true;	
		copyToSelected(chk,to);
	}
}

function getMatched(val,type)	{

	var lists = document.getElementsByTagName('ul');
	val = val.toLowerCase();
	for (i = 0; i < lists.length; i++) {
		var theList = lists[i];	
			if(theList.id == 'sector1' && type == 'sec')	{
				var it = theList.getElementsByTagName('li');			
				for (var j = 0; j < it.length; j++) {
					var theItem = it[j];
					if(theItem.title.toLowerCase().indexOf(val) != -1 && val.length>0)	{
						theItem.style['display'] = '';
					}
					else if(val.length<1)	{
						theItem.style['display'] = '';
					}				
					else	{
						theItem.style['display'] = 'none';
					}
				}
			}
			
			else if(theList.id == 'industry1' && type == 'ind')	{
				var it = theList.getElementsByTagName('li');			
				for (var j = 0; j < it.length; j++) {
					var theItem = it[j];
					if(theItem.title.toLowerCase().indexOf(val) != -1 && val.length>0)	{
						theItem.style['display'] = '';
					}
					else if(val.length<1)	{
						theItem.style['display'] = '';
					}				
					else	{
						theItem.style['display'] = 'none';
					}
				}
			}
			
			else if(theList.id == 'location1' && type == 'loc')	{
				var it = theList.getElementsByTagName('li');			
				for (var j = 0; j < it.length; j++) {
					var theItem = it[j];
					if(startsWith(theItem.title.toLowerCase(),val) && val.length>0 )	{
						theItem.style['display'] = '';
					}
					else if(val.length<1)	{
						theItem.style['display'] = '';
					}
					else	{
						theItem.style['display'] = 'none';
					}				
				}
			}	
	}		
}

function showChilds(val, type)	{
	var lists = document.getElementsByTagName('ul');
	for (i = 0; i < lists.length; i++) {
		var theList = lists[i];
		if(theList.id == 'sector1' && type == 'sec')	{
			var it = theList.getElementsByTagName('li');			
			for (var j = 0; j < it.length; j++) {
				var theItem = it[j];
				if(theItem.id.charAt(0) == val.charAt(0))	{
					theItem.style['display'] = '';
				}				
			}
		}
		
		else if(theList.id == 'industry1' && type == 'ind')	{
			var it = theList.getElementsByTagName('li');			
			for (var j = 0; j < it.length; j++) {
				var theItem = it[j];
				if(theItem.id.charAt(0) == val.charAt(0))	{
					theItem.style['display'] = '';
				}				
			}
		}
		
		else if(theList.id == 'location1' && type == 'loc')	{
			var it = theList.getElementsByTagName('li');			
			for (var j = 0; j < it.length; j++) {
				var theItem = it[j];
				if(theItem.id.substr(0,4) == val.substr(0,4))	{
					theItem.style['display'] = '';
				}				
			}
		}
	}
}

function hideChilds(val,type)	{
	var lists = document.getElementsByTagName('ul');
	for (i = 0; i < lists.length; i++) {
		var theList = lists[i];
		if(theList.id == 'sector1' && type == 'sec')	{
			var it = theList.getElementsByTagName('li');			
			for (var j = 0; j < it.length; j++) {
				var theItem = it[j];
				if(theItem.id.charAt(0) == val.charAt(0) && theItem.id!=val)	{
					theItem.style['display'] = 'none';
				}				
			}
		}
		
		else if(theList.id == 'industry1' && type == 'ind')	{
			var it = theList.getElementsByTagName('li');			
			for (var j = 0; j < it.length; j++) {
				var theItem = it[j];
				if(theItem.id.charAt(0) == val.charAt(0) && theItem.id!=val)	{
					theItem.style['display'] = 'none';
				}				
			}
		}		
		
		else if(theList.id == 'location1' && type == 'loc')	{
			var it = theList.getElementsByTagName('li');			
			for (var j = 0; j < it.length; j++) {
				var theItem = it[j];
				if(theItem.id.substr(0,4) == val.substr(0,4) && theItem.id!=val)	{
					theItem.style['display'] = 'none';
				}				
			}
		}
	}
}


