function doSearchExpertAsking()
	{
	lstrCatUID = document.getElementById("selProductRange").value;
	lstrManSel = document.getElementById("selManufacturer").selectedIndex;
	lstrManUID = document.getElementById("selManufacturer").value;
//	askTheExpert(lstrCatUID + (lstrManSel > 0 ? '.' + lstrManUID : ""));
	}

function populateManufacturers(pintProductTypeUID)
	{
	doSearchExpertAsking();
	document.getElementById('resultcount').innerHTML = '<img src="/magicLibrary/images/ajax-loader-2.gif" />';
	var divRefine = document.getElementById("search_refinement");
	divRefine.style.display = 'none';
	document.getElementById('resultcount').innerHTML = '';
	document.getElementById('resultdisplay').style.display = 'none';
	document.getElementById('product_results_panel').innerHTML = '';

	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;

	// Remove the attribute boxes from here down, if nothing selected in either select box on this line..
			for (i=1; i<=lintSearchBoxes; i++)
				{
				if (document.getElementById('divAttribute' + i))
					{
					divRefine.removeChild(document.getElementById('divAttribute' + i));
					}
				}


lobjManAjax = new sack();
lobjManAjax.requestFile = '/_common/ajax/product_category_options.php?site='+ms_gstrCountryRoot;
lobjManAjax.encVar("catUID",pintProductTypeUID);
lobjManAjax.encVar("f","getManufacturers");
lobjManAjax.onCompletion = function () {
    document.getElementById('selManufacturer').options.length=0;
    lintCounter=0;
    var larrOptions = [];
    larrOptions["-1"] = _t("All manufacturers");
    eval(lobjManAjax.response);
	 if (larrOptions.length > 1)
	 	{
    for(i in larrOptions) {
        document.getElementById('selManufacturer').options[lintCounter] = new Option(larrOptions[i],i);
        lintCounter++;
    }
	 document.getElementById('selManufacturer').style.display = '';
	 populateAttributes(-1);
	 }
	 else
	 {
	 document.getElementById('selManufacturer').style.display = 'none';
	 }

}
lobjManAjax.runAJAX();
}

function populateAttributes(pintSupplierUID)
	{
	document.getElementById('resultcount').innerHTML = '<img src="/magicLibrary/images/ajax-loader-2.gif" />';
	doSearchExpertAsking();
	var divRefine = document.getElementById("search_refinement");
	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;
			for (i=1; i<=lintSearchBoxes; i++)
				{
				if (document.getElementById('divAttribute' + i))
					{
					divRefine.removeChild(document.getElementById('divAttribute' + i));
					}
				}
			rePopulateAttributes(pintSupplierUID);
	}

function rePopulateAttributes(pintSupplierUID)
	{
	document.getElementById('resultcount').innerHTML = '<img src="/magicLibrary/images/ajax-loader-2.gif" />';

	document.getElementById('product_results_panel').innerHTML = '';
	var divRefine = document.getElementById("search_refinement");
	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;
	var larrUsedOptions = new Array();

	if (pintSupplierUID > -1) {document.getElementById('resultdisplay').style.display = '';} else { document.getElementById('resultdisplay').style.display = 'none';}

	lobjAttrAjax = new sack();
	lobjAttrAjax.requestFile = '/_common/ajax/product_category_options.php?site='+ms_gstrCountryRoot;
	lobjAttrAjax.encVar("catUID",document.getElementById('selProductRange').options[document.getElementById('selProductRange').selectedIndex].value);
	lobjAttrAjax.encVar("supplierUID",pintSupplierUID);
	lobjAttrAjax.encVar("f","getAttributeList");

	if (lintSearchBoxes > 1)
		{
		for (i=1;i<=lintSearchBoxes;i++)
			{
			if (document.getElementById('selAttribute' + i).selectedIndex > 0)
				{
				larrUsedOptions[larrUsedOptions.length] = document.getElementById('selAttribute' + i).value;
				}
			}
		}

	lobjAttrAjax.encVar("exclusions",larrUsedOptions.join(","));

	lobjAttrAjax.onCompletion = function () {
	if (lintSearchBoxes == 0)
		{
		add_refinement_box();
		lintSearchBoxes++;
		}
	else
		{
		document.getElementById('selAttribute' + lintSearchBoxes + 'b').options.length=0;
		document.getElementById('selAttribute' + lintSearchBoxes + 'b').style.display = 'none';
		}

    lintCounter=0;
    var larrOptions = [];
    larrOptions["-1"] = _t("Refine by") + "...";

    eval(lobjAttrAjax.response);
    
	if (larrOptions.length==0)
		{
		document.getElementById('resultcount').innerHTML= '';
		document.getElementById('search_refinement').style.display='none';
		}
	else
		{
		document.getElementById('search_refinement').style.display='block';
		for(i in larrOptions)
			{
			document.getElementById('selAttribute' + lintSearchBoxes).options[lintCounter] = new Option(larrOptions[i],i);
            lintCounter++;
        }
        
        

    }

	preSearchCounter();

}
lobjAttrAjax.runAJAX();
}





