/* *************************************************************************
*
*  File: common.js
*  Purpose: Commonly used functions
*
************************************************************************* */

/* *************************************************************************
*
*  Browser detection
*
************************************************************************* */
var sel0='';
var agt = navigator.userAgent.toLowerCase ();
var is_major = parseInt (navigator.appVersion);
var is_minor = parseFloat (navigator.appVersion);

var is_safari = (agt.indexOf ('safari') != -1);

var is_nav  = ((agt.indexOf ('mozilla') != -1) && (agt.indexOf ('spoofer') == -1) && (agt.indexOf ('compatible') == -1) && (agt.indexOf ('opera') == -1) && (agt.indexOf ('webtv') == -1) && (agt.indexOf ('hotjava') == -1) && (agt.indexOf ('safari') == -1));

var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf (";nav") != -1) || (agt.indexOf ("; nav") != -1)));
var is_nav6 = (is_nav && (is_major == 5) && (agt.indexOf("netscape") != -1) && (agt.indexOf ("netscape/7") == -1));
var is_nav6up = ((is_nav && (is_major >= 5)) || (is_nav && (agt.indexOf ('netscape/7') != -1)));
var is_gecko = (agt.indexOf ('gecko') != -1);

var is_firefox = (agt.indexOf ('firefox') != -1);

var is_ie     = ((agt.indexOf ("msie") != -1) && (agt.indexOf ("opera") == -1) && (agt.indexOf ("safari") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 5.0") != -1));
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf ("msie 5.5") != -1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf ("msie 6.") != -1));
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5 && !is_safari);

var is_opera = (agt.indexOf ("opera") != -1);
var is_opera2 = (agt.indexOf ("opera 2") != -1 || agt.indexOf ("opera/2") != -1);
var is_opera3 = (agt.indexOf ("opera 3") != -1 || agt.indexOf("opera/3") != -1);
var is_opera4 = (agt.indexOf ("opera 4") != -1 || agt.indexOf("opera/4") != -1);
var is_opera5 = (agt.indexOf ("opera 5") != -1 || agt.indexOf("opera/5") != -1);
var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4 && !is_opera5);
var is_opera7 = ((agt.indexOf ("opera/7.") != -1) || (agt.indexOf ("opera 7.") != -1));
var is_opera8 = ((agt.indexOf ("opera/8.") != -1) || (agt.indexOf ("opera 8.") != -1));

var dom = ((document.getElementById) ? true : false);

	var pat_email=/^[a-zA-Z][a-zA-Z\_\.\-\d]*@[a-zA-Z\_\.\-\d]+.[a-zA-Z]{2,4}$/;
	var pat_text=/^\s*$/;
	var pat_num=/^\d*$/;
	var pat_phone_num=/^[\d\s\-\_\+\(\)]*$/;



if (is_opera || is_nav6 || is_nav6up || is_firefox)
{
	addStyleSheet ('/templates/css/hacks.css');
}

var pathRoot = '';

var urlRoot = '';
urlRoot = document.location.href.split ('/');
urlRoot.length--;
urlRoot = urlRoot.join ('/') + '/';

/* *************************************************************************
*
*  function setInit (_pageType)
*
************************************************************************* */

function setInit (_pageType)
{
	switch (_pageType)
	{
		case 'index':
			break;

		case 'inner':
			break;

		case 'popup':
			break;

		default:
			break;
	}
}

/* *************************************************************************
*
*  function getObj (_idElement, _objParent)
*
************************************************************************* */

function getObj (_idElement, _objParent)
{
	var objElement = null;

	if (!_objParent)
	{
		_objParent = document;
	}

	if (document.getElementById)
	{
		objElement = _objParent.getElementById (_idElement);
	}

	return objElement;
}

/* *************************************************************************
*
*  function getX (_objElement)
*
************************************************************************* */

function getX (_objElement)
{
	var posX = 0;

	do
	{
		posX += _objElement.offsetLeft;
	}
	while ((_objElement = _objElement.offsetParent) != null);

	return posX;
}

/* *************************************************************************
*
*  function getY (_objElement)
*
************************************************************************* */

function getY (_objElement)
{
	var posY = 0;

	do
	{
		posY += _objElement.offsetTop;
	}
	while ((_objElement = _objElement.offsetParent) != null);

	return posY;
}

