
var FL = {
  url: '',               //locatie waar data vandaan komt (altijd relatief ivm security warnings)
  container: null,       //div-container waar data getoond moet worden
  connector: false,      //xmlhttp connector
  connectMethod: 'GET',  //get or post data
  sendParameters: null,  //send data via the connector?
  responseType: 'text',  //use xml data from url or plain text
  responseHandler: null, //user specified responseHandler
  confdiv_width: 300,    //width of div that show confirmation message from store action
  confdiv_height: 150,   //height of div that show confirmation message from store action
  mouseX: 0,             //horizontal mouse position
  mouseY: 0,             //vertical mouse position
  rollX: 0,              //horizontal scroll position
  rollY: 0,              //vertical scroll position
  
  create: function() {
    try {
      FL.connector = new XMLHttpRequest();
    } catch(w3c) {
      try {
        FL.connector = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(msie) {
        try {
          FL.connector = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(msie_old) {
          //throw exception xmlhttp not possible not supported
          FL.show_error( 'xmlhttp not supported' );
          return false;
        }
      }
    }  
  },
  
  get_content: function() {
    if( !FL.container ) {
      if( FL.handle.arguments.length <= 3 )
        FL.container = FL.getObject( FL.handle.arguments[2] );
      else
        FL.show_error( 'Too many arguments, get_content can only fill one field' );
    }
    FL.connectMethod = 'GET';
    FL.connect();
    FL.connector.onreadystatechange = function() {
      if(FL.connector.readyState == 4 ) {
        if( FL.connector.status != 200 && FL.connector.status != 0 ) {
          //throw exception file not found
          FL.show_error( 'Target not found' );
          return false;
        }
        if( FL.responseHandler == null ) {
          if( FL.responseType == 'xml' ) {
            FL.show_xml( FL.connector.responseXML );
          } else if( FL.responseType == 'text' ) {
            FL.show_text( FL.connector.responseText );
          } else {
            //throw exception nothing to give back (type missing)
            FL.show_error( 'responseType is empty' );
            return false;
          }
        } else {
          if( FL.responseType == 'xml' ) {
            eval( FL.responseHandler + '( FL.connector.responseXML )' );  
          } else
            eval( FL.responseHandler + '( "' + FL.connector.responseText + '" )' ); 
        }
      }
    }
      
  },
    
  store_content: function() {
    FL.connectMethod = 'POST';
    var args = FL.handle.arguments;
    var querystr = '';
    for( var i = 2; i < args.length; i++ ) {
      myValue = '';
      if( FL.getObject( args[i] ).type == 'select-one' ) {
        myValue = FL.getObject( args[i] ).options[FL.getObject( args[i] ).selectedIndex].value;
      } else
        myValue = FL.getObject( args[i] ).value;
      
      querystr += args[i] + '=' + encodeURIComponent( myValue );
      if( i < (args.length - 1 ) )
        querystr += '&';
    }
    FL.sendParameters = querystr;
    
    FL.connect();
    FL.connector.onreadystatechange = function() {
      if(FL.connector.readyState == 4 ) {
        if( FL.connector.status != 200 && FL.connector.status != 0 ) {
          //throw exception file not found
          FL.show_error( 'Target not found' );
          return false;
        }
        if( FL.responseHandler == null ) {
          if( FL.responseType == 'xml' ) {
            FL.show_xml_msg( FL.connector.responseXML );
          } else if( FL.responseType == 'text' ) {
            FL.show_text_msg( FL.connector.responseText );
          } else {
            //throw exception nothing to give back (type missing)
            FL.show_error( 'responseType is empty' );
            return false;
          }
        } else {
        	if( FL.responseType == 'xml' ) {
            eval( FL.responseHandler + '( ' + FL.connector.responseXML + ' )' );  
          } else
            eval( FL.responseHandler + '( "' + FL.connector.responseText + '" )' ); 
        }
      }
    }
  },
  
  connect: function() {
    FL.create()
    try {
      if( FL.connector ) {
        FL.connector.open(FL.connectMethod, FL.url, true);
		    if( FL.connectMethod == 'POST' )
		      FL.connector.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    FL.connector.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');  //prevent caching
        FL.connector.send( FL.sendParameters );        
      } else {
        FL.show_error( 'No connector available' );
        return false;
      }
    } catch(ex) {
      //throw exception connector not found
      FL.show_error( 'FL.connector niet aanwezig' );
      return false;
    }
  },
  
  handle: function() {
    //argument list with todo, url, multiple fields
    var args = FL.handle.arguments;
    todo = args[0];
    FL.url = args[1];

    switch (todo){
    case 'retrieve':
      FL.get_content();
      break;
    case 'store':
      FL.store_content();
      break;
    }
  },
  
  show_xml: function( xml ) {
    
  },
  
  show_text: function( text ) {
    FL.container.innerHTML = text;
  },
  
  show_error: function( error ) {
    alert( error );
  },
  
  show_xml_msg: function( xml ) {
    //display xml confirmation message from store action
    //create div als placeholder for the message
    //switch container with created div to preserve the store-target
    //position and style in your css (#confdiv) or here
  },
  
  show_text_msg: function( text ) {
    //destroy any old ones
    if( FL.getObject( 'confdiv' ) )
      FL.close_conf();
    
    //display text confirmation message from store action
    //create div als placeholder for the message
    //switch container with created div to preserve the store-target
    //position and style in your css (#confdiv) or here
    var confdiv = document.createElement( 'div' );
    confdiv.id = 'confdiv';
    confdiv.style.width = FL.confdiv_width + 'px';
    confdiv.style.Height = FL.confdiv_height + 'px';

		//place our confdiv in the center of the screen
		var x_pos = (window.innerWidth - FL.confdiv_width ) / 2;
		var y_pos = ((window.innerHeight - FL.confdiv_height ) / 2) + 17;  //17 pixels lower, because we'll stick a title bar with close button at the top
	  confdiv.style.left = x_pos + 'px';
		confdiv.style.top  = y_pos + 'px';
    
    closediv = document.createElement( 'div' );
    closediv.id = 'closediv';
    closediv.style.width = FL.confdiv_width + 'px';
    closediv.style.height = '17px';
    closediv.style.left = confdiv.style.left;
    closediv.style.top = ((window.innerHeight - FL.confdiv_height ) / 2) + 'px';
    closediv.innerHTML = '<a href="#" onclick="FL.close_conf();">x</a>';
    
    //finished with target-container, switch
    FL.container = confdiv;
    
    //stick to body
    document.getElementsByTagName( 'body' )[0].appendChild( closediv );
    document.getElementsByTagName( 'body' )[0].appendChild( FL.container );
    
    //use our simple show method
    FL.show_text( text );
  },
  
  close_conf: function() {
    //destroy the conf div and destroy the closediv div
    document.getElementsByTagName("body")[0].removeChild( FL.getObject( 'confdiv' ) );
    document.getElementsByTagName("body")[0].removeChild( FL.getObject( 'closediv' ) );
  },
  
  getObject: function( name ) {
    if (document.getElementById) {
    	obj = document.getElementById(name);
  	} else if (document.all) {
  	  obj = document.all[name];
  	} else if (document.layers) {
     	obj = document.layers[name];
    } 
    if( obj )
      return obj;
  },
  
  getMousePos: function( event ) {
    //get mouse position (pixels from top-left of window)
		if (!event) var event = window.event;
		FL.mouseX = event.clientX;
		FL.mouseY = event.clientY;
		FL.getRoll();
		// Safari fix (takes clientX/Y from start of page)
		if (FL.mouseX > FL.rollX && FL.rollX >= window.innerWidth) FL.mouseX -= FL.rollX;
		if (FL.mouseY > FL.rollY && FL.rollX >= window.innerHeight) FL.mouseY -= FL.rollY;     
  },
  
	getRoll: function() {
		// returns horizontal and vertical scroll motion
		if (isFinite(self.pageYOffset)) {	
		  // DOM style
			FL.rollX = self.pageXOffset;
			FL.rollY = self.pageYOffset;
		} else {
			if (isFinite(document.documentElement && document.documentElement.scrollTop)) {	
			  // IE new style
				FL.rollX = document.documentElement.scrollLeft;
				FL.rollY = document.documentElement.scrollTop;
			} else {
				if (isFinite(document.body.scrollTop)) {	
				  // IE old school
					FL.rollX = document.body.scrollLeft;
					FL.rollY = document.body.scrollTop;
				}
			}
		}
	}
};