var http = getHTTPObject(); // We create the HTTP Object
var bD = "";
var gD = "";
var emailTextBoxID = 'EmailAddress';
var emailErrorLabelID = 'EmailAddressError';
//comment out the checkEmail function calls in the two below to deactivate 
//web service call.
function verifyEmailAJAX()
{
	//return true;
	return checkEmail();
}
function verifyEmailNonStandardPage(emailTextBoxToValidate,errorLabelIDToCheck)
{
	//return true;
	emailTextBoxID = emailTextBoxToValidate;
	emailErrorLabelID = errorLabelIDToCheck;
	return checkEmail();
}
// change value of below to match location of webservice.
var localHost = urlConstructor();
var url = window.document.location.protocol + "//" + localHost + "/emailValidator/emailValidation.asmx/validateUsersEmail?emailAddress="; // The server-side script
var trackingURL = window.document.location.protocol + "//" + localHost + "/emailvalidator/emailValidation.asmx/trackYesClicks?"; //itemID=string&clientID=string";

function urlConstructor()
{
	if (document.location.host.toLowerCase() != "www.dogage.com")
	{
		return document.location.host + "/DogAge.Web";
	}
	else
	{
		return document.location.host;
	}
	//alert(urlConstructor());
}
// add in additional client validations if wanted.
function ValidateEmail(){
	//var emailID=document.getElementById(emailTextBoxID).value;
	return true
 }
// handles response from remote webservice (on same machine / farm). 
function handleHttpResponse() {
if ( ValidateEmail()  )
{
  if (http.readyState == 4) {
		if (http.status == 200){
			results = http.responseXML.documentElement;
			if (results.getElementsByTagName('isValid')[0].firstChild.data == "false")
			{
				clearUserNotification();
				var badDomain = results.getElementsByTagName('invalidDomain')[0].firstChild.data;
				var goodDomain = results.getElementsByTagName('validDomain')[0].firstChild.data;
				var uName = results.getElementsByTagName('userName')[0].firstChild.data;
				bD = badDomain;
				gD = goodDomain;
				showUserNotification(badDomain,goodDomain,uName);
			}
			else
			{
				if ( document.getElementById('misSpellBox').hasChildNodes() )
				{
					while(document.getElementById('misSpellBox').hasChildNodes() == true)
					{
						document.getElementById('misSpellBox').removeChild(document.getElementById('misSpellBox').childNodes[0]);
					}	
				}
			}
		}
  }
}
}
// creates yes/no radio buttons using DOM, Javascript
// yes/no radio buttons call other functions using onClick event
function showUserNotification(badDomain,goodDomain,uName)
{
	    trackYesClicks( "impression" );
	    var lineBreak = document.createElement("br");
		var mBox = document.getElementById('misSpellBox');
		mBox.appendChild(document.createElement("br"));
		var txt = document.createTextNode("Did you mean " + uName + "@" + goodDomain + "?");
		var spacer = document.createTextNode(" ");
		mBox.appendChild(txt);		
		var newDiv = document.createElement("div");
		newDiv.className = "Verd_Black_12_pad"; 
		
		var yBtn = document.createElement("input");
		yBtn.type = "radio";
		yBtn.onclick = fixEmailDomain;
		newDiv.appendChild(yBtn);
		newDiv.appendChild(document.createTextNode(" Yes "));
		
		newDiv.appendChild(document.createElement("br"));
		
		var nBtn = document.createElement("input");
		nBtn.type = "radio";
		nBtn.onclick = clickedNo;
		newDiv.appendChild(nBtn);
		newDiv.appendChild(document.createTextNode(" No "));
		mBox.appendChild(newDiv);
}

function clickedNo()
{
	trackYesClicks('no');
	clearUserNotification();
}

function clearUserNotification()
{
	if ( document.getElementById('misSpellBox').hasChildNodes() )
				{
					while(document.getElementById('misSpellBox').hasChildNodes() == true)
					{
						document.getElementById('misSpellBox').removeChild(document.getElementById('misSpellBox').childNodes[0]);
					}
				}
	//document.getElementById('misSpellBox').innerHTML = "";
	if ( document.getElementById(emailErrorLabelID)!= null )
	{
		while(document.getElementById(emailErrorLabelID).hasChildNodes() == true)
					{
						document.getElementById(emailErrorLabelID).removeChild(document.getElementById(emailErrorLabelID).childNodes[0]);
					}
	}
}

function fixEmailDomain( )
{
	document.getElementById(emailTextBoxID).value = document.getElementById(emailTextBoxID).value.toLowerCase().replace( bD.toLowerCase(), gD.toLowerCase() );
	
	//document.getElementById("Password").focus();
	clearUserNotification();
	trackYesClicks("yes");
}

function checkEmail() {
	if ( document.getElementById ) 
	{
	//alert('has document.getElementById');
		var emailValue = document.getElementById(emailTextBoxID);
		if (emailValue.value)
		{
			//alert(emailValue.value);
			if (http)
			{
				http.open("GET", url + emailValue.value.toLowerCase(), true);

				http.onreadystatechange = handleHttpResponse;

				http.send(null);
			}
		}
	}

 }
 
function trackYesClicks(userRepsonse) {
  // the two variables for tracking webservice vary by web site. 
  var impressionID = "2";
  var itemID = "3";
  var clientID = "1";
	if (userRepsonse == "impression" )
	{
		//alert( trackingURL + "itemID=" + itemID + "&clientID=" + clientID + "&answer=yes" );
		http.open("GET", trackingURL + "itemID=" + impressionID + "&clientID=" + clientID + "&answer=impression", true);

		http.onreadystatechange = handleTrackingResponse;

		http.send(null);
	}
	if (userRepsonse == "yes" )
	{
		//alert( trackingURL + "itemID=" + itemID + "&clientID=" + clientID + "&answer=yes" );
		http.open("GET", trackingURL + "itemID=" + itemID + "&clientID=" + clientID + "&answer=yes", true);

		http.onreadystatechange = handleTrackingResponse;

		http.send(null);
	}
	if (userRepsonse == "no" )
	{
		//alert( trackingURL + "itemID=" + itemID + "&clientID=" + clientID + "&answer=no" );
		http.open("GET", trackingURL + "itemID=" + itemID + "&clientID=" + clientID + "&answer=no", true);

		http.onreadystatechange = handleTrackingResponse;

		http.send(null);
	}
 }
 
 function handleTrackingResponse() {
if (http.readyState == 4) {
		if (http.status == 200){
			// do nothing, or uncomment to see pop up of tracking (0=failed, 1=success).
			//var trackResults = http.responseXML.documentElement;
			//alert( trackResults.firstChild.data );
		}
	}
}

function getHTTPObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
	  xmlhttp = false;
	}

  }
	return xmlhttp;

}

