
function echeck() {

    var email = document.getElementById('add');
    var str = email.value;
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

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

 		 talktoServer();					
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }

function talktoServer(){
    
    var req = newXMLHttpRequest();
    //register the callback handler function
    var callbackHandler = getReadyStateHandler(req, updateMsgOnBrowser);
    req.onreadystatechange = callbackHandler;
    req.open("POST", "add.php", true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //get the value from the text input element and send it to server
    var email = document.getElementById("add");
    var msg_value = email.value;
    req.send("email="+msg_value);
}


// This is the callback functions that gets called
// for the response from the server with the XML data

function updateMsgOnBrowser(testXML) {

    var test = testXML.getElementsByTagName("test")[0];
    var message = testXML.getElementsByTagName("message")[0];
    var message_value = message.firstChild.nodeValue;

    var msg_display = document.getElementById("msg_display");
        msg_display.innerHTML = " <cstext3>Added: \"" +
            message_value + "\"</cstext3>" ;

}


//the following two functions are helper infrastructure to
//craete a XMLHTTPRequest and register a listner callback function

function newXMLHttpRequest() {
    var xmlreq = false;
    if (window.XMLHttpRequest) {
        xmlreq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
            // Try ActiveX
        try {
            xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e1) {
            // first method failed
            try {
                xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
                 // both methods failed
            }
        }
    }
    return xmlreq;
}

function getReadyStateHandler(req, responseXmlHandler) {
    return function () {
    if (req.readyState == 4) {
        if (req.status == 200) {
                responseXmlHandler(req.responseXML);
        } else {
            var hellomsg = document.getElementById("hellomsg");
            hellomsg.innerHTML = "ERROR: "+ req.status;
            }
        }
    }
}

