window.onload=function(){
	program(1,1,1);
	clock();
	var a=setTimeout('timer()',991);
	}



function reload(){
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById('footer').innerHTML=xmlhttp.responseText;
} }
xmlhttp.open("GET","embedded/pages/footer.php",true);
xmlhttp.send();
}


function new_scenario()
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() 
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById("new_scenario").innerHTML=xmlhttp.responseText;
document.getElementById('automation_menu_1').className= 'page_menu_button_automation_active';
document.getElementById('automation_menu_2').className= 'page_menu_button_automation';
document.getElementById('automation_menu_3').className= 'page_menu_button_automation';
document.getElementById("step1").style.visibility="visible";
document.getElementById("step2").style.visibility="hidden";
document.getElementById("list").innerHTML="";
document.getElementById("schedule").innerHTML="";
}
 }
xmlhttp.open("GET","embedded/pages/new_scenario.php",true);
xmlhttp.send();
 }


function scenario_list(str)
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() 
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById("list").innerHTML=xmlhttp.responseText;
document.getElementById('automation_menu_3').className= 'page_menu_button_automation_active';
document.getElementById('automation_menu_2').className= 'page_menu_button_automation';
document.getElementById('automation_menu_1').className= 'page_menu_button_automation';
document.getElementById("new_scenario").innerHTML="";
document.getElementById("schedule").innerHTML="";
if (str!=1)
	{
	document.getElementById("delete_msg").innerHTML=str;
	}
 }
 }
xmlhttp.open("GET","embedded/pages/scenario_list.php",true);
xmlhttp.send();
}

function schedule_scenario()
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() 
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ document.getElementById("schedule").innerHTML=xmlhttp.responseText;
document.getElementById('automation_menu_2').className= 'page_menu_button_automation_active';
document.getElementById('automation_menu_3').className= 'page_menu_button_automation';
document.getElementById('automation_menu_1').className= 'page_menu_button_automation';
document.getElementById("new_scenario").innerHTML="";
document.getElementById("list").innerHTML="";
 }
 }
xmlhttp.open("GET","embedded/pages/schedule_scenario.php",true);
xmlhttp.send();
}



//functions
function scenario_name_validator(str){
var xmlhttp;
var str2;
str2=0;
if (str.length==0) 
{ 
document.getElementById("1").innerHTML='<span class="invalid">Please fill in scenario name!</span>'; 
document.getElementById("sensor1").selectedIndex = 0;
document.getElementById("sensor1").disabled=true;
document.getElementById("line1_data").style.visibility="hidden";
return; 
}

if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
var r =  document.getElementById("scenario_name").value;
var found = r.search("#");
var found_0 = r.search("&");
if(found != -1 || found_0 != -1)
{
document.getElementById("1").innerHTML="<span class='invalid'>Invalid data!</span>";
document.getElementById("sensor1").selectedIndex = 0;
document.getElementById("sensor1").disabled=true;
document.getElementById("line1_data").style.visibility="hidden";
}
else{
document.getElementById("1").innerHTML=xmlhttp.responseText;
var b =  document.getElementById("1").innerHTML;
var foundb = b.search("Done!");
if (foundb==-1)
	{
	document.getElementById("sensor1").selectedIndex = 0;
	document.getElementById("sensor1").disabled=true;
	document.getElementById("line1_data").style.visibility="hidden";
	}
else
	{
	document.getElementById("sensor1").disabled=false;
	}
}}}


xmlhttp.open("GET","embedded/functions/scenario_name_validate.php?q="+str,true);
xmlhttp.send();
}

function select_rule(str,str2)
{
var xmlhttp;
var id;
str2=str2+1;
id="r"+str2;
if (str.length==0) { 
document.getElementById(id).innerHTML=""; 
return; }
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
if (str2<6)
	{
	document.getElementById(id).innerHTML=xmlhttp.responseText;
	}
	} }
xmlhttp.open("GET","embedded/functions/select_rule.php?q="+str+"&s="+str2,true);
xmlhttp.send();
}

