/** global functions **/

function showFeedback(msgID, callback) {
	$('#' + msgID).fadeIn(300);
	setTimeout('hideFeedback("' + msgID + '", ' + callback + ');', 4000); // 4s delay
	$('.button').blur();
}
function hideFeedback(msgID, callback) {
	$('#' + msgID).fadeOut(400, callback);
}

function redirect(uri) {
	var base_href = $('base').attr('href');
	if (base_href.substr(base_href.length-1) == '/' && uri.charAt(0) == '/') {
		uri = uri.substr(1);
	}

	uri = base_href + uri;

	window.location = uri;
}


/** lightbox starts here **/
$(document).keyup(function(event) {
	if (event.keyCode == 27) { // ESC
		// check if the video lightbox is open
		if ($('#lightbox-player:visible').length == 0) {
			closeLightbox();
		}
	}
});

//need to get around the regular toggleLightbox having a return value of 'false'
function toggleNewsletter() {
	toggleLightbox('lb-newsletter');
}

function toggleLightbox(id) {
	
	if(id == 'lb-newsletter') {
		Tracking.trackNewsletterPageView();
	}
	
	$('div.lightbox-wrapper').hide();
	var myLB = $('#' + id).parents('div.lightbox-wrapper');

	myLB.find('form input[type=text], form input[type=password]').val(''); // clear the form before showing it (only the text fields)
	myLB.find('form input[type=checkbox]').attr('checked', false);
	myLB.find('form input[type=radio]').attr('checked', false);
	myLB.find('form select').val('');
	myLB.find('form :input').each(function() {
		if ($(this).attr('id') != '') {
			displayError($(this), false);
		}
	});
	$('a.submit').removeClass('disable');
	$('a.save').removeClass('disable');

	myLB.show();

	var windowWidth  = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight; // the height of the browser window


	if (myLB.height() > windowHeight) { // if the lightbox is longer than the window, then start lightbox at the top of the page plus margin
		var lbtop = 20 + $().scrollTop();
	} else { // centre the entire window based on lb height
		var lbtop =  windowHeight/2 - myLB.height()/2 + $().scrollTop();
	}




	if (lbtop + myLB.height() > $('body').height()) {
		$('body').height(lbtop + myLB.height());
	}
	$('#blanket').show();
	$('#blanket').height($('body').height());
	$('#blanket').width($('html').width()); // mostly for ie6

	// to compensate for the body { position: relative }, which fixes the resizing issue
	if ($.browser.msie) {
		var diff = $('html').width() - $('body').width();
		var offset = Math.round(diff / 2) * -1;
		$('#blanket').css('left', offset);
	} else {
		var offset = 0;
	}

	// centering
	$('.lightbox-wrapper').css({  
		'top':  lbtop,
		'left': windowWidth/2  - myLB.width()/2 + offset,
		'z-index': 100010
	});
	
	return false;
}
$('#blanket').click(function() {
	// kill the player
	if ($('#lb-register:visible').length == 0) {
		closeLightbox();
	}
});
function closeLightbox(now) {
	if ($.browser.msie || (now && now != 2)){ // kill everything now
		$('#blanket').hide();
		$('div.lightbox-wrapper').hide();
	} else if (now && now == 2) { // don't kill the blanket
		$('div.lightbox-wrapper').hide();
	} else { // fade out both blanket and content
		$('#blanket').fadeOut();
		$('div.lightbox-wrapper').fadeOut();
	}

	if ($('#flashfeature').length == 1) { // we have a feature rotator on this page, unpause it
		var movie = document.getElementById('flashfeature');
		if (typeof(movie) == 'object' || typeof(movie) == 'function') { // object for ie, function everyone else
			movie.startFeature();
		}
	}

	if (typeof(playerOpen) != 'undefined' && playerOpen == true) { // lightbox player
		var movie = document.getElementById('lightboxplayer');
		movie.stopVideo();
		playerOpen = false;
	}

	var off = $('#footer-nav').offset();
	$('body').height(off.top + 150);

	return false;
}

function showDetails(showID, networkID) {
	$.get('services/show.php', { 'id' : showID, 'network' : networkID } , function(data) {
		$('#lb-show-details-container').html(data);

		toggleLightbox('lb-show-details-container');

	}, 'html');
}


