function toggle_to_simple(){
	document.getElementById('simple_search_tab').className='searchformtab_selected';
	document.getElementById('advanced_search_tab').className='searchformtab_deselected';
	document.getElementById('simple_search_form').className='simple_search_form_show';
	document.getElementById('advanced_search_form').className='advanced_search_form_hide';
}

function toggle_to_advanced(){
	document.getElementById('advanced_search_tab').className='searchformtab_selected';
	document.getElementById('simple_search_tab').className='searchformtab_deselected';
	document.getElementById('advanced_search_form').className='advanced_search_form_show';
	document.getElementById('simple_search_form').className='simple_search_form_hide';
}

function openWhois(NewRowID, domain){
	tableRef=document.getElementById('resultstable');
	rowRef=NewRowID-1;
	cellRef=tableRef.rows[rowRef].cells[tableRef.rows[rowRef].cells.length-1];
	cellRef.innerHTML = '<a href="#" onclick="closeWhois(this.parentNode.parentNode.rowIndex+1, \''+domain+'\'); return false;">Close</a>';
	addRow(NewRowID, domain);
}

function closeWhois(RowID, domain){
	tableRef=document.getElementById('resultstable');
	rowRef=RowID-1;
	cellRef=tableRef.rows[rowRef].cells[tableRef.rows[rowRef].cells.length-1];
	cellRef.innerHTML = '<a href="style.js" target="_blank" onclick="closeAllWhois(); openWhois(this.parentNode.parentNode.rowIndex+1, \''+domain+'\'); return false;">Whois</a>';
	deleteRow(RowID);
}

function addRow(NewRowID, domain){
	var tableRef=document.getElementById('resultstable');
    var newRow=tableRef.insertRow(NewRowID);
	var newCell=newRow.insertCell(0);
	tableRef.rows[NewRowID].cells[0].width=460;
	tableRef.rows[NewRowID].cells[0].colSpan=5;
	tableRef.rows[NewRowID].cells[0].id='whoisresult_'+domain;
	tableRef.rows[NewRowID].cells[0].title=domain;
	tableRef.rows[NewRowID].className='whois';
	//var newText=document.createTextNode('Please wait...<br /><br />If nothing happens, <a href="style.js">click here</a>.');
	//newCell.appendChild(newText);
	newCell.innerHTML='Please wait...<br /><br />If nothing happens, <a href="style.js" target="_blank">click here</a>.';
	sndReq(domain);
}

function deleteRow(RowID){
	var tableRef=document.getElementById('resultstable');
	tableRef.deleteRow(RowID);
}

function closeAllWhois(){
	var tableRef=document.getElementById('resultstable');
	for(var i=0;i<tableRef.rows.length;i++){
		if(tableRef.rows[i].className=='whois'){
			//alert(tableRef.rows[i].cells[0].title);
			closeWhois(tableRef.rows[i].rowIndex, tableRef.rows[i].cells[0].title);
		}
	}
}

function createRequestObject(){
	req = false;
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}

	if(req) {
		return req;
	} else {
		return false;
	}
}

var http = createRequestObject();

function sndReq(action){
	domain = action;
	http.open('get', '/whois.php?query='+action);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function handleResponse(){
	if(http.readyState == 4){
		var whoisresult = http.responseText;
		whoisresult = whoisresult.replace(/\n/g, "<br />");
		document.getElementById('whoisresult_'+domain).innerHTML = whoisresult;
	}
}

function new_freecap(){
	if(document.getElementById){
		thesrc = document.getElementById("freecap").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
	}else{
		alert("Sorry, cannot autoreload freeCap image\nSubmit the form and a new freeCap will be loaded");
	}
}

