function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;	
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function resetDivs() {
	document.getElementById('web').style.visibility = 'hidden';	
	document.getElementById('print').style.visibility = 'hidden';	
	document.getElementById('photo').style.visibility = 'hidden';
	document.getElementById('motion').style.visibility = 'hidden';
}

function init() {
	document.getElementById('closeThanks').onclick = function() {
        document.getElementById('thanks').style.display='none';
		return false; 
	}
	
	document.getElementById('webLink').onclick = function() {
		resetDivs();
		document.getElementById('web').style.visibility = 'visible';
		return false; 
	}
	document.getElementById('printLink').onclick = function() {
		resetDivs();
		document.getElementById('print').style.visibility = 'visible';
		return false; 
	}
	document.getElementById('photoLink').onclick = function() {
		resetDivs();
		document.getElementById('photo').style.visibility = 'visible';
		return false; 
	}
	document.getElementById('motionLink').onclick = function() {
		resetDivs();
		document.getElementById('motion').style.visibility = 'visible';
		return false; 
	}
	
	document.getElementById('allLink').onclick = function() {
		document.getElementById('web').style.visibility = 'visible';
		document.getElementById('print').style.visibility = 'visible';	
		document.getElementById('photo').style.visibility = 'visible';
		document.getElementById('motion').style.visibility = 'visible';
	}
	return false; 
}

function setBackgrounds() {
	if (!document.getElementsByTagName) return false;
	var pA = document.getElementById("projects").getElementsByTagName("a");
	var mA = document.getElementById("motion").getElementsByTagName("a");

	for (var i=0; i<pA.length; i++) {
		pAid = pA[i].id;
		pA[i].style.backgroundImage = "url(images/projects/"+pAid+".gif)";
		pA[i].style.top = Math.floor(Math.random()* 460)+"px";
		pA[i].style.left = Math.floor(Math.random()* 400)-110+"px";
		pA[i].href = "images/projects/"+pAid+"-full.jpg";
		
		num1 = parseInt(-10);
		num2 = parseInt(10);
		var generator = Math.random()*(num2-num1);
		generator = Math.round(num1+generator);
		
		pA[i].style.MozTransform = 'rotate('+generator+'deg)';
		
	}
	
	for (var k=0; k<mA.length; k++) {
		mAid = mA[k].id;
		mA[k].href= "flash/"+mAid+".html?KeepThis=true&TB_iframe=true&height=240&width=320";
	}
}

function writeFlash(src) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="320" height="240"><param name="movie" value="../flash/'+src+'.swf"><param name="quality" value="high"><param name="wmode" value="transparent"><embed src="../flash/'+src+'.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" width="320" height="240"></embed></object>');
}

// contact form
 
function CheckRequiredFields() {

		if (document.contactForm.name.value=='') {
			document.getElementById('errors').innerHTML = 'Your name is required.<br />How else will I know who is contacting me?';
			return false;
		}
				
		if (document.contactForm.email.value=='') {
			document.getElementById('errors').innerHTML = 'A valid email address is required<br />so that I will be able to write back to you.';
			return false;
		}
		
		if (document.contactForm.email.value!=document.contactForm.verify.value) {
			document.getElementById('errors').innerHTML = 'Please make sure your email addresses match<br />so I can verify you are not spam.';	
			return false;
		}

		return true;
}

// email check

function emailCheck (emailStr) {var checkTLD=1;var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;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) {alert("Email address seems incorrect (check @ and .'s)");return false;}var user=matchArray[1];var domain=matchArray[2];for (i=0; i<user.length; i++) {if (user.charCodeAt(i)>127) {alert("Ths username contains invalid characters.");return false;}}for (i=0; i<domain.length; i++) {if (domain.charCodeAt(i)>127) {alert("Ths domain name contains invalid characters.");return false;}}if (user.match(userPat)==null) {alert("The username doesn't seem to be valid.");return false;}var IPArray=domain.match(ipDomainPat);if (IPArray!=null) {for (var i=1;i<=4;i++) {if (IPArray[i]>255) {alert("Destination IP address is invalid!");return false;}}return true;}var atomPat=new RegExp("^" + atom + "$");var domArr=domain.split(".");var len=domArr.length;for (i=0;i<len;i++) {if (domArr[i].search(atomPat)==-1) {alert("The domain name does not seem to be valid.");return false;}}if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {alert("The address must end in a well-known domain or two letter " + "country.");return false;}if (len<2) {alert("This address is missing a hostname!");return false;}return true;}
// form functions end


addLoadEvent(setBackgrounds);
addLoadEvent(init);