function getFocus() {
	this.window.focus();
}

function setVolume(vol) {
	$.cookie('vol', vol, { expires: 30 , path: '/' });
}
function getVolume() {
	return $.cookie('vol');
}

var playerOpen = false;

function showPlayer(id, file, embed, title, promoID, network) {
	if (!file) {
		return false;
	}

	playerOpen = true;

	//if ($('div#lightboxplayer').length == 1) {
		var flashvars = {
				'host' : $('base').attr('href'),
				'webPath' : window.location.pathname.substring(1), // movies/, series/, etc..
				'movieToLoad' : file,
				'hash' : embed,
				'volume' : getVolume(),
				'movieID' : id,
				'promoID'	: promoID,
				'useLBShare' : 'true',
				'title'		: escape(title),
				'network'	: network,
				'autoPlay'	: '1'
		};
			
		var params = {
				'wmode'				: 'opaque',
				'bgcolor'			: '#ffffff',
				'allowScriptAccess'	: 'always',
				'pluginspage'		: 'http://www.adobe.com/go/getflashplayer',
				'align'				: 'middle',
				'play'				: 'true',
				'loop'				: 'true',
				'scale'				: 'showall',
				'allowFullScreen'	: 'true'
		};
		
		swfobject.embedSWF("static/swf/videoPlayerSimple.swf", "lightboxplayer", "614", "347", "10.0.0","expressInstall.swf", flashvars, params);
	//}

	toggleLightbox('lightbox-player');

	var movie = document.getElementById('lightboxplayer');
	if (typeof(movie.playVideo) == 'function') {
		movie.playVideo();
	}

	return false;
}

function showVideo() {
	if ($('div#lightboxplayer').length == 0) {
		toggleLightbox('lightbox-player');
		// start playing again
		//var player = document.getElementById('lightboxplayer');
		//player.playVideo();
	} else {
		closeLightbox();
	}
}

function shareVideo() {
	$('div.lightbox-wrapper').hide();

	var id = videoID;

	$.get('services/share_video.php', { 'id' : id } , function(data) {
		$('#lb-share-video-container').html(data);

		toggleLightbox('lb-share-video-container');

	}, 'html');

	return false;
}
function embedVideo() {
	closeLightbox(2);

	var id = videoID;

	$('#embed_video').val('<object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always"/><param name="movie" value="http://www.themovienetwork.ca/video.swf?id='+id+'" /><embed src="http://www.themovienetwork.ca/video.swf?id='+id+'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object>');

	toggleLightbox('lb-video-embed');

	$('#embed_video').select();
	return false;
}

/** lightbox ends here **/

/** events go down here **/

