// ------------------------------------------------------------------ //   
// ------------------ UCK initialization -------------------- //
// ------------------------------------------------------------------ //   

var uck = new Uck();
if (!uck.init('uck-js-api'))
	alert('Init UCK Error : ' + uck.error);

var ProfileType = new Array();
ProfileType["1"]=home;
ProfileType["2"]=office;
ProfileType["3"]=nomad;
ProfileType["4"]=secret;

var uckSelectionBox = new UckDeviceSelectionBox('UckDeviceSelected');
/**
 * Test Applet is initialised with following parameters :
 * TestApplet(String divId, int appletWidth, int appletHeight, String loadingMessage);
 */
var testApplet = new TestApplet("TestAppletBox", 1, 1, "");
//TEST APPLET is started in the onload action	

/**
 *  Callback called when the TestApplet has finished executing
 *  javaEnvData is null if Java is not enabled
 *  otherwise, following members are available : osName, osArch, osVersion, jreVersion, jreVendor
 */
function TestAppletResultCallback(javaEnvData)
{
    if(!javaEnvData)
		jAlert(java_not_installed);
}
// ------------------------------------------------------------------ //
// ---- Callbacks required by the UCK Applet JavaScript Interface --- //
// ------------------------------------------------------------------ //

/*
 * Callback called whenever UCK Applet needs to log a warning or an error
 * 
 * @param log_code the string error code referenced in Uck JavaScript object
 * @param details the string details of the error (when provided)
 */
function UckLogCallback(log_code, details) {
	//must be implemented and handle error codes
	// see Uck.js for the error codes
	err_prefix = log_code.substr(0,2)
	if(err_prefix == "04" || err_prefix == "05") //codes starting with 04 & 05 are errors
	{
		jAlert(UckLogs[log_code]);
	}
	
}

function validatepin(element)
{
	element.value=element.value.replace(/[^0-9]/g, '');
}

/*
 * Callback called when UCK Applet has finished loading
 */
 var uckJS;
function UckReadyCallback() {
	showDevices();
}
 /*
  * Show Device Selection BOX
  */
var step=0;
function showDevices()
{
	if(step!=0)
	{
		uckJS = getUckApi();
	   //sessionRegDna = uckJS.sessionOpen(login,'0000',confirmcode);
	  if(uck.isException()) {return;}
	  var devices = uckJS.listConnectedDevices();
	  uckSelectionBox.open(devices);
	}
}
  
function next1()
{
	if(validateform1())
	{
		step=1;
		email = document.getElementById("email").value;
		
		jQuery.ajax( {
			url :"doCheckEmail.action?email=" + email,
			success : function(msg) {
				if (msg == "Existant_User") {
					
					jAlert(exist_email,"Id4Yoo");
					return false;
				} else {
					document.getElementById('page1').style.display = "none";
					document.getElementById('page2').style.display = "block";
					document.getElementById('page3').style.display = "none";
					document.getElementById('page4').style.display = "none";
					document.getElementById('page5').style.display = "none";
					
					document.getElementById('waitdna').style.display = "none";
					
					document.getElementById('imgetape1').src = "resources/images/"+lang+"/step/etape01-off.png";
					document.getElementById('imgetape2').src = "resources/images/"+lang+"/step/etape02-on.png";
					document.getElementById('imgetape3').src = "resources/images/"+lang+"/step/etape03-off.png";
					document.getElementById('imgetape4').src = "resources/images/"+lang+"/step/etape04-off.png";
					document.getElementById('imgetape5').src = "resources/images/"+lang+"/step/etape05-off.png";
					
					document.getElementById('profil').value = 0;
					document.getElementById('profilname').value = "";
					document.getElementById('options_computer').checked = false;
					document.getElementById('options_mobilephone').checked = false;
					document.getElementById('options_usbkey').checked = false;
					document.getElementById('SawsDeviceSelectionBox').style.display = "none";
				}
			}
		});
		return true;
	}
}

