//
//	XMLmind Spell-Checker Client-Server Kit
//	client bootstrap
//	Copyright © Xavier C. FRANC, XMLmind 2001-2002 -- All rights reserved --
//
//	This software is the proprietary information of 
//	 XMLmind/Pixware and Xavier C. FRANC.
//	Use is subject to license terms.
// 

// parameterization:
var sc_waitMessage = "processing...";

// pops up the spell-checker dialog
function xsc_popup( form )
{
	this.scanForm( form );
	// create the popup:
	var w = 306, h = 370;
	//	alert(""+browserType);
	if (browserType == "Netscape v4") {
	   h = 490; w = 360;
	}
	else if (browserType == "Netscape v5")
	   h = 340;
	var x = self.screenX + (self.outerWidth - w) / 2;
	var y = self.screenY + (self.outerHeight - h);
	var popup = window.open("", "xsc_popup",
	   "dependent=1,resizable=1,innerWidth="+w+",width="+w+",height="+h+",innerHeight="+h+",left="+x+",top="+y);
	this.startPump( popup.document );
	popup.focus();
}

function xsc_scanForm( form )
{
	if (browserStatus != "OK" && !browserReported) {
	 var yourbr = "the browser you are using ("+browserType + ")";
	 var work = "work with this application"
	 switch(browserStatus) {
	 case "buggy":
		alert("WARNING: there are known flaws in "+yourbr+
			",\nthat probably prevent it to "+work+".");
		break;
	 case "untested":
		alert("WARNING: "+yourbr+"\nhas never been tested with this application.\n"+
			"We cannot guarantee it will "+work+".");
		browserReported = true;
		break;
	 default:
		alert("WARNING: "+yourbr+" has "+browserStatus+".\nWe cannot guarantee it will "+work+".");
		browserReported = true;
	 }
	}
	// dictionary specified inside form ?
	var formLang = null;
	if (form.xsc_dictionary)
	    if (form.xsc_dictionary.type == "select-one")
		// form.xsc_dictionary.value does not work with broken old browsers
		 formLang = form.xsc_dictionary.options[form.xsc_dictionary.selectedIndex].value;
	    else formLang = form.xsc_dictionary.value;
	this.dictionary = formLang || this.defaultDictionary;

	this.cur_fields = new Array();
	var afields = this.cur_fields;
	// fields defined inside form ?
	var fields = form.xsc_fields ? form.xsc_fields.value : null;
	if (fields == null || fields == "" || fields == "*" ) {
	    // take all fields of type TEXTAREA or INPUT/TEXT:
	    for (var e = 0; e < form.elements.length; e++) {
		var elem = form.elements[e];
		if (elem.type == "textarea" || elem.type == "text")
		    afields[afields.length] = elem;
	    }
	}
	else {
	    // check and get fields listed in xsc_fields:
	    var fieldNames = fields.split(" ");
	    for (var n = 0; n < fieldNames.length; n++) {
		var name = fieldNames[n];
		if (fieldNames[n] != "")
		    if (form.elements[name]) {
			afields[afields.length] = form.elements[name];
		    }
		    else alert("no such field '" + name + "' in form " + form);
	    }
	}
	self.scBoot = this;
}

function xsc_startPump( doc )
{
	// do these twisted things in order to make a POST request
	doc.writeln("<html><head><meta http-equiv='content-type' content='text/html; charset=ISO-8859-1'></head>");
	doc.writeln("<body onLoad='document.forms[0].submit()'>");
	doc.writeln("<h3><img src='/xsccs/processing.gif'> " + sc_waitMessage + "</h3>");

	doc.writeln("<form method='POST' action='" + this.url + "'>");
	doc.writeln("<input type='hidden' name='lang' value='" + this.dictionary +"'>");
	doc.writeln("<input type='hidden' name='popup' value='" + this.popupClient +"'>");
	var fields = this.cur_fields;
	doc.writeln("<input type='hidden' name='f_cnt' value='"+ fields.length +"'>");
	for(var f = 0; f < fields.length; f++)
	doc.writeln("<input type='hidden' name='tf_"+ f +"' value=''>");
	if (this.optionSet) {
		var ops = "";
		for (var op in this.options) {
			ops += op + "=" + this.options[op] + ";";
		}
		doc.writeln("<input type='hidden' name='options' value='"+ ops +"'>");
	}
	doc.writeln(unescape("<input type='hidden' name='utf' value='%c2%a9xf'>"));
	doc.writeln("</form><s"+"cript><!-"+"-");
	for(var f = 0; f < fields.length; f++)
	doc.writeln(" document.forms[0].tf_"+f+".value = opener.scBoot.getField("+f+");");
	doc.writeln("//-"+"-></" + "script></" + "body>");
	doc.close();

	//doc.forms[0].submit(); // does not work on some browsers
}

function SpellBootstrap( dictionary, url )
{
	this.defaultDictionary = dictionary || "en";
	this.url = url || "xsccs_server1.jsp";
	this.popup = xsc_popup;
	this.scanForm = xsc_scanForm;
	this.startPump = xsc_startPump;

	this.getField = function (index) { return this.cur_fields[index].value; }
	this.setField = function (index, value) { this.cur_fields[index].value = value; }

	this.setURL = function (url) { this.url = url; } 
	this.setDictionary = function (lg) { this.defaultDictionary = lg; } 
	this.getLanguage = function () { return this.dictionary; }

	this.popupClient = "xsccs_client1_%L.html";
	this.options = new Object();
	this.options["azcheck.IgnoreCase"] = false;
	this.options["azcheck.IgnoreMixedCase"] = false;
	this.options["azcheck.IgnoreDigits"] = true;
	this.options["azcheck.IgnoreDuplicates"] = false;
	this.options["azcheck.IgnoreURL"] = true;
	this.options["azcheck.CheckPunctuation"] = false;
	this.options["azcheck.AutoReplace"] = true;
	this.options["azcheck.AllowCompound"] = true;
	this.options["azcheck.AllowPrefixes"] = true;
	this.options["azcheck.AllowFileExtensions"] = true;
	this.optionSet = false;
	this.setOption = function (option, value) {
		var azoption = "azcheck."+option;
		this.options[azoption] = value; this.optionSet = true;
	} 
	this.getOption = function (option) { return this.options[option]; } 
}

//-------- create default client bootstrap:

var xsc = new SpellBootstrap();

//-------- browser detection (adaptation of code by Peter-Paul Koch)

var userAgent = navigator.userAgent.toLowerCase();
var browserType = "Unknown", browserVersion = "?", browserStatus = "untested";
var browserReported = false;

if (checkBrowser('konqueror'))	{ 
	browserType = "Konqueror"; if (browserVersion <= 2) browserStatus = "buggy";
}
else if(checkBrowser('omniweb')){ browserType = "OmniWeb"; }
else if(checkBrowser('opera'))	{ browserType = "Opera"; browserStatus = "buggy";}
else if(checkBrowser('webtv'))	{ browserType = "WebTV"; }
else if(checkBrowser('icab'))	{ browserType = "iCab"; }
else if(checkBrowser('msie'))	{ browserType = "IE"; browserStatus = "OK"; }
else if(!checkBrowser('compatible')) {
	browserType = "Netscape"
	browserVersion = userAgent.charAt(8);
	browserStatus = browserVersion >= 5 ? "OK" : "display problems";
}
browserType += " v"+ browserVersion;

function checkBrowser(string) {
	var place = userAgent.indexOf(string) + 1;
	if (place != 0)
		browserVersion = userAgent.charAt(place + string.length);
	return place;
}


