function setLanguage(code){
	switch(code){
		case 'zh-cn':
			setCookie('language', 'zh-cn', 1);
		break;
		case 'zh-tw':
			setCookie('language', 'zh-tw', 1);
		break;
		default:
			setCookie('language', 'en-us', 1);
		break;
	}
	if(typeof(window.event) != 'undefined') window.event.returnValue=false;
	window.location.href=window.location.href;
}

function inputHandler(){
	var inputs = $A(document.getElementsByTagName('input'));
	var textboxes = $A(document.getElementsByTagName('textarea'));

	inputs.each(function(input){
		var type = $(input).readAttribute('type');

		if(type == 'text' || type == 'password'){
			var defaultClassName = $(input).className;

			$(input).onmouseover = function(){
				if($(this).className != 'text_field focus') $(this).className = 'text_field over';
			}
			$(input).onfocus = function(){
				$(this).className = 'text_field focus';
			}
			$(input).onmouseout = function(){
				if($(this).className != 'text_field focus') $(this).className = defaultClassName;
			}
			$(input).onblur = function(){
				$(this).className = defaultClassName;
			}
		}
	});

	textboxes.each(function(textbox){
		var defaultClassName = $(textbox).className;

		$(textbox).onmouseover = function(){
			if($(this).className != 'textbox focus') $(this).className = 'textbox over';
		}
		$(textbox).onfocus = function(){
			$(this).className = 'textbox focus';
		}
		$(textbox).onmouseout = function(){
			if($(this).className != 'textbox focus') $(this).className = defaultClassName;
		}
		$(textbox).onblur = function(){
			$(this).className = defaultClassName;
		}
	});
}

function bugsReportHandler(){
	var inputs = $A(document.getElementsByTagName('input'));
	var textboxes = $A(document.getElementsByTagName('textarea'));

	inputs.each(function(input){
		var type = $(input).readAttribute('type');

		if(type == 'text' || type == 'file'){
			var defaultClassName = $(input).className;

			$(input).onmouseover = function(){
				if($(this).className != 'text_field focus') $(this).className = 'text_field over';
			}
			$(input).onfocus = function(){
				$(this).className = 'text_field focus';
				$($(this).id + 'Helper').show();
			}
			$(input).onmouseout = function(){
				if($(this).className != 'text_field focus') $(this).className = defaultClassName;
			}
			$(input).onblur = function(){
				$(this).className = defaultClassName;
				$($(this).id + 'Helper').hide();
			}
		}
	});

	textboxes.each(function(textbox){
		var defaultClassName = $(textbox).className;

		$(textbox).onmouseover = function(){
			if($(this).className != 'textbox focus') $(this).className = 'textbox over';
		}
		$(textbox).onfocus = function(){
			$(this).className = 'textbox focus';
			$($(this).id + 'Helper').show();
		}
		$(textbox).onmouseout = function(){
			if($(this).className != 'textbox focus') $(this).className = defaultClassName;
		}
		$(textbox).onblur = function(){
			$(this).className = defaultClassName;
			$($(this).id + 'Helper').hide();
		}
	});
}

function loginHandler(){
	var labels = document.getElementsByTagName('label');
	var inputId;
	
	for(var i=0; i<labels.length; i++){
		if(labels[i].className == 'overlabel'){
			inputId = $(labels[i]).htmlFor || $(labels[i]).readAttribute('for');

			if($(inputId) == null) continue; // Skip if there is no "for" attribute
			
			$(labels[i]).className = 'overlabel-apply';

			// Hide label when there is a value
			if($(inputId).value.length > 0){
				hideLabel($(inputId).getAttribute('id'), true);
			}
			
			// When focus on the field
			$(inputId).onfocus = function (){
				hideLabel($(this).readAttribute('id'), true);
				$(this).className = 'text_field focus';
				//$(this).observe('keydown', submitLoginForm);
			};
			
			$(inputId).onblur = function (){
				if(this.value === ''){
					hideLabel($(this).readAttribute('id'), false);
					$(this).className = 'text_field default';
					//$(this).stopObserving();
				}
			};
			
			// Safari handler
			$(labels[i]).onclick = function(){
				var id, field;
				id = $(this).readAttribute('for');
				if($(id)) $(id).focus();
			};
		}
	}
}

function hideLabel(labelId, hide){
	var fieldFor;
	var labels = $A(document.getElementsByTagName('label'));

	labels.each(function(obj){
		fieldFor = $(obj).htmlFor || $(obj).readAttribute('for');

		if(fieldFor == labelId){
			$(obj).style.textIndent = (hide) ? '-5000px' : '0px';
			return true;
		}
	});
}

function displayHelper(obj){
	$($(obj).id + 'Helper').show();
	obj.onblur = function(){
		$($(this).id + 'Helper').hide();
	}
}

function closeAnnoucement(){
	setCookie('disableAnnoucement', 'true', 1);
	Effect.Fade('annoucement');
}

function donate(value){
	if(value>0 && value<500){
		if(typeof(window.event) != 'undefined') window.event.returnValue=false;
		window.location.href = 'https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=paypal%40whoreadme%2ecom&lc=GB&item_name=Donation%20for%20WhoReadMe%2ecom%20Service&amount=' + value + '%2e00&currency_code=USD&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted';
	}
}

function tickUntick(obj){
	if($(obj).checked == true) tickAll();
	else untickAll();
}

function tickAll(){
	var checkboxes = $A(document.getElementsByTagName('input'));

	checkboxes.each(function(input){
		var type = $(input).readAttribute('type');

		if(type == 'checkbox'){
			$(input).checked = true;
		}
	});
}

function untickAll(){
	var checkboxes = $A(document.getElementsByTagName('input'));

	checkboxes.each(function(input){
		var type = $(input).readAttribute('type');

		if(type == 'checkbox'){
			$(input).checked = false;
		}
	});
}

function openWindow(target, width, height, scroll){
	var width = (typeof(width) == "undefined" || width.length==0) ? (parseInt(screen.width) * 0.9) : width;
	var height = (typeof(height) == "undefined" || height.length==0) ? (parseInt(screen.height) * 0.9) : height;
	var scroll = (typeof(scroll) == "undefined" || scroll.length==0 || scroll) ? 'yes' : 'no';

	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	open(target,'','width=' + width +',height=' + height + ',top=' + top + ',left=' + left + ',resizeable=no,scrollbars=' + scroll + ',statusbar=no,toolbar=no,location=no,menubar=no');
}

function rowExpand(id){
	$(id + '_expand').hide();
	$(id + '_collapse').show();
}

function rowCollapse(id){
	$(id + '_expand').show();
	$(id + '_collapse').hide();
}