/**
 * @author florent@ezoulou.be
 */

$(document).ready(function(){
	$("select").focus();
/*	$("select").keyup(function(e) {
		if (e.keyCode == 13) {
			fctSubmitaecrForm();
			return false;
		}
	});
	*/
});


var resultPage = '';

function fctClearaecrForm(){
	var formObj = document.aecrForm; // your form	
	for (var loop = 0; loop < formObj.PLAND.options.length; loop++) {
		formObj.PLAND.options[loop].selected = false; // remove the option
	}
	initForm();
}

function initForm () {
	document.getElementById('results').innerHTML = "";
	resultPage = '';
	$('#print_button').remove();
}

function fctSubmitaecrForm() {
	initForm();	
	var mySelect = document.aecrForm.PLAND; // your form
 	var nbrSelected = 0;
	var index = 0;
	
	if (mySelect.value == '') {
		alert('please, choose at least one country on the list. ');
	}
	else {
		document.getElementById('results').innerHTML = "loading data ...";
		// first we check how many items are selected		
		for (var loop = 0; loop < mySelect.options.length; loop++) {
			if (mySelect.options[loop].selected) {
				nbrSelected++;
			}
		}		
		for (var loop = 0; loop < mySelect.options.length; loop++) {
			if (mySelect.options[loop].selected) {
				index ++;
				if (nbrSelected == index) {
					isLast = true;
				} else {
					isLast = false;
				}
				xmlhttp = getHTTPObject(isLast, mySelect.options[loop].text, mySelect.options[loop].value.toLowerCase());
				xmlhttp.open("GET", "countries/" + mySelect.options[loop].value.toLowerCase() + ".html", true);
				xmlhttp.send(null);
			}
		}
	}
}

function buildPage (isLast, title, str) {
	resultPage = resultPage + '<h1 align="center" style="color:#666;font-family:arial, sans-serif;font-size:18px;font-weight:bold;background-color:#F5F4EE;margin-bottom:0;padding:0.5em 0;width:99%;">'+title+'</h1>';	
	page = str.substring(str.indexOf('<body')+5, str.indexOf('</body>'));
	page = page.substring(page.indexOf('>')+1);
	resultPage = resultPage + page + '<br /><br /><br />' ;	
	if (isLast) showPage(); 
}
function showPage () {
	document.getElementById('results').innerHTML=resultPage;
	
	if ($('#print_button').length==0) $('#zeForm td:last').append('<a id="print_button" href="javascript:print()" title="print"><img style="border:0" src="images/printer.png" alt="print" /></a>');
}


function getHTTPObject(isLast, title, id)
{
  var xmlhttp = false;

  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }

  if (xmlhttp){
     /* on définit ce qui doit se passer quand la page répondra */
     xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState == 4) /* 4 : état "complete" */ {
			//buildPage(isLast, title, xmlhttp.responseText);
					
			 if (xmlhttp.status == 200) {
buildPage(isLast, title, xmlhttp.responseText);
			 } else {
document.getElementById('results').innerHTML = "Error. Mmmm... cannot load  file " + id + ".html ("+ title +"). Try one more time please. ";
			}
	}
     }
  } 

  
  return xmlhttp;
}
