/**
* YahooCurrencyConverter module
* This module allows you to add the Yahoo Currency Converter in a module position.
* Author: kksou
* Copyright (C) 2006-2008. kksou.com. All Rights Reserved
* Website: http://www.kksou.com/php-gtk2
* v1.5 August 28, 2008
*/

window.onload = yahoocurrency_prepareForm;

function yahoocurrency_prepareForm() {
	if(!document.getElementById) {
		return;
	}
	if(!document.getElementById("yahoocurrency_form")) {
		return;
	}
	document.getElementById("yahoocurrency_form").onsubmit = function() {
		var data = "";
		for (var i=0; i<this.elements.length; i++) {
			data+= this.elements[i].name;
			data+= "=";
			data+= escape(this.elements[i].value);
			data+= "&";
		}
		data+= "process=1";
		return !yahoocurrency_sendData(data);
	};
}

function yahoocurrency_sendData(data) {
	var request_yahoocurrency = yahoocurrency_getHTTPObject();
	if (request_yahoocurrency) {
		//yahoocurrency_displayLoading(document.getElementById("loading"));
		yahoocurrency_displayLoading_org(document.getElementById("yahoocurrency_loading"));
		url = yahoocurrency_lib_url+"?"+data+"&submit_button_label="+yahoocurrency_submit_button_label
		+"&label_convert="+yahoocurrency_label_convert
		+"&label_into="+yahoocurrency_label_into
		+"&use_curl="+yahoocurrency_use_curl
		+"&style="+yahoocurrency_style;
		request_yahoocurrency.onreadystatechange = function() {
			yahoocurrency_parseResponse(request_yahoocurrency);
		};
		request_yahoocurrency.open( "GET", url, true );
		request_yahoocurrency.send(null);
		return true;
	} else {
		return false;
	}
}

function yahoocurrency_parseResponse(request) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var container = document.getElementById("yahoocurrency_container");
			container.innerHTML = request.responseText;
			yahoocurrency_fadeUpErrors(container);
			yahoocurrency_prepareForm();
		}
	}
}

function yahoocurrency_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 yahoocurrency_displayLoading(element) {
	element.innerHTML = 'Loading...';
	//element.innerHTML = label_loading;
}

function yahoocurrency_displayLoading_v2(element) {
	var t1 = document.createElement("span");
	var text = document.createTextNode("Loading...");
	t1.appendChild(text);
	element.appendChild(t1);
}

function yahoocurrency_displayLoading_org(element) {
	var image = document.createElement("img");
	//image.setAttribute("src","progressbar.gif");
	image.setAttribute("src", yahoocurrency_progress_gif);
	image.setAttribute("alt","Loading...");
	image.setAttribute("align","middle");
	element.appendChild(image);
}

function yahoocurrency_fadeUp(element,red,green,blue) {
	if (element.fade) {
		clearTimeout(element.fade);
	}
	element.style.backgroundColor = "rgb("+red+","+green+","+blue+")";
	if (red == 255 && green == 255 && blue == 255) {
		return;
	}
	var newred = red + Math.ceil((255 - red)/10);
	var newgreen = green + Math.ceil((255 - green)/10);
	var newblue = blue + Math.ceil((255 - blue)/10);
	var repeat = function() {
		fadeUp(element,newred,newgreen,newblue)
	};
	element.fade = setTimeout(repeat,100);
}

function yahoocurrency_fadeUpErrors(element) {
	var messages = element.getElementsByTagName("strong");
	for (var i=0; i<messages.length; i++) {
		if (messages[i].className == "error") {
			fadeUp(messages[i],255,153,153);
		}
	}
}