
function sendReq(){	document.getElementById('loading').style.display = 'block';
	var qt = false;

	if(document.getElementById('qt_lookup').checked) qt = 'lookup';
	if(document.getElementById('qt_dig').checked) qt = 'dig';
	if(document.getElementById('qt_wwwhois').checked) qt = 'wwwhois';
	if(document.getElementById('qt_arin').checked) qt = 'arin';

	if(document.getElementById('qt_checkp').checked) qt = 'checkp';
	if(document.getElementById('qt_p').checked) qt = 'p';
	if(document.getElementById('qt_tr').checked) qt = 'tr';
	if(document.getElementById('qt_all').checked) qt = 'all';
	if(!qt){		alert('Не указано действие');
		return false;	}

	var ajax = new AVA_ajax();
	ajax.callBackFunc = 'ajaxInsert';
	ajax.phpVar = 'body';
	ajax.insert = "result";

	return ajax.ajaxCall('whois.php?target=' + document.getElementById('target').value + '&queryType=' + qt + '&port=' + document.getElementById('port').value);
}

function ajaxInsert(obj){
	/*
		Вставляет данные переданные через ajax
	*/

	document.getElementById(obj.insert).innerHTML = obj.getParsed(obj.phpVar);
	document.getElementById('loading').style.display = 'none';
}


function AVA_ajax(){

	this.request = false;
	this.result = '';

	this.parsedBlocks = new Array;
	this.parsedIds = new Array;

	this.postVars = new Array;
	this.postValues = new Array;

	this.insert = false;

	this.phpVar = '';
	this.objNum = '';
	this.callBackFunc = '';

	/*
		Обращение к удаленному
	*/

	this.ajaxCall = function (url){
		/*
			Отправляет запрос на обращение по указанному URL. Обращается методом GET, запрашивая переменную phpVar
			Устанавливает для ответа responseFunc
		 */

		this.objNum = ajaxObjects.append(this);
		url = this.setPhpVarToUrl(url);
		if(!this.createAjaxObj()) return false;

		this.request.open('GET', url, true);
		eval('this.request.onreadystatechange = function(){ ajaxResult(' + this.objNum + '); };');
		this.request.send(null);

		return true;
	}

	this.createAjaxObj = function(){
		//Создает объект Ajax

		try{
			this.request = new XMLHttpRequest();
		}
		catch(trymicrosoft){
			try {
				this.request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(othermicrosoft){
				try{
					this.request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(failed){
					this.request = false;
				}
			}
		}

		if(!this.request){
			alert("Error initializing XMLHttpRequest!");
			return false;
		}

		return true;
	}

	this.setPhpVarToUrl = function(url, phpVar, objNum){
		if(/\?/.test(url)) url = url + '&';
		else url = url + '?';

		return url + 'varOnly[]=' + this.phpVar + '&backObj=' + this.objNum + '&tmplName=ajax&rnd=' + Math.random();
	}


	/*
		Обработка ответа
	*/

	this.parseResult = function(){
		var parsed = this.result.split('|');
		for(var i = 0; i < parsed.length; i ++){
			var pair = parsed[i].split(':');
			if( pair.length < 2 ) continue;

			this.parsedIds[i] = pair[0];
			this.parsedBlocks[i] = Base64.decode(pair[1]);
		}
	}

	this.getParsed = function(id){
		if(!id) return this.result;

		for(var i = 0; i < this.parsedIds.length; i ++){
			if(this.parsedIds[i] == id) return this.parsedBlocks[i];
		}

		return this.result;
	}


	/*
		Отправка методом POST
	*/

	this.addPostVar = function(v, val){
		var n = this.postVars.length;
		this.postVars[n] = v;
		this.postValues[n] = val;
	}

	this.getPost = function (){
		var r = '';
		for(var i = 0; i < this.postVars.length; i ++){
			r += this.postVars[i] + '=' + escape(this.postValues[i]) + '&';
		}

		return r;
	}

	this.ajaxCallPost = function (url){
		/*
			Отправляет запрос на обращение по указанному URL. Обращается методом POST, запрашивая переменную phpVar
			Устанавливает для ответа responseFunc
		 */

		this.objNum = ajaxObjects.append(this);
		url = this.setPhpVarToUrl(url);
		if(!this.createAjaxObj()) return false;

		this.request.open('POST', url, true);
		eval('this.request.onreadystatechange = function(){ ajaxResult(' + this.objNum + '); };');
		this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.request.send(this.getPost());

		return true;
	}



	/*
		Подгрузка заставки
	*/

	this.waitPic = function(){
		var div = document.getElementById('doc_shadow');
		if(!div){
			div = this.insertShadowDiv();
		}

		div.style.position = 'fixed';
		div.style.zIndex = '1000';
		div.style.left = '0px';
		div.style.top = '0px';
		div.style.display = 'none';

		var ht = getBrowserWindowH();
		var wh = getBrowserWindowW();

		div.style.width = '100%';
		div.style.height = ht + 'px';
		div.style.display = 'block';

		div.innerHTML = '<div style="float: left; position: relative; width: 100%; height: ' + ht + 'px;">' +
			'<div style="position: absolute; background: #000; width: 100%; height: ' + ht + 'px; left: 0; top: 0;" class="doc_shadow2" id="doc_shadow2">&#160;</div>' +
			'<img src="' + _D + 'images/wait.gif" style="position: absolute; left: ' + Math.ceil(((wh - 118) / 2)) + 'px; top: ' + Math.ceil(((ht - 118) / 2)) + 'px;" id="wait_img" /></div>';
		setOpacity('doc_shadow2', 0, 0.5, 25);
	}

	this.insertShadowDiv = function(){
		//Добавляет заставку (если она не загружена)

		var div = document.createElement("div");
		div.setAttribute('id', 'doc_shadow');
		div.setAttribute('class', 'doc_shadow');
		document.body.appendChild(div);

		return div;
	}
}


function ajaxResult(id){
	var obj = ajaxObjects.load(id);

	if (obj.request.readyState == 4){		if (obj.request.status == 200){
			obj.result = obj.request.responseText;
			obj.parseResult();
			eval( obj.callBackFunc + '(obj)' );
		}
	}
}



/*
  base64 декодер

  copyRight:
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}


/*
  Объекты ajax
*/

function ajaxObjects(){

	this.objects = new Array;

	this.append = function(obj){
		var elms = this.objects.length;
		this.objects[elms] = obj;

		return elms;
	}

	this.load = function(id){
		return this.objects[id];
	}
}

ajaxObjects = new ajaxObjects();


