
var IDs = new Array("portfolio", "contact");

var nameClicked = false;
var emailClicked = false;
var questionClicked = false;

function fold(id) {
	if (document.getElementById(id).style.display != '') {
		foldAll();
		display(id);
	} else {
		conceal(id);
	}
}

function foldAll() {
	for (i=0; i<IDs.length; i++) {
		if (document.getElementById(IDs[i]).style.display == '')
			conceal(IDs[i]);
	}
}

function clearContent(id) {
	var element = document.getElementById(id);
	if (id == 'name' && !nameClicked) {
		nameClicked = true;
		element.value = '';
		element.style.color = '#444444';
		element.style.fontWeight = '';
	} else if (id == 'email' && !emailClicked) {
		emailClicked = true;
		element.value = '';
		element.style.color = '#444444';
		element.style.fontWeight = '';
	} else if (id == 'question' && !questionClicked) {
		questionClicked = true;
		element.value = '';
		element.style.color = '#444444';
		element.style.fontWeight = '';
	}
}

function resetForm() {
	document.contact_form.reset();
	for (i=0; i<document.contact_form.length; i++) {
		if (document.contact_form.elements[i].type != 'button') {
			document.contact_form.elements[i].style.color = '#BBBBBB';
			document.contact_form.elements[i].style.fontWeight = 'bold';
		}
	}
	nameClicked = false;
	emailClicked = false;
	questionClicked = false;
	hide('error');
	hide('success');
	hide('emailerror');
}

function validateForm() {
	var name = trim(document.getElementById('name').value);
	var email = trim(document.getElementById('email').value);
	var question = trim(document.getElementById('question').value);
	if (!nameClicked || name == '' || !emailClicked || email == '' || !questionClicked || question == '') {
		show('error');
		return;
	}
	if (!validateEmail(email)) {
		show('emailerror');
		return;
	}
	document.getElementById('name').value = name;
	document.getElementById('email').value = email;
	document.getElementById('question').value = question;
	document.contact_form.submit();
}

function trim(string) {
	return string.replace(/^\s+|^\n+|^\r+|^\t+|\s+$|\n+$|\r+$|\t+$/g, '');
}

function validateEmail(string) {
	if ( string == "-" || /^([a-zA-Z0-9._-]+)@([a-zA-Z0-9._-]+)\.([a-zA-Z]{2,4})$/.test(string) )
		return true;
	return false;
}

function show(id) {
	document.getElementById(id).style.visibility = 'visible';
}

function hide(id) {
	document.getElementById(id).style.visibility = 'hidden';
}

function display(id) {
	document.getElementById(id).style.display = '';
}

function conceal(id) {
	document.getElementById(id).style.display = 'none';
}

function year() {
	var year = new Date().getYear();
	if (year < 1900)
		year += 1900;
	return year;
}
