$(function() {
    
    var theDomain;
    var theQuery;
    var jqxhr;
    var errorMessage;
    var domainName;
    var extension;
    
    $('#theForm input').focus(function (){ 
    
    if ($('#theForm .error').css('display') !== 'none')
    {
    $('#theForm .error').fadeOut(300);
    errorMessage = null;
    
    }

    });
    
    $('#theForm form').submit(function(event) {
        
        $('#theForm .error').fadeOut(300);
        errorMessage = null;
        
        performActions();
        return false;
    });
        
    $('#theForm .invisibleButton').click(performActions);
    function performActions() {
    	
    	    $("#temp").html("loading");
    	    theDomain = $('#theForm').find('input').attr("value");
    	    
    	    checkDomain(theDomain);
    	    
    	    if (errorMessage != null){
   	    	$('#theForm .error').fadeIn(300); 
    	        $('#theForm .error p').text(errorMessage);
     	       
    	    } else{
    	    
    	    theDomain = domainName +"."+ extension;
    	    $('#theForm').find('input').attr('value', theDomain);
    	    
    	
    	    $.post('../spincheck.php', { action: "check", cSSld: domainName, Submit:"Check+Availability", 'ttld[]': extension }, function(data){
    	                
    	               //Response from script when comment inserted to database
    	               
    	                var $child = ($(data).find('#theResults .red:first').length == 0 && $(data).find('#theResults .green:first').length != 0) ? $(data).find('#theResults .green:first') : $(data).find('#theResults .red:first');
    	                
    	                $("#temp").html($child);
    	           });
    	    }
    
    }
    
    function checkDomain(theDomain) {
    
        
        var arr = new Array(
        '.com','.net','.org','.biz','.coop','.info','.museum','.name',
        '.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
        '.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
        '.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
        '.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
        '.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
        '.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
        '.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
        '.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
        '.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
        '.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
        '.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
        '.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
        '.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
        '.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
        '.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
        '.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
        '.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
        '.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
        '.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
        '.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
        '.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
        '.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
        '.wf','.ye','.yt','.yu','.za','.zm','.zw','.co.in');
        
        var mai = theDomain;
        var val = true;
        
        mai = mai.replace("www.", "");
        mai = mai.replace("http://", "");   
        
        if(mai.indexOf(".")<=0){
        	mai = mai+".com";
        }
        
        
        var dot = mai.indexOf(".");
        var dname = mai.substring(0,dot);
        var ext = mai.substring(dot,mai.length);

        domainName = dname;
        extension = ext.replace(".", "");       
                
        
        if(dot>2 && dot<57)
        
        {
        
        
            for(var i=0; i<arr.length; i++)
            {
              if(ext == arr[i])
              {
                val = true;
                break;
              } 
              else
              {
                val = false;
              }
            }
            if(val == false)
            {
                 errorMessage = "The extension "+ext+ " is not supported!";
                 return false;
            }
            else
            {
                for(var j=0; j<dname.length; j++)
                {
                  var dh = dname.charAt(j);
                  var hh = dh.charCodeAt(0);
                  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
                  {
                     if((j==0 || j==dname.length-1) && hh == 45)    
                     {
                        errorMessage = "The domain name should not begin or end with '-'";
                          return false;
                     }
                  }
                else    {
                     errorMessage = "The domain name can't have special characters";
                     return false;
                  }
                }
            }
        }
        else if(dot<=2)
        {
        errorMessage = "The domain name is too short!";
                 return false;
        }else if(dot>=57)
        {
        errorMessage = "The domain name is too long!";
                 return false;
        }    
        
        return true;
        
    
    }

});

