var emname;
console.log("emcontrol");

function emnamecatch(name){
    console.log("emnamecatch");
    emname=name;
    var ajax=getAjax();

  //  ajax.open("GET","//localhost:9090/"+emname+"/init",true);

    $.ajax({
      url: 'http://localhost:9090/fei/init',
      type: "GET",
      success: function(res){
        console.log("success");
      }
    });

    console.log(emname);
  }

function emcondition(command) {

var ajax=getAjax();

    if (command == "request") {
        var position1 = document.getElementById("Position");
        var GonioX = document.getElementById("GonioX");
        var GonioY = document.getElementById("GonioY");
        var GonioZ = document.getElementById("GonioZ");
        var GonioA = document.getElementById("GonioA");
        var tem = document.getElementById("tem");
        var SpotSizeIndex = document.getElementById("SpotSizeIndex");
        var MagIndex = document.getElementById("MagIndex");
        var Obj = document.getElementById("Obj");
        var BeamTiltX = document.getElementById("BeamTiltX");
        var BeamTiltY = document.getElementById("BeamTiltY");
        var Speed = document.getElementById("Speed");
        var position2 = document.getElementById("Position2");
        var GonioX2 = document.getElementById("GonioX2");
        var GonioY2 = document.getElementById("GonioY2");
        var GonioZ2 = document.getElementById("GonioZ2");
        var GonioA2 = document.getElementById("GonioA2");
        var tem2 = document.getElementById("tem2");
        var SpotSizeIndex2 = document.getElementById("SpotSizeIndex2");
        var MagIndex2 = document.getElementById("MagIndex2");
        var Obj2 = document.getElementById("Obj2");
        var BeamTiltX2 = document.getElementById("BeamTiltX2");
        var BeamTiltY2 = document.getElementById("BeamTiltY2");
        var Speed2 = document.getElementById("Speed2");
        var position3 = document.getElementById("Position3");
        var GonioX3 = document.getElementById("GonioX3");
        var GonioY3 = document.getElementById("GonioY3");
        var GonioZ3 = document.getElementById("GonioZ3");
        var GonioA3 = document.getElementById("GonioA3");
        var tem3 = document.getElementById("tem3");
        var SpotSizeIndex3 = document.getElementById("SpotSizeIndex3");
        var MagIndex3 = document.getElementById("MagIndex3");
        var Obj3 = document.getElementById("Obj3");
        var BeamTiltX3 = document.getElementById("BeamTiltX3");
        var BeamTiltY3 = document.getElementById("BeamTiltY3");
        var Speed3 = document.getElementById("Speed3");
    } else {
        var position1 = document.anchorsgetElementById("result");
        var position2 = document.anchorsgetElementById("result");
        var position3 = document.anchorsgetElementById('result');
    }

    if (ajax) {
        try {
            ajax.onreadystatechange = getData;
            console.log("getData");
        } catch (e) {
            position1.innerHTML = "Onreadystate change error.";
        }
        try {
            ajax.open("GET", "//localhost:9090/"+encodeURIComponent(emname)+"/get", true);
            //console.log("//localhost:9090/"+encodeURIComponent(emname)+"/get");
        } catch (e) {
            position1.innerHTML = "ajax open error";
        }
        try {
            ajax.send(null);
        } catch (e) {
            position1.innerHTML = "send error";
        }

    } else {
        position1.innerHTML = "you cannot use ajax....";
    }

    function getData() {
        if (ajax.readyState == 4) {
            if (ajax.status == 200) {
                console.log("Hello!");
                var resultjson = ajax.responseText;
                console.log(resultjson);
                resultjson = JSON.parse(resultjson);
                console.log(resultjson);
                console.log(typeof resultjson);
                /*
                 for (key in resultjson) {
                 console.log(key+":"+resultjson(key));
                 }
                 */
                //position1.innerHTML=resultjson;
                console.log(resultjson['GonioX']);//undefined
                //tem.innerHTML = resultjson["TEM"];
                GonioX.innerHTML = resultjson["x"];
                GonioY.innerHTML = resultjson['y'];
                GonioZ.innerHTML = resultjson['z'];
                GonioA.innerHTML = resultjson['a'];
                SpotSizeIndex.innerHTML = resultjson['SpotSizeIndex'];
                MagIndex.innerHTML = resultjson['MagIndex'];
                Obj.innerHTML = resultjson['Obj'];
                BeamTiltX.innerHTML = resultjson['BeamTiltX'];
                BeamTiltY.innerHTML = resultjson['BeamTiltY'];
                Speed.innerHTML = resultjson['Speed'];
                //tem2.innerHTML = resultjson["TEM"];
                GonioX2.innerHTML = resultjson["x"];
                GonioY2.innerHTML = resultjson['y'];
                GonioZ2.innerHTML = resultjson['z'];
                GonioA2.innerHTML = resultjson['a'];
                SpotSizeIndex2.innerHTML = resultjson['SpotSizeIndex'];
                MagIndex2.innerHTML = resultjson['MagIndex'];
                Obj2.innerHTML = resultjson['Obj'];
                BeamTiltX2.innerHTML = resultjson['BeamTiltX'];
                BeamTiltY2.innerHTML = resultjson['BeamTiltY'];
                Speed2.innerHTML = resultjson['Speed'];
                //tem3.innerHTML = resultjson["TEM"];
                GonioX3.innerHTML = resultjson["x"];
                GonioY3.innerHTML = resultjson['y'];
                GonioZ3.innerHTML = resultjson['z'];
                GonioA3.innerHTML = resultjson['a'];
                SpotSizeIndex3.innerHTML = resultjson['SpotSizeIndex'];
                MagIndex3.innerHTML = resultjson['MagIndex'];
                Obj3.innerHTML = resultjson['Obj'];
                BeamTiltX3.innerHTML = resultjson['BeamTiltX'];
                BeamTiltY3.innerHTML = resultjson['BeamTiltY'];
                Speed3.innerHTML = resultjson['Speed'];

                //   result.innerHTML = ajax.responseText;
            } else {
                position1.innerHTML = "HTTP transmission....";
                position2.innerHTML = "HTTP transmission....";
                position3.innerHTML = "HTTP transmission....";
            }
        }
    }
}

