var xmlHttp;
var Effect;

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function validate_contact_form()
{
    
	msg = "";
    valid = true;

    if ( document.contact_form.message.value == "" )
    {
		msg = msg + "You must enter a message.\n";
        valid = false;
    }   
	
    if ( document.contact_form.contact_name.value == "" )
    {
		msg = msg + "You must enter your name.\n";
        valid = false;
    }   	
	
    if ( document.contact_form.captcha_code.value == "" )
    {
		msg = msg + "You must enter the security code.\n";		
        valid = false;
    }   	
	
    if ( document.contact_form.contact_email.value == "" )
    {
		msg = msg + "You must enter your email address.\n";		
        valid = false;
    }   		

	
	if (document.contact_form.contact_email.value != "")
	{
	
		apos = document.contact_form.contact_email.value.indexOf("@");
		dotpos = document.contact_form.contact_email.value.lastIndexOf(".");
		
		if (apos < 1 || dotpos - apos < 2)
		{
			msg = msg + "The email address entered is not valid.\n";		
			valid = false;
		}
		
	}
	
/*
	if(document.contact_form.email.value == "" && document.contact_form.tel.value == "")
    {
		msg = msg + "You must enter either your telephone number or email address.\n";		
        valid = false;
    }   	

*/
	if(valid == false)       
	{
        alert (msg);		
	}
	   

    return valid;
	
}

function isEmailValid(email)
{
	valid = true;
	
	apos = email.indexOf("@");
	dotpos = email.lastIndexOf(".");
	
	if (apos<1||dotpos-apos<2) 
	{
        valid = false;		
	}		
	
	return valid;
	
}


function sendRequestForm()
{

	if(  document.request_form.name.value == "" ||  document.request_form.request.value == "")
	{
		alert("Please make sure all fields are complete.");
	}
	else if(document.request_form.emailme.checked == 1 && (document.request_form.email.value == "" || !isEmailValid(document.request_form.email.value)))
	{
/*
		if(document.request_form.email.value == "" || !isEmailValid(document.request_form.email.value))
		{
*/
			alert("Please make sure the provided email address is valid."); 
		/*
}
*/
	}
	else
	{
		
		var name = document.request_form.name.value;			
		
		if(document.request_form.emailme.checked == 1)
		{
			var email = document.request_form.email.value;			
			document.getElementById("email").disabled = "disabled";
		}
		else
		{
			var email = "Not specified";
		}		

		var request = document.request_form.request.value;
		
		document.getElementById("name").disabled = "disabled";
		document.getElementById("emailme").disabled = "disabled";
		document.getElementById("request").disabled = "disabled";
		document.getElementById("send_request").style.backgroundImage = "url('../images/button_sending.gif')";
		

		xmlHttp = GetXmlHttpObject()
	
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
		
		var url = "../php/ajax/sendRequestForm.php?name=" + name;
		url = url + "&email=" + email;
		url = url + "&request=" + request;				
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = requestSent;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);

	
	}	
}



function requestSent() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("request_form").innerHTML=xmlHttp.responseText;
	}
}



function validate_search_form()
{
    

    valid = true;

    if ( document.search_form.phrase.value == "" || document.search_form.phrase.value == "Search free resources..." )
    {
        valid = false;
    }   
	
    
	if(valid == false)       
	{
        alert ("You didn't tell us what you're looking for...");		
	}
	   

    return valid;
	
}


function scrollToElement(element_id, highlight)
{
	new Effect.ScrollTo(element_id);	
	
	if(highlight == 1)	
	{
		//document.getElementById(element_id).style.border = "2px solid blue";
		Effect.Shake(element_id);
	}

	
}


function subscribeToMailing()
{

	if(document.mailing_list_form.subscribe_email.value == "" || !isEmailValid(document.mailing_list_form.subscribe_email.value))
	{
		alert("Please make sure you provide a valid email address."); 
	}
	else
	{

		var email = document.mailing_list_form.subscribe_email.value;			
		document.getElementById("subscribe_email").disabled = "disabled";

		document.getElementById("subscribe").style.backgroundImage = "url('../images/button_sending.gif')";
		

		xmlHttp = GetXmlHttpObject()
	
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
		
		var url = "../php/ajax/subscribeToMailing.php?email=" + email;						
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange = subscribedToMailing;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);

	
	}	
}



function subscribedToMailing() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("mailing_list").innerHTML=xmlHttp.responseText;
	}
}
