var http = createRequestObject();

/* Instantiates the XML-HTTP request object for AJAX */
function createRequestObject() {
	
    var ro; // The Request Object
    var browser = navigator.appName;
    
    if(browser == "Microsoft Internet Explorer"){ // This is IE...
        ro = new ActiveXObject("Microsoft.XMLHTTP"); // ... Get the XMLHTTP ActiveX object
    }else{ // Not IE...
        ro = new XMLHttpRequest(); // Instantiate this instead
    }
    return ro;
}

/* Sends an XML request to a script (action) */
function sndReq(action,method) {
		
		if (!method)
			method = 'get';
		
		// Send the request
    http.open(method, action);
    
    // Pass handling of the response to a context-specific function
    http.onreadystatechange = handleResponse;
    http.send(null);
}

/* Sends an XML request to a script (action) */
function sndPostReq(URL,data) {
		
		// Send the request
    http.open("POST", URL, true);
    
    // Pass handling of the response to a context-specific function
    http.onreadystatechange = handlePostResponse;
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
    http.send(data);
}

function handlePostResponse() {
	
	  if(http.readyState == 4){
    
    	var response = http.responseText;
    
      var preview = window.open('','preview','menubar=0,toolbar=0,resizable=1,scrollbars=1');
      preview.document.write(response);
      //preview.close;
    }		
}

function previewPage() {
	
	var _title = document.forms("theform").elements("title").value;
	var body = document.forms("theform").elements("body").value;

	sndPostReq('index.php','preview=true&body='+escape(body)+'&title='+escape(_title)+'');
}
