/**
 * HTMLRequest
 * -----------
 *
 * Home of this script is http://blog.underconstruction.hu/ and
 * can be downloaded from http://blog.underconstruction.hu/scripts/HTTPRequest/HTTPRequest.js
 *
 * @author Adam Brunner <adambrunner-at-underconstruction.hu>
 * @copyright 2005-2006. underconstruction.hu
 * @version 0.14
 * @licence Creative Commons 2.5 Attribution-NoDerivs http://creativecommons.org/licenses/by-nd/2.5/
 *
 * Attribute me (Adam Brunner @ blog.underconstruction.hu) in the impressum, or other place
 * on the site and don't remove this comment!
 *
 * Parameters
 * ----------
 *
 * readonly string version
 *        Returns the actual version of the class.
 * string returnMode
 *        Can be Text, XML or JSON.
 * boolean debug
 *        If true, an alert will be shown is some error occures.
 * function startConnectionFunction
 *        Set it to a function and will be evulate just at the start of the 'send()' method
 * function endConnectionFunction
 *        Set it to a function and will be evulate just at the end of the 'send()' method
 *
 * Methods
 * -------
 *
 * boolean tryIt ( )
 *        Returns TRUE if the XMLHTTPRequest can be used or not.
 * string setMethod ( string method )
 *        Sets the method of the post to POST or GET. Default is GET.
 *        Returns the previous state on set, or if no parameter is given.
 * string setAction ( string action )
 *        Sets the URL to specified, where the data will be sent. Returns previous action.
 * int add ( string name, string value )
 *        Adds a parameter to the data pool, with given name and value.
 *        Return -1 on succes, 1 if no name was given.
 * array send ( )
 *        Returns an array, with two element in it.
 *        First with an error code:
 *                -1 SUCCESS
 *                 1 ERROR     Error creating the connection
 *                 2 ERROR     An error has occured calling the external site
 *                 3 ERROR     Bad Ready State
 *                 4 ERROR     The server respond with a bad status code
 *                 5 ERROR     Unknown readyState code returned
 *                 6 ERROR     Unknown readyState code returned
 *                 7 ERROR     Returned invalid JSON object
 *        Second is the response from the server (if was)
 *
 * int addArray ( array parameterArray )
 *        Adds all of values in the given assoc array. The post variable name will be the key
 *        of the assoc array.
 *        Return -1 on succes, 1 if no array (object) was passed.
 *
 * Example
 * -------
 *
 *     var myRequest = new HTTPRequest();
 *     if(myRequest.tryIt()) {
 *         myRequest.debug = true; //comment this line, to not see debug errors
 *         myRequest.setMethod('POST'); //default is GET
 *         myRequest.setAction('http://www.example.com/index.php'); //the URL where to send the data
 *         myRequest.add('variableName1','value1');
 *         myRequest.add('variableName2','value2');
 *         myRequest.returnMode = 'text'; //default is XML
 *         var response = myRequest.send(); //after the transaction, the 'response' variable will hold the ERROR Code and the XML or Text in an array
 *     } else {
 *         alert("Can't use HTTPRequest!");
 *     }
 *
 * @todo Support Array as value to send it.
 */

if(typeof(browser) == 'undefined') {
	var browser = {
		isIE: navigator.userAgent.toLowerCase().indexOf("msie")>-1 && navigator.userAgent.toLowerCase().indexOf("opera")==-1,
		isOpera: navigator.userAgent.toLowerCase().indexOf("opera")>-1,
		isGecko: navigator.userAgent.toLowerCase().indexOf("gecko")>-1
	};
}

var HTTPRequest = function() {};

/**
 * @return object Returns an HTTPRequest object
 */
