/**
 * Copyright 2005 Zervaas Enterprises (www.zervaas.com.au)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * History:
 * 	Steve Burke
 * 	Nov 15, 2005
 * 		- Created DebugMessage()
 */

var debugModeOn = false;

function DebugMessage(message) {
    if (debugModeOn) {
        alert(message);
    }
}

function ajaxac_createXMLHttp() {
    var ret = null;
    try {
        ret = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
        try {
            ret = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (ee) {
            ret = null;
        }
    }
    if (!ret && typeof XMLHttpRequest != 'undefined')
        ret = new XMLHttpRequest();

    return ret;
}

function ajaxac_attachWidget(hook, id) {
    if (hook.length > 0 && id.length > 0) {
        evalStr = hook + " = document.getElementById('" + id + "');";
//        DebugMessage('attaching widget: ' + evalStr);
        eval(evalStr);
    }
}

function ajaxac_receivejsarray(code) {
    //DebugMessage('receiving JS array: ' + code);
		if ( typeof code != 'string' ){
			return code;
		}
    eval('var ret = ' + code);
    return ret;
}

function ajaxac_countdowntimer(cmd, ms) {
    this.cmd = cmd;
    this.ms = ms;
    this.tp = 0;
}

ajaxac_countdowntimer.prototype.start = function() {
    if (this.tp > 0) {
        this.reset();
    }
    this.tp = window.setTimeout(this.cmd, this.ms);
}

ajaxac_countdowntimer.prototype.reset = function() {
    if (this.tp > 0) {
        window.clearTimeout(this.tp);
    }
    this.tp = 0;
}

function trim(str) {
    return str.replace(/^(\s+)?(\S*)(\s+)?$/, '$2');
}

function ltrim(str) {
    return str.replace(/^\s*/, '');
}

function rtrim(str) {
    return str.replace(/\s*$/, '');
}

function delay(milliseconds) {
    var then, now;
    then = new Date().getTime();
    now = then;
    while ((now - then) < milliseconds) {
        now = new Date().getTime();
    }
}

function ajaxac_getkeycode(e) {
    if (document.layers) {
        return e.which;
    }
    else if (document.all) {
        return event.keyCode;
    }
    else if (document.getElementById) {
        return e.keyCode;
    }
    return 0;
}

function ajaxac_xmlrpc_formatrequest(methodName, methodParameters){

	// Use the XMLRPCMessage class to format the request XML.
	// The XMLRPCMessage libraries must be included seperately.
	// @see http://www.scottandrew.com/xml-rpc

	// TODO: we could use the jsolait xmlrpc module to do this and
	// eliminate the duplicate functionality of XMLRPCMessage.

	var message = new XMLRPCMessage(methodName);
	message.addParameter(methodParameters);
	return message.xml();
}

function ajaxac_xmlrpc_receivepayload(responseText) {

	// try to parse the response (fault responses will throw an exception)
	try {
		
		// extract the payload
		xmlPayload = ajaxac_xmlrpc_extractpayload(responseText);

		//create a DOMParser
		//var xmlDOMParser = new DOMParser();
		//create new document from string
		//var xmlDoc = xmlDOMParser.parseFromString(xmlPayload, "text/xml");

		var xmlDoc;
	
		
		try {
			// Mozilla, create a new DOMParser
			var xmlDOMParser = new DOMParser();
			xmlDoc = xmlDOMParser.parseFromString(xmlPayload, "text/xml");
		} catch(e){
			// Internet Explorer, create a new XML document using ActiveX
			// and use loadXML as a DOM parser.
			try {
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async="false";
				xmlDoc.loadXML(xmlPayload);
			} catch(e){
				throw e;
			}
		}
		
	} catch(e) {

		/// TODO: deal with exceptions
		if ( typeof e == 'object' && e.faultCode ){
			throw e;
		} else {
			alert(e);
		}

	}

	return xmlDoc;

}

function ajaxac_xmlrpc_extractpayload(responseText){

	// use the jsolait xmlrpc module to parse the response text xml.
	// The jsolait 2.0 libraries must be included seperately.
	// @see http://jsolait.net/

	// try to import the xmlrpc module
	var xmlrpcMod=null;
	try{
		var xmlrpcMod = imprt("xmlrpc");
	}catch(e){
		//alert(e);
		throw "importing of xmlrpc module failed.";
	}

	// try to parse the response (fault responses will throw an exception)
	try {

		var xmlPayload = xmlrpcMod.unmarshall(responseText);

	} catch(e) {

		if ( e.constructor == xmlrpcMod.Fault ){
			//alert(e.faultCode + ' ' + e.faultString);
			throw e;
		} else {
			alert(e);
		}

	}

	return xmlPayload;

}

function ajaxac_xmldoc2string(xmlDocObject){

	// use the jsolait xmlrpc module to parse the response text xml.
	// The jsolait 2.0 libraries must be included seperately.
	// @see http://jsolait.net/
	var xmlMod=null;
	try{
		var xmlMod = imprt("xml");
	} catch(e){
		//alert(e);
		throw "importing of xml module failed.";
	}

	// try to parse the response (fault responses will throw an exception)
	try {
		
		var xmlString = xmlMod.node2XML(xmlDocObject.documentElement);
		
	} catch(e) {
		throw e;
	}

	return xmlString;

}
