//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();

//Initiate the AJAX request
function makeRequest(url, param) {
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
        receiveReq.open("POST", url, true);
        receiveReq.onreadystatechange = updatePage; 
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.setRequestHeader("Content-length", param.length);
        receiveReq.setRequestHeader("Connection", "close");
        receiveReq.send(param);
    }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
    var sStatus = "";
    if(receiveReq.readyState == 4) 
    {
        if(receiveReq.responseText == 1)
        {
            sStatus = ""
            saveProcess();          
        }
        else
        {
            sStatus = "Enter valid character";
            document.getElementById('lblStatus').innerHTML = sStatus;
            centerPopup();
			loadPopup();
        }
        changeCaptchaImg();
    }
}

function changeCaptchaImg()
{
    img = document.getElementById('imgCaptcha'); 
    img.src = 'captcha/create_image.php?' + Math.random();
}

//Called every time when form is perfomed
function Newsletter(theForm){
    
    if(clientValidation())
    {
        //Set the URL
        var url = 'captcha/captcha.php';
        //Set up the parameters of our AJAX call
        var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent(theForm.txtCaptcha.value);
        //Call the function that initiate the AJAX request
        makeRequest(url, postStr);
    }
}

function saveProcess(){
    var param = "";
    var theForm = document.frmNewsSignup;
    param = param + theForm.txtUserName.name + "=" + encodeURIComponent(theForm.txtUserName.value);
    param = param + "&";
    param = param + theForm.txtConfEmailID.name + "=" + encodeURIComponent(theForm.txtConfEmailID.value);
   
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   
        receiveReq.open("POST", "newjquery/signup.php", true);
        receiveReq.onreadystatechange = saveProcessStatus; 
        receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        receiveReq.setRequestHeader("Content-length", param.length);
        receiveReq.setRequestHeader("Connection", "close");
        receiveReq.send(param);
    }
}

function saveProcessStatus(){
    var sStatus = "";
    
    if(receiveReq.readyState == 4) 
    {
        if(receiveReq.responseText == "1")
        {
            clearControl();
            sStatus = "Thank You! We have added you to our newsletter mailing list!";    
        }
        else if(receiveReq.responseText == "-2")
        {
            sStatus = "You have added already to our newsletter mailing list!";
        }
        else
        {
            sStatus = "Record not saved";
        }
        centerPopup();
		loadPopup();
		document.getElementById('lblStatus').innerHTML = sStatus;
    }
}

function clientValidation(){

    var sStatus = "";
    var un = document.getElementById('txtUserName');
    var eid = document.getElementById('txtEmailID');
    var status = document.getElementById('lblStatus');
    var coneid = document.getElementById('txtConfEmailID');
    if(un.value != ""){
        if(eid.value != ""){
            if(validateEmail(eid.value)){
             if(eid.value==coneid.value){
                return true;
                }
                else{
                        status.innerHTML = "Verify Email Address mismatch!";
                        centerPopup();
			            loadPopup();
		            }
		 }
            else{
                status.innerHTML = "Invalid Email Address!";
                centerPopup();
				loadPopup();
            }
        }
        else{
            status.innerHTML = "Email Address cannot be blank!";
            centerPopup();
			loadPopup();
        }
        
       
        
    }
    else{
        status.innerHTML = "Name cannot be blank!";
        centerPopup();
		loadPopup();
	}
    return false;
}

function validateEmail(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(email) == false) {
        return false;
    }
    else{
        return true;
    }
}

function clearControl(){
    var un = document.getElementById('txtUserName');
    var eid = document.getElementById('txtEmailID');
     var coneid = document.getElementById('txtConfEmailID');
    var nCap = document.getElementById('txtCaptcha');
    un.value = "";
    eid.value = "";
    coneid.value="";
    nCap.value = "";
}


