function showHideByID(elementID){
	var currentStyle = document.getElementById(elementID).style;
	currentStyle.display = (currentStyle.display == 'none')? 'block' : 'none';
}

function resetForm(){
	document.getElementById('messages').innerHTML = "Simply fill in the details below and click \"Subscribe\" to subscribe to the WireITfree newsletter."	
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

function checkForm(){
	var fname = document.getElementById('fname').value;
	var lname = document.getElementById('lname').value;
	var email = document.getElementById('email').value;
	var errorDisp = document.getElementById('messages');
	errorDisp.innerHTML = "";
	
	
	if (window.XMLHttpRequest)
  	{// code for IE7+, Firefox, Chrome, Opera, Safari
  	  xmlhttp=new XMLHttpRequest();
  	}
    else
    {// code for IE6, IE5
  	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
	  xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      var response = xmlhttp.responseText;
	  if(response == "okay"){
		  errorDisp.innerHTML = "Thank you for subscribing, we will keep you updated with the latest news and offers!";
		  document.getElementById('subscribe_form').style.display = "none";
		  setTimeout('document.getElementById(\'subscribe_form_container\').style.display = "none"', 5000);
	  }
	  else if(response == "duplicate"){
		  errorDisp.innerHTML = "<img src=\"http://www.wireitfree.co.uk/images/warning.png\" /><font color=\"#FF0000\">It appears that the email address you entered is already subscribed, please rety with a different email address.</font>";
	  }
	  else{
		  errorDisp.innerHTML = "<img src=\"http://www.wireitfree.co.uk/images/warning.png\" /><font color=\"#FF0000\">There was an error with the submission, please try again later</font>";  
	  }
    }
}
	if(fname == "" || lname == "" || email == ""){
		errorDisp.innerHTML += "<img src=\"http://www.wireitfree.co.uk/images/warning.png\" /><font color=\"#FF0000\">You have not given all the details required, please fill in the details below and retry.</font><br />";
		errorDisp.style.display = "block";
	}
	else if(!echeck(email)){
		errorDisp.innerHTML += "<img src=\"http://www.wireitfree.co.uk/images/warning.png\" /><font color=\"#FF0000\">The email you have given is not valid, please give a valid email address. </font><br />";
		errorDisp.style.display = "block";
	}
	else {
			errorDisp.innerHTML = "<center>Submiting request... <br /><img src=\"images/loading.gif\" /></center>";
			errorDisp.style.display = "block";
			xmlhttp.open("GET","http://www.wireitfree.co.uk/subscribe.php?submit=true&fname="+fname+"&lname="+lname+"&email="+email,true);
			xmlhttp.send();
	}
}
