/**
 * remove cookie function
 *	@param c	cookie name
 *	@param r	redirect url
 */
function logout(c, r)
{
	document.cookie = c + '=; path=/; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	document.location = r;
}


/**
*	clone block and append it to container
*
*	sContainer		container
*	sTarget			source element to clone
*	sPostfix		unique postfix for id
*	idArray			array with ids to change
*/
function cloneBlock(vContainer, vTarget, sPostfix, idArray)
{
	oContainer = Var2Obj(vContainer);
	oTarget = Var2Obj(vTarget);
	
	oContainer.appendChild(oTarget.cloneNode(true));

	oNewBlock = oContainer.lastChild;

	iRand = sPostfix;

	str = oNewBlock.innerHTML;

	for(i = 0; i < idArray.length; i++)
		str = str.replace(new RegExp('id="' + idArray[i] + '[0-9]*"', "ig"), 'id="' + idArray[i] + iRand + '"');

	for(i = 0; i < idArray.length; i++)
		str = str.replace(new RegExp('id=' + idArray[i] + '[0-9]*', "ig"), 'id="' + idArray[i] + iRand + '"');
		
	oNewBlock.id = String(oNewBlock.id).replace(new RegExp('[0-9]*', "ig"), '') + iRand;

	oNewBlock.innerHTML = str;
	
	return oNewBlock;
}


/**
 * Search element by id in specified container
 *
 * @param	variant	vContainer
 * @param	string	sTargetId
 *
 * @return	object					found element
 */
function getElementById(vContainer, sTargetId)
{
	vContainer = Var2Obj(vContainer);
	
	if(vContainer.id == sTargetId)
		return vContainer;
		
	vContainer = vContainer.firstChild;
	
	oRes = null;
	
	while(isVal(vContainer))
	{
		if(vContainer.id == sTargetId)
			return vContainer;
			
		oRes = getElementById(vContainer, sTargetId);
		
		if(isVal(oRes) && oRes.id == sTargetId)
			return oRes;
			
		vContainer = vContainer.nextSibling;
	}
	
	return null;
}





/**
 * Return object, that is parent for specified child and it's ID like specified ID
 * 
 * @param	variant	vChild
 * @param	string	sTrgetId
 * @return	object
 */
function getParentById(vChild, sTargetId)
{
	vChild = Var2Obj(vChild);
	
	
	while(vChild.id != undefined && !isVal(vChild.id.match(new RegExp(sTargetId, 'ig'))))
		vChild = vChild.parentNode;
	
	return isVal(vChild.id) ? vChild : null;
}


/**
 * Insert HTML code into specified container before specified child
 * 
 * @param	string	sNewHtml
 * @param	variant	vContainer
 * @param	variant	vChild
 */
function insertHtmlBefore(sNewHtml, vContainer, vChild)
{
	vContainer = Var2Obj(vContainer);
	vChild = Var2Obj(vChild);
		
	oTxt = document.createElement('div');
	oTxt.innerHTML = sNewHtml;
	oNewNode = oTxt.firstChild;
		
	while(isVal(oNewNode))
	{
		vContainer.insertBefore(oNewNode.cloneNode(true), vChild);
		oNewNode = oNewNode.nextSibling;
	}
	
	delete(oTxt);
}

/**
 * Find element in string HTML code by ID
 * 
 * @param	string	sNewHtml
 * @param	string	sId
 *
 * @return	object
 */
function getElementFromHtml(sNewHtml, sId)
{
	oTmp = document.createElement('div');
	oTmp.innerHTML = sNewHtml;
	
	return getElementById(oTmp, sId);
}


/**
 * Set hover effects for all form elements (text inputs, passwords inputs, textareas)
 * 
 */
function hoverEffects() {
	//get all 
	var elements = document.getElementsByTagName('input');
	var j = 0;
	var hovers = new Array();
	for (var i4 = 0; i4 < elements.length; i4++) {
		if((elements[i4].type=='text')||(elements[i4].type=='password')) {
			hovers[j] = elements[i4];
			++j;
		}
	}
	elements = document.getElementsByTagName('textarea');
	for (var i4 = 0; i4 < elements.length; i4++) {
		hovers[j] = elements[i4];
		++j;
	}
	
	//add focus effects
	for (var i4 = 0; i4 < hovers.length; i4++) {
		hovers[i4].onfocus = function() {this.className += "Hovered";}
		hovers[i4].onblur = function() {this.className = this.className.replace(/Hovered/g, "");}
	}
}


/**
 * Check is variable is defined and not empty or null
 *
 * @param	string	name
 * @return	bool
 */
function isVal(variable)
{
	return (variable != '' && variable != undefined && variable != null);
}


/*
*	Search element in array
*/
function arraySearch(needle, haystack)
{
	var found = false;
	
	for(var i = 0; i < haystack.length; i++)
	{
		if(haystack[i] == needle)
		{
			found = true;
			break;
		}
	}
	
	if(found)
		return i
	else
		return -1;
}


/*
*	Delete element by value
*/
function arrayDelElement(value, haystack)
{
	return arrayMoveL(arraySearch(value, haystack), haystack);
}


/*
*	Delete element by index
*/
function arrayDelElementByIndex(index, haystack)
{
	return arrayMoveL(index, haystack);
}


/*
*	Move all elements one position left starting from index position
*/
function arrayMoveL(index, haystack)
{
	if(index < 0 || index > haystack.length)
		return haystack;
		
	for(var i = index; i < haystack.length-1; i++)
		haystack[i] = haystack[i+1];
		
	haystack.pop();
		
	return haystack;
}