function prev1()
{
	document.getElementById('page1').style.display = "block";
	document.getElementById('page2').style.display = "none";
	document.getElementById('page3').style.display = "none";
	document.getElementById('page4').style.display = "none";
	document.getElementById('page5').style.display = "none";
	document.getElementById('imgetape1').src = "resources/images/"+lang+"/step/etape01-on.png";
	document.getElementById('imgetape2').src = "resources/images/"+lang+"/step/etape02-off.png";
	document.getElementById('imgetape3').src = "resources/images/"+lang+"/step/etape03-off.png";
	document.getElementById('imgetape4').src = "resources/images/"+lang+"/step/etape04-off.png";
	document.getElementById('imgetape5').src = "resources/images/"+lang+"/step/etape05-off.png";
	resetUck();startUck();
	step=0;
	return true;
	
}
function next2()
{
	if(validateDeviceSelection())
	{
		document.getElementById('page1').style.display = "none";
		document.getElementById('page2').style.display = "none";
		document.getElementById('page3').style.display = "block";
		document.getElementById('page4').style.display = "none";
		document.getElementById('page5').style.display = "none";
		document.getElementById('imgetape1').src = "resources/images/"+lang+"/step/etape01-off.png";
		document.getElementById('imgetape2').src = "resources/images/"+lang+"/step/etape02-off.png";
		document.getElementById('imgetape3').src = "resources/images/"+lang+"/step/etape03-on.png";
		document.getElementById('imgetape4').src = "resources/images/"+lang+"/step/etape04-off.png";
		document.getElementById('imgetape5').src = "resources/images/"+lang+"/step/etape05-off.png";
		
	}
}
function prev2()
{
	
	document.getElementById('page1').style.display = "none";
	document.getElementById('page2').style.display = "block";
	document.getElementById('page3').style.display = "none";
	document.getElementById('page4').style.display = "none";
	document.getElementById('page5').style.display = "none";
	document.getElementById('imgetape1').src = "resources/images/"+lang+"/step/etape01-off.png";
	document.getElementById('imgetape2').src = "resources/images/"+lang+"/step/etape02-on.png";
	document.getElementById('imgetape3').src = "resources/images/"+lang+"/step/etape03-off.png";
	document.getElementById('imgetape4').src = "resources/images/"+lang+"/step/etape04-off.png";
	document.getElementById('imgetape5').src = "resources/images/"+lang+"/step/etape05-off.png";
	return true;
	
}
function next3()
{
	if(validateformpin())
	{
		document.getElementById('page1').style.display = "none";
		document.getElementById('page2').style.display = "none";
		document.getElementById('page3').style.display = "none";
		document.getElementById('page4').style.display = "block";
		document.getElementById('page5').style.display = "none";
		document.getElementById('imgetape1').src = "resources/images/"+lang+"/step/etape01-off.png";
		document.getElementById('imgetape2').src = "resources/images/"+lang+"/step/etape02-off.png";
		document.getElementById('imgetape3').src = "resources/images/"+lang+"/step/etape03-off.png";
		document.getElementById('imgetape4').src = "resources/images/"+lang+"/step/etape04-on.png";
		document.getElementById('imgetape5').src = "resources/images/"+lang+"/step/etape05-off.png";
		viewprofile();
		return true;
	}
	
}
function prev3()
{
	document.getElementById('page1').style.display = "none";
	document.getElementById('page2').style.display = "block";
	document.getElementById('page3').style.display = "none";
	document.getElementById('page4').style.display = "none";
	document.getElementById('page5').style.display = "none";
	document.getElementById('imgetape1').src = "resources/images/"+lang+"/step/etape01-off.png";
	document.getElementById('imgetape2').src = "resources/images/"+lang+"/step/etape02-on.png";
	document.getElementById('imgetape3').src = "resources/images/"+lang+"/step/etape03-off.png";
	document.getElementById('imgetape4').src = "resources/images/"+lang+"/step/etape04-off.png";
	document.getElementById('imgetape5').src = "resources/images/"+lang+"/step/etape05-off.png";
	resetUck();startUck();
	return true;
	
}
function next4()
{

		document.getElementById('page1').style.display = "none";
		document.getElementById('page2').style.display = "none";
		document.getElementById('page3').style.display = "none";
		document.getElementById('page4').style.display = "none";
		document.getElementById('page5').style.display = "block";
		document.getElementById('imgetape1').src = "resources/images/"+lang+"/step/etape01-off.png";
		document.getElementById('imgetape2').src = "resources/images/"+lang+"/step/etape02-off.png";
		document.getElementById('imgetape3').src = "resources/images/"+lang+"/step/etape03-off.png";
		document.getElementById('imgetape4').src = "resources/images/"+lang+"/step/etape04-off.png";
		document.getElementById('imgetape5').src = "resources/images/"+lang+"/step/etape05-on.png";
	
		//RegisterUser
		registerUserFull();
		return true;
	
}

