var shTextBox;
var shIdBox;
var shIdTypeBox;
var shIFrame;
var shURL;
var shTimeout = 0;
var shTextSave;
var shIdSave;
var shIdTypeSave;
var shLang = 'en'

function SearchHint_Init(textbox, iframeName, url, lang) {
	//called when the text box gains focus
	//take note of our values
	shTextBox = textbox;
	shIdBox = document.getElementById("ID");
	shIdTypeBox = document.getElementById("IDType");
	shTextSave = shTextBox.value;
	shIdSave = shIdBox.value;
	shIdTypeSave = shIdTypeBox.value;
	shIFrame = document.getElementById(iframeName);
	shURL = url;
	shLang = lang;
	//check if we can show the Iframe
	SearchHint_Check();
}

function SearchHint_Check() {
	// timed event
	// check to see if contents of Iframe need refreshing
	if (shTextBox.value.length > 1) {
		SearchHint_Refresh()
	} else {
		//IF too short then hide previous results if any
		SearchHint_Hide()
		//shTextBox.style.border = "solid 1px blue";
	}
	shTimeout = 0;
}
	
function SearchHint_Key(key)
/* function called on every keyUp in the text box */ 
/* keycodes
	8 = backspace
	9 = tab
	13 = enter
	27 = escape
	38 = uparrow
	40=down arrow
	46=del
	47=0
 */
{
	// press enter to select 
	// press TAB to move focus
	if(key == 13 || key == 9){
		SearchHint_Hide();
		return true;	
	}
	if(key == 27){
		// escape pressed - exit
		shTextBox.value = shTextSave;
		shIdBox.value = shIdSave;
		shIdTypeBox.value = shIdTypeSave;
		SearchHint_Hide();
		return true;	
	}
	if (key == 40) { // down arrow
		shIFrame.contentWindow.KeyDown();
		SearchHint_TimeoutUnset();
		return false;
	}
	if (key == 38) {// up
		shIFrame.contentWindow.KeyUp();
		SearchHint_TimeoutUnset();
		return false;
	}
	// if a key other than home, end  etc is pressed then set the timeout if it hasn't been set
	if (key==8 || key==46) { // backspace or del
		SearchHint_TimeoutSet();
		return true;
	}
	if (key>45 && key!=91 && key!=92) {// not control key and not Left/right Window key
		SearchHint_TimeoutSet();
		return true;
	}
	return true;
}
function SearchHint_TimeoutSet() {
	shIdBox.value = '0';
	shIdTypeBox.value = '0';
	if (shTimeout == 0) {
		//check not running already
		shTimeout = setTimeout("SearchHint_Check()", 200);
	}
}
function SearchHint_TimeoutUnset() {
	if (shTimeout>0) clearTimeout(shTimeout);
	shTimeout = 0;
}

function SearchHint_Close() {
	SearchHint_TimeoutUnset();
	SearchHint_Hide()
}
function SearchHint_Hide(){
	shIFrame.style.display = 'none';
}
function SearchHint_Show(){
	shIFrame.style.display = 'block';
}
function SearchHint_Refresh(){
	//shut off timer - next key press will start again 
	SearchHint_TimeoutUnset();
	//get the IFrame to do its biz
	tbv = shTextBox.value;
	if (shLang != 'en') {
		google.language.translate(tbv, shLang, "en", function (result) {
			if (result.error) {
				SearchHint_DoIt(shTextBox.value);
			} else {
				SearchHint_DoIt(result.translation);
			} 
		})
	} else {
		SearchHint_DoIt(shTextBox.value);
	}
}
function SearchHint_Resize(width, height) {
	shIFrame.width = width;
	shIFrame.height = height ;
}
function SearchHint_DoIt(tbv){
	shIFrame.src = shURL + tbv;
	//show the frame and wait for it to call back Resize when loaded
	SearchHint_Show();
}
function SearchHint_Select(IDtype, ID, name) {
	shTextBox.value = name;
	shIdTypeBox.value = IDtype;
	shIdBox.value = ID;
}
function SearchHint_Resize(w, h) {
	shIFrame.height = h + 'px'
	shIFrame.width = w + 'px'
}
