// ---------------------- Misc rowset functions ----------------------------------------

/**
 *  Find one row in a rowset, defined by it's $field's $value
 *	If $value is omitted, than $field is 'id' and $value is a second argument
 *	@param	array	$rowset	Rowset
 *	@param	string	$field	Field name
 *	@param	mixed	$value	Field value (optional)
 *	@return	integer	row index, of false if not found
 */
function rowset_findrow(rowset, field, value)
{
	if (typeof(rowset)!='object')
		return false;
	if (typeof(value)=='undefined')
		{ value=field; field='id'; }
	for (var i=0; i<rowset.length; i++)
		if (rowset[i][field] == value)
			return i;
	return false;
}

function numformat(number, decimals, thousands_sep, dec_point)
{
	number = parseFloat(number);
	if (typeof(decimals)=='undefined')	decimals = 0;
	if (typeof(dec_point)=='undefined')	dec_point = '.';
	if (typeof(thousands_sep)=='undefined')	thousands_sep = '';

	var result = (number<0) ? '-' : '' ;
	number = Math.abs(number);

	var intpart = String(Math.floor(number));
	if (thousands_sep!='' && intpart>=1000)
	{
		var newint = '';
		for (var i=0; i<Math.ceil(intpart.length/3); i++)
			newint = intpart.substring(intpart.length-(i+1)*3, intpart.length-i*3) + (i?thousands_sep:'') + newint;
		intpart = newint;
	}
	result += intpart;

	if (decimals>0)
	{
		var decpart = String(number - Math.floor(number));
		decpart = String( Math.round(decpart*Math.pow(10,decimals))/Math.pow(10,decimals) );
		decpart = decpart.substring(2,2+decimals);
		while (decpart.length<decimals)
			decpart += '0';
		result += dec_point+decpart;
	}

	return result;
}

function getArtPrice()
{
	var params=$('buyform').serialize();
	//delete 'shop.' in elements names to make values global
	params = unescape(params);
	var p = params.replace(/shop\./g,'');
	//encode #, which is recognized as anchor by toQueryParams
	p = p.replace('#','%23');
	params = p.toQueryParams();

	new Ajax.Request(absCorrect+'ajax.html?catalog2.display=getArticleValuesFromAttributes:json', {method:'post', parameters:params,onComplete: function(transport){  eval('res = '+ transport.responseText); $('articlePrice').innerHTML = numformat(res.article[0]['price'],2); $('artno').value = res.article[0]['artno']; $('indicator').style.display='none'; }});
	$('indicator').style.display='';
}

function hideErrorMsg (ev)
{
	 if (typeof ev.errorLayer == 'object' && ev.errorLayer != null)
	 {
	 	Element.remove(errorLayer);
	    errorLayer = null;
	 }
}
function createErrorLayer(elem,errorMsg)
{
	// errorLayer Contains the Message and the Style Informations
    errorLayer = document.createElement('div');
    errorLayer.setAttribute('id', 'absoluteerrorlayer_' + Math.random());
    document.getElementsByTagName('body')[0].appendChild(errorLayer);
    elem.errorLayer = $(errorLayer.id);
    // Set Element Class / Content
    Element.hide(errorLayer);
    Element.addClassName(errorLayer, 'form_validator_error_layer');
    errorLayer.innerHTML = errorMsg;

    // Get Posititon of the Element
    var text = elem.id;
    id = text.split("_");
    elem_to_show = $("attribute_"+id[1]);
    var element_position = Position.cumulativeOffset(elem_to_show);

    // Get Dimension of the Element
    var element_dimension = Element.getDimensions(elem_to_show);


    var position_x = element_position[0] + element_dimension.width;
    var position_y = element_position[1];


    errorLayer.style.position =  'absolute';
    errorLayer.style.top = position_y + 'px';
    errorLayer.style.left =  position_x + 'px';

    // Observe an on Click Event Endler to close an error Message by klicking
    Event.observe(errorLayer, 'click', hideErrorMsg.bind(elem));
    Element.show(errorLayer);

}

function checkForm()
{
	var elements = $('buyform').getElementsByClassName('attribute_mandatory');
	var checkboxChecked = 0;
	var firstCheckboxElem;
	var toSubmit = true;
	for (i=0;i<elements.length;i++)
	{
		if (elements[i].type == 'text' && elements[i].value.length == 0)
		{
			//alert("Your attribute is a mandatory field");
			createErrorLayer(elements[i],"* mandatory attribute");
			elements[i].select();
			elements[i].focus();
			toSubmit = false;
		}
		else hideErrorMsg(elements[i]);

		if(elements[i].type == 'checkbox') firstCheckboxElem = elements[i];
		if(elements[i].type == 'checkbox' && elements[i].checked)
		{
			checkboxChecked = 1;
		}
	}
	if (checkboxChecked == 0)
	{
		createErrorLayer(firstCheckboxElem,"* mandatory attribute");
		toSubmit = false;
	}

	return toSubmit;
}