/* *************************************************************************
*
*  function getHeight (_objElement)
*
************************************************************************* */

function getHeight (_objElement)
{
	var posY = 0;

	if (typeof (_objElement) == 'object')
	{
		posY = _objElement.offsetHeight;
	}
	else
	{
		posY = document.getElementById (_objElement).clientHeight;
	}

	return posY;
}

/* *************************************************************************
*
*  function addStyleSheet (_URL)
*
************************************************************************* */

function addStyleSheet (_URL)
{
	var strStyleSheet = '@import url(' + _URL + ');',
		objStyleSheet = null;

	if (document.createStyleSheet)
	{
		document.createStyleSheet (_URL);
	}
	else
	{
		objStyleSheet = document.createElement ('link');
		objStyleSheet.rel = 'stylesheet';
		objStyleSheet.type = 'text/css';
		objStyleSheet.href = _URL;
//		objStyleSheet.href = 'data:text/css,' + escape (strStyleSheet);

		document.getElementsByTagName ('head')[0].appendChild (objStyleSheet);
	}
}

/* *************************************************************************
*
*  function openPopup (_url, _name, _title, _width, _height, _posX, _posY)
*
************************************************************************* */

function openPopup (_url, _name, _title, _width, _height, _posX, _posY)
{
	var strParams = 0,
		idWindow = null;

	_posX = Math.round ((window.screen.availWidth / 2) - (_width / 2));

	if (window.screen.availHeight - 24 < _height)
	{
		_posY = 0;
		_width += 17;
		_height = window.screen.availHeight - 24;
	}
	else
	{
		_posY = Math.round ((window.screen.availHeight / 2) - (_height / 2 + 12));
	}

	strParams = "scrollbars=no,status=yes,resizable=no,width=" + _width + ",height=" + _height + ",left=" + _posX + ",top=" + _posY;

	idWindow = window.open (_url, _name, strParams);

	return idWindow;
}


/* *************************************************************************
*
*  function openPopup1 (_url, _name, _title, _width, _height, _posX, _posY)
*
************************************************************************* */

function openPopup1 (_url, _name, _title, _width, _height, _posX, _posY)
{
	var strParams = 0,
		idWindow = null;

	_posX = Math.round ((window.screen.availWidth / 2) - (_width / 2));

	if (window.screen.availHeight - 24 < _height)
	{
		_posY = 0;
		_width += 17;
		_height = window.screen.availHeight - 24;
	}
	else
	{
		_posY = Math.round ((window.screen.availHeight / 2) - (_height / 2 + 12));
	}

	strParams = "scrollbars=yes,status=yes,resizable=no,width=" + _width + ",height=" + _height + ",left=" + _posX + ",top=" + _posY;

	idWindow = window.open (_url, _name, strParams);

	return idWindow;
}

/* *************************************************************************
*
*  function clearInputDefault (_this, _default)
*
************************************************************************* */

function clearInputDefault (_this, _default)
{
	if (_this.value == _default)
	{
		_this.value = '';
	}
}

/* *************************************************************************
*
*  function restoreInputDefault (_this, _default)
*
************************************************************************* */

function restoreInputDefault (_this, _default)
{
	if (_this.value == '')
	{
		_this.value = _default;
	}
}

/* *************************************************************************
*
*  function expandFolder ()
*
************************************************************************* */

function expandFolder (_idGroup, _suffixTitle, _suffixContent)
{
	var objGroup = getObj (_idGroup),
		objGroupTitle = getObj (_idGroup + _suffixTitle),
		objGroupContent = getObj (_idGroup + _suffixContent);

	if (objGroup.className.indexOf ('Expanded') != -1)
	{
		objGroup.className = objGroup.className.replace (/Expanded/ig, 'Collapsed');
	}
	else
	{
		objGroup.className = objGroup.className.replace (/Collapsed/ig, 'Expanded');
	}
}

/* *************************************************************************
*
*  function switchOptionSearch (_this, _idContainer, _idSearchInput)
*
************************************************************************* */