function enable_sensor(str)
{
var rule;
rule="rule"+str;
var sensor;
sensor="sensor"+str;
var prev;
var x= document.getElementById(rule).value;
	if (x!="empty")
			{
			if (x=="none")
				{
				document.getElementById(sensor).disabled=true;
				next_field(str,9);	
				document.getElementById("next_step1").disabled=false;
				}
			else
				{
				document.getElementById(sensor).disabled=false;
				document.getElementById("next_step1").disabled=true;
				next_field(str,2);	
				}
			
			}
	else
			{
			document.getElementById("next_step1").disabled=true;
			if(document.getElementById(sensor).selectedIndex!=0)
				{
				var line_data="line"+str+"_data";
				document.getElementById(line_data).style.visibility="hidden";
				}
			document.getElementById(sensor).selectedIndex=0;
			document.getElementById(sensor).disabled=true;
				for (i=str; i<4; i++)
				{
					var n=i+1;
					var nrule="rule"+n;
					var nextline="line"+n;
					var nextsensor="sensor"+n;
					var nextlinedata="line"+n+"_data";
					if (document.getElementById(nextlinedata).style.visibility!="hidden")
						{
						document.getElementById(nrule).selectedIndex=0;
						document.getElementById(nrule).disabled=false;
						document.getElementById(nextsensor).selectedIndex=0;
						document.getElementById(nextsensor).disabled=false;
						document.getElementById(nextlinedata).style.visibility="hidden";
						}
				}
			}
}

function next_field(str,str2)
{
var i;
var current="sensor"+str;
if (str2==1)
{
if(document.getElementById(current).value=="no_sensor")
	{
	document.getElementById("next_step1").disabled=false;
	}
else
	{
	for (i=str+1;i<5;i++)
	{
	var nextsensor="sensor"+i;
	var rule="rule"+i;
	var nextline="line"+i;
	var nextlinedata="line"+i+"_data";
	document.getElementById(nextsensor).selectedIndex = 0;
	document.getElementById(nextsensor).disabled=true;
	if (document.getElementById(rule)!='undefined' && document.getElementById(rule)!=null)
		{
		document.getElementById(rule).disabled=true;
		}
	document.getElementById(nextline).style.visibility="hidden";
	document.getElementById(nextlinedata).style.visibility="hidden";
	}
	document.getElementById("next_step1").disabled=true;
	}
}
else if (str2==9)
	{
	for (i=str;i<5;i++)
	{
	var nextsensor="sensor"+i;
	var rule="rule"+i;
	var nextline="line"+i;
	var nextlinedata="line"+i+"_data";
	document.getElementById(nextsensor).selectedIndex = 0;
	document.getElementById(nextsensor).disabled=true;
	if (document.getElementById(rule)!='undefined' && document.getElementById(rule)!=null)
		{
		if(i!=str)
			{
			document.getElementById(rule).disabled=true;
			document.getElementById(rule).selectedIndex = 0;
			}
		}
	if(i!=str)
		{	
		document.getElementById(nextline).style.visibility="hidden";
		}
	document.getElementById(nextlinedata).style.visibility="hidden";
	}
	}
else
{
var i;
var k;
for (k=1;k<5;k++)
	{
	document.getElementById(current).options[k].disabled=false;
	}
for (i=str-1;i>0;i--)
	{
	var p_sensor="sensor"+i;
	var c=document.getElementById(p_sensor).selectedIndex;
	document.getElementById(current).options[c].disabled=true;
	}
}
}

function load_data(str,str2){
var xmlhttp;
var next;
var number;
var current_line_data;
current_line_data="line"+str2+"_data";
number=str2+1;
next="line"+number;
if (str.length==0) 
	{ 
	document.getElementById(current_line_data).innerHTML="";
	next_field(str2,1);
	return; 
	}
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(current_line_data).innerHTML=xmlhttp.responseText;
document.getElementById(current_line_data).style.visibility="visible";
next_field(str2,1);
}}
xmlhttp.open("GET","embedded/functions/load_data.php?q="+str+"&r="+str2,true);
xmlhttp.send();
}

function next_sensor(str3,id,str2)
{
	var str3;
	var id;
	var n;
	n=str3+1;
	if (isNaN(id)==false)
			{
			id="msg_"+id;
			}
	var b =  document.getElementById(id).innerHTML;
	var foundb = b.search("Done!");
if (str2==1)
	{	
		if (str3<4)
		{
		var line=str3+"_"+str3;
		if (document.getElementById(line)==null)
		{
		document.getElementById("p_i").insertRow(n);
		document.getElementById("p_i").rows[n].setAttribute("id",line);	
		}
		var xmlhttp;
		if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
		else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		document.getElementById("next_step1").disabled=true;
		var a=xmlhttp.responseText;
		document.getElementById(line).innerHTML=a;
		}
		}
		xmlhttp.open("GET","embedded/functions/select_rule.php?str3="+str3,true);
		xmlhttp.send();
		}
	else
		{
		if (foundb!=-1)
				{
				document.getElementById("next_step1").disabled=false;
				}
		}
	}
