// workaround for absence of target attributes in xhtml strict

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.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}



function toggle(id) {
   var item = document.getElementById(id);

   if (item.style.display == "block")
      item.style.display = "none";
   else
      item.style.display = "block";
}



function validateAdmin() {
	var aMsg = new Array();
	var aMandatory = new Array('admUsername','admEmail','admFirstName','admLastName');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fUsername = document.getElementById('admUsername');
	var sUsername = fUsername.value;
	if (sUsername != '' && (sUsername.length < 4 || sUsername.length > 16)) {
		aMsg[aMsg.length] = 'Username should be between 4 to 16 characters';
		highlightLabel(fUsername.previousSibling);
	}
	else if (sUsername != '' && !validateAlphanumeric(sUsername)) {
		aMsg[aMsg.length] = 'Username should contain only letters and numbers';
		highlightLabel(fUsername.previousSibling);
	}

	var fPassword1 = document.getElementById('admPassword1');
	var sPassword1 = fPassword1.value;
	if (sPassword1 != '' && (sPassword1.length < 8 || sPassword1.length > 32)) {
		aMsg[aMsg.length] = 'Password should be between 8 to 32 characters';
		highlightLabel(fPassword1.previousSibling);
	}

	var fPassword2 = document.getElementById('admPassword2');
	var sPassword2 = fPassword2.value;
	if (sPassword2 != sPassword1) {
		aMsg[aMsg.length] = 'Passwords do not match';
		highlightLabel(fPassword2.previousSibling);
	}

	var fEmail = document.getElementById('admEmail');
	var sEmail = fEmail.value;
	if (sEmail != '' && sEmail.length > 255) {
		aMsg[aMsg.length] = 'Email should be less than 255 characters';
		highlightLabel(fEmail.previousSibling);
	}
	else if (sEmail != '' && !validateEmail(sEmail)) {
		aMsg[aMsg.length] = 'Email is invalid';
		highlightLabel(fEmail.previousSibling);
	}

	var fFirstName = document.getElementById('admFirstName');
	var sFirstName = fFirstName.value;
	if (sFirstName != '' && sFirstName.length > 64) {
		aMsg[aMsg.length] = 'First Name should be less than 64 characters';
		highlightLabel(fFirstName.previousSibling);
	}

	var fLastName = document.getElementById('admLastName');
	var sLastName = fLastName.value;
	if (sLastName != '' && sLastName.length > 64) {
		aMsg[aMsg.length] = 'Last Name should be less than 64 characters';
		highlightLabel(fLastName.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateProgramme(type) {
	var aMsg = new Array();
	if (type == 'all')
		var aMandatory = new Array('progTitle','progDate','progTime','progLocation','progImgFilename','progShortDesc','progLongDesc');
	else
		var aMandatory = new Array('progTitle','progDate','progTime','progLocation','progShortDesc','progLongDesc');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fTitle = document.getElementById('progTitle');
	var sTitle = fTitle.value;
	if (sTitle != '' && sTitle.length > 255) {
		aMsg[aMsg.length] = 'Title should be less than 255 characters';
		highlightLabel(fTitle.previousSibling);
	}

	var fDate = document.getElementById('progDate');
	var sDate = fDate.value;
	if (sDate != '' && !validateYMD(sDate)) {
		aMsg[aMsg.length] = 'Date should be in yyyy-mm-dd format';
		highlightLabel(fDate.previousSibling);
	}

	var fTime = document.getElementById('progTime');
	var sTime = fTime.value;
	if (sTime != '' && sTime.length > 255) {
		aMsg[aMsg.length] = 'Time should be less than 255 characters';
		highlightLabel(fTime.previousSibling);
	}

	var fLocation = document.getElementById('progLocation');
	var sLocation = fLocation.value;
	if (sLocation != '' && sLocation.length > 255) {
		aMsg[aMsg.length] = 'Location should be less than 255 characters';
		highlightLabel(fLocation.previousSibling);
	}

	var fImg = document.getElementById('progImgFilename');
	var sImg = fImg.value;
	if (sImg != '' && !validateImgExt(sImg)) {
		aMsg[aMsg.length] = 'Image to be uploaded should be a gif, jpg, or png file';
		highlightLabel(fImg.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateProgrammeImage(type) {
	var aMsg = new Array();
	if (type == 'all')
		var aMandatory = new Array('piFilename');
	else
		var aMandatory = new Array();
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fImg = document.getElementById('piFilename');
	var sImg = fImg.value;
	if (sImg != '' && !validateImgExt(sImg)) {
		aMsg[aMsg.length] = 'Image to be uploaded should be a gif, jpg, or png file';
		highlightLabel(fImg.previousSibling);
	}

	var fCaption = document.getElementById('piCaption');
	var sCaption = fCaption.value;
	if (sCaption != '' && sCaption.length > 255) {
		aMsg[aMsg.length] = 'Image caption should be less than 255 characters';
		highlightLabel(fCaption.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateHighlight(type) {
	var aMsg = new Array();
	if (type == 'all')
		var aMandatory = new Array('hlDate','hlTitle','hlImgFilename','hlText','progID');
	else
		var aMandatory = new Array('hlDate','hlTitle','hlText','progID');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fDate = document.getElementById('hlDate');
	var sDate = fDate.value;
	if (sDate != '' && !validateYMD(sDate)) {
		aMsg[aMsg.length] = 'Date should be in yyyy-mm-dd format';
		highlightLabel(fDate.previousSibling);
	}

	var fTitle = document.getElementById('hlTitle');
	var sTitle = fTitle.value;
	if (sTitle != '' && sTitle.length > 255) {
		aMsg[aMsg.length] = 'Title should be less than 255 characters';
		highlightLabel(fTitle.previousSibling);
	}

	var fImg = document.getElementById('hlImgFilename');
	var sImg = fImg.value;
	if (sImg != '' && !validateImgExt(sImg)) {
		aMsg[aMsg.length] = 'Image to be uploaded should be a gif, jpg, or png file';
		highlightLabel(fImg.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateTag() {
	var aMsg = new Array();
	var aMandatory = new Array('confName','confDisplay');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fName = document.getElementById('confName');
	var sName = fName.value;
	if (sName != '' && sName.length > 32) {
		aMsg[aMsg.length] = 'System Name should be less than 32 characters';
		highlightLabel(fName.previousSibling);
	}
	else if (sName != '' && !validateAlphanumeric(sName)) {
		aMsg[aMsg.length] = 'System Name should contain only letters and numbers';
		highlightLabel(fName.previousSibling);
	}

	var fDisplay = document.getElementById('confDisplay');
	var sDisplay = fDisplay.value;
	if (sDisplay != '' && sDisplay.length > 255) {
		aMsg[aMsg.length] = 'Display Name should be less than 255 characters';
		highlightLabel(fDisplay.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateNewsMedia(type) {
	var aMsg = new Array();
	if (type == 'all')
		var aMandatory = new Array('newsDate','newsTitle','newsFilename');
	else
		var aMandatory = new Array('newsDate','newsTitle');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fDate = document.getElementById('newsDate');
	var sDate = fDate.value;
	if (sDate != '' && !validateYMD(sDate)) {
		aMsg[aMsg.length] = 'Date should be in yyyy-mm-dd format';
		highlightLabel(fDate.previousSibling);
	}

	var fTitle = document.getElementById('newsTitle');
	var sTitle = fTitle.value;
	if (sTitle != '' && sTitle.length > 255) {
		aMsg[aMsg.length] = 'Title should be less than 255 characters';
		highlightLabel(fTitle.previousSibling);
	}

	var fFile = document.getElementById('newsFilename');
	var sFile = fFile.value;
	if (sFile != '' && !validatePdfExt(sFile)) {
		aMsg[aMsg.length] = 'File to be uploaded should be a PDF file';
		highlightLabel(fFile.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateAlbum() {
	var aMsg = new Array();
	var aMandatory = new Array('albDate','albName');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fDate = document.getElementById('albDate');
	var sDate = fDate.value;
	if (sDate != '' && !validateYMD(sDate)) {
		aMsg[aMsg.length] = 'Date should be in yyyy-mm-dd format';
		highlightLabel(fDate.previousSibling);
	}

	var fName = document.getElementById('albName');
	var sName = fName.value;
	if (sName != '' && sName.length > 255) {
		aMsg[aMsg.length] = 'Album name should be less than 255 characters';
		highlightLabel(fName.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateImage(type) {
	var aMsg = new Array();
	if (type == 'all')
		var aMandatory = new Array('imgFilename');
	else
		var aMandatory = new Array();
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fImg = document.getElementById('imgFilename');
	var sImg = fImg.value;
	if (sImg != '' && !validateImgExt(sImg)) {
		aMsg[aMsg.length] = 'Image to be uploaded should be a gif, jpg, or png file';
		highlightLabel(fImg.previousSibling);
	}

	var fCaption = document.getElementById('imgCaption');
	var sCaption = fCaption.value;
	if (sCaption != '' && sCaption.length > 255) {
		aMsg[aMsg.length] = 'Image caption should be less than 255 characters';
		highlightLabel(fCaption.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validatePagin(iPageCnt) {
	var fP = document.getElementById('p');
	var sP = fP.value;
	if (sP != '' && validateNumber(sP) && sP > 0 && sP <= iPageCnt)
		return true;
	else
		return false;
}



function validateEmail(email) {
	var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (regex.test(email))
		return true;
	else
		return false;
}

function validatePdfExt(filename) {
	var dotIndex = filename.lastIndexOf('.');
	var extension = '';

	if (dotIndex != -1) {
		extension = filename.substring(dotIndex+1);
		extension = extension.toLowerCase();
		if (extension == 'pdf')
			return true;
		else
			return false;
	}
	else
		return false;
}

function validateImgExt(filename) {
	var dotIndex = filename.lastIndexOf('.');
	var extension = '';

	if (dotIndex != -1) {
		extension = filename.substring(dotIndex+1);
		extension = extension.toLowerCase();
		if (extension == 'jpg' || extension == 'jpeg' || extension == 'gif' || extension == 'png')
			return true;
		else
			return false;
	}
	else
		return false;
}

function validateAlphanumeric(txt) {
	var regex = /^[a-zA-Z0-9]+$/;

	if (regex.test(txt))
		return true;
	else
		return false;
}

function validateNumber(txt) {
	var regex = /^\d+$/;

	if (regex.test(txt))
		return true;
	else
		return false;
}

function validateYMD(txt) {
	var regex = /^\d\d\d\d-\d\d-\d\d$/;

	if (regex.test(txt))
		return true;
	else
		return false;
}



function resetLabels() {
	var labels = document.getElementsByTagName('label');
	var currLabel = '';

	for (var i = 0; i < labels.length; i++) {
		currLabel = labels[i];
		currLabel.style.color = '';
	}
}

function highlightLabel(label) {
	if (label.nodeName.toLowerCase() == 'label') {
		label.style.color = 'red';
	}
}



// equalize heights of left/right floated divs
// add 20 (px) to compensate for bottom padding

/*
Derived from a script by Alejandro Gervasio.
Modified to work in FireFox by Stefan Mischook for Killersites.com

How it works: just apply the CSS class of 'column' to your pages' main columns.
*/
matchColumns=function(){

     var divs,contDivs,maxHeight,divHeight,d;

     // get all <div> elements in the document
     divs=document.getElementsByTagName('div');
     contDivs=[];

     // initialize maximum height value
     maxHeight=0;

     // iterate over all <div> elements in the document
     for(var i=0;i<divs.length;i++){

          // make collection with <div> elements with class attribute 'container'
          if(/\bcolumn\b/.test(divs[i].className)){
                d=divs[i];
                contDivs[contDivs.length]=d;

                // determine height for <div> element
                if(d.offsetHeight){
                     divHeight=d.offsetHeight;
                }

                else if(d.style.pixelHeight){
                     divHeight=d.style.pixelHeight;
                }

                // calculate maximum height
                maxHeight=Math.max(maxHeight,divHeight);
          }
     }

     // assign maximum height value to all of container <div> elements
     for(var i=0;i<contDivs.length;i++){
          contDivs[i].style.height=(maxHeight) + "px";
     }
}



// eMail Obfuscator Script 1.31 by Tim Williams - freeware

function obfuscate(type) {

var obfuscated = '';

if (type == 'adam')

obfuscated = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,
65,100,97,109,95,84,69,79,64,109,99,121,115,46,103,111,118,46,115,103,
34,62,
65,100,97,109,95,84,69,79,64,109,99,121,115,46,103,111,118,46,115,103,
60,47,97,62);

document.write(obfuscated);

}



// run all necessary functions on page load

function loadAll() {
	externalLinks();
	matchColumns();
}
window.onload = loadAll;
