/* --------------------------------------------------------------- */
// Global js functions
//
// var author = Remi Palard;
// author.email = remi.palard@gmail.com;
//
/* --------------------------------------------------------------- */

// Debug 
var DebugMode = false;
var isPosted = false;
var _xmlReq;
var _rsID;

// Preload
var preload_image_object = new Image();
var preload_image_url = new Array();
preload_image_url[0] = '/static/gfx/icons/icon_plus.gif';
preload_image_url[1] = '/static/gfx/logos/ac_vsmall.gif';
preload_image_url[2] = '/static/gfx/logos/sa_vsmall.gif';
for(var i=0; i<preload_image_url.length; i++) preload_image_object.src = preload_image_url[i];

//alert(preload_image_object.complete);


/* --------------------------------------------------------------- */
// Global functions
/* --------------------------------------------------------------- */

function debug(aMsg) {
	if (DebugMode && detectBrowser() == 'firefox') {
		setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0); 
	}
}

/* --------------------------------------------------------------- */

function findPos(obj) {
    var pos = new Object();
    pos.left = 0;
    pos.top = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            pos.left += obj.offsetLeft;
            pos.top  += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x) {
        pos.left = obj.x;
        pos.top = obj.y;
    }
    return pos;

}

/* --------------------------------------------------------------- */

function show(id) {
    if ($(id) != null) $(id).style.display = 'inline';
}

/* --------------------------------------------------------------- */

function hide(id) {
    if ($(id) != null) $(id).style.display = 'none';
}

/* --------------------------------------------------------------- */
// Bazar
/* --------------------------------------------------------------- */

function mySnap(number,snap) {
    return Math.round(number/snap)*snap;
}

/* --------------------------------------------------------------- */

function firstNonTextChild(parent) {
    var node = parent.firstChild;
    while (node.nodeType == 3) {
        node = node.nextSibling;
    }
    return node;
}

/* --------------------------------------------------------------- */
// Browser Detect
/* --------------------------------------------------------------- */

function detectBrowser() {

    var detect = navigator.userAgent.toLowerCase();
    var browser;

    function checkIt(string) {
        return detect.indexOf(string) + 1;
    }

    if (checkIt('konqueror')) browser = "konqueror";
    else if (checkIt('firefox')) browser = "firefox"
    else if (checkIt('safari')) browser = "safari"
    else if (checkIt('omniweb')) browser = "omniweb"
    else if (checkIt('opera')) browser = "opera"
    else if (checkIt('webtv')) browser = "webtv";
    else if (checkIt('icab')) browser = "icab"
    else if (checkIt('msie')) browser = "msie"
    else if (!checkIt('compatible')) browser = "netscape"
    else browser = "E.T.";

    // Version IE
    var v = navigator.appVersion;
    if ( parseInt(v) == 4 && !checkIt("msie 6.") && browser == "msie") browser = "msie5x";
    return browser;
}

/* --------------------------------------------------------------- */
// Get Scroll
/* --------------------------------------------------------------- */

function getScrollXY() {
    var scroll = new Object();
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement &&
      ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  scroll.y = scrOfY;
  scroll.x = scrOfX;
  return scroll;
}
function helpLayer(url){
	
	show('helpLayer');
	show('helpLayerIframe');
	centerLayer('helpLayer');
	centerLayer('helpLayerIframe');
	include('helpContent',url,true);
}
function closeHelp(){
	hide('helpLayer');
	hide('helpLayerIframe');
}
function centerLayer(id){

	var w,h,el;
	
	w = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
	h = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight); 

	el = document.getElementById(id);
	
	el.style.left = ((w-el.offsetWidth)/2) + (document.documentElement.scrollLeft || document.body.scrollLeft) + "px";
	el.style.top = ((h-el.offsetHeight)/2) + (document.documentElement.scrollTop || document.body.scrollTop) + "px";
	
}
/* --------------------------------------------------------------- */
// Ajax function
/* --------------------------------------------------------------- */
function include(id,url,check) {
    var div;
    
    _rsID = id;


    var paramString = '';

    // Get Ajax Object
    if(window.XMLHttpRequest) {
        _xmlReq = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {
        try {
            _xmlReq = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            _xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    else {
        // FATAL ERROR: Cannot create XMLHTTP instance!
        $(_rsID).innerHTML = '<h3 class="tangerine">FATAL ERROR: Cannot create XMLHTTP instance!</h3>';
        return;
    }

    // Status
    _xmlReq.onreadystatechange = getIncludeResponse;

    // Start the request
    _xmlReq.open('POST', url, true);
    _xmlReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    _xmlReq.setRequestHeader("Content-length", paramString.length);
    _xmlReq.setRequestHeader("Connection", "close");
    _xmlReq.send(paramString);

}
function getIncludeResponse() {
    if(_xmlReq.readyState == 4) {
        // Clean response
        if(_xmlReq.status == 200) {
            var responseString = _xmlReq.responseText;

            // Clean up
            delete _xmlReq;
            
            $(_rsID).innerHTML = responseString;
        }
        else {
            // Server Error
            $(_rsID).innerHTML = '<h3 class="tangerine">FATAL ERROR: Unable to receive Ajax response (status=' + _xmlReq.status + ')</h3>';
            return;
        }
    }
}

/* --------------------------------------------------------------- */

function dialog(bodyOnclick) {

    var pos,x,y,ph,c,t,o,backup;
    this.o = bodyOnclick;
    
    // Hide dialog box
    dialog.prototype.hide = function () {
        hide(this.db);
        if(this.o) document.body.onclick = this.backup;
    };

    // Show dialog box
     dialog.prototype.show = function (launcher,dialogBox,text,corrX,corrY, id) {
 	
 	if (id) {
 		$('selectedTrip').value = id;	
 	}
 	
        // id
        this.ph = $(launcher);
        this.db = $(dialogBox);
        this.t = $(dialogBox + '_text');

        // Set Text
        if (text != null && text != '') this.t.innerHTML = text;

        // Get Object Height
        //this.db.style.visibility = 'hidden';
        this.top = '-1000px';
        show(this.db);
        var H = this.db.offsetHeight;

        // Position
        this.pos = findPos(this.ph);
        // Adjustment
        if (corrX != null) this.x = parseInt(corrX) + this.pos.left;
        else this.x = this.pos.left;

        if (corrY != null) this.y = parseInt(corrY) + this.pos.top - H ;
        else this.y = this.pos.top - H;

        // Set style
        this.db.style.position = 'absolute';
        this.db.style.top = this.y + 'px';
        this.db.style.left = this.x + 'px';

        // Onclick on body
        if (this.o) {
            this.backup = document.body.onclick;
            document.body.onclick = this.hide.bindAsEventListener(this);
        }

        // Show box
        this.db.style.visibility = '';
    };
}

/* --------------------------------------------------------------- */
// Auto Tab
/* --------------------------------------------------------------- */
// -- peter.van.broekhoven (6.12.03)
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
    }

    function containsElement(arr, ele) {
        var found = false, index = 0;
        while(!found && index < arr.length)
            if(arr[index] == ele)
                found = true;
            else
                index++;
        return found;
    }

    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
            if (input.form[i] == input)index = i;
            else i++;
        return index;
    }

    return true;
}

/* --------------------------------------------------------------- */