var DCE_Router_AP=0;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function detect_new_vera(){
	new Ajax.Request("detect_vera.php",
	{
		method:"get",
		onSuccess: function(transport){
			if(transport.responseText.substr(0,2)=="OK"){
				var data=transport.responseText.split(",");
				if(data.length>=3){
					clearTimeout(redirectTimer);
					if(data.length==4){
						check_ip(data[1],data[2]);
					}else{
						show_ap_picker(data);
					}
				}else{
					setTimeout("detect_new_vera()",2000);
				}
			}else if(transport.responseText=='NO_RECORDS'){
				clearTimeout(redirectTimer);
				force_redirect(3);
			}else{
				setTimeout("detect_new_vera()",2000);
			}
		},
		onFailure: function(transport){
			setTimeout("detect_new_vera()",2000);
		}
	});
}

function check_ip(PK_AccessPoint,ipAddress){
	DCE_Router_AP=0;
	_console("DCE_Router_AP=0")
	document.getElementById("scan_info").innerHTML="<p class=\"subtitlu\">Please wait ...</p>Attempting to connect with Vera #"+PK_AccessPoint+".<br><br><input type=\"button\" value=\"Cancel\" onClick=\"self.location=\'scanning_vera2.php\'\"><br>";
	document.getElementById("ip").value=ipAddress;
	connectTimer=setTimeout("force_redirect(2)", 5000);

	try{
		//var obj=new JSONscriptRequest("http://"+ipAddress+"/cgi-bin/cmh/online_check.sh?tmp=1");
		var obj = new JSONScriptRequest();
  		obj.open('GET', "http://"+ipAddress+"/cgi-bin/cmh/online_check.sh?tmp=1");
		obj.onreadystatechange = function() {
			if (obj.readyState == 4) {
   				obj.buildScriptTag(); // Build the script tag
				obj.addScriptTag(); // Execute (add) the script tag
		   }else{
				DCE_Router_AP=PK_AccessPoint;
				var DCEobj = new JSONScriptRequest();
		  		DCEobj.open('GET', "http://"+ipAddress+"/cmh/online.html?tmp=1");
				DCEobj.onreadystatechange = function() {
					if (DCEobj.readyState == 4) {
		   				DCEobj.buildScriptTag(); // Build the script tag
						DCEobj.addScriptTag(); // Execute (add) the script tag
				   }				
				}
				DCEobj.send(null);	
		   }
		};
  		obj.send(null);		
		

	}catch(e){
		_console(e);
	}
}

function test(data){
	var PK_AccessPoint=(DCE_Router_AP==0)?parseInt(data.PK_AccessPoint):DCE_Router_AP;

	if(PK_AccessPoint!=0 && PK_AccessPoint!=NaN){
		self.location="mios_vera2.php?PK_AccessPoint="+PK_AccessPoint+"&ip="+document.getElementById("ip").value;
	}else{
		self.location="cant_connect.php?error=2";
	}
}

function force_redirect(flag){
	self.location="cant_connect.php?error="+flag;
}

function show_ap_picker(data){
	//multiple veras, show picker
	var html='<p class="subtitlu">I found more than 1 Vera on your network. Which one do you want to setup?</p><br>';
	html+='<table class="regular" cellpadding="3" cellspacing="1">';
	for(var i=1;i<data.length;i=i+3){
		html+='<tr>'+
		'	<td>#'+data[i]+'</td>'+
		'	<td>'+data[i+1]+'</td>'+
		'	<td><input type="button" class="button" onClick="check_ip('+data[i]+',\''+data[i+1]+'\')" value="Setup"></td>'+
		'</tr>';
	}
	html+='</table>';
	
	document.getElementById("scan_info").innerHTML=html;
}

function go_to_upgrade(PK_AccessPoint,oldVersion,ip,upgrade_file,upgradeParm,siteFrom){
	self.location='upgrade.php?ap='+PK_AccessPoint+'&version='+oldVersion+'&redir='+encodeURIComponent('http://'+ip+'/cgi-bin/cmh/upgrade_step2.sh?'+upgradeParm+'='+upgrade_file+'&AutomaticReturn='+siteFrom);
}

function _console(str){
	try{
		console.log(str);
	}catch(e){
		
	}	
}
