function showSystemIsBusy() {
	jQuery('#loading-layout').css('height', jQuery(document).height()).show();
	jQuery('#loading-image')
		.css('position', 'absolute')
		.css('z-index', 1000)			
		.css('top', jQuery(document).scrollTop() + (jQuery(window).height() / 2) - 50)
		.css('left', jQuery(window).width() / 2 - 50)
		.show();
}

function showSystemIsFree() {
	jQuery('#loading-layout, #loading-image').hide();
}

function redirect(url) {
	document.location.href = url;
}

function checkState() {
	if (jQuery('#country').val() == 'US') {
		jQuery('#state').hide().attr('name', 'undefined');
		jQuery('#state_ca').hide().attr('name', 'undefined');
		jQuery('#state_us').show().attr('name', 'data[state]');
	} else if (jQuery('#country').val() == 'CA') {
		jQuery('#state').hide().attr('name', 'undefined');
		jQuery('#state_us').hide().attr('name', 'undefined');
		jQuery('#state_ca').show().attr('name', 'data[state]');
	} else {
		jQuery('#state_ca').hide().attr('name', 'undefined');
		jQuery('#state_us').hide().attr('name', 'undefined');
		jQuery('#state').show().attr('name', 'data[state]');
	}
}

function setSelectedByValue(id, value) {
	var someOptions = $(id).options;
	for (var count = 0; count < someOptions.length; count++) {
		if (someOptions[count].value == value) {
			$(id).options[count].selected = true;
		}
	}
}

function inArray(what, where) {
	var a = false;
	for (var i = 0; i < where.length; i++) {
		if (what == where[i]) {
			a = true;
			break;
		}
	}
	return a;
}

function showWarning() {
	showMessageDialog('You are not currently subscibed to this service.<br/>Please contact your account manager ' + glManager + ' at <a href="mailto:' + glManagerEmail + '">' + glManagerEmail + '</a> or phone ' + glManagerPhone + ' for assistance.');
}
// TODO main page functions to separate js
function forgotPassword() {
	var html =
			'<div><div id="new-pass-errors"></div>' +
					'<br/>' +
					'<div id="new-pass-container">' +
					'	Please enter your email <input id="recovery-email" maxlenght="255" type="text">' +
					'</div></div>';

	showCustomDialog(function(paramObj) {
		jQuery.ajax({
			type: "POST",
			url: "/becomeclient/newpassword/",
			data: {
				email: jQuery('#recovery-email').val()
			},
			dataType : 'json',
			success: function(res) {
				if (res.status == true) {
					paramObj.dialog.dialog('close');
					showMessageDialog(res.message);
				} else {
					jQuery('#new-pass-errors').html(res.message)
				}
			}
		});
	}, html, {width: 400, height: 150, title: 'Login/Password Recovery'});
}

function viewMedia(src, type, imgW, imgH) {
	src = escape(src);
	if (type == 'o') {
		Modalbox.show('<embed width="600px" autostart="true" src="/adfiles/' + src + '"/>', {title: 'Player', slideDownDuration: 0.3, width: 650, height: 450});
	} else if (type == 'v') {
		var mediaHeight = 338;
		var mediaWidth = 413;
		var html = '';
		var deltaHeight = 0;
		if (Prototype.Browser.IE) {
			html =
					'<object id="player" height="' + mediaHeight + '" width="' + mediaWidth + '"' +
							'classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6">' +
							'   <param name="URL" value="/adfiles/' + src + '" />' +
							'   <param name="uiMode" value="none" />' +
							'   <embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"' +
							'		src="/adfiles/' + src + '"' +
							'       name="mediaplayer"' +
							'       width="413"' +
							'       height="338"' +
							'       uimode="none"' +
							'		autostart="true">' +
							'    </embed>' +
							'</object>' +
							'<table id="controls" width="255" align="center">' +
							'   <tr>' +
							'       <td align="middle"> <input onclick="document.player.Controls.Pause()"' +
							'       type="image" height="21" alt="Pause" width="21" ' +
							'           src="/images/pause_w.gif" value="Pause" />' +
							'       </td>' +
							'       <td width="52" align="center" valign="center"><input onclick="document.player.Controls.Play()" ' +
							'          type="image" height="30" alt="Play" width="30" ' +
							'           src="/images/play_w.gif" value="Play" />' +
							'       </td>' +
							'       <td align="center" valign="center"><input onclick="document.player.Controls.Stop()"' +
							'           type="image" height="21" alt="Stop" width="21" ' +
							'           src="/images/stop_w.gif" value="Stop" />' +
							'       </td>' +
							'   </tr>' +
							'</table>'
					;
			deltaHeight = 80;
		} else {
			html =
					'<embed id="player" uimode="none" src="' + glBaseUrl + '/adfiles/' + src + '" width="' + mediaWidth + '" height="' + mediaHeight + '" autostart="true"></embed>';
			deltaHeight = 45;
		}
		Modalbox.show(html, {title: 'Player', slideDownDuration: 0.3, width: mediaWidth + 45, height: mediaHeight + deltaHeight, afterLoad: onShowPlayer});
	} else {
		var width = 600;
		var height = 400;
		Modalbox.show('<img src="' + glBaseUrl + '/image/index?src=' + src + '&width=' + width + '&height=' + height + '" />', {title: '<a style="color:#fff" href="javascript:Modalbox.hide();showImageInPopupSrc(\'' + src + '\', ' + (imgW * 1 + 13) + ', ' + (imgH * 1 + 38) + ')">Fullscreen</a>', slideDownDuration: 0.3, width: 650, height: 450});
	}
	jQuery('#MB_span').html('__');
}

