$(document).ready(function(){
		
		//Press button of Registration's form
		$("#btn_RegAdd").click(function () {
			
			$("#progressBarRegistration").show();
			$("#errorRegArea").hide();
			
			var pars = $('#regForm').serialize()+"&resAgreeTerm="+$('#edt_RegTerms').attr("checked")+"&resRun=can";
			$.ajax({
					type: "POST",
					data: pars,
					url: $('#base_url').attr('href')+"account/checkRegistrarion",  
					success: function(data){
					
					var response = eval("(" + data + ")");
					
					$("#progressBarRegistration").hide();
					$("#errorRegArea").show();
					$("#errorRegArea").html('');
					
					if (response.termsValid!=1)  $("#errorRegArea").html(response.termsValid);
					
					if (response.captchaValid!=1) {
						$("#errorRegArea").html(response.captchaValid);
						$('#captcha_code').focus();
					}
					
					if (response.passcValid!=1 || response.passValid==0){
						$("#errorRegArea").html(response.passcValid);
						$('#edt_RegPasswordCoff').focus();
					}
					
					if (response.passValid !=0 && response.passcValid==-1){
						$("#errorRegArea").html(response.passValid);
						$('#edt_RegPassword').focus();
					}
					
					if (response.emailValid!=true) { 
						$("#errorRegArea").html(response.emailValid);
						$('#edt_RegMail').focus();
					}
					
					if (0 == response.error){ 
						
						$("#errorRegArea").html('');
						$('#regForm').submit();
					}
				}
			});
	});
	
	
	$("#edt_LoginField").click(function(){
		if ($("#edt_LoginField").val()==$("#edt_YourEmail").val()) $("#edt_LoginField").val('');
		$("#edt_LoginField").attr("class", "login_inputstyle");	
	})
	
	$("#edt_LoginField").blur(function(){
		
		if ($("#edt_LoginField").val()==''){
			$("#edt_LoginField").val($("#edt_YourEmail").val());
			$("#edt_LoginField").attr("class", "login_inputstyle_lostpass");
		}
	})
	
	$("#edt_LoginField").keypress(function (e){
		if (e.which==13) { $('#btn_LostPass').click(); return false; }
	});
	
	$('#edt_RegMail').keypress(function (e){
		if (e.which==13) { $("#btn_RegAdd").click(); return false;	}
	});
	
	$("#edt_RegPassword").keypress(function (e){
		if (e.which==13) { $("#btn_RegAdd").click(); return false;	}
	});
	
	$("#edt_RegPasswordCoff").keypress(function (e){
		if (e.which==13) { $("#btn_RegAdd").click(); return false;	}
	});
	
	$("#captcha_code").keypress(function (e){
		if (e.which==13) { $("#btn_RegAdd").click(); return false;	}
	});
	
	$("#edt_NickEdit").keypress(function (e){
		if (e.which==13) { $("#btn_EditAccount").click(); return false;	}
	});
	
	$("#edt_MailEdit").keypress(function (e){
		if (e.which==13) { $("#btn_EditAccount").click(); return false;	}
	});
	
	$("#edt_PasswordCurrent").keypress(function (e){
		if (e.which==13) { $("#btn_EditAccount").click(); return false;	}
	});
	
	$("#edt_PasswordNew").keypress(function (e){
		if (e.which==13) { $("#btn_EditAccount").click(); return false;	}
	});
	
	$("#edt_PasswordConfNew").keypress(function (e){
		if (e.which==13) { $("#btn_EditAccount").click(); return false;	}
	});

	//Press button Edit account
	$("#btn_EditAccount").click(function (){
		
		$("#progressBar").show();
		$("#errorArea").hide();
		$("#errorAreaEditAccount").html("");
		
		if (0==$('#edt_PassEdit').val()) $('#edt_PasswordCurrent').val('');
		if (''!=$('#edt_PasswordCurrent').val() || ''!=$('#edt_PasswordNew').val() || ''!=$('#edt_PasswordConfNew').val()) 
			$('#edt_PassEdit').val(1) 
		else 
			$('#edt_PassEdit').val(0);
		var pars = $('#regForm').serialize()+"&resRun=can";
		$.ajax({
				type: "POST",
				data: pars,
				url: $('#base_url').attr('href')+"account/editAccountInfo",  
				success: function(data){

						$("#progressBar").hide();
						
						var response = eval("(" + data + ")");
						
						$("#errorAreaEditAccount").html("");
						
						if (response.userInSystem==1){
							
							//Edit password too
							if (response.editPass==1){
								if (response.newConfPass!=1) { 
									$("#errorAreaEditAccount").html(response.newConfPass);
									$('#edt_PasswordConfNew').focus();
								}
								
								if (response.newPass!=1) { 
									$("#errorAreaEditAccount").html(response.newPass);
									$('#edt_PasswordNew').focus();
								}
								
								if (response.currentPass!=1) { 
									$("#errorAreaEditAccount").html(response.currentPass);
									$('#edt_PasswordCurrent').focus();
								}
							}
							
							
							if (response.emailValid!=true) { 
									$("#errorAreaEditAccount").html(response.emailValid);
									$('#edt_MailEdit').focus();
							}
							if (response.nickValid!=1) { 
									$("#errorAreaEditAccount").html(response.nickValid);
									$('#edt_NickEdit').focus();
							}
							
							
							if (0 == response.error) 
							{
						
								$("#btn_ReturnToMyFiles").html($("#edt_ReturnToMyFilesLabel").attr('value'));
								
								$("#errorAreaEditAccount").html(response.messageError);
								if (0 != response.reSendNewEmailNotice)  $('#reSendNewEmail').html(response.reSendNewEmailNotice);
								$('#editPassArea').hide();
								$('#edt_PassEdit').val(0);
								$('#changePass').show();
								
								$('#edt_PasswordNew').val('');
								$('#edt_PasswordConfNew').val('');
								$('#edt_MailEdit').val($('#edt_OldMail').val());
							}
						}else  window.location = $("#base_url").attr("href");			
					}
				});
	});
	
	//Press button Edit Pass
	$('#changePassHref').click(function (){

		$('#editPassArea').show();
		$('#edt_PassEdit').val(1);
		$('#changePass').hide();
	});
	
	//Press button Restore password
	$('#btn_LostPass').click(function (){

		$("#progressBarLostPass").show();
		$("#errorForgotPass").hide();
		
		var pars = "resEmail="+$('#edt_LoginField').val()+"&resRun=can";
	
		$.ajax({
				type: "POST",
				data: pars,
				url: $('#base_url').attr('href')+"account/newPassword",  
				success: function(data){
					
					$("#progressBarLostPass").hide();
					$("#errorForgotPass").show();
					$("#errorForgotPass").html('');
					
					var response  = eval("(" + data + ")");
					
					if (0==response.errors) {
						$('#lostPassForm').submit();
					}else{
						$("#errorForgotPass").html(response.emailValid);
					}
				}
		});
	});
	
	$('.additional_links_input').click(function (){
		$(this).select();
	});
});

	function orderTable(field_name, page_num, showType){

		$("#progressBar").show();

		var fields = $("#filesTableForm :checkbox").serializeArray();
		var myArray  = new Array();
		jQuery.each(fields, function(i, field){
			myArray.push(field.value);
		});
		var checkedFields = myArray.toString();

		var order_type = $('#edt_FilesTableOrder').val();  
		var pars = "resField="+field_name+"&resType="+order_type+"&resCheckedFields="+checkedFields+"&resPageNum="+page_num+"&resShowType="+showType+"&resRun=can";
		$.ajax({
				type: "POST",
				data: pars,
				url: $('#base_url').attr('href')+"files/orderTableFiles",  
				success: function(data){
					
					var response = eval("(" + data + ")");
					
					if (response.userInSystem==1){
						$("#progressBar").hide();
						$("#files_tablearea").html(response.html_text);
					}
					else window.location = $("#base_url").attr("href");				
				}
			});
	}

	function pageNum(orderField, orderType, pageNum, showType){
	
		$("#progressBar").show();
		$("#uploadMessage").html('');	
		var pars = "resPageNum="+pageNum+"&resOrderType="+orderType+"&resOrderField="+orderField+"&resShowType="+showType+"&resRun=can";
		$.ajax({
				type: "POST",
				data: pars,
				url: $('#base_url').attr('href')+"files/pageNum",  
				success: function(data){
					
					var response = eval("(" + data + ")");
					if (response.userInSystem==1){
						$("#progressBar").hide();
						$("#files_tablearea").html(response.html_text);
					}else window.location = $("#base_url").attr("href");
					}
				});
	}

	function deleteFiles(url, question, theElement, pageNum, showType){

		var fields = $("#filesTableForm :checkbox").serializeArray();
		
		var myArray  = new Array();
			jQuery.each(fields, function(i, field){
			myArray.push(field.value);
		});
	
	
		var checkedFields = myArray.toString();
		
		if (''==checkedFields) alert($("#langNoFileSelect").val());
		else{
			
			if (true==confirm(question)){
			$("#progressBar").show();	
		
			var pars = "resCheckedFields="+checkedFields+"&resPageNum="+pageNum+"&resShowType="+showType+"&resRun=can";
			$.ajax({
				type: "POST",
				data: pars,
				url: $('#base_url').attr('href')+"files/deleteFiles",
				success: function(data){
	
						var response = eval("(" + data + ")");
						
						
						if (response.userInSystem==1){
							$("#progressBar").hide();
							$("#files_tablearea").html(response.html_text);
						}else window.location = $("#base_url").attr("href");
					}
				});
			}else{
				$("#progressBar").hide();
			}	
		}	
	}
	
	function checkRow(id){

		if (true == $('#edt_Terms'+id).attr("checked")) $('#row'+id).attr("class", "select_row"); else  $('#row'+id).attr("class", "unselect_row");
	}
	
	function checkImage(id){
		
		var check = false;
		if (false==$('#edt_Terms'+id).attr('checked')) check = true;
		
		$('#edt_Terms'+id).attr('checked',check);
	}
	
	function reSendNewMail(){
		
		$("#progressBar").show();
		$("#errorArea").hide();
		$("#errorAreaEditAccount").html('');
		
		$.ajax({
				type: "POST",
				data: "resRun=can",
				url: $('#base_url').attr('href')+"account/reSendConfirationMail",  
				success: function(data){
					
					$("#progressBar").hide();
					$("#errorArea").show();

					var response  = eval("(" + data + ")");
					
					if (response.userInSystem==1){
						
						$("#errorAreaEditAccount").html(response.message);
						if (response.error==1) $("#reSendNewEmail").html('');
						
					}else window.location = $("#base_url").attr("href");
				}
		});
	}
	
	function SelectAll(){
		$("#filesTableForm").checkCheckboxes();
	}
	
	function UnSelectAll(){
		$("#filesTableForm").unCheckCheckboxes();
	}
	
	function resendRegistrationKey(){
		
		$('#errorArea').html('');
		$('#progressBarConfReg').show('');
		$.ajax({
				type: "POST",
				data: "resRun=can",
				url: $('#base_url').attr('href')+"account/reSendConfirationKeyRegistration",  
				success: function(data){
					
					$('#progressBarConfReg').hide();

					var response  = eval("(" + data + ")");

					$('#errorArea').html(response.message);
				}
		});
	}
	
	function resendRegistrationKeyRegForm(){
		
		$('#errorRegArea').html('');
		$('#progressBarRegistration').show('');
		$.ajax({
				type: "POST",
				data: "fromRegForm=1&resRun=can",
				url: $('#base_url').attr('href')+"account/reSendConfirationKeyRegistration",  
				success: function(data){
					
					$('#progressBarRegistration').hide();

					var response  = eval("(" + data + ")");

					$('#errorRegArea').html(response.message);
				}
		});
	}
