function addEvent (elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

// =================================================================

// CLEAR TEXT FROM FORM FIELDS
function addFormClear(id, defaultText) {	
	var el = document.getElementById(id);
	if (!el) {
		return;
	}
	if (el.value == '' || el.value == defaultText) {	
		el.value = defaultText;
		el.onfocus = function() {
			if (el.value == defaultText) {
				el.value = '';
			}
		}
		el.onblur = function() {
			if (el.value == '') {			
				el.value = defaultText;
			}
		}
	}
	var parent = el.parentNode
	while (parent != null) {
		if (parent.tagName == 'FORM') {
			parent.onsubmit = function() {
				if (el.value == defaultText) {
					el.value = '';
				}
			}
			break;
		}
		parent = parent.parentNode;
	}
}

// =================================================================

// DONATE BUTTON
function donate(){
	if(!document.getElementById('donateButton') && !document.getElementById('donateLink') && !document.getElementById('donateForm')) return false;
	var donateForm = document.getElementById('donateForm');
	var donateButton = document.getElementById('donateButton');
	var donateLink = document.getElementById('donateLink');
	donateButton.style.display = 'none';
	donateLink.style.display = 'block';
	donateLink.onclick = function(){
		donateForm.submit();
		return false;
	}
}

// =================================================================

// CONTACT FORM
function contactUsForm() {
	if (!document.getElementById('contactForm')) {
		return;
	}
		
	var contactForm = document.getElementById('contactForm');
	
	// validate
	contactForm.onsubmit = function() {
		
		// Firstname
		if (!document.getElementById('name').value) {
			alert('Please enter your Name');
			return false;
		}

		// Email
		var email = document.getElementById('email').value;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;		
		
		if (!email || email == 'Your Email Address') {
    		alert('Your Email Address is required');
    		return false;
	    } else if (!filter.test(email)) {
	    	alert('Your Email Address is not valid');
	    	return false;
	    }
		// Telephone
		if (!document.getElementById('phone').value) {
			alert('Please enter a telephone number');
			return false;
		}
	    // Subject
		if (!document.getElementById('subject').value) {
			alert('Please enter a Subject');
			return false;
		}
		// Message
		if (!document.getElementById('message').value) {
			alert('Please enter your Message');
			return false;
		}
	}
	    
}

// =================================================================

// add load events
addEvent(window,'load',donate);
addEvent(window,'load',contactUsForm);
// add text clear
addEvent(window,'load',function(){
	addFormClear('search', 'Search for ...');
});