function getAttributeDetails(pintAttrSelect) {
	document.getElementById('product_results_panel').innerHTML = '';
	var divRefine = document.getElementById("search_refinement");
	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;

		if (pintAttrSelect<lintSearchBoxes)
			{
			for (i=pintAttrSelect + 1; i<=lintSearchBoxes; i++)
				{
				if (document.getElementById('divAttribute' + i))
					{
					divRefine.removeChild(document.getElementById('divAttribute' + i));
					}
				}
			}


if (document.getElementById('selAttribute' + pintAttrSelect).selectedIndex != 0)
	{
	lobjAttrDetailAjax = new sack();
	lobjAttrDetailAjax.requestFile = '/_common/ajax/product_category_options.php?site='+ms_gstrCountryRoot;
	lobjAttrDetailAjax.encVar("attrUID",document.getElementById('selAttribute'+pintAttrSelect).options[document.getElementById('selAttribute'+pintAttrSelect).selectedIndex].value);
	lobjAttrDetailAjax.encVar("f","getAttributeDetails");
	lobjAttrDetailAjax.onCompletion = function () {
	    document.getElementById('selAttribute'+pintAttrSelect+'b').options.length=0;

	    lintCounter=0;
	    var larrOptions = [];
	    larrOptions["-1"] = _t("Any") + "...";
	    //alert(lobjAttrDetailAjax.response);
	    eval(lobjAttrDetailAjax.response);
	    for(i in larrOptions) {
	        document.getElementById('selAttribute'+pintAttrSelect+'b').options[lintCounter] = new Option(larrOptions[i],i);
	        lintCounter++;
	    }
	    document.getElementById('selAttribute'+pintAttrSelect+'b').disabled = false;
	document.getElementById('selAttribute' + pintAttrSelect + 'b').style.display = '';

	}
	lobjAttrDetailAjax.runAJAX();
	}
else
	{
	document.getElementById('selAttribute'+pintAttrSelect+'b').disabled = true;
	document.getElementById('selAttribute' + pintAttrSelect + 'b').style.display = 'none';
	preSearchCounter();
	}

}



function preSearch(pintSearchBox)
	{
	document.getElementById('resultcount').innerHTML = '<img src="/magicLibrary/images/ajax-loader-2.gif" />';
	var divRefine = document.getElementById("search_refinement");
	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;
	document.getElementById('product_results_panel').innerHTML = '';

	// Remove the attribute boxes from here down, if nothing selected in either select box on this line..
		if (pintSearchBox<lintSearchBoxes)
			{
			for (i=pintSearchBox + 1; i<=lintSearchBoxes; i++)
				{
				if (document.getElementById('divAttribute' + i))
					{
					divRefine.removeChild(document.getElementById('divAttribute' + i));
					}
				}
			}

	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;


		lobjPreSearchAjax = new sack('/_common/ajax/product_search-v2.php?site='+ms_gstrCountryRoot+'&format=short');
		lobjPreSearchAjax.encVar("catUID",document.getElementById('selProductRange').options[document.getElementById('selProductRange').selectedIndex].value);
		if (document.getElementById('selManufacturer').value != -1)
			{
			lobjPreSearchAjax.encVar("supplierUID",document.getElementById('selManufacturer').options[document.getElementById('selManufacturer').selectedIndex].value);
			}
		lobjPreSearchAjax.encVar("attributes",lintSearchBoxes);

		for (i=1;i<=lintSearchBoxes;i++)
			{
			if (document.getElementById('selAttribute' + i).selectedIndex>0 && document.getElementById('selAttribute' + i + 'b').selectedIndex>0) {
			    lobjPreSearchAjax.encVar("attr" + i + "UID",document.getElementById('selAttribute' + i).options[document.getElementById('selAttribute' + i).selectedIndex].value);
			    lobjPreSearchAjax.encVar("attr" + i + "Value",document.getElementById('selAttribute' + i + 'b').options[document.getElementById('selAttribute' + i + 'b').selectedIndex].value);
			}
			}

		lobjPreSearchAjax.onCompletion = function () {
		    document.getElementById('resultcount').innerHTML = lobjPreSearchAjax.response.replace(/^[ \s]+|[ \s]+$/,"");
			 if (document.getElementById('resultcount').innerHTML.substr(document.getElementById('resultcount').innerHTML.length -9 ,9).toLowerCase() != "</strong>")
			 	{
				document.getElementById('resultdisplay').style.display = 'none';
				document.getElementById('product_results_panel').innerHTML = '';
				}
else			 	{
				document.getElementById('resultdisplay').style.display = '';
				if (document.getElementById("selAttribute" + pintSearchBox).options.length > 2 && document.getElementById("selAttribute" + pintSearchBox + 'b').selectedIndex > 0)
					{
					add_refinement_box();
					rePopulateAttributes(document.getElementById('selManufacturer').value);
					}
				}
		}
		lobjPreSearchAjax.runAJAX();


	}