function switchOptionSearch (_this, _idContainer, _idSearchInput)
{
	var arrContainerChildren = _this.parentNode.parentNode.childNodes,
		posX = 35;
		objContainer = getObj (_idContainer),
		objSearchInput = getObj (_idSearchInput);

	for (var indexOptions = 0; indexOptions < arrContainerChildren.length; indexOptions++)
	{
		arrContainerChildren [indexOptions].className = 'item';
	}

	_this.parentNode.className = 'item this';

	posX = getX (_this) - getX (objContainer) + Math.round (_this.offsetWidth / 2) - 4;

	objContainer.style.backgroundPosition = posX + 'px 0px';

	objSearchInput.focus ();
}

/* *************************************************************************
*
*  function setAutoFocus (_idInput)
*
************************************************************************* */

function setAutoFocus (_idInput)
{
	var objInput = (_idInput != null ? getObj (_idInput) : null),
		objInputTemp = null;

/*	if (objInput == null)
	{
		for (var indexForms = 0; indexForms < document.forms.length; indexForms++)
		{
			for (var indexInput = 0; indexInput < document.forms[indexForms].elements.length; indexInput++)
			{
				objInputTemp = document.forms[indexForms].elements[indexInput];

				if (objInputTemp.type != 'checkbox' && objInputTemp.type != 'radio' && objInputTemp.type != 'select' && objInputTemp.type != 'hidden' && objInputTemp.type != 'button' && objInputTemp.type != 'image' && objInputTemp.type != 'submit')
				{
					objInput = objInputTemp;

					break;
				}
			}

			if (objInput != null)
			{
				break;
			}
		}
	}*/

	if (objInput != null)
	{
		objInput.focus ();
	}
}

/* *************************************************************************
*
*  function getElementCollectionByName(_name, _tags)
*
************************************************************************* */

function getElementCollectionByName (_name, _tags)
{
	var count = 0,
		objCollection = new Array (),
		tagsCollection = new Array (),
		namesCollection;

	if (_tags != null)
	{
		tagsCollection = _tags.split (',');
	}
	else
	{
		return null;
	}

	for (var indexTags = 0; indexTags < tagsCollection.length; indexTags++)
	{
		namesCollection = document.all.tags (tagsCollection[indexTags]);

		for (var indexNames = 0; indexNames < namesCollection.length; indexNames++)
		{
			if (namesCollection[indexNames].name == _name)
			{
				objCollection[count++] = namesCollection[indexNames];
			}
		}
	}

	if (count > 0)
	{
		return objCollection;
	}

	return null;
}

/* *************************************************************************
*
*  function getFormElementCollectionByName(_name, _tags)
*
************************************************************************* */

function getFormElementCollectionByName (_name)
{
	var count = 0,
		objCollection = new Array ();

	for (var indexForms = 0; indexForms < document.forms.length; indexForms++)
	{
		for (var indexInput = 0; indexInput < document.forms[indexForms].elements.length; indexInput++)
		{
			if (document.forms[indexForms].elements[indexInput].name == _name)
			{
				objCollection[count++] = document.forms[indexForms].elements[indexInput];
			}
		}
	}

	if (count > 0)
	{
		return objCollection;
	}

	return null;
}

/* *************************************************************************
*
*  function setCheckboxes (_this, _nameGroup)
*
************************************************************************* */

function setCheckboxes (_this, _nameGroup)
{
	var elementsList = new Array ();

	elementsList = getFormElementCollectionByName (_nameGroup);

	for (var indexCheckboxes = 0; indexCheckboxes < elementsList.length; indexCheckboxes++)
	{
		elementsList[indexCheckboxes].checked = (_this.checked ? true : false);
	}
}

/* *************************************************************************
*
*  function loadSelectbox (_objSelectboxID, _arrValues)
*
************************************************************************* */

function loadSelectbox (_objSelectbox, _arrValues)
{
	_objSelectbox.options.length = 0;

	for (var indexValues = 0; indexValues < _arrValues.length; indexValues++)
	{
		_objSelectbox.options[indexValues] = new Option (_arrValues[indexValues][1], _arrValues[indexValues][0]);
	}
}

/* *************************************************************************
*
*  function closeWindow ()
*
************************************************************************* */

function closeWindow ()
{
	window.close ();
}

/* *************************************************************************
*
*  function switchFormType (_idGroupActive, _listIDGroupInactive, _idGroupInputFirst)
*
************************************************************************* */

