var TableHelper = {
	setCheck: function(tr, id)
	{
	    if ($(tr).tagName == 'TR')
		{
			var oo = $(id);
			if ( oo.checked && oo.type != 'radio')
			{
				oo.checked = false;
				$(tr).removeClassName('selTr');
			}
			else
			{
				oo.checked = true;
				if (oo.type == 'radio')
				{
					var trs = $A($(tr).parentNode.getElementsByTagName('tr'));
					trs.each( function (item, index)
					{
						$(item).removeClassName('selTr');
					} );	
				}
				$(tr).addClassName('selTr');
			}
		}
	},
	
	unCheck: function(ch)
	{
	    $(ch).checked = !$(ch).checked;
	},
	
	checkAll: function(formID, tableID, val)
	{
		var checkboxes = Form.getInputs(formID, 'checkbox');
        checkboxes.each( function (item, index)
		{
			$(item).checked = val;
		} );
		
		var trs = $A($(tableID).getElementsByTagName('tr'));
		trs.each( function (item, index)
		{
		    if (val)
		    	$(item).addClassName('selTr');
			else
				$(item).removeClassName('selTr');
		} );
	}
}

function step3cc(o, rule)
{
	id = $(o).id;
	checked = $(o).checked;
	if (id == 'AuctionVisibilityRule3' && (rule || $('AuctionBidRule4').checked) && !checked)
	{
		$(o).checked = true;
		alert(alerts[0]);
		return false;
	}
	if (id == 'AuctionBidRule4' && checked && !$('AuctionVisibilityRule3').checked)
	{
		if (confirm(alerts[4] + '\n' + alerts[5]))
		{
			$('AuctionVisibilityRule3').checked = true;
			return true;
		}
		else
		{
			$(o).checked = false;
			return false;
		}
	}
	if (id == 'AuctionVisibilityRule5' && $('AuctionBidRule1').checked && checked)
	{
		$(o).checked = false;
		alert(alerts[2]);
		return false;
	}
	if (id == 'AuctionBidRule1' && !$('AuctionVisibilityRule1').checked && !$('AuctionVisibilityRule3').checked && checked)
	{
		$(o).checked = false;
		alert(alerts[3]);
		return false;
	}		
	if (id == 'AuctionBidRule1' && $('AuctionVisibilityRule5').checked && checked)
	{
		$(o).checked = false;
		alert(alerts[1]);
		return false;
	}
	if (id == 'AuctionBidRule3' && !$('AuctionBidRule2').checked && checked)
	{
		$(o).checked = false;
		alert(alerts[7]);
		return false;
	}
	if (id == 'AuctionBidRule2' && $('AuctionBidRule3').checked && !checked)
	{
		$(o).checked = true;
		alert(alerts[7]);
		return false;
	}
	if (id == 'AuctionVisibilityRule4' && !$('AuctionVisibilityRule2').checked && !$('AuctionVisibilityRule3').checked && !checked)
	{
		$(o).checked = true;
		alert(alerts[6]);
		return false;
	}		
	if (id == 'AuctionVisibilityRule1' && $('AuctionBidRule1').checked && !$('AuctionVisibilityRule3').checked && !checked)
	{
		$(o).checked = true;
		alert(alerts[3]);
		return false;
	}		
	if (id == 'AuctionVisibilityRule3' && $('AuctionBidRule1').checked && !$('AuctionVisibilityRule1').checked && !checked)
	{
		$(o).checked = true;
		alert(alerts[3]);
		return false;
	}		
}

var thousands_sep;
var dec_point;

function number_format(o)
{
	var val = $(o).value;
	val = parse_number(val);
	if (!isNaN(val))
	{
		$(o).value = reformat(val);
	}
}
function parse_number(val)
{	
	if(thousands_sep == undefined)
		thousands_sep = "";
	if(dec_point == undefined)
		dec_point = ".";

	if (thousands_sep)
	{
		if (thousands_sep == '.')
			regexp_ts = "\\" + thousands_sep;
		else
			regexp_ts = thousands_sep; 
		val = eval("val.replace(/"+regexp_ts+"/g, '')");
	}
	if (dec_point != ".")
		val = eval("val.replace(/"+dec_point+"/g, '.')");
	
	val = eval("val.replace(/,/g, '.')");		
	val = eval("val.replace(/ /g, '')");

	return val;
}

