function options_checker(switcho)
{
	if(switcho==1)
	{
		if(document.getElementById("even_more_results"))
		{
			if(document.getElementById("even_more_results").checked)
			{
				document.getElementById("even_more_results").checked =false;
			}
		}
	}
	else
	{
		if(document.getElementById("more_results"))
		{
			if(document.getElementById("more_results").checked)
			{
				document.getElementById("more_results").checked =false;
			}
		}
	}
}


function search_options()
{
		if(!document.getElementById("search_options").innerHTML.match("More"))
		{
			document.getElementById("search_options").innerHTML = 'More <input type="checkbox" id="more_results" name="more_results" onClick="options_checker(1)"> Even more <input type="checkbox" id="even_more_results" name="even_more_results" onClick="options_checker(2)"> Inverted <input type="checkbox" id="inverted" name="inverted">';
	//document.getElementById("search_options").innerHTML = '<a href=\'JavaScript:xmlhttpPost("index.php?ajax=1&more_results=1")\'>More results</a> - <a href=\'JavaScript:xmlhttpPost("index.php?ajax=1&inverted=1")\'>Inverted results</a>';
		}
		else
		{
			document.getElementById("search_options").innerHTML = "";
		}
}
function search()
{
	
	xmlhttpPost('index.php?ajax=1');
}
function xmlhttpPost(strURL) 
{
	if(getquery())
	{
		updatepage1("Searching, please wait<br><br><img src=images/loading.gif><br><br>");
	    var xmlHttpReq = false;
	    var self = this;
	    // Mozilla/Safari
	    if (window.XMLHttpRequest) {
	        self.xmlHttpReq = new XMLHttpRequest();
	    }
	    // IE
	    else if (window.ActiveXObject) {
	       self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    self.xmlHttpReq.open('POST', strURL, true);
	    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    self.xmlHttpReq.onreadystatechange = function() {
	        if (self.xmlHttpReq.readyState == 4) {
	            updatepage(self.xmlHttpReq.responseText);
	        }
	    }
	//alert(getquerystring());
	   self.xmlHttpReq.send(getquerystring());
	}
}

function xmlhttpPost2(strURL) 
{
	//alert(strURL);
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
       self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			//alert(self.xmlHttpReq.responseText);
            updatepage2(self.xmlHttpReq.responseText);
        }
    }
	
   self.xmlHttpReq.send(getquerystring2());
}

function xmlhttpPost3(strURL) {
	
		updatepage1("Searching, please wait<br><br><img src=images/loading.gif><br><br>");
	    var xmlHttpReq = false;
	    var self = this;
	    // Mozilla/Safari
	    if (window.XMLHttpRequest) {
	        self.xmlHttpReq = new XMLHttpRequest();
	    }
	    // IE
	    else if (window.ActiveXObject) {
	       self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    self.xmlHttpReq.open('POST', strURL, true);
	    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    self.xmlHttpReq.onreadystatechange = function() {
	        if (self.xmlHttpReq.readyState == 4) {
	            updatepage(self.xmlHttpReq.responseText);
	        }
	    }
	   self.xmlHttpReq.send();
}

function send_pic_for_question()
{
	var cando = false;
	if(document.getElementById("loption1"))
	{
		if(document.getElementById("loption1").value)
		{
			cando = true;
		}
	}
	if(document.getElementById("loption2"))
	{
		if(document.getElementById("loption2").value)
		{
			cando = true;
		}
	}
	if(document.getElementById("lelse"))
	{
		if(document.getElementById("lelse").value)
		{
			cando = true;
		}
	}
	if(cando)
	{
		var loading2 = document.getElementById("loading2").innerHTML;
		if(!loading2.match("Please wait"))
		{
			document.getElementById("loading2").innerHTML = "<br>Sending. Please wait<br><img src='http://utyp.net/images/loading.gif'><br>";
			xmlhttpPost2("index.php?ajax=2");
		}
		else
		{
			alert("Already sending. Please wait.");
		}
	}
	else
	{
		alert("Please type something");
	}
}

function getquerystring2() 
{
	var qstr = 'ajax2=1&lapict=' + document.getElementById("lapict").value;
	if(document.getElementById("loption1"))
	{
		if(document.getElementById("loption1").value)
		{
			qstr += '&loption1=' + document.getElementById("loption1").value;
		}
	}
	if(document.getElementById("loption2"))
	{
		if(document.getElementById("loption2").value)
		{
			qstr += '&loption2=' + document.getElementById("loption2").value;
		}
	}
	if(document.getElementById("lelse"))
	{
		if(document.getElementById("lelse").value)
		{
			qstr += '&lelse=' + document.getElementById("lelse").value;
		}
	}
	return qstr;
}

function getquery() {
	var lquery = document.getElementById("lquery").value;
	if(!lquery)
	{
		lquery = parent.location.hash.replace(/#/i, "");
	}
	return escape(lquery);
}

function gethashquery() 
{
	//alert('gethashquery');
	if(typeof(  parent.location.hash ) != "undefined")
	{
		ya = parent.location.hash.replace(/#/i, "");
		if(ya != "")
		{
			//alert("gethashquery"+ parent.location.hash.replace(/#/i, ""));
			xmlhttpPost3('index.php?ajax=1&lquery=' + ya);
		}
	}
}

function getquerystring() {
	var qstr = "";
	if(document.getElementById("more_results"))
	{
		if(document.getElementById("more_results").checked)
		{
			qstr += 'more_results=1&';
		}		
	}
	
	if(document.getElementById("even_more_results"))
	{
		if(document.getElementById("even_more_results").checked)
		{
			qstr += 'even_more_results=1&';
		}		
	}
	
	if(document.getElementById("inverted"))
	{
		if(document.getElementById("inverted").checked)
		{
			qstr += 'inverted=1&';
		}		
	}
    qstr += 'lquery=' + getquery();  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage2(str){
    document.getElementById("loggin").innerHTML = str;
	document.getElementById("loading2").innerHTML = "";
}


function updatepage1(str){
    document.getElementById("result").innerHTML = str;
}
function updatepage(str){
    document.getElementById("result").innerHTML = str;
    var reg = /for<i> (.+)<\/i>/i;
    var ar = reg.exec(str); 
	if(ar!=null)
	{
		document.title = "UTYP results for "+ar[1];
		parent.location.hash = ar[1];
		document.getElementById("lquery").focus();
		var currentTime = new Date();
		googlead.location = "adsense.php?lquery="+getquery()+"&s=" + currentTime.getTime();
		 metatags = document.getElementsByTagName("link");
		 for (cnt = 0; cnt < metatags.length; cnt++)
		 {
			var name = metatags[cnt].getAttribute("name");
			 var content = metatags[cnt].getAttribute("href");
			 if (metatags[cnt].getAttribute("href") == "rss.php?query="){metatags[cnt].setAttribute("href", "rss.php?query="+ar[1]);content = metatags[cnt].getAttribute("href");}
		 }
	}
}

function placeFocus() 
{
	document.getElementById("lquery").focus();
}

function catchEnter(e,swi) 
{
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code==13) 
	{	
		if(swi==2)
		{
			send_pic_for_question();
		}
		if(swi==1)
		{
			search();
		}
	}
}


