/**
 * should be included wherever there is a domain checker.
 */

function checkAll(inputName){
    $('input[name="'+inputName+'"]').click(function(){
       if(this.checked == false){
            $('input[type="checkbox"]').each(function(){

                this.checked = false;
            });


       }else{
            $('input[type="checkbox"]').each(function(){

                this.checked = true;
            });
       }
   });
}

function in_array(needle,haystack){
     for (key in haystack) {
            if (haystack[key] == needle) {
              
                return true;
            }
        }return false;
}

function checkBox(val){
    
    tlds = val.split(",");
    $('input[type="checkbox"]').each(function(){
                if($(this).val() == val){
                    this.checked = true;
                }else{
                     this.checked = false;
                }
    });
}

function validateDomainInput(domain){
    if(domain.indexOf(".")!= -1){
        var tld = domain.split(".");
     
        var arrayLenth = tld.length -1;
        tld =  tld[arrayLenth];
        alert("please remove the ."+tld);
        return false;
    }
}

function sortDomainPage(){
    tlds = new Array("com.au","european","eu.com","co.nz","tv", "com","ie","net","uk","org","biz","info","eu",
	"tel","co.uk","org.uk","name","at","de","es","be","ch","cz","dk","it","li","lt","lu","lv","nl","pl","se",
	"ro","ru","us","cn","jp","in","co.za","cc","mobi","co","je","me","ca");
    url = document.location.href;
    splitUrl = url.split("/");
    arg = splitUrl[splitUrl.length - 1];
    arg = arg.replace("-domain-registration.php","");
    arg = arg.replace("-domain-names.php","");
    arg = arg.replace("-registration-guide.php","");
    if(!in_array(arg,tlds))return false;
    if(arg == "uk"){arg = "co.uk";}
    if(arg == "european"){arg = "eu";}
    
    return "."+arg;
}




//START IF DOCUMENT READY FUNCTION CALLS
var tld = "";
var tldDiv = "";
var clear = "";
var data;
$('document').ready(function(){
    tld = sortDomainPage();
   
    if(tld !=false){
        checkBox(tld);
    }
  


   $('a[id="switchform"]').click(function(){
       
       if($(this).html()!= "search for a single domain"){
           $('.small-search').load('includes/forms/bulkdomainchecker.php');
           $(this).html("search for a single domain");
             return false;
       }else{
           $('.small-search').load('includes/forms/regformsmall.php');
           $(this).html("search for multiple domains");
             return false;
       }
  });
  $('a[id="showtlds"]').hover(function(e){
                           mousePosTop = e.pageY;
			mousePosLeft = e.pageX;

                           if(tldDiv == ""){
				tldDiv = document.createElement("div");
				tldDiv.id = "tldsbox";

			document.body.appendChild(tldDiv);
                        
				$("#"+tldDiv.id).css("width","60px");
				$("#"+tldDiv.id).css("height","125px");
				$("#"+tldDiv.id).css("position","absolute");
				$("#"+tldDiv.id).css("z-index","900");
				$("#"+tldDiv.id).css("top",mousePosTop + 15);
				$("#"+tldDiv.id).css("left",mousePosLeft + 10);
				$("#"+tldDiv.id).css("background-color","white");
				$("#"+tldDiv.id).css("border-color","#77A5D2");
				$("#"+tldDiv.id).css("border-style","solid");
				$("#"+tldDiv.id).css("border-width","3px 2px");
				$("#"+tldDiv.id).css("-moz-box-shadow","0 2px 10px #000000");
				$("#"+tldDiv.id).css("padding","8px");
				$("#"+tldDiv.id).css("font-size","11px");
				$("#"+tldDiv.id).css("font-family"," Arial, Helvetica, sans-serif");
				$("#"+tldDiv.id).css("color","#666666");
				$("#"+tldDiv.id).css("text-align","left");
                                $("#"+tldDiv.id).css("overflow","auto");
				$("#"+tldDiv.id).load('includes/forms/forminfo/tlds.php',function(data){$("#"+tldDiv.id).html(data);}, $("#"+tldDiv.id).hover(function(){
                                 clearTimeout(clear);
                                 $('.tldin').hover(function(){
                                     $("#"+this.id).css("background-color","#dedede");
                                 }, function(){
                                    $("#"+this.id).css("background-color","#ffffff");
                                 });
                                },function(){clear = setTimeout(function(){
                                $("#"+tldDiv.id).hide();
                                },300);}
                                            ));


                        

			}else{
                            $("#"+tldDiv.id).css("top",mousePosTop + 15);
				$("#"+tldDiv.id).css("left",mousePosLeft + 10);
                                $("#"+tldDiv.id).show();
                        }
  },function(){
        clear = setTimeout(function(){
        $("#"+tldDiv.id).hide();
        },300);
	
	});
        $('a[id="showtlds"]').click(function(){return false;});
  $('form[name="domaincheck"]').submit(function(){

      var domain =  $('input[name="domain"]').val();
    
      if(validateDomainInput(domain) == false) return false;
      else return true;
     
  });


});