function preSearchCounter()
	{
	document.getElementById('resultcount').innerHTML = '<img src="/magicLibrary/images/ajax-loader-2.gif" />';
	document.getElementById('product_results_panel').innerHTML = '';
	var divRefine = document.getElementById("search_refinement");
	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;

	var lobjCounterAjax = new sack('/_common/ajax/product_search-v2.php?site='+ms_gstrCountryRoot+'&format=short');
	lobjCounterAjax.encVar("catUID",document.getElementById('selProductRange').options[document.getElementById('selProductRange').selectedIndex].value);
		if (document.getElementById('selManufacturer').value != -1)
			{
			lobjCounterAjax.encVar("supplierUID",document.getElementById('selManufacturer').options[document.getElementById('selManufacturer').selectedIndex].value);
			}
	lobjCounterAjax.encVar("attributes",lintSearchBoxes);

	for (i=1;i<=lintSearchBoxes;i++)
		{
		if (document.getElementById('selAttribute' + i).selectedIndex>0 && document.getElementById('selAttribute' + i + 'b').selectedIndex>0) {
		lobjCounterAjax.encVar("attr" + i + "UID",document.getElementById('selAttribute' + i).options[document.getElementById('selAttribute' + i).selectedIndex].value);
		lobjCounterAjax.encVar("attr" + i + "Value",document.getElementById('selAttribute' + i + 'b').options[document.getElementById('selAttribute' + i + 'b').selectedIndex].value);
		}
		}

		lobjCounterAjax.onCompletion = function () {
		    document.getElementById('resultcount').innerHTML = lobjCounterAjax.response.replace(/^[ \s]+|[ \s]+$/,"");
			 if (document.getElementById('resultcount').innerHTML.substr(document.getElementById('resultcount').innerHTML.length -9 ,9).toLowerCase() != "</strong>")
			 	{
				document.getElementById('resultdisplay').style.display = 'none';
				document.getElementById('product_results_panel').innerHTML = '';
				}
else			 	{
				document.getElementById('resultdisplay').style.display = '';
				}
		}
		lobjCounterAjax.runAJAX();


	}


function add_refinement_box()
	{
	// If an item has been selected, add another level of refinement in
	var divRefine = document.getElementById("search_refinement");
	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;
	var lintNewBox = lintSearchBoxes + 1;
	var lobjNewSpan = document.createElement("span");
	lobjNewSpan.innerHTML += '<span class="right"><select onchange="preSearch(' + lintNewBox + ');" name="selAttribute' + lintNewBox + 'b" id="selAttribute' + lintNewBox + 'b" class="userform_half" style="display:none" disabled="true";></select></span><select name="selAttribute' + lintNewBox + '" id="selAttribute' + lintNewBox + '" class="userform_half" onchange="getAttributeDetails(' + lintNewBox + ')"></select><div class=\"spacer\"><!-- --></div>';
	lobjNewSpan.id = "divAttribute" + lintNewBox;
	divRefine.appendChild(lobjNewSpan);
	divRefine.style.display = '';
	}


