function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
function validate_form( )
{
    valid = true;

    if ( trim(document.login.AgencyID.value) == "" )
    {
        alert ( "Please enter the Agency ID." );
        document.login.AgencyID.focus
        valid = false;
	    return valid;
    }
    if ( trim(document.login.UsrName.value) == "" )
    {
        alert ( "Please enter the User name." );
        document.login.UsrName.focus
        valid = false;
	    return valid;
    }
    if ( trim(document.login.Pasword.value) == "" )
    {
        alert ( "Please enter the Password." );
        document.login.Pasword.focus
        valid = false;
	    return valid;
    }
    return valid;
}

function validate_email_form()
{
	valid = true;
	err_msg = '';
	elem = '';
	if ( trim(document.QuickEmail.mailFrom.value) == '' )
	{
		err_msg += 'Please enter your email ID.\n';
	    valid = false;
	    elem='mailFrom';
	}
	else
	{
		xyz=document.QuickEmail.mailFrom.value;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(xyz)){
			err_msg += 'Invalid Email ID. Please enter correct email ID.'; 
		    valid = false;
		    elem='mailFrom';
		}
	}
	if ( trim(document.QuickEmail.mailMessage.value) == '' )
	{
		err_msg += 'Please enter the message.\n';
	    valid = false;
	    elem='mailMessage';
	}
	if ( trim(document.QuickEmail.mailSubject.value) == '' )
	{
		err_msg += 'Please enter the Subject.\n';
	    valid = false;
	    elem='mailMessage';
	}
	if ( trim(document.QuickEmail.mailFirstname.value) == '' )
	{
		err_msg += 'Please enter your First name.\n';
	    valid = false;
	    elem='mailMessage';
	}
	if ( trim(document.QuickEmail.mailSurname.value) == '' )
	{
		err_msg += 'Please enter your Surname.\n';
	    valid = false;
	    elem='mailMessage';
	}
	if ( trim(document.QuickEmail.mailContactNumber.value) == '' )
	{
		err_msg += 'Please enter your Contact Number.\n';
	    valid = false;
	    elem='mailMessage';
	}
	if (err_msg != '')
	{
		alert(err_msg);
		document.getElementById(elem).focus();		
	    return valid;
	}	
	SendMsg();
}

var req;
function SendMsg()
{
    var mailFrom = document.getElementById("mailFrom");
    var mailMessage = document.getElementById("mailMessage");
    var mailSubject = document.getElementById("mailSubject");
    var mailFirstname = document.getElementById("mailFirstname");
    var mailSurname = document.getElementById("mailSurname");
    var mailContactNumber = document.getElementById("mailContactNumber");
    
    var url = "quickmail.asp";
    if (typeof XMLHttpRequest != "undefined") 
    {
       req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("POST", url, true);
    req.onreadystatechange = callback;
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send("function=login&mailFrom=" + encodeURIComponent(mailFrom.value) + "&mailContactNumber=" + encodeURIComponent(mailContactNumber.value) 
			+ "&mailMessage=" + encodeURIComponent(mailMessage.value)+ "&mailSubject=" + encodeURIComponent(mailSubject.value)
			+ "&mailFirstname=" + encodeURIComponent(mailFirstname.value)+ "&mailSurname=" + encodeURIComponent(mailSurname.value));
	document.getElementById("divQuickEmail").innerHTML="<br><br><b>Please wait.. Your email is being sent.</b>";
}

function callback()
{
    if (req.readyState == 4) 
    {
        if (req.status == 200) 
        {
            parseMessage();
        }
    }
}

function parseMessage() 
{
    if (req.responseXML.getElementsByTagName("message").length > 0)
    {    
        var message = req.responseXML.getElementsByTagName("message")[0];
        document.getElementById("divQuickEmail").innerHTML = "<br><br><b>" + message.childNodes[0].nodeValue + "</b>";
    }
}
