// JavaScript Document

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

var makeRound = function(){var els = document.getElementsByTagName('div'); for(var i=0; el=els[i]; i++) if(el.className.indexOf('round')>-1 && el.firstChild && el.firstChild.className!='t') el.innerHTML = '<b class="t"><b class="r"></b></b><div class="c"><b class="br"></b>'+el.innerHTML+'<b class="br"></b></div><b class="b"><b class="r"><!----></b></b>';}
var strSearchValue = " enter keyword(s)"
var setSearchValue = function(){document.searchform.searchinput.value = strSearchValue;}

function clearSearchValue(objval){
  if (objval == strSearchValue) {document.searchform.searchinput.value = ""}
}

function validateSearchValue(objval){
  if(objval == strSearchValue || objval == ""){event.returnValue = false; return}
}

window.onload = onloadFuncs
function onloadFuncs() {
  makeRound();
  setSearchValue();
}

function initDate(){
  //creates a date object and returns today's date as a string expression
  var obj = new Date();
  var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  var str = months[obj.getMonth()] + " " + obj.getDate() + ", " + obj.getFullYear();
  document.write(str)
}

function initTextCounter(field, countfield, maxlimit){
  if(field.value.length > maxlimit) //if too long...trim it!
  field.value = field.value.substring(0, maxlimit);
  //otherwise, update 'characters left' counter
  else 
  countfield.value = maxlimit - field.value.length;
}

function displayHiddenBlock(id){
  //display hidden blocks on htm page which display information
  linkObjLeft = document.getElementById('linkblock'+id).offsetLeft
  linkObjTop = document.getElementById('linkblock'+id).offsetTop
  hiddObj = document.getElementById('hiddenblock'+id)
  hiddObj.style.left = linkObjLeft + 20 + "px"
  hiddObj.style.top = linkObjTop + 20 + "px"
  hiddObj.style.display = "block";
}

function hideHiddenBlock(id){
  //hide hidden blocks on htm page which display information
  document.getElementById('hiddenblock'+id).style.display = "none";
}

function initSearch(strSearch){
  //arrays and variables for search engine
  sroot = "http://www.heartlands-ppi.co.uk/"  //root pointer of site
  url = new Array();  //page name
  title = new Array();  //title of page
  meta1 = new Array();  //meta keywords
  meta2 = new Array();  //meta description
  sword = new Array();  //search word(s)
  matches = new Array();  //matches per page
  keeper = new Array();  //used as a pointer to other arrays after sorting process of matches()
  
  readXMLDocument("search.xml",'page' );
  for (var i=0; i<xmldata.length; i++) {
	url[i] = xmldata[i].getAttribute('url')
	title[i] = xmldata[i].getAttribute('title')
	meta1[i] = xmldata[i].getAttribute('metakeywords')
	meta2[i] = xmldata[i].getAttribute('metadescription')
  }
  
  //set all matches() array values to zero and keeper() array values to increment by 1
  for(i=0;i<url.length;i++){matches[i]=0}
  for(i=0;i<url.length;i++){keeper[i]=i}
  
  //variables to store number of search words and their position in strSearch
  var nsword=0
  var psword=0

  //routine to run if strSearch contains any spaces e.g. " ", i.e. more than one word
  for(i=0;i<strSearch.length;i++){
    if(strSearch.substring(i,i+1)==" "){
	  sword[nsword] = strSearch.substring(psword,i).toLowerCase()
	  nsword++
	  psword=i+1
	}
  }
  
  //condition used to store strSearch as one word or, the final word if there are many words
  sword[nsword] = strSearch.substring(psword,strSearch.length).toLowerCase()
  
  //match search words with page meta and title data and count total entries
  var entries=0
  for(i=0;i<nsword+1;i++){
    for(t=0;t<url.length;t++){
	  if(title[t].toLowerCase().search(sword[i])>-1){matches[t]++;entries++;matches[t]++;entries++;} //double up findings
	  if(meta1[t].toLowerCase().search(sword[i])>-1){matches[t]++;entries++;}
	  if(meta2[t].toLowerCase().search(sword[i])>-1){matches[t]++;entries++;matches[t]++;entries++;} //double up findings
	}
  }
  
  //sort matches() array by number of entries found per link and keeper array() accordingly
  var temp,temp2
  for(i=0;i<matches.length;i++){
    for(t=0;t<matches.length-1;t++){
	  if(matches[t]<matches[t+1]){		
	    temp=matches[t];temp2=keeper[t]
	    matches[t]=matches[t+1];keeper[t]=keeper[t+1]
	    matches[t+1]=temp;keeper[t+1]=temp2
	  }
	}
  }
  
  //populate results in <div> with id 'leftcol' 
  var printsearch = "<p class='content_sectionhdr'>Search results for keywords(s): " + strSearch + "</p>"
  var maxindexes = matches[0]
  var maxresults = 6;countresults=0
  for(i=0;i<matches.length;i++){
    if(matches[i]>0&&i<maxresults){
	  percentage = Math.round((matches[i]/maxindexes)*100)
	  printsearch+= "<div style='position:relative'><p class='content_normal'><span><strong>"+(i+1)+" . "+title[keeper[i]]+"</strong></span><br /><a href="+(sroot+url[keeper[i]])+">"+(sroot+url[keeper[i]])+"</a><br /><span>Matched with keyword(s) : "+percentage+"%</span><div id='searchstars'>"
	  //produce stars in relation to percentage per link
	  if(percentage>9&&percentage<30){starloop=1}
	  if(percentage>29&&percentage<50){starloop=2}
	  if(percentage>49&&percentage<70){starloop=3}
	  if(percentage>69&&percentage<90){starloop=4}
	  if(percentage>89){starloop=5}
	  for(s=0;s<starloop;s++){
	    printsearch+= "<img src='imgs/star.gif' height='24' width='24' />"
	  }
	  printsearch+= "</div></p></div>";countresults++
	}
  }
  
  //if there are no results
  if(countresults==0){printsearch+= "<p class='content_normal'>There are no search results.</p><p class='content_normal'><strong>hint:</strong> Try using more than one keyword in your search or use a broader word selection.</p><p class='content_normal'>For a more accurate search then avoid using conjuction words like 'AND' or 'TO'.</p>"}
  document.getElementById("searchmachine").innerHTML = printsearch
}

