//--------------------------------
/*
*	AJAX DETAILSEARCH
*	Version 1.0 11.02.2008
*/  
//--------------------------------

//--------------------------------
// config vars 
var SearchUri           = "/phps/shop_ajax_detailsearch.php";
//--------------------------------
// global scrip vars 
var ajaxReq             = null;
var Debug               = false;


InitSearch();


// Init the search vars 
function InitSearch()
{
	//Our XmlHttpRequest object
	ajaxReq = getXmlHttpRequestObject();
	if (!ajaxReq)
		alert('Init error 1');
		
}
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject()
{
	http_request = false;
	if (window.XMLHttpRequest)
	 { // Mozilla, Safari,...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType)
            http_request.overrideMimeType('text/xml');
	} 
    else if (window.ActiveXObject) 
    { // IE
    	try 
        {
       		http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e) 
        
        {
          try
          {
             http_request = new ActiveXObject("Microsoft.XMLHTTP");
          }catch (e) 
          {
          	
          }
        }
     }
     return(http_request);
}
// Called from keyup on the size box or click on checkboxes 
// Starts the AJAX request.
function searchRequest(query) 
{
	if (ajaxReq && (ajaxReq.readyState==4 || ajaxReq.readyState==0)) 
	{
		ExchangDisplay_ProgressBar("id_ProgressBar","on"); 

		if (typeof query == 'undefined')
			ajaxReq.open("GET",SearchUri,true);
		else	
			ajaxReq.open("GET",SearchUri + query,true);

		ajaxReq.onreadystatechange = handleSearchRequest; 
		ajaxReq.send(null);
	}		
}
// Called when the AJAX response is returned.
function handleSearchRequest() 
{
	if (ajaxReq && ajaxReq.readyState==4)
	{
	  	//alert("handleSearchRequest");
		ExchangDisplay_ProgressBar("id_ProgressBar","off"); 
		var str         = ajaxReq.responseText.split("#LEX#");
		//alert(str);
		var found_start = false;
		for(i=0; i < str.length - 1; i++) 
		{
			if (str[i].search(/<!--STOP_SEARCH_OUTPUT-->/)!=-1)
				found_start = false;

			var pattern = str[i].split("|");
			if (pattern.length>=2)  // min 2 param
			{
				for(p=1; p < pattern.length; p++) 
				{
					if (pattern[0]=="msgbox")
					{
						alert(unescape(pattern[p]));
					}
					else
					{
//						alert(pattern[p]);
						var element = document.getElementById(pattern[0])
						if (element)
						{
							var cmd = pattern[p].split("=");
							if (cmd.length==2)  // min key + value
							{
							    // alert(cmd[0] + " = " + cmd[1]);
								switch(cmd[0])
								{
									case "disabled":
										element.disabled = parseInt(cmd[1]);
										break;
									case "checked":
										element.checked = parseInt(cmd[1]);
										break;
									case "innerHtml":
									case "innerHTML":
										element.innerHTML = unescape(cmd[1]);
										break;
									case "value":
										element.value = unescape(cmd[1]);
										break;
									case "display":
										element.style.display = cmd[1];
										break;
									default:
											DebugMsg('Key nicht gefunden: ' + cmd[0]);
										break;
								}
							}
							else
								DebugMsg('Command hat nicht key und value');
					  	}
						else
							DebugMsg('Id: ' + pattern[0] + ' nicht gefunden');
					}
				}
			}
			if (str[i].search(/<!--START_SEARCH_OUTPUT-->/)!=-1)
				found_start = true;
		}
	}
}
// submit event only if not cr key on formular

// onblur event close Box and set focus if CR was pressed 
function eventOnBlur(id)
{
//	if (KeyCr) 	// IE: if cr dont't jump to next submit button 
//		document.getElementById(txtSearchSizeId).focus();
}

function DebugMsg(msg)
{
	if (Debug)
		alert(msg);
}


// radiobutton geklickt 
function RadioClick(id)
{
	var query = "?" + id.substring(0,id.lastIndexOf("_")) + "=" + document.getElementById(id).value;

	searchRequest(query);
}
// Checkbox gedrückt 
function CheckboxClick(id)
{
	var query = "?" + id + "=";
//	alert(id + document.getElementById(id).checked);	
	if (document.getElementById(id).checked) 
		query += "set";
	else	
		query += "unset";
	
	searchRequest(query);
}

// Taste losgelassen in input textfield
function TxtFieldKeyUp(id)
{
	var value = escape(document.getElementById(id).value);

	if (id=="id_Size" &&  value.length==1) 
		return;
	if ((id=="id_Size" || id=="formAmountFrom" || id=="formAmountTo" || id=="formPercentFrom" || id=="formPercentTo")  && !value.length)
		value = "0";
	
	var query = "?" + id + "=" + value;
	searchRequest(query);
}



