// Global Var
var currentCityId = "currentcity";
var xmlhttp;
var locText = new Array();
locText['e'] = "Select your Province or Territory first...";
locText['f'] = "Choisissez d'abord votre province";

//function createQuickLink() {

	var quickLink = document.getElementById('quickLinkForm');
	var c1 = 'hidden';
   var rep=quickLink.className.match(' '+c1)?' '+c1:c1;
   quickLink.className=quickLink.className.replace(rep,'');
	//return;
//}
//setProvince();

// Sets province selection list to province of city page
function setProvince() {
	var provinceCode = ""; 
	var provinceCodeObj;
	var provinceSelectObj = document.getElementById('provinces');	

	if (document.getElementById('provincecode')) {
		provinceCodeObj = document.getElementById('provincecode');
		provinceCode = provinceCodeObj.title;

		for (var i=0; i<provinceSelectObj.options.length; i++) {
			if (provinceSelectObj.options[i].value.toLowerCase() == provinceCode) {
				provinceSelectObj.selectedIndex = i;
				break;
			}
		}
	} 
	//updatecities(provinceSelectObj, document.body.className); // class name is 'en' or 'fr'
}

function updatecities(provinceFieldObj, lang) {
	var cityFieldObj = document.getElementById('cities');
	var cityList = null;
	var provincialSummary;
	var olympicSummary;
	var cityCode = ""; 
	var foundDefaultCity = 0;
	var defaultCityIndex = 0;
	var newCitySelection = document.createElement('select');

	cityFieldObj.options.length=0; // clear options list

	// Remove forecast quick link error label (if present)
	if (document.getElementById('errorMessage')) {
		var quickLink = document.getElementById("quickLink");
		quickLink.removeChild(quickLink.lastChild);
	}

	if (document.getElementById('citycode')) {
		cityCode = document.getElementById('citycode').title;
	}

	if (lang == 'e') {
		cityList = citiesEn[provinceFieldObj[provinceFieldObj.selectedIndex].value]; // grab selected English city list
	} else {
		cityList = citiesFr[provinceFieldObj[provinceFieldObj.selectedIndex].value]; // grab selected French city list
	}

	// Create city list for selected province
	for (cityOption in cityList) {
		var optionVals = cityList[cityOption].split('|');

		if(optionVals[1] == ''){
			optionVals[0] = locText[lang];
		}

		cityFieldObj.options[cityFieldObj.options.length] = new Option(optionVals[0], optionVals[1]);
		if (optionVals[1].toLowerCase() == cityCode) {
			cityFieldObj.options[cityFieldObj.options.length-1].id = currentCityId; // set element ID so that can be accessed by DOM for the setTimeout
			foundDefaultCity = 1;
		}
	}  
	if (foundDefaultCity == 0) {
		setCity(0);
	} else {   
		setCity(currentCityId);
	}
	updateCodeButton();
}

// Set default city to first in city list or current city
function setCity(index) {
//	var isOpera = navigator.userAgent.toLowerCase().indexOf('opera');
	// NOTE: must use setTimeout for Opera when setting the selectedIndex. 
	// Otherwise, "ghost" options are generated for the selection box
	// Reference: http://www.greywyvern.com/code/opera/Bug-GhostOptions
	//				  http://my.opera.com/community/forums/topic.dml?id=167366
 
	if (index == currentCityId) {// set to current city 
		setTimeout("document.getElementById('" + currentCityId + "').selected = \"selected\";", 0);
	} else { // set to first city in list
		setTimeout("document.getElementById('cities').selectedIndex = " + index + ";", 0);
	}
}