/*
*	Print Array Information
*/
function printArrayInfo(array)
{
	alert(array + "\n"+array.length);
}


/**
 * Set cookie with specified name and value
 *
 * @param	string	name
 * @param	string	value
 * @param	int		hours			experiation time
 * @return
 */
function setCookie(name, value, hours)
{
	if(!isVal(hours))
		hours = 12;
		
	if(hours)
	{
		var date = new Date();
		date.setTime(date.getTime()+(hours*3600));
		var expires = "; expires=" + date.toGMTString();
	}
	else
		var expires = "";
	document.cookie = name+'='+value+expires+'; path=/';
}


/**
 * Get cookie by name
 *
 * @param	string	name
 * @return	string
 */
function getCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


/**
 * Unset Cookie by name
 *
 * @param	string	name
 */
function unsetCookie(name)
{
	set_cookie(name,"",-1);
}


/*
*	Hide preload screen
*/
function hideScreen()
{
	hideObject('screen');
	hideObject('screenMessage');
}

/*
*	Show preload screen
*/
function showScreen()
{
	showObject('screen');
	showObject('screenMessage');
}


/**
*	Hide object by Id
*	@param	int		id
*/
function hideObject(id)
{
	oObject = Var2Obj(id);
	oObject.style.display = 'none';
}


/**
*	Show object by Id
*	@param	int		id
*/
function showObject(id)
{
	oObject = Var2Obj(id);
	oObject.style.display = 'block';
}


/**
 * Add for with stated parameters and content and submit it
 *
 * @param	string	sAction
 * @param	string	sContent
 * @param	string	sWndName		target frame to submit into
 * @param	string	sFileName
 * @TODO	remove last parameter and test the function
 */
function addSubmitForm(sAction, sContent, sWndName, sFileName)
{
	sId = String(sWndName).match(/[0-9]+/);
	
	if(isVal(document.getElementById(sWndName)))
	{
		//	frame and form are already created then submit form
		document.getElementById("addImgForm" + sId).submit();
		return false;
	}
	
	
	//	create iframe - trget frame for submission
	oWnd = getBrowserType() == 'ie' ? document.createElement('<' + 'iframe' + ' name="' + sWndName + '">') : document.createElement('iframe');
	oWnd.name = sWndName;
	oWnd.id = sWndName;
	oWnd.className = 'hidden';
	
	
	//	create form for submission
	oFrm = getBrowserType() == 'ie' ? document.createElement('<form' + ' name="addImgForm' + sId + '" enctype="multipart/form-data">') : document.createElement('form');
	oFrm.action = sAction;
	oFrm.innerHTML = sContent;
	oFrm.method = 'post';
	oFrm.name = "addImgForm" + sId;
	oFrm.id = oFrm.name;
	oFrm.enctype = "multipart/form-data";
	oFrm.className = 'hidden';
	oFrm.target = sWndName;
	
	document.body.appendChild(oFrm);
	document.body.appendChild(oWnd);
	
	oFrm.appendChild(document.getElementById('addImgFldReal' + sId));
	
	oFrm.submit();
	
	document.getElementById('addImgItmReal' + sId).innerHTML = '';
	hideObject('addImgItmReal' + sId);
	document.getElementById('addImgFldReal' + sId).parentNode.removeChild(document.getElementById('addImgFldReal' + sId));
}


/**
 * Create and return created hidden input with specified name and value
 *
 * @param	string	sName
 * @param	string	sValue
 * @return	object
 */
function addHiddenInput(sName, sValue)
{
	oRnd = getBrowserType() == 'ie' ? document.createElement('<input name="' + sName + '">') : document.createElement('input');
	oRnd.type = 'hidden';
	oRnd.className = 'hidden';
	oRnd.name = sName;
	oRnd.value = sValue;
	
	return oRnd;
}


/**
 * Return object with specified by vVar
 * If vVar is object then returns it directly
 *
 * @param	variant	vVar		ID of object
 * @return	object
 */
function Var2Obj(vVar)
{
	return typeof(vVar) != 'object' ? document.getElementById(vVar) : vVar;
}


/**
 * Returns browser type
 *
 * @return	string			browser type
 */
function getBrowserType()
{
	// browser name
	ua = navigator.userAgent.toLowerCase(); 
	res = '';
	
	res	= (ua.indexOf('gecko') != -1) ? 'gecko' : res;
	res	= ((ua.indexOf('gecko') != -1) && ua.indexOf("gecko/") + 14 == ua.length) ? 'mozilla' : res;
	res	= ((ua.indexOf('gecko') != -1) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) ) ? 'netscape' : res;
	res	= ( (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1) ) ? 'ie' : res;
	res	= (ua.indexOf("opera") != -1) ? 'opera' : res;
	
	return res;
}







aBxMeta =
{
	'compare'	:	'Compare',
	'close'		:	'Close',
	'view'		:	'View'
};


/**
 * Language function (show words by metas)
 *
 * @param	string	sMeta		metaword
 * @return	string				real phrase, associated with specified metaword
 */
function w(sMeta)
{
	
	if(isVal(aBxMeta[sMeta]))
		return aBxMeta[sMeta];
		
	return '_' + sMeta + '_';
}


/**
 * Out specified text in new browser window
 *
 * @param	variant	s		text to show
 */
function debugOut(s)
{
	wnd = window.open();
	wnd.document.write(s);
}