// search bar
$(function() {
	// search button mouseover/mouseout
	$('#search-magnifier').hover(function() {
		$(this).attr('src', 'static/images/SearchMagGlass2.gif');
	}, function() {
		$(this).attr('src', 'static/images/SearchMagGlass.gif');
	});


	// dropdown toggle
	$('.search-nav-dd').click(function() {
		$(this).find('ul').toggle();
	});

	// dropdown mouseout
	$('.search-nav-dd').hover(
		function() { },
		function() {
			$(this).find('ul').hide();
		}
	);

	// dropdown item click
	$('.search-nav-dd li input').click(function() {
		return false;
	});
	$('.search-nav-dd li a').click(function() {
		$(this).parent().parent().parent().find('li.active').removeClass('active');
		$(this).parent().parent().parent().parent().find('span').text($(this).text());
		$(this).addClass('active');

		return true;
	});
	$('.search-nav-dd li').click(function() {
		$(this).parent().find('li.active').removeClass('active');
		$(this).parent().parent().find('span').text($(this).text());
		$(this).addClass('active');

		redirect($(this).find('a').attr('href'));

		return true;
	});


	// search input focus
	$('#search-input').focus(function() {
		if ($(this).val() == 'Search') {
			$(this).val('');
		}
	});

	// search input blur
	$('#search-input').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Search');
		}
	});

	// search button submit
	$('#search-form').submit(function() {
		var text  = $('#search-input').val();
		//var genre = $('input[name=genre]:checked').val();
		//var show  = $('input[name=show]:checked').val();

		var url = 'search';
		if (text && text != 'Search') {
			url += '/text:' + text; //text.replace(/\//, '%2F'); // %2F = encodeURIComponent('/')
		}

		/*
		if (genre) {
			url += '/genre:' + genre;
		}

		if (show) {
			url += '/show:' + show;
		}
		*/

		redirect(url);

		return false;
	});

	// timezone / tz links
	$('#tz-list li').click(function() {
		var tz = $(this).attr('id').substr(3);
		$.cookie('tz', $(this).attr('id').substr(3), { expires: 60, path: '/' });

		$('#tz-list').attr('class', $(this).attr('id'));

		if (tz == 'et') {
			$('.tz_at').hide();
			$('.tz_nt').hide();
			$('.tz_et').show();
			$('body').append('<style type="text/css">span.tz_at, span.tz_nt { display: none } span.tz_et { display: inline }</style>');
		} else if (tz == 'at') {
			$('.tz_at').show();
			$('.tz_nt').hide();
			$('.tz_et').hide();
			$('body').append('<style type="text/css">span.tz_et, span.tz_nt { display: none } span.tz_at { display: inline }</style>');
		} else {
			$('.tz_at').hide();
			$('.tz_nt').show();
			$('.tz_et').hide();
			$('body').append('<style type="text/css">span.tz_et, span.tz_at { display: none } span.tz_nt { display: inline }</style>');
		}

		if ($('#tmnflashschedule').length > 0) {

			var movie = swfobject.getObjectById('tmnflashschedule'); //document.getElementById('tmnflashschedule');
			movie.doTimeZoneOffset(tz);

			var movie = swfobject.getObjectById('mpixflashschedule'); //document.getElementById('mpixflashschedule');
			movie.doTimeZoneOffset(tz);
		}
	});
});


// promo rotators
$(function() {
	$('.promo-rotator ul li a').hover(function() {
		if ($(this).attr('rel') != '') {
			$(this).parent().siblings().find('a.active').removeClass('active');
			$(this).closest('.promo-rotator').find('img').attr('src', $(this).attr('rel'));
			$(this).parent().parent().parent().find('a:first').attr('href', $(this).attr('href'));

			if (!$(this).hasClass('single')) {
				$(this).addClass('active');
			}
		}
	}, function() {});

	$('.promo-rotator ul li a').focus(function() {
		$(this).parent().siblings().find('a.active').removeClass('active');
		$(this).closest('.promo-rotator').find('img').attr('src', $(this).attr('rel'));
		$(this).parent().parent().parent().find('a:first').attr('href', $(this).attr('href'));

		if (!$(this).hasClass('single')) {
			$(this).addClass('active');
		}
	}, function() {});

});


function showLoginForm() {
	toggleLightbox('lb-login'); 
	$('#login_email').focus(); 
	Tracking.trackLoginForm();	
}

function showRegisterForm() {
	toggleLightbox('lb-register'); 
	$('#reg_first_name').focus(); 
	Tracking.trackRegisterPage();
}


/** pagination **/

function setHash(string) {
	window.location.hash = string;
}

