var WEB_SERVICE_PATH='http://www.notequalsoft.com/web/services/captcha/';
var WEB_SERVICE_URI=WEB_SERVICE_PATH+'Captcha.asmx';
function doWebServiceInit(operation, parameters) {
		var url=WEB_SERVICE_URI+'?op='+operation;
		var xmlhttp=null;
		if (window.XMLHttpRequest) {
			xmlhttp=new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
		}
		if (typeof(xmlhttp)=='object') {
			try {
				var soapMessage='<?xml version="1.0" encoding="utf-8"?>\n'
					+'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
					+' <soap:Body>'
					+'  <'+operation+' xmlns="'+WEB_SERVICE_PATH+'">'
					+parameters
					+'  </'+operation+'>'
					+' </soap:Body>'
					+'</soap:Envelope>';
				xmlhttp.onreadystatechange=function () {doWebServiceProcess(xmlhttp, url, operation)};
				xmlhttp.open('POST', WEB_SERVICE_URI, true);
				xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
				xmlhttp.setRequestHeader('Content-Length', soapMessage.length);
				xmlhttp.setRequestHeader('SOAPAction', WEB_SERVICE_PATH+operation);
				xmlhttp.send(soapMessage);
				return true;
			}
			catch(e) {
				raiseError('Unable to initialize CAPTCHA service.');
			}
		}
		return false;
}
function doWebServiceProcess(xmlhttp, url, operation) {
	var field1=null;
	var field2=null;
	if (xmlhttp.readyState==4) {
		if (((xmlhttp.status==200) || (xmlhttp.status==0)) && (xmlhttp.responseXML!=null)) {
			switch (operation) {
				case 'Generate':
					field1=document.getElementById('captchaimage');
					field2=document.getElementById('captchapublickey');
					if ((field1==null) || (field2==null)) raiseError('CAPTCHA fields not rendered.');
					else {
						field1.src=xmlhttp.responseXML.selectSingleNode('//ImageUri').text;
						field2.value=xmlhttp.responseXML.selectSingleNode('//PublicKey').text;
						return true;
					}
					break;
				case 'Validate':
					alert(xmlhttp.responseXML.selectSingleNode('//Valid').text);
					alert(xmlhttp.responseXML.selectSingleNode('//Success').text);
					break;
			}
		}
		else if ((xmlhttp.status==0) || (xmlhttp.status==2) || (xmlhttp.status==404) || (xmlhttp.status==410)) raiseError('Resource not found: \''+url+'\'');
		else if ((xmlhttp.status==400) || (xmlhttp.status==405) || (xmlhttp.status==406) || (xmlhttp.status==412) || (xmlhttp.status==414) || (xmlhttp.status==415)) raiseError('Invalid resource request: \''+url+'\'');
		else if ((xmlhttp.status==401) || (xmlhttp.status==403) || (xmlhttp.status==407)) raiseError('Access denied: \''+url+'\'');
		else if (xmlhttp.status>=500) raiseError('Resource unavailable: \''+url+'\'');
		else raiseError('HTTP error '+xmlhttp.status.toString()+': \''+url+'\'');
	}
	return false;
}
function raiseError(message) {
	alert(message);
	return true;
}
function renderCaptchaField(width, height) {
	if (width<100) width=100;
	else if (width>640) width=640;
	if (height<25) height=25;
	else if (height>400) height=400;
	if (width<(height*2)) {
		width=height*2;
		if (width>640) width=640;
	}
	document.write('<img id="captchaimage" src="'+WEB_SERVICE_PATH+'blank.gif" width="'+width.toString()+'" height="'+height.toString()+'" alt="" />');
	document.write('<input id="captchapublickey" type="hidden" name="captchapublickey" value="" />');
	return doWebServiceInit('Generate','<Width>'+width.toString()+'</Width><Height>'+height.toString()+'</Height>');
}
function rerenderCaptchaField() {
	var field1=document.getElementById('captchaimage');
	var field2=document.getElementById('captchapublickey');
	if ((field1==null) || (field2==null)) return false;
	else return doWebServiceInit('Generate','<Width>'+field1.width.toString()+'</Width><Height>'+field1.height.toString()+'</Height>');
}
