var AllInputs = document.getElementsByTagName("input");
var AllText = document.getElementsByTagName("textarea");
var yname, business, telephone, email, message;
var ErrorCheck = 0;

for(var i = 0; i < AllInputs.length; i++){
	if(AllInputs[i].className == "submit"){
		AllInputs[i].onclick = function(){
			ErrorCheck = 0;
			
			unThrow(message);
			if(message.value == ""){
				throwError("message.", message);
			}
			
			unThrow(email);
			if(email.value == "" || email.value == "Email address"){
				throwError("email address.", email)
			}
			
			unThrow(telephone);
			if(telephone.value == "" || telephone.value == "Contact telephone number"){
				throwError("contact telephone number", telephone)
			}
			
			unThrow(business);
			if(business.value == "" || business.value == "Business name"){
				throwError("business name.", business)
			}
			
			unThrow(yname);
			if(yname.value == "" || yname.value == "Your name"){
				throwError("name.", yname)
			}
			
			if(ErrorCheck == 0){
				sendMessage(yname.value, business.value, telephone.value, email.value, message.value);
			}
		}
	}
	else{
		eval(AllInputs[i].name+" = AllInputs[i]");
	}
	
	if(AllInputs[i].className == "text"){
		if(AllInputs[i].value == "" || AllInputs[i].value == AllInputs[i].title){
			AllInputs[i].value = AllInputs[i].title;
			AllInputs[i].style.color = "#bbb";
		}
		AllInputs[i].onfocus = function(){
			if(this.value == this.title){
				this.value = "";
				if(this.style.color != "rgb(217, 0, 29)"){
					this.style.color = "#666";
				}
			}
		}
		AllInputs[i].onblur = function(){
			if(this.value == ""){
				this.value = this.title;
				if(this.style.color != "rgb(217, 0, 29)"){
					this.style.color = "#bbb";
				}
			}
		}
	}
}

for(var i = 0; i < AllText.length; i++){
	eval(AllText[i].name+" = AllText[i]");
	
	AllText[i].onfocus = function(){
		if(this.value == ""){
			if(this.style.color != "rgb(217, 0, 29)"){
				this.style.color = "#666";
			}
		}
	}
	AllText[i].onblur = function(){
		if(this.value == ""){
			this.value = this.title;
			if(this.style.color != "rgb(217, 0, 29)"){
				this.style.color = "#bbb";
			}
		}
	}
}

function throwError(value, obj){
	document.getElementById("confirm").style.display = "none";
	
	var Error = document.getElementById("error");
	Error.style.display = "block";
	if(obj){
		value = "Please enter your "+value;
	}
	Error.innerHTML = value;
	
	if(obj){
		colorFade(obj, 'background', 'f3f3f3', 'fbe5e8', 500, 30);
		obj.style.borderColor = "#ec7e8c";
		obj.style.color = "#d9001d";
	}
	
	ErrorCheck = 1;
}
function unThrow(obj){
	obj.style.background = "#f3f3f3";
	obj.style.borderColor = "#ccc";
	obj.style.color = "#666";
}

function sendMessage(name, business, telephone, email, message){
	document.getElementById("error").style.display = "none";
	
	var Confirm = document.getElementById("confirm");
	Confirm.style.display = "block";
	Confirm.style.textAlign = "center";
	Confirm.innerHTML = '<img src="images/loading.gif" alt="Loading..." />';
	
	var xmlhttp;
	if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}
	else{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var params = "name="+name+"&business="+business+"&telephone="+telephone+"&email="+email+"&message="+message;
	xmlhttp.open("POST", "send.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
	
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState == 4 && xmlhttp.responseText != ""){
			if(xmlhttp.responseText == "Success"){
				var Confirm = document.getElementById("confirm");
				Confirm.innerHTML = "Thank you for your enquiry.";
				Confirm.style.textAlign = "left";
			}
			else{
				throwError("There has been an error sending this message. Plese try again later.");
			}
		}
	}
}


