// Preload Images
img1 = new Image(16, 16);
img1.src="/dir/images/loading2.gif";

img2 = new Image(220, 19);
img2.src="/dir/images/loading2.gif";

// When DOM is ready
$(document).ready(function(){

function myOpen(dialog){
	dialog.overlay.fadeIn('slow', function () {
		dialog.data.hide();
		dialog.container.fadeIn('slow', function () {
			dialog.data.slideDown('slow');
		});
	});
}
function myClose(dialog){
	dialog.data.slideUp('slow', function () {
		dialog.container.fadeOut('fast', function () {
			dialog.overlay.fadeOut('fast', function () {
				$.modal.close();
			});
		});
	});
}
//******************************************************************************************
function MyProccess(name,message){

  // Launch MODAL BOX if the Login Link is clicked
  $("#" + name + "_link").live("click",function(e){
  e.preventDefault();
  
  $('#' + name + '_form').modal({onOpen:myOpen, onClose:myClose, overlayClose:true});
  });
  
  // When the form is submitted
  $("#" + name + "_status > form").submit(function(){  
  
  // Hide 'Submit' Button
  $('#' + name + '_submit').hide();
  
  // Show Gif Spinning Rotator
  $('#ajax_loading').show();
  
  // 'this' refers to the current submitted form
  var str = $(this).serialize();  
  
  // -- Start AJAX Call --
  
  $.ajax({
      type: "POST",
      url: "/auth/" + name + "_check.php?type=ajax",  // Send the info to this page
      data: str,
      success: function(msg){  
  
  $("#" + name + "_status").ajaxComplete(function(event, request, settings){  
  
   // Show 'Submit' Button
  $('#' + name + '_submit').show();
  
  // Hide Gif Spinning Rotator
  $('#ajax_loading').hide();  
  
   if(msg == 'OK') // LOGIN OK?
   {
   var name + _response = message;
  
  $('a.modalCloseImg').hide();  
  
  $('#simplemodal-container').css("width","500px");
  $('#simplemodal-container').css("height","120px");
  
   $(this).html(login_response); // Refers to 'status'
  
  // After 3 seconds redirect the
  setTimeout('go_to_private_page()', 4000);
   }
   else // ERROR?
   {
   var login_response = msg;
   $('#login_response').html(login_response);
   }  
  
   });  
  
   }  
  
    });  
  
  // -- End AJAX Call --
  
  return false;
  
  }); // end submit event


}//end function MyAjaxProccess
//******************************************************************************************



$("#forgot_link").live("click",function(e){
e.preventDefault();
$.ajax({
    url: "/auth/password_form.php",  // Send the login info to this page
    success: function(msg){
        $('#login_form').slideUp().html(msg).slideDown();
        }
    });
});

$("#backtologin").live("click",function(e){
e.preventDefault();
$.ajax({
    url: "/auth/login_form.php",  // Send the login info to this page
    success: function(msg){
        $('#login_form').slideUp().html(msg).slideDown();
        }
    });
});


//logout ajax call
$("#logout_link").live("click",function(e){
e.preventDefault();
$.ajax({
    url: "/auth/logout.php?type=ajax",  // Send the login info to this page
    success: function(msg){
        $('#auth_box').html(msg);
        }
    });

setTimeout('go_to_private_page()', 5000);
});


// LOGIN CODE
// Launch MODAL BOX if the Login Link is clicked
$("#login_link").live("click",function(e){
e.preventDefault();

$('#login_form').modal({onOpen:myOpen, onClose:myClose, overlayClose:true});
});

// When the form is submitted
$("#login_status > form").submit(function(){  

// Hide 'Submit' Button
$('#login_submit').hide();

// Show Gif Spinning Rotator
$('#ajax_loading').show();

// 'this' refers to the current submitted form
var str = $(this).serialize();  

// -- Start AJAX Call --

$.ajax({
    type: "POST",
    url: "/auth/login_check.php?type=ajax",  // Send the login info to this page
    data: str,
    success: function(msg){  

$("#login_status").ajaxComplete(function(event, request, settings){  

 // Show 'Submit' Button
$('#login_submit').show();

// Hide Gif Spinning Rotator
$('#ajax_loading').hide();  

 if(msg == 'OK') // LOGIN OK?
 {
 var login_response = '<div id="logged_in">' +
'<div style="margin:0 auto;width:350px">' +
'<div style="float:left;margin:0 30px">' +
'<img style="margin:0;" src="/dir/images/loading2.gif">' +
'</div>' +
'<div style="float:left;line-height:18px">'+
'Email and password match! <br /> Please wait while you\'re logged in...</div><div style="clear:both"></div></div>';

$('a.modalCloseImg').hide();  

$('#simplemodal-container').css("width","500px");
$('#simplemodal-container').css("height","120px");

 $(this).html(login_response); // Refers to 'status'

// After 3 seconds redirect the
setTimeout('go_to_private_page()', 4000);
 }
 else // ERROR?
 {
 var login_response = msg;
 $('#login_response').html(login_response);
 }  

 });  

 }  

  });  

// -- End AJAX Call --

return false;

}); // end submit event
//END LOGIN CODE

// REGISTRATION CODE
// Launch MODAL BOX if the Login Link is clicked
$("#registration_link").live("click",function(e){
e.preventDefault();

$('#registration_form').modal({onOpen:myOpen, onClose:myClose, overlayClose:true});
});

// When the form is submitted
$("#registration_status > form").submit(function(){  

// Hide 'Submit' Button
$('#registration_submit').hide();
$('#registration_form_meat').fadeOut('fast');

// Show Gif Spinning Rotator
$('#ajax_loading').show();

// 'this' refers to the current submitted form
var str = $(this).serialize();  

// -- Start AJAX Call --

$.ajax({
    type: "POST",
    url: "/auth/register_check.php?type=ajax",  // Send the login info to this page
    data: str,
    success: function(msg){  

$("#registration_status").ajaxComplete(function(event, request, settings){  

 // Show 'Submit' Button
$('#registration_submit').show();
$('#registration_form_meat').fadeIn('fast');

// Hide Gif Spinning Rotator
$('#ajax_loading').hide();  

 if(msg == 'OK') // LOGIN OK?
 {
 var registration_response = '<!--div id="logged_in">' +
'<div style="margin:0 auto;width:350px"-->' +
'<div style="float:left;margin:0 30px">' +
'<img style="margin:0;" src="/dir/images/loading2.gif">' +
'</div>' +
'<div style="float:left;line-height:18px">'+
'Thank you for registering an account at PetroleumETC.com!<br />A confirmation has been sent to your email address.<br>Please confirm your account before logging in.</div><div style="clear:both"><!--/div></div-->';

$('a.modalCloseImg').hide();  

//$('#simplemodal-container').css("width","500px");
//$('#simplemodal-container').css("height","120px");

 $(this).html(registration_response); // Refers to 'status'

// After 3 seconds redirect the
setTimeout('go_to_private_page()', 4000);
 }
 else // ERROR?
 {
 var registration_response = msg;
 $('#registration_response').html(registration_response);
 }  

 });  

 }  

  });  

// -- End AJAX Call --

return false;

}); // end submit event
//END REGISTRATION CODE

// PROFILE LIST CODE
// Launch MODAL BOX if the Profile Link is clicked
$("#profile_link").live("click",function(e){
e.preventDefault();

$.ajax({
    url: "/auth/profile_list.php",  // 
    success: function(msg){
        $('#profile_list').html(msg);
        }
    });

$('#profile_form').modal({onOpen:myOpen, onClose:myClose, overlayClose:true});
});


// When the form is submitted
$("#profile_status > form").submit(function(){

// Hide 'Submit' Button
$('#profile_submit').hide();
$('#profile_form_meat').fadeOut('fast');

// Show Gif Spinning Rotator
$('#profile_ajax_loading').show();

// 'this' refers to the current submitted form
var str = $(this).serialize();  

// -- Start AJAX Call --
$.ajax({
    type: "POST",
    url: "/auth/update_check.php",  // Send to Update Check
    data: str,
    success: function(msg){  

$("#profile_status").ajaxComplete(function(event, request, settings){  

 // Show 'Submit' Button
$('#profile_submit').show();
$('#profile_form_meat').fadeIn('fast');

// Hide Gif Spinning Rotator
$('#profile_ajax_loading').hide();  

 if(msg == 'OK') // Success?
 {
 var profile_response = '<div id="logged_in">' +
'<div style="margin:0 auto;width:450px">' +
'<div style="float:left;margin:0 30px">' +
'<img style="margin:0;" src="/dir/images/loading2.gif"></div>' +
'<div style="float:left;line-height:18px;margin-top:5px">'+
'Username and password match! <br /> Please wait while your records are updated...</div><div style="clear:both"></div></div>';

$('a.modalCloseImg').hide();  

//$('#simplemodal-container').css("width","500px");
//$('#simplemodal-container').css("height","120px");

 $(this).html(profile_response); // Refers to 'status'

// After 3 seconds redirect the
setTimeout('go_to_private_page()', 4000);
 }
 else // ERROR?
 {
 var profile_response = msg;
 $('#profile_response').html(profile_response);
 }  

 });  

 }  

  });  

// -- End AJAX Call --

return false;

}); // end submit event







}); // CLOSE DOC READY



function go_to_private_page()
{
//window.location = '/'; // Members Area
$.modal.close();
$.ajax({
    url: "/auth/msg1.php",  // Re-Display AuthBox
    success: function(msg){
        $('#auth_box').html(msg);
        }
    });

}

