var $j = jQuery.noConflict();

function GetSelect(id) {
	return document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
}

function ShowLoading(id, alt, label, centered) {
	if (typeof centered == 'undefined') {
		centered = false;
	}
	
	var html = '<img src="' + BASE_URL + 'templates/customer/classic/images/1x1.gif" class="ajax-loader" alt="' + alt + '" />';
	
	if (label) {
		html += ' ' + label;
	}
	
	if (centered) {
		html = '<div align="center">' + html + '</div>';
	}
	
	$j('#' + id).html(html);
}

function ProcessMsgs(msg_txt, err_txt) {
	if (msg_txt !== '' || err_txt !== '') {
		$j.getJSON(BASE_URL + 'messages.php', {msg: $j.base64Decode(msg_txt), err: $j.base64Decode(err_txt)},
			function(obj) {
				if (obj && obj.html) {
					$j('#sv-msgs-block').html($j.base64Decode(obj.html));
				
					var i     = 0;
					var count = obj.arg4;
					var timeToShow = obj.arg5;
				
					fadeItOut = function() {
						$j('#sv-msgs-block').fadeOut(200, function() {
							fadeItIn();
						});
						i++;
					};
					
					fadeItIn = function() {
						$j('#sv-msgs-block').fadeIn(200, function() {
							if (i < count - 1) {
								fadeItOut();
							} else {
								if (msg_txt != '' && timeToShow > 0) {
									setTimeout(function() {
										$j('#sv-msgs-block').fadeOut(1000);
									}, timeToShow * 1000);
								}
							}
						});
					};
					
					setTimeout(function() {
						fadeItIn();
					}, 50);
				}
			}
		);
	} else {
		$j('#sv-msgs-block').html('');
	}
	return false;
}

function ClearMsgs() {
	$j('#sv-msgs-block').html('');
	return false;
}

function trim(s) {
	var l = 0;
	var r = s.length - 1;
	
	while (l < s.length && s[l] == ' ') {
		l++;
	}
	
	while (r > l && s[r] == ' ') {
		r -= 1;
	}
	
	return s.substring(l, r + 1);
}

function isCookieEnabled() {
	var cookieEnabled = (navigator.cookieEnabled) ? true : false;

	if (!cookieEnabled) {
		document.cookie = "testcookie";
		var cookies = document.cookie.split(';');
		
		for (var i = 0; i < cookies.length; i++) {
			if (trim(cookies[i]) == 'testcookie') {
				cookieEnabled = true;
				break;
			}
		}

		document.cookie = "";
	}

	return cookieEnabled;
}

function dec2hex(n) {
	n = parseInt(n); var c = 'ABCDEF';
	var b = n / 16; var r = n % 16; b = b-(r/16); 
	b = ((b>=0) && (b<=9)) ? b : c.charAt(b-10);    
	return ((r>=0) && (r<=9)) ? b+''+r : b+''+c.charAt(r-10);
}

function getRGB(color) {
	var result;

	// Look for rgb(num,num,num)
	if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
		return dec2hex(parseInt(result[1])) + '' + dec2hex(parseInt(result[2])) + '' + dec2hex(parseInt(result[3]));

	// Look for rgb(num%,num%,num%)
	if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
		return dec2hex(parseFloat(result[1]) * 2.55) + '' + dec2hex(parseFloat(result[2]) * 2.55) + '' + dec2hex(parseFloat(result[3]) * 2.55);

	// Look for #a0b1c2
	if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
		return result[1] + result[2] + result[3];

	// Look for #fff
	if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
		return result[1] + result[1] + result[2] + result[2] + result[3] + result[3];

	// Otherwise, we're most likely dealing with a named color
	return color;
}

function ReloadCaptcha() {
	$j.getJSON(BASE_URL + 'captcha.php',
		function(data) {
			$j('.sv-captcha').attr('src', BASE_URL + data.html);
		}
	);
}

function BannerClicked(id) {
	$j.post(BASE_URL + 'modules/banners/banners_js.php', {act: 'click', id: id, page: location.href});
}

function ConfirmDialog(title, text, on_yes_button, on_no_button) {
	$j('#sv-confirm-dialog-text').html(text);

	$j.blockUI({
		theme: true,
		title: title,
		message: $j('#sv-confirm-dialog'),
		overlayCSS: {
			backgroundColor: '#fff',
			opacity: 0.6
		}
	});
	
	$j('#sv-confirm-dialog-yes-button').click(function(){
		on_yes_button.call();
	});
	
	$j('#sv-confirm-dialog-no-button').click(function(){
		on_no_button.call();
		$j.unblockUI();
	});
}