function reformat(val)
{
	var out = '';
	var dec_part = '';
	var dec
	val = String(val);
	dec = val.indexOf('.');
	if (dec > 0)
	{
		dec_part = val.substr(dec+1);
		val = val.substr(0, dec);
	}
	for (i = 1; i <= val.length; i++)
	{
		if (i % 3 == 1 && i>1)
			out = thousands_sep + out;
		out = val.charAt(val.length - i) + out;
	}
	if (dec_part)
		out = out + dec_point + dec_part;
	return out;
}

var itemList;
var paramList;
var origBids;

function bid_parse(item_id)
{
	var param_id;
	var bid_found = false;
	if (itemList != undefined && paramList != undefined)
	{
		// check all params for entry
		for (pid=0; pid<paramList.length; pid++)
		{
			field_name = 'Bidparam_' + item_id + '_' + paramList[pid] + '_A';
			field_name2 = 'Bidparam_' + item_id + '_' + paramList[pid] + '_B';
			var val1 = parse_number($(field_name).value);
			var val2 = parse_number($(field_name2).value);
			if (val1 || val2)
			{
				bid_found = true;
			}
		}
		// check all params for errors
		for (pid=0; pid<paramList.length; pid++)
		{
			field_name = 'Bidparam_' + item_id + '_' + paramList[pid] + '_A';
			field_name2 = 'Bidparam_' + item_id + '_' + paramList[pid] + '_B';
			var val1 = parse_number($(field_name).value);
			var val2 = parse_number($(field_name2).value);
		
			if (bid_found && (!val1 || !val2 || isNaN(val1) || isNaN(val2) || val1 != val2))
			{
				$(field_name).addClassName('bidError');
				$(field_name2).addClassName('bidError');
			}
			else
			{
				$(field_name).removeClassName('bidError');
				$(field_name2).removeClassName('bidError');
			}
			if (!isNaN(val1))
			{
				$(field_name).value = reformat(val1);
			}
			if (!isNaN(val2))
			{
				$(field_name2).value = reformat(val2);
			}
		}
		// sum the prices
		var sum = 0;
		var fname;
		for (si=0;si<itemList.length;si++)
		{
			fname = 'Bidparam_' + itemList[si] + '_' + paramList[0] + '_A';
			if ($(fname) && !$(fname).hasClassName('bidError'))
			{
				sum += Number(parse_number($(fname).value));
			}
		}
		$('bidSum').innerHTML = reformat(sum);
	}
}

var normalConfirm;
var bigChangeConfirm;
var emptyAlert;
var errorAlert;
var emptyFieldAlert;

function bidSubmit()
{
	// default alerts
	if (bigChangeConfirm == undefined)
	{
		bigChangeConfirm = 'There is more then 10% change in the prices. \n Are you sure?';
	}
	if (normalConfirm == undefined)
	{
		normalConfirm = 'Are you sure?';
	}
	if (emptyAlert == undefined)
	{
		emptyAlert = 'The form is empty';
	}
	if (errorAlert == undefined)
	{
		errorAlert = 'There are errors on the form';
	}
	if (emptyFieldAlert == undefined)
	{
		emptyFieldAlert = 'Please fill all parameter fields!';
	}
	var re = false;
	var params = true;
	var paramFocus;
	// check for empty form
	for (si=0;si<itemList.length;si++)
	{
		fname = 'Bidparam_' + itemList[si] + '_' + paramList[0] + '_A';
		fname2 = 'Bidparam_' + itemList[si] + '_' + paramList[0] + '_B';
		if (($(fname) && $(fname).value) || ($(fname2) && $(fname2).value))
		{
			re = true;
			// check if all the parameter fields are filled
			for (pk=0;pk<paramList.length;pk++)
			{
				pname = 'Bidparam_' + itemList[si] + '_' + paramList[pk] + '_A';
				pname2 = 'Bidparam_' + itemList[si] + '_' + paramList[pk] + '_B';
				if (($(pname) && !$(pname).value) || ($(pname2) && !$(pname2).value))
				{
					params = false;
					if (!$(pname).value)
						paramFocus = pname;
					else
						paramFocus = pname2;
					break;
				}
			}
		}
	}
	if (re == false)
	{
		alert(emptyAlert);	
	}
	else
	{
		// empty param field found
		if (params == false)
		{
			alert(emptyFieldAlert);		
			re = false;	
			if (paramFocus)
			{
				$(paramFocus).focus();
			}			
		}
	}
	if (re)
	{
		// check for errors 
		cc = document.getElementsByClassName('bidError');
		if (cc.length > 0)
		{
			re = false;
			alert(errorAlert);
			cc[0].focus();
			cc[0].select();
		}
	}
	if (re)
	{
		var bigChange = false;
		for (si=0;si<itemList.length;si++)
		{
			fname = 'Bidparam_' + itemList[si] + '_' + paramList[0] + '_A';
			if ($(fname) && $(fname).value)
			{
				// check for more then 10% change
				newVal = parse_number($(fname).value);
				if (origBids[si] && Math.abs(newVal - origBids[si]) > origBids[si] / 10)
				{
					bigChange = true;
					var bcO = fname;
				}
			}
		}
		if (bigChange)
		{
			re = confirm(bigChangeConfirm);
			if (!re && bcO)
			{
				$(bcO).focus();
				$(bcO).select(); 
			}
		}
		else
		{
			re = confirm(normalConfirm);
		}
	}
	return re;
}

