$(document).ready(function () {
  var strCurrentLocation = window.location.hash;
  
  $('#generalSettings').hide();
  $('#profileSettings').hide();
  $('ul.settingsTabs a').removeClass('active');
  
  
  var country = $("#country").val();
    if ( country !== "United States" ) {
            $(".state").hide();
    }
  
  $("#country").change(function() {

    var country = $("#country").val();
    if ( country == "United States" ) {
            $(".state").show();
    } else {
            $(".state").hide();
    }
    
  });
 
 
  
     
  if (strCurrentLocation == '#profileSettings'){
    $('#pSettings').addClass('active');
    }
  if (strCurrentLocation == '#generalSettings'){
    $('#gSettings').addClass('active');
    }
    
  if (strCurrentLocation == '#profileSettings'){
    $(strCurrentLocation).show();
  }
  if (strCurrentLocation == '#generalSettings'){
    $(strCurrentLocation).show();
    }
  else{
    $('ul.settingsTabs a').removeClass('active');
    $('#pSettings').addClass('active');
    $('#profileSettings').show();
    }
  
  
  
  $('ul.settingsTabs a').click(function () {
  	$('#generalSettings').hide();
    $('#profileSettings').hide();
    $('#DOM_Control_SettingsErrors').hide();
    $('ul.settingsTabs a').removeClass('active');
    $(this).addClass('active');
  	$(this.hash).show();
  	window.location.hash = $(this).attr('href');
  	return false;
  });
  
  $('#formNewPassword').submit(function () {
    var oldPassword = $('#oPass').val();
    var newPassword = $('#nPass').val();
    var confPassword = $('#confirm_password').val();
    var msgBox = $('#msgBoxPassword');
    var passErrors = false;
    
    if (newPassword != confPassword) {
      msgBox.show().text('Passwords do not match');
      passErrors = true;
    }
    if (!passErrors) {
      $.ajax({
        type: 'POST',
        data: 'oPass=' + oldPassword + '&nPass=' + newPassword, 
        url: 'ajax/ajax.changePassword.php', 
        cache: false,
        global: false, 
        success: function (html) {
          if (html == 1) {
            msgBox.show().text('You have successfully changed your password');
            $('#oPass').val('');
            $('#nPass').val('');
            $('#confirm_password').val('');
          }
          else if (html == 2) {
            msgBox.show().text('Password was not changed');
          }
          else if (html == 3) {
            msgBox.show().text('Old Password was incorrect');
          }
          else if (html == 4) {
            msgBox.show().text('User was not found');
          }
        }
      });
    }
    return false;
  });
  
  $('#formDeleteProfile').submit(function () {
    var profileID = $('#profile_del').val();
    var passErrors = false;
    
    if (profileID == 167) {
      msgBox.show().text('Cannot Delete this account...Main account');
      passErrors = true;
    }
    if (!passErrors && confirm("Are you sure you want to delete your profile? This can not be undone.")) {
      $.ajax({
        type: 'POST',
        data: 'profileID=' + profileID, 
        url: 'ajax/ajax.deleteProfile.php', 
        cache: false,
        global: false, 
        success: function (html) {
          if (html == 1) {
            parent.window.location.href = "http://www.newartist.com/logout.php";
          }
          else if (html == 0) {
            msgBox.show().text('Unable to remove profile');
          }
          
        }
      });
    }
    return false;
  });
  
  $('#formAbout').submit(function () {
    //var strForm = $('#formAbout').serialize();
    arrayCheckBox = new Array;
                                $("input:checked").each(function(id) {
                         myVar = $("input:checked").get(id);
                         arrayCheckBox.push(myVar.value);
                        }); 
    var fname = $('#fname').val();
    var lname = $('#lname').val();
    var displayname = $('#displayname').val();
    var dob = $('#dobMonth').val() + $('#dobDay').val() + $('#dobYear').val();
	var dob_vis = $('#bday_vis').val();
    var gender = $('#genderSelect').val();
    var relationship = $('#relationshipSelect').val();
    var genre = $('#GenreSelect').val();
    //var addgenre = $('#addgenreSelect').val();
    
    $('#submitAboutMe').attr('disabled', true);
    $('#fname').attr('disabled', true);
    $('#lname').attr('disabled', true);
    $('#displayname').attr('disabled', true);
    $('#dobMonth').attr('disabled', true);
    $('#dobDay').attr('disabled', true);
    $('#dobYear').attr('disabled', true);
	$('#bday_vis').attr('disabled', true);
    $('#genderSelect').attr('disabled', true);
    $('#relationshipSelect').attr('disabled', true);
    $('#GenreSelect').attr('disabled', true);
    $('#addgenreSelect').attr('disabled', true);
    $.ajax({
        type: 'POST',
        data: 'fname='+fname+'&lname='+lname+'&displayname='+displayname+'&dob='+dob+'&dob_vis='+dob_vis+'&gender='+gender+'&relationship='+relationship+'&genre='+genre+'&addgenre='+arrayCheckBox,
        //data: strForm,
        url: 'ajax/ajax.updateAboutMe.php', 
        success: function (html) {
          if (html == 1) {
            $('#msgBoxAbout').show().text('About Me section has been updated.');
            setTimeout(function () {$('#msgBoxAbout').fadeOut();}, 3000);
            $('#submitAboutMe').removeAttr('disabled');
            $('#fname').removeAttr('disabled');
            $('#lname').removeAttr('disabled');
            $('#displayname').removeAttr('disabled');
            $('#dobMonth').removeAttr('disabled');
            $('#dobDay').removeAttr('disabled');
            $('#dobYear').removeAttr('disabled');
			$('#bday_vis').removeAttr('disabled');
            $('#genderSelect').removeAttr('disabled');
            $('#relationshipSelect').removeAttr('disabled');
            $('#GenreSelect').removeAttr('disabled');
            $('#addgenreSelect').removeAttr('disabled');
          }
          else {
            $('#msgBoxAbout').show().text('Sorry, there seems to be an error in one of your fields.');
            setTimeout(function () {$('#msgBoxAbout').fadeOut();}, 5000);
            $('#submitAboutMe').removeAttr('disabled');
            $('#fname').removeAttr('disabled');
            $('#lname').removeAttr('disabled');
            $('#displayname').removeAttr('disabled');
            $('#dobMonth').removeAttr('disabled');
            $('#dobDay').removeAttr('disabled');
            $('#dobYear').removeAttr('disabled');
			$('#bday_vis').removeAttr('disabled');
            $('#genderSelect').removeAttr('disabled');
            $('#relationshipSelect').removeAttr('disabled');
            $('#GenreSelect').removeAttr('disabled');
            $('#addgenreSelect').removeAttr('disabled');
          }
        }
      });
      return false;
  });
  
  $('#settingsFormBio').submit(function () {
    var strForm = $(this).serialize();
    var objTxtArea = $(this).find('textarea:first');
    
    $.ajax({
      type: 'POST',
      data: strForm,
      url: 'ajax/ajax.updateBio.php',
      success: function (response) {
        $('#msgBoxBio').show();
        
        if (response == 1)
          $('#msgBoxBio').text('Your biography has been updated.');
        else
          $('#msgBoxBio').text('Error. Unable to update your biography at this time.');
      }
    });
    return false;
  });
  
  $('#settingsFormShortcut').submit(function () {
    var strForm = $(this).serialize();
    var shortcut = $('#settingsShortcut').val();
    var shortcut2 = $('#settingsShortcut2').val();
    $.ajax({
      type: 'POST',
      data: strForm,
      url: 'ajax/ajax.updateShortcut.php',
      success: function (response) {
        $('#msgBoxShortcut').show();
        
        if (response == 1){
          $('#msgBoxShortcut').text('Your Personalized Url: http://www.newartist.com/'+shortcut+' has been updated.');
          $('#settingsShortcut2').attr('value', shortcut);
        }else{
          $('#msgBoxShortcut').text('Error. Personalized Url: http://www.newartist.com/'+shortcut+' is already in use, please try again.');
          $('#settingsShortcut').attr('value', shortcut2);
        }
        setTimeout(function() {
           $('#msgBoxShortcut').fadeOut('fast');
        }, 4000);
      }
    });
    return false;
  });
  
  $('#formContact').submit(function () {
    var email = $('#email').val();
    //var im = $('#im').val();
    var website = $('#website').val();
    var phone = $('#phone').val();
    var address = $('#address').val();
    var city = $('#city').val();
    var country = $('#country').val();
    var state = $('#state').val();
    var zip = $('#zip').val();
    var hometown = $('#hometown').val();
    
    if ( country !== "United States" ) {
            var state = '';
    }
    
    $('#email').attr('disabled', 'disabled');
    //$('#im').attr('disabled', true);
    $('#phone').attr('disabled', 'disabled');
    $('#address').attr('disabled', 'disabled');
    $('#country').attr('disabled', 'disabled');
    $('#city').attr('disabled', 'disabled');
    $('#state').attr('disabled', 'disabled');
    $('#zip').attr('disabled', 'disabled');
    $('#hometown').attr('disabled', 'disabled');
    $('#submitContact').attr('disabled', 'disabled');
    
    $.ajax({
        type: 'POST',
        data: 'email='+email+'&phone='+phone+'&address='+address+'&country='+country+
              '&city='+city+'&state='+state+'&zip='+zip+'&hometown='+hometown,
        url: 'ajax/ajax.updateContact.php', 
        success: function (html) {
          if (html == 1) {
            $('#msgBoxContact').show().text('Contact section has been updated.');
            setTimeout(function () {$('#msgBoxContact').fadeOut();}, 3000);
          }
          else if (html == 2) {
            $('#msgBoxContact').show().text('Unable to save your information at this time. Please try again later.');
            setTimeout(function () {$('#msgBoxContact').fadeOut();}, 5000);
          }
          else {
            $('#msgBoxContact').show().text('Sorry, there seems to be an error in one of your fields.');
            setTimeout(function () {$('#msgBoxContact').fadeOut();}, 5000);
          }
          $('#email').removeAttr('disabled');
          //$('#im').removeAttr('disabled');
          $('#phone').removeAttr('disabled');
          $('#address').removeAttr('disabled');
          $('#country').removeAttr('country');
          $('#city').removeAttr('disabled');
          $('#state').removeAttr('disabled');
          $('#zip').removeAttr('disabled');
          $('#hometown').removeAttr('disabled');
          $('#submitContact').removeAttr('disabled');
        }
      });
      return false;
  });
  
  $('#formEducation').submit(function () {
    var sType = $('#educationSchool').val();
    var sName = $('#educationSchoolName').val();
    var sDegree = $('#educationDegree').val();
    var sSpecialize = $('#educationSpecialize').val();
    
    $('#educationSchool').attr('disabled', true);
    $('#educationSchoolName').attr('disabled', true);
    $('#educationDegree').attr('disabled', true);
    $('#educationSpecialize').attr('disabled', true);
    $('#submitEducation').attr('disabled', true);
    $.ajax({
        type: 'POST',
        data: 'sType='+sType+'&sName='+sName+'&sDegree='+sDegree+'&sSpecialize='+sSpecialize,
        url: 'ajax/ajax.updateEducation.php', 
        success: function (html) {
          if (html == 1) {
            $('#msgBoxEducation').show().text('Education section has been updated.');
            setTimeout(function () {$('#msgBoxEducation').fadeOut();}, 3000);
            $('#educationSchool').removeAttr('disabled');
            $('#educationSchoolName').removeAttr('disabled');
            $('#educationDegree').removeAttr('disabled');
            $('#educationSpecialize').removeAttr('disabled');
            $('#submitEducation').removeAttr('disabled');
          }
          else if (html == 2) {
            $('#msgBoxEducation').show().text('Unable to save your information at this time. Please try again later.');
            setTimeout(function () {$('#msgBoxEducation').fadeOut();}, 5000);
            $('#educationSchool').removeAttr('disabled');
            $('#educationSchoolName').removeAttr('disabled');
            $('#educationDegree').removeAttr('disabled');
            $('#educationSpecialize').removeAttr('disabled');
            $('#submitEducation').removeAttr('disabled');
          }
          else {
            $('#msgBoxEducation').show().text('Sorry, there seems to be an error in one of your fields.');
            setTimeout(function () {$('#msgBoxEducation').fadeOut();}, 5000);
            $('#educationSchool').removeAttr('disabled');
            $('#educationSchoolName').removeAttr('disabled');
            $('#educationDegree').removeAttr('disabled');
            $('#educationSpecialize').removeAttr('disabled');
            $('#submitEducation').removeAttr('disabled');
          }
        }
      });
      return false;
  });
  
  $('#formWork').submit(function () {
    var strForm = $(this).serialize();
    
    $('#workEmployer').attr('disabled', 'disabled');
    $('#workPosition').attr('disabled', 'disabled');
    $('#workDescription').attr('disabled', 'disabled');
    $('#workCity').attr('disabled', 'disabled');
    $('#workTime').attr('disabled', 'disabled');
    $('#submitWork').attr('disabled', 'disabled');
    
    $.ajax({
        type: 'POST',
        data: strForm,
        url: 'ajax/ajax.updateWork.php', 
        success: function (html) {
          if (html == 1) {
            $('#msgBoxWork').show().text('Work section has been updated.');
          }
          else if (html == 2) {
            $('#msgBoxWork').show().text('Unable to save your information at this time. Please try again later.');
          }
          else {
            $('#msgBoxWork').show().text('Sorry, there seems to be an error in one of your fields.');
          }
          setTimeout(function () {$('#msgBoxWork').fadeOut();}, 5000);
          $('#workEmployer').removeAttr('disabled');
          $('#workPosition').removeAttr('disabled');
          $('#workDescription').removeAttr('disabled');
          $('#workCity').removeAttr('disabled');
          $('#submitEducation').removeAttr('disabled');
          $('#workTime').removeAttr('disabled');
          $('#submitWork').removeAttr('disabled');
        }
      });
      return false;
  });
  
});


