//--------------------number of country can choice---------------------
var maxCountries = 5;
//--------------------country array[18][X]------------------------------------
var all = ["austria", "benelux", "switzerland", "germany", "denmark", "spain", "finland", "france", "greece", "hungary", "ireland", "italy", "norway", "portugal", "romania", "sweden", "slovenia_croatia", "serbia_bulgaria", "czechrepublic"];
//--------------------array[X]------------------------------------------------				
var austria = ["germany", "switzerland", "italy", "hungary", "slovenia_croatia", "czechrepublic"];
var benelux = ["germany", "france", "ireland"];
var czechrepublic = ["germany", "austria"];
var denmark = ["norway", "sweden", "germany"];
var finland = ["germany", "sweden"];
var france = ["spain", "italy", "switzerland", "germany", "ireland", "benelux"];
var germany = ["finland", "sweden", "denmark", "switzerland", "austria", "france", "benelux", "czechrepublic"];
var greece = ["italy", "serbia_bulgaria"];
var hungary = ["austria", "romania", "slovenia_croatia", "serbia_bulgaria"];
var ireland = ["benelux", "france", "northenireland"];
var italy = ["switzerland", "austria", "france", "greece", "spain", "slovenia_croatia"];
var norway = ["sweden", "denmark"];
var portugal = ["spain"];
var romania = ["hungary", "serbia_bulgaria"];
var spain = ["france", "portugal", "italy"];
var sweden = ["norway", "finland", "denmark", "germany"];
var switzerland = ["germany", "italy", "austria", "france"];
var serbia_bulgaria = ["greece", "romania", "slovenia_croatia", "hungary"];
var slovenia_croatia = ["italy", "austria", "hungary", "serbia_bulgaria"];	
//--------------------country array[18] used for message error----------------			
var allUCase = ["Austria", "Benelux", "Switzerland", "Germany", "Denmark", "Spain", "Finland", "France", "Greece", "Hungary", "Ireland", "Italy", "Norway", "Portugal", "Romania", "Sweden", "Slovenia/Croatia", "Bulgaria/Montenegro/Serbia", "Czech Republic"];			
//----------------------------------------------------------------------
var arrOk = [];
var arrTempSelected = [];
var arrSelected = [];
function color(inVar) {
	goOn = true;
	if (window.document.getElementById(inVar).value == "notValid") {
		goOn = false;
	}
	if ((window.document.getElementById(inVar).value == "notSelected") || (window.document.getElementById(inVar).value == null)) {
		for (i = 0; i < all.length; i++) {
			if ((window.document.getElementById(all[i]).value == "notSelected") || (window.document.getElementById(all[i]).value == null)) {
			} else {
				goOn = false;
				break;
			}
		}
	}
	if (goOn == true) {
		if (window.document.getElementById(inVar).value == "selected") {
			window.document.getElementById(inVar).value = "notSelected";
			arrOk = [];
			arrSelected = [];
			isOk();
			selectionGoOn = theOk();
		} else {
			if (window.document.getElementById(inVar).value == "possible") {
				window.document.getElementById(inVar).value = "selected";
				underMaximum = countryCounter();
				if (underMaximum == true) {
					selectionGoOn = true;
				} else {
					selectionGoOn = "max";
				}
			} else {
				if (window.document.getElementById(inVar).value == "notSelected") {
					window.document.getElementById(inVar).value = "selected";
					underMaximum = countryCounter();
					if (underMaximum == true) {
						selectionGoOn = true;
					} else {
						selectionGoOn = "max";
					}
				} else {
					if (window.document.getElementById(inVar).value == null) {
						window.document.getElementById(inVar).value = "selected";
						underMaximum = countryCounter();
						if (underMaximum == true) {
							selectionGoOn = true;
						} else {
							selectionGoOn = "max";
						}
					}
				}
			}
		}
		if (selectionGoOn == true) {
			arrSelected = [];
			for (i = 0; i < all.length; i++) {
				if (window.document.getElementById(all[i]).value == "selected") {					
					window.document.getElementById(all[i]).src = 'images/map_select/' + all[i] + '_over.gif';
				} else {
					if (window.document.getElementById(all[i]).value == "possible") {
						for (s = 0; s < eval(all[i]).length; s++) {
							if (window.document.getElementById(eval(all[i])[s]).value == "selected") {
								window.document.getElementById(all[i]).value = "possible";
								window.document.getElementById(all[i]).src = "./images/map/" + all[i] + ".gif";
								break;
							} else {
								window.document.getElementById(all[i]).value = "notSelected";
								window.document.getElementById(all[i]).src = "./images/map/" + all[i] + ".gif";
							}
						}
					} else {
						if ((window.document.getElementById(all[i]).value == "notSelected") || (window.document.getElementById(all[i]).value == null)) {
							for (s = 0; s < eval(all[i]).length; s++) {
								if (window.document.getElementById(eval(all[i])[s]).value == "selected") {
									window.document.getElementById(all[i]).value = "possible";
									window.document.getElementById(all[i]).src = "./images/map/" + all[i] + ".gif";
									break;
								} else {
									window.document.getElementById(all[i]).src = "./images/map/" + all[i] + ".gif";
								}
							}
						}
					}
				}
			}
		} else {
			if (selectionGoOn == "max") {
				alert('<fmt:message key="product.error.selectPasses" />');
				window.document.getElementById(inVar).value = "notSelected";
			} else {
				alert('<fmt:message key="product.error.removeCountry" />');
				window.document.getElementById(inVar).value = "selected";
			}
		}
	} else {
		for (p = 0; p < all.length; p++) {
			if (all[p] == inVar) {
				alert(allUCase[p] + " is not a valid selection.");
				notValidContinue = false;
				break;
			} else {
				notValidContinue = true;
			}
		}
		if (notValidContinue == true) {
			alert('<fmt:message key="product.error.validateCountry" />');
		}
	}
	writeSelected();
}
function deselectAll() {
	for (i = 0; i < all.length; i++) {
		window.document.getElementById(all[i]).value = "notSelected";
		window.document.getElementById(all[i]).src = './images/map/' + all[i] + '.gif';
	}
	writeSelected();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// writeSelected
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function writeSelected() {
	tempText = "";
	tempText_two = "";
	for (u = 0; u < all.length; u++) {
		if ((window.document.getElementById(all[u]).value == "selected") && (tempText == "")) {
			tempText = allUCase[u];  
			tempText_two = allUCase[u];                                
		} else {
			if (window.document.getElementById(all[u]).value == "selected") {
				tempText = tempText + "<br/>" + allUCase[u];
				tempText_two = tempText_two + "," + allUCase[u];                              
			}
		}
	}
	window.document.getElementById("selectedCountries").innerHTML = tempText;	
	window.document.getElementById("selCountries").value = tempText_two;	
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// isOk
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isOk() {
	arrOk = [];
	arrSelected = [];
	theFirst = false;
	for (o = 0; o < all.length; o++) {
		if ((window.document.getElementById(all[o]).value == "selected") && (theFirst == false)) {
			arrOk[arrOk.length] = all[o];
			theFirst = true;
		} else {
			if ((window.document.getElementById(all[o]).value == "selected") && (theFirst == true)) {
				arrSelected[arrSelected.length] = all[o];
			} else {
			}
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// theOk
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function theOk() {
	oldLength = arrOk.length;
	for (q = 0; q < arrOk.length; q++) {
		getSelected = theSelected(arrOk[q]);
		if (getSelected == false) {
		} else {
			if (getSelected == true) {
				break;
			}
		}
	}
	newLength = arrOk.length;
	toCont = goOnLoop(oldLength, newLength);
	if (toCont == false) {
		theValue = false;
	} else {
		if (toCont == true) {
			theValue = true;
		}
	}
	return (theValue);
}
function goOnLoop(oldLength, newLength) {
	if (oldLength < newLength) {
		theOk();
	} else {
		if (arrSelected.length != 0) {
			return (false);
		} else {
			return (true);
		}
	}
}
function theSelected(theOkVar) {
	for (s = 0; s < arrSelected.length; s++) {
		getSelectedItem = theSelectedItem(theOkVar, arrSelected[s]);
		if (getSelectedItem == true) {
			return (true);
			break;
		}
	}
	return (false);
}
function theSelectedItem(theOkVar, theSelectedVar) {
	for (p = 0; p < eval(theSelectedVar).length; p++) {
		theSelectedItemVar = eval(theSelectedVar)[p];
		if (theOkVar == theSelectedItemVar) {
			arrOk[arrOk.length] = theSelectedVar;
			reOrder(theSelectedVar);
			return (true);
			break;
		}
	}
	return (false);
}
function reOrder(toReorder) {
	arrTempSelected = [];
	for (r = 0; r < arrSelected.length; r++) {
		if (toReorder == arrSelected[r]) {
		} else {
			arrTempSelected[arrTempSelected.length] = arrSelected[r];
		}
	}
	arrSelected = arrTempSelected;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// countryCounter
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function countryCounter() {
	counter = 0;
	for (o = 0; o < all.length; o++) {
		if (window.document.getElementById(all[o]).value == "selected") {
			counter = counter + 1;
		}
	}
	if (counter > maxCountries) {
		toReturn = false;
	} else {
		toReturn = true;
	}
	return (toReturn);
}