else if (str2==12)
	{
	delete_questions();
	document.getElementById("next_step1").disabled=false;
	}
else
	{
	if (str3!=5)
		{
		var rule;
		var sensor="sensor"+n;
		rule="rule"+n;
		var line="line"+n;
		var line_data="line"+n+"_data";
		if (foundb==-1)
				{
				document.getElementById(rule).selectedIndex=0;
				document.getElementById(sensor).selectedIndex=0;
				document.getElementById(rule).disabled=true;
				document.getElementById(line).style.visibility="hidden";
				document.getElementById(line_data).innerHTML="";
				document.getElementById(line_data).style.visibility="hidden";
				}
			else
				{
				document.getElementById(line).style.visibility="visible";
				document.getElementById(rule).disabled=false;
				}
			delete_questions();
			document.getElementById(sensor).disabled=true;
			document.getElementById("next_step1").disabled=true;
		}
	}
}

function temp1_validator(str,str3,str4,str5){
var xmlhttp;
var str2;
var str55;
id="msg_"+str3;
var iddd="line"+str3+"_data";
if (str5==1 || str5==3)
	{
	str55="temp2";
	}
else
	{
	str55="temp1";
	}
if (document.getElementById(str55).value!="")
	{
	str2=document.getElementById(str55).value;
	}
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
if (str.length==0)
{ 
	if (document.getElementById(str55).value=="") 
		{ 
		document.getElementById(id).innerHTML= ""; 
		}
	else 
		{
		document.getElementById(id).innerHTML="<span class='invalid'>Fill in temp"+str5+"!</span>";
		
		}
	if (str4==1 && str5!=3 && str5!=4)
	{
	next_sensor(str3,id,2);
	}
	else
	{
	document.getElementById("finish").disabled=true;
	}
if (str5!=3 && str5!=4)
{
delete_questions();
}
	return; 
}
var r =  document.getElementById("temp1").value;
var found = r.search("#");
var found_0 = r.search("&");
if(found != -1 || found_0 != -1)
{
document.getElementById(id).innerHTML="<span class='invalid'>Only numbers!</span>";
}
else
{
var a=xmlhttp.responseText;
document.getElementById(id).innerHTML=a;
var f = a.search("Done!");
	if (f!=-1)
	{
	if (str4==1 && str5!=3 && str5!=4)
		{
		next_sensor(str3,id,1);
		}
	else
		{
		document.getElementById("finish").disabled=false;
		}
	}
	else
	{
	if (str5!=3 && str5!=4)
		{
		delete_questions();
		}
	}	
}
}}
xmlhttp.open("GET","embedded/functions/temp_validate.php?q="+str+"&p="+str2+"&a="+1,true);
xmlhttp.send();
}


function hum1_validator(str,str3,str4,str5){
var xmlhttp;
var str2;
var id;
id="msg_"+str3;
var str55;
if (str5==1 || str5==3)
	{
	str55="hum2";
	}
else
	{
	str55="hum1";
	}
if (document.getElementById(str55).value!="")
	{
	str2=document.getElementById(str55).value;
	}
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
if (str.length==0)
{ 
	if (document.getElementById(str55).value=="") 
		{ 
		document.getElementById(id).innerHTML= ""; 
		}
	else 
		{
		document.getElementById(id).innerHTML="<span class='invalid'>Fill in level"+str5+"</span>";
		}
	if (str4==1 && str5!=3 && str5!=4)
	{
	next_sensor(str3,id,2);
	}
	else
	{
	document.getElementById("finish").disabled=true;
	}
		if (str5!=3 && str5!=4)
		{
		delete_questions();
		}
	return; 
}
var r =  document.getElementById("hum1").value;
var found = r.search("#");
var found_0 = r.search("&");
if(found != -1 || found_0 != -1)
{
document.getElementById(id).innerHTML="<span class='invalid'>Only numbers!</span>";
}
else
{
var a=xmlhttp.responseText;
document.getElementById(id).innerHTML=a;
var f = a.search("Done!");
	if (f!=-1)
	{
	if (str4==1 && str5!=3 && str5!=4)
		{
		next_sensor(str3,id,1);
		}
	else
		{
		document.getElementById("finish").disabled=false;
		}
	}
	else
	{
	if (str5!=3 && str5!=4)
		{
		delete_questions();
		}
	}	
}
}}
xmlhttp.open("GET","embedded/functions/hum_validate.php?q="+str+"&p="+str2+"&a="+2,true);
xmlhttp.send();
}

