states = new Array();

function Japan_prefectures() {
  states[0] = 'Hokkaido';
  states[1] = 'Aomori';
  states[2] = 'Iwate';
  states[3] = 'Akita';
  states[4] = 'Miyagi';
  states[5] = 'Yamagata';
  states[6] = 'Fukushima';
  states[7] = 'Ibaraki';
  states[8] = 'Tochigi';
  states[9] = 'Gumma';
  states[10] = 'Chiba';
  states[11] = 'Saitama';
  states[12] = 'Tokyo';
  states[13] = 'Kanagawa';
  states[14] = 'Niigata';
  states[15] = 'Toyama';
  states[16] = 'Ishikawa';
  states[17] = 'Nagano';
  states[18] = 'Gifu';
  states[19] = 'Fukui';
  states[20] = 'Yamanashi';
  states[21] = 'Shizuoka';
  states[22] = 'Aichi';
  states[23] = 'Shiga';
  states[24] = 'Kyoto';
  states[25] = 'Hyogo';
  states[26] = 'Mie';
  states[27] = 'Nara';
  states[28] = 'Osaka';
  states[29] = 'Wakayama';
  states[30] = 'Tottori';
  states[31] = 'Shimane';
  states[32] = 'Okyama';
  states[33] = 'Hiroshima';
  states[34] = 'Yamaguchi';
  states[35] = 'Kagawa';
  states[36] = 'Tokushima';
  states[37] = 'Ehime';
  states[38] = 'Kochi';
  states[39] = 'Fukuoka';
  states[40] = 'Saga';
  states[41] = 'Nagasaki';
  states[42] = 'Oita';
  states[43] = 'Kumamoto';
  states[44] = 'Miyazaki';
  states[45] = 'Kagoshima';
  states[46] = 'Okinawa';
}

function Australia_states() {
  states[0] = 'Australian Capital Territory';
  states[1] = 'New South Wales';
  states[2] = 'Victoria';
  states[3] = 'Queensland';
  states[4] = 'South Australia';
  states[5] = 'Western Australia';
  states[6] = 'Tasmania';
  states[7] = 'Northern Territory';
}

