   
    function getCookie(NameOfCookie)
    { 
        if (document.cookie.length > 0) 
        {
            begin = document.cookie.indexOf(NameOfCookie+"="); 
            if (begin != -1) 
            {
                begin += NameOfCookie.length+1; 
                end = document.cookie.indexOf(";", begin);
                if (end == -1) end = document.cookie.length;
                return unescape(document.cookie.substring(begin, end));
            } 
        }
        return null; 
    }
    
    function setCookie(NameOfCookie, value, expiredays) 
    {
        var ExpireDate = new Date ();
        ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
        document.cookie = NameOfCookie + "=" + escape(value) + 
        ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
    }
    
    function validateEmail(email)
    {		
        var x=email;
        var atpos=x.indexOf("@");
        var dotpos=x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    function validateForm(form)
    {
        var Name = form.Name1.value;
        var Email = form.Email1.value;
        var Age = form.Age.value;

        if (Name.length < 2)
        {
            alert("Please enter your first name");
            return false;
        }
        if (validateEmail(Email) == false)
        {
            alert("Plesae enter a valid e-mail address");
            return false;
        }
        if (isNaN(Age))
        {
            alert("Please enter a valid age");
            return false;
        }
        if (Age <13)
        {
            alert("Sorry you must be at least 13 to provide your email address.");
            return false;
        }	
    }
			
        
    function isDomainChars(e)
    {
        g_keyCount++; 
        if (g_keyCount > 16) 
        {
            if (g_userCookie == "No")
            {
                doRegister();
                return false;
            }
            else if (g_userCookie == "Yes")
            {
                g_userCookie = "No";
                setCookie("UserRegistered", g_userCookie, 365);
            }
        }
        
        keyCode = event.keyCode;
        isShift = event.shiftKey == 1;
        return (((keyCode >= 48 && keyCode <= 57)&& isShift == false) ||  //0 - 9                   
                (keyCode >= 65 && keyCode <= 90) ||     //a - z
                (keyCode == 8) ||                       //backspace
                (keyCode == 109) ||                     //subtract
                (keyCode == 46) ||                      //delete
                (keyCode == 189 && isShift == false) || //dash and underscore key
                (keyCode == 37 && isShift == false)  || //left arrow
                (keyCode == 39 && isShift == false)  || //right arrow
                (keyCode == 36 && isShift == false)  || //home
                (keyCode == 35 && isShift == false))    //end
    }                      
    
    
    function removeOptionSelected()
    {
      var elSel = document.getElementById('savednames');
      var i;
      for (i = elSel.length - 1; i>=0; i--) {
        if (elSel.options[i].selected) {
          elSel.remove(i);
        }
      }
    }

    function appendOptionLast(text,value)
    {
      var elOptNew = document.createElement('option');
      elOptNew.text = text;
      elOptNew.value = value;
      var elSel = document.getElementById('savednames');

      try {
        elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
      }
      catch(ex) {
        elSel.add(elOptNew); // IE only
      }
    }
    
    function doSave(domain)
    {
        if  (domain.length = 0) return;
        
        appendOptionLast(domain,0);
       
        var requestSave = new ajaxObject('ajaxwhoissave.php',callbackSaveNameStatus);
        requestSave.update('name=' + domainName,  'GET');
    }
    
    function doBuy(domain)
    {
        document.getElementById("buydiv").style.visibility = "visible";
        document.getElementById("buydivguts").style.visibility = "visible";
        document.getElementById("buydivclose").style.visibility = "visible";
    }
    
    function doHideBuyDialog()
    {
        document.getElementById("buydiv").style.visibility = "hidden";
        document.getElementById("buydivguts").style.visibility = "hidden";
        document.getElementById("buydivclose").style.visibility = "hidden";
    }
    
    function doRegister()
    {
        document.getElementById("registerdiv").style.visibility = "visible";
        document.getElementById("registerdivguts").style.visibility = "visible";
        document.getElementById("registerdivclose").style.visibility = "visible";
    }
    
    function doHideRegisterDialog()
    {
        document.getElementById("registerdiv").style.visibility = "hidden";
        document.getElementById("registerdivguts").style.visibility = "hidden";
        document.getElementById("registerdivclose").style.visibility = "hidden";
    }
    
    function callbackSaveNameStatus(responseText, responseStatus)
    {
        if (responseStatus==200) {
            var response = responseText.split(":",2);
            if (response.length == 2)
            {
                var domain = response[0];
                var status = response[1];
                var temp = domain.split(".",2);
                if (temp.length == 2)
                {
                    var topdomain = temp[1];
                    
                    var quoteddomain = "'" + domain + "'";
                    var htmlText =  '<a href="#" onclick="javascript:doSave(' + quoteddomain + ');">Save</a>&nbsp;&nbsp;&nbsp;' + 
                                    '<a href="#" onclick="javascript:doBuy(' + quoteddomain + ');">Buy</a>&nbsp;&nbsp;&nbsp;' +
                                    '<a href="http://www.' + domain + '" target="_blank">Go To Site</a><br>' +
                                    responseText;
                    
                    try
                    {
                        document.getElementById(topdomain).innerHTML = htmlText;
                    }
                    catch(ex)
                    {
                        alert("failed: responseText=" + responseText + " htmlText=" + htmlText + " topDomain=" + topdomain);
                    }
                    var i = status.indexOf("available", 0);
                    if (i < 0)  //note need thriple equal operator in case needle starts at index 0.
                    {
                        document.getElementById("outer"+topdomain).style.backgroundColor = '#da676a';
                    }
                    else    
                    {
                        document.getElementById("outer"+topdomain).style.backgroundColor = '#878b03';
                    }   
                  
                    //clear http timer request (allow other http requests to occur)
                    g_httpDomainInUse[topdomain] = 0;
                }
                else
                {
                    document.getElementById('errordiv').innerHTML = "error " + temp;
                }
            }
            else
            {
                document.getElementById('errordiv').innerHTML = "error " + response;
            }
        }
    }   
        
    function callbackProcessData(responseText, responseStatus)
    {
        if (responseStatus==200) {
            var response = responseText.split(":",2);
            if (response.length == 2)
            {
                var domain = response[0];
                var status = response[1];
                var temp = domain.split(".",2);
                if (temp.length == 2)
                {
                    var topdomain = temp[1];
                    
                    var quoteddomain = "'" + domain + "'";
                    var htmlText =  '<a href="#" onclick="javascript:doSave(' + quoteddomain + ');">Save</a>&nbsp;&nbsp;&nbsp;' + 
                                    '<a href="#" onclick="javascript:doBuy(' + quoteddomain + ');">Buy</a>&nbsp;&nbsp;&nbsp;' +
                                    '<a href="http://www.' + domain + '" target="_blank">Go To Site</a><br>' +
                                    responseText;
                    
                    try
                    {
                        document.getElementById(topdomain).innerHTML = htmlText;
                    }
                    catch(ex)
                    {
                        alert("failed: responseText=" + responseText + " htmlText=" + htmlText + " topDomain=" + topdomain);
                    }
                    var i = status.indexOf("available", 0);
                    if (i < 0)  //note need thriple equal operator in case needle starts at index 0.
                    {
                        document.getElementById("outer"+topdomain).style.backgroundColor = '#da676a';
                    }
                    else    
                    {
                        document.getElementById("outer"+topdomain).style.backgroundColor = '#878b03';
                    }   
                  
                    //clear http timer request (allow other http requests to occur)
                    g_httpDomainInUse[topdomain] = 0;
                }
                else
                {
                    document.getElementById('errordiv').innerHTML = "error " + temp;
                }
            }
            else
            {
                document.getElementById('errordiv').innerHTML = "error " + response;
            }
        }
    }   
  
    /* http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object */
    function ajaxObject(url, callbackFunction)
    {
      var that=this;      
      this.updating = false;
      
      this.abort = function()
                    {
                        if (that.updating)
                        {
                            that.updating=false;
                            that.AJAX.abort();
                            that.AJAX=null;
                        }
                    }
                    
      this.update = function(passData,postMethod)
                    { 
                        if (that.updating) { return false; }
                        that.AJAX = null;                          
                        if (window.XMLHttpRequest)
                        {              
                            that.AJAX=new XMLHttpRequest();              
                        }
                        else
                        {                                  
                            that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
                        }                                             
                        if (that.AJAX==null)
                        {                             
                            return false;                               
                        }
                        else
                        {
                            that.AJAX.onreadystatechange = function()
                                {  
                                    if (that.AJAX.readyState==4)
                                    {             
                                        that.updating=false;                
                                        that.callback(that.AJAX.responseText,
                                                      that.AJAX.status,
                                                      that.AJAX.responseXML);        
                                        that.AJAX=null;                                         
                                    }                                                      
                                }                                                        
                            that.updating = new Date();                              
                            if (/post/i.test(postMethod))
                            {
                                var uri=urlCall+'?'+that.updating.getTime();
                                that.AJAX.open("POST", uri, true);
                                that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                                that.AJAX.setRequestHeader("Content-Length", passData.length);
                                that.AJAX.send(passData);
                            }
                            else
                            {
                                var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
                                that.AJAX.open("GET", uri, true);                             
                                that.AJAX.send(null);                                         
                            }              
                            return true;                                             
                        }                                                                           
                    }
      var urlCall = url;        
      this.callback = callbackFunction || function () { };
    }
    
    var g_httpDomainInUse =[];
    
    function processAllTopDomainsSelected()
    {
        var domainName = document.getElementById("domain").value;
        
        //init array of ajax http objects
        ajaxArray=[];
        for (var topdomain in g_httpDomainInUse)
        {
            ajaxArray[topdomain] = new ajaxObject('ajaxwhoisvalidate.php',callbackProcessData);
            g_httpDomainInUse[topdomain] = g_timerIndex;    
        }
                
        // Call them all at once!
        for (var prop in ajaxArray)
        {
            ajaxArray[prop].update('name=' + domainName + '.' + prop, 'GET');
        }
    }
    
    function topDomainChanged(obj)
    {
        //queryServer();
    }
    
    function validate(e)
    {
      if (! isDomainChars(e.keyCode)) return false;
      return true;
    }
    
    // Declaring class "Timer"
    var Timer = function()
    {        
        // Property: Frequency of elapse event of the timer in millisecond
        this.Interval = 1000;
        
        // Property: Whether the timer is enable or not
        this.Enable = new Boolean(false);
        
        // Event: Timer tick
        this.Tick;
        
        // Member variable: Hold interval id of the timer
        var timerId = 0;
        
        // Member variable: Hold instance of this class
        var thisObject;
        
        // Function: Start the timer
        this.Start = function()
        {
            this.Enable = new Boolean(true);
    
            thisObject = this;
            if (thisObject.Enable)
            {
                thisObject.timerId = setInterval(
                function()
                {
                    thisObject.Tick(); 
                }, thisObject.Interval);
            }
        };
        
        // Function: Stops the timer
        this.Stop = function()
        {            
            thisObject.Enable = new Boolean(false);
            clearInterval(thisObject.timerId);
        };
    
    };
    
    var g_userCookie = "No";
    var g_keyCount = 0;
    var g_timerIndex = 1;
    var obj = new Timer();
    var prevDomainName = "";
    obj.Interval = 500;
    obj.Tick = timer_tick;
    obj.Start();
    
    function timer_tick()
    {
        g_timerIndex = g_timerIndex + 1;
        var domainName = document.getElementById("domain").value;
        if (domainName == prevDomainName) return; //nothing changed
    
        processAllTopDomainsSelected();
        prevDomainName = domainName;
        
        for (var prop in ajaxArray)
        {
            //reset topdomain timer - in case http packet is lost so refresh can happen again
            lasttime = g_httpDomainInUse[prop];
            if (lasttime + 15 <= g_timerIndex)
            {
                g_httpDomainInUse[prop] = 0;
           
            }
            
            if (g_httpDomainInUse[prop] == 0)
            {
              //make sure name was completed
                var htmlText = document.getElementById(prop).innerHTML;
                var fulldomain = domainName + '.' + prop;
                var i = htmlText.indexOf(fulldomain, 0);
                if (i == -1)
                {
                    //ajaxArray[prop].update('name=' + domainName + '.' + prop, 'GET');
                    //g_httpDomainInUse[prop] = g_timerIndex;
                }
            }
        }
        
    }
    
    function loadPage()
    {
        document.getElementById("domain").focus();
        g_userCookie = getCookie("UserRegistered");
        if (g_userCookie == null) g_userCookie = "Yes";
               
        for (var i=0; i < document.forms['domainForm'].elements.length; i++)
        {
            if (document.forms['domainForm'].elements[i].type == "checkbox")
            {
                if (document.forms['domainForm'].elements[i].checked)
                {
                    var topdomain = document.forms['domainForm'].elements[i].value;
            
                    g_httpDomainInUse[topdomain] = g_timerIndex;
                }
                else
                {
                    g_httpDomainInUse[topdomain] = 0;
                }
            }
        }
    }
    
    
    