function inli1_validator(str,str3,str4,str5){
var xmlhttp;
var str2;
var str55;
id="msg_"+str3;
if (str5==1 || str5==3)
	{
	str55="inli2";
	}
else
	{
	str55="inli1";
	}
if (document.getElementById(str55).value!="")
	{
	str2=document.getElementById(str55).value;
	}
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
if (str.length==0)
{ 
	if (document.getElementById(str55).value=="") 
		{ 
		document.getElementById(id).innerHTML= ""; 
		}
	else 
		{
		document.getElementById(id).innerHTML="<span class='invalid'>Fill in level"+str5+"!</span>";
		}
	if (str4==1 && str5!=3 && str5!=4)
	{
	next_sensor(str3,id,2);
	}
	else
	{
	document.getElementById("finish").disabled=true;
	}
		if (str5!=3 && str5!=4)
		{
		delete_questions();
		}
	return; 
}
var r =  document.getElementById("inli1").value;
var found = r.search("#");
var found_0 = r.search("&");
if(found != -1 || found_0 != -1)
{
document.getElementById(id).innerHTML="<span class='invalid'>Only numbers!</span>";
}
else
{
var a=xmlhttp.responseText;
document.getElementById(id).innerHTML=a;
var f = a.search("Done!");
	if (f!=-1)
	{
	if (str4==1 && str5!=3 && str5!=4)
		{
		next_sensor(str3,id,1);
		}
	else
		{
		document.getElementById("finish").disabled=false;
		}
	}
	else
	{
	if (str5!=3 && str5!=4)
		{
		delete_questions();
		}
	}	
}
}}
xmlhttp.open("GET","embedded/functions/il_validate.php?q="+str+"&p="+str2+"&a="+1,true);
xmlhttp.send();
}

function sm_validator(str,str3,str4){
var xmlhttp;
var id;
id="msg_"+str3;
if (str.length==0) 
{ 
document.getElementById(id).innerHTML="";
 if (str4==1)
	{
	next_sensor(str3,id,2);
	}
	else
	{
	document.getElementById("finish").disabled=true;
	}
		if (str4!=3)
		{
		delete_questions();
		}
 return; 
 }
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
var a=xmlhttp.responseText;
document.getElementById(id).innerHTML=a;
var f = a.search("Done!");
	if (f!=-1)
	{
	if (str4==1)
		{
		next_sensor(str3,id,1);
		}
	else
		{
		document.getElementById("finish").disabled=false;
		}
	}
	else
	{
	if (str4!=3)
		{
		delete_questions();
		}
	}	
}}
xmlhttp.open("GET","embedded/functions/sm_validator.php?q="+str,true);
xmlhttp.send();
}

