1jQuery(document).ready(function() {
2// Get the iphelper
3var iphelper = document.getElementById("myiphelper");
4var iphelpersubnetcalc = document.getElementById("calc");
5
6// Get the <span> element that closes the iphelper
7var span = document.querySelector('.iphelperclose');
8// When the user clicks on <span> (x), close the iphelper
9span.onclick = function () {
10    iphelper.style.display = "none";
11};
12
13// When the user clicks anywhere outside of the iphelper, close it
14window.onclick = function(event) {
15    if (event.target == iphelper) {
16        iphelper.style.display = "none";
17    }
18};
19
20
21
22
23
24
25jQuery(".iphelper").click(function() {
26iphelper.style.display = "block";
27var iphelperaddress = jQuery(this).text();
28document.getElementById("iphelperinput").value = iphelperaddress;
29if (iphelperaddress.search("/") != -1) {
30    iphelpersubnetcalc.style.display = "block";
31	document.getElementById("iphelpersubnetcalcsubnetinput").value = "";
32	document.getElementById("iphelpersubnetcalcinput").value = iphelperaddress;
33	var iphelperaddressright = iphelperaddress.split("/")[1];
34	if (iphelperaddressright == "1" || iphelperaddressright == "2" || iphelperaddressright == "3" || iphelperaddressright == "4" || iphelperaddressright == "5" || iphelperaddressright == "6" || iphelperaddressright == "7" || iphelperaddressright == "8" || iphelperaddressright == "9" || iphelperaddressright == "10" || iphelperaddressright == "11" || iphelperaddressright == "12" || iphelperaddressright == "13" || iphelperaddressright == "14" || iphelperaddressright == "15" || iphelperaddressright == "16" || iphelperaddressright == "17" || iphelperaddressright == "18" || iphelperaddressright == "19" || iphelperaddressright == "20" || iphelperaddressright == "21" || iphelperaddressright == "22" || iphelperaddressright == "23" || iphelperaddressright == "24" || iphelperaddressright == "25" || iphelperaddressright == "26" || iphelperaddressright == "26" || iphelperaddressright == "27" || iphelperaddressright == "28" || iphelperaddressright == "29" || iphelperaddressright == "30" || iphelperaddressright == "31" || iphelperaddressright == "32") {
35	} else {document.getElementById("iphelpersubnetcalcsubnetinput").value = iphelperaddressright;
36		document.getElementById("iphelpersubnetcalcinput").value = iphelperaddress.split("/")[0];
37		console.log('CIDR in input address is out of range');
38	}
39    document.getElementById("iphelperbodyp").innerHTML = document.getElementById("iphelpertemplatemask").innerHTML.replace(/\%ip\%/g, iphelperaddress);
40	ipChange();
41} else {
42    iphelpersubnetcalc.style.display = "none";
43    document.getElementById("iphelperbodyp").innerHTML = document.getElementById("iphelpertemplate").innerHTML.replace(/\%ip\%/g, iphelperaddress);
44}
45});
46
47
48
49jQuery( "#iphelperinput" ).keyup(function() {
50var iphelperaddress = document.getElementById("iphelperinput").value;
51if (iphelperaddress.search("/") != -1) {
52    iphelpersubnetcalc.style.display = "block";
53	document.getElementById("iphelpersubnetcalcsubnetinput").value = "";
54	document.getElementById("iphelpersubnetcalcinput").value = iphelperaddress;
55	var iphelperaddressright = iphelperaddress.split("/")[1];
56	if (iphelperaddressright == "1" || iphelperaddressright == "2" || iphelperaddressright == "3" || iphelperaddressright == "4" || iphelperaddressright == "5" || iphelperaddressright == "6" || iphelperaddressright == "7" || iphelperaddressright == "8" || iphelperaddressright == "9" || iphelperaddressright == "10" || iphelperaddressright == "11" || iphelperaddressright == "12" || iphelperaddressright == "13" || iphelperaddressright == "14" || iphelperaddressright == "15" || iphelperaddressright == "16" || iphelperaddressright == "17" || iphelperaddressright == "18" || iphelperaddressright == "19" || iphelperaddressright == "20" || iphelperaddressright == "21" || iphelperaddressright == "22" || iphelperaddressright == "23" || iphelperaddressright == "24" || iphelperaddressright == "25" || iphelperaddressright == "26" || iphelperaddressright == "26" || iphelperaddressright == "27" || iphelperaddressright == "28" || iphelperaddressright == "29" || iphelperaddressright == "30" || iphelperaddressright == "31" || iphelperaddressright == "32") {
57		} else {
58			document.getElementById("iphelpersubnetcalcsubnetinput").value = iphelperaddressright;
59			document.getElementById("iphelpersubnetcalcinput").value = iphelperaddress.split("/")[0];
60	}
61    document.getElementById("iphelperbodyp").innerHTML = document.getElementById("iphelpertemplatemask").innerHTML.replace(/\%ip\%/g, iphelperaddress);
62	ipChange();
63} else {
64    iphelpersubnetcalc.style.display = "none";
65    document.getElementById("iphelperbodyp").innerHTML = document.getElementById("iphelpertemplate").innerHTML.replace(/\%ip\%/g, iphelperaddress);
66}});
67
68
69
70
71//Subnetcalc - written by Oscar Virot. All Rights reserved 2014
72// https://google.com/+OscarVirot?rel=author
73// Source https://tools.virot.eu/ipcalc/
74
75var cidrTosubnets = ["0.0.0.0", "128.0.0.0", "192.0.0.0", "224.0.0.0", "240.0.0.0", "248.0.0.0", "252.0.0.0", "254.0.0.0", "255.0.0.0", "255.128.0.0", "255.192.0.0", "255.224.0.0", "255.240.0.0", "255.248.0.0", "255.252.0.0", "255.254.0.0", "255.255.0.0", "255.255.128.0", "255.255.192.0", "255.255.224.0", "255.255.240.0", "255.255.248.0", "255.255.252.0", "255.255.254.0", "255.255.255.0", "255.255.255.128", "255.255.255.192", "255.255.255.224", "255.255.255.240", "255.255.255.248", "255.255.255.252", "255.255.255.254", "255.255.255.255"];
76var ipPattern = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
77var PatternAddr = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
78var PatternSubnet = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
79var PatternCidr = /10|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|1|2|3|4|5|6|7|8|9/;
80
81turladdr=(document.URL).replace(/.*\/ipcalc\/(.*)\/.*/, '$1').replace(/#$/, '');
82turlcidr=(document.URL).replace(/.*\/ipcalc\/.*\/(.*)/, '$1').replace(/#$/, '');
83if (turladdr.match(PatternAddr) == turladdr && turlcidr.match(PatternCidr) == turlcidr)
84  {
85   tinSubnet = cidrTosubnets[(parseInt(turlcidr,10))];
86   document.forms.input.in_address.value = turladdr;
87   document.forms.input.in_subnet.value = tinSubnet;
88   checkAndUpdate();
89  }
90
91function ipChange() {
92  if (document.forms.input.in_address.value.indexOf('/') > -1)
93    {
94      var tcidr = (document.forms.input.in_address.value).substring(document.forms.input.in_address.value.indexOf('/') + 1);
95      if (tcidr !== '' && isFinite(tcidr) && tcidr >= 0 && tcidr <= 32)
96        {
97          document.forms.input.in_subnet.value = cidrTosubnets[(parseInt(tcidr,10))];
98//          document.getElementById('out_cidr').value = tcidr
99//          document.getElementById('out_subnet').value = cidrTosubnets[tcidr]
100        }
101    }
102  checkAndUpdate();
103}
104
105function subChange()
106{
107  if (document.forms.input.in_address.value.indexOf('/') > -1)
108    {
109      var address = (document.forms.input.in_address.value).substring(0,document.forms.input.in_address.value.indexOf('/'));
110//Find the CIDR of the subnetmask
111      var i;
112      for (i = 0; i < cidrTosubnets.length; i++)
113      {
114        if (document.forms.input.in_subnet.value == cidrTosubnets[i]){var tcidr = i;}
115      }
116      if (tcidr !== '' && isFinite(tcidr) && tcidr >= 0 && tcidr <= 32)
117      {
118        document.forms.input.in_address.value = address+"/"+tcidr;
119//      document.getElementById("out_cidr").innerHTML = tcidr
120//      document.getElementById("out_subnet").innerHTML = cidrTosubnets[tcidr]
121      }
122    }
123  checkAndUpdate();
124}
125function checkAndUpdate()
126{
127  if (checkValid())
128  {
129    UpdateOutput();
130  }
131  else
132  {
133    InvalidateOutput();
134  }
135}
136function checkValid() {
137//validate CIDR range
138  if (document.forms.input.in_address.value.indexOf('/') > -1)
139  {
140    var tcidr = (document.forms.input.in_address.value).substring(document.forms.input.in_address.value.indexOf('/') + 1);
141    if (tcidr === '' || isNaN(tcidr) || tcidr <= 0 || tcidr > 32
142)
143    {
144      console.log('CIDR in input address is out of range');
145      return false;
146    }
147  }
148//validate address
149  if (document.forms.input.in_address.value.indexOf('/') > -1)
150  {
151    var taddress = (document.forms.input.in_address.value).substring(0,document.forms.input.in_address.value.indexOf('/'));
152  }
153  else
154  {
155    var taddress = document.forms.input.in_address.value;
156  }
157 //format
158  if (taddress.match(ipPattern) != taddress)
159  {
160    console.log('IP Address not formated x.x.x.x -> xxx.xxx.xxx.xxx');
161    return false;
162  }
163 //nothing over 255
164  var tbytes = taddress.split(".");
165  var i;
166    for (i = 0; i <= 3; i++)
167    {
168      if (tbytes[i]>=256)
169      {
170        console.log('IP address octect is larger than 255');
171        return false;
172      }
173    }
174//validate subnet
175 //format
176  if (document.forms.input.in_subnet.value.match(ipPattern) != document.forms.input.in_subnet.value)
177  {
178    console.log('Subnetmask not formated x.x.x.x -> xxx.xxx.xxx.xxx');
179    return false;
180  }
181 //bit ordering
182  for (i = 0; i < cidrTosubnets.length; i++)
183  {
184    if (document.forms.input.in_subnet.value == cidrTosubnets[i]){var tcidr = i;}
185  }
186  if (tcidr === '' || isNaN(tcidr))
187  {
188    console.log('Subnetmask improper ordering, not in order');
189    return false;
190  }
191  return true;
192}
193function InvalidateOutput()
194{
195  document.getElementById("out_address").innerHTML = '';
196  document.getElementById("out_subnet").innerHTML = '';
197  document.getElementById("out_cidr").innerHTML = '';
198  document.getElementById("out_netaddr").innerHTML = '';
199  document.getElementById("out_bcast").innerHTML = '';
200  document.getElementById("out_firstusable").innerHTML = '';
201  document.getElementById("out_lastusable").innerHTML = '';
202  document.getElementById("out_amountaddresses").innerHTML = '';
203  document.getElementById("out_ptraddr").innerHTML = '';
204  document.getElementById("is_valid").innerHTML = '<center>INVALID</center>';
205  document.getElementById("is_valid").style.display = 'block';
206  document.getElementById("is_valid").style.backgroundColor = "red";
207}
208
209function UpdateOutput()
210{
211// Update Valid text
212  document.getElementById("is_valid").style.display = 'none';
213  document.getElementById("is_valid").innerHTML = 'VALID VALID VALID VALID VALID';
214  document.getElementById("is_valid").style.backgroundColor = "green";
215// Locate Address
216  if (document.forms.input.in_address.value.indexOf('/') > -1)
217    {
218      var taddress = (document.forms.input.in_address.value).substring(0,document.forms.input.in_address.value.indexOf('/'));
219    }
220  else
221    {
222      var taddress = document.forms.input.in_address.value;
223    }
224//Do subnet calc
225  var i;
226    for (i = 0; i < cidrTosubnets.length; i++)
227    {
228      if (document.forms.input.in_subnet.value == cidrTosubnets[i]){var tcidr = i;}
229    }
230//Calculate Network & Broadcast addresses
231  var tabytes = taddress.split(".");
232  var tsbytes = document.forms.input.in_subnet.value.split(".");
233 //Network address
234  var tnaddr = (tabytes[0] & tsbytes[0]) + "." + (tabytes[1] & tsbytes[1]) + "." + (tabytes[2] & tsbytes[2]) + "." + (tabytes[3] & tsbytes[3]);
235 //Broadcast address
236  var tbaddr = ((tabytes[0] & tsbytes[0]) | (255 ^ tsbytes[0])) + "." + ((tabytes[1] & tsbytes[1]) | (255 ^ tsbytes[1])) + "." + ((tabytes[2] & tsbytes[2]) | (255 ^ tsbytes[2])) + "." + ((tabytes[3] & tsbytes[3]) | (255 ^ tsbytes[3]));
237 //Reverse PTR
238  var tptraddr = (tabytes[3] + '.' + tabytes[2] + '.' + tabytes[1] + '.' + tabytes[0] + '.in-addr.arpa');
239if (tcidr == 32)
240{
241 //gw1 address
242  var tgw1 = tnaddr;
243 //gw2 address
244  var tgw2 = tnaddr;
245 // Count usable addresses
246 tusable = 1;
247}
248if (tcidr != 32)
249{
250 //gw1 address
251  var tgw1 = (tabytes[0] & tsbytes[0]) + "." + (tabytes[1] & tsbytes[1]) + "." + (tabytes[2] & tsbytes[2]) + "." + ((tabytes[3] & tsbytes[3])+1);
252 //gw2 address
253  var tgw2 = ((tabytes[0] & tsbytes[0]) | (255 ^ tsbytes[0])) + "." + ((tabytes[1] & tsbytes[1]) | (255 ^ tsbytes[1])) + "." + ((tabytes[2] & tsbytes[2]) | (255 ^ tsbytes[2])) + "." + (((tabytes[3] & tsbytes[3]) | (255 ^ tsbytes[3]))-1);
254 // Count usable addresses
255 tusable = (Math.pow(2, (32-tcidr)))-2;
256}
257
258//Do real outputing
259  document.getElementById("out_address").innerHTML = taddress;
260  document.getElementById("out_subnet").innerHTML = document.forms.input.in_subnet.value;
261  document.getElementById("out_cidr").innerHTML = tcidr;
262  document.getElementById("out_netaddr").innerHTML = tnaddr;
263  document.getElementById("out_bcast").innerHTML = tbaddr;
264  document.getElementById("out_firstusable").innerHTML = tgw1;
265  document.getElementById("out_lastusable").innerHTML = tgw2;
266  document.getElementById("out_amountaddresses").innerHTML = tusable;
267  document.getElementById("out_ptraddr").innerHTML = tptraddr;
268
269}
270
271function changeSection(Section)
272{
273  document.getElementById("static").className = 'hiddensection';
274  document.getElementById("calc").className = 'hiddensection';
275  document.getElementById("calcSelector").className = 'sectionselector';
276  document.getElementById("staticSelector").className = 'sectionselector';
277
278  if (Section=="calc")
279  {
280    document.getElementById("calc").className = 'section';
281    document.getElementById("calcSelector").className = 'sectionselectoractive';
282  }
283  if (Section=="static")
284  {
285    document.getElementById("static").className = 'section';
286    document.getElementById("staticSelector").className = 'sectionselectoractive';
287  }
288  if (Section=="IPv6Static")
289  {
290    document.getElementById("IPv6Static").className = 'hiddensection';
291    document.getElementById("IPv6StaticSelector").className = 'sectionselector';
292  }
293}
294});