var isEmailDivVisible = false;
var locale = 'en_US';

var usernameWarning = 'Dimdim ID field cannot be left blank';
var pwdWarning = 'Password field cannot be left blank';
var emailSuccess = 'Please  check your email for changing the password';
var usernameEnter = 'Please enter your Dimdim ID';

var requestorParam = '';

function saveid() 
	{
		Cookies.erase('logincookie5');
		Cookies.erase('logincookie2');
		var x = document.forms['myform'].name.value;
		var z = document.forms['myform'].password.value;
		//alert('in saveid x = '+x);
		//alert('in asveid z = '+z);
		if (!x || !z){
			//alert('Please fill in a value in the input box.');
		}
		else {
			Cookies.create('logincookie5',x,7);
			Cookies.create('logincookie2',z,7);
		}
	}

	function init() 
	{
		var x = Cookies['logincookie5'];
		var z = Cookies['logincookie2'];
		//alert('in init '+x);
		//alert('in init '+z);
		if(x)
		{
			document.getElementById("username").value =  x;
			document.getElementById("password").value =  z;
			document.getElementById("checkbox").checked=true;
		}

	}

	function pp()
	{
		window.open('http://www.dimdim.com/ppolicy.html','newwindow1','toolbar=0,top=0,left=0,width=550, height=390,scrollbars=1,resizable=1');
	}
	function tm()
	{
		window.open('http://www.dimdim.com/tm.html','newwindow1','toolbar=0,top=0,left=0,width=550, height=390,scrollbars=1,resizable=1');
	}
	
function waiting()
{
 document.getElementById('ajax_waiting').className = 'show';
 document.getElementById('ajax_waiting_css').className = 'show';
}

function login_mouseover(button)
{
	//$("loginbtn").className = "btn_login_hover";
	button.className = "btn_login_hover";
}

function clearErrors(){
	if($('errormsgLogin')){
		$('errormsgLogin').update('&nbsp;');
	}
	if($('errormsgEmail')){
		$('errormsgEmail').update('&nbsp;');
	}
}

function login_mouseout(button)
{
	//alert('inside login_mouseout');
	//document.getElementById("loginbtn").className = "btn_login";
	//$("loginbtn").className = "btn_login";
	button.className = "btn_login";
}
function submitform(requestor)
{
	//alert('inside submit..requestor = '+requestor);
  //document.myform.submit();
	requestorParam = requestor;
	if(document.myform.checkbox.checked!= true){
		Cookies.erase('logincookie5');
		Cookies.erase('logincookie2');
	}
	else{
		saveid();
	}

  clearErrors();
  var allNodes = Form.serialize("testFrom");
  var url = 'SignIn.action?lc='+locale;

	//alert(allNodes);
  if($F('username').length > 0  && $F('password').length > 0){
	  document.body.style.cursor = 'wait';
	  var myAjax = new Ajax.Request(
				url,
				{
					method: 'post',
					parameters: allNodes,
					onComplete: showResponseLogin
				});
	  waiting();
	}else{
		if($F('username').length == 0){
			$('errormsgLogin').update(usernameWarning);
		}else if($F('password').length == 0){
			$('errormsgLogin').update(pwdWarning);
		}
	}

}

function showResponseLogin(originalRequest)
{
	//alert('originalRequest = '+originalRequest.responseText);
	//alert(response ? Object.inspect(response) : "no JSON object");
	//$('result').value = originalRequest.responseText;
	var data = originalRequest.responseText.evalJSON();
	//alert('data = '+data.data);
	//alert('requestorParam = '+requestorParam);
	if('true'== data.success){
		//alert('inside true');
		var rootpage = 'html/portal/rootpage.action?lc='+locale+'&uuid='+data.data.text;
		if('site'==requestorParam)
		{
			rootpage = rootpage+'&requestor=site';
		}
		//alert('inside true rootpage '+rootpage);
		window.location=rootpage;
	}else{
		//alert('inside false');
		document.getElementById('ajax_waiting').className = 'Hide';
	document.getElementById('ajax_waiting_css').className = 'Hide';
		$('errormsgLogin').update(data.data.text);
	}
	document.body.style.cursor = 'default';
}


function showResponseEmail(originalRequest)
{
	//alert('originalRequest = '+originalRequest.responseText);
	//alert(response ? Object.inspect(response) : "no JSON object");
	//$('result').value = originalRequest.responseText;
	var data = originalRequest.responseText.evalJSON();
	//alert('data = '+data);
	//alert('success = '+data.success);
	if('true'== data.success){
		//alert('inside success');
		//$('forgotPwdLink').className = 'Hide';
		$('email').update(emailSuccess);
	}else{
		//alert('inside false');
		$('errormsgEmail').update(data.data.text);
	}
	document.body.style.cursor = 'default';
}


function showSendemail(){

	//alert('inside sending email isClickedEmail = '+isEmailDivVisible);
	clearErrors();
	if(!isEmailDivVisible){
		//alert('showing... email..');
		$('email').className = 'show';
		$('emailaddress').value=usernameEnter;
		//$('emailLink_image').className = 'show';
		//$('space').className = 'Hide';
		isEmailDivVisible = true;
	}else{
		$('email').className ='Hide';
		//$('emailLink_image').className = 'Hide';
		//$('space').className = 'show';
		isEmailDivVisible = false;
	}

}

function initialize(localeCode){
//	alert('inside initialize '+$('email'));
//	$('email').className = 'Hide';
	//$('emailLink_image').className = 'Hide';
	locale = localeCode;

}

function sendmeMail(){

	clearErrors();
	//alert('inside send mail '+$F('emailaddress'));
	var userID = $F('emailaddress');

 	var params = 'email=' + userID;
  	var url = 'ForgotPassword.action';

  	if(userID != null && userID.length > 0){
		var myAjax = new Ajax.Request(
				url,
				{
					method: 'post',
					parameters: params,
					onComplete: showResponseEmail
				});
	}
	else{
		$('errormsgEmail').update(usernameWarning);
	}
}

function captureKeyEvent(myfield, e){
	captureKeyEvent(myfield, e);
}

function captureKeyEvent(myfield, e, requestor){
	var keycode;

	//alert('event = '+e);

	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	//else {
	//	login_mouseout(myfield);
	//	return true;
	//}

	//alert('key code = '+keycode);
	//13 for enter key and 32 for space
	if (keycode == 13 || keycode == 32)
	{
	   	//alert(myfield)
	   	if(myfield.id == 'sendmeButton' || myfield.id == 'emailaddress'){
//	   	alert('send me button');
	   		sendmeMail();
	   		return false;
	   	}else if(myfield.id == 'username' || myfield.id == 'password' || myfield.id == 'login_button'){
			submitform(requestor);
	   		return false;
	   }
		//alert('no buttons matched...');
	}
	else if(myfield.id == 'loginbtn'){
		login_mouseout(myfield);
	    return true;
	}

}