/****** generic pagination starts here ****/
var pagination_id_prefix = "pg";
var num_pages = 1; // simple default value
function updatePagination(page, maxPages ) {
	var next = $('a#pagination-right');
	var prev = $('a#pagination-left');
	var pageNums = $('ul#pagination-pages');

	if (pageNums.length == 0) {
		return true;
	}


	//setHash(page);

	var html = '';
	
	maxPages = parseInt(maxPages);
	page = parseInt(page);
	
	//DO THE PAGE NAV BUTTONS
	
	if( page == 1 && maxPages <= 1 ) {
		prev.addClass('disabled');
		next.addClass('disabled');
	} else if( page == 1 ) {
		prev.addClass('disabled');
		next.removeClass('disabled');
	} else if( page == maxPages ) {
		prev.removeClass('disabled');
		next.addClass('disabled');
	} else {
		prev.removeClass('disabled');
		next.removeClass('disabled');
	}
	
	//DO THE PAGE NUMBRERS

	if ( maxPages < 10 ) { 	// there are fewer total pages than 6 (5 in a row)
		for ( i = 1; i < maxPages; i++ ) {
			html += '<li><a href="#" id="'+pagination_id_prefix+i+'">'+i+'</a></li>';
		}
		html += '<li><a href="#" id="'+pagination_id_prefix+maxPages+'" class="last">'+maxPages+'</a></li>';
	} else if ( page < 4) { 	//if page is less than 5 but there are more than 6 total pages
		for( i = 1; i < 4; i++ ) {
			html += '<li><a href="#" id="'+pagination_id_prefix+i+'">'+i+'</a></li>';
		}
		html += '<li><a href="#" id="'+pagination_id_prefix+4+'" class="last">'+4+'</a></li>';
		html += '<li>&hellip;</li>';
		html += '<li><a href="#" rel="'+maxPages+'" id="'+pagination_id_prefix+maxPages+'" class="last">'+maxPages+'</a></li>';
	} else if ( (maxPages - page) < 5 ) {
		html = '<li><a href="#" id="'+pagination_id_prefix+'1" class="last">1</a></li>';
		html += '<li>&hellip;</li>';

		for( i = maxPages - 5; i < maxPages; i++ ) {
			html += '<li><a href="#" id="'+pagination_id_prefix+i+'">'+i+'</a></li>';
		}
		html += '<li><a href="#" id="'+pagination_id_prefix+maxPages+'" class="last">'+maxPages+'</a></li>';
	} else {
		html = '<li><a href="#" id="'+pagination_id_prefix+'1" class="last">1</a></li>';
		html += '<li>&hellip;</li>';

		for( i = page - 2; i < page + 2; i++ ) {
			html += '<li><a href="#" id="'+pagination_id_prefix+i+'">'+i+'</a></li>';
		}
		html += '<li><a href="#" id="'+pagination_id_prefix+(page+2)+'" class="last">'+(page+2)+'</a></li>';
	
		html += '<li>&hellip;</li>';
		html += '<li><a href="#" id="'+pagination_id_prefix+maxPages+'" class="last">'+maxPages+'</a></li>';
	}
	
	pageNums.html(html);
	pageNums.show();
}

function initPagination() {
	if ($("#pagination-pages li").length > 0) {
		if (window.location.hash) {
			var page_num = window.location.hash.substr(1);

			var num_rows = $('.pagination-content .pagination-item').length;
			var first_visible = $('.pagination-item').not('.hidden');
			var current_index = $('.pagination-item').index(first_visible);

			if ($(".pagination-content").hasClass("items-per-page-8")) {
				var num_per_page = 8;
			} else if ($(".pagination-content").hasClass("items-per-page-10")) {
				var num_per_page = 10;
			} else if ($(".pagination-content").hasClass("items-per-page-12")) {
				var num_per_page = 12;
			}

			var num_pages = Math.ceil(num_rows / num_per_page);

			if (page_num > num_pages) {
				page_num = 1;
			}

			updatePagination(page_num , num_pages); // regenerate pagination

			current_index = (page_num - 1) * num_per_page;
			$(".pagination-content .pagination-item").addClass('hidden');
			$(".pagination-content .pagination-item").slice(current_index, current_index + num_per_page).removeClass('hidden');

			$("#" + pagination_id_prefix + page_num).addClass("active"); // set active on the new link
		} else {
			var page_num = 1;

			num_pages = $("#pagination-pages li").length;
			updatePagination(page_num, num_pages); // start pagination on first page

			$("#" + pagination_id_prefix + page_num).addClass("active"); // highlight first page
		}
	}
	$(".pagination-content").parent(".pagination-content-container").attr("style", "background: none");
	$(".pagination-content").show();
}

//$(document).ready(function () {
	initPagination();
//});

