
function ChessStartPosition()
{
	return "rnbqkbnrpppppppp                                PPPPPPPPRNBQKBNR";
}

var pChessURL = "http://www.danmarinescu.com/WebServices/ChessCGIServer.exe/soap/IBorlandChess";
var g_Position = ChessStartPosition();
var g_WhiteMovesNext = true;

var g_PositionHistory = new Array();



/* ==============================================================
   ============================================================== */
function DoSOAP( i_DIVToSet )
{
	var pISOAP = new SOAP( pChessURL, "XML_GetNextMove" );
	pISOAP.AddParameterAsString( "Position", ChessStartPosition() );
	pISOAP.AddParameterAsBool( "WhiteMovesNext", g_WhiteMovesNext );
	pISOAP.AddParameterAsInt( "SearchDepth", 3 );
	pISOAP.Invoke( i_DIVToSet );
}

function DoGoogle( i_DIVToSet )
{
	var GoogleKey = "FWLQhEa2Nz+cyG9uLbNZO2qJtUiK7pHd";
	var GoogleURL = "http://api.google.com/search/beta2";
	var soapaction = "GoogleSearchAction";
	var operation = "doSpellingSuggestion";
	var namespace = "urn:GoogleSearch";
	
	var pISOAP = new SOAP( GoogleURL, "doSpellingSuggestion", namespace, soapaction );
	pISOAP.AddParameterAsString( "key", GoogleKey );
	pISOAP.AddParameterAsString( "phrase", "pankake" );
	pISOAP.Invoke( i_DIVToSet );
}

function DoGoogleSearch( i_DIVToSet, i_sQuery )
{
	var GoogleKey = "FWLQhEa2Nz+cyG9uLbNZO2qJtUiK7pHd";
	var GoogleURL = "http://api.google.com/search/beta2";
	var soapaction = "GoogleSearchAction";
	var operation = "doGoogleSearch";
	var namespace = "urn:GoogleSearch";
	
	var pISOAP = new SOAP( GoogleURL, operation, namespace, soapaction );
	pISOAP.AddParameterAsString( "key", GoogleKey );
	pISOAP.AddParameterAsString( "q", i_sQuery );
	
	pISOAP.AddParameterAsInt( "start", 0 );
	pISOAP.AddParameterAsInt( "maxResults", 10 );
	pISOAP.AddParameterAsBool( "filter", false );
	pISOAP.AddParameterAsString( "restrict", "" );
	pISOAP.AddParameterAsBool( "safeSearch", false );
	pISOAP.AddParameterAsString( "lr", "" );
	pISOAP.AddParameterAsString( "ie", "latin1" );
	pISOAP.AddParameterAsString( "oe", "latin1" );

	pISOAP.Invoke( "google" );
}
/* ==============================================================

   JSSOAP, Version 0.1 Alpha
   Last Modified: 06-29-2002
   Copyright (C) 2002 by Suite75 - Rotterdam - The Netherlands 
   Author:	Tim Knip

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version
   2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this program; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   Contact Information:

   Suite75 - Tim Knip
   t.knip@suite75.com
   http://www.suite75.com/
   
   description: 
   - create a simple SOAP-message and send/load it using XMLHTTP
  
   ============================================================== */

SOAP.prototype.Create               = SOAP_Create;
SOAP.prototype.Invoke               = SOAP_Invoke;
SOAP.prototype.AddParameterAsString = SOAP_AddParameterAsString;
SOAP.prototype.AddParameterAsInt    = SOAP_AddParameterAsInt;
SOAP.prototype.AddParameterAsBool   = SOAP_AddParameterAsBool;
SOAP.prototype.Error                = SOAP_Error;