function registerUserFull()
{
	email = document.getElementById("email").value;
	jQuery.ajax( {
		url :"doCheckEmail.action?email=" + email,
		success : function(msg) {
			if (msg == "Existant_User") {
				document.getElementById('waitsignup').style.display = "none";
				document.getElementById('resultsignup').style.display = "block";
				document.getElementById('signup_exist').style.display = "block";
			}
			else
			{
				/*
				 * Register USER in IDBOX
				 */
				login = document.getElementById("login").value;
				 client = document.getElementById("client").value;
				email = document.getElementById("email").value;
				profil = document.getElementById("profil").value;
				profilname = document.getElementById("profilname").value;
				seldevicetmp2 = document.getElementById("seldevices").value;
				password=	 document.getElementById("pincode").value;
				
				var dna = new Dna();
				dna.setDescription(profilname);
				dna.setType(profil); //custom value for handling DNA categories
				dna.setDevices(selecteddevices);
				var uckJS = getUckApi();
				
				login=clientPrefixe+"_"+client+"_"+login;
				
				var userSetting = new UserSettings();
				userSetting.setEmail(email);
				userSetting.setPassword(password);
				uckJS.registerUser(login, userSetting, dna);
				if(uck.isException()) 
			    {
					uck.closeApi();
			    	document.getElementById('waitsignup').style.display = "none";
					document.getElementById('resultsignup').style.display = "block";
					document.getElementById('signup_error').style.display = "block";
			    	return;
			    }
				uck.closeApi();
			    $.post(
						"doSignup.action",
						$("#form").serialize(),
						function(responseText) {
							document.getElementById('waitsignup').style.display = "none";
							document.getElementById('resultsignup').style.display = "block";
							if (responseText == "OK")
							{
								document.getElementById('signup_success').style.display = "block";
							}
							else if(responseText=="ERROR")
							{
								document.getElementById('signup_error').style.display = "block";
							}
							else
							{
								document.getElementById('signup_error').style.display = "block";
							}
						});
		    }
	}});
			
}
function prev4()
{
	document.getElementById('page1').style.display = "block";
	document.getElementById('page2').style.display = "none";
	document.getElementById('page3').style.display = "none";
	document.getElementById('page4').style.display = "none";
	document.getElementById('page5').style.display = "none";
	document.getElementById('imgetape1').src = "resources/images/"+lang+"/step/etape01-off.png";
	document.getElementById('imgetape2').src = "resources/images/"+lang+"/step/etape02-off.png";
	document.getElementById('imgetape3').src = "resources/images/"+lang+"/step/etape03-off.png";
	document.getElementById('imgetape4').src = "resources/images/"+lang+"/step/etape04-on.png";
	document.getElementById('imgetape5').src = "resources/images/"+lang+"/step/etape05-off.png";
	return true;
	
}

function isInteger(s) 
	{
		return !isNaN(s);
	}

function validateformpin()
{
	pincode = document.getElementById("pincode").value;
	pincode2 = document.getElementById("pincode2").value;
	if(pincode=="")
	{
		jAlert(signup_str1,"Id4Yoo");
		return false;
	}
	if((pincode!=pincode2)||!isInteger(pincode))
	{
		jAlert(signup_str2,"Id4Yoo");
		return false;
	}
	if(pincode.length>50)
	{
		jAlert(big_pin,"Id4Yoo");
		return false;
	}
	return true;
}

function VerifField(login) {
	  var exp=new RegExp("^[a-zA-Z0-9]{3,50}$","g");
	  if ( exp.test(login) ) {
		  return true;
		  }
	  else {
		  return false; 
		  }
	}

