/* FAQ load jump-to links for page */
function popFAQLinks(){

  var divWriteTo = $('jumptolinks');
  var quests = $$('.faqWrap li.nobullet');
  //var quests = $$('.faqWrap ol li b');
  var arrQs = [];

  for(var i=0;i<quests.length;i++)
  {

    	var el2 = quests[i].down('h2');
    	var nodeB = quests[i].select('ol li b');

        if(el2.nodeName === "H2") 
        {
          var str2 = el2.innerHTML;
          var a3 = new Element('h6', { 'class': 'scrl'}).update(str2);
          divWriteTo.appendChild(a3);
        }
    
	for(var x=0;x<nodeB.length;x++) 
	{


		var el = $(nodeB[x]);

		var str1 = nodeB[x].innerHTML;
		var str = str1.strip();
		if(str.endsWith('?')) {
		var strURL = str.replace(/ /g,"-").toLowerCase();
		strURL = (strURL.length > 40) ? strURL.substring(0,40) : strURL.substring(0,(strURL.length - 1));
		strURL = strURL.escapeHTML();
		var a1 = new Element('a', { 'id': strURL, name: strURL });
		el.appendChild(a1);
		var a2 = new Element('a', { 'class': 'scrl', href: '#' + strURL, title:str }).update(str.truncate(55));
		divWriteTo.appendChild(a2);
		divWriteTo.appendChild(document.createElement('br'));
	
		}
	}

	
  }
  
  //alert(divWriteTo.getHeight());
  var elQ = $('quickQs');
  //alert(elQ.getHeight());
  //elQ.setHeight(elQ.getHeight() + divWriteTo.getHeight());
  //var HT = elQ.getHeight()+'px';
  
  if(divWriteTo.getHeight()>200) {
  //elQ.style.height = (divWriteTo.getHeight()-200+elQ.getHeight()) + 'px';
  elQ.style.height = divWriteTo.getHeight()+ 'px';
  //alert('new height is ' + (divWriteTo.getHeight()-200+elQ.getHeight())  );
  }
  
}

function genRandomFAQLinks(page){
    url = context + "faq/"+page+".jsp";
    var myAjax = new Ajax.Request(
            url, 
            {
                    method: 'get', 
                    parameters: '', 
                    onComplete: showResponse
            });
}

function showResponse(originalRequest) {
    //parse returned JSP
    var faqJSP = originalRequest.responseText;
    var list = getFAQs(faqJSP);
}

function getFAQs(jsp) {
  var divWrap = $('faqWrap');
  if(!divWrap) return;
  var divWriteTo = $('jumptolinks');
  divWrap.hide();
  divWrap.innerHTML = jsp.substring(jsp.indexOf("<ol>"),jsp.indexOf("</ol>"));
  var quests = $$('.faqWrap ol li b');
  var randomSet = genRandomNumbers(quests.length,10);
  
  var totalToGenerate = 3;
  var questionsAdded = 0;
  var i = 0;
  var faqPageLoc = "";
  
  if(location.href.indexOf('coil')>=0) {
    faqPageLoc = context + 'faq/coil.jsp#';
  } else {
    faqPageLoc = context + 'faq/extrusion.jsp#';
  }
  
  while(questionsAdded < totalToGenerate) {
    var str1 = quests[randomSet[i]].innerHTML;
    var str = str1.strip();
    
    if(str.endsWith('?')) {
      var strURL = str.replace(/ /g,"-").toLowerCase();
      strURL = (strURL.length > 40) ? strURL.substring(0,40) : strURL.substring(0,(strURL.length - 1));
      strURL = strURL.escapeHTML();
      var a1 = new Element('a', { 'id': strURL, name: strURL });
      quests[randomSet[i]].appendChild(a1);
      var a2 = new Element('a', { 'class': 'icon1', href: faqPageLoc + strURL, title:str }).update(str.truncate(23));
      divWriteTo.appendChild(a2);
      divWriteTo.appendChild(document.createElement('br'));
      questionsAdded+=1;
    }
    i+=1;
  }
  divWrap.innerHTML = "";
  //alert("test");
  //alert(divWriteTo.getHeight());
  
}

function genRandomNumbers(length,count) {
  if(length < count) {
     count = length;
  }
  var list = new Array();
  var randomSet = new Array();
  var total = 0;
  
  for(var i=0;i<length;i++) {
    list[i] = i;
  }
  
  while(total<count) {
    var index = Math.floor(list.length*Math.random());
    if(list[index] != null) {
      randomSet[total] = index;
      list[index] = null;
      total+=1;
    }
  }
  return randomSet;
}


var newWindow2;
function newWindow(path, winName, chrome) {
	var URL = path;
	//var URL = path + "&site=pac";
	if (!newWindow2 || newWindow2.closed) {

		newWindow2 = window.open(URL,winName,chrome);
		//if (!newWindow.opener) newWindow2.opener = window;

	} else {
		// window's already open; bring to front
		newWindow2.location = URL;
		newWindow2.focus();
	}

}


function logoff(site) {
	if (window.opener == null || 
	    window.opener.window == null || 
		window.opener.window.closed) {
                var parm = "";
                if (site) parm = "?site=" + site;
		document.location.href = "/val/logoff.jsp" + parm;
	}
	else {
		if (window.opener) {
			opener.logout(site);
		}
		else {
			alert("Error: could not get a hold of the opener window!");
		}
	}
}