// SGH 7/29/05	Common routines from ProdQuery
var featChangeListeners
function modFeatString (featID) {

		/*Temporarily add commas to the end of the featID and featstring variables*/
		if (featStr_js.length != 0){
			featStr_js += ","
			featID += ","
		}
    if (featStr_js.indexOf(featID) != -1) {
        //Remove feature
        featStr_js = featStr_js.replace(featID, "");
        // remove first or last comma
        if (featStr_js.charAt(0) == ',') {
            featStr_js = featStr_js.substr(1,featStr_js.length-1);
        }
        if (featStr_js.charAt(featStr_js.length-1) == ',') {
            featStr_js = featStr_js.substr(0,featStr_js.length-1);
        }
        if ((featStr_js.indexOf(",,")) != -1) {
            a=featStr_js.split(",,");
            featStr_js = a[0] + "," + a[1];
        }
    } else {
        featStr_js = featStr_js.concat(featID);
        if (featStr_js.charAt(featStr_js.length-1) == ',') {
            featStr_js = featStr_js.substr(0,featStr_js.length-1);
        }
    }
    saveFeatString ();
}

function modPriceString (featID) {
	priceStr_js = featID;
    savePriceString ();
}

function newTopic (newTopic) {
   if (newTopic == currentTopic) {
        URLString = "ProdQuery.asp?topic=";
    }
    else {
 		if( newTopic == 'ALL'){
			//If the anchor is part of the querystring then the floating SplashScreen div does not work
	        URLString = "ProdQuery.asp?topic=" + newTopic;
		}
		else{
	        URLString = "ProdQuery.asp?topic=" + newTopic;
		}
    }
    saveFeatString();
    location.replace(URLString);   
}

function saveFeatString () {
    document.cookie = "featString=" + escape(featStr_js)
}

function savePriceString () {
    var strPriceCookie;
    strPriceCookie = ComputePriceString();
    document.cookie = "priceString=" + escape(strPriceCookie)
}

function ComputePriceString()
{
//  LMB 06-17-2005  this function calculates a bottom price range of zero so that all the prices are selected.
	var priceString; 
	var stringIndex;
    if (priceStr_js != "") {
        priceArr = priceStr_js.split("=");
        stringIndex = priceArr[1].indexOf("-");
        if  (stringIndex != -1 ){
			pricearray2 = priceArr[1].split("-");
			priceArr[1] = "#0-" + pricearray2[1];
		}		
		else{
			priceArr[1] = "#0-25,000#";
		}
		
		priceString = priceArr[1];
        priceString = replaceHash(priceString);
        
		return "F10=" + priceArr[1];
     }
    else
	return "";
}


function clearCookie () {
	featStr_js = "";
	priceStr_js = "";
    saveFeatString ();
    savePriceString ();
}

function getFeatString () {
    var allCookies = document.cookie;
    var pos =  allCookies.indexOf("featString=");
    var ppos = allCookies.indexOf("priceString=");
    
    priceStr_js = retrieveCookie("priceString");
    if (priceStr_js != "") {
		setPrice();
    }
    else {
        priceStr_js = "";
    }
    
   featStr_js = retrieveCookie("featString");
   var addedFeatures = gup('addF');
   if ((addedFeatures != "None") && (addedFeatures != "")) {
        featStr_js = addedFeatures;
        saveFeatString();
   }
   var replaceFeatures = gup('repF');
   if (replaceFeatures.length > 0) {
       if (replaceFeatures == "Nothing") {
           replaceFeatures = "";
       }
       featStr_js = replaceFeatures;
       saveFeatString();
   }
   if (featStr_js != "") {
        setForm();
   }
}

function retrieveCookie(entry) {
    var allCookies = document.cookie;
    var pos =  allCookies.indexOf(entry);
    var temp = "";
    if (pos != -1) {
        var start = pos + entry.length;
        if (allCookies.charAt(start) == ';') return "";
        start = start+1;
        var end = allCookies.indexOf(";", start);
        if (end == -1) end = allCookies.length;
        temp = unescape(allCookies.substring(start, end));
    }
    return temp;
}

function setForm () {
    if (featStr_js != "") {
        featArr = featStr_js.split(",");
        for (i=0; i<featArr.length; i++) {
            temp = eval("document.query." + featArr[i]);
               // dgk   alert(featArr[i]);
          if (temp != null) {
                temp.checked = true;
            }
        }
    }
}

