var sgLoc = new AjaxSuggest('sgLoc','Locality','sg_locality','County', new getLocality());
var sgAct = new AjaxSuggestDbl('sgAct','Activity','sg_activity','', new getActivity());
var sgComp = new AjaxSuggest('sgComp','companyName','sg_company','', new getCompany());


function generatePrintPopup( Src, h, w ) { 
     var pictureSrc;
     tmp = window.open( Src,
                        "sp_print",
                        "scrollbars=1,resizable=0,height="+h+",width="+w+""
            );
     tmp.focus();
 }  


function goGoogle(address, companyName, fullAddress, errormsg, locality)
{
	var tmp = window.open( '/map.php',
                        "sp_google",
                        "scrollbars=1,resizable=0,height=600,width=800"
            );
     tmp.focus();
	 
	 tmp.window['aStr'] = address + ', Romania';
	 tmp.window['cStr'] = companyName;
	 tmp.window['aaStr'] = fullAddress;
   tmp.window['lStr'] = locality + ', Romania';
	 tmp.window['erStr'] = errormsg + ': \n'+address + ', Romania';
}

function frameGoogle(address, companyName, fullAddress, errormsg, locality)
{
	var tmp = window.frames["gg"];
	
	/*window.open( '/map.php',
                        "sp_google",
                        "scrollbars=1,resizable=0,height=600,width=800"
            );
     tmp.focus();*/
	 
	 tmp['aStr'] = address + ', Romania';
	 tmp['cStr'] = companyName;
	 tmp['aaStr'] = fullAddress;
	 tmp['lStr'] = locality + ', Romania';
	 tmp['erStr'] = errormsg + ': \n'+address + ', Romania';
}

function keres()
{
	//var oo = new getRes();
	//alert(oo.getList(escape(document.getElementById('Activity').value
	//document.getElementById('companyName').value = escape(document.getElementById('companyName').value);
	document.forms[0].submit();
}

function sInfo()
{
	
	document.getElementById('infoMsg').className = 'infoBlack';
	document.getElementById('infoMsg').innerHTML = sendInfoFormSendWaitingMsg;
	
	var sI = new sendInfo();
	var CUI = '';
	var RegNum = '';
	var Address = '';
	var cameFrom = '';
	var iCounty = '';
	if (document.getElementById('infoCUI'))
		CUI = document.getElementById('infoCUI').value;
	if (document.getElementById('infoRegNum'))
		RegNum = document.getElementById('infoRegNum').value;
	if (document.getElementById('infoCAddress'))
		Address = document.getElementById('infoCAddress').value;
	if (document.getElementById('infoFrom'))
		cameFrom = document.getElementById('infoFrom').value;
	if (document.getElementById('infoCounty'))
		iCounty = document.getElementById('infoCounty').value;
	//alert(	escape(cameFrom) + ' / ' +escape(CUI));

	var res = sI.sendMail(
		escape(document.getElementById('infoName').value),
		escape(document.getElementById('infoTel').value),
		escape(document.getElementById('infoCompanyName').value),
		escape(document.getElementById('infoEmail').value),
		escape(CUI),
		escape(RegNum),
		escape(Address),
		escape(cameFrom),
		escape(iCounty));
	if (res == 'ok')
	{
		document.getElementById('infoMsg').className = 'infoGreen';
		document.getElementById('infoMsg').innerHTML = sendInfoFormSendOkMsg;
		document.getElementById('infoName').value = "";
		document.getElementById('infoTel').value = "";
		document.getElementById('infoCompanyName').value = "";
		document.getElementById('infoEmail').value = "";
		if (document.getElementById('infoCUI'))
			document.getElementById('infoCUI').value = "";
		if (document.getElementById('infoRegNum'))
			document.getElementById('infoRegNum').value = "";
		if (document.getElementById('infoCAddress'))
			document.getElementById('infoCAddress').value = "";
	}
	else
	{
		document.getElementById('infoMsg').className = 'infoRed';
		document.getElementById('infoMsg').innerHTML = sendInfoFormSendFailedMsg;
	}
}

