// JavaScript Document

function formSubmit(formName){
	var tempName = document.getElementById(formName);
	tempName.submit();
}


function hideDiv(divName){
	tempDiv = document.getElementById(divName);
	tempDiv.style.display="none";
}

function showDiv(divName){
	var tempDiv = document.getElementById(divName);
	tempDiv.style.display="inline";

}

function selectForm(itemName){
	tempName = document.getElementById(itemName);
	tempName.className='selected';
}

function deselectForm(itemName){
	var tempName = document.getElementById(itemName);
	tempName.className='';

}

function contact(){
	empty = false;
	fm 	= document.contact_form;
	checker(fm.name,"Please enter your Name");
	checker(fm.email,"Please enter your E-mail address");
	checker(fm.message,"Please enter a message");
	email_validator(fm.email)
	if (empty == false){document.contact_form.submit()}
}

function checker(obj,msg){
	if(obj.value=="" && empty==false){
		alert(msg);empty=true;obj.focus()
	}
}
	
function email_validator(obj){
	var emailStr=obj.value;var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)!£$%^&*()+='#~?<>@,;:\\\\\\\"\\.\\[\\]";var validChars="\[^\\s" + specialChars + "\]";var quotedUser="(\"[^\"]*\")";var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+';var word="(" + atom + "|" + quotedUser + ")";var userPat=new RegExp("^" + word + "(\\." + word + ")*$");var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");var matchArray=emailStr.match(emailPat)
	if ((matchArray==null) && empty==false){message("Email address seems incorrect (check @ and .'s)",obj)}
	if (empty==false){var user=matchArray [1];var domain=matchArray [2];if (user.match(userPat)==null){message("The username doesn't seem to be valid.",obj)}}
	if (empty==false){var IPArray=domain.match(ipDomainPat);if (IPArray!=null){for (var j=1;j<=4;j++){if (IPArray[j]>255){message("Destination IP address is invalid!",obj)}}}}
	if (empty==false){var domainArray=domain.match(domainPat);if (domainArray==null){message("The domain name doesn't seem to be valid.",obj)}}
	if (empty==false){var atomPat=new RegExp(atom,"g");var domArr=domain.match(atomPat);var len=domArr.length;if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3){message("The address must end in a three-letter domain, or two letter country.",obj)}}
	if ((len<2) && empty==false){message("This address is missing a hostname!",obj)}
}

function message(msg,obj){
	alert(msg);empty=true;obj.focus()
}

function editportfolio(){
	empty = false;
	fm 	= document.edit_form;
	checker(fm.site,"Please enter a Site name");
	checker(fm.url,"Please enter a site URL");
	//if(fm.imgfile.value=="" && fm.imgcurr.value=="" && empty==false){alert("Please enter an image");empty=true;obj.focus()}
	if (empty == false){document.edit_form.submit()}
}

function del_portfolio(id)
	{ok=window.confirm('Click OK to delete this record');if (ok==true){location.href=('/admin/portfolio.php?action=delete&id='+id)}}
	
	
this.sitemapstyler = function(){
	var sitemap = document.getElementById("sitemap")
	if(sitemap){
		
		this.listItem = function(li){
			if(li.getElementsByTagName("ul").length > 0){
				var ul = li.getElementsByTagName("ul")[0];
				ul.style.display = "none";
				var span = document.createElement("span");
				span.className = "collapsed";
				span.onclick = function(){
					ul.style.display = (ul.style.display == "none") ? "block" : "none";
					this.className = (ul.style.display == "none") ? "collapsed" : "expanded";
				};
				li.appendChild(span);
			};
		};
		
		var items = sitemap.getElementsByTagName("li");
		for(var i=0;i<items.length;i++){
			listItem(items[i]);
		};
		
	};	
};

window.onload = sitemapstyler;
