/**
* GoogleWeather module
* This module allows you to add the Google Weather in a module position.
* Author: kksou
* Copyright (C) 2006-2009. kksou.com. All Rights Reserved
* Website: http://www.kksou.com/php-gtk2
* v1.5 October 11, 2009
*/

//window.onload = prepareForm;

function gw_submitform(mod_id) {
	var form = document.getElementById("WeatherForm"+mod_id)
	var a = document.getElementById('str_a'+mod_id).value;
	if (a=='') return false;
	var data = "";
	for (var i=0; i<form.elements.length; i++) {
		data+= form.elements[i].name;
		data+= "=";
		data+= escape(form.elements[i].value);
		data+= "&";
	}
	data+= "process=1";
	var unit = document.getElementById('temp_unit'+mod_id).value;
	if (unit=='US') data+= "&toF=1";
	else if (unit=='SI') data+= "&toC=1";
	//return !sendData(data);
	gw_sendData(mod_id, data);
	return false;
};

function gw_toF(mod_id) {
	var a = document.getElementById('str_a_org'+mod_id).value;
	document.getElementById('temp_unit'+mod_id).value = 'US';
	gw_sendData(mod_id, 'process=1&toF=1&a='+escape(a));
}

function gw_toC(mod_id) {
	var a = document.getElementById('str_a_org'+mod_id).value;
	document.getElementById('temp_unit'+mod_id).value = 'SI';
	gw_sendData(mod_id, 'process=1&toC=1&a='+escape(a));
}

function gw_sendData(mod_id, data) {
	var request = gw_getHTTPObject();
	if (request) {
		gw_displayLoading(mod_id, document.getElementById("gw_loading"+mod_id));
		request.onreadystatechange = function() {
			gw_parseResponse(request, mod_id);
		};
		url = eval('lib_url'+mod_id)+"?"+data
		+"&label_city="+eval('label_city'+mod_id)
		+"&popup_city="+eval('popup_city'+mod_id)
		+"&popup_deg_F="+eval('popup_deg_F'+mod_id)
		+"&popup_deg_C="+eval('popup_deg_C'+mod_id)
		+"&size_city="+eval('size_city'+mod_id)
		+"&hide_input="+eval('hide_input'+mod_id)
		+"&hide_humidity="+eval('hide_humidity'+mod_id)
		+"&hide_wind="+eval('hide_wind'+mod_id)
		+"&hide_forecast="+eval('hide_forecast'+mod_id)
		+"&lang="+eval('lang'+mod_id)
		+"&request_interval="+eval('request_interval'+mod_id)
		+"&mod_id="+mod_id
		+"&googleweather_unit="+eval('googleweather_unit'+mod_id)
		+"&use_curl="+eval('googleweather_use_curl'+mod_id)
		+"&default_location="+eval('default_location'+mod_id);
		request.open( "GET", url, true );
		request.send(null);
		return true;
	} else {
		return false;
	}
}

function gw_parseResponse(request, mod_id) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var container = document.getElementById("googleWeather_container"+mod_id);
			container.innerHTML = request.responseText;
			var myRegExp = /No such city/;
			var str = request.responseText;
			var matchPos1 = str.search(myRegExp);
			var a = document.getElementById('str_a'+mod_id);
			var set_focus = eval('focus_on_city'+mod_id);
			if (set_focus==1) {
				if(a.type!='hidden') {
					if(matchPos1 == -1) {
						a.value = '';
						a.focus();
					} else {
						a.focus();
						a.select();
					}
				}
			}
			//fadeUpErrors(container);
			//prepareForm();
		}
	}
}

function gw_getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	}
	return xhr;
}

function gw_displayLoading(mod_id, element) {
	if (element==null) return;
	var image = document.createElement("img");
	//image.setAttribute("src","progressbar.gif");
	image.setAttribute("src", eval('progress_gif'+mod_id));
	image.setAttribute("alt","Loading...");
	//image.setAttribute("align","middle");
	element.appendChild(image);
}