function AjaxSuggest(name,inputFieldID,suggestDivID,preFilterFieldID,phpObject) {
    
	this.inputFieldID = inputFieldID;
    this.suggestDivID = suggestDivID;
	this.preFilterFieldID = preFilterFieldID;
	this.phpObject = phpObject;
	
	
	this.selectedElem = -1;
	this.lastElem = -1;
	this.myElements = new Array();
	this.name = name;
		
	this.initialized = false;
	
	this.init = function() {
		if (this.initialized == false)
		{
			this.inputField = document.getElementById(inputFieldID);
			this.suggestDiv = document.getElementById(suggestDivID);
			if (this.preFilterFieldID == '')
			{
				this.preFilterField = null;
			}
			else
			{
				this.preFilterField = document.getElementById(preFilterFieldID);
			}
			
			this.initialized = true;
		}
	}
		
    this.searchSuggest = function(e) {
	
		this.init();
	
		if (this.inputField.value == '')
		{
			this.clearSuggest();
			return;
		}
		
		if (e==13)
		{
			this.suggestDiv.style.visibility = 'hidden';
			this.suggestDiv.innerHTML = '';
			return;
		}
		if (e==38 || e==40 )
		{
			//var ss = document.getElementById('search_suggest');
			if (this.suggestDiv.style.visibility == 'visible')
			{
				if (e==38)
				{
					this.next('up');
				}
				if (e==40)
				{
					this.next('down');
				}
			}
			return;
		}
		if (e==37 || e==38 || e==39 || e==40 || e==16 || e==17) return;
		
			var str = escape(this.inputField.value);
			//var str = this.inputField.value;
			
	
			this.suggestDiv.innerHTML = '';
			
			if (this.preFilterFieldID == '')
			{
				var respText = this.phpObject.getList(str,'');
			}
			else
			{
				var respText = this.phpObject.getList(str,this.preFilterField.value);
			}
			
			if(respText == null) return;
			
			this.myElements = respText.split("\n");
			this.selectedElem = -1;
			this.lastElem = this.myElements.length -2;
			
			if (respText != '')
			{
				this.suggestDiv.style.visibility = 'visible';
				this.fillDiv(-1);
			}
			else
			{
				this.suggestDiv.style.visibility = 'hidden';
			}
			

	};
	
	this.next = function(direction) {
		if (direction=='down')
		{
			if (this.selectedElem == this.lastElem) this.selectedElem = -1;
			this.selectedElem += 1;
			this.setValue(this.myElements[this.selectedElem]);
			
		}
		if (direction=='up')
		{
			if (this.selectedElem == 0) this.selectedElem = this.lastElem + 1;
			this.selectedElem -= 1;
			this.setValue(this.myElements[this.selectedElem]);
		}
		this.fillDiv(this.selectedElem);
	}
	
	this.fillDiv = function(sel)
	{
		
		this.suggestDiv.innerHTML = '';
		
		for(i=0; i < this.myElements.length-1; i++) {
				//Build our element string.  This is cleaner using the DOM, but
				//IE doesn't support dynamically added attributes.
				var suggest = '<div id="sItem' + i + '" onmouseover="'+this.name+'.suggestOver(this);" ';
				suggest += 'onmouseout="'+this.name+'.suggestOut(this);" ';
				suggest += 'onmousedown="'+this.name+'.setSearch(this.innerHTML);" ';
				
				if ( i == sel )
				{
					suggest += 'class="suggest_link_over">' + this.myElements[i] + '</div>';
				}
				else
				{
					suggest += 'class="suggest_link">' + this.myElements[i] + '</div>';
				}
				
				this.suggestDiv.innerHTML += suggest;
			}
	}
	
	this.clearSuggest = function()
	{
		this.init();
		this.myElements=new Array();
		this.suggestDiv.innerHTML = '';
		this.suggestDiv.style.visibility = 'hidden';
	}
	
	//Mouse over function
	this.suggestOver = function(div_value) {
		div_value.className = 'suggest_link_over';
	}
	//Mouse out function
	this.suggestOut = function(div_value) {
		div_value.className = 'suggest_link';
	}
	//Click function
	this.setSearch = function(value) {
		this.inputField.value = unescape(value).replace('&amp;','&');
		this.clearSuggest();
	}
	this.setValue = function(value) {
		this.inputField.value = unescape(value).replace('&amp;','&');	
	}
	
}