function UK_counties() {
  states[0] = '-- England';
  states[1] = 'Bedfordshire';
  states[2] = 'Berkshire';
  states[3] = 'Buckinghamshire';
  states[4] = 'Cambridgeshire';
  states[5] = 'Cheshire';
  states[6] = 'Cornwall';
  states[7] = 'Cumberland';
  states[8] = 'Derbyshire';
  states[9] = 'Devon';
  states[10] = 'Dorset';
  states[11] = 'Durham';
  states[12] = 'Essex';
  states[13] = 'Gloucestershire';
  states[14] = 'Hampshire';
  states[15] = 'Herefordshire';
  states[16] = 'Hertfordshire';
  states[17] = 'Huntingdonshire';
  states[18] = 'Isle of Man';
  states[19] = 'Kent';
  states[20] = 'Lancashire';
  states[21] = 'Leicstershire';
  states[22] = 'Lincolnshire';
  states[23] = 'Middlesex';
  states[24] = 'Norfolk';
  states[25] = 'Northamptonshire';
  states[26] = 'Northumberland';
  states[27] = 'Nottinghamshire';
  states[28] = 'Oxfordshire';
  states[29] = 'Rutland';
  states[30] = 'Shropshire';
  states[31] = 'Somerset';
  states[32] = 'Staffordshire';
  states[33] = 'Suffolk';
  states[34] = 'Surrey';
  states[35] = 'Sussex';
  states[36] = 'Warwickshire';
  states[37] = 'Westmorland';
  states[38] = 'Wiltshire';
  states[39] = 'Worcestershire';
  states[40] = 'Yorkshire';
  states[41] = '-- Wales';
  states[42] = 'Anglesey/Sir Fon';
  states[43] = 'Brecknockshire/Sir Frycheiniog';
  states[44] = 'Caernarfonshire/Sir Gaernarfon';
  states[45] = 'Carmarthenshire/Sir Gaerfyrddin';
  states[46] = 'Cardiganshire/Ceredigion';
  states[47] = 'Denbighshire/Sir Ddinbych';
  states[48] = 'Flintshire/Sir Fflint';
  states[49] = 'Glamorgan/Morgannwg';
  states[50] = 'Merioneth/Meirionnydd';
  states[51] = 'Monmouthshire/Sir Fynwy';
  states[52] = 'Montgomeryshire/Sir Drefaldwyn';
  states[53] = 'Pembrokeshire/Sir Benfro';
  states[54] = 'Radnorshire/Sir Faesyfed';
  states[55] = '-- Scotland';
  states[56] = 'Aberdeenshire';
  states[57] = 'Angus/Forfarshire';
  states[58] = 'Argyllshire';
  states[59] = 'Ayrshire';
  states[60] = 'Banffshire';
  states[61] = 'Berwickshire';
  states[62] = 'Buteshire';
  states[63] = 'Cromartyshire';
  states[64] = 'Caithness';
  states[65] = 'Clackmannanshire';
  states[66] = 'Dumfriesshire';
  states[67] = 'Dunbartonshire/Dumbartonshire';
  states[68] = 'East Lothian/Haddingtonshire';
  states[69] = 'Fife';
  states[70] = 'Inverness-shire';
  states[71] = 'Kincardineshire';
  states[72] = 'Kinross-shire';
  states[73] = 'Kirkcudbrightshire';
  states[74] = 'Lanarkshire';
  states[75] = 'Midlothian/Edinburghshire';
  states[76] = 'Morayshire';
  states[77] = 'Nairnshire';
  states[78] = 'Orkney';
  states[79] = 'Peeblesshire';
  states[80] = 'Perthshire';
  states[81] = 'Renfrewshire';
  states[82] = 'Ross-shire';
  states[83] = 'Roxburghshire';
  states[84] = 'Selkirkshire';
  states[85] = 'Shetland';
  states[86] = 'Stirlingshire';
  states[87] = 'Sutherland';
  states[88] = 'West Lothian/Linlithgowshire';
  states[89] = 'Wigtownshire';
  states[90] = '-- Northern Ireland';
  states[91] = 'Antrim';
  states[92] = 'Armagh';
  states[93] = 'Down';
  states[94] = 'Fermanagh';
  states[95] = 'Londonderry';
  states[96] = 'Tyrone';
}

function Canada_provinces() {
  states[0] = 'Alberta';
  states[1] = 'British Columbia';
  states[2] = 'Manitoba';
  states[3] = 'New Brunswick';
  states[4] = 'Newfoundland and Labrador';
  states[5] = 'Northwest Territories';
  states[6] = 'Nova Scotia';
  states[7] = 'Nunavut';
  states[8] = 'Ontario';
  states[9] = 'Prince Edward Island';
  states[10] = 'Quebec';
  states[11] = 'Saskatchewan';
  states[12] = 'Yukon Territory';
}

