	function getValueByID(id) {
		//alert(id);
		if (parent.document.getElementById(id) != null)
		{
			return parent.document.getElementById(id).value;
		}
	}

//from line RASCOCLM.[Unit1.pas]:1553
function update(){
	
	ip_class = document.getElementById('cye_subnet_class').value;
	start = 0;
	
    prev_power_1st  = 0;
    prev_power_2nd  = 0;
    prev_power_3rd  = 0;

    noe_subnets =  0;
    noe_hosts =  0;

	cye_slash_class = getValueByID('cye_slash_class');
	//alert(cye_slash_class);

	switch (ip_class) {
		case 'no_value':
				document.getElementById('cye_subnet_sm').value = '255.0.0.0';
				start=8;
				break;
		case 'A':
				document.getElementById('cye_subnet_sm').value = '255.0.0.0';
				start = 8;
				for (i = start+1; i <= cye_slash_class; i++) {
					if (i <= 16) {
						prev_power_1st = prev_power_1st  + Math.pow(2, 16 - i);
						document.getElementById('cye_subnet_sm').value = '255.' + prev_power_1st + '.0.0';
						//alert('prev_power_1st=' + prev_power_1st);
					}

					if ((i > 16) && (i <= 24)) {
						prev_power_2nd  =  prev_power_2nd  + Math.pow(2, 24 - i);
						document.getElementById('cye_subnet_sm').value = '255.255.' + prev_power_2nd + '.0';
					}

					if (i > 24) {
						prev_power_3rd  =  prev_power_3rd  + Math.pow(2, 32 - i);
						document.getElementById('cye_subnet_sm').value = '255.255.255.' + prev_power_3rd;
					}
				}//for
					
				noe_subnets =  Math.pow(2, cye_slash_class - 8);
				noe_hosts   =  Math.pow(2, 32 - cye_slash_class);
				break;
		case 'B':
				document.getElementById('cye_subnet_sm').value = '255.255.0.0';
				start = 16;
				for (i = start+1; i <= cye_slash_class; i++) {
					if (i <= 24) {
						prev_power_1st = prev_power_1st  + Math.pow(2, 24 - i);
						document.getElementById('cye_subnet_sm').value = '255.255.' + prev_power_1st + '.0';
					}

					if (i > 24) {
						prev_power_2nd  =  prev_power_2nd  + Math.pow(2, 32 - i);
						document.getElementById('cye_subnet_sm').value = '255.255.255.' + prev_power_2nd;
					}
				}//for

				noe_subnets =  Math.pow(2, cye_slash_class - 16);
				noe_hosts   =  Math.pow(2, 32 - cye_slash_class);
				break;
		case 'C':	
				document.getElementById('cye_subnet_sm').value = '255.255.255.0';
				//document.getElementById('div_cye_slash_class').innerHTML = 'Slash class:C';
				start = 24;
				for (i = start+1; i <= cye_slash_class; i++) {
					prev_power_1st = prev_power_1st  + Math.pow(2, 32 - i);
					document.getElementById('cye_subnet_sm').value = '255.255.255.' + prev_power_1st;
				}

				noe_subnets =  Math.pow(2, cye_slash_class - 24);
				noe_hosts   =  Math.pow(2, 32 - cye_slash_class);
				break;
	}//switch
	
	slash_class_content = 'Netmask: <select id="cye_slash_class"  onChange="update()">' ;//+ '\n' + 		'<option value="no_value"> </option>' + '\n';
	for (i=start; i <= 32; i++) {
		if (cye_slash_class == i) {			
			slash_class_content = slash_class_content + '<option value="' + i + '" selected> /'+ i + '</option>' + '\n';
		} else {			
			slash_class_content = slash_class_content + '<option value="' + i + '"> /'+ i + '</option>' + '\n';
		}
	}
	slash_class_content = slash_class_content + '</select>';
	document.getElementById('div_cye_slash_class').innerHTML = slash_class_content;

	document.getElementById('cye_total_subnets').value = '' + noe_subnets;
	document.getElementById('cye_total_hosts').value = '' + noe_hosts;

	if (noe_subnets - 2 >= 0) {
		document.getElementById('cye_subnets_to_use').value = '' + (noe_subnets - 2);
	}
    else {
		document.getElementById('cye_subnets_to_use').value = '-';
	}

	if (noe_hosts - 2 >= 0) {
		document.getElementById('cye_hosts_per_subnet').value = '' + (noe_hosts - 2);
	}
    else {
		document.getElementById('cye_hosts_per_subnet').value = '-';
	}

}//update