$("#pagination-pages a").live("click", function(){ // handle regular pagination link
	$(this).blur();

	var num_rows = $('.pagination-content .pagination-item').length;
	var first_visible = $('.pagination-item').not('.hidden');
	var current_index = $('.pagination-item').index(first_visible);

	if ($(".pagination-content").hasClass("items-per-page-8")) {
		var num_per_page = 8;
	} else if ($(".pagination-content").hasClass("items-per-page-10")) {
		var num_per_page = 10;
	} else if ($(".pagination-content").hasClass("items-per-page-12")) {
		var num_per_page = 12;
	}

	var num_pages = Math.ceil(num_rows / num_per_page);

	var page_num = $(this).attr('id').substr(pagination_id_prefix.length);

	updatePagination(page_num , num_pages); // regenerate pagination

	current_index = (page_num - 1) * num_per_page;
	$(".pagination-content .pagination-item").addClass('hidden');
	$(".pagination-content .pagination-item").slice(current_index, current_index + num_per_page).removeClass('hidden');

	$("#" + pagination_id_prefix + page_num).addClass("active"); // set active on the new link
	return false;
});
// deal with the prev/next buttons
$("#pagination-left").live("click", function() {
	if ($('ul#pagination-pages').length == 0) {
		return true;
	}

	if (!$(this).hasClass('disabled')) {
		$(this).blur();

		var num_rows = $('.pagination-content .pagination-item').length;
		var first_visible = $('.pagination-item').not('.hidden');
		var current_index = $('.pagination-item').index(first_visible);

		if ($(".pagination-content").hasClass("items-per-page-8")) {
			var num_per_page = 8;
		} else if ($(".pagination-content").hasClass("items-per-page-10")) {
			var num_per_page = 10;
		} else if ($(".pagination-content").hasClass("items-per-page-12")) {
			var num_per_page = 12;
		}

		var num_pages = Math.ceil(num_rows / num_per_page);
		current_index -= num_per_page;

		$(".pagination-content .pagination-item").addClass('hidden');
		$(".pagination-content .pagination-item").slice(current_index, current_index + num_per_page).removeClass('hidden');

		var page_num = (current_index + num_per_page) / num_per_page;

		updatePagination(page_num, num_pages); // regenerate pagination

		$("#" + pagination_id_prefix + page_num).addClass("active"); // set active on the new link
	}
	return false;
});
$('#pagination-right').live('click', function() {
	if ($('ul#pagination-pages').length == 0) {
		return true;
	}

	if (!$(this).hasClass('disabled')) {
		$(this).focus();

		var num_rows = $('.pagination-content .pagination-item').length;
		var first_visible = $('.pagination-item').not('.hidden');
		var current_index = $('.pagination-item').index(first_visible);

		if ($(".pagination-content").hasClass("items-per-page-8")) {
			var num_per_page = 8;
		} else if ($(".pagination-content").hasClass("items-per-page-10")) {
			var num_per_page = 10;
		} else if ($(".pagination-content").hasClass("items-per-page-12")) {
			var num_per_page = 12;
		}

		var num_pages = Math.ceil(num_rows / num_per_page);
		current_index += num_per_page;

		$('.pagination-item').addClass('hidden');
		$('.pagination-item').slice(current_index, current_index + num_per_page).removeClass('hidden');

		var page_num = (current_index + num_per_page) / num_per_page;

		updatePagination(page_num, num_pages); // regenerate pagination

		$("#" + pagination_id_prefix + page_num).addClass("active"); // set active on the new link
	}
	return false;
});
/****** generic pagination ends here ****/

/** reminder start **/

function isSignedIn() {
	return _is_signed_in;
}

// enable or disable a reminder
// enable = true to add reminder, false to remove reminder
// value = "{CHANNEL}-{TIME}-{SHOWID}"
// refresh = whether or not to reload the page (used when signing-in)
function addReminder(enable, value, refresh) {
	if (isSignedIn()) {
		var data = { 'id' : value , 'enable' : enable };

		$.post("services/reminder.php", data, function() {
			if (refresh) {
				window.location.reload(false);
			}
		});

		return true;
	} else {
		$(":input[name=postaction]").val('addReminder(' + enable + ', \'' + value + '\', true);');
		toggleLightbox('lb-login');
	}
	return false;
}

// the show details checkbox links
function addManualReminder(obj, id) {
	$(obj).blur();

	if ($(obj).hasClass('active')) {
		var enable = false;
	} else {
		var enable = true;
	}
	if (addReminder(enable, id, false)) {
		$(obj).toggleClass('active');
	}
}

