// Upper Case first symbol
function ucfirst(obj)
{
    text = trim(obj.value);
    first = text.substr(0, 1);
    obj.value = first.toUpperCase() + text.substr(1);
}

// Trim string (left and right) and delete double spaces 
function trim(text)
{
	if ( typeof(text) == 'object' )
		text.value = trim(text.value);
	else
	{
		text = text.replace(/^\ +/, '');
		text = text.replace(/\ +$/, '');
		text = text.replace(/[\ ]{2,}/, ' ');

		return text;
	}
}

// Apply filters (brands, types, prices)
function apply_filters()
{
	p = Array();
	q = '';
	i = 0
	
	pt = document.all.ptypes.value;
	p1 = document.all.price1.value.replace(/[^0-9]/, ''); 
	p2 = document.all.price2.value.replace(/[^0-9]/, '');
	
	if ( pt ) { p[i] = 'ptype='+ pt; i++; }
	if ( p1 == '' || p1 > 0 ) {  p[i] = 'price1='+ p1; i++; }
	if ( p2 == '' || p2 > 0 ) {  p[i] = 'price2='+ p2; i++; }
	if ( i )
	{
		num = p.length - 1;
		for ( j = 0; j < num; j++ ) {
			q += p[j] + '&';
		}
		q += p[num];
	}

	if ( document.all.brands.value || q ) {
		location.href = '/catalog/'+ document.all.category.value +'/'+ ( document.all.brands.value ? document.all.brands.value +'/' : '' ) + ( q ? '?'+ q : '' );
	}
}

function select_elements(elements, value)
{
	for ( i = 0; i < elements.options.length; i ++ ) {
		if ( elements.options[i].value == value ) {
			elements.options[i].selected = true;
		}
	}
}

var marked_row = new Array;
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
	var theCells = null;

    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    }

    if (currentColor.indexOf("rgb") >= 0)
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    }

    if (newColor) {
        var c = null;
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            }
        }
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    }

    return true;
}