function step2(str)
{
var scenario_name= document.getElementById("scenario_name").value;
var i;
var f_url="";
for (i=1; i<6; i++)
	{
	var url;
	var sensor="sensor"+i;
	var n=i+1;
	var rule="rule"+n;
	var nextlinedata="line"+i+"_data";
	var nextsensor="sensor"+n;
		if (document.getElementById(nextlinedata).style.visibility=="visible")
		{
			var sen="sen"+i;
			if (document.getElementById(sensor).value=="temperature")
				{
				var temp1=document.getElementById("temp1").value;
				var temp2=document.getElementById("temp2").value;
				if (document.getElementById(nextsensor).value=="no_sensor" || document.getElementById(nextsensor).value=="")
					{
					var temp_rule="no";
					}
				else
					{
					var temp_rule=document.getElementById(rule).value;
					}
				url="&"+sen+"="+"t"+"&temp1="+temp1+"&temp2="+temp2+"&temp_rule="+temp_rule;
				}
			else if (document.getElementById(sensor).value=="humidity")
				{
				var hum1=document.getElementById("hum1").value;
				var hum2=document.getElementById("hum2").value;
				if (document.getElementById(nextsensor).value=="no_sensor" || document.getElementById(nextsensor).value=="")
					{
					var hum_rule="no";
					}
				else
					{
					var hum_rule=document.getElementById(rule).value;
					}
				url="&"+sen+"="+"h"+"&hum1="+hum1+"&hum2="+hum2+"&hum_rule="+hum_rule;
				}
			else if (document.getElementById(sensor).value=="indoor_lighting")
				{
				var inli1=document.getElementById("inli1").value;
				var inli2=document.getElementById("inli2").value;
				if (document.getElementById(nextsensor).value=="no_sensor" || document.getElementById(nextsensor).value=="")
					{
					var inli_rule="no";
					}
				else
					{
					var inli_rule=document.getElementById(rule).value;
					}
				url="&"+sen+"="+"i"+"&inli1="+inli1+"&inli2="+inli2+"&inli_rule="+inli_rule;
				}
			else if (document.getElementById(sensor).value=="soil_moisture")
				{
				var somo1=document.getElementById("soil_moisture").value;
				if (document.getElementById(nextsensor).value=="no_sensor" || document.getElementById(nextsensor).value=="")
					{
					var somo_rule="no";
					}
				else
					{
					var somo_rule=document.getElementById(rule).value;
					}
				url="&"+sen+"="+"s"+"&somo="+somo1+"&somo_rule="+somo_rule;
				}
			else
				{
				url="&"+sen+"="+"n";
				}
			
			f_url=f_url+url;
		}
	}

var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("step2").style.visibility="visible";
document.getElementById("step1").style.visibility="hidden";
delete_questions();
visibility(1);
document.getElementById("step2").innerHTML=xmlhttp.responseText;
} }
xmlhttp.open("GET","embedded/functions/step2.php?a="+scenario_name+f_url,true);
xmlhttp.send();
}


function visibility(str)
{
var i;
for (i=1;i<6;i++)
	{
	var line="line"+i;
	var line_data="line"+i+"_data";
	var sensor="sensor"+i;
	var rule="rule"+i;
	if (str==1)
		{
		if (document.getElementById(sensor).disabled==false)
				{
				document.getElementById(line).style.visibility="hidden";
				document.getElementById(line_data).style.visibility="hidden";
				}
		else
			{
			if (document.getElementById(line).style.visibility=="visible")
				{
				if (document.getElementById(rule).disabled==false)
						{
						document.getElementById(line).style.visibility="hidden";
						document.getElementById(line_data).style.visibility="hidden";
						break;
						}
				}
			document.getElementById("step1").style.visibility="hidden";
			document.getElementById("step2").style.visibility="visible";
			}
		}
	else
		{
		if (document.getElementById(sensor).disabled==false)
			{
			document.getElementById(line).style.visibility="visible";
			document.getElementById(line_data).style.visibility="visible";
			}
		else
			{
			if (document.getElementById(rule)!='undefined' && document.getElementById(rule)!=null)
			{
				if (document.getElementById(rule).disabled==false)
					{
					document.getElementById(line).style.visibility="visible";
					}
			}	
			}
		document.getElementById("step2").innerHTML="";
		document.getElementById("step2").style.visibility="hidden";
		document.getElementById("step1").style.visibility="visible";
		}
	}
	
}

function enablef(str,str1)
{
var str3=document.getElementById("s_n").innerHTML;
var xmlhttp;
if (str ==1)
	{
	str="f";
	}
else if (str==2)
	{
	str="l";
	}
else
	{
	str="w";
	}
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
document.getElementById('table_index').innerHTML=xmlhttp.responseText;
} }
xmlhttp.open("GET","embedded/functions/scenario_devices.php?"+str+"="+str1+"&a="+str3,true);
xmlhttp.send();
}

function month(str,str2)
{
var str1;
if (str==111 || str==222)
	{
	str1=document.getElementById("viewed_month").innerHTML;
	}
else
	{
	str1="1,0";
	}
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
document.getElementById("marginn").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","embedded/functions/month.php?q="+str+"&p="+str1+"&y="+str2,true);
xmlhttp.send();
}

function day_info(str,str1,str2)
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
document.getElementById("place").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","embedded/functions/info_day.php?m="+str+"&y="+str1+"&d="+str2,true);
xmlhttp.send();
}

function info_day_button(str,str1,str2,str3)
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if (str==1)
	{
	document.getElementById("day_list").innerHTML="";
	document.getElementById("day_list").innerHTML=xmlhttp.responseText;
	document.getElementById("add_scenario_button").disabled=true;
	} 
else
	{
	document.getElementById("place").innerHTML="";
	document.getElementById("marginn").innerHTML="";
	month(str2,str1);
	}