function executeSearch() {
	var divRefine = document.getElementById("search_refinement");
	var lintSearchBoxes = divRefine.getElementsByTagName("SELECT").length / 2;
lobjSearchAjax = new sack('/_common/ajax/product_search-v2.php?site='+ms_gstrCountryRoot);
lobjSearchAjax.encVar("catUID",document.getElementById('selProductRange').options[document.getElementById('selProductRange').selectedIndex].value);
lobjSearchAjax.encVar("supplierUID",document.getElementById('selManufacturer').options[document.getElementById('selManufacturer').selectedIndex].value);
lobjSearchAjax.encVar("attributes",lintSearchBoxes);

var lintSearchBoxes = document.getElementById("search_refinement").getElementsByTagName("SELECT").length / 2;

for (i=1;i<=lintSearchBoxes;i++)
	{
	if (document.getElementById('selAttribute' + i).selectedIndex>0 && document.getElementById('selAttribute' + i + 'b').selectedIndex>0) {
	    lobjSearchAjax.encVar("attr" + i + "UID",document.getElementById('selAttribute' + i).options[document.getElementById('selAttribute' + i).selectedIndex].value);
	    lobjSearchAjax.encVar("attr" + i + "Value",document.getElementById('selAttribute' + i + 'b').options[document.getElementById('selAttribute' + i + 'b').selectedIndex].value);
	}
	}

lobjSearchAjax.onCompletion = function () {
    document.getElementById('product_results_panel').innerHTML = lobjSearchAjax.response;
}
lobjSearchAjax.runAJAX();
}

function getResultsPage(pintPage) {
lobjSearchAjax = new sack('/_common/ajax/product_search-v2.php');
lobjSearchAjax.method = "GET";
lobjSearchAjax.setVar("msPage",pintPage);
lobjSearchAjax.setVar("site",ms_gstrCountryRoot);
lobjSearchAjax.onCompletion = function () {
    document.getElementById('product_results_panel').innerHTML = lobjSearchAjax.response;
}
lobjSearchAjax.runAJAX();
}



function showProductDetail(pintPage, pstrUID) {
lobjSearchAjax = new sack('/_common/ajax/product_detail.php');
lobjSearchAjax.method = "GET";
lobjSearchAjax.setVar("msPage",pintPage);
lobjSearchAjax.setVar("uid",pstrUID);
lobjSearchAjax.setVar("site",ms_gstrCountryRoot);
lobjSearchAjax.onCompletion = function () {
    document.getElementById('product_results_panel').innerHTML = lobjSearchAjax.response;
}
lobjSearchAjax.runAJAX();
}

function showProductQuote(pintPage, pstrUID) {
lobjSearchAjax = new sack('/_common/ajax/product_quote.php');
lobjSearchAjax.method = "GET";
lobjSearchAjax.setVar("msPage",pintPage);
lobjSearchAjax.setVar("uid",pstrUID);
lobjSearchAjax.setVar("site",ms_gstrCountryRoot);
lobjSearchAjax.onCompletion = function () {
    document.getElementById('product_results_panel').innerHTML = lobjSearchAjax.response;
}
lobjSearchAjax.runAJAX();
}



function isNumeric(pText) {
   var validChars = "01234565789";
   var isNumber = true;
   var Char;

   for(i=0; i<pText.length && isNumber == true; i++) {
       Char = pText.charAt(i);
       if (validChars.indexOf(Char) == -1) {
           isNumber = false;
       }
   }

   return isNumber;
}

function checkForm() {
   alertTxt='';
   if(document.getElementById('txtQty').value=='' || !isNumeric(document.getElementById('txtQty').value)) {
       alertTxt += '\n - ' + _t("Quantity");
   }
   if(document.getElementById('txtName').value=='') {
       alertTxt += '\n - ' + _t("Name");
   }
   if(document.getElementById('txtTelephone').value=='') {
       alertTxt += '\n - ' + _t("Telephone");
   }
   if(document.getElementById('txtEmail').value=='' || document.getElementById('txtEmail').value.indexOf('@')==-1 || document.getElementById('txtEmail').value.indexOf('.')==-1 ) {
       alertTxt += '\n - ' + _t("Email");
   }
   if(document.getElementById('txtPostcode').value=='') {
       alertTxt += '\n - ' + _t("Postcode");
   }
   if (alertTxt!='') {
       alert(_t("Please complete the following required fields") + ": \n" +alertTxt);
   } else {
       document.getElementById('frmSendQuote').submit();
   }
}