function clear_item_fields()
{
	var fields = $('newAutionForm').getElements();
	fields.each( function (item, index)
	{
		if(item.type == 'text' || item.type == 'textarea')
		{
			item.value = '';
		}
	} );
	var msgs = $('newAutionForm').getElementsByClassName('error-message');
	if (msgs.length)
	{
		msgs.each( function (item, index)
		{
			item.hide();
		} );
	}
	if ($('ItemItemId'))
		$('ItemItemId').value = '';
	if ($('uploadedFiles'))
		$('uploadedFiles').innerHTML = '';
}

function scrolldown(o)
{
	$(o).scrollTop = $(o).scrollHeight
}

var tick;
function stm()
{
	sec = Number(sec) + 1;
	if(sec>59){sec=0;min=Number(min)+1;}
	if(min>59){min=0;hour=Number(hour)+1;}
	if(hour>23){hour=0;day=Number(day)+1;}
	if(day>monthdays){day=1;month=Number(month)+1;}
	if(month>12){month=1;year=Number(year)+1;}

	var stime = " ";
	var sdate = "";
	if (sec<=9) sec = "0" + sec;
	min = Number(min);
	if (min<=9) min = "0" + min;
	hour = Number(hour);
	if (hour<=9) hour = "0" + hour;

	sdate+=js_date_format(year, month, day); 
	stime+=hour+":"+min+":"+sec;
	$('serverdatum').innerHTML=sdate;
	$('servertime').innerHTML=stime;
	tick=setTimeout("stm()",1000);
}
var tltick;
var timeLeft;
function tlc()
{
	if (timeLeft != undefined)
	{
		var days = 0;
		var hours = 0;
		var minutes = 0;
		var seconds = 0;
		
		timeLeft--;
		var tl = timeLeft;
	
		if (tl > 86400)
		{
			days = Math.floor(tl / 86400);
			tl = tl % 86400;			
		}
		if (tl >= 3600)
		{
			hours = Math.floor(tl / 3600);
			tl = tl % 3600;			
		}
		if (tl >= 60)
		{
			minutes = Math.floor(tl / 60);
			tl = tl % 60;			
		}
		if (tl > 0)
		{
			seconds = tl;
		}
		
		out = '';
		if (days)
		{
			out += days + ' ' + timeNames[0] + ' ';
		}
		if (days || hours)
		{
			out += hours + ' ' + timeNames[1] + ' ';
		}
		if (days || hours || minutes)
		{
			out += minutes + ' ' + timeNames[2] + ' ';
		}
		if (!seconds)
			seconds = '0';
		if (days || hours || minutes || seconds)
		{
			out += seconds + ' ' + timeNames[3];
		}
		
		$('AuctionTimeLeft').innerHTML = out;
		if (timeLeft > 0)
		{
			tltick=setTimeout("tlc()",1000);
		}
		
		if (timeLeft < 120 && !$('AuctionTimeLeft').hasClassName('TLblink'))
		{
			$('AuctionTimeLeft').addClassName('TLblink');
		}
		else
		{
			$('AuctionTimeLeft').removeClassName('TLblink');
		}
	}
}

window.onload = function() {
	if ($('UserEmail') && $('UserPassword'))
	{
		if ($('UserEmail').value)
			$('UserPassword').focus();
		else
			$('UserEmail').focus();
	}
	if ($('serverdatum') && $('servertime'))
	{
		tick=setTimeout('stm()',1000);
	}
	if ($('message-box'))
	{
		//setTimeout('scrolldown(\'message-box\')',0);
		scrolldown('message-box');
	}	
	if ($('AuctionTimeLeft') && timeLeft != undefined)
	{
		tltick=setTimeout('tlc()',1000);
	}	
}