function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}
function submitContact(from) {
	if(from=='unsubscribe') {
		var email = $('email').value;
		var reason = $('reason').value;
		if(email!='') {
			$('send_button').className = 'send_button_submitted';
			var set_height = $('form_fields').offsetHeight;
			$('contact_form_cont').setStyle('height',set_height);
			$('form_fields').set('morph',{duration:800,onComplete: showSubmitting});
			$('form_fields').morph({opacity:0});
			var http = getHTTPObject();
			http.open("GET", "/includes/unSub.php?email="+email+"&reason="+reason, true);
			http.onreadystatechange = function(http) { if(this.readyState==4) { var textout = this.responseText; } };
			http.send(null);
		} else {
			alert('You must provide your email address to unsubscribe.  Please amend and try again.');
			return false;
		}
	} else { 
		var name = $('name').value;
		var phone = $('phone').value;
		var email = $('email').value;
		var subject = $('subject').value;
		var message = $('message').value;
		if(name!=''&&email!=''&&message!='') {
			$('send_button').className = 'send_button_submitted';
			var set_height = $('form_fields').offsetHeight;
			$('contact_form_cont').setStyle('height',set_height);
			$('form_fields').set('morph',{duration:800,onComplete: showSubmitting});
			$('form_fields').morph({opacity:0});
			var http = getHTTPObject();
			http.open("GET", "/includes/sendEmail.php?name="+name+"&phone="+phone+"&email="+email+"&subject="+subject+"&message="+message, true);
			http.onreadystatechange = function(http) { if(this.readyState==4) { var textout = this.responseText; } };
			http.send(null);
		} else {
			alert('You must provide at least a name, email and message to continue.  Please amend and try again.');
			return false;
		}
	}
}
function showSubmitting() {
	$('form_fields').style.display = 'none';
	$('successfully_sent').setStyle('opacity',0);
	$('successfully_sent').style.display = 'block';
	var set_height = $('successfully_sent').offsetHeight;
	$('successfully_sent').style.display = 'none';
	$('sending_request').setStyle('opacity',0);
	$('sending_request').style.display = 'block';
	$('contact_form_cont').set('morph',{duration:1000,onComplete: function() { setTimeout(showCompleted,2500); } });
	$('contact_form_cont').morph({height:set_height});
	$('sending_request').set('morph',{duration:800});
	$('sending_request').morph({opacity:1});
}
function showCompleted() {
	$('sending_request').set('morph',{
		duration:800,
		onComplete: function() {
			$('sending_request').style.display = 'none';
			$('successfully_sent').setStyle('opacity',0);
			$('successfully_sent').style.display = 'block';
			$('successfully_sent').set('morph',{duration:800});			
			$('successfully_sent').morph({opacity:1});
		}
	});
	$('sending_request').morph({opacity:0});
}
function keyCheck(check_type,obj,e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	switch(check_type) {
		case 'phone':
			var chrcodes_allowed = new Array(32,43,48,49,50,51,52,53,54,55,56,57);
			var keycodes_allowed = new Array(32,96,97,98,99,100,101,102,103,104,105,107);
		break;
		case 'email':
			var chrcodes_allowed = new Array(38,43,45,46,47,48,49,50,51,52,53,54,55,56,57,61,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,95,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122);
			var keycodes_allowed = new Array(58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,96,97,98,99,100,101,102,103,104,105,109,189,187,191);
		break;
	}
	if(keycodes_allowed.indexOf(keycode)<0) {
		var initial_string = obj.value;
		for(var v=0;v<obj.value.length;v++) {
			var chrcod = obj.value.charCodeAt(v);
			if(chrcodes_allowed.indexOf(chrcod)==-1) {
				obj.value = obj.value.split(initial_string.substr(v,1)).join('');
			}
		}
	}
}
function validateEmail(vEmail, vEmailValue) {
var err = 0;
if (vEmailValue=='') return true;
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (vEmailValue.indexOf(invalidChars.charAt(i),0) > -1) err = 1;
}
for (i=0; i<vEmailValue.length; i++) {
   if (vEmailValue.charCodeAt(i)>127) err = 1;
}

var atPos = vEmailValue.indexOf('@',0);
if (atPos == -1) err = 1;
if (atPos == 0)  err = 1;
if (vEmailValue.indexOf('@', atPos + 1) > - 1)  err = 1;
if (vEmailValue.indexOf('.', atPos) == -1)  err = 1;
if (vEmailValue.indexOf('@.',0) != -1)  err = 1;
if (vEmailValue.indexOf('.@',0) != -1)  err = 1;
if (vEmailValue.indexOf('..',0) != -1)  err = 1;
var suffix = vEmailValue.substring(vEmailValue.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum')  err = 1;
if(err)
	{
	alert("The email address you have tried to enter is invalid.  Please rectify to continue.");
	globalvar = vEmail;
	setTimeout("globalvar.focus()",250);
	return true;
	}
return true;
}
