/*
*	AUTHOR: 		GILEMON for nThinking HK ltd.
*	CREATED:		1.21.2008
*	UPDATED:		2.12.2008
*	VERSION:		1.0.2
*
*	LICENSE:		THIS IS AN OPEN SOURCE API. YOU ARE FREE TO USE THIS API IN ANY APPLICATION,
*               	TO COPY IT OR MODIFY THE FUNCTIONS FOR YOUR OWN NEEDS, AS LONG THIS HEADER INFORMATION
*              	 	REMAINS IN TACT AND YOU DON'T CHARGE ANY MONEY FOR IT. USE THIS API AT YOUR OWN
*               	RISK. NO WARRANTY IS EXPRESSED OR IMPLIED, AND NO LIABILITY ASSUMED FOR THE RESULT OF
*               	USING THIS API.
*
*	for more information visit
*	http://nthinking.net/miss/lab.html
*/
var host  = 'http://www.utyp.net/';
var req  = host + 'getwww-json.php';

function addScript(url) 
{
    var script = document.createElement('script');
	script.src = url;
	document.getElementsByTagName('head')[0].appendChild(script); //fix for ie7
    //document.body.appendChild(script);
}

function callback(jsonData) 
{ 
	 updatepage(jsonData.result);
}

function callback2(jsonData) 
{ 
	 updatepage2(jsonData.result);
}

//first call
addScript(req);


function updatepage(str)
{
	displayB = "submit";
	if(typeof( pictchadisplay ) != "undefined")
	{
		displayB = pictchadisplay;
	}
	htmlToAdd = '<a href="http://utyp.net" target="utyp">Pictcha</a> Verification.<br>';
	htmlToAdd += '<br><div id="loading"></div><IMG SRC="' +str+ '"><br>';
	htmlToAdd += '<br>Select your language: <select tabindex="0" id="ulg" name="ulg"><option value="en" selected="">English</option><option value="fr">French</option><option value="es">Spanish</option><option value="af">Afrikaans</option><option value="sq">Albanian</option><option value="ar">Arabic</option><option value="be">Belarusian</option><option value="bg">Bulgarian</option><option value="ca">Catalan</option><option value="zh-CN">Chinese</option><option value="hr">Croatian</option><option value="cs">Czech</option><option value="da">Danish</option><option value="nl">Dutch</option><option value="en">English</option><option value="et">Estonian</option><option value="tl">Filipino</option><option value="fi">Finnish</option><option value="fr">French</option><option value="gl">Galician</option><option value="de">German</option><option value="el">Greek</option><option value="iw">Hebrew</option><option value="hi">Hindi</option><option value="hu">Hungarian</option><option value="is">Icelandic</option><option value="id">Indonesian</option><option value="ga">Irish</option><option value="it">Italian</option><option value="ja">Japanese</option><option value="ko">Korean</option><option value="lv">Latvian</option><option value="lt">Lithuanian</option><option value="mk">Macedonian</option><option value="ms">Malay</option><option value="mt">Maltese</option><option value="no">Norwegian</option><option value="fa">Persian</option><option value="pl">Polish</option><option value="pt">Portuguese</option><option value="ro">Romanian</option><option value="ru">Russian</option><option value="sr">Serbian</option><option value="sk">Slovak</option><option value="sl">Slovenian</option><option value="es">Spanish</option><option value="sw">Swahili</option><option value="sv">Swedish</option><option value="th">Thai</option><option value="tr">Turkish</option><option value="uk">Ukrainian</option><option value="vi">Vietnamese</option><option value="cy">Welsh</option><option value="yi">Yiddish</option></select>';
	htmlToAdd += '<br><br>Question: <a href="javascript:openFrameless()">What</a>\'s this?<br><br><input type="text" id="llna" name="llna" SIZE="10" maxlength="60" onkeypress="catchEnter(event);"><input type="hidden" name="llur" id="llur" value="'+str+'"><br><br><input type="button" value="'+displayB+'" onClick="verify();" onkeypress="catchEnter(event);">';
	document.getElementById("input").innerHTML = htmlToAdd;
	document.getElementById("llna").focus();
	lg = readCookie("pictcha_lg");
	if(lg)
	{
		select_lang(lg);
	}
}
function updatepage2(str)
{
	//alert(str);
    document.getElementById("result").innerHTML = "";
	if(str.match("&found=1"))
	{
		document.getElementById("loading").innerHTML = "";
		document.getElementById("result").innerHTML += "OK!<br> You are human.";
		if(typeof( pictchafunction ) != "undefined")
		{
			eval(pictchafunction);
		}
		else
		{
			pictchaform.submit();
		}
	}
	else
	{
		addScript(req + '?r=' + Math.random());
		document.getElementById("result").innerHTML += "Wrong, Try Again...<br> Are you a robot?";
	}
}

function verify() 
{ 
	if (document.getElementById("llna").value == "")
	{
		alert("Please type something");
    }
	else
	{
		scripta = host +"check-json.php?llna="+document.getElementById("llna").value+"&llur="+document.getElementById("llur").value+"&lg="+document.getElementById("ulg").value;
		//alert(scripta);
		addScript(scripta);
		createCookie("pictcha_lg",document.getElementById("ulg").value,30);
		document.getElementById("loading").innerHTML = "<br><br>Please wait<br><br><img src='http://utyp.net/images/loading.gif'><br><br>";
	}
	return;
} 

function catchEnter(e) 
{
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code==13) {
		verify();
	}
}

function select_lang(e) 
{
	for(i=0;i<document.getElementById("ulg").options.length;i++)
	{
		if(document.getElementById("ulg")[i].value == e)
		{
			document.getElementById("ulg")[i].selected = "1";	
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function openFrameless(){

var windowW=400 // wide
var windowH=500 // high

// set the screen position where the popup should appear

var windowX = (screen.width-windowW)/2;
var windowY = (screen.height-windowH)/2;

// set the url of the page to show in the popup

var urlPop = "pictcha-help.html"

// set the title of the page

var title =  "PICTCHA Form Spam Protection - HELP"

// set this to true if the popup should close
// upon leaving the launching page; else, false

var autoclose = true

	s = "width="+windowW+",height="+windowH;
var beIE = document.all?true:false
  if (beIE){
    NFW = window.open("","popFrameless",'toolbar=0,status=0,filemenu=0,scrolling=0,left=0,top=0,'+s) 
    NFW.blur()
    window.focus()       
    NFW.resizeTo(windowW,windowH)
    NFW.moveTo(windowX,windowY)
    var frameString=""+
"<html>"+
"<head>"+
"<title>"+title+"</title>"+
"</head>"+
"<frameset rows='*,0' framespacing=0 border=0 frameborder=0>"+
"<frame name='top' src='"+urlPop+"' scrolling=auto>"+
"<frame name='bottom' src='about:blank' scrolling='no'>"+
"</frameset>"+
"</html>"
    NFW.document.open();
    NFW.document.write(frameString)
    NFW.document.close()
  } else {
    NFW=window.open(urlPop,"popFrameless",'toolbar=0,status=0,filemenu=0,scrolling=0,left=0,top=0,'+s) 
    NFW.blur()
    window.focus() 
    NFW.resizeTo(windowW,windowH)
    NFW.moveTo(windowX,windowY)
  }   
  NFW.focus()   
  if (autoclose){
    window.onunload = function(){NFW.close()}
  }
}