/**
 * Util scripts
 *
 * @copyright (c) 2004-2006, SWsoft
 */

var sbLogs = null;

function log(key, val, clear) {
	if (null == sbLogs) {
		SbInitLogging();
	}
	if (null != sbLogs) {
		if (clear) {
			sbLogs.innerHTML = '';
		}
		sbLogs.innerHTML = sbLogs.innerHTML + '<br>' + key  + '==' + val;
	}
}

function SbInitLogging() {
	sbLogs = document.getElementById('SbLogs');
}




function getScreenWidth() {
	var result = 2000;
	
	if (window.innerWidth) {
		// all except Explorer
		result = parseInt(window.innerWidth);
	} else if (document.documentElement && document.documentElement.clientWidth) {
		// Explorer 6 Strict Mode
		result = parseInt(document.documentElement.clientWidth);
	} else if (document.body) {
		// other Explorers
		result = parseInt(document.body.clientWidth);
	}
	if (isNaN(result)) {
		result = 2000;
	}
	return result;
}

function getScreenHeight() {
	var result = 2000;
	
	if (window.innerHeight) {
		// all except Explorer
		result = parseInt(window.innerHeight);
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		result = parseInt(document.documentElement.clientHeight);
	} else if (document.body) {
		// other Explorers
		result = parseInt(document.body.clientHeight);
	}
	if (isNaN(result)) {
		result = 2000;
	}
	return result;
}

function getScrollTop() {
	var result = 0;
	if (document.documentElement && document.documentElement.scrollTop) {
		result = parseInt(document.documentElement.scrollTop);
	} else if (document.body) {
		result = parseInt(document.body.scrollTop);
	}
	if (isNaN(result)) {
		result = 0;
	}
	return result;
}

function getScrollLeft() {
	var result = 0;
	if (document.documentElement && document.documentElement.scrollLeft) {
		result = parseInt(document.documentElement.scrollLeft);
	} else if (document.body) {
		result = parseInt(document.body.scrollLeft);
	}
	if (isNaN(result)) {
		result = 0;
	}
	return result;
}

function addEvent(element, evname, func) {
	if (document.all) {
		element.attachEvent('on' + evname, func);
	} else {
		if (evname == 'scroll') {
			element.addEventListener(evname, func, false);
		} else {
			element.addEventListener(evname, func, true);
		}
	}
}

function removeEvent(element, evname, func) {
	if (document.all) {
		element.detachEvent('on' + evname, func);
	} else {
		element.removeEventListener(evname, func, true);
	}
}

function stopEvent(event) {
	if (document.all) {
		event = window.event;
		event.cancelBubble = true;
		event.returnValue = false;
	} else {
		event.preventDefault();
		event.stopPropagation();
	}
	return false;
}

function getScreenX(event) {
	var result = parseInt(event.clientX);
	if (isNaN(result)) {
		result = 0;
	}
	result += getScrollLeft();
	return result;
}

function getScreenY(event) {
	var result = parseInt(event.clientY);
	if (isNaN(result)) {
		result = 0;
	}
	result += getScrollTop();
	return result;
}

function getObjectWidth(object) {
	var result = 0;
	if (null != object) {
		result = parseInt(object.clientWidth);
		if (isNaN(result) || (0 == result)) {
			result = parseInt(object.style.width);
			if (isNaN(result)) {
				result = 0;
			}
		}
	}
	return result;
}

function getObjectHeight(object) {
	var result = 0;
	if (null != object) {
		result = parseInt(object.clientHeight);
		if (isNaN(result) || (0 == result)) {
			result = parseInt(object.style.height);
			if (isNaN(result)) {
				result = 0;
			}
		}
	}
	return result;
}

function setObjectHeight(object, height) {
	if (null != object) {
		object.style.height = height + 'px';
	}
}

function setObjectWidth(object, width) {
	if (null != object) {
		object.style.width = width + 'px';
	}
}

function setObjectSize(object, width, height) {
	if (null != object) {
		object.style.width = width + 'px';
		object.style.height = height + 'px';
	}
}

function setObjectPosition(object, left, top) {
	if (null != object) {
		object.style.left = left + 'px';
		object.style.top = top + 'px';
	}
}