function AjaxSuggestDbl(name,inputFieldID,suggestDivID,preFilterFieldID,phpObject) {
    
	this.inputFieldID = inputFieldID;
    this.suggestDivID = suggestDivID;
	this.preFilterFieldID = preFilterFieldID;
	this.phpObject = phpObject;
	
	
	this.selectedElem = -1;
	this.lastElem = -1;
	this.myElements = new Array();
	this.name = name;
		
	this.initialized = false;
	
	this.init = function() {
		if (this.initialized == false)
		{
			this.inputField = document.getElementById(inputFieldID);
			this.suggestDiv = document.getElementById(suggestDivID);
			if (this.preFilterFieldID == '')
			{
				this.preFilterField = null;
			}
			else
			{
				this.preFilterField = document.getElementById(preFilterFieldID);
			}
			
			this.initialized = true;
		}
	}
		
    this.searchSuggest = function(e) {
	
		this.init();
	
		if (this.inputField.value == '')
		{
			this.clearSuggest();
			return;
		}
		
		if (e==13)
		{
			this.suggestDiv.style.visibility = 'hidden';
			this.suggestDiv.innerHTML = '';
			return;
		}
		if (e==38 || e==40 )
		{
			//var ss = document.getElementById('search_suggest');
			if (this.suggestDiv.style.visibility == 'visible')
			{
				if (e==38)
				{
					this.next('up');
				}
				if (e==40)
				{
					this.next('down');
				}
			}
			return;
		}
		if (e==37 || e==38 || e==39 || e==40 || e==16 || e==17) return;
		
			var str = escape(this.inputField.value);
			//var str = this.inputField.value;
			
	
			this.suggestDiv.innerHTML = '';
			
			if (this.preFilterFieldID == '')
			{
				var respText = this.phpObject.getList(str,'');
			}
			else
			{
				var respText = this.phpObject.getList(str,this.preFilterField.value);
			}
			
			if(respText == null) return;
			
			this.myElements = respText.split("\n");
			this.selectedElem = -1;
			this.lastElem = this.myElements.length -2;
			
			if (respText != '')
			{
				this.suggestDiv.style.visibility = 'visible';
				this.fillDiv(-1);
			}
			else
			{
				this.suggestDiv.style.visibility = 'hidden';
			}
			

	};
	
	this.next = function(direction) {
		if (direction=='down')
		{
			if (this.selectedElem == this.lastElem) this.selectedElem = -1;
			this.selectedElem += 1;
			var ar = new Array();
				
			ar = this.myElements[this.selectedElem].split("[[");
											 
			this.setValue(ar[0]);
			
		}
		if (direction=='up')
		{
			if (this.selectedElem == 0) this.selectedElem = this.lastElem + 1;
			this.selectedElem -= 1;
			var ar = new Array();
				
			ar = this.myElements[this.selectedElem].split("[[");
															 
			this.setValue(ar[0]);
		}
		this.fillDiv(this.selectedElem);
	}
	
	this.fillDiv = function(sel)
	{
		
		this.suggestDiv.innerHTML = '';

		for(i=0; i < this.myElements.length-1; i++) {
				//Build our element string.  This is cleaner using the DOM, but
				//IE doesn't support dynamically added attributes.
				var ar = new Array();
				
				ar = this.myElements[i].split("[[");
				var suggest = '<div style="float:left;" id="sItem' + i + '" onmouseover="'+this.name+'.suggestOver(this);" ';
				suggest += 'onmouseout="'+this.name+'.suggestOut(this);" ';
				suggest += 'onmousedown="'+this.name+'.setSearch(this.innerHTML);" ';
				
				if ( i == sel )
				{
					suggest += 'class="suggest_link_over">' + ar[0] + '</div>';
				}
				else
				{
					suggest += 'class="suggest_link">' + ar[0] + '</div>';
				}
				this.suggestDiv.innerHTML += suggest;
				
				var suggest = '<div style="float:left; margin-right:auto;" onmouseover="'+this.name+'.suggestOver(this);" ';
				suggest += 'onmouseout="'+this.name+'.suggestOut(this);" ';
				suggest += 'onmousedown="'+this.name+'.setSearch(this.innerHTML);" ';
				
				if ( i == sel )
				{
					suggest += 'class="suggest_link_over">' + ar[1] + '</div>';
				}
				else
				{
					suggest += 'class="suggest_link">' + ar[1] + '</div>';
				}
				this.suggestDiv.innerHTML += suggest + '<div style="clear:left;"></div>';
			}
	}
	
	this.clearSuggest = function()
	{
		this.init();
		this.myElements=new Array();
		this.suggestDiv.innerHTML = '';
		this.suggestDiv.style.visibility = 'hidden';
	}
	
	//Mouse over function
	this.suggestOver = function(div_value) {
		div_value.className = 'suggest_link_over';
	}
	//Mouse out function
	this.suggestOut = function(div_value) {
		div_value.className = 'suggest_link';
	}
	//Click function
	this.setSearch = function(value) {
		this.inputField.value = unescape(value);
		//.replace('&amp;','&')
		this.clearSuggest();
	}
	this.setValue = function(value) {
		this.inputField.value = unescape(value);	
	}
	
}


