// Handle Text removal

var nlpq;

function clearText(){
	if (nlpq.value == queryPrompt){
	  nlpq.value = "";
  }
}

function initEvents () {
	nlpq = document.getElementById('nlpq');
	if (nlpq) {
    nlpq.onfocus = clearText;
  }
  initRadio();
  changeLinks();
  //writePrintLink();
  if(window.opener) {
        writeCloseLink()
    }
}

function changeLinks(){
	for(var e=0; e<document.links.length; e++){
		var linkRef = document.links[e];
		if (linkRef.href.indexOf("mailto") == -1 && linkRef.hostname.indexOf("metafaq.com") == -1 && linkRef.hostname.indexOf("faqs.directline.com") == -1){
			linkRef.onclick = function(){
                openWin(this.href);
                return false;
            }
        }
    if(linkRef.href.indexOf("seo/callPage") != -1){
      linkRef.onclick = function(){
        openWin(this.href);
        return false;
        }
      }
    }
}
function rateAnswerNew() {
     document.forms['rate_form'].submit();
}
function initRadio() {
    if (!document.getElementsByTagName){ return; }
   var allbuttons = document.getElementsByTagName("input");
    // loop through all input tags and add events
    for (var i=0; i<allbuttons.length; i++){
        var button = allbuttons[i];
        if ((button.getAttribute("type") == "radio")) {
           button.onclick = rateAnswerNew;
        }
    }
 }
 function writePrintLink () {
  var printIt = document.getElementById('footerLinks');
  if (printIt) {
    var printBtnListItem = document.createElement('li');
    var printBtnLink = document.createElement('a');
        printBtnLink.setAttribute('href', '#');
	 var printBtnText = document.createTextNode('Print');
    // set onclicks - have to perform function else auto triggers the behaviour
    printBtnLink.onclick = function() { window.print(); return false;}
    printBtnLink.appendChild(printBtnText);
    printBtnListItem.appendChild(printBtnLink);
    printIt.appendChild(printBtnListItem);
  }
}
function writeCloseLink() {
    var holder = document.getElementById('footerLinks');
    if (holder) {
        var closeButtonList = document.createElement('li');
        var closeButtonLink = document.createElement('a');
            closeButtonLink.setAttribute('href', '#');
        var closeButtonText = document.createTextNode('Close window');
        closeButtonLink.onclick = function() {window.close(); return false;}
        closeButtonLink.appendChild(closeButtonText);
        closeButtonList.appendChild(closeButtonLink);
        holder.appendChild(closeButtonList);
      }
}
// Function: Opens links in a new window
function openWin(url){
	if (url){
		window.open(url, "newWin", "width=1050, height=960, scrollbars=yes, resizable=yes, toolbar=yes");
	}
}
// ie image non-caching fix
var ie = document.all;
if (ie) {
  try {
    document.execCommand("BackgroundImageCache", false, true);
  } catch(err) {}
}
