/*
    SimpleAjax - its a simple Ajax Class for handling various scripts (PHP, ASP, ASP.NET, PERL ...) response.
    Copyright (C) 2008 Simpletags.org
    Author: Marcin Rosinski

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	contact: marcin@simpletags.org
	web: www.simpletags.org
*/


var SimpleAjax = {

	_xmlHttp_Simple: "",
	_temp_simple_response_id: "",
	_temp_simple_loading: "",
	_temp_simple_loading_id: "",

	//POST
	postRequest: function(url, request, response_id, loading, loading_id){
		
		this._xmlHttp_Simple=this.GetXmlHttpObject();
		if (this._xmlHttp_Simple==null){
		  alert ("Your browser does not support AJAX!");
		  return;
		} 
		  
		
		this._temp_simple_response_id = response_id;
		this._temp_simple_loading = loading;
		this._temp_simple_loading_id = loading_id;
		
		url=url+"?sid="+Math.random();
		
		if(response_id){
			this._xmlHttp_Simple.onreadystatechange=this.stateChanged;
		}
		
		this._xmlHttp_Simple.open("POST",url,true);
		this._xmlHttp_Simple.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    this._xmlHttp_Simple.setRequestHeader("Content-length", request.length);
	    this._xmlHttp_Simple.setRequestHeader("Connection", "close");
		this._xmlHttp_Simple.send(request);
	},
	
	//POST
	stateChanged: function(){ 
		
		if(SimpleAjax._temp_simple_loading != false){document.getElementById(SimpleAjax._temp_simple_loading_id).innerHTML=SimpleAjax._temp_simple_loading;}
	
		if (SimpleAjax._xmlHttp_Simple.readyState==4){ 
			
			document.getElementById(SimpleAjax._temp_simple_response_id).innerHTML=SimpleAjax._xmlHttp_Simple.responseText;		
		}
		
	},
	
	//POST
	postRequest_: function(url, request, response_id, loading, loading_id){
			
		this._xmlHttp_Simple=this.GetXmlHttpObject();
		if (this._xmlHttp_Simple==null){
		  alert ("Your browser does not support AJAX!");
		  return;
		} 
			  
			
		this._temp_simple_response_id = response_id;
		this._temp_simple_loading = loading;
		this._temp_simple_loading_id = loading_id;
			
		url=url+"?sid="+Math.random();
			
		if(response_id){
			this._xmlHttp_Simple.onreadystatechange=this.stateChanged_;
		}
			//alert(request);
		this._xmlHttp_Simple.open("POST",url,true);
		this._xmlHttp_Simple.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    this._xmlHttp_Simple.setRequestHeader("Content-length", request.length);
		    this._xmlHttp_Simple.setRequestHeader("Connection", "close");
		this._xmlHttp_Simple.send(request);
	},
		
	//POST
	stateChanged_: function(){ 
			
			if(SimpleAjax._temp_simple_loading != false){document.getElementById(SimpleAjax._temp_simple_loading_id).innerHTML=SimpleAjax._temp_simple_loading;}
		
			if (SimpleAjax._xmlHttp_Simple.readyState==4){ 
					//alert(SimpleAjax._xmlHttp_Simple.responseText);
				var json_obj = eval( '(' + SimpleAjax._xmlHttp_Simple.responseText + ')' );
						
				if(json_obj.result != 0){
					document.getElementById(SimpleAjax._temp_simple_response_id).innerHTML=json_obj.html;
					pageTracker._trackPageview('/signedup');
				}else{
					document.getElementById(SimpleAjax._temp_simple_loading_id).innerHTML='<input type="button" onclick="DreamJob.submitDetails();" value="Find my Dream Job!" class="button" id="find" />';
					alert(json_obj.error);
				}
			}
			
	},
	
	//XMLHTTPOBJECT
	GetXmlHttpObject: function(){
		var xmlHttp=null;
		try{xmlHttp=new XMLHttpRequest();}
		catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}
	return xmlHttp;
	}

}