// Example usage of this section:
//    
// var Ar = new Array();  
// Ar.push(convert_to_decimal(1.2.3.4));
// Ar.push(convert_to_decimal(1.2.3.5));
// Ar.push(convert_to_decimal(1.2.3.7));
// Ar.push(convert_to_decimal(1.2.3.8));
// 
// missing_bin = find_hole(Ar);
// missing_octet = decimal_to_ip(missing_bin);



// JavaScript sees numbers with leading zeros as octal values, so strip zeros
function stripZeros(inputStr) {
	var result = inputStr;
	while (result.substring(0,1) == "0") {
		result = result.substring(1,result.length);
	}
	return result;
}

// general purpose function to see if an input value has been entered at all
function isEmpty(inputStr) {
	if (inputStr == "" || inputStr === null) {
		return true;
	}
	return false;
}

// general purpose function to see if a suspected numeric input
// is a positive integer
function isNumber(inputStr) {
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.substring(i, i + 1);
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
	}
	return true;
}

// function to determine if value is in acceptable range for this application
function inRange(inputStr) {
	var num = parseInt(inputStr, 10);
	if ((num < 0) || (num > 255)) {
		return false;
	}
	return true;
}

function isValid(inputStr) {
	if (isEmpty(inputStr)) {
		alert("Please enter a number");
		return false;
	} else {
		if (!isNumber(inputStr)) {
			alert("Please enter numbers only");
			return false;
		} else {
			if (!inRange(inputStr)) {
				alert("Valid range is 0 - 255");
				return false;
			}
		}
	}
	return true;
}

//equiv to padl()
function padTextPrefix (InString, PadChar, DefLength) {
	if (InString.length >= DefLength) {
		return (InString);
	}
	var OutString = InString;
	for (var Count = InString.length; Count < DefLength; Count++)  {
		OutString = PadChar + OutString;
	}
	return (OutString);
}

//Decimal to binary, returns an eight character string
function toBin(inVal) {
	var base = 2 ;
	var num = parseInt(inVal, 10);
	var binNum = num.toString(base);
	// pad leading spaces with "0"
	binNum = padTextPrefix(binNum, "0", 8) ;
	return binNum;
}

function convert_to_decimal(dotted_ip) {
	var inputStr1 = (dotted_ip.split("."))[0];
	var inputStr2 = (dotted_ip.split("."))[1];
	var inputStr3 = (dotted_ip.split("."))[2];
	var inputStr4 = (dotted_ip.split("."))[3];
	var doneVal;

	if ( isEmpty(inputStr1) || isEmpty(inputStr2) || isEmpty(inputStr3) || isEmpty(inputStr4) ) {
		return 0;
	} else {
		// Concatenate binary characters
		doneVal = toBin(inputStr1) + toBin(inputStr2) + toBin(inputStr3) + toBin(inputStr4) ;
		// strip any leading zero
		doneVal = stripZeros(doneVal) ;
		// convert binary to decimal
		return parseInt(doneVal, 2) ;
	}
}

function RemoveDuplicates(arr) {
	//get sorted array as input and returns the same array without duplicates.
	var result = [];
	var lastValue="";
	for (var i=0; i<arr.length; i++) {
		var curValue=arr[i];
		if (curValue != lastValue) {
			result[result.length] = curValue;
		}
		lastValue=curValue;
	}
	return result;
}

function decimal_to_ip(dec) {
	var oct0 = dec >>> 24;
	var oct1 = (dec << 8) >>> 24;
	var oct2 = (dec << 16) >>> 24;
	var oct3 = (dec << 24) >>> 24;
	return oct0 +"."+ oct1 +"."+ oct2 +"."+ oct3;
}

function padTextPrefix (InString, PadChar, DefLength) {
	if (InString.length >= DefLength) {
		return (InString);
	}
	var OutString = InString;
	for (var Count = InString.length; Count < DefLength; Count++)  {
		OutString = PadChar + OutString;
	}
	return (OutString);
}

// Function that finds an emptry spot in an numerical array
function find_hole (decArray) {
	var ar = RemoveDuplicates(decArray);
	ar.sort();
	var len = ar.length;
	if (len == 1) {
		return ar[0] + 1;
	} else {
		var i=0, ret;
		for ( i = 1; i<len ; i++ ) {
			if ((ar[i] - ar[i-1]) != 1) {
				ret=ar[i-1]+1;
				return ret;
			}
		}
		ret=ar[i-1]+1;
		return ret;
	}
}