function US_states() {
  states[0] = 'Alabama';
  states[1] = 'Alaska';
  states[2] = 'American Samoa';
  states[3] = 'Arizona';
  states[4] = 'Arkansas';
  states[5] = 'California';
  states[6] = 'Colorado';
  states[7] = 'Connecticut';
  states[8] = 'Delaware';
  states[9] = 'District of Columbia';
  states[10] = 'Federated States of Micronesia';
  states[11] = 'Florida';
  states[12] = 'Georgia';
  states[13] = 'Guam';
  states[14] = 'Hawaii';
  states[15] = 'Idaho';
  states[16] = 'Illinois';
  states[17] = 'Indiana';
  states[18] = 'Iowa';
  states[19] = 'Kansas';
  states[20] = 'Kentucky';
  states[21] = 'Louisiana';
  states[22] = 'Maine';
  states[23] = 'Marshall Islands';
  states[24] = 'Maryland';
  states[25] = 'Massachusetts';
  states[26] = 'Michigan';
  states[27] = 'Minnesota';
  states[28] = 'Mississippi';
  states[29] = 'Missouri';
  states[30] = 'Montana';
  states[31] = 'Nebraska';
  states[32] = 'Nevada';
  states[33] = 'New Hampshire';
  states[34] = 'New Jersey';
  states[35] = 'New Mexico';
  states[36] = 'New York';
  states[37] = 'North Carolina';
  states[38] = 'North Dakota';
  states[39] = 'Northern Mariana Islands';
  states[40] = 'Ohio';
  states[41] = 'Oklahoma';
  states[42] = 'Oregon';
  states[43] = 'Palau';
  states[44] = 'Pennsylvania';
  states[45] = 'Puerto Rico';
  states[46] = 'Rhode Island';
  states[47] = 'South Carolina';
  states[48] = 'South Dakota';
  states[49] = 'Tennessee';
  states[50] = 'Texas';
  states[51] = 'Utah';
  states[52] = 'Vermont';
  states[53] = 'Virginia';
  states[54] = 'Virgin Islands';
  states[55] = 'Washington';
  states[56] = 'West Virginia';
  states[57] = 'Wisconsin';
  states[58] = 'Wyoming';
  states[59] = 'Armed Forces Africa';
  states[60] = 'Armed Forces Americas';
  states[61] = 'Armed Forces Canada';
  states[62] = 'Armed Forces Europe';
  states[63] = 'Armed Forces Middle East';
  states[64] = 'Armed Forces Pacific';
}

function updatestates() {
  this.document.register.state.options.length = 0;
  states.length = 0;
  if (this.document.register.country.options[this.document.register.country.selectedIndex].value == 225) US_states();
  if (this.document.register.country.options[this.document.register.country.selectedIndex].value == 37) Canada_provinces();
  if (this.document.register.country.options[this.document.register.country.selectedIndex].value == 223) UK_counties();
  if (this.document.register.country.options[this.document.register.country.selectedIndex].value == 15) Australia_states();
  if (this.document.register.country.options[this.document.register.country.selectedIndex].value == 111) Japan_prefectures();
  for (i=0; i < states.length; i++) {
     if (states[i] == '') continue;
     this.document.register.state.options[i] = new Option(states[i], i);
  }
  if (states.length == 0) {
    this.document.register.state.options[0] = new Option("--------------------------------------------------------------", 0);
    this.document.register.state.disabled = true;
  } else {
    this.document.register.state.disabled = false;
  }
}

function checkDomain(nname) {
  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','.eu');

  var mai = nname;
  var val = true;

  var dot = mai.lastIndexOf(".");
  var dname = mai.substring(0,dot);
  var ext = mai.substring(dot,mai.length);
	
  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) {
//      alert ("Invalid domain! Domain name extension "+ext+" is invalid!");
//      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) {
            alert ("Invalid domain! Domain name should not begin or end with '-'");
            return false;
          }
        } else {
          alert("Invalid domain! Domain name should not have special characters!");
          return false;
        }
      }
//    }
  } else {
    alert("Invalid domain! Domain name is either too short or too long or has a missing domain extension, such .com, .net, .org!");
    return false;
  }	
  return true;
}

var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters;
var minDigitsInIPhoneNumber = 8;

function isInteger(s) {
   var i;
   for (i = 0; i < s.length; i++) {   
     var c = s.charAt(i);
     if (((c < "0") || (c > "9"))) return false;
   }
   return true;
}

function validateZIP(postalCode) {
  var validFormat = /^(\d{5})(-\d{4})?$/i
  var res = validFormat.exec(postalCode);
  if (res == null) res = false;
  return res;
}

