/*
 * vicidDomainCheck.js
 *
 * Part of the vicidDomainCheck project:
 *  vicidDomainCheck.css - styles
 *  vicidDomainCheck.js  - javascript
 *  vicidDomainCheck.php - php
 *
 * This should be included between <head> and </head> using the following line:
 * <script type="text/javascript" src="vicidDomainCheck.js"></script>
 * The style information should be included between <head> and </head>:
 * <link rel="stylesheet" type="text/css" href="vicidDomainCheck.css" />
 * 
 * This should be located somewhere on the page and will display the domain
 * availability information:
 * <span id="domainCheckResult"></span>
 *
 *
 * Prereqs:
 * Make sure that the static global id's (sDomainInputId, etc.) match the
 * values of the domain input, tld input, and result output from the html.
 * 
 * How to use:
 * Call initializeDomainCheck(); once the page has loaded
 * (either <body onLoad="initializeDomainCheck()"> or in javascript)
 */

// global settings
var sDomainCheckDelay = 1000; // milliseconds before checking a domain
var sDomainAvailableHtml = "<span class=\"domainAvailable\">[<a href=\"http://www.enom.com/whois/Whois.aspx?DomainName=%DOMAINTLD%&submit.x=0&submit.y=0\" target=\"_blank\">Available!</a>]</span>"; // note: replace %DOMAINTLD%
var sDomainNotAvailableHtml = "<span class=\"domainNotAvailable\">[<a href=\"http://www.enom.com/whois/Whois.aspx?DomainName=%DOMAINTLD%&submit.x=0&submit.y=0\" target=\"_blank\">Not available</a>]</span>"; // note: replace %DOMAINTLD%
var sDomainCheckingHtml = "<span class=\"domainAvailableStatus\">Checking...</span>";

// global ids (these must match ids in the html)
// note: these elements do not exist yet, because this JS is called
// before they are even written to the page.
var sDomainInputId = "domainInput";
var sTldInputId = "tldInput";
var sDomainCheckResultId = "domainCheckResult";

// global variables
var wCheckDomain = "";
var wCheckTld = "";
var wLastCheckedDomainAvailable = false;


// preDelayCheckDomain()
// schedules postDelayCheckDomain to be called after a delay.
// this prevents multiple/redundant ajax calls
function preDelayCheckDomain(){
	var wDomainInput = document.getElementById(sDomainInputId);
	var wTldInput = document.getElementById(sTldInputId);
	if(wDomainInput.value!==wCheckDomain || wTldInput.value!==wCheckTld){ // make sure we haven't already checked this domain
		wCheckDomain = wDomainInput.value;
		wCheckTld = wTldInput.value;
		setTimeout("postDelayCheckDomain('"+wCheckDomain+"','"+wCheckTld+"')", sDomainCheckDelay);
	}
}

// postDelayCheckDomain(pCheckDomain,pCheckTld)
// checks if the domain and tld have been unchanged for the delay.
// If unchanged, the result is changed to "checking..." and the ajax request is
// made.
function postDelayCheckDomain(pCheckDomain,pCheckTld){
	if(pCheckDomain==wCheckDomain && pCheckTld==wCheckTld){
		var wDomainCheckResult = document.getElementById(sDomainCheckResultId);
		wDomainCheckResult.innerHTML = sDomainCheckingHtml;
		ajaxCheckDomainAvailability(wCheckDomain,wCheckTld);
	}
}

// ajaxCheckDomainAvailability(pDomain, pTld)
// Ajax call to checkdomain.php that checks the domain availability through Enom
// return values (of php) are: blank/null string, available, and notavailable
// this function sets the wDomainCheckResult to "available"/"not available".
function ajaxCheckDomainAvailability(pDomain, pTld){
	var wDomainCheckResult = document.getElementById(sDomainCheckResultId);
	var wXmlHttp;

	// cross browser way of creating a XmlHttpRequest object
	if(window.XMLHttpRequest){
		wXmlHttp = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
	}else if(window.ActiveXObject){
		wXmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE5, IE6
	}else{
		wDomainCheckResult.innerHTML = "";
		return; // their browser does not meet the requirements for ajax
	}

	// setup callback function (will be called after request is complete)
	wXmlHttp.onreadystatechange=function(){
		if(wXmlHttp.readyState==4){
			if(wXmlHttp.responseText == "available"){
				wDomainCheckResult.innerHTML = sDomainAvailableHtml.replace("%DOMAINTLD%",pDomain+"."+pTld);
				wLastCheckedDomainAvailable = true;
			}else if(wXmlHttp.responseText == "notavailable"){
				wDomainCheckResult.innerHTML = sDomainNotAvailableHtml.replace("%DOMAINTLD%",pDomain+"."+pTld);
				wLastCheckedDomainAvailable = false;
			}else{
				// error, show "not available"
				pResultLocation.innerHTML = sDomainNotAvailableHtml.replace("%DOMAINTLD%",pDomain+"."+pTld);
				wLastCheckedDomainAvailable = false;
			}
		}
	}

	// make request
	wXmlHttp.open("GET","vicidDomainCheck.php?domain="+pDomain+"&tld="+pTld,true);
	wXmlHttp.send(null);
}

// initializeDomainCheck()
// causes "preDelayCheckDomain()" to be called when onChange and onKeyUp events
// occur on the domain input box and the tld combo box.
// Also starts an initial check (in case the form is reloaded and contains a 
// domain that needs to be checked, but the user didn't type it)
function initializeDomainCheck(){
	var wDomainInput = document.getElementById(sDomainInputId);
	var wTldInput = document.getElementById(sTldInputId);

	// set event handlers
	wDomainInput.onchange = preDelayCheckDomain;
	wTldInput.onchange = preDelayCheckDomain;
	wDomainInput.onkeyup = preDelayCheckDomain;

	if(wDomainInput.value.length>0){ // if a domain has been typed, send off req.
		wCheckDomain = wDomainInput.value;
		wCheckTld = wTldInput.value;
		postDelayCheckDomain(wCheckDomain,wCheckTld);
	}
}