function centerObject(object) {
	var x = getScreenWidth();
	var y = getScreenHeight();
	var width = getObjectWidth(object);
	var height = getObjectHeight(object);
	var left = Math.ceil((x - width) / 2);
	var top = Math.ceil((y - height) / 2);
	if (left <= 0) {
		left = 10;
	}
	if (top <= 0) {
		left = 20;
	}
	setObjectPosition(object, left, top)
}

/**
 * Validate field with validator method and show message if it contains invalid data
 *
 * @param string methodName
 * @param string elementId
 * @param string localeKey
 * @return bool
 */
function validateField(methodName, elementId, localeKey) {
	eval('var result = ' + methodName + '(document.getElementById(\'' + elementId + '\').value);');

	if (asterix = document.getElementById(elementId + '_asterix')) {
		showElement(asterix, !document.getElementById(elementId).value);
	}
	
	if (!result) {
		alert(SbGetLocaleByKey(localeKey));
		
		try {
			document.getElementById(elementId).focus();
		} catch (e) {
			
		}
		
		return false;
	}
	
	return true;
}

/**
 * Validate field if filed value is not empty 
 * with validator method and show message if it contains invalid data
 *
 * @param string methodName
 * @param string elementId
 * @param string localeKey
 * @return bool
 */
function validateFieldIfNotEmpty(methodName, elementId, localeKey) {
	val = document.getElementById(elementId).value;
	if (val) {
		return validateField(methodName, elementId, localeKey);
	}
	return true;
}

/**
 * Validate field with validation expression and show message if it contains invalid data
 *
 * @param string validator
 * @param string elementId
 * @param string localeKey
 * @return bool
 */
function extendedValidateField(validator, elementId, localeKey) {	
	eval('var result = ' + validator + ';');
	
	// hide asterix if field have valid data
	if (asterix = document.getElementById(elementId + '_asterix')) {
		showElement(asterix, !result);
	}
	
	if (!result) {
		alert(SbGetLocaleByKey(localeKey));
		
		try {
			document.getElementById(elementId).focus();
		} catch (e) {
			
		}
		
		return false;
	}
	
	return true;
}

function showAsterix(id) {
	if ((asterix = document.getElementById(id + '_asterix')) && !asterix.disabled) {
		showElement(asterix, !document.getElementById(id).value);
	}
}

function disableAsterix(id) {
	if (asterix = document.getElementById(id + '_asterix')) {
		asterix.disabled = true;
		showElement(asterix, false);
	}
}

function enableAsterix(id) {
	if (asterix = document.getElementById(id + '_asterix')) {
		asterix.disabled = false;
		showAsterix(id);
	}
}

/**
 * Add hidden input to form
 *
 * @param string
 * @param string
 * @param string
 */
function addHiddenToForm(name, value, formName) {
	var input = document.createElement("input");
	input.setAttribute('type', 'hidden');
	input.setAttribute('name', name);
	input.setAttribute('id', name);
	input.value = value;

	window.document.forms[formName].appendChild(input);
}

function htmlSpecialChars(str) {
	try {
		str = str.replace(/&/ig, '&amp;');
		str = str.replace(/\?/ig, '&#63;');
		str = str.replace(/</ig, '&lt;');
		str = str.replace(/>/ig, '&gt;');
		str = str.replace(/\'/ig, '&#39;');
		str = str.replace(/\"/ig, '&#34;');
	} catch(e){ }

	return str;
}

function unHtmlSpecialChars(str) {
	try {
		str = str.replace(/&#34;/ig, '\"');
		str = str.replace(/&#39;/ig, "'");
		str = str.replace(/&gt;/ig,'>');
		str = str.replace(/&lt;/ig,'<');
		str = str.replace(/&#63;/ig,'?');
		str = str.replace(/&amp;/ig, '&');
	} catch(e){ }

	return str;
}

function clickLinkIE(linkId) {
	if (document.all) {
		var link = self.document.getElementById(linkId);
		link.click();
	}
}

function disableButton(button) {
	if (!button) {
		return ;
	}
	
	button.disabled = true;
	if (document.all) {
		button.style.filter = "alpha(opacity='40')";
	} else {
		button.style.MozOpacity = '0.4';
	}
	button.style.cursor = 'default';
}

function enableButton(button) {
	if (!button) {
		return ;
	}
	
	button.disabled = false;
	if (document.all) {
		button.style.filter = '';
	} else {
		button.style.MozOpacity = '';
	}
	button.style.cursor = 'pointer';
}