if(navigator.userAgent.indexOf("Mac") !=-1){ var is_mac=true;}
if(navigator.userAgent.indexOf("MSIE") !=-1){ var is_ie=true;}
if(navigator.userAgent.indexOf("Gecko") !=-1){ var is_gecko=true;}
if(is_mac && is_ie){ document.write("<link rel='stylesheet' type='text/css' media='all' href='css/mac_ie.css' />"); }
   
function submitSearch(){
	document.forms['kwSearch'].action ='search-results.aspx?page=1&query='+document.forms['kwSearch'].elements.searchI.value;
	document.forms['kwSearch'].submit();
}  
  
function newWin(URL){
  aWindow=window.open(URL,"newWindow");
  if (!aWindow.opener) aWindow.opener=self;  
}

function newWinPopup(URL, nm, w, h, gw, gh) {
  if(is_gecko){ w = gw; h = gh; }
  aWindow=window.open(URL, nm, "toolbar=no,width="+w+",height="+h+",status=yes,scrollbars=yes,resize=no,menubar=no");
  if (!aWindow.opener){ aWindow.opener=self; } 
}

function montre(id) {
	var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
	if (d) {d.style.display='block';}
}


// *********OPEN POPUP WINDOW (set dimensions)************ //
var newWindow = null;

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed) newWindow.close();
	}
}

function popUpWinChild(url, type, strWidth, strHeight){
	closeWin();
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=100,top=200";
	newWindow = window.open(url, 'newWinChild', tools);
	newWindow.focus();
}

function popUpWin(url, type, strWidth, strHeight){
	closeWin();
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=100,top=200";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}
// **********END OPEN POPUP WINDOW********** //

/* clears text from text boxes */
function clearItem(objElement, value) { 
	if(objElement.value == value) objElement.value="" ;
}

/* on dropdown select go to appropriate internal link */
function findLink(strValue) {
	document.location=strValue;
}

/* colors tables in a striped fashion */
function stripe() {
	var even = false;

	var evenColor = arguments[1] ? arguments[1] : "#F2F2F2";
	var oddColor = arguments[2] ? arguments[2] : "#ffffff";

	var table = document.getElementsByTagName("table");
	if (! table) { return; }

	for(var g=0; g<table.length; g++){  //for all tables
   		var tbodies = table[g].getElementsByTagName("tbody"); //get child tags
		for (var h = 0; h < tbodies.length; h++) {
			var trs = tbodies[h].getElementsByTagName("tr");
			for (var i = 0; i < trs.length; i++) {
        		// avoid rows that have a class attribute
        		// or backgroundColor style
        		if (! hasClass(trs[i]) && ! trs[i].style.backgroundColor) {  // get all the cells in this row...
      	    		var tds = trs[i].getElementsByTagName("td");
        	  		for (var j = 0; j < tds.length; j++) {         // and iterate through them...
            			var mytd = tds[j];
            			// avoid cells that have a class attribute
           		 		// or backgroundColor style
            			if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
              				mytd.style.backgroundColor =
                			even ? evenColor : oddColor;
            			}
          			}
        		}
        		// flip from odd to even, or vice-versa
        		even =  ! even;
      		}
		}
	}
}

// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
    var result = false;
    if (obj.getAttributeNode("class") != null) {
        result = obj.getAttributeNode("class").value;
    }
    return result;
}

function initValidate(type){
	document.getElementById('submitBtn').onclick = function(){validate(type)};
}

function validate(type) {
	document.getElementById('formMessage').innerHTML = "";
	messageStr = "";
	isValid = true;

	if(type == 'enquiry') {
		var myExp = /^.+@.+\..{2,4}$/;
		var form  = document.forms['submitToCMS'];
		var email = form.emailTxt.value;

		if(form.fnameTxt && form.fnameTxt.value == "first name" | form.fnameTxt.value == ""){ isValid = false; messageStr = 'first name missing<br />';}
		if(form.lnameTxt && form.lnameTxt.value == "last name" | form.lnameTxt.value == ""){ isValid = false; messageStr = 'last name missing<br />';}
		if(form.emailTxt && form.emailTxt.value == "mail@here.com" | form.emailTxt.value == ""){isValid = false; messageStr += 'email missing<br />';}
		if(form.emailTxt && form.emailTxt.value != "" && myExp.test(email) == false){isValid = false; messageStr += 'invalid email<br />'; }
		if(form.enquiryTxt && form.enquiryTxt.value == "feedback" | form.enquiryTxt.value == ""){isValid = false; messageStr += 'enquiry missing<br />';}
		if(form.enquiryTxt && form.enquiryTxt.value.length > 500){isValid = false; messageStr += 'enquiry exceeds character limit, please rephrase';}
		
		if(!isValid) document.getElementById('formMessage').innerHTML = messageStr;
		else {
			document.forms['submitToCMS'].action = "actions/Enquiry.aspx";
			document.forms['submitToCMS'].submit();
			
		}
		
		return isValid;
	
	} else if(type == 'search') {
		var form  = document.forms['Search'];

		if(form.queryTxt && form.queryTxt.value == "Search Terms" | form.queryTxt.value == ""){ isValid = false; messageStr = 'Search query missing<br />';}
		if(form.resultsTxt && form.resultsTxt.value == "10" | form.resultsTxt.value == "" |  isNaN(form.resultsTxt.value)){isValid = false; messageStr += 'results to display missing<br />';}
		
		if(!isValid) document.getElementById('formMessage').innerHTML = messageStr;
		else         document.location='advanced-search-results.aspx?page=1&query='+document.forms['Search'].elements.queryTxt.value + '&display=' + document.forms['Search'].elements.resultsTxt.value + '&searchtype=' + document.forms['Search'].elements.matchSel.value + '&orderby=' + document.forms['Search'].elements.orderSel.value;
		
		return isValid;
	}
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.href && anchor.rel == "external"){
			anchor.target = "_blank";
		}
	}
} 

function setActiveStyleSheet(title) {
	if (title == "null") {
		title = "Small text";
	}

	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return null;
}

function getPreferredStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title") ) return a.getAttribute("title");
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  //setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}
*/

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
var title = getActiveStyleSheet();
createCookie("style", title, 365);
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

/* image hovers */
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
