//------------------------------------------------------------------------
// This file contains the common JavaScript used for AJAX searching
// 
//------------------------------------------------------------------------

var bgAllowAjax=true;
var gsUserEntered='';
function xmlhttpPost(strURL, strSubmit, strResultFunc) {
        //page, qry, func
		var xmlhttp=false;
		
		//if (@_jscript_version >= 5)
		// JScript gives us Conditional compilation, we can cope with old IE versions.
		// and security blocked creation of the objects.
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				
			}catch (E) {
				xmlhttp = false;
				bgAllowAjax=false;
			}
		}

		if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		  xmlhttp = new XMLHttpRequest();
		  bgAllowAjax=true
		}
		
		xmlhttp.open('POST', strURL, true);
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//note that this onreadystatechange MUST BE LOWERCASE
        xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
					strResponse = xmlhttp.responseText;
					switch (xmlhttp.status) {
					        // Page-not-found error
					        case 404:
					                alert('Error: Not Found. The requested URL ' + 
					                        strURL + ' could not be found.');
					                break;
					        // Display results in a full window for server-side errors
					        case 500:
					                handleErrFullPage(strResponse);
					                break;
					        default:
					                // Call JS alert for custom error or debug messages
					                if (strResponse.indexOf('Error:') > -1 || 
					                        strResponse.indexOf('Debug:') > -1) {
					                        alert(strResponse);
					                }
					                // Call the desired result function
					                else {	
					                        eval(strResultFunc + '(strResponse);');
					                }
					                break;
					}
			}
        }
        //strSubmit
        xmlhttp.send(strSubmit);
		//alert(xmlhttp.responseText)
}
//----------------------------------------------------------------------
function handleErrFullPage(strIn) {
        var errorWin;
//alert(strIn)
        // Create new window and display error
        try {
               //errorWin = window.open('', 'errorWin');
               document.getElementById('frmeError').body.innerHTML = strIn;
        }
        // If pop-up gets blocked, inform user
        catch(e) {
                alert('An error occurred, ' + strIn );
        }
}
//----------------------------------------------------------------------
function getSuggestions(objSelBox, strSuggestions, strPage, strParam, strRenderer){
	//objSelBox is the Select Listbox where the user selects an option.
	//		 This value is sent to strPage as sInputText
	//strSuggestions is the name and id of a div where the suggestions will be displayed
	//strPage is the name of the asp page that gets the data from the database.  Note that you cannot
	//			send parameters, so they must be added seperately as strParam
	//strParam is sent to strPage as sInputParam
	//strRenderer is the name of the javascript routine that interprets the string returned from strPage
		
	//alert(bgAllowAjax)
	if(bgAllowAjax==true){
		var tmpValue;
		var tmpSelBox;
		tmpSelBox = document.getElementById(objSelBox); 
		tmpValue=tmpSelBox[tmpSelBox.selectedIndex].value;
			
		if (tmpValue!==''){
			//alert('running')
			var qry = 'sInputText=' + escape(tmpValue) + '&' + 'sInputParam=' + strParam;
			xmlhttpPost(strPage,qry,strRenderer);

		}
	}
}
//----------------------------------------------------------------------
function ElementListing(strEntry) {
//Split up the entry according to how it was put together in the selection page
	var strEntryArray = strEntry.split('|');
    
    this.ValueID = strEntryArray[0];
    this.DisplayText   = strEntryArray[1];
    
}
//----------------------------------------------------------------------
function displayModelDetails(pstrIn){
// Interpret the string returned from the database selection page (strIn)
// and display it to the user
	strResponseArray = pstrIn.split('|');
	document.getElementById('detailPower').value = strResponseArray[0];
	document.getElementById('detailVoltage').value = strResponseArray[1];
	document.getElementById('detailPlug').value = strResponseArray[2];

}

//----------------------------------------------------------------------
function odmienModel($ilosc){        
	if($ilosc=='0')return 'modeli';
	$model = 'model';        
	if($ilosc>1) $model += ($ilosc<5||($ilosc%10<5&&$ilosc%10>1)?'e':'i');        
	return $model;     
}

function displayResultInList(psDropDownName, pstrIn) {
// Interpret the string returned from the database selection page (strIn)
// and display it to the user in the Listbox with the id ( and name) as psDropDownName
		//alert(pstrIn)
		
        var strContent = '';
        var strPrompt = '';
        var nRowCount = 0;
        var strResponseArray;
        var strContentArray;
		
        // Split row count / main results
        strResponseArray = pstrIn.split('\n\n');
        
        // Get row count, set prompt text
        nRowCount = strResponseArray[0];
        strPrompt = nRowCount + ' ' + odmienModel(nRowCount);
		
        // Actual records are in second array item --
        // Split them into the array of DB rows
        strContentArray = strResponseArray[1].split('\n');
    
	    //clear the current set of options, leaving in the Please Select
		document.getElementById(psDropDownName).options.length=0;
        document.getElementById(psDropDownName).options[0]=new Option();
		document.getElementById(psDropDownName).options[0].value=''
		document.getElementById(psDropDownName).options[0].text=strPrompt
		
        // Create table rows
        for (var i = 0; i < strContentArray.length-1; i++) {
                // Create a new option for each row of data
                objElement = new ElementListing(strContentArray[i]);
				
				//alert('row ' + i + ' value=' + objArray[0] + ' text= ' + objArray[1]);
								
                document.getElementById(psDropDownName).options[i+1]=new Option();
				document.getElementById(psDropDownName).options[i+1].value=objElement.ValueID
				document.getElementById(psDropDownName).options[i+1].text=objElement.DisplayText
				//set up the selected value//
				if (document.getElementById(psDropDownName).value == objElement.ValueID) {
					document.getElementById(psDropDownName).selectedIndex = i;
				}
        }
}
//----------------------------------------------------------------------
function displayResultInListSeries(pstrIn) {
//The dropdown list name and the counter div name change for each
// different selection.
	displayResultInList('lstSeriesSuggestions', pstrIn);
}
//----------------------------------------------------------------------
function displayResultInListModel(pstrIn) {
//The dropdown list name and the counter div name change for each
// different selection.
	displayResultInList('lstModelSuggestions', pstrIn);
}

//----------------------------------------------------------------------