//Allow you to add multiple events to the onload event handler
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//Get elements by class name - Dustin Diaz
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( hasClass(els[i], searchClass) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//Determine if a class is present on a target element
function hasClass(target, classValue) {
		var pattern = new RegExp("(^| )" + classValue + "( |$)"); 
  if (target.className.match(pattern)) {
     return true;
    }   
  return false;
}

//Remove a class from a target element
function removeClass(target, classValue) {
    var removedClass = target.className;
    var pattern = new RegExp("(^| )" + classValue + "( |$)");
 
    removedClass = removedClass.replace(pattern, "$1");
    removedClass = removedClass.replace(/ $/, "");
    target.className = removedClass;

    return true;
}

//Add a class to a target element
function addClass(target, classValue) {
    if (!hasClass(target, classValue)) {
        if (target.className == "") {
            target.className = classValue;
        }
        else {
            target.className += " " + classValue;
        }
    }
    return true;
}

//Makes creating arrays easier and shorter
function makeArray() {
     for (i = 0; i<makeArray.arguments.length; i++)
          this[i + 1] = makeArray.arguments[i];
}

function replaceText(dest,element,att,value,text){
 /*LOCATE DESTINATION IN HTML*/
 var textDest = document.getElementById(dest);
 /*remove old text (if an existing child node already exists)*/ 
 if (textDest.childNodes[0]) {
  textDest.removeChild(textDest.childNodes[0]);
 }
 /*MAKE NEW HTML DATA*/
 var newelmt = document.createElement(element);
 var newtext = document.createTextNode(text);
 newelmt.appendChild(newtext);
 newelmt.setAttribute(att,value);
 /*OUTPUT NEW HTML DATA TO DETINATION*/
 textDest.appendChild(newelmt); //append
 return false;
}

//END GENERAL FUNCTIONS



// Random testimonial switcher
function doTestiSwitcher()
{
	if (document.getElementById("quote")) {
		
		var quote = new Array();
		quote[0] = "\"We immediately had the confidence to go with Servatech. They put our minds at rest that they could deliver what we were looking for - a reliable and efficient maintenance and support service. Everyone at Servatech is so helpful and always available at the end of a phone. They spend time listening to our needs but are quick to act upon them.\"";
		quote[1] = "\"At Brooke North we needed to be sure that our IT support would rid us of irritating issues which had the potential to hinder the work of our partners. Servatech offered us a solution with a full maintenance and support contract providing the firm with a safe, secure and stable network. Servatech deliver on what they promise to do – an excellent team of people who can be relied upon.\"";
		quote[2] = "\"Information Technology is critical to our business and we aim to provide a personalised service using knowedgeable staff combined with the latest Internet technology. We decided to work with Servatech because they provided a variety and level of service second to none. Servatech staff have very high levels of skills in all areas of the IT service sector.\"";
		quote[3] = "\"We have grown with Servatech and trust them to provide us with the kind of service our business demands. Their team is friendly, approachable and very supportive. They always respond very quickly to any queries and offer insightful suggestions on how we can do things better – it has been a great partnership.\"";
		quote[4] = "\"We have an excellent relationship with Servatech. Other companies we have dealt with in the past have tended to be faceless and rigid. Servatech on the other hand are extremely approachable and flexible in the way they work. They are always prepared to listen to us and take time to explain how things should be done. The service they have provided us over the years has proved to be very good value for money.\"";
		quote[5] = "\"The great thing about the team at Servatech is that everyone is so easy to talk to and are always available. Any problems are sorted out with the minimum of fuss. The fact that we have been working with Servatech for more than five years tells its own story. We have considerable faith in their ability to deliver a professional IT service, which compliments the service we provide to our clients.\"";
		quote[6] = "\"I would just like to say how impressed I am with your guys.  Their response has been fantastic and certainly underlines my decision to move my maintenance contract to yourselves.\"";
		
		var cl = new Array();
		cl[0] = "Lynda Clutton";
		cl[1] = "Paul Butler";
		cl[2] = "Ben Jordan";
		cl[3] = "Sarah Brooksbank";
		cl[4] = "Ian Varney";
		cl[5] = "John Paterson"; 
		cl[6] = "Rosemary Exley"; 
		
		var job = new Array();
		job[0] = "IT administrator, Flexible Steel";
		job[1] = "Partnership Secretary, Brooke North Solicitors";
		job[2] = "Commercial Director, Fuel Card Group";
		job[3] = "Operations Manager, Horizon Recruitment";
		job[4] = "Group IT Manager, Harratts Group Limited";
		job[5] = "Hardware Support Manager, Consumer Credit Counselling Service";
		job[6] = "IT Manager, A Moon & Sons Ltd";
		
		var length = quote.length;
		var whichNo = Math.round(Math.random()*(length-1));
	
		var quotetext = quote[whichNo];
		document.getElementById("quote").firstChild.nodeValue = quotetext;

		
		if (document.getElementById("cl")) {
			var cltext = cl[whichNo];
			document.getElementById("cl").firstChild.nodeValue = cltext;
		}
		
		if (document.getElementById("job")) {
			var jobtext = job[whichNo];
			document.getElementById("job").firstChild.nodeValue = jobtext;
		}
	}
}
addLoadEvent(doTestiSwitcher);


// Random ad switcher
function doAdSwitcher()
{
	if (!document.getElementById("ad_space")) return false;
	
	var hrefs = new Array();
	hrefs[0] = "http://www.servatech.co.uk/infrastructure/voip.htm";
	hrefs[1] = "http://www.servatech.co.uk/internet/webhosting.htm";
	hrefs[2] = "http://www.servatech.co.uk/support/hardware.htm";
	hrefs[3] = "http://www.servatech.co.uk/support/software.htm";
	hrefs[4] = "http://www.servatech.co.uk/managed/";
	hrefs[5] = "http://www.servatech.co.uk/internet/";
	hrefs[6] = "http://www.servatech.co.uk/downloads/";
	hrefs[7] = "http://www.servatech.co.uk/managed/drawbridge.htm";
	hrefs[8] = "http://www.servatech.co.uk/mitel/";
	
	var imgs = new Array();
	imgs[0] = "http://www.servatech.co.uk/images/ad/ad_voip.gif";
	imgs[1] = "http://www.servatech.co.uk/images/ad/ad_hosting.gif";
	imgs[2] = "http://www.servatech.co.uk/images/ad/ad_hardware.gif";
	imgs[3] = "http://www.servatech.co.uk/images/ad/ad_software.gif";
	imgs[4] = "http://www.servatech.co.uk/images/ad/ad_managed.gif";
	imgs[5] = "http://www.servatech.co.uk/images/ad/ad_internet.gif";
	imgs[6] = "http://www.servatech.co.uk/images/ad/ad_brochure.gif";
	imgs[7] = "http://www.servatech.co.uk/images/ad/ad_drawbridge.gif";
	imgs[8] = "http://www.servatech.co.uk/images/ad/ad_minisite.gif";
	
	var alts = new Array();
	alts[0] = "Looking for VOIP";
	alts[1] = "Looking for Hosting";
	alts[2] = "Looking for Hardware Support";
	alts[3] = "Looking for Software Support";
	alts[4] = "Looking for Managed Services";
	alts[5] = "Looking for Internet Services";
	alts[6] = "Looking for Our Brochure";
	alts[7] = "Looking for Drawbridge";
	alts[8] = "Looking for Our VoIP Mini-site";
	
	var length = hrefs.length;
	var whichNo = Math.round(Math.random()*(length-1));

	var adimg = document.createElement('img');
	adimg.setAttribute('src', imgs[whichNo]);
	adimg.setAttribute('alt', alts[whichNo]);
	
	var adlink = document.createElement('a');
	adlink.setAttribute('href', hrefs[whichNo]);
	adlink.appendChild(adimg);

	document.getElementById('ad_space').appendChild(adlink);
}
addLoadEvent(doAdSwitcher);


//Makes link elements with class 'popup' or 'ext' open in a new browser window
function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup") || links[i].className.match("ext")) {
      links[i].onclick = function() {
        window.open(this.getAttribute("href"));
        return false;
      }
    }
  }
}
addLoadEvent(doPopups);