function switchFormType (_idGroupActive, _listIDGroupInactive, _idGroupInputFirst)
{
	var arrIDGroupInactive = new Array (),
		objGroupActive = getObj (_idGroupActive),
		objGroupInputFirst = getObj (_idGroupInputFirst);

	if (_listIDGroupInactive != null)
	{
		arrIDGroupInactive = _listIDGroupInactive.split (',');
	}

	for (var indexGroup = 0; indexGroup < arrIDGroupInactive.length; indexGroup++)
	{
		getObj (arrIDGroupInactive[indexGroup]).className = getObj (arrIDGroupInactive[indexGroup]).className.replace (/Enabled/ig, 'Disabled');
	}

	objGroupActive.className = objGroupActive.className.replace (/Disabled/ig, 'Enabled');

	objGroupInputFirst.focus ();
}

/* *************************************************************************
*
*  function highlightRow (_this)
*
************************************************************************* */

function highlightRow (_this)
{
	if (_this.className.indexOf ('rwFocus') == -1)
	{
		if (_this.className == '')
		{
			_this.className = 'rwFocus';
		}
		else
		{
			_this.className = _this.className + ' rwFocus';
		}
	}
}

/* *************************************************************************
*
*  function unhighlightRow (_this)
*
************************************************************************* */

function unhighlightRow (_this)
{
	if (_this.className.indexOf ('rwFocus') != -1)
	{
		if (_this.className == 'rwFocus')
		{
			_this.className = '';
		}
		else
		{
			_this.className = _this.className.replace (/ rwFocus/ig, '');
		}
	}
}

/* *************************************************************************
*
*  function loadListContent (_this, _idListRecipient)
*
************************************************************************* */

function loadListContent (_this, _objListRecipientID, _objFrameID)
{


	if (_objListRecipientID=='formFilterModel') {
		document.getElementById("formFilterModel").disabled=true;
		document.getElementById("formFilterModification").disabled=true;
	}
	if (_objListRecipientID=='formFilterModification') document.getElementById("formFilterModification").disabled=true;
	var strListContent = '';

	getListContent (_objFrameID, _this.value, _objListRecipientID);

	if (_objListRecipientID=='formFilterModel' && document.getElementById("formFilterLabel").selectedIndex!=0) document.getElementById("formFilterModel").disabled=false;
	if (_objListRecipientID=='formFilterModification') document.getElementById("formFilterModification").disabled=false;

}

/* *************************************************************************
*
*  function getListContent (_objFrameID, _loaderURL)
*
************************************************************************* */

function getListContent (_objFrameID, _loaderURL, _objListRecipientID)
{
	var objFrame = getObj (_objFrameID);

	window.frames[_objFrameID].window.location = _loaderURL;
}

var sel;

sel='tr_0';

function check_form () {


	if (pat_text.test(document.formOrder.name.value)) {alert ("Заполните поле \"Имя\"");	return false;}
	if (pat_text.test(document.formOrder.shipping.value)) {alert ("Заполните поле \"Адрес доставки\"");	return false;}
	if (pat_text.test(document.formOrder.phone.value)) {alert ("Заполните поле \"Контактный телефон\"");	return false;}
	if (!pat_text.test(document.formOrder.email.value) && !pat_email.test(document.formOrder.email.value)) {alert ("Заполните правильно поле \"E-mail\"");	return false;}

	return true;
}

function sel_hr(id) {
	var i;
	i=1;
	while (document.getElementById("hr"+i)) {
		document.getElementById("hr"+i).className='nav_img_pr';
		document.getElementById("img_pr_"+i).style.display='none';
		i=i+1;
	}
	document.getElementById("hr"+id).className='nav_img_pr_a';
	document.getElementById("img_pr_"+id).style.display='';
}

function icon_add_bascet(id) {
	document.getElementById('bas_000').className='item itemBasket';
	document.getElementById('cat_'+id+'_1').style.display='none';
	document.getElementById('cat_'+id+'_2').style.display='';
	if (document.getElementById('cat_'+id+'_1_an')) document.getElementById('cat_'+id+'_1_an').style.display='none';
	if (document.getElementById('cat_'+id+'_2_an')) document.getElementById('cat_'+id+'_2_an').style.display='';
};