function accountReminderReplace(old_reminder, new_reminder) {
	addReminder("false", old_reminder, false);
	addReminder("true", new_reminder, true);
}

/** reminder end **/

function displayError(elem, error, msg) {
	var dd = $(elem).parent("dd");

	dd.removeClass("loading");
	if (error) {
		dd.addClass("error");
		if (msg != null) {
			var span = dd.find("span.error");
			if (span.length) {
				span.html(msg);
			} else {
				dd.append('<span class="error">' + msg + '</span>');
			}

			dd.find("span.error").show();
			dd.find("span.info").hide();
		}
	} else {
		dd.find("span.error").hide();
		dd.find("span.info").show();
		dd.removeClass("error");
	}
}

function processFormRegister() {
	var error = false;

	// first name
	if ($.trim($('#reg_first_name').val()) == '') {
		displayError($('#reg_first_name'), true, 'First Name is required');
		error = true;
	} else {
		displayError($('#reg_first_name'), false);
	}

	// last name
	if ($.trim($('#reg_last_name').val()) == '') {
		displayError($('#reg_last_name'), true, 'Last Name is required');
		error = true;
	} else { 
		displayError($('#reg_last_name'), false);
	}

	// display name
	if ($.trim($('#reg_display_name').val()) == '') {
		displayError($('#reg_display_name'), true, 'Display Name is required');
		error = true;
	} else {
		var pattern = /^([a-zA-Z0-9_])+$/;

		if (!pattern.test($('#reg_display_name').val())) {
			displayError($('#reg_display_name'), true, 'Display Name is not valid');
			error = true;
		} else {
			displayError($('#reg_display_name'), false);
		}
	}

	// email
	if ($('#reg_email_confirm').length > 0) {
		if ($.trim($('#reg_email').val()) == '') {
			displayError($('#reg_email'), true, 'Email is required');
			displayError($('#reg_email_confirm'), true, 'Confirm Email is required');
			error = true;
		} else if ($('#reg_email').val() != $('#reg_email_confirm').val()) {
			displayError($('#reg_email'), true, 'Emails did not match');
			displayError($('#reg_email_confirm'), 'Emails did not match');
			error = true;
		} else {
			var pattern = /^([a-zA-Z0-9_\.\-+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,5})+$/;

			if (!pattern.test($('#reg_email').val())) {
				displayError($('#reg_email'), true, 'Email address is not valid');
				displayError($('#reg_email_confirm'), 'Email address is not valid');
				error = true;
			} else {
				displayError($('#reg_email'), false);
				displayError($('#reg_email_confirm'), false);
			}
		}
	} else {
		if ($.trim($('#reg_email').val()) == '') {
			displayError($('#reg_email'), true, 'Email is required');
			error = true;
		} else {
			var pattern = /^([a-zA-Z0-9_\.\-+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,5})+$/;

			if (!pattern.test($('#reg_email').val())) {
				displayError($('#reg_email'), true, 'Email address is not valid');
				error = true;
			} else {
				displayError($('#reg_email'), false);
			}
		}
	}

	// password
	if ($('#reg_password').length > 0) { // register
		if ($.trim($('#reg_password').val()) == '') {
			displayError($('#reg_password'), true, 'Password is required');
			displayError($('#reg_password_confirm'), true, 'Confirm Password is required');
			error = true;
		} else if ($('#reg_password').val() != $('#reg_password_confirm').val()) {
			displayError($('#reg_password'), true, 'Passwords did not match');
			displayError($('#reg_password_confirm'), true, 'Passwords did not match');
			error = true;
		} else {
			displayError($('#reg_password'), false);
			displayError($('#reg_password_confirm'), false);
		}
	}

	// province
	if ($('#reg_province').val() == ''){
		displayError($('#reg_province'), true, 'Province is required');
		error = true;
	} else {
		displayError($('#reg_province'), false);
	}

	// provider
	if ($('#reg_service_provider').val() == '') {
		displayError($('#reg_service_provider'), true, 'Service Provider is required');
		error = true;
	} else {
		displayError($('#reg_service_provider'), false);
	}


	// postal
	if ($('#reg_postal_code').val() == ''){
		displayError($('#reg_postal_code'), true, 'Postal Code is required');
		error = true;
	} else {
		displayError($('#reg_postal_code'), false);
	}

	// city
	if ($('#reg_city').val() == ''){
		displayError($('#reg_city'), true, 'City is required');
		error = true;
	} else {
		displayError($('#reg_city'), false);
	}

	// dob
	if ($('#reg_dob_year').val() == '' || $('#reg_dob_month').val() == '' || $('#reg_dob_day').val() == ''){
		displayError($('#reg_dob_year'), true, 'Date of Birth is required');
		error = true;
	} else {
		displayError($('#reg_dob_year'), false);
	}


	// gender
	if ($('#reg_gender').val() == ''){
		displayError($('#reg_gender'), true, 'Gender is required');
		error = true;
	} else {
		displayError($('#reg_gender'), false);
	}

	
	// subscriber
	if ($('#reg_subscriber').val() == '0'){
		displayError($('#reg_subscriber'), true, 'Are you a subscriber?');
		error = true;
	} else {
		displayError($('#reg_subscriber'), false);
	}

	if (!error) {
		var data = $('#register-form').serialize();

		$('.button').blur();

		$('#register-form a.save').addClass('disable');

		$.post($('#register-form').attr('action'), data, function(data) {

			if (data.result == 1) {
				var postaction = $('#register-form [name=postaction]').val();
				if (postaction != '-1') {
					_is_signed_in = true;
					eval(postaction);
					closeLightbox();
				} else if ($('#reg_password').length > 0) { // register
					redirect('account/?welcome');
				} else {
					showFeedback('feedback-acnt-update', closeLightbox);
					if ($('#original_email').length > 0) {			
						$('#original_email').val($('#reg_email').val());
					}
				}
			} else if (data.result == 0) {
				// error
				if (typeof(data.errors.email) != 'undefined') {
					displayError($('#reg_email'), true, 'Email address is already being used');
				}
				if (typeof(data.errors.display_name) != 'undefined') {
					displayError($('#reg_display_name'), true, 'Display name is already being used');
				}

			}

			$('#register-form a.save').removeClass('disable');
		}, "json");
	}

	return false;
}