function onShowPlayer() {
	if (Prototype.Browser.IE) {
		$('MB_close').onclick = function() {
			document.player.Controls.Stop();
		};
		$('MB_overlay').onclick = function() {
			document.player.Controls.Stop();
		};
	}
}

function addAdToCart(id, $this) {
	jQuery.ajax({
		type : 'POST',
		url  : '/shopping/add/',
		cache : 'false',
		data : {
			'id' : id
		},
		dataType : 'json',
		success : function(res) {
			if (res.status == true) {
				jQuery($this).remove();
				checkCart();
			} else {
				showMessageDialog(res.text);
			}
		}
	});
}

function addAllAdsToCart() {
	var res = '';
	jQuery('.to-cart').each(function() {
		res += (res == '') ? jQuery(this).attr('did') : ',' + jQuery(this).attr('did');
	});

	jQuery.ajax({
		type : 'POST',
		url  : '/shopping/add-all/',
		data : {
			'ids' : res
		},
		cache : 'false',
		dataType : 'json',
		success : function(res) {
			if (res && res.status == true) {
				jQuery('.to-cart').remove();
				checkCart();
			} else {
				showMessageDialog('An error occurred');
			}
		}
	});
}

function checkCart() {
	if (jQuery('.to-cart').length <= 0) {
		jQuery('#all-ads').hide();
	} else if (jQuery('#all-ads').is(':hidden')) {
		jQuery('#all-ads').show();
	}
}

function login() {
	jQuery('#login-button').hide();
	jQuery('#login-loader').show();

	jQuery.ajax({
		type: "POST",
		url: "/auth/",
		data: {
			password: jQuery('#password').val(),
			login: jQuery('#username').val(),
			role: jQuery('#service').val(),
			ref: jQuery('#ref').val(),
			remember: jQuery('#remember').is(':checked') ? 1 : 0
		},
		dataType : 'json',
		success: function(res) {
			if (res.status == true) {
				document.location.href = res.data.url;
			} else {
				jQuery('#login-loader').hide();
				jQuery('#login-button').show();
				showMessageDialog(res.message);
			}
		}
	});
}

function register() {
	showLoadingDialog();

	jQuery.ajax({
		type: "POST",
		url: "/becomeclient/",
		dataType : 'html',
		success: function(html) {
			hideDialog();

			showCustomDialog(function(paramObj) {
				jQuery.ajax({
					type: "POST",
					url: "/becomeclient/try",
					data: jQuery('#add-client-form').serialize(),
					dataType : 'json',
					success: function(res) {
						if (res.status == true) {
							showMessageDialog('Your information will be approved shortly and you will receive all nessesary information via email.');
						} else {
							jQuery('#add-client-form *').removeClass('error');
							jQuery('#add-client-form .custom_error').html('');

							jQuery.each(res.data, function(key, value) {
								jQuery('#' + key).addClass('error');
								if (value != 'Required') {
									jQuery('#' + key + '_error').html(value);
								}
							});
						}
					}
				});
			}, html, {'width': 370, 'height': 380, 'title': 'New Client Registration'});
		}
	});
}

function checkSessionExpiration() {
	var interval = setInterval(function() {
		jQuery.ajax({
			type: "post",
			url: "/session/check",
			dataType : 'json',
			success: function(res) {
				if (res.status == false) {
					var buttons = {
						'Ok' : function() {
							globalParams.dialog = jQuery(this);
							globalFunc(globalParams);
						}
					}
					showCustomDialog(
						function() {
							redirect("/");
						},
						'<div style="text-align:center;padding: 5px;">Due to non activity you have been logged out</div>',
						{'width': 370, 'height': 180, 'title': 'Session Expired', 'buttons': buttons}
					)

					clearInterval(interval);
				}
			}
		});
	}, 10000);
}