program(1,1,0);
}
}
xmlhttp.open("GET","embedded/functions/add_scenario.php?m="+str2+"&y="+str1+"&d="+str3,true);
xmlhttp.send();
}

function scenario_status_edit (str,str1,str3,str4,str5,str6)
{
var xmlhttp;
if (str6==12)
	{
	str6=document.getElementById("disable_frequency").value;
	}
else if (str6==11)
	{
	str6=document.getElementById("delete_frequency").value;
	}

if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var a=xmlhttp.responseText;
document.getElementById("ena").innerHTML=a;
}
}
xmlhttp.open("GET","embedded/functions/scenario_status.php?q="+str+"&p="+str1+"&e="+str3+"&d="+str4+"&f="+str5+"&df="+str6,true);
xmlhttp.send();
}

function enable_edit(str)
{
if (str!="")
	{
	document.getElementById('edit').disabled=false;
	}
else
	{
	document.getElementById('edit').disabled=true;
	}
}


function time_validator(str,str1,str2,str3,str4,str5)
{
if (str==3)
	{
	var s=document.getElementById("scenario_select").value;
	var s_h=document.getElementById("t1").value;
	var s_m=document.getElementById("t2").value;
	var e_h=document.getElementById("t3").value;
	var e_m=document.getElementById("t4").value;
	var f=document.getElementById("frequency").value;
	var f_s=document.getElementById("frequency_stop").value;
	if(document.getElementById("frequency_stop").disabled==true)
		{
		f_s=1;
		}
	if (str1==1)
		{
		var xmlhttp;
		if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
		else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		var a=xmlhttp.responseText;
		document.getElementById("day_notes").innerHTML=a;
		var found = a.search("Done!");
		if (found!=-1)
			{
			day_info(str4,str3,str5);
			}
		}
		}
		xmlhttp.open("GET","embedded/functions/time_validate.php?s="+s+"&s_h="+s_h+"&s_m="+s_m+"&e_h="+e_h+"&e_m="+e_m+"&f="+f+"&f_s="+f_s+"&y="+str3+"&m="+str4+"&d="+str5,true);
		xmlhttp.send();
		}
	else if (str1==3)
		{
		if (f==6)
			{
			document.getElementById("cc").innerHTML="-";
			document.getElementById("frequency_stop").disabled=true;
			
			}
		else
			{
			document.getElementById("cc").innerHTML="For";
			document.getElementById("frequency_stop").disabled=false;
			}
		}
	else	
		{
		if (str2==2)
			{
			day_info(str4,str3,str5);
			}
		else
			{
			document.getElementById("scenario_select").selectedIndex = 0;
			document.getElementById("t1").selectedIndex = 0;
			document.getElementById("t2").selectedIndex = 0;
			document.getElementById("t3").selectedIndex = 0;
			document.getElementById("t4").selectedIndex = 0;
			document.getElementById("frequency").selectedIndex = 0;
			document.getElementById("frequency_stop").selectedIndex = 0;
			}
		}

	}
program(1,1,0);
}


function save_scenario(str,str1)
{
var xmlhttp;
if (str==3 || str==4)
	{
	var str3=document.getElementById("scenario_list_select").value;
	}
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var a=xmlhttp.responseText;
if (str==3 || str==4)
	{
	appear_scn(str3,a);
	}
else
	{	
	document.getElementById("step1").innerHTML="";
	document.getElementById("step2").innerHTML="";
	document.getElementById("step2").innerHTML=a;
	}
}
}
xmlhttp.open("GET","embedded/functions/save_scenario.php?q="+str+"&p="+str1,true);
xmlhttp.send();
}

function appear_scn(str,str2)
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{

if(document.getElementById("delete_msg").innerHTML!="" && str!="")
	{
	document.getElementById("delete_msg").innerHTML="";
	}
var a=xmlhttp.responseText;
document.getElementById("summarize").innerHTML=a;
if (str2!=1)
	{
	document.getElementById("edit_msg").innerHTML=str2;
	document.getElementById("edit_msg").className="vision11";
	}
}
}
xmlhttp.open("GET","embedded/functions/scenario_appear.php?q="+str,true);
xmlhttp.send();
}

