/* **** BEGIN LICENSE BLOCK *****
 * "The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Kirys Dynaxaj Classes.
 *
 * The Initial Developer of the Original Code is Federico Improta.
 * Portions created by Initial Developer are Copyright (C) Initial Developer.
 *  All Rights Reserved.
 *
 * Contributor(s): .
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/* **** BEGIN INFO BLOCK *****
   Kirys Dynaxaj Classes
   $Id: kitdynamic.js,v 1.20 2007/06/01 14:40:52 Kirys Exp $
 * ***** END INFO BLOCK ***** */


//Common functions
var isIE = document.all?true:false

var browsertype=0;
if (isIE) {
   browsertype=1; //Internet explorer
} else if(true) {
}

function isDynaxajCompatible(){
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        return true;
        } else if (window.ActiveXObject) { // IE
            try {
                _request = new ActiveXObject("Msxml2.XMLHTTP");
                return true;
            } catch (e) {
                try {
                    _request = new ActiveXObject("Microsoft.XMLHTTP");
                    return true;
                } catch (e) {return false;}
            }
        }
return false;
}

function extractElementText (node) {
   if (node.nodeType == 3) {
     return node.nodeValue;
   }
   else {
     var result = '';
     for (var i = 0; i < node.childNodes.length; i++) {
       result += extractElementText(node.childNodes[i]);
     }
     return result;
   }

}

function getTagText(owner,name){
   if ((owner!=null)&& (owner.getElementsByTagName(name)) && (owner.getElementsByTagName(name).item(0))) {
      return extractElementText(owner.getElementsByTagName(name).item(0));
   } else {
      return '';
   }
}

function _doNothing(){
//nothing
}

/* XML Parser*/

function CrossXml(x){
    if (x!=null){
       this.source=x;
       }else{
       alert('Invalid source xml');
       }
}

CrossXml.prototype.getDocument=function(){
    return new CrossXmlDocument(this.source.documentElement);
}

function CrossXmlDocument (item){
    this.owner=item;
}

CrossXmlDocument.prototype.getTags=function(name){
   if ((this.owner!=null)&& (this.owner.getElementsByTagName(name))) {
      return new CrossXmlItemsList(this.owner.getElementsByTagName(name));
   }else{
      return null;
   }
}

CrossXmlDocument.prototype.getFirstTag=function(name){
   if ((this.owner!=null)&& (this.owner.getElementsByTagName(name)) && (this.owner.getElementsByTagName(name).item(0))) {
      return new CrossXmlItem(this.owner.getElementsByTagName(name).item(0));
   }else{
      return null;
   }
}

CrossXmlDocument.prototype.existsTag=function(name){
   return ((this.getTags(name)!=null)&&  (this.getTags(name).getCount()>0));
}

function CrossXmlItemsList(list){
    this.owner=list;
}

CrossXmlItemsList.prototype.getItem=function(i){
   if (this.owner!=null) {
      return new CrossXmlItem(this.owner.item(i));
   } else {
      return 0;
   }
}

CrossXmlItemsList.prototype.getCount=function(){
   if (this.owner!=null) {
      return this.owner.length;
   } else {
      return 0;
   }
}

function CrossXmlItem(item){
    this.owner=item;
}

CrossXmlItem.prototype.getFirstTag=function(name){
   if ((this.owner!=null)&& (this.owner.getElementsByTagName(name)) && (this.owner.getElementsByTagName(name).item(0))) {
      return new CrosXmlItem(this.owner.getElementsByTagName(name).item(0));
   }else{
      return null;
   }
}

CrossXmlItem.prototype.getTags=function(name){
   if ((this.owner!=null)&& (this.owner.getElementsByTagName(name))) {
      return new CrossXmlItemsList(this.owner.getElementsByTagName(name));
   }else{
      return null;
   }
}

CrossXmlItem.prototype.getText=function(){
   if ((this.owner!=null)&&(this.owner)) {
      return extractElementText(this.owner);
   } else {
      return '';
   }
}

CrossXmlItem.prototype.existsTag=function(name){
   return ((this.getTags(name)!=null)&&  (this.getTags(name).getCount>0));
}

CrossXmlItem.prototype.getAttribute=function(name){
   if (this.owner.getAttribute(name)){
      return this.owner.getAttribute(name);
   } else {
      return null;
   }
}




/* Ajax Classes */

function CrossAjax(){
    this.status=0;
    this.requester = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        this._request = new XMLHttpRequest();
        if (this._request.overrideMimeType) {
           this._request.overrideMimeType('text/xml');
        }
        } else if (window.ActiveXObject) { // IE
            try {
                this._request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    this._request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    this.onComplete=_doNothing();
    var _this=this;
    this._request.onreadystatechange=function(){
         _this._onAlert();
    }
}

CrossAjax.prototype.getXml=function(){
    if (this.status==1){
        return this._request.responseXML;
        } else {
        return null;
        }
}

CrossAjax.prototype.getXmlDocument=function(){
    if (this.status==1){
        return new CrossXml(this.getXml());
        } else {
        return null;
        }
}

CrossAjax.prototype.getText=function(){
    if (this.status==1){
        return this._request.responseText;
        } else {
        return '';
        }
}


CrossAjax.prototype._onAlert=function(){
    if (this._request.readyState == 4) {
        if (this._request.status == 200) {
                this.status=1;
                this.onComplete();
            } else {
                this.status=0;
            }
        }
}

CrossAjax.prototype.getFile=function(url){
    /*    if (!isIE) {
             this._request.overrideMimeType('text/html');
             }*/
        this._request.open('GET', url, true);
        this._request.send(null);
}

CrossAjax.prototype.postRequest=function(url,data){
        this._request.open('POST', url, true);
        this._request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        this._request.send(data);
}

CrossAjax.prototype.postXMLRequest=function(url,data){
        this._request.open('POST', url, true);
        this._request.setRequestHeader('Content-Type','text/xml');
        this._request.send(data);
}

/* */
function _getLayerByName(layername){
        if (document.getElementById)
			return document.getElementById(layername);
		else if (document.all)
			return document.all[layername];
                else return false;

}



/*******************************************************************************
 **** Layer class
 **************************/

function CrossLayer(){
    this.isValid=false;
}

function CrossLayer(layername){
    this.isValid=true;
    this.layer=_getLayerByName(layername);
    if (this.layer == false)
        this.isValid=false
}

CrossLayer.prototype.style=function(){
    return this.layer.style;
}

CrossLayer.prototype.setText=function(text){
    this.layer.innerHTML=text;
}

CrossLayer.prototype.show=function(){
    this.layer.style.visibility="visible";
}

CrossLayer.prototype.hide=function(){
    this.layer.style.visibility="hidden";
}

CrossLayer.prototype.moveTo=function(x,y){

    this.layer.style.left=x+"px";
    this.layer.style.top=y+"px";
}

CrossLayer.prototype.moveBy=function(dx,dy){
    this.layer.style.left+=dx;
    this.layer.style.top+=dy;
}

CrossLayer.prototype.getX=function(){
    return this.layer.offsetLeft;
}

CrossLayer.prototype.getY=function(){
    return this.layer.offsetTop;
}

CrossLayer.prototype.getHeight=function(){
    return this.layer.offsetHeight;
}

CrossLayer.prototype.getWidth=function(){
    return this.layer.offsetWidth;
}

