// JavaScript Document
function Browser_Check(xmlHttp)
{ 	
	// Firefox, Opera 8.0+, Safari
	try	{ xmlHttp=new XMLHttpRequest(); }
	catch (e)
	{
		// Internet Explorer
		try	{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e)
		{
			try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function isBlank(s)
{
	var len=s.length;
	var i;
	for(i=0;i<len;++i)
	{
		if(s.charAt(i)!=" ") return false;
	}
	return true;
}

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 fun_mail_frm(field_name,val_name)
{
	if(eval('document.frm_mail.'+field_name).value=="") eval('document.frm_mail.'+field_name).value=val_name;
	else if(eval('document.frm_mail.'+field_name).value==val_name) eval('document.frm_mail.'+field_name).value="";
}

var SendMailHttp;

function ajax_send_email_show()
{
	if(SendMailHttp.readyState==4)
	{			
		if(SendMailHttp.status == 200 || SendMailHttp.status == 0)
		{ 
			var SubmitMsg=SendMailHttp.responseText;			
			document.getElementById("EmailDiv").style.display='block';
			document.getElementById("MsgDiv").style.display='block';
			document.getElementById("AjaxLoader").style.display='none';
			document.getElementById("user_name").value="Name";
			document.getElementById("email_address").value="Email Address";		
		}			
		else alert("Retrieval Error: " + SendMailHttp.statusText);
	}
}

function ajax_send_email()
{
	var UserName=document.getElementById("user_name").value;			
	var EmailAddress=document.getElementById("email_address").value;
	if(UserName=="Name" || isBlank(UserName))
	{
		alert('Please enter your name.');
		document.getElementById("user_name").value="";
		document.getElementById("user_name").focus();		
	}
	else if(EmailAddress=="Email Address" || isBlank(EmailAddress))
	{
		alert('Please enter your email address.');
		document.getElementById("email_address").value="";
		document.getElementById("email_address").focus();
	}
	else if(!echeck(EmailAddress))
	{
		alert('Please enter a valid email address.');
		document.getElementById("email_address").focus();
	}
	else
	{	
		document.getElementById("MsgDiv").style.display='none';
		document.getElementById("EmailDiv").style.display='none';
		document.getElementById("AjaxLoader").style.display='block';
			
		SendMailHttp=Browser_Check(SendMailHttp);
				
		var data = "UserName=" + UserName + "&EmailAddress=" + EmailAddress;
		var url='ajax_send_email.php';		
		
		SendMailHttp.open("POST",url,true);
		
		SendMailHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		SendMailHttp.setRequestHeader("Content-length", data.length);
		SendMailHttp.setRequestHeader("Connection", "close");
		
		SendMailHttp.onreadystatechange = ajax_send_email_show;
		
		SendMailHttp.send(data);
	}			
}