// constructor...
function SOAP( i_URL, i_MethodName, i_NameSpace, i_SoapAction )
{
	try
	{
		if( typeof i_URL == "undefined" ){ alert( "SOAP(): URL not set!" ); return; };
		if( typeof i_MethodName == "undefined" ){ alert( "SOAP(): MethodName not set!" ); return; };
		// create response and request xml-objects
		this.Request = null;
		this.Response = "";
		this.methodRequest = new ActiveXObject("MSXML.DOMDocument");
		this.methodResponse = new ActiveXObject("MSXML.DOMDocument");
		
		this.URL = i_URL;
		this.methodName = (typeof i_NameSpace == "undefined") ? i_MethodName : "ns1:" + i_MethodName;
		if( typeof i_SoapAction != "undefined" )
			this.SoapAction = i_SoapAction;
		
		this.elRequest = this.methodRequest.createElement( this.methodName );	
		if( typeof i_NameSpace != "undefined" )
			this.NameSpace = i_NameSpace;
	}
	catch(e)
	{
		alert("Error in SOAP() :\n"+e.description);
	}
}
//--------------------------------------------------------------------
// send the SOAP-request using XMLHTTP
function SOAP_Invoke( i_DIVToSet )
{
	if( !this.Create() ) return false;
	
	var sRequest = this.Request.xml;

	// try to create XMLHTTP-object to send request....
	try	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try	{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			this.Error(2);
			return false;
		}
	}
	// what to do after recieving the response?
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4) 
		{
			// TODO: place your code here
			if( i_DIVToSet == "google" )
			{
				var doc = new ActiveXObject("MSXML.DOMDocument");
				doc.async = false;
				
				if( doc.loadXML( xmlhttp.responseText ) )
				{
					var item, str = "";
					var ResultsCount = doc.selectSingleNode( "//estimatedTotalResultsCount" );
					var lst = doc.selectNodes( "//item" );
					
					str += "results: (" + ResultsCount.text + ")" + "<br>";
					
					while( item = lst.nextNode() )
					{
						var nod_url = item.selectSingleNode( "./URL" );
						var nod_title = item.selectSingleNode( "./title" );
						
						if( nod_url && nod_title )
						{
								
							str += '<a href="'+nod_url.text+'" class="realsmall">';
							/*
							str += " onmouseover="';
							str += "window.status='" + s + "';return true;";
							str += '" onmouseout="' + "window.status='';return true;";
							*/
							str += nod_title.text + '</a><br><br>';
						}
					}
					document.all[i_DIVToSet].innerHTML = str;
				}
			}
			else
				document.all[i_DIVToSet].innerHTML = xmlhttp.responseText;
		}
	}	
	// will fail with standard IE-security settings...
	try{
		xmlhttp.open("POST", this.URL, true );	
	}catch(e){
		this.Error(0);	
		return false;
	}
	// but we are happy if we have adjusted them!
	// time to send the request....
	//if( typeof this.SoapAction != "undefined" )
	//{
		//xmlhttp.setRequestHeader('SOAPAction', this.SoapAction );
	//}

	// xmlhttp.setRequestHeader("Man", "POST http://api.google.com/search/beta2 HTTP/1.1")
	// xmlhttp.setRequestHeader("MessageType", "CALL")
	xmlhttp.setRequestHeader("Content-Type", "text/xml")

	xmlhttp.send( sRequest );
}
//--------------------------------------------------------------------
// show some errors
function SOAP_Error( idx )
{
	var msg = new Array()	
	msg.push("Connection to webservice failed...\n\nPlease adjust Internet Explorer's security-settings.\n"+	
			"------------------------------------------------------------\n"+
			"HOWTO:\nIn IE's menubar, goto 'Tools->Internet Options'\n"+
			"- select tab 'Security'\n"+
			"- select the 'Internet zone'\n- press button 'Custom Level'\n"+
			"- goto option 'Access data sources across domains'\n"+
			"- select option 'Enable'\n"+
			"- click 'Ok', you will be prompted whether you are sure to change the security-settings\n"+
			"- press 'OK' once more...\n"+
			"------------------------------------------------------------\n"+
			"After adjusting you should be able to connect to the webservice."												
			);
	msg.push( "Empty request, aborting..." );	
	msg.push( "Unable to create XMLHTTP-object!" );
	alert(msg[idx]);
	delete msg;
}			
//--------------------------------------------------------------------
// add a string-param
function SOAP_AddParameterAsString( i_paramName, i_Value )
{
	var attrName = "xsi:type";
	var attrVal =  "xsd:string";
	
	var el = this.methodRequest.createElement( i_paramName );
	el.setAttribute(attrName, attrVal);
	el.text = i_Value;
	
	this.elRequest.appendChild(el);
}
//--------------------------------------------------------------------
// add a int-param
function SOAP_AddParameterAsInt( i_paramName, i_Value )
{
	var attrName = "xsi:type";
	var attrVal =  "xsd:int";
	
	var el = this.methodRequest.createElement( i_paramName );
	el.setAttribute(attrName, attrVal);
	el.text = i_Value;
	
	this.elRequest.appendChild(el);
}
//--------------------------------------------------------------------
// add a boolean-param
function SOAP_AddParameterAsBool( i_paramName, i_Value )
{
	var attrName = "xsi:type";
	var attrVal =  "xsd:boolean";
	
	var el = this.methodRequest.createElement( i_paramName );
	el.setAttribute(attrName, attrVal);
	el.text = i_Value;
	
	this.elRequest.appendChild(el);
}
//--------------------------------------------------------------------
// Create the SOAP-request
function SOAP_Create()
{
    var elEnvelope;
    var elBody;
   	var elMethod, elResult;
   	
   	if( !this.elRequest || !this.methodRequest ) {this.Error(1); return false;}
   	
   	this.methodRequest.appendChild( this.elRequest );
   	
    this.Request = new ActiveXObject("MSXML.DOMDocument");
    
    elEnvelope = this.Request.createElement("SOAP-ENV:Envelope");
    elBody = this.Request.createElement("SOAP-ENV:Body");

    elEnvelope.setAttribute( "xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    elEnvelope.setAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema");
    elEnvelope.setAttribute("xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/");
    elEnvelope.setAttribute("xmlns:SOAP-ENC","http://schemas.xmlsoap.org/soap/encoding/");
    elEnvelope.setAttribute("SOAP-ENV:encodingStyle","http://schemas.xmlsoap.org/soap/encoding/");

	if( typeof this.methodRequest != "undefined" )
	{
	    if( typeof this.NameSpace != "undefined" )
			this.methodRequest.documentElement.setAttribute("xmlns:ns1", this.NameSpace);
			
		elBody.appendChild(this.methodRequest.documentElement);
	}
    elEnvelope.appendChild( elBody );
    this.Request.appendChild( elEnvelope );
    
    return true;
}
