//
// Universal Box javascript.
//

//var d=document;
//var thumb=null;

window.onresize=OnWindowResize;

$(window).load(function(){
	$("#content").fadeIn();
	setInterval(checkHiddenFrame, 500);
});

function checkHiddenFrame()
{
//	document.title+='.';
	if(window.frames[0])
	{
		var hresult = window.frames[0].document.getElementById('hresult');
		var himei = window.frames[0].document.getElementById('himei');
		var hhash = window.frames[0].document.getElementById('hhash');
		if(hresult && hresult.value != '')
		{
			$('#model').html(hresult.value);
			$('#imei').val(himei.value);
			$('#himei').val(himei.value);
			$('#hash').val(hhash.value);
			$('#hhash').val(hhash.value);
			hresult.value = '';
			himei.value = '';
			hhash.value = '';
			$('#one').slideDown( function() { $('#two').slideUp(); } );
			checkimeisoft();
			checkhashsoft();
		}
	}
}

function _(obj)
{
	return document.getElementById(obj);
}

function ShowWindow(title, what)
{
	var bgdiv = _("bgdiv");
	var thumb = _("thumb");
	if(thumb)
	{
		if(bgdiv)
		{
			bgdiv.style.left = 110;
			bgdiv.style.top = 110;
		
			//bgdiv.style.width = document.body.clientWidth;
			//bgdiv.style.height = document.body.clientHeight;
			bgdiv.style.display = 'block';
	        }
		
		thumb.style.left = String(document.body.clientWidth/2 - 200) + 'px';
		thumb.style.top = String(document.body.clientHeight/2 - 50) + 'px';
		thumb.style.visibility = "visible";
//		thumb.innerHTML='<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="400"><tr><td width="400" height="45" background="i/smallwindow_top.png"><table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" height="100%"><tr><td width="400" height="10" colspan="3"></td></tr><tr><td width="110" height="25"></td><td width="170" height="25" align="center"><p class="title">'+title+'</p></td><td width="120" height="25"></td></tr><tr><td width="400" height="10" colspan="3"></td></tr></table></td></tr><tr><td width="400" background="i/smallwindow_body.png"><table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%"><tr><td width="100%"><p class="news">' + what + '</p></td></tr><tr><td width="100%" align="center"><a href="#"><img border="0" src="i/b_ok.gif" onclick="javascript:HideWindow()"></a></td></tr></table></td></tr><tr><td width="40" height="20" background="i/smallwindow_bottom.png"></td></tr></table>';
		thumb.innerHTML='<div id="wnd"><div id="wndtitle"><p class="title" style="color: white;">'+title+'</p></div><div id="wndbody"><p class="news">' + what + '</p></div><div id="wndfooter"><input class="button" style="-moz-border-radius:5px;-webkit-border-radius:5px;" type="button" value="OK" onclick="javascript:HideWindow()" /></div></div>';
	}
}

function HideWindow()
{
	var bgdiv = _("bgdiv");
	if(bgdiv) bgdiv.style.display = 'none';
	var thumb = _("thumb");
	if(thumb) thumb.style.visibility = "hidden";
}

function OnWindowResize()
{
	var thumb = _("thumb");
	if(thumb)
	{
		thumb.style.left = String(document.body.clientWidth/2 - 200) + 'px';
		thumb.style.top = String(document.body.clientHeight/2 - 50) + 'px';
	}
}

function OpenWindow(fname, w, h)
{
	window.open('viewimage.php?filename=' + fname,'download','width=' + w + ',height=' + h + ',left=0,top=0,menubar=no,status=no,location=no,toolbar=no,scrollbars=no,resizable=no');
}

function Copy(sobj, tobj)
{
	var a = new Array();
	a = (sobj.options[sobj.selectedIndex]).value.split("|"); 
	tobj.value = a[1];
}

function mOver(obj)
{
	$("#"+obj.id+"_submenu").stop().fadeTo("fast", 1);
}

function mOut(obj)
{
	$("#"+obj.id+"_submenu").stop().fadeOut("fast");
}
    
function getField(sobj, i)
{
        var a = new Array();
        a = (sobj.options[sobj.selectedIndex]).value.split("|");
        return a[i];
}

function tg(objid)
{
	if($("#"+objid).style.display != 'none')
	{
		$("#"+objid).slideUp();
	}
	else
	{
		$("#"+objid).slideDown();
	}
}

function checkimeisoft()
{
	var re = new RegExp("^[0-9]+$");
	if($('#imei').val().length != 14 && $('#imei').val().length != 15)
	{
		$('#imei_error').html('<a title="IMEI must be 14 or 15 digits long!"><img src="/i/alert.png" /></a>');
		sweetTitles.init();
		return;
	}

	if(!($('#imei').val().match(re)))
	{
		$('#imei_error').html('<a title="IMEI must be numeric!"><img src="/i/alert.png" /></a>');
		sweetTitles.init();
		return;
	}

	$('#imei_error').html('<a title="IMEI syntax is correct!"><img src="/i/ok.png" /></a>');
	sweetTitles.init();
}

function checkimei(obj)
{
	checkimeisoft();
	var re = new RegExp("^[0-9]+$");

	if($('#imei').val().length != 14 && $('#imei').val().length != 15)
	{
		alert("IMEI length error!");
		return;
	}

	if(!($('#imei').val().match(re)))
	{
		alert("IMEI must be numeric!");
	}
	else
	{
		$('#model').html('Determining model, please wait...');
		$.ajax({
			url: "checkimei.php?imei=" + encodeURIComponent($('#imei').val()),
			success: function(msg){
				if(msg.substr(0, 6) == "ERROR=")
				{
					$('#model').html(msg.substr(6));
					alert(msg.substr(6));
				}
				else if(msg.substr(0, 6) == "MODEL=")
				{
					$('#model').html(msg.substr(6));
				}
			},
			error: function(){
				alert("AJAX error!");
			}
	        });
	}
}

function checkhashsoft()
{
	var re = new RegExp("^[0-9a-fA-F]+$");
	if($('#hash').val().length != 40)
	{
		$('#hash_error').html('<a title="HASH must be 40 characters long!"><img src="/i/alert.png" /></a>');
		sweetTitles.init();
		return;
	}
	
	if(!($('#hash').val().match(re)))
	{
		$('#hash_error').html('<a title="HASH must be hexadecimal string!"><img src="/i/alert.png" /></a>');
		sweetTitles.init();
		return;
	}
	$('#hash_error').html('<a title="HASH syntax is correct!"><img src="/i/ok.png" /></a>');
	sweetTitles.init();
}

function checkhash()
{
	checkhashsoft();
	var re = new RegExp("^[0-9a-fA-F]+$");
	
	if($('#hash').val().length != 40)
	{
		alert("Hash length error!");
		return;
	}
	
	if(!($('#hash').val().match(re)))
	{
		alert("Hash must be hexadecimal!");
	}
}

