var projects = false;
var cForm = null;
function initFuncs() {
//	dropDowns();
	SetupMenu();
	if(projects) {
		pullAllData();
	}
	if (document.getElementById('contactForm')) {
		var contact = document.getElementById('contactForm');
		var inputs = contact.getElementsByTagName('input');
		var area = contact.getElementsByTagName('textarea');	
		var sel = contact.getElementsByTagName('select');		
		for (var i=0;i<inputs.length;i++) {
			if (inputs[i].id == 'sbmtBtn') continue;
			BNM.addEvent(inputs[i],'focus',highlight);
			BNM.addEvent(inputs[i],'blur',unhighlight);		
		}
		for (var i=0;i<area.length;i++) {
			BNM.addEvent(area[i],'focus',highlight);
			BNM.addEvent(area[i],'blur',unhighlight);		
		}	
		for (var i=0;i<sel.length;i++) {
			BNM.addEvent(sel[i],'focus',highlight);
			BNM.addEvent(sel[i],'blur',unhighlight);		
		}						
	}	
	if(document.getElementById('clientLogin')) {
		var clientLink = document.getElementById('clientLogin').getElementsByTagName('A')[0];
		BNM.addEvent(clientLink,'click',cLogin);
	}
}

BNM.addEvent(window,'load',initFuncs);

function cLogin(e) {
	if (!e) var e = window.event;
	stopDefault(e);
	var target = (e.target) ? e.target: e.srcElement;	
	var form = buildLoginForm(null);
	var clientDiv = document.getElementById('clientLogin');	
	BNM.removeChildern(clientDiv);
	BNM.prependChild(clientDiv,form);	
}

function checkLogin(form) {
	var form = buildLoginForm();
	var clientDiv = document.getElementById('clientLogin');	
	var form = buildLoginForm(true);
	BNM.removeChildern(clientDiv);
	BNM.prependChild(clientDiv,form);
}
function buildLoginForm(error) {
	var form = document.createElement('form');
	form.setAttribute('method','post');
	form.setAttribute('action','#');
	form.setAttribute('onsubmit','return checkLogin(this)');
	form.setAttribute('id','cLoginForm');
	
	if(error) {
		var p = document.createElement('p');
		p.setAttribute('class','formError');
		var ptext = document.createTextNode('username/password incorrect!');
		p.appendChild(ptext);
		form.appendChild(p);
	}
	
	var uInput = document.createElement('input');
	uInput.setAttribute('type','text');
	uInput.setAttribute('size','15');
	uInput.setAttribute('id','cLoginFormUser');
	uInput.setAttribute('name','c[user]');
	uInput.setAttribute('value','username');
	uInput.setAttribute('tabindex','1');	
	form.appendChild(uInput);
	
	var uInput = document.createElement('input');
	uInput.setAttribute('type','text');
	uInput.setAttribute('size','15');
	uInput.setAttribute('id','cLoginFormPass');
	uInput.setAttribute('name','c[pass]');
	uInput.setAttribute('value','password');	
	uInput.setAttribute('tabindex','2');	
	form.appendChild(uInput);	
	
	var input = document.createElement('input');
	input.setAttribute('type','submit');
	input.setAttribute('value','login');	
	input.setAttribute('tabindex','3');		
	form.appendChild(input);
	
	return form;
}
/*
		<form method="post" action="#" onsubmit="return checkLogin(this);" id="cLoginForm">
			<p class="formError">username/password incorrect!</p>					
			<label for="cLoginFormUser">Username: 
				<input type="text" size="15" id="cLoginFormUser" name="c[user]" />
			</label>
			<label for="cLoginFormPass">Password:
				<input type="text" size="15" id="cLoginFormPass" name="c[pass]" />
			</label>
			<input type="submit" value="login" />
		</form>
		*/

function stopDefault(e) {
	if (e.preventDefault) {
		e.preventDefault();
	} else {
		e.returnValue = false;
	}	
}

// global variables for timeout and for current menu
var t=false,current;
function SetupMenu() {
	if (!document.getElementsByTagName) return;
	items=document.getElementsByTagName("li");
	for (i=0; i<items.length; i++) {
		if (items[i].className != "dd") continue;
		//set up event handlers
		thelink=findChild(items[i],"A");
		thelink.onmouseover=ShowMenu;
		thelink.onmouseout=StartTimer;
		//is there a submenu?
		if (ul=findChild(items[i],"UL")) {		
			ul.style.display="none";
			for (j=0; j<ul.childNodes.length; j++) {
				ul.childNodes[j].onmouseover=ResetTimer;
				ul.childNodes[j].onmouseout=StartTimer;
			}		
		}
	}
}
// find the first child object of a particular type
function findChild(obj,tag) {
	cn = obj.childNodes;
	for (k=0; k<cn.length; k++) {
		if (cn[k].nodeName==tag) return cn[k];
	}
	return false;
}
function ShowMenu(e) {
	if (!e) var e = window.event;
	// which link was the mouse over?
	thislink = (e.target) ? e.target: e.srcElement;
	ResetTimer();
	// hide the previous menu, if any
	if (current) HideMenu (current);
	// we want the LI, not the link
	thislink = thislink.parentNode;
	current=thislink;
	// find sun menu, if any
	ul = findChild(thislink,"UL");
	if (!ul) return;
	ul.style.display="block";
}
function HideMenu(thelink) {
	// find the submenu,if any
	ul = findChild(thelink,"UL");
	if (!ul) return;
	ul.style.display="none";
}
function ResetTimer() {
	if (t) window.clearTimeout(t);
}
function StartTimer() {
	t = window.setTimeout("HideMenu(current)",200);
}

function checkContact(form) {
	if (form.emailName.value == '') {
		alert('You must provide your name.');	
		form.emailName.className = 'formError';		
		form.emailName.focus();
		return false;
	}
	if ((form.emailEmail.value == '') || (form.emailEmail.value.indexOf('@') == -1)) {
		alert('You need to provide an email address.');	
		form.emailEmail.className = 'formError';		
		form.emailEmail.focus();
		return false;	
	}
	if (form.emailSubject.value == '') {
		alert('Please provide a subject line.');	
		form.emailSubject.className = 'formError';		
		form.emailSubject.focus();
		return false;	
	}	
	if (form.emailMsg.value == '') {
		alert('Please fill in a message.');	
		form.emailMsg.className = 'formError';		
		form.emailMsg.focus();
		return false;	
	}	
	if (form.captcha.value == '') {
		alert('You must answer the math validation question!');
		form.captcha.className = 'formError';
		form.captcha.focus();
		return false;
	}
	
	return true;
}

function highlight(e) {
	if (!e) e = window.event;
	var target = e.srcElement || e.target;
	target.style.background = '#b05f5f';	
}
function unhighlight(e) {
	if (!e) e = window.event;
	var target = e.srcElement || e.target;
	target.style.background = '#eee';	
	target.className = 'noerror';
}
