// JavaScript Document

var isUserPasswordChanged = false;
var isPhysicianPasswordChanged = false;

var isUserUsernameChanged = false;
var isPhysicianUsernameChanged = false;
             
function showLoginFormLayer()
{
	document.getElementById('login_link').className = "active";
	document.getElementById('user_signin').style.display = "block";
	hidePhysicianLoginFormLayer();
}

function hideLoginFormLayer()
{
	document.getElementById('login_link').className = "";
	document.getElementById('user_signin').style.display = "none";
}

function showPhysicianLoginFormLayer()
{
	document.getElementById('physician_login_link').className = "active";
	document.getElementById('physician_signin').style.display = "block";
	hideLoginFormLayer();
}

function hidePhysicianLoginFormLayer()
{

	document.getElementById('physician_login_link').className = "";
	document.getElementById('physician_signin').style.display = "none";
}

function login(loginForm)
{
	//alert((xajax.getFormValues(loginForm.id)));
	xajax_login((xajax.getFormValues(loginForm.id)));
	//alert("login");
	return false;
}

function hiduser()
{
	//signInForm.hide();
	//xajax_login_load_view();
}
function cancelText(){   
   // alert("cancel!");   
	//hideSigninErrorLayer();
    return false;   
}   
  
//Event.observe(window, 'load', function() {   
    //Event.observe('head_user_signin_cancel', 'click', cancelText);
//}); 

function clearUserLabel()
{
	if ( !isUserUsernameChanged )
	{
		document.getElementById('input_user-username').value = "";
	}
	isUserUsernameChanged = true;
}

function clearPhysicianLabel()
{
	if ( !isPhysicianUsernameChanged )
	{
		document.getElementById('input_physician-username').value = "";
	}
	isPhysicianUsernameChanged = true
}

function clearPassword()
{
	//alert("clear");
	//document.getElementById('input_user-password').value = "";
	//document.getElementById('input_physician-password').value = "";
}

function updateUserPasswordInput()
{
	if ( !isUserPasswordChanged )
	{
		document.getElementById('input_user-password').value = "";
		document.getElementById('input_user-password').type = "password";
	}
	isUserPasswordChanged = true;
}

function updatePhysicianPasswordInput()
{
	if ( !isPhysicianPasswordChanged )
	{
		document.getElementById('input_physician-password').value = "";
		document.getElementById('input_physician-password').type = "password";
	}
	isPhysicianPasswordChanged = true;
}


function showSigninErrorLayer()
{
	if(!document.getElementById('signin_error')) return false;
	document.getElementById('signin_error').style.display = "block";
	//signInErrorForm.show();
	//alert("signInErrorForm : "+signInErrorForm);
}

function hideSigninErrorLayer()
{
	if(!document.getElementById('signin_error')) return false;
	document.getElementById('signin_error').style.display = "none";
	//signInErrorForm.hide();
	//alert("hide " + signInErrorForm.hide);
}
function redirectToMember()
{
	var base_url = '/';
	if(typeof PATHWAY_BASE != 'undefined'){
		base_url = PATHWAY_BASE;
	}
    window.location=(base_url+'overview');
}



function doubleCheckPassword(value)
{
	var isValid = isPassword2Valid();
	if(isValid || value)
	{
		document.getElementById('pword2').className = "textField";
		document.getElementById('error_password').style.display = "none";
		
		
	}else{
		document.getElementById('pword2').className = "textField error";
		document.getElementById('error_password').style.display = "block";
		
	}

	if(isValid && isPasswordValid())
	{
		document.getElementById('password2_check').style.display = "block";	
	}else{
		document.getElementById('password2_check').style.display = "none";
	}
	

	
}
function passwordLengthCheck()
{
	//alert((document.getElementById('pword').value).length);
	runPassword(document.getElementById('pword').value, 'password');
	if(isPasswordValid())
	{
		document.getElementById('password_length_check').style.display = "block";
	}else{
		document.getElementById('password_length_check').style.display = "none";
	}
}

function isPasswordLengthValid()
{
	return ($('pword').value.length >= 8);
}
function isPasswordStrong()
{
	pswd = $('pword').value;
	return (/[A-Za-z]/.test(pswd) && /[\d]/.test(pswd));
}
function isPasswordValid()
{
	return (isPasswordLengthValid() && isPasswordStrong());
}
function isPassword2Valid()
{
	return ($('pword').value == $('pword2').value);
}

function submitUserName()
{
	//alert("true : " + document.getElementById('input_username').value );
	if(document.getElementById('input_username').value == "" || document.getElementById('input_email').value == ""){
		showErrMsg("0");
	}else{
		xajax_check_forget_username(document.getElementById('input_username').value,document.getElementById('input_email').value);
	}
}

function inputSecurityQuestion(value)
{
	if(value != -1){
		document.getElementById('forget_security_question').style.display = "block";
		document.getElementById('forget_username_email').style.display = "none";
		document.getElementById('security_answer_value').value = value['security_answer'];
		document.getElementById('user_id_value').value = value['ID'];
		hideErrMsg();
	}else{
		document.getElementById('forget_security_question').style.display = "none";
		showErrMsg("1");
	}
}

