// Get CPU temp
var cpuReadCount;
CPU_timeRefresh = CPU_timeRefresh * 1000;
function cpu_temp() {
	lf('cpu_temp');
	Processes = GetProcesses2().toLowerCase();
	if (!fso.FolderExists(temp + '\\drp')) {
		try {
			fso.CreateFolder(temp + '\\drp');
			if (!fso.FolderExists(temp + '\\drp')) {
				log("Failed to create DRP dir");
			}
		}
		catch(e) {
			log("Failed to create DRP dir (expection)");
		}
	}
	if ((fso.FileExists(temp + '\\drp\\cpu.txt')) && (Processes.indexOf('cpuz') == -1)) {
		try {
			var tempfile = fso.OpenTextFile(temp + '\\drp\\cpu.txt', 1, false);
			cpu_txt = tempfile.ReadAll();
		}
		catch(e) {
			log("Failed to open "+temp + '\\drp\\cpu.txt');
			cpu_notRead();
			return;
		}
		tempfile.Close();

		//alert(cpu_txt);
		//alert(cpuReadCount);

		document.getElementById('cpu_temperature').innerHTML = cpu_parser(cpu_txt);
		document.getElementById('cpu_img').src = './tools/ico/6-white.png';
		if ((getCPUtemp)&& (!getCPUautorun)){
			setTimeout('cpu_get();',CPU_timeRefresh);
		}
		getCPUautorun = false;
	}
	else
		cpu_notRead();
}
function cpu_notRead(){
	lf('cpu_notRead');
	if ((cpuReadCount < 20) && (getCPUtemp))
		setTimeout('cpu_temp();',1000); cpuReadCount++;
}
var cpuInfo;
function cpu_parser(log) {
	lf('cpu_parser');
	log = log.substring(log.indexOf('Hardware Monitors'));
	log = log.substring(0,log.indexOf('Software'));
	cpuInfo = log;
	log = log.toLowerCase();

	var re = new RegExp("temperature [0-9][\\t]([0-9]{0,2})","mg");
	result = "";
	while (regResult = re.exec(log)) { result += RegExp.$1 + "° "; }
	return result;
}
function cpu_get() {
	lf('cpu_get');
	cpuReadCount = 0;
	if (document.getElementById('cpu_img').src.indexOf('indicator3') == -1){ document.getElementById('cpu_img').src = './tools/indicator3.gif'; }
	setTimeout('cpu_temp();',0);

	Processes = GetProcesses2().toLowerCase();
	if (Processes.indexOf('cpuz') == -1)
		setTimeout('WshShell.Run("cmd /C cd tools\\\\CPUz & start cpuz.exe -txt=%temp%\\\\drp\\\\cpu",0,false);',0);
}
function toogleCPUtemp() {
	lf('toogleCPUtemp');
	if (getCPUtemp){ getCPUtemp = false; }
	else { getCPUtemp = true; cpu_get(); }
}
if (getCPUtemp){
	var getCPUautorun = true;
	cpu_get();
}