/**
 * @author Michal Kevicky, michal@kevicky.info
 * @copyright 2007, Michal Kevicky
 */

function oid(id) { return document.getElementById(id); }
function oidv(id) { return document.getElementById(id).value; }
function oEnable(elem) { document.getElementById(elem).disabled = false; }
function oDisable(elem) { document.getElementById(elem).disabled = true; }
function isEmpty(variable) { return (typeof(variable) == "undefined") || (variable == ''); }
function addOrReplaceClass(objid, newClass, oldClass) {
	if (YAHOO.util.Dom.hasClass(objid, oldClass)) {
		YAHOO.util.Dom.replaceClass(objid, oldClass, newClass);
	} else {
		YAHOO.util.Dom.addClass(objid, newClass);
	}	
}



function showDiv(objectId) {
	if (YAHOO.util.Dom.hasClass(objectId, 'div_hidden')) {
		YAHOO.util.Dom.replaceClass(objectId, 'div_hidden', 'div_visible');
	} else if (!YAHOO.util.Dom.hasClass(objectId, 'div_visible')) {
		YAHOO.util.Dom.addClass(objectId, 'div_visible');
	}
}

function showObj(objectId) {
	if (YAHOO.util.Dom.hasClass(objectId, 'div_hidden'))
		YAHOO.util.Dom.removeClass(objectId, 'div_hidden');	
}

function hideObj(objectId) {
	if (YAHOO.util.Dom.hasClass(objectId, 'div_visible')) {
		YAHOO.util.Dom.replaceClass(objectId, 'div_visible', 'div_hidden');
	} else if (!YAHOO.util.Dom.hasClass(objectId, 'div_hidden')) {
		YAHOO.util.Dom.addClass(objectId, 'div_hidden');
	}
}


function insertStringToCursorPosition(objectId, strToInsert, selected) {
	var posPre = oid(objectId).selectionStart;
	var pref = oidv(objectId).toString().substring(0, posPre);
	var suff = oidv(objectId).toString().substring(posPre, oidv(objectId).toString().length);
	oid(objectId).value = pref + strToInsert + suff;
	if (!isEmpty(selected) && (selected == true)) {
		oid(objectId).focus();
		oid(objectId).selectionStart = posPre;
		oid(objectId).selectionEnd = posPre + strToInsert.toString().length;
	}
	return true;
}

function decorateSelectedString(objectId, prefix, suffix, selected) {
	var posPre = oid(objectId).selectionStart;
	var posSuf = oid(objectId).selectionEnd;
	var strBu = oidv(objectId).toString();
	oid(objectId).value = strBu.substring(0, posPre) + prefix + strBu.substring(posPre, posSuf) + suffix + strBu.substring(posSuf, strBu.length);
	if (!isEmpty(selected) && (selected == true)) {
		oid(objectId).focus(); 
		oid(objectId).selectionStart = posPre;
		oid(objectId).selectionEnd = posSuf + prefix.toString().length + suffix.toString().length;
	}
}



function activateLanguage(actlang) { oid('mainpage').className = actlang; }
function setdBoyHeader(text) { oid('dBoyHeader').innerHTML = "<h2>" + text + "</h2>"; }
function setdBoyContent(content) { oid('dBoyContent').innerHTML = content; }

function validateEmail(email) {
	var ret = false;
	if (!isEmpty(email)) {
		var em = email.toString();
		var zp = em.indexOf('@', 1);
		if (zp > 1) {
			var dp = em.indexOf('.', zp+2);
			if ((dp > 0) && (dp+1 < em.length)) {
				ret = true;
			}
		}
	}
	return ret;
}


function jumpTo(view, topic, country, special) {
	if ((view != -1) && (topic != -1) && (country != -1))
		window.location.href = '?view=' + view + '&topic=' + topic + '&country=' + country + special;
}



function jumpIfStatesFit(view, topic, countryAId, countryBId) {
	if ((document.getElementById(countryAId).value != '-1')
		&& (document.getElementById(countryBId).value != '-1')
		&& (document.getElementById(countryAId).value != document.getElementById(countryBId).value)) {
		window.location.href = "?view=" + view + "&topic=" + topic + "&country=" + document.getElementById(countryAId).value + "&countryB=" + document.getElementById(countryBId).value;
	}
}



function setFontSize(size) {
	var newSize = '';
	
	if (size == 'small') {
		if (YAHOO.util.Dom.hasClass('mainpage', 'fnt_std')) YAHOO.util.Dom.removeClass('mainpage', 'fnt_std');
		if (YAHOO.util.Dom.hasClass('mainpage', 'fnt_big')) YAHOO.util.Dom.removeClass('mainpage', 'fnt_big');
		YAHOO.util.Dom.addClass('mainpage', 'fnt_small');
		newSize = 'small';
		
	} else if (size == 'std') {
		if (YAHOO.util.Dom.hasClass('mainpage', 'fnt_small')) YAHOO.util.Dom.removeClass('mainpage', 'fnt_small');
		if (YAHOO.util.Dom.hasClass('mainpage', 'fnt_big')) YAHOO.util.Dom.removeClass('mainpage', 'fnt_big');
		YAHOO.util.Dom.addClass('mainpage', 'fnt_std');
		newSize = 'std';		
		
	} else if (size == 'big') {
		if (YAHOO.util.Dom.hasClass('mainpage', 'fnt_small')) YAHOO.util.Dom.removeClass('mainpage', 'fnt_small');
		if (YAHOO.util.Dom.hasClass('mainpage', 'fnt_std')) YAHOO.util.Dom.removeClass('mainpage', 'fnt_std');
		YAHOO.util.Dom.addClass('mainpage', 'fnt_big');
		newSize = 'big';				
	}

	if (newSize != '') {
		
		var callback =  {  
			success: function(o) { /*alert(o.responseText);*/},  
			failure: function(o) {/*failure handler code*/},  
			argument: [],
			cache: false  
		} 
		
		var transaction = YAHOO.util.Connect.asyncRequest('GET', 'ajax_wrapper.php?action=setFontSize&font_size=' + newSize, callback, null);
	}
	return false;
}