function processFormRegisterCSR() {
	var error = false;

	// first name
	if ($.trim($('#reg_first_name').val()) == '') {
		displayError($('#reg_first_name'), true, 'First Name is required');
		error = true;
	} else {
		displayError($('#reg_first_name'), false);
	}

	// last name
	if ($.trim($('#reg_last_name').val()) == '') {
		displayError($('#reg_last_name'), true, 'Last Name is required');
		error = true;
	} else { 
		displayError($('#reg_last_name'), false);
	}

	// company
	if ($.trim($('#reg_company_name').val()) == '') {
		displayError($('#reg_company_name'), true, 'Employer Name is required');
		error = true;
	} else { 
		displayError($('#reg_company_name'), false);
	}


	// email
	if ($('#reg_email_confirm').length > 0) {
		if ($.trim($('#reg_email').val()) == '') {
			displayError($('#reg_email'), true, 'Email is required');
			displayError($('#reg_email_confirm'), true, 'Confirm Email is required');
			error = true;
		} else if ($('#reg_email').val() != $('#reg_email_confirm').val()) {
			displayError($('#reg_email'), true, 'Emails did not match');
			displayError($('#reg_email_confirm'), 'Emails did not match');
			error = true;
		} else {
			var pattern = /^([a-zA-Z0-9_\.\-+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,5})+$/;

			if (!pattern.test($('#reg_email').val())) {
				displayError($('#reg_email'), true, 'Email address is not valid');
				displayError($('#reg_email_confirm'), 'Email address is not valid');
				error = true;
			} else {
				displayError($('#reg_email'), false);
				displayError($('#reg_email_confirm'), false);
			}
		}
	} else {
		if ($.trim($('#reg_email').val()) == '') {
			displayError($('#reg_email'), true, 'Email is required');
			error = true;
		} else {
			var pattern = /^([a-zA-Z0-9_\.\-+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,5})+$/;

			if (!pattern.test($('#reg_email').val())) {
				displayError($('#reg_email'), true, 'Email address is not valid');
				error = true;
			} else {
				displayError($('#reg_email'), false);
			}
		}
	}

	

	// province
	if ($('#reg_province').val() == ''){
		displayError($('#reg_province'), true, 'Province is required');
		error = true;
	} else {
		displayError($('#reg_province'), false);
	}

	// provider
	if ($('#reg_service_provider').val() == '') {
		displayError($('#reg_service_provider'), true, 'Service Provider is required');
		error = true;
	} else {
		displayError($('#reg_service_provider'), false);
	}


	// postal
	if ($('#reg_postal_code').val() == ''){
		displayError($('#reg_postal_code'), true, 'Postal Code is required');
		error = true;
	} else {
		displayError($('#reg_postal_code'), false);
	}

	// city
	if ($('#reg_city').val() == ''){
		displayError($('#reg_city'), true, 'City is required');
		error = true;
	} else {
		displayError($('#reg_city'), false);
	}

	// dob
	if ($('#reg_dob_year').val() == '' || $('#reg_dob_month').val() == '' || $('#reg_dob_day').val() == ''){
		displayError($('#reg_dob_year'), true, 'Date of Birth is required');
		error = true;
	} else {
		displayError($('#reg_dob_year'), false);
	}


	// gender
	if ($('#reg_gender').val() == ''){
		displayError($('#reg_gender'), true, 'Gender is required');
		error = true;
	} else {
		displayError($('#reg_gender'), false);
	}

	
	// subscriber
	if ($('#reg_subscriber').val() == '0'){
		displayError($('#reg_subscriber'), true, 'Are you a subscriber?');
		error = true;
	} else {
		displayError($('#reg_subscriber'), false);
	}
	
		// subscriber
	if ($('#reg_affiliate').val() == '0'){
		displayError($('#reg_affiliate'), true, 'Required');
		error = true;
	} else {
		displayError($('#reg_affiliate'), false);
	}

	if (!error) {
		document.csrForm.submit();
	}

	return false;
}



