sfHover = function() {
	var sfEls = document.getElementById("topnav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function setActiveStyleSheet(title) {
  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 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];}
}
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_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 popupWindow(theURL,winName,txtHeight,txtWidth,features,centered) {
	if(centered == 1){
  /* Centre the popup Window  */
		txtHeight = (txtHeight > screen.height) ? screen.height : txtHeight;
		txtWidth = (txtWidth > screen.width) ? screen.width : txtWidth;
  var xcorner = parseInt((screen.width - txtWidth)/2);
  var ycorner = parseInt((screen.height - txtHeight)/2);
	var isErr = false,errMsg = 'Some required information was either missing or incorrect. Please check the following:\n\n',field = document.frmDonation;
	if(field.Web_Payment_Amount.value=='') {
		isErr = true; errMsg += '* The donation amount \n';
	}
	if(field.Web_Payment_Amount.value!='') {
		if(Right(field.Web_Payment_Amount.value, 3) != ".00") {
		isErr = true; errMsg += '* The donation amount must be in dollar format (e.g. 50.00) \n';
		}
	}
	dType = -1;
	for (i=field.DonationType.length-1; i > -1; i--) {
		if (field.DonationType[i].checked) {
			dType = i;
		}
	}
	if (dType == -1) {
		isErr = true; errMsg += '* The type of online donation \n';
	} else {
		if((field.DonationType[1].checked) && (field.DonationEvent.value=='')) {
			isErr = true; errMsg += '* The event your donation is supporting (for an event donation) \n';
		}
		if((field.DonationType[2].checked) && (field.CompanyName.value=='')) {
			isErr = true; errMsg += '* Your company name (for a corporate donation) \n';
		}
		if((field.DonationType[3].checked) && (field.inMemoryName.value=='')) {
			isErr = true; errMsg += '* The name of the person this donation is in memory of \n';
		}
	}
	if(field.Title.value=='') {
		isErr = true; errMsg += '* Your title \n';
	}
	if(field.FirstName.value=='') {
		isErr = true; errMsg += '* Your first name \n';
	}
	if(field.Surname.value=='') {
		isErr = true; errMsg += '* Your surname \n';
	}  
  features = features + ',left=' + xcorner + ',top=' + ycorner + ', height=' + txtHeight + ', width=' + txtWidth;
		} else {
		features = features + ', height=' + txtHeight + ', width=' + txtWidth;
	}
		window.open(theURL,winName,features);
}

function validateTellaFriend() {
	var isErr = false,errMsg = 'Some required information was either missing or incorrect. Please check the following:\n\n',field = document.frmTellaFriend;
	if(field.from_name.value=='') {
		isErr = true; errMsg += '* Your name \n';
	}
	if(field.from_email.value=='') {
		isErr = true; errMsg += '* Your email address \n';
	} else {
		if(!isEmail(field.from_email.value)){
			isErr = true; errMsg += '* Your email address must be a valid format \n';
		}
	}
	if(field.to_name.value=='') {
		isErr = true; errMsg += '* The name of the message recipient \n';
	}
	if(field.to_email.value=='') {
		isErr = true; errMsg += '* The email address of the message recipient \n';
	} else {
		if(!isEmail(field.to_email.value)){
			isErr = true; errMsg += '* The email address of the recipient must be a valid format \n';
		}
	}
	if(field.subject.value=='') {
		isErr = true; errMsg += '* The subject line of the message \n';
	}
	if(isErr) {
		alert(errMsg); 
		return false;
	} else {
		return true;
	}
}

function validateDonationForm() {
	var isErr = false,errMsg = 'Some required information was either missing or incorrect. Please check the following:\n\n',field = document.frmDonation;
	if(field.Web_Payment_Amount.value=='') {
		isErr = true; errMsg += '* No donation amount entered \n';
	} else if (field.Web_Payment_Amount.value < 0.0 ) {
		isErr = true; errMsg += '* negative donation amounts cannot be entered \n';
	}
	if(field.Email.value=='') {
		isErr = true; errMsg += '* Your email address \n';
	}
	if(field.Address.value=='') {
		isErr = true; errMsg += '* Your address \n';
	}
	if(field.State.value=='') {
		isErr = true; errMsg += '* Your state \n';
	}
	if(field.PostCode.value=='') {
		isErr = true; errMsg += '* Your postcode \n';
	}
	if(field.cardtype.value=='') {
		isErr = true; errMsg += '* Your credit card type \n';
	}
	if(field.CC_Name.value=='') {
		isErr = true; errMsg += '* The name appearing on your credit card \n';
	}
	if(field.Credit_Card_Main.value=='') {
		isErr = true; errMsg += '* Your credit card number \n';
	} else {
		if(!isInteger(field.Credit_Card_Main.value)) {
			isErr = true; errMsg += '* Your credit card number must be all numbers \n';
		}
	}
	if(field.Ecom_Card_Security_Code.value=='') {
		isErr = true; errMsg += '* Your credit card security code \n';
	}
	if(field.cardMonth.value=='') {
		isErr = true; errMsg += '* Your credit card expiry month \n';
	}
	if(field.cardYear.value=='') {
		isErr = true; errMsg += '* Your credit card expiry year \n';
	}
	if(isErr) {
		alert(errMsg); 
		return false;
	} else {
		return true;
	}
}

function validateChristmasDonationForm() {
	var isErr = false,errMsg = 'Some required information was either missing or incorrect. Please check the following:\n\n',field = document.frmDonation;
	if(field.Email.value=='') {
		isErr = true; errMsg += '* Your email address \n';
	}
	if(field.Address.value=='') {
		isErr = true; errMsg += '* Your address \n';
	}
	if(field.State.value=='') {
		isErr = true; errMsg += '* Your state \n';
	}
	if(field.PostCode.value=='') {
		isErr = true; errMsg += '* Your postcode \n';
	}
	if(field.cardtype.value=='') {
		isErr = true; errMsg += '* Your credit card type \n';
	}
	if(field.CC_Name.value=='') {
		isErr = true; errMsg += '* The name appearing on your credit card \n';
	}
	if(field.Credit_Card_Main.value=='') {
		isErr = true; errMsg += '* Your credit card number \n';
	} else {
		if(!isInteger(field.Credit_Card_Main.value)) {
			isErr = true; errMsg += '* Your credit card number must be all numbers \n';
		}
	}
	if(field.Ecom_Card_Security_Code.value=='') {
		isErr = true; errMsg += '* Your credit card security code \n';
	}
	if(field.cardMonth.value=='') {
		isErr = true; errMsg += '* Your credit card expiry month \n';
	}
	if(field.cardYear.value=='') {
		isErr = true; errMsg += '* Your credit card expiry year \n';
	}
	if(isErr) {
		alert(errMsg); 
		return false;
	} else {
		return true;
	}
}

function isInteger(strValue){
	var objRegExp  = /(^-?\d\d*$)/;
	return objRegExp.test(strValue);
}

function tidyCCNo(strValue){
	strValue = strValue.replace(/\./g,""); // remove dots
	strValue = strValue.replace(/-/g,""); // remove dashes
	strValue = strValue.replace(/\s/g,""); // remove spaces
	return strValue;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function isEmail(strValue){
	// var objRegExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
	var objRegExp = /^(\w+[\.|-])*\w+@(\w+[\.|-])*\w+\.\w{2,4}$/
	return objRegExp.test(strValue);
}