/*function jumpToCity(cityObj, provinceObj, units, lang) {
	var citycode = cityObj.options[cityObj.selectedIndex].value;
	var provincecode = provinceObj.options[provinceObj.selectedIndex].value;

	if (provincecode == "--") {
		// This means that no province has been selected, so display error message
		if (document.getElementById('errorMessage')==null) { // only need to display it once
			var quicklinkObj = document.getElementById('quickLink');
			var errorLabel = document.createElement('div');
			errorLabel.setAttribute('id', 'errorMessage');
			errorLabel.className = 'error';
			if (lang == "e") { // display message based on language
				errorLabel.appendChild(document.createTextNode("Please select a region"));
			} else {
				errorLabel.appendChild(document.createTextNode("S.V.P. Choisissez une rÃ©gion"));
			}
			quicklinkObj.appendChild(errorLabel);
		}
	} else {
		location.href = "<?php echo $_SERVER['PHP_SELF']?>?citycode="+citycode.toLowerCase()+"&lang="+lang;

	//	if (units == 'metric') {
	//		location.href = "/city/pages/" + citycode.toLowerCase() + "_" + units + "_" + lang + ".html"; // metric city page
	//	} else {
	//		location.href = "/forecast/city_" + lang + ".html?" + citycode.toLowerCase() + "&unit=i"; // imperial city page
	//	}
	}
}*/

function resetForm(lang){
	var defaultPhrase = new Array();
	defaultPhrase['e'] = "You must select your Province or Territory, and city before clicking Display Code.  Once you do this you will see the code you need to cut and paste.";
	defaultPhrase['f'] = "Vous devez choisir une province ou un territoire, et une ville avant de cliquer sur Affichage du code.\nUne fois ces données entrées, vous verrez apparaître le code à copier.";
	var provSelectObj = document.getElementById('provinces');
	var citySelectObj = document.getElementById('cities');
	var textAreaObj = document.getElementById('codeSnipet');
	textAreaObj.value = defaultPhrase[lang];

	
	return false;
}

function createSnipet(lang){
	var random = Math.random();
	var provCodeObj = document.getElementById('provinces');
	var cityCodeObj = document.getElementById('cities');
	var provCode = provCodeObj.options[provCodeObj.selectedIndex].value;
	var cityCode = cityCodeObj.options[cityCodeObj.selectedIndex].value;

	var url = "/business/include/getSnipet.php?provCode=" + provCode + "&cityCode=" + cityCode + "&lang=" + lang + "&rand=" + random ; 
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=stateChange;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	} else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.onreadystatechange=stateChange;
		xmlhttp.open("GET",url,true);
		// Do not send null for ActiveX
		xmlhttp.send();
	}
	return false;
}
function stateChange(){
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200 ) {
			// process whatever has been sent back here
			var codeSnipetObj = document.getElementById('codeSnipet');
			//codeSnipetObj.innerHTML=xmlhttp.responseText;
			codeSnipetObj.value = xmlhttp.responseText;
		} else {
				alert("There was a problem in the returned data");
		}
	}
}
function updateCodeButton(){
	var provObj = document.getElementById('provinces');
	var displayCodeObj = document.getElementById('dispCodeBtn');
	if(provObj.value != '--'){
		displayCodeObj.disabled = '';
	} else {
		displayCodeObj.disabled = 'disabled';
	}
}
function resetPage(thisObj,lang){
	var displayCodeObj = document.getElementById('dispCodeBtn');
	var provCodeObj = document.getElementById('provinces');
	var cityCodeObj = document.getElementById('cities');
	var snipetObj = document.getElementById('codeSnipet');
	provCodeObj.selectedIndex = '';
	//Remove all the options
	cityCodeObj.options.length = 0;
	var objOption = document.createElement("option");
	objOption.text = locText[lang];
	if(document.all && !window.opera){
		cityCodeObj.add(objOption);
	} else {
		cityCodeObj.add(objOption,null);
	}
	displayCodeObj.disabled = 'disabled';
	if(lang == 'f'){
		snipetObj.value = "Vous devez choisir une province ou un territoire, et une ville avant de cliquer sur Affichage du code.\nUne fois ces données entrées, vous verrez apparaître le code à copier.";
	} else {
		snipetObj.value = 'You must select your Province or Territory, and city before clicking Display Code.  Once you do this you will see the code you need to cut and paste.';
	}
	return false;
}