/** providers **/
function getProviders(province, id, callback, showIDs) {
	if (!province) {
		$('#' + id).attr('disabled', true);
		$('#' + id).html('');
		return true;
	}

	$.get('services/service_providers.php', { prov : province}, function(data) {
		if (data.length > 0) {
			$('#' + id).replaceWith('<select name="provider" id="'+id+'"></select>');
			$('#' + id).attr('disabled', false);
			$('#' + id).html('<option>Choose Provider</option>');

			$.each(data, function(i, val) {
				if (showIDs) {
					$('#' + id).append('<option value="' + val.affiliate_id + '">' + val.name + '</option>');
				} else {
					$('#' + id).append('<option value="' + val.name + '">' + val.name + '</option>');
				}
			});
		} else {
			$('#' + id).replaceWith('<input type="text" name="provider" value="" id="'+id+'"/>');
		}

		if (callback) {
			callback();
		}
	}, 'json');
}

/** providers **/
function getProvidersCSR(province, id, callback, showIDs) {
	if (!province) {
		$('#' + id).attr('disabled', true);
		$('#' + id).html('');
		return true;
	}

	$.get('/services/service_providers.php', { prov : province}, function(data) {
		if (data.length > 0) {
			$('#' + id).replaceWith('<select name="provider" id="'+id+'"></select>');
			$('#' + id).attr('disabled', false);
			$('#' + id).html('<option>Choose Provider</option>');

			$.each(data, function(i, val) {
				if (showIDs) {
					$('#' + id).append('<option value="' + val.affiliate_id + '">' + val.name + '</option>');
				} else {
					$('#' + id).append('<option value="' + val.name + '">' + val.name + '</option>');
				}
			});
		} else {
			$('#' + id).replaceWith('<input type="text" name="provider" value="" id="'+id+'"/>');
		}

		if (callback) {
			callback();
		}
	}, 'json');
}

function syncSchedules(network, eventName, date) {
	if (network == 'TMN') {
		var name = 'mpixflashschedule';
	} else {
		var name = 'tmnflashschedule';
	}
	var movie = swfobject.getObjectById(name);
	//var movie = document.getElementById(name);
	movie.changeTime(eventName, date);
	return true;
}

