	function isValidEmailAddress(emailAddress) {
		var val = $(emailAddress).val();
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		
		if (pattern.test(val) == false) {
			$(emailAddress).css({'background-color':'#ffd3ba'});
			return false;
		}
		else {
			$(emailAddress).css({'background-color':'#ffffff'});
			return true;
		}
	} 

/*
if (! ('console' in window) || !('firebug' in console)) {
    var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
    window.console = {};
    for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};
}

if (window.console && window.console.firebug) {
	// firebug found!
	alert("Firebug found! \n\nPlease disable Firebug in order to continue using this site. For now you will be redirected to an alternate page. \n\nSincerely,\nSonic Team");
	window.location = dot+"disable_firebug";
}
*/

$(document).ready(function () { 
	
	$("iframe").addClass("frame");

	var password_reset = 0 ;
	
	$('a').attr("onfocus","if(this.blur)this.blur()");
	
	var elem = $(window.location.hash.replace('_', ''));
    if(elem.length >= 1) {
         //$.scrollTo(elem.left, (elem.offset().top - 126));
         $('html,body').animate({scrollTop: (elem.offset().top - 118)},'slow');
        // alert('done');
    }

var screenlock = 1;

if (!$.browser.msie) {	
/*
	window.onblur = function() {
		if (screenlock == 1) {
		
			$('#body').fadeTo(10, 0.00, function(){
			//$('.copyProtect').addClass("current");
			
			//if (stoplock == 1) {
				$('.copyProtect').show().fadeTo("fast", 1);
			//}
			
			});
		}

	};
*/
	window.onfocus = function() {
		
		$('#body').fadeTo("fast", 1, function(){
			//$('.copyProtect').removeClass("current");
			$('.copyProtect').hide();
			//screenlock = 1;
		});

	}; 
}

/*
	
	$(".frame, .fb_ltr, iframe, #facebook").click(function() {
		screenlock = 0;
	});
	$(".frame, .fb_ltr, iframe, #facebook").mouseover(function() {
		screenlock = 0;
	});
	$(".frame, .fb_ltr, iframe, #facebook").mouseenter(function() {
		screenlock = 0;
	});
	$(".frame, .fb_ltr, iframe, #facebook").mousedown(function() {
		screenlock = 0;
	});
	
	$(".frame, .fb_ltr, iframe, #facebook").mouseout(function() {
		screenlock = 1;
	});
	$(".frame, .fb_ltr, iframe, #facebook").mouseleave(function() {
		screenlock = 1;
	});
	$(".frame, .fb_ltr, iframe, #facebook").mouseup(function() {
		screenlock = 1;
		//alert('up');
	});
	$(".frame, .fb_ltr, iframe, #facebook").blur(function() {
		screenlock = 1;
	});
	
	
	$(".frame, .fb_ltr, iframe, #facebook").livequery("click",function() {
		screenlock = 0;
	});
	$(".frame, .fb_ltr, iframe, #facebook").livequery("mouseover",function() {
		screenlock = 0;
	});
	$(".frame, .fb_ltr, iframe, #facebook").livequery("mouseenter",function() {
		screenlock = 0;
	});
	$(".frame, .fb_ltr, iframe, #facebook").livequery("mousedown",function() {
		screenlock = 0;
	});
	
	$(".frame, .fb_ltr, iframe, #facebook").livequery("mouseout",function() {
		screenlock = 1;
	});
	$(".frame, .fb_ltr, iframe, #facebook").livequery("mouseleave",function() {
		screenlock = 1;
	});
	$(".frame, .fb_ltr, iframe, #facebook").livequery("mouseup",function() {
		screenlock = 1;
		//alert('up');
	});
	$(".frame, .fb_ltr, iframe, #facebook").livequery("blur",function() {
		screenlock = 1;
	});
*/
	
	
	$(this).bind("contextmenu", function(e) {
		e.preventDefault();
    });
    
    $('img').click(function(){return false;});
    $('img').mousedown(function(){return false;});
    $('.postContent:not(textarea, input)').disableTextSelect();
    
var input_selected = 0;
if ($.browser.name !== "msie") {   
    $(document).keydown(function (e) {
		var keyIs = e.which;
		if ((keyIs == 91 || keyIs == 224) && input_selected == 0) {
			$('#body').hide();
			$('.copyProtect').show().fadeTo(0, 1);
		}
		
	}).keyup(function (e) {
		$('#body').show();
		$('.copyProtect').hide();
	});
	
	$('input,textarea').focus(function(){
		input_selected = 1;
	});
	
	$('input,textarea').blur(function(){
		input_selected = 0;
	});
	
	$('input,textarea').click(function(){
		input_selected = 1;
	});
}


	$('#mysonic .slide').click(function(){
		var id = $('#' + this.rel);		
		if (id.length >= 1) {
			$('html,body').animate({scrollTop: (id.offset().top - 118)},'slow');
			return false;
		}
	});

	$('.slide').click(function(){
		if ($("body#home").length > 0) {
			var id = $('#' + this.rel);		
			if (id.length >= 1) {
				$('html,body').animate({scrollTop: (id.offset().top - 118)},'slow');
				return false;
			}
		}
		else {
			return true;
		}
	});
	
/*
	$("#active_account_email").click(function(){
		$('#dialogHolderPayPal,#oinionPayPal').fadeIn(200); centerItB();
		//return false;
	});
*/
	
	function saveDonation() {
		var data = $('#donate').serialize()+"&p=donate_process&token="+token;
		$.post(pPg, data, function(html) {
			var message = html.split("::");
			var error = message[1];
			var sonic_order_number = message[1];
			var sonic_token = message[2];
			message = message[0];
			
			if (message == "success") {
				// set order_number
				$("#paypal_donate").find("#sonic_custom").val(sonic_token);
				$("#paypal_donate").find("#sonic_invoice").val(sonic_order_number);
				
				
				var sonic_donation_amount_xvf = $('#donate #amount').val();
				$("#sonic_donation_amount_xvf").val(sonic_donation_amount_xvf);
				
				if ($("#sonic_donation_amount_xvf").val()) {
					$('#dialogHolderPayPal,#oinionPayPal').fadeIn(200); centerItB();
					$("#paypal_donate").submit();
				}
				else {
					alert("There was an error transferring you to PayPal to process your donation. Please Try again.");
					refresh();
				}
			}
			if (message == "failed") {
				$('#donate').find(".error").html(error);
			}
		});
	}
	function saveDonationLogin() {
		var data = $('#donate').serialize()+"&p=donate_login_process&token="+token;
		$.post(pPg, data, function(html) {
			var message = html.split("::");
			var error = message[1];
			var sonic_order_number = message[1];
			var sonic_token = message[2];
			message = message[0];
			
			if (message == "success") {
				// set order_number
				$("#paypal_donate").find("#sonic_custom").val(sonic_token);
				$("#paypal_donate").find("#sonic_invoice").val(sonic_order_number);
				
				var sonic_donation_amount_xvf = $('#donate #amount').val();
				$("#sonic_donation_amount_xvf").val(sonic_donation_amount_xvf);
				
				if ($("#sonic_donation_amount_xvf").val()) {
					$('#dialogHolderPayPal,#oinionPayPal').fadeIn(200); centerItB();
					$("#paypal_donate").submit();
				}
				else {
					alert("There was an error transferring you to PayPal to process your donation. Please Try again.");
					refresh();
				}
			}
			if (message == "failed") {
				$('#donate').find(".error").html(error);
			}
		});
	}
	function signUp() {
		var data = $('#donate').serialize()+"&p=register_process&token="+token;
		$.post(pPg, data, function(html) {
			var message = html.split("::");
			var error = message[1];
			var sonic_order_number = message[1];
			var sonic_token = message[2];
			message = message[0];
			
			if (message == "success") {
				// set order_number
				$("#paypal_donate").find("#sonic_custom").val(sonic_token);
				$("#paypal_donate").find("#sonic_invoice").val(sonic_order_number);
				
				var sonic_donation_amount_xvf = $('#donate #amount').val();
				$("#sonic_donation_amount_xvf").val(sonic_donation_amount_xvf);
				
				if ($("#sonic_donation_amount_xvf").val()) {
					$('#dialogHolderPayPal,#oinionPayPal').fadeIn(200); centerItB();
					alert("Please note in order to active your account you need to complete your donation. Thank you for your $"+sonic_donation_amount_xvf+" donation.\n\nPlease do not refresh or do anything that may effect the loading of PayPal as you are being redirected to PayPal and it may take a few moments.\n\nFurthermore, an email has been sent to you containing a link for you to activate your account in the future if you decide not to do so now.");
					$("#paypal_donate").submit();
				}
				else {
					alert("There was an error transferring you to PayPal to process your donation. Please Try again.");
					refresh();
				}
			}
			if (message == "failed") {
				$('#donate').find(".error").html(error);
			}
		});
	};
	
	function activeAccount() {
		var original_amount_t = $('#original_amount').val();
		var donation_token = $('#sonic_custom_b').val();
		var amount = $('#sonic_donation_amount_xvf_b').val()
		var data = "p=active_account_change_amount&token="+token+"&t="+donation_token+"&oa="+original_amount_t+"&a="+amount; //$('#activeAccount').serialize()+
		//alert(data);
		//return false;
		$.post(pPg, data, function(html) {
			var message = html.split("::");
			//alert(html);
			if (message[0] == "success") {
				$('#dialogHolderPayPal,#oinionPayPal').fadeIn(200); centerItB();
				$('#activeAccount').submit();
				//alert("s");
				//return true;
			}
			if (message[0] == "failed") {
				$('#activeAccount').find(".error").html(message[1]);
				//alert("");
			}
		});
	};
	
	function logIn() {
		var data = $('#login_form').serialize()+"&p=login_process&token="+token;

		$.post(pPg, data, function(html) {
			var message = html.split("::");
			var error = message[1];
			message = message[0];
			
			if (message == "success") {
				if (password_reset == 1) { 
					window.location = "../";
				}
				else {
					window.location = "./";
					//refresh();
					window.location = window.location;
				}
			}
			if (message == "failed") {
				$('#login_form').find(".error").html(error);
			}
		});
	};
	function forgot_password() {
		var data = $('#forgot_password').serialize()+"&p=forgot_password_process&token="+token;
		$.post(pPg, data, function(html) {
			var message = html.split("::");
			var error = message[1];
			var sonic_order_number = message[1];
			var sonic_token = message[2];
			message = message[0];
			
			if (message == "success") {
				// set order_number
				$("#forgot_password").find("#forgot_pass_mess").html("An email has been sent. Please check your email.");
			}
			if (message == "failed") {
				$('#forgot_password').find("#forgot_pass_mess").html("An email has been sent. Please check your email.");
			}
		});
	};
	function reset_password() {
		var data = $('#reset_password').serialize()+"&p=reset_password_proccess&token="+token;
		$.post(pPg, data, function(html) {
			var message = html.split("::");
			var error = message[1];
			var sonic_order_number = message[1];
			var sonic_token = message[2];
			message = message[0];
			
			var username = $("#reset_password_username").val();
			
			if (message == "success") {
				// set order_number
				$("#reset_password").find("#forgot_pass_mess").html("<center>Your Password has been reset. Please try and login.</center>");
				$("#dialogHolder .error").html("Your Password has been reset. Please try and login.");
				$('#dialogHolder,#oinion').fadeIn(200);
				centerIt();
				password_reset = 1;
				$('#login_form #usernameB').val(username);
				$('#login_form #passwordB').focus();
				
			}
			if (message == "failed") {
				$('#reset_password .error').html(error);
			}
		});
	};
  
	$('#accountNo').click(function(){
  		$('.accountNo').show();
  	});
  	$('#accountYes').click(function(){
  		$('.accountNo').hide();
  	});
  
  	function checkMin(min,id) {
  		var val = $(id).val();
  		var minAmount = (min);
  		
  		if (val < minAmount) {
			$(id).val(min);
			$(id).css({'background-color':'#ffd3ba'});
			return false;
		}
		else {
			$(id).css({'background-color':'#ffffff'});
			return true;
		}
  	};
  	
  	  function checkTrue(id) {  		
  		if ($(id).val().length == 0) {
			$(id).css({'background-color':'#ffd3ba'});
			return false;
		}
		else {
			$(id).css({'background-color':'#ffffff'});
			return true;
		}
  	};
  
	$('#donate .send').click(function() {
		if (loggedIn == 0) {
			if ($("#donate").find("#accountNo").is(':checked')) {
				// check if required is met
				if (isValidEmailAddress('#email_address') && isValidEmailAddress('#confirm_email_address') && checkMin(1.00,'#amount') && checkTrue('#confirm_password') && checkTrue('#password') && checkTrue('#username')) {
					signUp(); //$(this).serialize()
				}
			}
			else {
				// check if required is met
				if (checkMin(1.00,'#amount') && checkTrue('#password') && checkTrue('#username')) {
					saveDonationLogin();
				}
			}
		}
		else {
			if (checkMin(1.00,'#amount') && checkTrue('#password') && checkTrue('#username')) {
				saveDonation();
			}
		}
	
		return false;
	});
	
	$('#donate').submit(function() {
		if (loggedIn == 0) {
			if ($("#donate").find("#accountNo").is(':checked')) {
				// check if required is met
				if (isValidEmailAddress('#email_address') && isValidEmailAddress('#confirm_email_address') && checkMin(1.00,'#amount') && checkTrue('#confirm_password') && checkTrue('#password') && checkTrue('#username')) {
					signUp(); //$(this).serialize()
				}
			}
			else {
				// check if required is met
				if (checkMin(1.00,'#amount') && checkTrue('#password') && checkTrue('#username')) {
					saveDonationLogin();
				}
			}
		}
		else {
			if (checkMin(1.00,'#amount') && checkTrue('#password') && checkTrue('#username')) {
				saveDonation();
			}
		}
	
		return false;
		
	});
	////////  Activate account
	$('#activeAccount #active_account_email').click(function() {
		if (checkMin(1.00,'#sonic_donation_amount_xvf_b')) {
			activeAccount();
			//alert("hi");
		}
		
		return false;
	});
	
/*
	$('#activeAccount').submit(function() {
		if (checkMin(1.00,'#sonic_donation_amount_xvf_b')) {
			//activeAccount();
			//alert("by");
		}
	
		return false;
	});
*/
	
//forgot_password	  
	$('#forgot_password .send').click(function() {
		// check if required is met
		if (isValidEmailAddress('#email_address_f')) {
			forgot_password();
		}
		return false;
	});
	
	$('#forgot_password').submit(function() {
		if (isValidEmailAddress('#email_address_f')) {
			forgot_password();
		}
		return false;
	});	
// END - forgot_password	

//reset_password	  
	$('#reset_password .send').click(function() {
		// check if required is met
		if (checkTrue('#password_f_a') && checkTrue('#password_f_b')) {
			reset_password();
		}
		return false;
	});
	
	$('#reset_password').submit(function() {
		if (checkTrue('#password_f_a') && checkTrue('#password_f_b')) {
			reset_password();
		}
		return false;
	});	
// END - reset_password	
	
	$('#login_form .send').click(function() {
		if (loggedIn == 0) {
			// check if required is met
			if (checkTrue('#passwordB') && checkTrue('#usernameB')) {
				logIn();
			}
		}
		else {
		}
	
		return false;
	});
	
	$('#login_form').submit(function() {
		if (loggedIn == 0) {
			// check if required is met
			if (checkTrue('#passwordB') && checkTrue('#usernameB')) {
				logIn(); //$(this).serialize()
			}
		}
		else {
		}
	
		return false;
		
	});
	
  
var el = $("#dialog");
var centerIt = function () {
    if (!$("#dialog")) {
        return;
    }
    var moveIt = function () {
        var winWidth = $(window).width();
        var winHeight = $(window).height();
        
        var topP = ((winHeight / 2) - ($("#dialog").height() / 2));
        
        if (topP < ($("#dialog").height() / 5)) {
        	topP = ($("#dialog").height() / 5);
        }
        
        
        $("#dialog").css("position","absolute").css("left", ((winWidth / 2) - ($("#dialog").width() / 2)) + "px").css("top", topP + "px");
    }; 
    $(window).resize(moveIt);
    moveIt();
};

var centerItB = function () {
    if (!$("#dialogPayPal")) {
        return;
    }
    var moveItB = function () {
        var winWidth = $(window).width();
        var winHeight = $(window).height();
        
        var topP = ((winHeight / 2) - ($("#dialogPayPal").height() / 2));
        
        if (topP < ($("#dialogPayPal").height() / 5)) {
        	topP = ($("#dialogPayPal").height() / 5);
        }
        
        
        $("#dialogPayPal").css("position","absolute").css("left", ((winWidth / 2) - ($("#dialogPayPal").width() / 2)) + "px").css("top", topP + "px");
    }; 
    $(window).resize(moveItB);
    moveItB();
};
	centerIt();
	centerItB();
  
  $('.login').click(function(){
	$('#dialogHolder,#oinion').fadeIn(200);
	centerIt();
	return false;
  });
  
  $('.dialogClose').click(function(){
	$('#dialogHolder,#oinion').fadeOut(200);
	return false;
  });
  
  
  // display login on PayPal return
	if (paypal_return == 1) {
		$('#dialogHolder,#oinion').fadeIn(200);
		centerIt();
	}

	$(".paypal_logo").click(function(){
		window.open('https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');
		return false;
	});

/*
	$('body#home #sonicTeaser,body#home #sonicTeaserWrap, body#home #sonicTeaserOnion').show();
	$('body#home').parents('html').css({'overflow':'hidden'});
  	$('body#home').children("#body").fadeOut(1750);
  	
  	$('#enterSite').click(function(){
  		$('body#home #sonicTeaser,body#home #sonicTeaserWrap, body#home #sonicTeaserOnion').fadeOut(1200,function(){ $(this).remove(); });
  		
  		$('body#home').children("#body").fadeIn(300,function() {
  			$('body#home').parents('html').css({'overflow':'auto'});
  		});
  	});
*/
  	$('#watchTeaser').click(function(){
  		$('body#home #sonicTeaser iframe').fadeIn(400);
  	});
  	
  	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (agentID) {
 
        // do something special
        $('#watchTeaser').hide();
        $('body#home #sonicTeaser iframe').fadeIn(900);
 
	}
	
	var agentIDB = deviceAgent.match(/(ipad)/);
	if (agentIDB) {
 
        // do something special
        //$('body#home #sonicTeaser iframe').css({'top':'150px'});
 		
	}
  	
  	
});
