// JavaScript Document
function newwindow(url) {
	// Set width and height
	var winWidth = 300;
	var winHeight = 400;
	// Set Window position
	var winTop = 0;
	var winLeft = 0;
	// Set other attributes
	var toolbar = "no";
	var location = "no";
	var directories = "no";
	var status = "no";
	var menubar = "no";
	var scrollbars = "no";
	var resizable = "no";
	var atts="width="+winWidth+",height="+winHeight+",top="+winTop+",screenY="+winTop+",left="+winLeft+", screenX="+winLeft+", scrollbars="+scrollbars+"";
	// open the window
	window.open(url,'',atts);
}

function newwindow_image(url,width,height) {
	// Set width and height
	var winWidth = width;
	var winHeight = height;
	// Set Window position
	var winTop = 0;
	var winLeft = 0;
	// Set other attributes
	var toolbar = "no";
	var location = "no";
	var directories = "no";
	var status = "no";
	var menubar = "no";
	var scrollbars = "no";
	var resizable = "no";
	var atts="width="+winWidth+",height="+winHeight+",top="+winTop+",screenY="+winTop+",left="+winLeft+", screenX="+winLeft+", scrollbars="+scrollbars+"";
	// open the window
	window.open(url,'',atts);
}

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";
 }
}
window.onload = externalLinks;


function colourTextBox(field) {
	field.className = 'textBoxFocus';
}

function restoreTextBox(field) {
	field.className = 'textBox';
}

function validateContactForm(contactForm) {
	name    = contactForm.name.value;
	email   = contactForm.email.value;
	subject = contactForm.subject.value;
	message = contactForm.message.value;
	
	if (name == "") {
		alert("Please enter a name!");
		contactForm.name.focus();
		return false;
	}
	
	email = email.trim();
	if (email == "") {
		alert("Please enter a correct email address!");
		conactForm.email.focus();
		return false;
	} else {
		var emailRegxp = /^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$/
		if (emailRegxp.test(email) != true) {
			alert("Please enter a valid email address");
			return false;
		}
	}
	
	if (subject == "") {
		alert("Please enter a subject!");
		contactForm.subject.focus();
		return false;
	}
	
	if (message == "") {
		alert("Please enter a message!");
		contactForm.message.focus();
		return false;
	}
}

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

function validateAccountEdit(form) {	
	email		= form.email.value;
	curpass     = form.curpass.value;
	newpass     = form.newpass.value;
	
	email = email.trim();
	
	if (email == "") {
		alert("Please enter an email address!");
		form.email.focus();
		return false;
	} else {
		var emailRegxp = /^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$/
		if (emailRegxp.test(email) != true) {
			alert("Please enter a valid email address");
			form.email.focus();
			return false;
		}
	}
	
	if (curpass && (!newpass) ) {
		alert("If you wish to change your password, please supply a new password. Otherwise, leave both fields blank.");
		form.curpass.focus();
		return false;
	}
	
}

function validateLogin(form) {
	user		= form.user.value;
	pass     	= form.pass.value;
	
	if (user == "") {
		alert("Please enter a username!");
		form.user.focus();
		return false;
	}
	
	if (pass == "") {
		alert("Please enter a password!");
		form.pass.focus();
		return false;
	}
}

function validateCreateUser(form) {
	user		= form.user.value;
	email     	= form.email.value;
	
	if (user == "") {
		alert("Please enter a username!");
		form.user.focus();
		return false;
	}
	
	email = email.trim();
	
	if (email == "") {
		alert("Please enter an email address!");
		form.email.focus();
		return false;
	} else {		
	///^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
		var emailRegxp = /^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$/
		if (emailRegxp.test(email) != true) {
			alert("Please enter a valid email address");
			form.email.focus();
			return false;
		}
	}
}

function validateAddCat(form) {
	cat_name	= form.cat_name.value;
	
	if (cat_name == "") {
		alert("Please enter a category!");
		form.cat_name.focus();
		return false;
	}
	
}

function validateEditCat(form) {
	cat_name	= form.cat_name.value;
	
	if (cat_name == "") {
		alert("Please enter a category!");
		form.cat_name.focus();
		return false;
	}
	
}

function validateAddImg(form) {
	upload	= form.upload.value;
	cat		= form.cat.value;
	
	
	if (upload == "") {
		alert("Please select an image to be uploaded!");
		form.upload.focus();
		return false;
	}
	
	if (cat == "null") {
		alert("You must select a category!");	
		form.cat.focus();
		return false;
	}
	

}

function validateForgotPass(form) {
	user	= form.user.value;	
	
	if (user == "") {
		alert("Please enter a username!");
		form.user.focus();
		return false;
	}
	
}

sfHover = function() {
	var sfEls = document.getElementById("nav").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);