function getTypeResult(el,qt)
{
	if (qt == null){qt = 1;}
	var q = trim(el.value);
	var elid = el.id;
	
	if(q != "")
	{
		setTimeout("xssAjax('"+q+"','"+p+"','"+elid+"',"+qt+")",50)
	}
	else
	{
		document.getElementById("divtyperesult").style.display = "none";
		document.getElementById("divtyperesult").innerHTML = "";
	}
	return true;
}

function closeTypeResult()
{
	document.getElementById("divtyperesult").style.display = "none";
	document.getElementById("divtyperesult").innerHTML = "";
}

var p = 0;
function nextTypeResult(el)
{
	p = p+1;
	getTypeResult(el,0);
}

function prevTypeResult(el)
{
	p = p-1;
	getTypeResult(el,0);
}

function pageTypeResult(el,p)
{
	p = p;
	getTypeResult(el,0);
}

function tr_imgresize(elem,maxw,maxh)
{
	if (elem == undefined || elem == null) return false;
	if (maxw == undefined) maxw = 50;
	if (maxh == undefined) maxh = 50;
	var orig_width = elem.width;
	var orig_height = elem.height;
	
	if (elem.width > elem.height) 
	{
		if(elem.width > maxw) 
		{ 
			elem.width = maxw; 
			elem.height = orig_height*(maxw/orig_width);
		}
	} 
	else 
	{
		if(elem.height > maxh) 
		{ 
			elem.height = maxh; 
			elem.width = orig_width*(maxh/orig_height);
		}
	}
	return true;
}

function xssAjax(q,p,el,qt) 
{
	var q2 = trim(document.getElementById(""+el+"").value);
	if((q == q2 && q != "") || qt == 0)
	{
		var host = document.location.protocol;
		q = q+"*";
		var qweery_url = host+"//box5.qweery.nl/index.php?fe="+tr_encodeURIComponent(qweery_client)+"&qprx=1&q="+tr_encodeURIComponent(q);
		//alert(qweery_url);
		var script_id = null;
		var script = document.createElement('script');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', qweery_url);
		script.setAttribute('id', 'script_id');
		
		script_id = document.getElementById('script_id');
		if(script_id)
		{
			document.getElementsByTagName('head')[0].removeChild(script_id);
		}
		document.getElementsByTagName('head')[0].appendChild(script);
	}
}

function callback(data) 
{
	var htmlcontent = "";
	for(var key in data) 
	{
		if(key == "content")
		{
			var rawdata = data[key];
			htmlcontent = base64_decode(rawdata);
			break;
		}
	}
	//alert(htmlcontent);
	
	if(trq != "")
	{
		var inputid = document.getElementById(""+trq+"");
		var posx = inputid.offsetLeft;
		var posy = inputid.offsetTop;
		var posh = inputid.offsetHeight;
		var posx = posx+tr_x
		var posy = posy+posh+tr_y;
		//alert(posx+" "+posy);
		document.getElementById("divtyperesult").style.position = "absolute";
		document.getElementById("divtyperesult").style.left = posx+"px";
		document.getElementById("divtyperesult").style.top = posy+"px";
	}
	
	document.getElementById("divtyperesult").innerHTML = htmlcontent;
	document.getElementById('divtyperesult').style.visibility = "visible";
	document.getElementById("divtyperesult").style.display = "";
	return true;
}

function trim( str, charlist ) 
{
    var whitespace;
    
    if(!charlist){
        whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    } else{
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    }
  
	for (var i = 0; i < str.length; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
		str = str.substring(i);
		break;
		}
	}
	for (i = str.length - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
    	}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function base64_decode( data ) 
{
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];

    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
    
    dec = tmp_arr.join('');
    dec = utf8_decode(dec);
    
    return dec;
}

function utf8_decode ( str_data ) 
{
 	var tmp_arr = [], i = ac = c = c1 = c2 = 0;

    while ( i < str_data.length ) {
        c = str_data.charCodeAt(i);
        if (c < 128) {
            tmp_arr[ac++] = String.fromCharCode(c); 
            i++;
        } else if ((c > 191) && (c < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    
    return tmp_arr.join('');
}

function tr_encodeURIComponent(str) 
{
	if (typeof(encodeURIComponent) == 'function') 
	{
		return encodeURIComponent(str);
	} 
	else 
	{
		return escape(str);
	}
}
