﻿$(document).ready(function() { 
	// 
	//$("#company_selectedCats").css("opacity","0.6");
	//$("#company_selectedCats > div").css("opacity","0.9");
	userPanelLogOut = function()
	{
			$.post('/account/check.php',{opt:'logout'},function(data){
				//alert(data)
				window.location = '/index.php';
			});

	}
	$(window).scroll(function () {	
		var offset = ($(document).scrollTop())+"px";  
		$("#company_selectedCats").css("top",offset);							   
	})
	//
	editProfile_element = function (bt,_id){
		__elem = $("#profile_info_"+_id);
		_val = __elem.text();
		bt.parentNode.innerHTML = '<input type="button" id="profile_bt_'+_id+'" onclick="$(\'#profile_temp_'+_id+'\').focus();this.focus();" value="تایید" />';
	//	bt.disabled = true;
		switch(_id){
			case 'address':
				__elem.html( '<textarea id="profile_temp_'+_id+'" onblur="profile_gotoUpdate(\''+_id+'\',this.value)" name="'+_id+'">'+_val+'</textarea>');
				break;
			case 'password':
				
				break;
			case 'email':
				__elem.html( '<input id="profile_temp_'+_id+'" onchange="profileChanged(\''+_id+'\')" onblur="profile_gotoUpdate(\''+_id+'\',this.value)" dir="ltr" align="left" type="text" value="'+_val+'" name="'+_id+'">');
				break;		
			case 'tell':
				__elem.html( '<input id="profile_temp_'+_id+'" onblur="profile_gotoUpdate(\''+_id+'\',this.value)" dir="ltr" align="left" type="text" value="'+_val+'" name="'+_id+'">');
				break;		
			case 'mobile':
				__elem.html( '<input onblur="profile_gotoUpdate(\''+_id+'\',this.value)" dir="ltr" align="left" type="text" value="'+_val+'" name="'+_id+'">');
				break;		
			case 'gender':
				__elem.html( '<select id="profile_temp_'+_id+'" onblur="profile_gotoUpdate(\''+_id+'\',this.options[this.options.selectedIndex].text);" name="'+_id+'"> <option value="m">آقا</option> <option value="f">خانم</option></select>');
				break;		
			default:
				__elem.html( '<input id="profile_temp_'+_id+'" onblur="profile_gotoUpdate(\''+_id+'\',this.value)" dir="rtl" align="left" type="text" value="'+_val+'" name="'+_id+'">');
				break;
		}
	}

	profile_gotoUpdate = function (_id,_val){
		_text = _val;
		if(_id == 'gender'){
			_val = $('$profile_temp_'+_id).val();
		}
		_val= _val.replace("'","\'");
		_text= _text.replace("'","\'");
		$("#profile_info_"+_id).val('<center><em style="color:#999999; font-weight:400">کمی صبر کنید ...</em></center>');
		_function="function(){if (httpXMl.readyState == 4) {getToText('"+_id+"','"+escape(_text)+"')}}";
		_p = 'ajax.php?action=updateprofile&id='+escape(_id)+"&val="+encodeURI(_val)+"&";	
		
		sendRequest(_p,"",_function);
	}


	var formOptions = { 
 		type:			'post',
		beforeSubmit:  	userPanelLoginRequest,
        clearForm: 		false,
		success:       	userPanelLoginResponse  
    }; 
	
     $('#userPanelForm').ajaxForm(formOptions); 
});


function userPanelLoginRequest(formData, jqForm, options) {
	
	if($('#userPanelForm [name="username"]:input').val() ==""){
		alert('لطفا نام کاربری خود را وارد فرمایید');
		$('#userPanelForm [name="username"]:input').focus();
		return false;
	}
	if($('#userPanelForm [name="password"]:input').val() ==""){
		alert('لطفا کلمه عبور خود را وارد فرمایید');
		$('#userPanelForm [name="password"]:input').focus();
		return false;
	}
	
	$('.loginFormLoading').css("display","block");
	$('.loginFormFalse').css("display","none");
	
	//jqForm.parents('div').parents('div').children('#loginFormLoading').css("display","block");
	//jqForm.parents('div').parents('div').children('#loginFormFalse').css("display","none");
	
	
//	$('#loginFormLoading')
//	$('#loginFormFalse').css("display","none");
	return true; 
}
function userPanelLoginResponse(responseText, statusText)  { 
	
	if(responseText.indexOf("1") > -1)
		$('#loginForm').load('/account/menu.php?',{login : true});
	else
		$('.loginFormFalse').css("display","block");
	$('.loginFormLoading').css("display","none");
} 
