
var isValid = true;
var ServiceJson = {		
		"001":"greenclouds-dashboard",
		"002":"formistry-dashboard",
		"003":"IIM-Shillong-dashboard",
		"004":"Nalanda-dashboard",
		"005":"Tao-dashboard",
		"006":"People-Facet-dashboard"
}

window.addEventListener("load",checkUserAuthentication);
function checkUserAuthentication(e){
	document.getElementById("login-submit").addEventListener("click",submitForm);
	document.getElementById("userid").addEventListener("blur", showServiceOptions);
	var xhr = new XMLHttpRequest(); 
	xhr.open("POST","/form-dashboard-node/checkAuth",true);
	xhr.onload = function() {

		  	if (xhr.readyState === 4){
		  		
		  			var response = xhr.responseText;
		  			if(response != "e" && response != "fails" && response != "loggedout"){
		  				response = JSON.parse(response);
		  				if(response['r']=="individual"){
		  				//showTabs();
		  					//document.getElementById("username").innerHTML=response["u"];
		  					window.location.href = 'http://'+window.location.host+'/dashboard/index.html';
		  		  			
		  				
		  				}
		  				if(response['r']=="manager"){
		  					window.location.href = 'http://'+window.location.host+'/dashboard/index.html';
		  		  				// not authorized to view
		  					// redirect to individual page
		  				}
		  			}
		  			else{
		  				console.log('fails');
		  				// logout
		  			}
		  			//removeOverlay();
			    }
		  	else{
		  		console.log('please try again !');
		  		// logout
		  		//removeOverlay();
		  	}
		  }
	xhr.send();
	
}
if(document.getElementById("login-box")){
	document.getElementById("login_container").removeChild(document.getElementById("login-box"));
}


function showServiceOptions(e){ 
	var target = this;
	var elementId = target.id;
 	if(checkForBlank("userid")){
 		var email = document.getElementById("userid").value;
 		var xhr = new XMLHttpRequest(); 
	    xhr.onload = function() {
		  	if (xhr.readyState === 4){
		  		var response = JSON.parse(xhr.response);
		  		if(response.status === "error"){
		  			document.getElementById("signup-header").innerHTML=response['msg'];
		  			setInterval(function(){document.getElementById("signup-header").innerHTML = "Login"}, 3000);
		  		}else{
		  			makeOptions(response['msg'], 'services');
		  		}		
			}else{
		  		console.log('please try again !');
		  		// logout
		  		//removeOverlay();
		  	}
		}
		xhr.open("POST","/form-dashboard-node/getServiceCode",true);
		xhr.send("e="+email+"");
 	}	
}

function  makeOptions(data, elementId){
	var select = document.getElementById(elementId);
	for(var key in data){
		var option = document.createElement("option");
		option.value = data[key];
		option.text = ServiceJson[data[key]];
		select.appendChild(option);
	}
}
function checkForBlank(elementId) {
	var eleValue = document.getElementById(elementId).value;
	eleValue = eleValue.replace(/^\s+|\s+$/g,'');
	if(eleValue == "" || eleValue == null || eleValue == undefined) {
		document.getElementById(elementId+"Error").innerHTML = "Must Enter";
		isValid = false;
		//return true;
	}
	return isValid;
	
}

function submitForm(e){
	e.preventDefault();
	e.stopPropagation();
	isValid = true;
	 checkForBlank("userid"); 
	 checkForBlank("password");
	 checkForBlank("services"); 
	if(isValid){
		//var name = document.getElementById("name").value;
		var email = document.getElementById("userid").value;
		var password = document.getElementById("password").value;
		var serviceCode = document.getElementById("services").value;
	//	var passwordAgain = document.getElementById("password-again").value;
		var xhr = new XMLHttpRequest(); 
		xhr.onload = function() {
		  	if (xhr.readyState === 4){
		  		
	  			var response = xhr.responseText;
	  			if(response == "e" || response=="0"){
	  				document.getElementById("signup-header").innerHTML="Invalid username or password";
	  			}else{
	  				// response = JSON.parse(response);
	  				console.log(response);
	  				window.location.href = 'http://'+window.location.host+'/dashboard/index.html';
	  			}
		    }
		  	else{
		  		removeOverlay();
		  	}
		};
		xhr.open("POST","/form-dashboard-node/login",true);
		xhr.send("e="+email+"&p="+password+"&s="+serviceCode+"");
	}
}