function validateform1()
{
	login = document.getElementById("login").value;
	email = document.getElementById("email").value;
	pass = document.getElementById("pass").value;
	questionsecret = document.getElementById("questionsecrete").value;
	repQuestion = document.getElementById("repQuestion").value;
	CG = document.getElementById("CG").checked;
	if(login=="")
	{
		jAlert(signup_str4,"Id4Yoo");
		return false;
	}
	
	if(login.length>255)
	{
		jAlert(big_login,"Id4Yoo");
		return false;
	}
	
	 client = document.getElementById("client").value;
	uckJS = getUckApi();
	if(uck.isException()) {return;}
	///TODO: existDnaUSer
	login=clientPrefixe+"_"+client+"_"+login;
	
	if(uckJS.existDnaUser(login))
	{
		jAlert(signup_str4_2,"Id4Yoo");
		return false;
	}
	if(pass=="")
	{
		jAlert(signup_str5,"Id4Yoo");
		return false;
	}
	if(pass.length>50)
	{
		jAlert(big_pass,"Id4Yoo");
		return false;
	}
	if(email=="")
	{
		jAlert(signup_str3,"Id4Yoo");
		return false;
	}
	if(!checkemail(email))
	{
		jAlert(signup_str3_1,"Id4Yoo");
		return false;
	}
	if(email.length>50)
	{
		jAlert(big_email,"Id4Yoo");
		return false;
	}
	if(questionsecret==1 || questionsecret==14)
	{
		jAlert(signup_str6,"Id4Yoo");
		return false;
	}
	if(repQuestion=="")
	{
		jAlert(signup_str7,"Id4Yoo");
		return false;
	}
	if(repQuestion.length>50)
	{
		jAlert(big_response,"Id4Yoo");
		return false;
	}
	if(CG==false)
	{
		jAlert(signup_str8,"Id4Yoo");
		return false;
	}
	
	return true;
}

function viewprofile()
{
	login = document.getElementById("login").value;
	
	profil = document.getElementById("profil").value;
	profilname = document.getElementById("profilname").value;
	seldevicetemp = document.getElementById("seldevices").value;
	document.getElementById('sel_login').innerHTML = login;
	document.getElementById('sel_profiletype').innerHTML = ProfileType[profil];
	document.getElementById('sel_profilename').innerHTML = profilname;
	document.getElementById('sel_devices').innerHTML = seldevicetemp;
}

// ------------------------------------------------------------------ //   
// --------------- Functions used to handle actions ----------------- //
// ------------------------------------------------------------------ //

function startUck() {
	document.getElementById("waitdna").style.display="block";
	//start UCK and wait for the UckLoadedCallback callback to notify
	// the end of the applet execution and submit the form
	if (!uck.start())
		jAlert('Start UCK Error : ' + uck.error,"Id4Yoo");
}

function resetUck() {
	if (!uck.reset())
		jAlert('Reset UCK Error : ' + uck.error,"Id4Yoo");
}

function submitLoginForm() {
	//jAlert('Submit form !');
	// document.forms['loginform'].submit();
}
function redirectToRegister() {
	window.location = "showSignup.action";
	return false;
}
function redirecttoAdminTDB() {
	window.location = 'showAdminTDB.action';
	return false;
}
var selecteddevices=new Array();
function validateDeviceSelection()
{
	selecteddevices = uckSelectionBox.getSelection();
	
	if(selecteddevices.length==0)
	{
		jAlert(signup_str9,"Id4Yoo");
		return false;
	}
	profil = document.getElementById("profil").value;
	profilname = document.getElementById("profilname").value;
	if(profil==0)
	{
		jAlert(signup_str10,"Id4Yoo");
		return false;
	}
	if(profilname==0)
	{
		jAlert(signup_str11,"Id4Yoo");
		return false;
	}
	if(profilname.length>50)
	{
		jAlert(big_profilename,"Id4Yoo");
		return false;
	}
	var strdescription="";
	for(var i=0;i<selecteddevices.length;i++)
	{
		strdescription+=selecteddevices[i].getDescription()+"<br/>";
	}
	
	document.getElementById("seldevices").value=strdescription;
	uckSelectionBox.close();
	return true;
}

function checkemail(str) {
	
	var reg = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false;
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false;
	 }
	 if(reg.test(str) == false) {
	      return false;
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false;
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false;
	 }
	 if (str.indexOf(" ")!=-1){
	    return false;
	 }
		return true;				
}