function enable1f(str,str1,str2)
{
var xmlhttp;
if (str ==1)
	{
	str="f";
	}
else if (str==2)
	{
	str="l";
	}
else
	{
	str="w";
	}
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
document.getElementById('summarize').innerHTML=xmlhttp.responseText;
} }
xmlhttp.open("GET","embedded/functions/scenario_appear.php?"+str+"="+str1+"&q="+str2+"&sd=11",true);
xmlhttp.send();
}

function edit_scenario(str,str1)
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var a=xmlhttp.responseText;
if (str!=2)
	{
document.getElementById("summarize").innerHTML=a;
	}
else
	{
	scenario_list(a);
	}
}
}
xmlhttp.open("GET","embedded/functions/scenario_appear.php?sd="+str+"&q="+str1,true);
xmlhttp.send();
}

function clock(){
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
document.getElementById("clock").innerHTML=xmlhttp.responseText;
} }
xmlhttp.open("GET","embedded/functions/clock.php",true);
xmlhttp.send();
}

function timer()
{

h=document.getElementById("h").innerHTML;
m=document.getElementById("m").innerHTML;
s=document.getElementById("s").innerHTML;
s++;
if(s<10)
	{
	s="0"+s;
	}
if (s==60)
	{
	s="00";
	m++;
	if(m<10)
		{
		m="0"+m;
		}
	document.getElementById("m").innerHTML=m;
	reload();
	program(1,1,1);
	}
if (m==60)
	{
	m="00";
	h++;
		if(h<10)
		{
		h="0"+h;
		}
	document.getElementById("h").innerHTML=h;
	clock();
	
	}
document.getElementById("s").innerHTML=s;	
	

setTimeout("timer()",991);
}

function delete_questions()
{
var i;
var line;
for (i=1;i<4;i++)
{
line=i+"_"+i;
if (document.getElementById(line)!='undefined' && document.getElementById(line)!=null)
	{
	var row = document.getElementById(line);
	row.parentNode.removeChild(row);
	}

}
}

function change_condition(str,str1,str2,str3)
{
if(str==str2)
	{
	var a;
	var b;
	var c;
	if (str==1)
		{
		a=document.getElementById("temp1").value;
		b=document.getElementById("temp2").value;
		if (document.getElementById("temp_rule")!='undefined' && document.getElementById("temp_rule")!=null)
			{
			c=document.getElementById("temp_rule").value;
			}
		else
			{
			c="";
			}
		}
	else if (str==2)
		{
		a=document.getElementById("hum1").value;
		b=document.getElementById("hum2").value;
		if (document.getElementById("hum_rule")!='undefined' && document.getElementById("hum_rule")!=null)
			{
			c=document.getElementById("hum_rule").value;
			}
		else
			{
			c="";
			}
		}
	else if(str==3)
		{
		a=document.getElementById("soil_moisture").value;
		b="somo";
		if (document.getElementById("somo_rule")!='undefined' && document.getElementById("somo_rule")!=null)
			{
			c=document.getElementById("somo_rule").value;
			}
		else
			{
			c="";
			}
		}
	else
		{
		a=document.getElementById("inli1").value;
		b=document.getElementById("inli2").value;
		if (document.getElementById("inli_rule")!='undefined' && document.getElementById("inli_rule")!=null)
			{
			c=document.getElementById("inli_rule").value;
			}
		else
			{
			c="";
			}
		}
	str2=a+"_"+b+"_"+c;
	}
	var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var a=xmlhttp.responseText;
document.getElementById("summarize").innerHTML=a;
}
}
xmlhttp.open("GET","embedded/functions/scenario_appear.php?e_s="+str+"&sd="+str3+"&q="+str1+"&a="+str2,true);
xmlhttp.send();
}

function logout(str)
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var aa=xmlhttp.responseText;
if (str==1)
	{
	var c=confirm(aa);
	if (c==true)
		{
		logout(2);
		}
	}
else if(str==2)
	{
	window.location.href='index.php';
	}
else
	{
	window.location.href='automation.php';
	}
} }
xmlhttp.open("GET","embedded/functions/log_out.php?a="+str,true);
xmlhttp.send();
}

function program(str,str2,str3)
{
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{ 
document.getElementById("programm").innerHTML=xmlhttp.responseText;
} }
xmlhttp.open("GET","embedded/functions/programm.php?a="+str+"&s="+str2+"&x="+str3,true);
xmlhttp.send();
}

function popup(str)
{
	if (str==1)
		{
		document.getElementById("s_msg").style.visibility="visible";
		}
	else
		{
		document.getElementById("s_msg").style.visibility="hidden";
		}
	
}