/*
	Store finder selectors
*/
function refreshStoreFinder(_country, _city, _div, _action, _formid) {
	new Ajax.Updater( _div, "/system/accent/store_find_ajax.asp?div="+_div+"&formid="+_formid+"&country="+_country+"&city="+_city+"&action="+_action+"&r=" + new Date().valueOf(), { method: "get" });
}
function parseResultStoreFinder(_div, _city) {
	new Ajax.Updater( _div, "/system/accent/store_find_result_ajax.asp?city="+_city+"&r=" + new Date().valueOf(), { method: "get" });
}

/*
	register.asp > get the states list for the selected country
*/
function GetCountryStatesForCountry(_country, _div, _select, _selectedState)
{
	_select = $(_select);
	new Ajax.Request('/system/user/get_country_states.asp?country=' + _country, {
		method: 'get',
		onSuccess: function(transport) {
			// truncate options
			// add default option
			_select.options.length = 1;
			
			// get state object
			eval('var list = ' + transport.responseText);
			
			var i = 1;
			// add options
			for (var key in list) {
				_select.options[i] = new Option(list[key], key);
				if (key == _selectedState) _select.options[i].selected = true;
				i++;
			}
			
			
			// show div
			if (_select.options.length > 1)
				$(_div).show();
			else
				$(_div).hide();
		}
	});
}

/*
	register.asp > check dayes for valid birth date
*/
function fillDay(fo, sel)
{
	y = fo.uBirthYear[fo.uBirthYear.selectedIndex].value;
	m = fo.uBirthMonth[fo.uBirthMonth.selectedIndex].value;
	fo.uBirthDay.options.length = 0;
	if (y && m)
	{
		leap = !( y % 4 ) && ( ( y % 100 ) || !( y % 400 ) );
		days = [31,(leap?29:28),31,30,31,30,31,31,30,31,30,31][m-1];
		for(x=1;x<days+1;x++)
		{
			var day=(x < 10 ? "0"+x:x)
			fo.uBirthDay.options[x-1] = new Option(day, day);
			if (day == sel)
				fo.uBirthDay.options[x-1].selected = true;
		}
	}
	else
	{
		fo.uBirthDay.options[0] = new Option();
	}
}

function valRegExp(value, pattern)
{
	var regX = new RegExp(pattern,"i")
	return regX.test(value)
}

/*
 checkout.asp > validate creditcard input
*/
function checkcreditcardV2()
{
	var ccbox=document.getElementById("fcc");
	var cvcbox=document.getElementById("cvx2");

	if(ccbox && (!isValidStringLen(ccbox.value, 14) && !isValidStringLen(ccbox.value, 15) && !isValidStringLen(ccbox.value, 16)))
	{
		alert(ccbox.getAttribute("retstring"));
		ccbox.select();
	}
	else if(cvcbox && (!isValidStringLen(cvcbox.value, 3) && !isValidStringLen(cvcbox.value, 4)) )
	{
		alert(cvcbox.getAttribute("retstring"));
		cvcbox.select();
	}
	else
	{
		justhide("orderbuttons");
		justhide("order_payments");

		justshow("pleasewait");

		formObj=document.getElementById("creditcard");
		formObj.action='cc_save.asp';
		formObj.submit();		
	}
}


function open_xml_edit()
{
	matrix_window = window.open("matrix.asp", "matrix", "status=yes,scrolling=yes,width=800,height=600");
}

/*
	redirects to url if confirm(msg)
*/
function confirm_del(msg, url)
{
	if(confirm(msg))
		location.href = url;
}
/*
	product.asp > check the selected size
*/
function validateSizeSelection(form, msg)
{
	var msg = new String(msg);

	form = $(form);

	if (form.s)
	{
		if (form.s.value != 0)
			return true;
		else
		{
			alert(msg);
			return false;
		}
	}
	else
		return true;
}

/*
	check all checkboxes in the form (except for the "allbox")
*/
function CheckAll(formObj, matchFirst)
{
	var first = false;
	for (var i = 0, x = 0; i < formObj.elements.length; i++)
	{
		var e = formObj.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		{
			if (x++ == 0)
			{
				first = !e.checked;
			}
			e.checked = (matchFirst ? first : !e.checked);
		}
	}
}
/*
	evaluate func then 'enter'-key is pressed
*/
function checkKey(e, func)
{
	if(e.keyCode == 13)
		eval(func);
}


