var toEmail;

function showCallToAction(str)
{
	toEmail=str;
	setOpacity('mask',50);
	document.getElementById('mask').style.display='block';
	document.getElementById('callToAction').style.display='block';
	
	// fix for IE < 7
	if(isOldBrowser)
	{
		// IE 6
		if (document.documentElement && document.documentElement.scrollTop)
			theTop = document.documentElement.scrollTop;
		else if (document.body) // IE 5 or 5.5
			theTop = document.body.scrollTop;

		// window offsets
		document.getElementById('mask').style.top=theTop;
		document.getElementById('mask').style.width=screen.width;
		document.getElementById('mask').style.height=screen.height;		
		document.getElementById('callToAction').style.top=theTop+200;
		document.getElementById('callToActionSuccess').style.top=theTop+250;
	}
}

function hideCallToAction()
{
	document.getElementById('mask').style.display='none';
	document.getElementById('callToAction').style.zIndex='250';
	document.getElementById('callToAction').style.display='none';
	
	resetForm();
}

function resetErrors()
{
	// reset all errors
	var arr=document.getElementById('callToAction').getElementsByTagName('span');
	for(var i=0;i<arr.length;i++)
	{
		arr[i].style.fontWeight='normal';
		arr[i].style.color='#000000';
	}
}

function resetForm()
{
	var arr=document.getElementById('callToAction').getElementsByTagName('input');
	for(var i=0;i<arr.length;i++)
		if(arr[i].type == 'text')
			arr[i].value='';
	var arr=document.getElementById('callToAction').getElementsByTagName('textarea');
	for(var i=0;i<arr.length;i++)
		arr[i].value='';
	resetErrors();
	//reloadValidator();
}

function setOpacity(id,opacity)
{ 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity/101); 
    object.MozOpacity = (opacity/101); 
    object.KhtmlOpacity = (opacity/100); 
    object.filter = "alpha(opacity="+opacity+")"; 
} 

var successOpacity;
var maskOpacity;
var fadeInterval;
function fadeSuccess()
{
	if(successOpacity==maskOpacity)maskOpacity-=5;
	successOpacity-=5;
	
	if(successOpacity>100)return; // delay before fading
	
	setOpacity('mask',maskOpacity);
	setOpacity('callToActionSuccess',successOpacity);
	
	if(successOpacity==0)
	{
		clearInterval(fadeInterval);
		document.getElementById('mask').style.display='none';
		document.getElementById('callToActionSuccess').style.display='none';
	}
}

var http = navigator.appName == "Microsoft Internet Explorer"?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();

function submitForm()
{
	// disable call to action
	document.getElementById('callToAction').style.zIndex='0';

	resetErrors();

	// get all vars to pass
	var vars;
	vars='name='+document.form.name.value;
	vars+='&email='+document.form.email.value;
	vars+='&comments='+document.form.comments.value;
	vars+='&phone='+document.form.ph1.value+document.form.ph2.value+document.form.ph3.value;
	vars+='&toEmail='+toEmail;

	http.open('GET','callToAction/formSubmit.php?'+vars);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function handleResponse()
{
	if(http.readyState == 4)
	{
		var response = http.responseText;

		if(response=='done') // submission successful
		{
			// hide call to action
			document.getElementById('callToAction').style.zIndex='250';
			document.getElementById('callToAction').style.display='none';
			
			// show success
			document.getElementById('callToActionSuccess').style.display='block';
			setOpacity('callToActionSuccess',100);
			successOpacity=2000; // successOpacity>100 means a delay before fading
			maskOpacity=50;
			fadeInterval=setInterval('fadeSuccess()',1);
			resetForm();
		}
		else // invalid fields
		{
			// make invalid fields stand out
			var vars=response.split(',');
			for(var i=0;i<vars.length;i++)
			{
				document.getElementById(vars[i]).style.fontWeight='normal';
				document.getElementById(vars[i]).style.color='#ff0000';
			}

			// re-enable call to action
			document.getElementById('callToAction').style.zIndex='250';
		}
	}
}