var boardContainer;
var numbersContainer;

function getChildrenCount(){
	var argv = getChildrenCount.arguments;
	var argc = argv.length;
	var country = 'total';
	if(argc > 0 && argv[0] != '')
		country = argv[0];
		
	var proccessUrl = '/ajax_server/getChildrenCount.php';
	//var testLogin = new Request.JSON();
	var submitOptions = {	noCache: true,
													//onSuccess: proccessLogin,
													onError: function(eData){
																											
																										},																										
													onFaliure: function(eData){

																										},	
													onRequest: initBoard,
													onSuccess:function(sData){proccessCount(sData, country)},
													method:'post',
													url: proccessUrl,
													link: 'cancel'
													};
	var formData = 'country='+country;
	var Opt = {data: formData}
	var getCount = new Request.JSON();
	getCount.setOptions(submitOptions);
	getCount.setOptions(Opt);
	getCount.send();
}

function proccessCount(data, country){
	var numChildren = 0;
	if($chk(data[country])) numChildren = data[country];
	
	
	// proccess only valid countries
	switch(country){
		case 'usa':
		case 'uk':
		case 'ca':
		case 'total':
		//change bakground image
		//boardContainer.setStyle('background', "url(/css/"+skinPath+"images/chalkboard_"+country+".jpg) top left no-repeat");
		boardContainer.set('class', 'childrenBoard_'+country);
		//now draw the numbers
		drawNumbers(numChildren,$('numbersContainer'));
		break;
		default:
			boardContainer.set('class', 'childrenBoard_default');
	}
		
}
function drawNumbers(num, el){
	numStr = num.toString();
	numArr = new Array;
	k=0;
	for(j=numStr.length-1;j>=0;j--){
		if(k==3){
			numArr[k]= ',';
			k++;
		}
		numArr[k]= numStr.charAt(j);
		k++;
	}
	
	if(numArr.length > 0){
		for(j=0;j<numArr.length;j++){
			var numVal = parseInt(numArr[j]);
			var extraClass = '';
			
			if(numVal.toString() != numArr[j])
				extraClass = "nn";
			else
				extraClass = 'n'+numArr[j];
			var divNumber = new Element('div',{'class': 'number '+extraClass});
			divNumber.inject(el);
		}
	}
}
function initBoard(){
	boardContainer = $('childrenBoard');
	numbersContainer =  $('numbersContainer');
	numbersContainer.empty();
}

