var counter_style = 1;
var counter_count = 0;
var counter_unique = 0;
var counter_min_len = 0;
var php_separator_left = '?ph';
var counter_id = '';
var php_separator_right = '?';
var asp_separator_left = '%=';
var asp_separator_right = '%';
var counter_ip = '';
var counter_link = '';
var rand = '';
function ValidChars(sText,ValidChars) {
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1)  {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function changeStyle(style_num) {
	counter_style = style_num;
	updateCode();
}

function changeVariables() {
	if (!ValidChars(document.getElementById("code_count").value,"0123456789")) {
	 alert( "Please enter only numbers for the starting count" );
	 document.getElementById("code_count").focus();
	} else if  (!ValidChars(document.getElementById("code_length").value,"0123456789")) {
	 alert( "Please enter only numbers for the minimum length" );
	 document.getElementById("code_length").focus();
	} else {
		counter_count = document.getElementById("code_count").value;
		counter_min_len = document.getElementById("code_length").value;
		if (document.getElementById("code_ip").value == 'php') {
			counter_ip = "&amp;ip=<" + php_separator_left + "p echo $_SERVER['REMOTE_ADDR']; " + php_separator_right + ">";
		}		
		if (document.getElementById("code_ip").value == 'all') {
			counter_ip = "";
		}
		if (document.getElementById("code_ip").value == 'asp') {
			counter_ip = '&amp;ip=<' + asp_separator_left + ' Request.ServerVariables("REMOTE_ADDR") ' + asp_separator_right + '>';
		}
		updateCode();
	}
	return false;
}

function getRandomNumber(range) {
	return Math.floor(Math.random() * range);
}

function getRandomChar() {
	var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
	return chars.substr(getRandomNumber(52), 1);
}

function randomID(size) {
	var str = "";
	for(var i = 0; i < size; i++)
	{
		str += getRandomChar();
	}
	return str;
}

function getID() {
	return randomID(70);
}


function updateCode() {
	rand = Math.floor(Math.random()*3) + 1;
	if (rand == 1) {
		counter_link = '<a href="http://www.web-design-factory.net">web-design-factory.net</a>';
	}
	if (rand == 2) {
		counter_link = '<a href="http://www.web-design-factory.net/free-web-counter.html">Free Web Counters</a>';
	}
	if (rand == 3) {
		counter_link = '<a href="http://www.web-design-factory.net/articles.html">Web Design Articles</a>';
	}
	//counter_id = ("id" + Math.random() * Math.pow(10, 17) + Math.random() * Math.pow(10, 17) + Math.random() * Math.pow(10, 17) + Math.random() * Math.pow(10, 12));	
	counter_id = getID();
	document.getElementById("code_text").value = '<style type="text/css">.count {font-size:10px;font-family:Arial, Helvetica, sans-serif;text-align:center;margin:0px}.ct img{border:0} .ct a,.ct a:visited{color:#999999;text-decoration:none}.ct a:hover{color:gray;text-decoration:underline}</style><div class="count"><img src="http://web-design-tools.org/counter2.php?id=' + counter_id + '&amp;style=' + counter_style + '&amp;count=' + counter_count + '&amp;min_len=' + counter_min_len + counter_ip + '" alt="Web Design Factory" /><br />' + counter_link + '</div>';
}