function stagecontrol(command,absolutex,absolutey,absolutez,absolutea) {

 var ajax=getAjax();

 if(command == "stage"){
	var result = document.getElementById("stage");
        var result2 = document.getElementById("stagetomo");
        var result3 = document.getElementById("stagemds");
        console.log('move');
	}else {
		var result = document.getElementById("stage");
                var result2 = document.getElementById("stagetomo");
                var result3 = document.getElementById("stagemds");
	}

	if (ajax) {
	/*	 try{
			 ajax.onreadystatechange = getData;
		 }catch(e){
			result.innerHTML="Onreadystate change error.";
                        result2.innerHTML="Onreadystate change error.";
                        result3.innerHTML="Onreadystate change error.";
		 }*/

		 try{
			ajax.open("GET","//localhost:9090/"+encodeURIComponent(emname)+"/set/stagea?x="+encodeURIComponent(absolutex)+"&y="+encodeURIComponent(absolutey)+"&z="+encodeURIComponent(absolutez)+"&a="+encodeURIComponent(absolutea),true);
			//document.getElementById("mid").innerHTML="absolutex="+absolutex+" absolutey="+absolutey;
		 }catch(e){
			result.innerHTML = "ajax open error";
                        result2.innerHTML = "ajax open error";
                        result3.innerHTML = "ajax open error";

		 }

		try {
			 ajax.send(null);
		}catch (e) {
			result.innerHTML ="send error";
                        result2.innerHTML ="send error";
                        result3.innerHTML ="send error";
		}

	}else {
		result.innerHTML = "you cannot use ajax....";
                result2.innerHTML = "you cannot use ajax....";
                result3.innerHTML = "you cannot use ajax....";
	}

	function getData() {
			if (ajax.readyState==4) {
					if (ajax.status==200) {
							 result.innerHTML = ajax.responseText;
                                                         result2.innerHTML = ajax.responseText;
                                                         result3.innerHTML = ajax.responseText;
						 }else {
							 result.innerHTML = "HTTP transmission....";
                                                         result2.innerHTML = "HTTP transmission....";
                                                         result3.innerHTML = "HTTP transmission....";
						}
			 }
	}
 }

 function objcontrol(command,obj) {

 var ajax=getAjax();

 if(command == "obj"){
	var result = document.getElementById("obj");
        var result2 = document.getElementById("objtomo");
        var result3 = document.getElementById("objmds");
        console.log('move');
	}else {
		var result = document.getElementById("obj");
                var result2 = document.getElementById("objtomo");
                var result3 = document.getElementById("objmds");
	}

	if (ajax) {
		 try{
			 ajax.onreadystatechange = getData;
		 }catch(e){
			result.innerHTML="Onreadystate change error.";
                        result2.innerHTML="Onreadystate change error.";
                        result3.innerHTML="Onreadystate change error.";
		 }

		 try{
			ajax.open("GET","//localhost:9090/"+encodeURIComponent(emname)+"/set/obj?obj="+encodeURIComponent(obj),true);
			//document.getElementById("mid").innerHTML="absolutex="+absolutex+" absolutey="+absolutey;
		 }catch(e){
			result.innerHTML = "ajax open error";
                        result2.innerHTML = "ajax open error";
                        result3.innerHTML = "ajax open error";

		 }

		try {
			 ajax.send(null);
		}catch (e) {
			result.innerHTML ="send error";
                        result2.innerHTML ="send error";
                        result3.innerHTML ="send error";
		}

	}else {
		result.innerHTML = "you cannot use ajax....";
                result2.innerHTML = "you cannot use ajax....";
                result3.innerHTML = "you cannot use ajax....";
	}

	function getData() {
			if (ajax.readyState==4) {
        console.log("getData2")
					if (ajax.status==200) {
							 result.innerHTML = ajax.responseText;
                                                         result2.innerHTML = ajax.responseText;
                                                         result3.innerHTML = ajax.responseText;
						 }else {
							 result.innerHTML = "HTTP transmission....";
                                                         result2.innerHTML = "HTTP transmission....";
                                                         result3.innerHTML = "HTTP transmission....";
						}
			 }
	}
 }