function checkSecurityQuestion()
{
	if(document.getElementById('security_answer_value').value == document.getElementById('input_security_question').value){
		xajax_check_security_question(document.getElementById('user_id_value').value,document.getElementById('security_answer_value').value);
	}else{
		showErrMsg("4");
	}
	
}

function notarizeSecurityQuestion(value)
{
	if(value){
		document.getElementById('forget_security_question').style.display = "none";
		document.getElementById('forget_password').style.display = "block";
		hideErrMsg();
	}else{
		document.getElementById('forget_security_question').style.display = "block";
	}
}

function checkForgetPassword()
{
	if($('pword').value == $('pword2').value && isPasswordValid())
	{
		return true;
	}else if(!isPasswordLengthValid()){
		showErrMsg("3");
	}else if(!isPasswordStrong()){
		showErrMsg("5");
	}
	else{
		showErrMsg("2");
	}
	return false;
}

function updatePassworderSuccess()
{
	document.getElementById('forget_password').style.display = "none";
	document.getElementById('update_success').style.display = "block";
	hideErrMsg();
}	

function showErrMsg(id)
{
	document.getElementById('err_msg').style.display = "block";
	document.getElementById('err_'+id).style.display = "block";
}

function hideErrMsg()
{
	document.getElementById('err_msg').style.display = "none";
	
	document.getElementById('err_0').style.display = "none";
	document.getElementById('err_1').style.display = "none";
	document.getElementById('err_2').style.display = "none";
	document.getElementById('err_3').style.display = "none";
	document.getElementById('err_4').style.display = "none";
}

function openWindow(url)
{
	window.open(url,"mywindow","menubar=1,resizable=1,scrollbars=1,width=1000,height=600");

}
             
function checkUserName(value)
{
        //alert(value);
	if (value){
		document.getElementById('error_layer').style.display = "block";
		document.getElementById('input_username').className = "textField error";
	}else{
		document.getElementById('error_layer').style.display = "none";
		document.getElementById('input_username').className = "textField";
	}

	// alert(document.getElementById('input_username').value.length);
	if (value || document.getElementById('input_username').value.length == 0){
		document.getElementById('username_check').style.display = "none";
	}else{
		document.getElementById('username_check').style.display = "block";	
	}
}

function clearUsernameWarnings()
{
	document.getElementById('error_layer').style.display = "none";
	document.getElementById('input_username').className = "textField";
	document.getElementById('username_check').style.display = "none";
}

function checkEmail(value)
{
	if (value && document.getElementById('input_username')){
		document.getElementById('email_error_layer').style.display = "block";
		document.getElementById('myemail').className = "textField error";
	}else{
		document.getElementById('email_error_layer').style.display = "none";
		document.getElementById('myemail').className = "textField";
	}
}

function doubleCheckEmail(value)
{
  if(document.getElementById('myemail').value.toUpperCase() == document.getElementById('myemail2').value.toUpperCase() || value)
	{
		document.getElementById('myemail2').className = "textField";
		document.getElementById('email2_error_layer').style.display = "none";
	}else{
		document.getElementById('myemail2').className = "textField error";
		document.getElementById('email2_error_layer').style.display = "block";
	}
}



function showDiffWindow(value)
{
	if(value){
		document.getElementById('create_form').style.display = "none";
		document.getElementById('longin_form').style.display = "block";
	}else{
		document.getElementById('create_form').style.display = "block";
		document.getElementById('longin_form').style.display = "none";
	}
	
}

function changeCaptche()
{
	xajax_change_captche();
}

function updateCaptche(captche)
{
	document.getElementById('captche_text').value = captche;
	document.getElementById('captche_test_text').value = captche;
}

//Takes everything with a class of futura and converts it using Cufon 
document.observe("dom:loaded", function() {
	Cufon.replace('.futura');
});

//Futura Cufon for main nav
jQuery(document).ready(function(){
	Cufon.replace('#main_nav a', {
		hover: true
	});
});

//Use JavaScript to add ends to active element

/*
var signInErrorForm = {
	id: 'signin_error',
	visible: false,
	animating: false,
	toggle: function(){
		if(signInErrorForm.visible == false) {
			signInErrorForm.show();
		} else {
			signInErrorForm.hide();
		}
	},
	hide: function(){
		if(signInErrorForm.visible == true)
			signInErrorForm.move(-25,0,1);
	},
	show: function(){
		if(signInErrorForm.visible == false)
			signInErrorForm.move(0,1,0);
	},
	move: function(destY,destOp,startOp){
		signInErrorForm.animating = true;
		$(signInErrorForm.id).setStyle({
			display:'block',
			'opacity': startOp
		});
		new Effect.Parallel([
			new Effect.Move(signInErrorForm.id,{
				x: 295,
				y: destY,
				mode: 'absolute'
			}),
			new Effect.Opacity(signInErrorForm.id,{
				sync: true,
				from: startOp,
				to: destOp
			})
		],{
			duration: 0.5,
			afterFinish: function(){
				if(destOp == 1) {
					signInErrorForm.visible = true;
				} else {
					signInErrorForm.visible = false;
				}
				signInErrorForm.animating = false;
			}
		});
	}
}
*/