﻿
function DoCompSearch(qId,boxId,isAdvanced,pgCode, wsCode)
{
	var el=document.getElementById(qId);
	var query;
	if(el)
	{
	    // CHANGE ERRORS. (Tonya) 06.06.2007
	    //check : if function exists....(in DesignView may be function not exists)
	    if (el.value!="" && typeof(Add2Query)=="function")
	    {
	        var doSearch = "0";
	        var qParam="q";
	        if (isAdvanced)
	            qParam="qa";
	        else
		        doSearch = "1";
		        
		    query=Add2Query(qParam,escape(el.value));
		    query=Add2Query("doSearch",doSearch,query);
		    query=Add2Query("box",boxId,query);
		    query=Add2Query("pg","",query);
		    query=Add2Query("comp","all",query);
		    if (pgCode)
		       query=Add2Query("page",pgCode,query); 
		    if (wsCode)
		       query=Add2Query("ws",wsCode,query); 
		    document.location= "?"+query;
	    }
	    else
	    {
	        alert (noSearchString);
	    }
	}
	else alert("Search Error");
	return false;
}

function chkFields(e)
{
    if (!e) var e=window.event
    
    var f=document.pageForm;
    
	var worker=false;
	for (var i=0; i<f.length; i++) // loop thru textboxes
	{
		if (f[i].type=="text")
			if (f[i].value != "")
			    worker=true; // make sure that at least one is filled - if no one is filled - don't send search
			    // add the name and value to the query string
	}
	if (!worker){alert(fieldsNotFilled);e.returnValue=null;return false;}
	
	return true;
}

function GetFiledsData(query)
{
    var f=document.pageForm;
    
	for (var i=0; i<f.length; i++) // loop thru textboxes
	{
	    switch (f[i].type)
	    {
	        case "text":
	            if (f[i].value != "")
			    {
			        var obj= f[i];
				    var element=obj.name;
				    var val=obj.value;
				    //update query string
				    query=Add2Query(element,escape(val),query);
			    }
			    break
	        case "radio":
	            if (f[i].checked)
			    {
			        var obj= f[i];
				    var element=obj.name;
				    var val=obj.value;
				    //update query string
				    query=Add2Query(element,val,query);
			    }
			    break
	    }
	}
	// add advanced search parameter to query
	//query=Add2Query("qs","",query);
	f.send.disabled = true;
	return query;
}
function submitForm()
{
    var query = document.location.search;
    query = GetFiledsData(query);
    query = Add2Query("doSearch","1",query);
    document.location = "?" + query;
    
}


function getElement(aID)
{ 
  return (document.getElementById) ? document.getElementById(aID)
                                   : document.all[aID];
} 

/********* Function to run search A-Synchronously**************/

// CHANGE ERRORS (Lea) 10/06/07
// remove var, so that null doesn't get insertes in the middle of a search
//var XMLHttpRequestObj = null;

function GetCompSearchResults()
{
        if (document.location.search.indexOf("&q=")>-1 || document.location.search.indexOf("?q=")==0 || document.location.search.indexOf("&qa=")>-1)
        {
		    var Src = "";
            // CHANGE UPDATE: (Lea) 19/08/2009 Make link start from root, check if in localhost to add "project" to the link
            var prfix = document.location.href.indexOf("localhost")>-1?"/project":"";
		    Src = prfix + "/Apps/Public/SearchResults.aspx" + document.location.search+"&TmplCode="+TmplCode;

		    XMLHttpRequestObj = newXMLHttpRequest();
		    if(XMLHttpRequestObj)
		    {
			    XMLHttpRequestObj.onreadystatechange = CheckAndLoad;
			    XMLHttpRequestObj.open('GET', Src, true);
			    XMLHttpRequestObj.send(null);
   		    }
		    else
		    {
			    alert('There was a problem with the request.\nObject XMLHttpRequest is not created.\nPlease wait for alternative loading.');
    //			document.location.href = Src;
		    }
		}
}

function CheckAndLoad()
{
	if((typeof(XMLHttpRequestObj) != "undefined") && XMLHttpRequestObj)
	{
		if(XMLHttpRequestObj.readyState == 4) //Completed
		{
			if(XMLHttpRequestObj.status == 200) //OK
			{
				var htmldoc = XMLHttpRequestObj.responseText;
				if(htmldoc)
				{
				    var element = document.getElementById("idSearchResults");
					if(element)
					{
					    element.style.paddingTop=0;
						element.innerHTML = htmldoc;
						//alert(element.innerHTML);
					}
				}
			} 
			else 
			{
				alert('There was a problem with the request. Status code: ' + XMLHttpRequestObj.status);
			}
		}
	}
}


function newXMLHttpRequest()
{
	var httpRequest = false;
	if(window.XMLHttpRequest) //Mozilla, Safari
	{
		httpRequest = new XMLHttpRequest();
		if(httpRequest.overrideMimeType)
		{
			httpRequest.overrideMimeType("text/xml");
		}
	}
	else
	{
		if(window.ActiveXObject) //IE
		{
			try
			{
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e01)
			{
				try
				{
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e02)
				{}
			}
		}
	}
	
	return httpRequest;
}

function gotoPage(lnk)
{
    	event.cancelBubble = true;
    	event.returnValue = false;

	    var HTTP_Request = null;
	    var Src = "";

	    Src ="../Public/SearchResults.aspx" + lnk;

	    HTTP_Request = newXMLHttpRequest();
		
	    if(HTTP_Request)
	    {
		    HTTP_Request.onreadystatechange = CheckAndLoad;
		    HTTP_Request.open('GET', Src, true);
		    HTTP_Request.send(null);

		    XMLHttpRequestObj = HTTP_Request;
	    }
	    else
	    {
		    alert('There was a problem with the request.\nObject XMLHttpRequest is not created.\nPlease wait for alternative loading.');
//			document.location.href = Src;
	    }

}

function TxtKeyEventHandlerComp(qId,boxId,isAdvanced,pgName)
{
   if(event.keyCode==13)
   {
    	event.cancelBubble = true;
    	event.returnValue = false;
    	
    	DoCompSearch(qId,boxId,isAdvanced,pgName);
   }
}

/* Attach events*/ 