function popupWin(value, name, w, h){
  if (! window.focus) return true;
  var win = window.open('', name, 'width=' + w + ', height=' + h + ', ' + 'location=no, menubar=no, ' + 'status=no, toolbar=no, scrollbars=no, resizable=no');
  value.target = name;
  win.focus();
  return true;
}

var xmldata

function readXMLDocument(doc, tag) {
  //open external .xml document  
  if (window.ActiveXObject) {var xml = new ActiveXObject("Microsoft.XMLDOM")} // IE
  else if (document.implementation.createDocument) {var xml = document.implementation.createDocument("","",null)} // Mozilla, Netscape, Firefox
  xml.async = false
  xml.load(doc)
  xmldata = xml.getElementsByTagName(tag);
}

function removeOption(obj){
  if (obj.options[0].value=="null") {obj.remove(obj.options[0])}  
}

function validateForm(stage) {
  var cfQ = new Array(true, true, true, true, false, true); cfA = []; cancelaction = false
  if (ecardform.fullname.value=="") {cfQ[0] = false; cfA[0] = "your name"}
  if (ecardform.email.value=="") {cfQ[1] = false; cfA[1] = "your email address"}
  if (ecardform.patientsName.value=="") {cfQ[2] = false; cfA[2] = "the patients name"}
  if (ecardform.hospital.value=="null") {cfQ[3] = false; cfA[3] = "hospital site"}
  for (var i=0; i<5; i++) {if (ecardform.ECpic[i].checked) {cfQ[4] = true}}
  cfA[4] = "select a picture"
  if (ecardform.securitycode.value=="") {cfQ[5] = false; cfA[5] = "security code"}
  str = "To continue please complete the following:\n\n"
  for (i=0; i<cfQ.length; i++) {if (cfQ[i]==false) {str+="* "+cfA[i]+"\n"; cancelaction = true}}
  if (cancelaction==true) {alert(str); return}
  
  if (stage=="preview") {
    popupWin(document.ecardform, 'ecp', 640, 450)
    document.ecardform.action = "ecards_preview.asp"
    document.ecardform.submit()
  }
  
  if (stage=="send") {
    document.ecardform.action = "ecards.asp"
	document.ecardform.target = "_self"
    document.ecardform.submit()
  }
}

function validateForm2(){
  var cfQ = new Array(true,true,true,true); cfA = []; cancelaction = false
  if (cancelaction==true) {alert(str); return}
  if (emailform.recipient.value=="null") {cfQ[0] = false; cfA[0] = "recipient"}
  if (emailform.senders_email.value=="") {cfQ[1] = false; cfA[1] = "your email address"}
  if (emailform.message.value=="") {cfQ[2] = false; cfA[2] = "your message"}
  if (emailform.securitycode.value=="") {cfQ[3] = false; cfA[3] = "security code"}
  str = "To continue please complete the following fields:\n\n"
  for (var i=0; i<cfQ.length; i++) {if (cfQ[i]==false) {str+="* "+cfA[i]+"\n"; cancelaction = true}}
  if (cancelaction==true) {alert(str); event.returnValue = false; return}
}

function Advert_hide(adkey) {
  document.getElementById("advert").style.visibility = "hidden"
  if (adkey!=0) {
	var d = new Date();
	d.setTime(d.getTime() + (7 * 24 * 60 * 60 * 1000));
	document.cookie = adkey + "=hide" + "; expires=" + d.toGMTString();
  }
}
