// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // stores the reference to the XMLHttpRequest object
  var xmlHttp;
 // if running Internet Explorer 6 or older
  if(window.ActiveXObject)
  {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function process()
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // retrieve the name typed by the user on the form
    name = encodeURIComponent(document.getElementById("email").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "empid-validate1.php?name=" + name, true);  
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('process()', 1000);
}

// callback function executed when a message is received from the server
function handleServerResponse() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
      helloMessage = xmlDocumentElement.firstChild.data;
      // display the data received from the server
      document.getElementById("divMessage").innerHTML = 
                                            '<i>' + helloMessage + '</i>';
      // restart sequence
      setTimeout('process()', 1000);
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}



// JavaScript Document
function formValidator()
{
	// Make quick references to our fields
	var title = document.getElementById('title');
	var lname = document.getElementById('lname');
	var fname = document.getElementById('fname');
	var institute =document.getElementById('institute');	
	var address1 =document.getElementById('address1');	
	var address2 =document.getElementById('address2');		
	var email=document.getElementById('email');
	var sex = document.getElementById('sex');
	var city= document.getElementById('city');
	var ccode= document.getElementById('ccode');
	var telphone= document.getElementById('telphone');
	var mobileno = document.getElementById('mobileno');
	var bdate=document.getElementById('bdate');
	var bmonth=document.getElementById('bmonth');
	var byear=document.getElementById('byear');	
	    
	// Check each input in the order that it appears in the form!
	if(madeSelection(title,"Please select valid title")){				  
	if(isAlphabet(lname, "Please enter valid last name")){						
	if(isAlphabet(fname, "Plese enter valid first name")){
	if(isAlphanumeric(institute,"Please enter Institute name")){
	if(isAlphanumeric(address1,"Please enter address")){
	if(isAlphanumeric(address2,"Please enter address1")){	
	if(emailValidator(email, "Please enter a valid email address")){	
	if(madeSelection(sex,"Please select valid gender name")){
	if(isNumeric(ccode,"Please enter valid city code")){	
	if(isAlphabet(city,"Please enter valid city")){
	if(isNumeric(telphone, "Please enter valid telephone no.")){
	if(isNumeric(mobileno, "Please enter valid mobile no.")){			 
	if(madeSelection(bdate,"Please select Date of Birth")){
	if(madeSelection(bmonth,"Please select Birth Month")){
	if(madeSelection(byear,"Please select Birth Year")){

							return true;  															
	
												 }
											    } 
											   } 
											  }
											 }
							 			    }
										   }
								 		  }		  
 	  							  	     }	
									    } 
								       }
							          } 	
							         } 
						            } 
						           }
	return false;	
}

function isEmpty(elem, helperMsg)
{
	if(elem.value.length == 0){
	alert(helperMsg);
	elem.focus(); // set the focus to this input
	return true;
	}
	return false;
}

function isNumeric(elem, helperMsg)
{
    var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression))
	{
	  return true;
	} else
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
function isAlphabet(elem, helperMsg)
{
	var alphaExp = /^[a-zA-Z]/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg)
{
	var alphaExp = /^[0-9a-zA-Z]/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max)
{
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem,helperMsg)
{
  if(elem.value == "NULL")
  { alert(helperMsg);
	elem.focus();
	return false;
	}else{return true;}
}
function emailValidator(elem, helperMsg)
{
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}



function confirmation() 
{
	var answer = confirm("You are not eligible")
	if (answer)
	{
		alert("Bye bye!")
		window.location = "forfulltextcontact.php";
	}
	else{
		 alert("Thanks for sticking around!")
	}
}


function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) 
{
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1024;
        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) 
		{
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
        if (txtField)
    	{  
                txtField.innerHTML = field.value.length;
        }
		
}


function off() 
{
   document.getElementById("hidethis").style.display = 'none';
   document.getElementById("hidethis1").style.display = 'none';
   document.getElementById("hidethis2").style.display = 'none';
   document.getElementById("hidethis3").style.display = 'none';
   document.getElementById("hidethis4").style.display = 'none';
   document.getElementById("hidethis5").style.display = 'none';
   document.getElementById("hidethis6").style.display = 'none';
   document.getElementById("hidethis7").style.display = 'none';
   document.getElementById("hidethis8").style.display = 'none';
   document.getElementById("hidethis9").style.display = 'none';
   document.getElementById("hidethis10").style.display = 'none';
   document.getElementById("hidethis11").style.display = 'none';   
   document.getElementById("hidethis12").style.display = 'none';
   document.getElementById("hidethis13").style.display = 'none';
   document.getElementById("hidethis14").style.display = 'none';
   document.getElementById("hidethis15").style.display = 'none';
   document.getElementById("hidethis16").style.display = 'none';
   document.getElementById("hidethis17").style.display = 'none';
   document.getElementById("hidethis18").style.display = 'none';
   document.getElementById("hidethis19").style.display = 'none';
   document.getElementById("hidethis20").style.display = 'none';   
   document.getElementById("hidethis21").style.display = 'none';   
            
   
   
}
function on() 
{
   document.getElementById("hidethis").style.display = '';
   document.getElementById("hidethis1").style.display = '';
   document.getElementById("hidethis2").style.display = '';
   document.getElementById("hidethis3").style.display = '';
   document.getElementById("hidethis4").style.display = '';
   document.getElementById("hidethis5").style.display = '';
   document.getElementById("hidethis6").style.display = '';
   document.getElementById("hidethis7").style.display = '';
   document.getElementById("hidethis8").style.display = '';
   document.getElementById("hidethis9").style.display = '';
   document.getElementById("hidethis10").style.display = '';
   document.getElementById("hidethis11").style.display = '';   
   document.getElementById("hidethis12").style.display = '';
   document.getElementById("hidethis13").style.display = '';
   document.getElementById("hidethis14").style.display = '';
   document.getElementById("hidethis15").style.display = '';
   document.getElementById("hidethis16").style.display = '';
   document.getElementById("hidethis17").style.display = '';
   document.getElementById("hidethis18").style.display = '';
   document.getElementById("hidethis19").style.display = '';
   document.getElementById("hidethis20").style.display = '';         
   document.getElementById("hidethis21").style.display = '';   
}