function getSubnets() {
	//alert('subnets');
	ip = getValueByID('cye_subnet_ip');

	noe_subnets = getValueByID('cye_total_subnets');
	noe_hosts = getValueByID('cye_total_hosts');

	subnets = 'Sub #<span id="space"></space>Network IP<span id="space"></space>Host Range<span id="space"></space>Broadcast IP\n<BR>';
    subnets = subnets + '--------------------------------------------------------------------\n<BR>';
    subnets = subnets + ' \n<BR>';

	if (noe_subnets < 32768) {
		for (i = 0; i <= noe_subnets - 1; i++) {
			//network ip
			net_ip = setIPByNOEHosts(ip, i * noe_hosts);

			if ( noe_hosts - 2 > 0 ) {
	            host_range  =  setIPByNOEHosts(ip, 1 + i * noe_hosts);
                host_range  =  host_range + ' - ' + setIPByNOEHosts(ip, noe_hosts - 2 + i * noe_hosts);
			} else{
                host_range =  '-';
			}

			bcast_ip    =  setIPByNOEHosts(ip, noe_hosts - 1 + i * noe_hosts);

			subnets = subnets + i + '<span id="space"></space>' + net_ip + '<span id="space"></space>' + host_range + '<span id="space"></space>' + bcast_ip + '\n<BR>';
		}//for
	}else{
		alert('Number of subnets is to big to perform subnetting !');
	}

    subnets = subnets + ' \n<BR>';
    subnets = subnets + ' \n<BR>';
	
    subnets = subnets + '/**\n<BR>';
    subnets = subnets + '&nbsp;*		subnetting provided by http://subnetting.cyecorp.ro\n<BR>';
    subnets = subnets + '&nbsp;*/\n<BR>';

	document.getElementById('subnets').innerHTML = subnets;
}//getSubnets

function setIPByNOEHosts(ipaddress, hosts) {
	first_byte  ="";
	second_byte ="";
	third_byte  ="";
	fourth_byte ="";
	
	dotpos_1    =-1;      //between the 1st and the 2nd byte
	dotpos_2    =-1;      //between the 2nd and the 3rd byte
	dotpos_3    =-1;      //between the 3rd and the 4th byte

	temp_IP     ="";       //temporary IP, working IP address

	m           =-1;
	n           =-1;
	o           =-1;

    dotpos_1    =  ipaddress.indexOf('.');
        first_byte  =  ipaddress.substr(0, dotpos_1);
        temp_IP     =  ipaddress.substr(dotpos_1 + 1, ipaddress.length - first_byte.length + 1);

    dotpos_2    =  temp_IP.indexOf('.');
        second_byte  =  temp_IP.substr(0, dotpos_2);
        temp_IP     =  temp_IP.substr(dotpos_2 + 1, temp_IP.length - second_byte.length + 1);

    dotpos_3    =  temp_IP.indexOf('.');
        third_byte  =  temp_IP.substr(0, dotpos_3);
        temp_IP     =  temp_IP.substr(dotpos_3 + 1, temp_IP.length - third_byte.length + 1);

    fourth_byte =  temp_IP;
	//alert(first_byte + '-' +              second_byte+ '-' +              third_byte + '-' +              fourth_byte);
//-------------------------------------------------------------------
	if ( hosts < 256) {
		fourth_byte   =  hosts;
	}else{
		m = Math.floor(hosts / 256);
		
		if (m < 256) {
			n =  hosts - (256 * m);

			third_byte    =  m;
			fourth_byte   =  n;
		}else {
			m =  Math.floor(m / 256);
			n =  hosts - (256 * 256 * m);
			o =  Math.floor(n / 256);
			if ( o < 256) {
				n =  n - 256 * o;

                second_byte =  m;
                third_byte  =  o;
                fourth_byte =  n;
			}
		}
	}

    return first_byte + '.' +
              second_byte+ '.' +
              third_byte + '.' +
              fourth_byte;
}//setIpByNOEHosts

function saveSubnets() {
	
	ip = getValueByID('cye_subnet_ip');
	//windows.location = 'http://subnetting.cyecorp.ro/saves/subnetting_' + ip + '.txt';

	subnets = document.getElementById('subnets').innerHTML;
	subnets = subnets.replace(/<br>/g, '__newline__');
	subnets = subnets.replace(/<\/span>/g, '');
	subnets = subnets.replace(/<span id=\"space\">/g, '__space__');
	subnets = subnets.replace(/&nbsp;/g, '__nbsp__');
	alert(subnets);

	writeToFile(subnets, ip);
	//alert(subnets);
}

/**
 *		Writes text to a file
 */
function writeToFile(text, ip) {
   try{
      // Opera 8.0+, Firefox, Safari
      xmlhttp_wtf = new XMLHttpRequest();
   } catch (e){
      // Internet Explorer Browsers
      try{
         xmlhttp_wtf = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            xmlhttp_wtf = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
         }
      }
   }

   xmlhttp_wtf.open("GET", 'inc_ajax_write_to_file.php?ip=' + ip + '&text=' + serialize(text), true);
   xmlhttp_wtf.onreadystatechange= function() {
      if (xmlhttp_wtf.readyState==4) {
		 //alert(xmlhttp_wtf.responseText);
		 
		 alert(xmlhttp_wtf.responseText);
      }
   }
   xmlhttp_wtf.send(null);
   return false;
}

function serialize (mixed_value) {
    // Returns a string representation of variable (which can later be unserialized)  
    // 
    // version: 908.406
    // discuss at: http://phpjs.org/functions/serialize
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // +   bugfixed by: Andrej Pavlovic
    // +   bugfixed by: Garagoth
    // +      input by: DtTvB (http://dt.in.th/2008-09-16.string-length-in-bytes.html)
    // +   bugfixed by: Russell Walker (http://www.nbill.co.uk/)
    // +   bugfixed by: Jamie Beck (http://www.terabit.ca/)
    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
    var _getType = function (inp) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = this.serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            */
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += this.serialize(okey) +
                        this.serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
            val = "N";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";
    }
    return val;
}

