var okToClose = false;

function showMyProfile(){
	okToClose = false;
	$("#backgroundPopup").css({
			"opacity": "0.7",
			"zIndex":100
		});
	$("#backgroundPopup").fadeIn("slow");
	
	$('body').append('<div class="myprofileBox"></div>');
	
	//Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();
               
        
	
	$(".myprofileBox").css("margin","0px 0px 50px 0px");
	$(".myprofileBox").css("position","absolute");
	$("html, body").css("overflow","visible");
	
	//Set the popup window to center
	var TopValue = (winH/2 - $(".myprofileBox").height()/2)+20;
	if(TopValue<20){
	    $(".myprofileBox").css('top', 20);
	}else{
		$(".myprofileBox").css('top', TopValue);
	}
        $(".myprofileBox").css('left', winW/2 - $(".myprofileBox").width()/2);
		
	//add close button
	$('.myprofileBox').append('<div style="position:relative"><a class="popcloseimage"><img src="images/spacer.png" alt="Close" name="Image11" width="37" height="37" border="0" id="Image11" onClick="hideProfile()" /></a><div>');	
	$(".popcloseimage").css('top', -60);
	$(".popcloseimage").css('right', 120);
	
	
	//add profile container
	$('.myprofileBox').append('<div id="myProfileContainer"></div>');
	loadProfile();
	
}

function editMode(){	
	$(".prfBtnContainer").html('<a class="submitBtn" onclick="saveProfile()"></a><a class="cancelBtn" onclick="hideProfile()"></a>');
	okToClose = false;
}

function loadProfile(){
	$("#myProfileContainer").html("<h1>Please Wait...</h1>");
	$.ajax({
	   type: "GET",
	   url: "profile/myprofile.php",
	   success: function(msg){
		  $("#myProfileContainer").html(msg);
	   }
	});
}



function hideProfile(){
	
	if(okToClose || confirm("Are you sure? All the unsaved changes will be destroyed.")){
	$("#backgroundPopup").css({
			"opacity": "0",
			"zIndex":1
		});
	$("#backgroundPopup").fadeOut("slow");
	$(".myprofileBox").remove();
	$("html, body").css("overflow","hidden");
	
	}
	
}

function isValidProfile(){
	
	
	if($("#password").val().length<4){
		return false;
	}
	
	if($("#password").val()==$("#password2").val()){
		return true;
	}
	else{
		$("#passwordText").css("color","#d80000");
		$("#passwordText").html("Both Passwords Must Match");
		return false;
	}
}

function saveProfile(){
	if(isValidProfile()){
		
		var strFeilds = $("#profileForm").serialize(); 
		
		$(".prfBtnContainer").html('<h2 class="savingProfLbl">Saving Profile...</h2>');
		
		$.ajax({
		   type: "POST",
		   data: strFeilds,
		   url: "profile/saveprofile.php",
		   success: function(msg){
			 $(".prfBtnContainer").html(msg);
			 okToClose = true;
			 hideProfile();
			 window.location.href = "index.php?page=568&msg=104&status=4";
		   }
		});		
		
	}
}