/*
	popup windows
*/
function ScreenObj()
{
	var frameWidth=0;
	var frameHeight=0;

	frameWidth = self.screen.width;
	frameHeight = self.screen.height;

	scrn = new Object();
	scrn.x=frameWidth;
	scrn.y=frameHeight;

	return scrn;

}

function open_popup(url, handler, w, h)
{
	var scrn = new ScreenObj();
	var wa = scrn.x;
	var ha = scrn.y;
	ha = (ha/2)-(h/2)
	wa = (wa/2)-(w/2)
	wHandler = window.open(url, handler ,'width='+w+',height='+h+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=yes,resizable=yes,status=yes,top=0,left=0');
	wHandler.focus();
	wHandler.moveTo(wa,ha);
}

/*
	submits the form aye!
*/
function submit_form(_id)
{
	if( $(_id) && $(_id).action.length > 0 )
	{
		$(_id).submit();
	}
	else
	{
		alert("This is not a valid form ID attribute, check your code.");
	}
}

/*
	toggle a layer between visible and hidden
*/
function ToggleLayer(_id)
{
	$(_id).toggle();
}

if (typeof Panagora != 'object')
	Panagora = {};

Panagora.FilterOrderListByCountry = function (countryList) {
	var container, select, button;

	var init = function () {
		var countries = [];
		for (var countryCode in countryList)
			countries.push(countryCode);
		countries.sort();
		
		button = $$('.filter-by-country');
		if (button) {
			button = button.pop();
			button.onclick = show;
			
			container = new Element('div', { 'class': 'filter-by-country-container', 'style': 'display:none' });
			container.style.position = 'absolute';
			
			select = new Element('select', { 'multiple': 'multiple' });
			select.options[0] = new Option('*', 'all', true);
			for (var i = 0, l = countries.length; i < l; i++) {
				select.options[i+1] = new Option(countries[i], countries[i], true);
			}
			select.size = countries.length + 1;
			container.appendChild(select);
			select.onclick = filter;
			
			//button.parentNode.style.position = 'relative';
			button.parentNode.appendChild(container);

			container.style.right = (-container.getWidth() - 5) + 'px';
			
			$$('#multicheckform input[type=checkbox][name=order]').each(function (checkbox) {
				checkbox.disabled = false;
			});
		}
	}

	var show = function () {
		container.show();
		button.onclick = hide;
	}
	this.show = show;
	
	var hide = function () {
		container.hide();
		button.onclick = show;
	}
	this.hide = hide;
	
	var filter = function () {
		if (select.options[0].selected) { // all
			selectAll();
		}
		else {
			var count = 0;
			var i = select.options.length;
			while (i-- > 1) {
				console.log(select.options[i])
				var opt = select.options[i];
				if (opt.selected) {
					$$('#multicheckform tr.country' + opt.value).each(function (row) {
						row.show();
						var checkbox = row.down('input[type=checkbox][name=order]');
						checkbox.disabled = false;
						count++;
					});
				}
				else {
					$$('#multicheckform tr.country' + opt.value).each(function (row) {
						row.hide();
						var checkbox = row.down('input[type=checkbox][name=order]');
						checkbox.disabled = true;
					});
				}
			}
			$('number-of-orders').innerHTML = count;
		}
	}
	
	var selectAll = function () {
		var count = 0;
		var i = select.options.length;
		while (i-- > 0) {
			select.options[i].selected = true;
		}
		$$('#multicheckform input[type=checkbox][name=order]').each(function (checkbox) {
			checkbox.disabled = false;
		});
		$$('#multicheckform > table tbody tr').each(function (row) {
			row.show();
			count++;
		});
		$('number-of-orders').innerHTML = count;
	}
	
	init();
}

function open_popup_noscroll(url, handler, w, h)
{
	var wa = screen.Width;
	var ha = screen.Height;
	ha = (ha/2)-(h/2)
	wa = (wa/2)-(w/2)
	wHandler = window.open(url, handler ,'width='+w+',height='+h+',location=no,menubar=no,directories=no,toolbar=no,scrollbars=no,resizable=no,status=no,top='+ha+',left='+wa);
//	wHandler.focus();
}