function replaceHash(entry) {
out = "#"; // replace this
add = ""; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
replaceSemi(temp);
replacePlus(temp);
return temp;
}

function replaceSemi(entry) {
out = ":"; // replace this
add = ","; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
return temp;
}

function replacePlus(entry) {
out = "@"; // replace this
add = "+"; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
return temp;
}

function setPrice() {
	var priceString, prce1, price1, prce2, price2;
    if (priceStr_js != "") {
        priceArr = priceStr_js.split("=");
        priceString = priceArr[1];
        priceString = replaceHash(priceString);
		prce1 = priceString.split("-");
	    price1 = prce1[1];
        temp = eval("document.query." + priceArr[0]);
        if (temp != null) {
			for(var i = 0 ; i < temp.length ; ++i) {
			if(temp[i].type == "radio") {
				prce2 = temp[i].value.split("-");
			    price2 = prce2[1];
				if(price1 == price2){
					temp[i].checked = true;
					priceStr_js = priceArr[0] + "=#" + temp[i].value + "#";
					savePriceString ();
					}
				}
			}
        }
    }
} 

function clearAll () {
    clearCookie();
    document.query.reset();
    fetchCurrentProductsSelected();
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function submitQuery () {
    if (document.pressed == "Save") {
       if ((featStr_js != "") || (priceStr_js != "")) {
           var currentTime = new Date();
           var month = currentTime.getMonth() + 1;
           var day = currentTime.getDate();
           var year = currentTime.getFullYear();
           srchname = prompt("Enter name to save new profile as", month+"-"+day+"-"+year)
           if (trim(srchname).length < 4) {
                alert('A search name must have at least four characters');
           } else if ((srchname.indexOf("\\") > -1) || (srchname.indexOf("'") > -1) || (srchname.indexOf("\"") > -1) || (srchname.indexOf("<") > -1) ||(srchname.indexOf(">") > -1) ||(srchname.indexOf(";") > -1)) {
                alert('A search name cannot contain these characters: \'\"\\<>;');
           } else {
                d = document.getElementById('GlobalSearch');
                if (d != null) {
                    if (d.value == "Y") {
                        globl = 1
                    } else {
                        globl = 0
                    }
                } else {
                    globl = 0
                }
                d = document.getElementById('OrgSearch');
                var orgl = 0;
                if (d != null) {
                    if (d.value == "Y") {
                        orgl = 1
                    }
                }
                self.location = 'ProdResults.asp?' + 'topic=' + escape(currentTopic) + "&srchcmd=S&srchname=" + srchname + "&global=" + globl + "&orgl=" + orgl;
           }
       } else {
           alert('You must select at least one feature in order to save a search!');
       }
       document.pressed = "";
    } else if (document.pressed == "Load") {
       d = document.getElementById('Param1');
       if (d != null) {
           srchid = d.value;
           self.location = 'ProdQuery.asp?' + 'topic=' + escape(currentTopic) + "&srchcmd=L&srchid=" + srchid;
       }
       document.pressed = "";
    } else if (document.pressed == "Add") {
       d = document.getElementById('Param1');
       if (d != null) {
           srchid = d.value;
           self.location = 'ProdQuery.asp?' + 'topic=' + escape(currentTopic) + "&srchcmd=A&srchid=" + srchid;
       }
       document.pressed = "";
    } else if (document.pressed == "Subtract") {
       d = document.getElementById('Param1');
       if (d != null) {
           srchid = d.value;
           self.location = 'ProdQuery.asp?' + 'topic=' + escape(currentTopic) + "&srchcmd=Sub&srchid=" + srchid;
       }
       document.pressed = "";
    } else if (document.pressed == "Delete") {
       d = document.getElementById('Param1');
       if (d != null) {
           if (confirm("Are you sure you want to permanently delete this Profile?")) {
               srchid = d.value;
               if (srchid == "Ignore") {
               } else {
                   self.location = 'ProdQuery.asp?' + 'topic=' + escape(currentTopic) + "&srchcmd=D&srchid=" + srchid;
               }
           }
       }
       document.pressed = "";
    } else if (document.pressed == "TSearchFeatureText") {
       d = prompt("Enter the text string to search the criteria text for", "");
       if (d != null) {
           var txtval = trim(d);
           if (txtval == "") {
               alert("No valid text was entered to search feature descriptions for");
           } else {
               self.location = 'ProdQuery.asp?' + 'topic=' + escape(currentTopic) + "&srchcmd=SFT&srchtxt=" + txtval;
           }
       }
       document.pressed = "";
    } else if (document.pressed == "useFilters") {
        self.location = 'ProdQuery.asp?cmd=UF&uf=' + document.getElementById('Param1').value.toLowerCase() + '&topic=' + escape(currentTopic);
        document.pressed = "";
    } else if (document.pressed == "changeFilters") {
        d1 = document.getElementById('Param1');
        d2 = document.getElementById('Param2');
        nloc = 'ProdQuery.asp?cmd=CF&topic=' + escape(currentTopic) + '&f=' + d1.value + '&v=' + d2.value;
        self.location = nloc;
        document.pressed = "";
    } else {
       if ((featStr_js != "") || (priceStr_js != "")) {
            d = document.getElementById('allv');
            if (d != null) {
                allv = d.value;
            } else {
                allv = 'N';
            }
           self.location = 'ProdResults.asp?' + 'topic=' + escape(currentTopic) + '&allv=' + allv;
       } else {
           alert('You must select at least one feature!');
       }
    }
}
function popUp(url) {
    defWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=450,height=350');
    defWin.self.focus();
}

function showDiv(e, id){
	var docX, docY;
	if( e )
	{
	  if( typeof( e.pageX ) == 'number' )
	  {
	     docX = e.pageX;
	     docY = e.pageY;
	  }
	  else
	  {
	     docX = e.clientX;
	     docY = e.clientY;
	  }
	}
	else
	{
 	  e = window.event;
	  docX = e.clientX;
	  docY = e.clientY;
	  if( document.documentElement
	    && ( document.documentElement.scrollTop
	    || document.documentElement.scrollLeft ) )
	  {
	     docX += document.documentElement.scrollLeft;
	     docY += document.documentElement.scrollTop;
	  } 
	  else if( document.body
	    && ( document.body.scrollTop
            || document.body.scrollLeft ) )
	  {
	     docX += document.body.scrollLeft;
	     docY += document.body.scrollTop;
	  }
	}

	var pagewidth = document.body.clientWidth+document.body.scrollLeft;
	var pageheight = document.body.clientHeight+document.body.scrollTop;
        var mouseposx = docX;
        var mouseposy = docY;
	var div = eval('document.all.div' + id);

	if(mouseposx < pagewidth/2){
		div.style.posLeft = mouseposx + 10;
	}
	else{
		div.style.posLeft = mouseposx - 410;
	}
	
	if(mouseposy < document.body.clientHeight/2){
		div.style.posTop = mouseposy + document.body.scrollTop + 10
	}
	else{
		div.style.posTop = mouseposy + document.body.scrollTop - 70
	}
	div.style.display = 'block';
}

function hideDiv(id){
	var div = eval('document.all.div' + id)
	div.style.display = 'none';
}

function hideSplash_local() {
	var splash;
	if(document.layers) {
		splash = document.splashScreen;
		if (splash)
			splash.visibility = "hide";
	}
	else if(document.all) {
		splash = document.all.splashScreen.style;
		if (splash)
			splash.visibility = "hidden";
	}
}


//Show and Hide instructions

function showHelp()    
{
	var d  = document.getElementById('instructions');
	if (d)
	{
		d.style.display = '';
	}
	d = document.getElementById('showLink')
	if (d)
	{
		d.style.display = 'none';
	}
	
}
function hideHelp()    
{
	var d  = document.getElementById('instructions');
	if (d)
	{
		d.style.display = 'none';
	}
	d = document.getElementById('showLink')
	if (d)
	{
		d.style.display = '';
	}
}

// Function to retrieve a URL parameter
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// Functions to implement AJAX for the ProdQuery screen
function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
}

var xmlhttp

function fetchCurrentProductsSelected()
{
    document.getElementById("currProdsCount").innerHTML="";
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support XMLHTTP!");
      return;
      } 
    var url="ajaxProdCountFetch.asp";
    xmlhttp.onreadystatechange=stateChangedPC;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

function stateChangedPC()
{
    if (xmlhttp.readyState==4)
      {
      document.getElementById("currProdsCount").innerHTML=xmlhttp.responseText;
      }
}