HTTPRequest.prototype = {
	version: '0.14',
	returnMode: 'XML',
	debug: false,
	startConnectionFunction: function() {},
	endConnectionFunction: function() {},

	tryIt: function() {
		var x = this.send(true);
		return (x[0] == -1);
	},

	setMethod: function(_method) {
		var _ret = this._method;
		switch(_method.toUpperCase()) {
			case 'POST':
			case 'GET':
				this._method = _method.toUpperCase();
				break;
			default:
				_ret = this._method;
				break;
		}
		return _ret;
	},

	setAction: function(_action) {
		if(_action == '') {
			this._error('No action was specified!');
			return 1;
		}
		_ret = this._action
		this._action = _action;
		return _ret;
	},

	add: function(_name,_value) {
		if(!_name) { return 1; }

		this._params[this._params.length] = {
			name: _name,
			value: _value
		};
		return -1;
	},

	addArray: function(_parameterArray) {
		if(typeof(_parameterArray) != 'object') { return 1; }
		for(var i in _parameterArray) {
			this.add(i, _parameterArray[i]);
		}
		return -1;
	},

	send: function() {
		var onlyTry = arguments.length > 0?arguments[0]:false
		var ret = [-1,''];

		var httpRequest;
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				httpRequest = null;
			}
		}

		if(!httpRequest && typeof XMLHttpRequest != "undefined") {
			httpRequest= new XMLHttpRequest();
		}

		if(!httpRequest) {
			this._error('Error creating the connection!');
			ret[0] = 1;
			return ret;
		}

		if(onlyTry) {
			return ret;
		}

		if(typeof(this.startConnectionFunction) == 'function') {
			this.startConnectionFunction();
		}

		try {
			var txt = [];
			for(var i in this._params) {
				txt[txt.length] = escape(this._params[i].name)+'='+escape(this._params[i].value);
			}

			this._host = this._action+(this._action.indexOf('?')==-1?'?':'&')+txt.join('&');
			this._realHost = this._host.substr(0,this._host.indexOf('?'));
			this._post = this._host.substr(this._host.indexOf('?')+1);

			switch(this._method) {
        case 'POST':


					httpRequest.open("POST", this._realHost, false, null, null);
					if(!browser.isOpera) {

						// Why Opera don't know this?
						httpRequest.setRequestHeader("content-type","application/x-www-form-urlencoded");

					}
					httpRequest.send(this._post);
					break;
				default:
					httpRequest.open("GET", this._realHost+'?'+this._post, false, null, null);
					httpRequest.send('');
					break;
			}

		} catch (e) {
			this._error('An error has occured calling the external site: '+e);
			ret[0] = 2;
			return ret;
		}

		switch(httpRequest.readyState) {
			case 1,2,3:
				this._error('Bad Ready State: '+httpRequest.status);
				ret[0] = 3;
				return ret;
				break;
			case 4:
				if(httpRequest.status != 200) {
					this._error('The server respond with a bad status code: '+httpRequest.status);
					ret[0] = 4;
					return ret;
				} else {
					switch(this.returnMode.toUpperCase()) {
						case 'JSON':

						  try {
								ret[1] = eval(httpRequest.responseText.replace(/\r|\n|\r\n/g,""));
								
							} catch(e) {
								ret[1] = null;
								ret[0] = 7;
							}
							break;
						case 'TEXT':
							ret[1] = httpRequest.responseText;
							
							break;
						case 'XML':
						default:
							ret[1] = httpRequest.responseXML;
							break;
					}
					this._reset();

					if(typeof(this.startConnectionFunction) == 'function') {
						this.endConnectionFunction();
					}
				}
				break;
			default:
				this._error('Unknown readyState code returned!');
				ret[0] = 5;
				return ret;
				break;
		}

		return ret;
	},

	_params: [],
	_action: null,
	_method: 'GET',
	_host: '',
	_realHost: '',
	_post: '',

	_error: function(errMsg) {
		if(this.debug) { alert(errMsg); }
		return;
	},

	_reset: function() {
		this._params = [];
		return -1;
	}
}
function addEvent(obj, evType, fn){
//	alert(obj);
 if ( obj != null && obj.addEventListener){
   obj.addEventListener(evType, fn, false);
   return true;
 } else if (obj.attachEvent){
   var r = obj.attachEvent("on"+evType, fn);
   return r;
 } else {
   return false;
 }
}
function setLastPosts(){
  var myRequest = new HTTPRequest();
  if(myRequest.tryIt()) {
//       myRequest.debug = true; //comment this line, to not see debug errors
//if on bys site:
 			myRequest.setAction('http://www.contrarianeffect.com/reach_blog.php'); //the URL where to send the data
//if on ntserver:
//			myRequest.setAction('http://81.22.144.20:8099/michaelport/reach_blog.php'); //the URL where to send the data

       myRequest.returnMode = 'JSON'; //default is XML
      var response = myRequest.send(); //after the transaction, the 'response' variable will hold the ERROR Code and the XML or Text in an array

    try{
    //alert(response[1][0].Ahref);
      document.getElementById('entry0').innerHTML = '<span><a href="'+response[1][0].Ahref+'" target="_new">'+response[1][0].Abody+'...</a></span> ('+response[1][0].Atitle +', ' +response[1][0].Adate+')' ;
      document.getElementById('entry1').innerHTML = '<span><a href="'+response[1][0].Bhref+'" target="_new">'+response[1][0].Bbody+'...</a></span> ('+response[1][0].Btitle +', ' +response[1][0].Bdate+')' ;
      document.getElementById('entry2').innerHTML = '<span><a href="'+response[1][0].Chref+'" target="_new">'+response[1][0].Cbody+'...</a></span> ('+response[1][0].Ctitle +', ' +response[1][0].Cdate+')' ;
//      document.getElementById('entry0').innerHTML = '<span><a href="'+response[1][0].href+'">'+response[1][0].body+'...</a></span> ('+response[1][0].title +', ' +response[1][0].date+')' ;
//      document.getElementById('entry1').innerHTML = '<span><a href="'+response[1][1].href+'">'+response[1][1].body+'...</a></span> ('+response[1][1].title +', ' +response[1][1].date+')' ;
//     document.getElementById('entry2').innerHTML = '<span><a href="'+response[1][2].href+'">'+response[1][2].body+'...</a></span> ('+response[1][2].title +', ' +response[1][2].date+')' ;

    }catch(_e){
      document.getElementById('entry0').firstChild.innerHTML = 'the blogsite is currently unreachable';
    }

  } else {
      alert("Can't use HTTPRequest!");
  }
  if(document.getElementById("submitbutton"))addEvent(document.getElementById("submitbutton"),'click',validateFLGSW);	
}
function validateFLGSW(){
	var myRequest = new HTTPRequest();
	if(myRequest.tryIt()) {
		myRequest.setAction('http://www.bookyourselfsolid.com/flgswchk.php?Secret='+document.getElementById("Secret").value); //the URL where to send the data flgswchk.php
//alert(document.getElementById("Secret").value);
//		myRequest.setAction('http://ntserver/michaelport/flgswchk.php?secret='+document.getElementById("secretword").value); //the URL where to send the data flgswchk.php
		myRequest.returnMode = 'TEXT'; 
		var response = myRequest.send();
	
	//	alert(response[1]);
	//
		if(document.getElementById("Secret").value == 'booked solid' || document.getElementById("Secret").value == 'booked solid.' || document.getElementById("Secret").value == 'BOOKED SOLID'){
		//if(response[1]==1){
//			alert('http://ntserver/michaelport/flgswchk.php?secret='+document.getElementById("secretword").value);
//			alert(response[1]);
			document.cosmin.submit();
		}else{
			alert('wrong secret code');
		}
	}
	return false;	
}


addEvent(window,'load',setLastPosts);