var AllMenuA = document.getElementById("menu").getElementsByTagName("a");
var AllMenuDiv = document.getElementById("menu").getElementsByTagName("div");
var fadeCheck = new Array();
for(var i = 0; i < AllMenuA.length; i++){
	AllMenuDiv[i].innerHTML = AllMenuA[i].innerHTML;
	if(AllMenuA[i].id != "selected"){
		AllMenuA[i].id = "menu"+i;
		AllMenuA[i].onmouseover = function(){
			this.style.opacity = 0;
			fadeIn(this.id, 0);
		}
		AllMenuA[i].onmouseout = function(){
			this.style.backgroundPosition = "0 -42px";
			fadeOut(this.id);
		}
	}
}

var Submit = document.getElementById("submit");
if(Submit){
	Submit.onmouseover = function(){
		this.style.opacity = 0;
		fadeIn(this.id, 0);
	}
	Submit.onmouseout = function(){
		this.style.backgroundPosition = "0 40px";
		fadeOut(this.id);
	}
}

function fadeIn(obj, cur){
	var obj = document.getElementById(obj);
	cur = parseFloat(cur) + 0.1;
	obj.style.opacity = cur;
	obj.style.filter = "alpha(opacity = "+(cur*100)+")";
	if(cur < 1){
		fadeCheck[obj.id] = setTimeout("fadeIn('"+obj.id+"', '"+cur+"')", 30);
	}
	else{
		obj.style.opacity = 1;
		obj.style.filter = "alpha(opacity = 100)";
	}
}

function fadeOut(obj){
	var obj = document.getElementById(obj);
	if(fadeCheck[obj.id]){
		clearTimeout(fadeCheck[obj.id]);
	}
	var cur = parseFloat(obj.style.opacity) - 0.1;
	obj.style.opacity = cur;
	obj.style.filter = "alpha(opacity = "+(cur*100)+")";
	if(cur > 0){
		setTimeout("fadeOut('"+obj.id+"')", 30);
	}
	else{
		obj.style.opacity = 0;
		obj.style.filter = "alpha(opacity = 0)";
	}
}


//FADER SCRIPT - http://www.leigeber.com/2008/05/javascript-color-fading-script/
//Minor variations have been added to the script linked above.

// main function to process the fade request //
function colorFade(target,element,start,end,steps,speed) {
  var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
  steps = steps || 20;
  speed = speed || 20;
  clearInterval(target.timer);
  endrgb = colorConv(end);
  er = endrgb[0];
  eg = endrgb[1];
  eb = endrgb[2];
  if(!target.r) {
    startrgb = colorConv(start);
    r = startrgb[0];
    g = startrgb[1];
    b = startrgb[2];
    target.r = r;
    target.g = g;
    target.b = b;
  }
  rint = Math.round(Math.abs(target.r-er)/steps);
  gint = Math.round(Math.abs(target.g-eg)/steps);
  bint = Math.round(Math.abs(target.b-eb)/steps);
  if(rint == 0) { rint = 1 }
  if(gint == 0) { gint = 1 }
  if(bint == 0) { bint = 1 }
  target.step = 1;
  target.timer = setInterval( function() { animateColor(target,element,steps,er,eg,eb,rint,gint,bint) }, speed);
}

// incrementally close the gap between the two colors //
function animateColor(target,element,steps,er,eg,eb,rint,gint,bint) {
  var color;
  if(target.step <= steps) {
    var r = target.r;
    var g = target.g;
    var b = target.b;
    if(r >= er) {
      r = r - rint;
    } else {
      r = parseInt(r) + parseInt(rint);
    }
    if(g >= eg) {
      g = g - gint;
    } else {
      g = parseInt(g) + parseInt(gint);
    }
    if(b >= eb) {
      b = b - bint;
    } else {
      b = parseInt(b) + parseInt(bint);
    }
    color = 'rgb(' + r + ',' + g + ',' + b + ')';
    if(element == 'background') {
      target.style.background = color;
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
    target.r = r;
    target.g = g;
    target.b = b;
    target.step = target.step + 1;
  } else {
    clearInterval(target.timer);
    color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
    if(element == 'background') {
      target.style.background = color;
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
  }
}

// convert the color to rgb from hex //
function colorConv(color) {
  var rgb = [parseInt(color.substring(0,2),16), 
    parseInt(color.substring(2,4),16), 
    parseInt(color.substring(4,6),16)];
  return rgb;
}

function externalLinks(){
	if(!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for(var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
			anchor.target = "_blank";
		}
	}
}
externalLinks();