function validateUK(postalCode) {
  var validFormat = /(^[a-z][a-z]?\d\d? ?\d[a-z][a-z])$/i
  var res = validFormat.exec(postalCode);
  if (res == null) res = false;
  return res;
}

function validate_username(username) {
  var validFormat = /\W/;
  var res = validFormat.exec(username);
  if (res) res = false; else res = true;
  return res;
}

function validate_userlen(username) {
  var ln = username.length;
  if (ln < 2 || ln > 8) return false;
  return true;
}

function check_and_submit() {
  if (this.document.register.firstname.value == "") {
    alert("Please enter your first name!");
    this.document.register.firstname.focus();
    this.document.register.firstname.className = "textbar-reg-firstname-red";
    return false;
  }
  this.document.register.firstname.className = "textbar-reg-firstname";

  if (this.document.register.lastname.value == "") {
    alert("Please enter your last name!");
    this.document.register.lastname.focus();
    this.document.register.lastname.className = "textbar-reg-red";
    return false;
  }
  this.document.register.lastname.className = "textbar-reg";

  if (this.document.register.address1.value == "") {
    alert("Please enter at least address line 1!");
    this.document.register.address1.focus();
    this.document.register.address1.className = "textbar-reg-red";
    return false;
  }
  this.document.register.address1.className = "textbar-reg";

  if (this.document.register.city.value == "") {
    alert("Please enter your city or town name!");
    this.document.register.city.focus();
    this.document.register.city.className = "textbar-reg-red";
    return false;
  }
  this.document.register.city.className = "textbar-reg";

  if (this.document.register.zip.value == "") {
    alert("Please enter your ZIP or postal code!");
    this.document.register.zip.focus();
    this.document.register.zip.className = "textbar-reg-zip-red";
    return false;
  }
  this.document.register.zip.className = "textbar-reg-zip";

  if (this.document.register.country.options[this.document.register.country.selectedIndex].value == 225 && validateZIP(this.document.register.zip.value) == false) {
    alert("Invalid ZIP code! The ZIP code must be in the format xxxxx or xxxxx-xxxx");
    this.document.register.zip.focus();
    this.document.register.zip.className = "textbar-reg-zip-red";
    return false;
  }

  if (this.document.register.country.options[this.document.register.country.selectedIndex].value == 223 && validateUK(this.document.register.zip.value) == false) {
    alert("Invalid postal code code! The postal code must be in the format XX or XXX XXX");
    this.document.register.zip.focus();
    this.document.register.zip.className = "textbar-reg-zip-red";
    return false;
  }

  if (this.document.register.phone_code.value == "") {
    alert("Please enter your country dialing code!");
    this.document.register.phone_code.focus();
    this.document.register.phone_code.className = "textbar-reg-phonecode-red";
    return false;
  }
  this.document.register.phone_code.className = "textbar-reg-phonecode";

  if (this.document.register.phone_area.value == "") {
    alert("Please enter your phone number area code!");
    this.document.register.phone_area.focus();
    this.document.register.phone_area.className = "textbar-reg-phonecode-red";
    return false;
  }

  if (isInteger(this.document.register.phone_area.value) == false) {
    alert("Invalid phone area number! Please enter a valid phone area number. Digits only. For example 555.")
    this.document.register.phone_area.focus();
    this.document.register.phone_area.className = "textbar-reg-phonecode-red";
    return false
  }
  this.document.register.phone_area.className = 'textbar-reg-phonecode';

  if (this.document.register.phone.value == "") {
    alert("Please enter your phone number!");
    this.document.register.phone.focus();
    this.document.register.phone.className = "textbar-reg-phone-red";
    return false;
  }

  if (isInteger(this.document.register.phone.value) == false) {
    alert("Invalid phone number! Please enter a valid phone number. Digits only. For example 5555555.")
    this.document.register.phone.focus();
    this.document.register.phone.className = "textbar-reg-phone-red";
    return false
  }
  this.document.register.phone.className = 'textbar-reg-phone';

  if (this.document.register.email.value == "") {
    alert("Please enter an e-mail address for contacts!");
    this.document.register.email.focus();
    this.document.register.email.className = "textbar-reg-red";
    return false;
  }

  if (!emailCheck(this.document.register.email.value)) {
    alert("Invalid e-mail address! Please, provide a valid e-mail contact address.");
    this.document.register.email.select();
    this.document.register.email.className = "textbar-reg-red";
    return false;
  }
  this.document.register.email.className = "textbar-reg";

  if (this.document.register.domain.value == "") {
    alert("Please enter a domain name for your account!");
    this.document.register.domain.focus();
    this.document.register.domain.className = "textbar-reg-red";
    return false;
  }

  if (checkDomain(this.document.register.domain.value) == false) {
    alert("Ivalid domain name specified!");
    this.document.register.domain.focus();
    this.document.register.domain.className = "textbar-reg-red";
    return false;
  }

  if (this.document.register.domain.value.substring(0, 4) == "www.") {
    alert("Ivalid domain name specified! The domain name should not begin with www. (it is automatically added after your registration)");
    this.document.register.domain.focus();
    this.document.register.domain.className = "textbar-reg-red";
    return false;
  }
  this.document.register.domain.className = "textbar-reg";

  if (this.document.register.username.value == "") {
    alert("Please enter your desired web hosting account username!");
    this.document.register.username.focus();
    this.document.register.username.className = "textbar-reg-red";
    return false;
  }

  if (validate_username(this.document.register.username.value) == false) {
    alert("Invalid username specified! The username may contain only the letters from a-z and numbers from 0-9 but it must start with a letter.")
    this.document.register.username.focus();
    this.document.register.username.className = "textbar-reg-red";
    return false;
  }

  if (validate_userlen(this.document.register.username.value) == false) {
    alert("Invalid username specified! The username must be at least 2 characters or maximum 8 characters long.")
    this.document.register.username.focus();
    this.document.register.username.className = "textbar-reg-red";
    return false;
  }
  this.document.register.username.className = "textbar-reg";

  if (this.document.register.pass1.value == "") {
    alert("Please enter your desired web hosting account password!");
    this.document.register.pass1.focus();
    this.document.register.pass1.className = "textbar-reg-red";
    return false;
  }
  this.document.register.pass1.className = "textbar-reg";

  if (this.document.register.pass2.value == "") {
    alert("Please re-enter your desired web hosting account password!");
    this.document.register.pass2.focus();
    this.document.register.pass2.className = "textbar-reg-red";
    return false;
  }

  if (this.document.register.pass1.value != this.document.register.pass2.value) {
    alert("The two entered passwords do not match! Please enter the same password twice in order for us to be able to verify that you did not make mistake while entering your password the first time.");
    this.document.register.pass2.focus();
    this.document.register.pass2.className = "textbar-reg-red";
    return false;
  }
  this.document.register.pass2.className = "textbar-reg";

  var SelOption = -1;
  var payobj = this.document.register.paymentcycle;
  var paylen = payobj.length;
  if (paylen == undefined) if (payobj.checked) SelOption = payobj.value; else SelOption -1;
  for (j = 0; j < paylen; j++) {
    if (payobj[j].checked == true) SelOption = j;
  }
  if (SelOption == -1) {
    alert("Please select your billing cycle before submitting the registration form.");
    return false;
  }

  if (!this.document.register.agree.checked) {
    alert("Before registering with us you must agree to the terms and conditions.");
    this.document.register.agree.focus();
    this.document.register.agree.className = "errorline";
    return false;
  }
  this.document.register.agree.className = "textline";

  if (this.document.register.state.selectedIndex > 0) {
    this.document.register.state2.value = states[this.document.register.state.options[this.document.register.state.selectedIndex].value];
  }

  this.document.register.submit();
  return true;
}
