// adattamento del seguente codice:
// http://www.hscripts.com/scripts/JavaScript/domainValidate.php
function checkDomain(nname) {
  var mai = nname;
  var dot = mai.lastIndexOf(".");
  var dname = mai.substring(0,dot);
  var ext = mai.substring(dot,mai.length);

  if (ext == ".uk" || ext == ".mx") {
    mai = dname;
    dot = mai.lastIndexOf(".");
    dname = mai.substring(0,dot);
    ext = ext+mai.substring(dot,mai.length);
  }

  if (dot>2 && dot<57) {
    for (var j=0; j<dname.length; j++) {
      var dh = dname.charAt(j);
      var hh = dh.charCodeAt(0);
      if (dh==".") {
        var ddot = dname.lastIndexOf(".");
        var rdomain = dname.substring(ddot+1,dname.length);
        alert (this.i18n.get("/js/adm/domain/domainNameContainDots",new Array(rdomain+ext,nname)));
        return false;
      } else 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)	{
          alert (this.i18n.get("/js/adm/domain/domainNameBeginOrEndWithMinus"));
          return false;
        }
      }	else {
        alert (this.i18n.get("/js/adm/domain/domainNameHasSpecialChar"));
        return false;
      }
    }
  }
  else {
    alert(this.i18n.get("/js/adm/domain/domainNameIsShortOrLong"));
    return false;
  }
  return true;
}
function IwAdmDomains() {
  this.i18n=window.iw.i18n;
  this.form=null;
  this.f_op=null;
  this.f_orderType=null;
  this.f_domainName=null;
  this.f_tld=null;
  this.f_domainName_r=null;
  this.f_tld_r=null;
  this.f_domainName_t=null;
  this.f_tld_t=null;
  this.f_fullDomainName=null;
}
IwAdmDomains.prototype.init = function() {
  if (this.form!=null) {
    this.f_op=this.form['iw.op'];
    this.f_orderType=this.form['dn.orderType'];
    this.f_domainName=this.form['dn.domainName'];
    this.f_tld=this.form['dn.tld'];
    this.f_domainName_r=this.form['dn.domainName_r'];
    this.f_tld_r=this.form['dn.tld_r'];
    this.f_domainName_t=this.form['dn.domainName_t'];
    this.f_tld_t=this.form['dn.tld_t'];
    this.f_fullDomainName=this.form['dn.fullDomainName'];
  }
}
IwAdmDomains.prototype.cancelNewDomain = function() {
  this.f_op.value='cancelNewDomain';
  this.form.submit();
}
IwAdmDomains.prototype.register = function() {
  this.f_orderType.value="registration";
  this.f_domainName.value=this.f_domainName_r.value;
  this.f_tld.value=this.f_tld_r.value
  var domain=this.f_domainName.value;
  var tld=this.f_tld.value;
  if (domain==null || domain=='') {
    alert(this.i18n.get("/js/adm/domain/domainNameIsEmpty"));
  } else if	(checkDomain (domain+"."+tld)) {
    this.f_op.value='addDomainOrder';
    this.f_fullDomainName.value=this.f_domainName.value+'.'+this.f_tld.value;
    this.form.submit();
  }
}
IwAdmDomains.prototype.transfer = function() {
  this.f_orderType.value="transfer";
  this.f_domainName.value=this.f_domainName_t.value;
  this.f_tld.value=this.f_tld_t.value;
  var domain=this.f_domainName.value;
  if (domain==null || domain=='') {
    alert(this.i18n.get("/js/adm/domain/domainNameIsEmpty"));
  } else {
    this.f_op.value='addDomainOrder';
    this.f_fullDomainName.value=this.f_domainName.value+'.'+this.f_tld.value;
    this.form.submit();
  }
}
IwAdmDomains.prototype.verify = function() {
  this.f_domainName.value=this.f_domainName_r.value;
  this.f_tld.value=this.f_tld_r.value;
  var domain=this.f_domainName.value;
  var tld=this.f_tld.value;
  if (domain==null || domain=='') {
    alert(this.i18n.get("/js/adm/domain/domainNameIsEmpty"));
  } else if	(checkDomain (domain+"."+tld)) {
    showCompById("searchIframe");
    hideCompById("resultIframe");
    var notSupported = tld == "es" ? "&supported=no&noTldDomain="+escape(domain) : "";
    window.resultIframe.location = "/iw/adm/domain/checkDomain.jsp?domain="+escape(domain)+"."+tld+notSupported;
  }
}
IwAdmDomains.prototype.popup = function(windowUrl) {
  var w = 550;
  var h = 350;
  var l = Math.floor((screen.width-w)/2);
  var t = Math.floor((screen.height-h)/2);
  var page = window.open(windowUrl+'?whoisdomain='+escape(this.f_domainName.value)+'&ext='+escape(this.f_tld.value),'checkDomain','width=' + w + ',height=' + h + ',top=' + t + ',left=' + l +',status=yes, menubar=no, toolbar=no, scrollbars=yes, resizable=yes');
  page.focus();
}
